blob: b956b968d0af43d68828cdf9fcc3e566525e5802 [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 */
jiabinb4fed192020-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 */
jiabinb4fed192020-09-22 14:45:40 -0700105status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port_v7 *port)
Eric Laurent951f4552014-05-20 10:48:17 -0700106{
jiabinb4fed192020-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 // TODO: implement getAudioPort for mix.
110 return INVALID_OPERATION;
111 }
112 AudioHwDevice* hwDevice = findAudioHwDeviceByModule(port->ext.device.hw_module);
113 if (hwDevice == nullptr) {
114 ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module);
115 return BAD_VALUE;
116 }
117 if (!hwDevice->supportsAudioPatches()) {
118 return INVALID_OPERATION;
119 }
120 return hwDevice->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -0700121}
122
Eric Laurent951f4552014-05-20 10:48:17 -0700123/* Connect a patch between several source and sink ports */
124status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
125 audio_patch_handle_t *handle)
126{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700127 if (handle == NULL || patch == NULL) {
128 return BAD_VALUE;
129 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700130 ALOGV("%s() num_sources %d num_sinks %d handle %d",
131 __func__, patch->num_sources, patch->num_sinks, *handle);
132 status_t status = NO_ERROR;
133 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700134
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700135 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700136 return BAD_VALUE;
137 }
Eric Laurent874c42872014-08-08 15:13:39 -0700138 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
139 // only the audio policy manager can request a patch creation with 2 sources.
140 if (patch->num_sources > 2) {
141 return INVALID_OPERATION;
142 }
Eric Laurent951f4552014-05-20 10:48:17 -0700143
Eric Laurent83b88082014-06-20 18:31:16 -0700144 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700145 auto iter = mPatches.find(*handle);
146 if (iter != mPatches.end()) {
147 ALOGV("%s() removing patch handle %d", __func__, *handle);
148 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700149 // free resources owned by the removed patch if applicable
150 // 1) if a software patch is present, release the playback and capture threads and
151 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700152 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700153 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700154 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700155 // 2) if the new patch and old patch source or sink are devices from different
156 // hw modules, clear the audio HAL patches now because they will not be updated
157 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700158 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700159 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
160 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
161 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
162 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
163 oldPatch.sources[0].ext.device.hw_module !=
164 patch->sources[0].ext.device.hw_module)) {
165 hwModule = oldPatch.sources[0].ext.device.hw_module;
166 } else if (patch->num_sinks == 0 ||
167 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
168 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
169 oldPatch.sinks[0].ext.device.hw_module !=
170 patch->sinks[0].ext.device.hw_module))) {
171 // Note on (patch->num_sinks == 0): this situation should not happen as
172 // these special patches are only created by the policy manager but just
173 // in case, systematically clear the HAL patch.
174 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700175 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700176 hwModule = oldPatch.sinks[0].ext.device.hw_module;
177 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700178 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
179 if (hwDevice != 0) {
180 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700181 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800182 halHandle = removedPatch.mHalHandle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700183 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800184 erasePatch(*handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700185 }
186 }
187
Mikhail Naganovdea53042018-04-26 13:10:21 -0700188 Patch newPatch{*patch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700189 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700190
Eric Laurent951f4552014-05-20 10:48:17 -0700191 switch (patch->sources[0].type) {
192 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700193 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700194 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
195 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700196 status = BAD_VALUE;
197 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700198 }
199 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700200 // support only one sink if connection to a mix or across HW modules
201 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700202 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
203 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700204 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700205 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700206 status = INVALID_OPERATION;
207 goto exit;
208 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700209 // reject connection to different sink types
210 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700211 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700212 status = BAD_VALUE;
213 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700214 }
Eric Laurent951f4552014-05-20 10:48:17 -0700215 }
216
Eric Laurent3bcf8592015-04-03 12:13:24 -0700217 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700218 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700219 // - Device to device AND
220 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700221 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700222 if ((patch->num_sources == 2) ||
223 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
224 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700225 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800226 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
227 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700228 if (patch->num_sources == 2) {
229 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700230 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
231 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700232 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700233 status = INVALID_OPERATION;
234 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700235 }
Eric Laurent83b88082014-06-20 18:31:16 -0700236
237 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700238 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700239 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700240 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700241 status = INVALID_OPERATION;
242 goto exit;
243 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700244 // existing playback thread is reused, so it is not closed when patch is cleared
245 newPatch.mPlayback.setThread(
246 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700247 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700248 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700249 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700250 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
251 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
252 config.sample_rate = patch->sinks[0].sample_rate;
253 }
254 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
255 config.channel_mask = patch->sinks[0].channel_mask;
256 }
257 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
258 config.format = patch->sinks[0].format;
259 }
260 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
261 flags = patch->sinks[0].flags.output;
262 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700263 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800264 patch->sinks[0].ext.device.hw_module,
265 &output,
266 &config,
juyuchen2224c5a2019-01-21 12:00:58 +0800267 outputDevice,
268 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700269 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700270 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
271 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700272 status = NO_MEMORY;
273 goto exit;
274 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700275 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Eric Laurent83b88082014-06-20 18:31:16 -0700276 }
Eric Laurent83b88082014-06-20 18:31:16 -0700277 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700278 String8 address = String8(patch->sources[0].ext.device.address);
279 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700280 // open input stream with source device audio properties if provided or
281 // default to peer output stream properties otherwise.
282 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
283 config.sample_rate = patch->sources[0].sample_rate;
284 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700285 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700286 }
287 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
288 config.channel_mask = patch->sources[0].channel_mask;
289 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700290 config.channel_mask = audio_channel_in_mask_from_count(
291 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700292 }
293 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
294 config.format = patch->sources[0].format;
295 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700296 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700297 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700298 audio_input_flags_t flags =
299 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
300 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700301 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700302 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700303 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700304 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700305 device,
306 address,
307 AUDIO_SOURCE_MIC,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800308 flags,
309 outputDevice,
310 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700311 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700312 thread.get(), config.channel_mask);
313 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700314 status = NO_MEMORY;
315 goto exit;
316 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700317 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700318 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700319 if (status != NO_ERROR) {
320 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700321 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700322 if (audioHwDevice->isInsert()) {
323 insertedModule = audioHwDevice->handle();
324 }
Eric Laurent951f4552014-05-20 10:48:17 -0700325 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700326 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700327 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700328 patch->sinks[0].ext.mix.handle);
329 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700330 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800331 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700332 ALOGW("%s() bad capture I/O handle %d",
333 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800334 status = BAD_VALUE;
335 goto exit;
336 }
Eric Laurent83b88082014-06-20 18:31:16 -0700337 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700338 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800339 if (status == NO_ERROR) {
340 newPatch.setThread(thread);
341 }
342
Eric Laurent526aa572019-01-15 10:54:58 -0800343 // remove stale audio patch with same input as sink if any
344 for (auto& iter : mPatches) {
345 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800346 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800347 break;
348 }
349 }
Eric Laurent83b88082014-06-20 18:31:16 -0700350 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700351 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
352 status = hwDevice->createAudioPatch(patch->num_sources,
353 patch->sources,
354 patch->num_sinks,
355 patch->sinks,
356 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700357 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700358 }
Eric Laurent951f4552014-05-20 10:48:17 -0700359 }
360 } break;
361 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700362 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700363 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700364 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700365 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700366 status = BAD_VALUE;
367 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700368 }
369 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700370 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700371 for (unsigned int i = 0; i < patch->num_sinks; i++) {
372 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700373 ALOGW("%s() invalid sink type %d for mix source",
374 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700375 status = BAD_VALUE;
376 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700377 }
378 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700379 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700380 status = BAD_VALUE;
381 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700382 }
jiabinc52b1ff2019-10-31 17:20:42 -0700383 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
384 patch->sinks[i].ext.device.type);
385 device->setAddress(patch->sinks[i].ext.device.address);
386 device->applyAudioPortConfig(&patch->sinks[i]);
387 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700388 }
Eric Laurent951f4552014-05-20 10:48:17 -0700389 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700390 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700391 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700392 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800393 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700394 ALOGW("%s() bad playback I/O handle %d",
395 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800396 status = BAD_VALUE;
397 goto exit;
398 }
Eric Laurent951f4552014-05-20 10:48:17 -0700399 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700400 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700401 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700402 }
403
Eric Laurent054d9d32015-04-24 08:48:48 -0700404 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800405 if (status == NO_ERROR) {
406 newPatch.setThread(thread);
407 }
Eric Laurent526aa572019-01-15 10:54:58 -0800408
409 // remove stale audio patch with same output as source if any
410 for (auto& iter : mPatches) {
411 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800412 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800413 break;
414 }
415 }
Eric Laurent951f4552014-05-20 10:48:17 -0700416 } break;
417 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700418 status = BAD_VALUE;
419 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700420 }
Eric Laurent83b88082014-06-20 18:31:16 -0700421exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700422 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700423 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700424 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
425 newPatch.mHalHandle = halHandle;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800426 mAudioFlinger.mDeviceEffectManager.createAudioPatch(*handle, newPatch);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700427 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700428 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
429 addSoftwarePatchToInsertedModules(insertedModule, *handle);
430 }
Eric Laurent83b88082014-06-20 18:31:16 -0700431 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700432 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700433 }
434 return status;
435}
436
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700437AudioFlinger::PatchPanel::Patch::~Patch()
438{
439 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
440 mRecord.handle(), mPlayback.handle());
441}
442
Mikhail Naganovdea53042018-04-26 13:10:21 -0700443status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700444{
445 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700446 status_t status = panel->createAudioPatch(
447 PatchBuilder().addSource(mAudioPatch.sources[0]).
448 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
449 mRecord.handlePtr());
Eric Laurent83b88082014-06-20 18:31:16 -0700450 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700451 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700452 return status;
453 }
454
455 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700456 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700457 status = panel->createAudioPatch(
458 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
459 mPlayback.handlePtr());
Eric Laurentd60560a2015-04-10 11:31:20 -0700460 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700461 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700462 return status;
463 }
464 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700465 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700466 }
467
Eric Laurent83b88082014-06-20 18:31:16 -0700468 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700469 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700470 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700471 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
472 uint32_t sampleRate = mPlayback.thread()->sampleRate();
473 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700474
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700475 audio_format_t inputFormat = mRecord.thread()->format();
476 if (!audio_is_linear_pcm(inputFormat)) {
477 // The playbackThread format will say PCM for IEC61937 packetized stream.
478 // Use recordThread format.
479 format = inputFormat;
480 }
481 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
482 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700483 if (sampleRate == mRecord.thread()->sampleRate() &&
484 inChannelMask == mRecord.thread()->channelMask() &&
485 mRecord.thread()->fastTrackAvailable() &&
486 mRecord.thread()->hasFastCapture()) {
487 // Create a fast track if the record thread has fast capture to get better performance.
488 // Only enable fast mode when there is no resample needed.
489 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
490 } else {
491 // Fast mode is not available in this case.
492 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
493 }
Eric Laurent83b88082014-06-20 18:31:16 -0700494
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700495 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
496 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700497 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
498 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
499 // "reuse one existing output mix" case
500 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
501 }
jiabin01c8f562018-07-19 17:47:28 -0700502 if (mPlayback.thread()->hasFastMixer()) {
503 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700504 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700505 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700506 } else {
507 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700508 }
509
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700510 sp<RecordThread::PatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800511 const bool usePassthruPatchRecord =
512 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100513 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
514 const size_t recordFrameCount = mRecord.thread()->frameCount();
515 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800516 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100517 // PassthruPatchRecord producesBufferOnDemand, so use
518 // maximum of playback and record thread framecounts
519 frameCount = std::max(playbackFrameCount, recordFrameCount);
520 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
521 __func__, playbackFrameCount, recordFrameCount, frameCount);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700522 tempRecordTrack = new RecordThread::PassthruPatchRecord(
523 mRecord.thread().get(),
524 sampleRate,
525 inChannelMask,
526 format,
527 frameCount,
528 inputFlags);
529 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100530 // use a pseudo LCM between input and output framecount
531 int playbackShift = __builtin_ctz(playbackFrameCount);
532 int shift = __builtin_ctz(recordFrameCount);
533 if (playbackShift < shift) {
534 shift = playbackShift;
535 }
536 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
537 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
538 __func__, playbackFrameCount, recordFrameCount, frameCount);
539
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700540 tempRecordTrack = new RecordThread::PatchRecord(
541 mRecord.thread().get(),
542 sampleRate,
543 inChannelMask,
544 format,
545 frameCount,
546 nullptr,
547 (size_t)0 /* bufferSize */,
548 inputFlags);
549 }
550 status = mRecord.checkTrack(tempRecordTrack.get());
551 if (status != NO_ERROR) {
552 return status;
553 }
554
Eric Laurent83b88082014-06-20 18:31:16 -0700555 // create a special playback track to render to playback thread.
556 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700557 sp<PlaybackThread::PatchTrack> tempPatchTrack = new PlaybackThread::PatchTrack(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700558 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700559 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700560 sampleRate,
561 outChannelMask,
562 format,
563 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700564 tempRecordTrack->buffer(),
565 tempRecordTrack->bufferSize(),
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700566 outputFlags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700567 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700568 if (status != NO_ERROR) {
569 return status;
570 }
Eric Laurent83b88082014-06-20 18:31:16 -0700571
572 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800573 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
574 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
575 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
576 // is alive. There is no need to hold a reference, and there is no need
577 // to clear it. In fact, since playback stopping is asynchronous, there is
578 // no proper time when clearing could be done.
579 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
580 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700581
582 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700583 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
584 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700585
586 return status;
587}
588
Mikhail Naganovdea53042018-04-26 13:10:21 -0700589void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700590{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700591 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
592 __func__, mRecord.handle(), mPlayback.handle());
593 mRecord.stopTrack();
594 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700595 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700596 mRecord.closeConnections(panel);
597 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700598}
599
Andy Hungc3ab7732018-06-01 13:46:29 -0700600status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
601{
602 if (!isSoftware()) return INVALID_OPERATION;
603
604 auto recordTrack = mRecord.const_track();
605 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
606
607 auto playbackTrack = mPlayback.const_track();
608 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
609
610 // Latency information for tracks may be called without obtaining
611 // the underlying thread lock.
612 //
613 // We use record server latency + playback track latency (generally smaller than the
614 // reverse due to internal biases).
615 //
616 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700617
Andy Hung30282562018-08-08 18:27:03 -0700618 // For PCM tracks get server latency.
619 if (audio_is_linear_pcm(recordTrack->format())) {
620 double recordServerLatencyMs, playbackTrackLatencyMs;
621 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
622 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
623 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
624 return OK;
625 }
626 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700627
Andy Hung30282562018-08-08 18:27:03 -0700628 // See if kernel latencies are available.
629 // If so, do a frame diff and time difference computation to estimate
630 // the total patch latency. This requires that frame counts are reported by the
631 // HAL are matched properly in the case of record overruns and playback underruns.
632 ThreadBase::TrackBase::FrameTime recordFT{}, playFT{};
633 recordTrack->getKernelFrameTime(&recordFT);
634 playbackTrack->getKernelFrameTime(&playFT);
635 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
636 const int64_t frameDiff = recordFT.frames - playFT.frames;
637 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
638
639 // It is possible that the patch track and patch record have a large time disparity because
640 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
641 // time difference based on how often we expect the timestamps to update in normal operation
642 // (typical should be no more than 50 ms).
643 //
644 // If the timestamps aren't sampled close enough, the patch latency is not
645 // considered valid.
646 //
647 // TODO: change this based on more experiments.
648 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
649 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
650 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
651 - timeDiffNs * 1e-6;
652 return OK;
653 }
654 }
655
656 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700657}
658
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700659String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700660{
Andy Hungc3ab7732018-06-01 13:46:29 -0700661 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800662 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
663 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
664 mRecord.const_thread().get(), mPlayback.const_thread().get());
665
666 bool hasSinkDevice =
667 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
668 bool hasSourceDevice =
669 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
670 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
671 hasSinkDevice ? "num sinks" :
672 (hasSourceDevice ? "num sources" : "no devices"),
673 hasSinkDevice ? mAudioPatch.num_sinks :
674 (hasSourceDevice ? mAudioPatch.num_sources : 0),
675 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
676 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700677
678 // add latency if it exists
679 double latencyMs;
680 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700681 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700682 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700683 return result;
684}
685
Eric Laurent951f4552014-05-20 10:48:17 -0700686/* Disconnect a patch */
687status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
688{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700689 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700690 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700691
Mikhail Naganovdea53042018-04-26 13:10:21 -0700692 auto iter = mPatches.find(handle);
693 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700694 return BAD_VALUE;
695 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700696 Patch &removedPatch = iter->second;
697 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700698
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700699 const struct audio_port_config &src = patch.sources[0];
700 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700701 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700702 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
703 if (hwDevice == 0) {
704 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700705 status = BAD_VALUE;
706 break;
707 }
Eric Laurent83b88082014-06-20 18:31:16 -0700708
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700709 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700710 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700711 break;
712 }
713
Mikhail Naganovdea53042018-04-26 13:10:21 -0700714 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
715 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
716 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700717 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700718 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800719 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700720 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800721 status = BAD_VALUE;
722 break;
723 }
Eric Laurent951f4552014-05-20 10:48:17 -0700724 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700725 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700726 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700727 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700728 }
729 } break;
730 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700731 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
732 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700733 status = BAD_VALUE;
734 break;
735 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700736 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700737 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700738 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700739 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800740 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700741 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800742 status = BAD_VALUE;
743 break;
744 }
Eric Laurent951f4552014-05-20 10:48:17 -0700745 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700746 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700747 } break;
748 default:
749 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700750 }
751
Eric Laurentb82e6b72019-11-22 17:25:04 -0800752 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700753 return status;
754}
755
Eric Laurentb82e6b72019-11-22 17:25:04 -0800756void AudioFlinger::PatchPanel::erasePatch(audio_patch_handle_t handle) {
757 mPatches.erase(handle);
758 removeSoftwarePatchFromInsertedModules(handle);
759 mAudioFlinger.mDeviceEffectManager.releaseAudioPatch(handle);
760}
761
Eric Laurent951f4552014-05-20 10:48:17 -0700762/* List connected audio ports and they attributes */
763status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
764 struct audio_patch *patches __unused)
765{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700766 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700767 return NO_ERROR;
768}
769
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700770status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
771 audio_io_handle_t stream,
772 std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const
773{
774 for (const auto& module : mInsertedModules) {
775 if (module.second.streams.count(stream)) {
776 for (const auto& patchHandle : module.second.sw_patches) {
777 const auto& patch_iter = mPatches.find(patchHandle);
778 if (patch_iter != mPatches.end()) {
779 const Patch &patch = patch_iter->second;
780 patches->emplace_back(*this, patchHandle,
781 patch.mPlayback.const_thread()->id(),
782 patch.mRecord.const_thread()->id());
783 } else {
784 ALOGE("Stale patch handle in the cache: %d", patchHandle);
785 }
786 }
787 return OK;
788 }
789 }
790 // The stream is not associated with any of inserted modules.
791 return BAD_VALUE;
792}
793
794void AudioFlinger::PatchPanel::notifyStreamOpened(
795 AudioHwDevice *audioHwDevice, audio_io_handle_t stream)
796{
797 if (audioHwDevice->isInsert()) {
798 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
799 }
800}
801
802void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
803{
804 for (auto& module : mInsertedModules) {
805 module.second.streams.erase(stream);
806 }
807}
808
809AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700810{
811 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
812 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
813 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700814 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700815 return nullptr;
816 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700817 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700818}
819
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700820sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700821{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700822 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
823 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
824}
825
826void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
827 audio_module_handle_t module, audio_patch_handle_t handle)
828{
829 mInsertedModules[module].sw_patches.insert(handle);
830}
831
832void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
833 audio_patch_handle_t handle)
834{
835 for (auto& module : mInsertedModules) {
836 module.second.sw_patches.erase(handle);
837 }
838}
839
840void AudioFlinger::PatchPanel::dump(int fd) const
841{
842 String8 patchPanelDump;
843 const char *indent = " ";
844
Mikhail Naganov201369b2018-05-16 16:52:32 -0700845 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700846 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800847 if (!headerPrinted) {
848 patchPanelDump += "\nPatches:\n";
849 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700850 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800851 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700852 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700853
854 headerPrinted = false;
855 for (const auto& module : mInsertedModules) {
856 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
857 if (!headerPrinted) {
858 patchPanelDump += "\nTracked inserted modules:\n";
859 headerPrinted = true;
860 }
861 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
862 for (const auto& stream : module.second.streams) {
863 moduleDump.appendFormat("%d ", stream);
864 }
865 moduleDump.append("; SW Patches: ");
866 for (const auto& patch : module.second.sw_patches) {
867 moduleDump.appendFormat("%d ", patch);
868 }
869 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
870 }
871 }
872
873 if (!patchPanelDump.isEmpty()) {
874 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700875 }
876}
877
Glenn Kasten63238ef2015-03-02 15:50:29 -0800878} // namespace android