blob: 18cb53b1da3ce1c52fe7b24d517363d4ca686edb [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>
Mikhail Naganovdc769682018-05-04 15:34:08 -070028#include <media/PatchBuilder.h>
Andy Hungab7ef302018-05-15 19:35:29 -070029#include <mediautils/ServiceUtilities.h>
Eric Laurent951f4552014-05-20 10:48:17 -070030
31// ----------------------------------------------------------------------------
32
33// Note: the following macro is used for extremely verbose logging message. In
34// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
35// 0; but one side effect of this is to turn all LOGV's as well. Some messages
36// are so verbose that we want to suppress them even when we have ALOG_ASSERT
37// turned on. Do not uncomment the #def below unless you really know what you
38// are doing and want to see all of the extremely verbose messages.
39//#define VERY_VERY_VERBOSE_LOGGING
40#ifdef VERY_VERY_VERBOSE_LOGGING
41#define ALOGVV ALOGV
42#else
43#define ALOGVV(a...) do { } while(0)
44#endif
45
46namespace android {
47
48/* List connected audio ports and their attributes */
49status_t AudioFlinger::listAudioPorts(unsigned int *num_ports,
50 struct audio_port *ports)
51{
52 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070053 return mPatchPanel.listAudioPorts(num_ports, ports);
Eric Laurent951f4552014-05-20 10:48:17 -070054}
55
56/* Get supported attributes for a given audio port */
57status_t AudioFlinger::getAudioPort(struct audio_port *port)
58{
59 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 */
105status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port *port __unused)
106{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700107 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700108 return NO_ERROR;
109}
110
Eric Laurent951f4552014-05-20 10:48:17 -0700111/* Connect a patch between several source and sink ports */
112status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
113 audio_patch_handle_t *handle)
114{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700115 if (handle == NULL || patch == NULL) {
116 return BAD_VALUE;
117 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700118 ALOGV("%s() num_sources %d num_sinks %d handle %d",
119 __func__, patch->num_sources, patch->num_sinks, *handle);
120 status_t status = NO_ERROR;
121 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700122
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700123 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700124 return BAD_VALUE;
125 }
Eric Laurent874c42872014-08-08 15:13:39 -0700126 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
127 // only the audio policy manager can request a patch creation with 2 sources.
128 if (patch->num_sources > 2) {
129 return INVALID_OPERATION;
130 }
Eric Laurent951f4552014-05-20 10:48:17 -0700131
Eric Laurent83b88082014-06-20 18:31:16 -0700132 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700133 auto iter = mPatches.find(*handle);
134 if (iter != mPatches.end()) {
135 ALOGV("%s() removing patch handle %d", __func__, *handle);
136 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700137 // free resources owned by the removed patch if applicable
138 // 1) if a software patch is present, release the playback and capture threads and
139 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700140 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700141 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700142 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700143 // 2) if the new patch and old patch source or sink are devices from different
144 // hw modules, clear the audio HAL patches now because they will not be updated
145 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700146 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700147 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
148 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
149 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
150 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
151 oldPatch.sources[0].ext.device.hw_module !=
152 patch->sources[0].ext.device.hw_module)) {
153 hwModule = oldPatch.sources[0].ext.device.hw_module;
154 } else if (patch->num_sinks == 0 ||
155 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
156 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
157 oldPatch.sinks[0].ext.device.hw_module !=
158 patch->sinks[0].ext.device.hw_module))) {
159 // Note on (patch->num_sinks == 0): this situation should not happen as
160 // these special patches are only created by the policy manager but just
161 // in case, systematically clear the HAL patch.
162 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700163 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700164 hwModule = oldPatch.sinks[0].ext.device.hw_module;
165 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700166 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
167 if (hwDevice != 0) {
168 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700169 }
170 }
171 mPatches.erase(iter);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700172 removeSoftwarePatchFromInsertedModules(*handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700173 }
174 }
175
Mikhail Naganovdea53042018-04-26 13:10:21 -0700176 Patch newPatch{*patch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700177 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700178
Eric Laurent951f4552014-05-20 10:48:17 -0700179 switch (patch->sources[0].type) {
180 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700181 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700182 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
183 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700184 status = BAD_VALUE;
185 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700186 }
187 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700188 // support only one sink if connection to a mix or across HW modules
189 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700190 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
191 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700192 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700193 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700194 status = INVALID_OPERATION;
195 goto exit;
196 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700197 // reject connection to different sink types
198 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700199 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700200 status = BAD_VALUE;
201 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700202 }
Eric Laurent951f4552014-05-20 10:48:17 -0700203 }
204
Eric Laurent3bcf8592015-04-03 12:13:24 -0700205 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700206 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700207 // - Device to device AND
208 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700209 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700210 if ((patch->num_sources == 2) ||
211 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
212 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700213 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800214 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
215 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700216 if (patch->num_sources == 2) {
217 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700218 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
219 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700220 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700221 status = INVALID_OPERATION;
222 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700223 }
Eric Laurent83b88082014-06-20 18:31:16 -0700224
225 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700226 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700227 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700228 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700229 status = INVALID_OPERATION;
230 goto exit;
231 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700232 // existing playback thread is reused, so it is not closed when patch is cleared
233 newPatch.mPlayback.setThread(
234 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700235 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700236 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700237 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700238 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
239 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
240 config.sample_rate = patch->sinks[0].sample_rate;
241 }
242 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
243 config.channel_mask = patch->sinks[0].channel_mask;
244 }
245 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
246 config.format = patch->sinks[0].format;
247 }
248 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
249 flags = patch->sinks[0].flags.output;
250 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700251 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800252 patch->sinks[0].ext.device.hw_module,
253 &output,
254 &config,
juyuchen2224c5a2019-01-21 12:00:58 +0800255 outputDevice,
256 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700257 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700258 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
259 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700260 status = NO_MEMORY;
261 goto exit;
262 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700263 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Eric Laurent83b88082014-06-20 18:31:16 -0700264 }
Eric Laurent83b88082014-06-20 18:31:16 -0700265 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700266 String8 address = String8(patch->sources[0].ext.device.address);
267 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700268 // open input stream with source device audio properties if provided or
269 // default to peer output stream properties otherwise.
270 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
271 config.sample_rate = patch->sources[0].sample_rate;
272 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700273 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700274 }
275 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
276 config.channel_mask = patch->sources[0].channel_mask;
277 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700278 config.channel_mask = audio_channel_in_mask_from_count(
279 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700280 }
281 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
282 config.format = patch->sources[0].format;
283 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700284 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700285 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700286 audio_input_flags_t flags =
287 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
288 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700289 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700290 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700291 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700292 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700293 device,
294 address,
295 AUDIO_SOURCE_MIC,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800296 flags,
297 outputDevice,
298 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700299 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700300 thread.get(), config.channel_mask);
301 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700302 status = NO_MEMORY;
303 goto exit;
304 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700305 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700306 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700307 if (status != NO_ERROR) {
308 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700309 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700310 if (audioHwDevice->isInsert()) {
311 insertedModule = audioHwDevice->handle();
312 }
Eric Laurent951f4552014-05-20 10:48:17 -0700313 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700314 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700315 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700316 patch->sinks[0].ext.mix.handle);
317 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700318 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800319 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700320 ALOGW("%s() bad capture I/O handle %d",
321 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800322 status = BAD_VALUE;
323 goto exit;
324 }
Eric Laurent83b88082014-06-20 18:31:16 -0700325 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700326 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent526aa572019-01-15 10:54:58 -0800327 // remove stale audio patch with same input as sink if any
328 for (auto& iter : mPatches) {
329 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
330 mPatches.erase(iter.first);
331 break;
332 }
333 }
Eric Laurent83b88082014-06-20 18:31:16 -0700334 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700335 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
336 status = hwDevice->createAudioPatch(patch->num_sources,
337 patch->sources,
338 patch->num_sinks,
339 patch->sinks,
340 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700341 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700342 }
Eric Laurent951f4552014-05-20 10:48:17 -0700343 }
344 } break;
345 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700346 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700347 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700348 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700349 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700350 status = BAD_VALUE;
351 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700352 }
353 // limit to connections between devices and output streams
Eric Laurent054d9d32015-04-24 08:48:48 -0700354 audio_devices_t type = AUDIO_DEVICE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700355 for (unsigned int i = 0; i < patch->num_sinks; i++) {
356 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700357 ALOGW("%s() invalid sink type %d for mix source",
358 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700359 status = BAD_VALUE;
360 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700361 }
362 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700363 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700364 status = BAD_VALUE;
365 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700366 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700367 type |= patch->sinks[i].ext.device.type;
Eric Laurent951f4552014-05-20 10:48:17 -0700368 }
Eric Laurent951f4552014-05-20 10:48:17 -0700369 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700370 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700371 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700372 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800373 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700374 ALOGW("%s() bad playback I/O handle %d",
375 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800376 status = BAD_VALUE;
377 goto exit;
378 }
Eric Laurent951f4552014-05-20 10:48:17 -0700379 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700380 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
Eric Laurent054d9d32015-04-24 08:48:48 -0700381 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -0700382 param.addInt(String8(AudioParameter::keyRouting), (int)type);
Eric Laurent054d9d32015-04-24 08:48:48 -0700383
Mikhail Naganovdea53042018-04-26 13:10:21 -0700384 mAudioFlinger.broacastParametersToRecordThreads_l(param.toString());
Eric Laurent951f4552014-05-20 10:48:17 -0700385 }
386
Eric Laurent054d9d32015-04-24 08:48:48 -0700387 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent526aa572019-01-15 10:54:58 -0800388
389 // remove stale audio patch with same output as source if any
390 for (auto& iter : mPatches) {
391 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id()) {
392 mPatches.erase(iter.first);
393 break;
394 }
395 }
Eric Laurent951f4552014-05-20 10:48:17 -0700396 } break;
397 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700398 status = BAD_VALUE;
399 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700400 }
Eric Laurent83b88082014-06-20 18:31:16 -0700401exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700402 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700403 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700404 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
405 newPatch.mHalHandle = halHandle;
406 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700407 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
408 addSoftwarePatchToInsertedModules(insertedModule, *handle);
409 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700410 ALOGV("%s() added new patch handle %d halHandle %d", __func__, *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700411 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700412 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700413 }
414 return status;
415}
416
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700417AudioFlinger::PatchPanel::Patch::~Patch()
418{
419 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
420 mRecord.handle(), mPlayback.handle());
421}
422
Mikhail Naganovdea53042018-04-26 13:10:21 -0700423status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700424{
425 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700426 status_t status = panel->createAudioPatch(
427 PatchBuilder().addSource(mAudioPatch.sources[0]).
428 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
429 mRecord.handlePtr());
Eric Laurent83b88082014-06-20 18:31:16 -0700430 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700431 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700432 return status;
433 }
434
435 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700436 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700437 status = panel->createAudioPatch(
438 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
439 mPlayback.handlePtr());
Eric Laurentd60560a2015-04-10 11:31:20 -0700440 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700441 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700442 return status;
443 }
444 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700445 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700446 }
447
448 // use a pseudo LCM between input and output framecount
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700449 size_t playbackFrameCount = mPlayback.thread()->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700450 int playbackShift = __builtin_ctz(playbackFrameCount);
jiabin01c8f562018-07-19 17:47:28 -0700451 size_t recordFrameCount = mRecord.thread()->frameCount();
452 int shift = __builtin_ctz(recordFrameCount);
Eric Laurent83b88082014-06-20 18:31:16 -0700453 if (playbackShift < shift) {
454 shift = playbackShift;
455 }
jiabin01c8f562018-07-19 17:47:28 -0700456 size_t frameCount = (playbackFrameCount * recordFrameCount) >> shift;
457 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
458 __func__, playbackFrameCount, recordFrameCount, frameCount);
Eric Laurent83b88082014-06-20 18:31:16 -0700459
460 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700461 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700462 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700463 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
464 uint32_t sampleRate = mPlayback.thread()->sampleRate();
465 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700466
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700467 audio_format_t inputFormat = mRecord.thread()->format();
468 if (!audio_is_linear_pcm(inputFormat)) {
469 // The playbackThread format will say PCM for IEC61937 packetized stream.
470 // Use recordThread format.
471 format = inputFormat;
472 }
473 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
474 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700475 if (sampleRate == mRecord.thread()->sampleRate() &&
476 inChannelMask == mRecord.thread()->channelMask() &&
477 mRecord.thread()->fastTrackAvailable() &&
478 mRecord.thread()->hasFastCapture()) {
479 // Create a fast track if the record thread has fast capture to get better performance.
480 // Only enable fast mode when there is no resample needed.
481 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
482 } else {
483 // Fast mode is not available in this case.
484 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
485 }
Eric Laurent83b88082014-06-20 18:31:16 -0700486
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700487 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
488 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700489 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
490 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
491 // "reuse one existing output mix" case
492 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
493 }
jiabin01c8f562018-07-19 17:47:28 -0700494 if (mPlayback.thread()->hasFastMixer()) {
495 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700496 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700497 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700498 } else {
499 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700500 }
501
Mikhail Naganov48f1d452019-10-01 16:52:14 -0700502 sp<RecordThread::PatchRecord> tempRecordTrack;
503 if ((inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
504 tempRecordTrack = new RecordThread::PassthruPatchRecord(
505 mRecord.thread().get(),
506 sampleRate,
507 inChannelMask,
508 format,
509 frameCount,
510 inputFlags);
511 } else {
512 tempRecordTrack = new RecordThread::PatchRecord(
513 mRecord.thread().get(),
514 sampleRate,
515 inChannelMask,
516 format,
517 frameCount,
518 nullptr,
519 (size_t)0 /* bufferSize */,
520 inputFlags);
521 }
522 status = mRecord.checkTrack(tempRecordTrack.get());
523 if (status != NO_ERROR) {
524 return status;
525 }
526
Eric Laurent83b88082014-06-20 18:31:16 -0700527 // create a special playback track to render to playback thread.
528 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganov48f1d452019-10-01 16:52:14 -0700529 sp<PlaybackThread::PatchTrack> tempPatchTrack = new PlaybackThread::PatchTrack(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700530 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700531 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700532 sampleRate,
533 outChannelMask,
534 format,
535 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700536 tempRecordTrack->buffer(),
537 tempRecordTrack->bufferSize(),
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700538 outputFlags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700539 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700540 if (status != NO_ERROR) {
541 return status;
542 }
Eric Laurent83b88082014-06-20 18:31:16 -0700543
544 // tie playback and record tracks together
Andy Hungabfab202019-03-07 19:45:54 -0800545 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack);
546 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack);
Eric Laurent83b88082014-06-20 18:31:16 -0700547
548 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700549 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
550 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700551
552 return status;
553}
554
Mikhail Naganovdea53042018-04-26 13:10:21 -0700555void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700556{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700557 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
558 __func__, mRecord.handle(), mPlayback.handle());
559 mRecord.stopTrack();
560 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700561 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700562 mRecord.closeConnections(panel);
563 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700564}
565
Andy Hungc3ab7732018-06-01 13:46:29 -0700566status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
567{
568 if (!isSoftware()) return INVALID_OPERATION;
569
570 auto recordTrack = mRecord.const_track();
571 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
572
573 auto playbackTrack = mPlayback.const_track();
574 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
575
576 // Latency information for tracks may be called without obtaining
577 // the underlying thread lock.
578 //
579 // We use record server latency + playback track latency (generally smaller than the
580 // reverse due to internal biases).
581 //
582 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700583
Andy Hung30282562018-08-08 18:27:03 -0700584 // For PCM tracks get server latency.
585 if (audio_is_linear_pcm(recordTrack->format())) {
586 double recordServerLatencyMs, playbackTrackLatencyMs;
587 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
588 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
589 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
590 return OK;
591 }
592 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700593
Andy Hung30282562018-08-08 18:27:03 -0700594 // See if kernel latencies are available.
595 // If so, do a frame diff and time difference computation to estimate
596 // the total patch latency. This requires that frame counts are reported by the
597 // HAL are matched properly in the case of record overruns and playback underruns.
598 ThreadBase::TrackBase::FrameTime recordFT{}, playFT{};
599 recordTrack->getKernelFrameTime(&recordFT);
600 playbackTrack->getKernelFrameTime(&playFT);
601 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
602 const int64_t frameDiff = recordFT.frames - playFT.frames;
603 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
604
605 // It is possible that the patch track and patch record have a large time disparity because
606 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
607 // time difference based on how often we expect the timestamps to update in normal operation
608 // (typical should be no more than 50 ms).
609 //
610 // If the timestamps aren't sampled close enough, the patch latency is not
611 // considered valid.
612 //
613 // TODO: change this based on more experiments.
614 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
615 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
616 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
617 - timeDiffNs * 1e-6;
618 return OK;
619 }
620 }
621
622 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700623}
624
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700625String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700626{
Andy Hungc3ab7732018-06-01 13:46:29 -0700627 // TODO: Consider table dump form for patches, just like tracks.
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700628 String8 result = String8::format("Patch %d: thread %p => thread %p",
629 myHandle, mRecord.const_thread().get(), mPlayback.const_thread().get());
Andy Hungc3ab7732018-06-01 13:46:29 -0700630
631 // add latency if it exists
632 double latencyMs;
633 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700634 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700635 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700636 return result;
637}
638
Eric Laurent951f4552014-05-20 10:48:17 -0700639/* Disconnect a patch */
640status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
641{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700642 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700643 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700644
Mikhail Naganovdea53042018-04-26 13:10:21 -0700645 auto iter = mPatches.find(handle);
646 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700647 return BAD_VALUE;
648 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700649 Patch &removedPatch = iter->second;
650 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700651
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700652 const struct audio_port_config &src = patch.sources[0];
653 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700654 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700655 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
656 if (hwDevice == 0) {
657 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700658 status = BAD_VALUE;
659 break;
660 }
Eric Laurent83b88082014-06-20 18:31:16 -0700661
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700662 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700663 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700664 break;
665 }
666
Mikhail Naganovdea53042018-04-26 13:10:21 -0700667 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
668 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
669 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700670 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700671 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800672 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700673 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800674 status = BAD_VALUE;
675 break;
676 }
Eric Laurent951f4552014-05-20 10:48:17 -0700677 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700678 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700679 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700680 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700681 }
682 } break;
683 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700684 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
685 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700686 status = BAD_VALUE;
687 break;
688 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700689 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700690 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700691 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700692 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800693 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700694 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800695 status = BAD_VALUE;
696 break;
697 }
Eric Laurent951f4552014-05-20 10:48:17 -0700698 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700699 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700700 } break;
701 default:
702 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700703 }
704
Mikhail Naganovdea53042018-04-26 13:10:21 -0700705 mPatches.erase(iter);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700706 removeSoftwarePatchFromInsertedModules(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700707 return status;
708}
709
Eric Laurent951f4552014-05-20 10:48:17 -0700710/* List connected audio ports and they attributes */
711status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
712 struct audio_patch *patches __unused)
713{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700714 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700715 return NO_ERROR;
716}
717
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700718status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
719 audio_io_handle_t stream,
720 std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const
721{
722 for (const auto& module : mInsertedModules) {
723 if (module.second.streams.count(stream)) {
724 for (const auto& patchHandle : module.second.sw_patches) {
725 const auto& patch_iter = mPatches.find(patchHandle);
726 if (patch_iter != mPatches.end()) {
727 const Patch &patch = patch_iter->second;
728 patches->emplace_back(*this, patchHandle,
729 patch.mPlayback.const_thread()->id(),
730 patch.mRecord.const_thread()->id());
731 } else {
732 ALOGE("Stale patch handle in the cache: %d", patchHandle);
733 }
734 }
735 return OK;
736 }
737 }
738 // The stream is not associated with any of inserted modules.
739 return BAD_VALUE;
740}
741
742void AudioFlinger::PatchPanel::notifyStreamOpened(
743 AudioHwDevice *audioHwDevice, audio_io_handle_t stream)
744{
745 if (audioHwDevice->isInsert()) {
746 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
747 }
748}
749
750void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
751{
752 for (auto& module : mInsertedModules) {
753 module.second.streams.erase(stream);
754 }
755}
756
757AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700758{
759 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
760 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
761 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700762 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700763 return nullptr;
764 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700765 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700766}
767
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700768sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700769{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700770 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
771 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
772}
773
774void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
775 audio_module_handle_t module, audio_patch_handle_t handle)
776{
777 mInsertedModules[module].sw_patches.insert(handle);
778}
779
780void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
781 audio_patch_handle_t handle)
782{
783 for (auto& module : mInsertedModules) {
784 module.second.sw_patches.erase(handle);
785 }
786}
787
788void AudioFlinger::PatchPanel::dump(int fd) const
789{
790 String8 patchPanelDump;
791 const char *indent = " ";
792
Mikhail Naganov201369b2018-05-16 16:52:32 -0700793 // Only dump software patches.
794 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700795 for (const auto& iter : mPatches) {
Mikhail Naganov201369b2018-05-16 16:52:32 -0700796 if (iter.second.isSoftware()) {
797 if (!headerPrinted) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700798 patchPanelDump += "\nSoftware patches:\n";
Mikhail Naganov201369b2018-05-16 16:52:32 -0700799 headerPrinted = true;
800 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700801 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700802 }
803 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700804
805 headerPrinted = false;
806 for (const auto& module : mInsertedModules) {
807 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
808 if (!headerPrinted) {
809 patchPanelDump += "\nTracked inserted modules:\n";
810 headerPrinted = true;
811 }
812 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
813 for (const auto& stream : module.second.streams) {
814 moduleDump.appendFormat("%d ", stream);
815 }
816 moduleDump.append("; SW Patches: ");
817 for (const auto& patch : module.second.sw_patches) {
818 moduleDump.appendFormat("%d ", patch);
819 }
820 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
821 }
822 }
823
824 if (!patchPanelDump.isEmpty()) {
825 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700826 }
827}
828
Glenn Kasten63238ef2015-03-02 15:50:29 -0800829} // namespace android