blob: 3381e4d9b9c4fba0f9b1dcfd19a8018d9289e46e [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()))) {
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800214 audio_devices_t outputDevice = AUDIO_DEVICE_NONE;
215 String8 outputDeviceAddress;
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;
237 audio_devices_t device = patch->sinks[0].ext.device.type;
238 String8 address = String8(patch->sinks[0].ext.device.address);
239 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700240 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
241 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
242 config.sample_rate = patch->sinks[0].sample_rate;
243 }
244 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
245 config.channel_mask = patch->sinks[0].channel_mask;
246 }
247 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
248 config.format = patch->sinks[0].format;
249 }
250 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
251 flags = patch->sinks[0].flags.output;
252 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700253 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800254 patch->sinks[0].ext.device.hw_module,
255 &output,
256 &config,
257 device,
258 address,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700259 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700260 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
261 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700262 status = NO_MEMORY;
263 goto exit;
264 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700265 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800266 outputDevice = device;
267 outputDeviceAddress = address;
Eric Laurent83b88082014-06-20 18:31:16 -0700268 }
Eric Laurent83b88082014-06-20 18:31:16 -0700269 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700270 String8 address = String8(patch->sources[0].ext.device.address);
271 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700272 // open input stream with source device audio properties if provided or
273 // default to peer output stream properties otherwise.
274 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
275 config.sample_rate = patch->sources[0].sample_rate;
276 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700277 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700278 }
279 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
280 config.channel_mask = patch->sources[0].channel_mask;
281 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700282 config.channel_mask = audio_channel_in_mask_from_count(
283 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700284 }
285 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
286 config.format = patch->sources[0].format;
287 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700288 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700289 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700290 audio_input_flags_t flags =
291 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
292 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700293 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700294 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700295 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700296 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700297 device,
298 address,
299 AUDIO_SOURCE_MIC,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800300 flags,
301 outputDevice,
302 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700303 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700304 thread.get(), config.channel_mask);
305 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700306 status = NO_MEMORY;
307 goto exit;
308 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700309 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700310 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700311 if (status != NO_ERROR) {
312 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700313 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700314 if (audioHwDevice->isInsert()) {
315 insertedModule = audioHwDevice->handle();
316 }
Eric Laurent951f4552014-05-20 10:48:17 -0700317 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700318 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700319 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700320 patch->sinks[0].ext.mix.handle);
321 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700322 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800323 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700324 ALOGW("%s() bad capture I/O handle %d",
325 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800326 status = BAD_VALUE;
327 goto exit;
328 }
Eric Laurent83b88082014-06-20 18:31:16 -0700329 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700330 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent526aa572019-01-15 10:54:58 -0800331 // remove stale audio patch with same input as sink if any
332 for (auto& iter : mPatches) {
333 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
334 mPatches.erase(iter.first);
335 break;
336 }
337 }
Eric Laurent83b88082014-06-20 18:31:16 -0700338 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700339 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
340 status = hwDevice->createAudioPatch(patch->num_sources,
341 patch->sources,
342 patch->num_sinks,
343 patch->sinks,
344 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700345 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700346 }
Eric Laurent951f4552014-05-20 10:48:17 -0700347 }
348 } break;
349 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700350 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700351 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700352 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700353 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700354 status = BAD_VALUE;
355 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700356 }
357 // limit to connections between devices and output streams
Eric Laurent054d9d32015-04-24 08:48:48 -0700358 audio_devices_t type = AUDIO_DEVICE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700359 for (unsigned int i = 0; i < patch->num_sinks; i++) {
360 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700361 ALOGW("%s() invalid sink type %d for mix source",
362 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700363 status = BAD_VALUE;
364 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700365 }
366 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700367 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700368 status = BAD_VALUE;
369 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700370 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700371 type |= patch->sinks[i].ext.device.type;
Eric Laurent951f4552014-05-20 10:48:17 -0700372 }
Eric Laurent951f4552014-05-20 10:48:17 -0700373 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700374 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700375 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700376 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800377 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700378 ALOGW("%s() bad playback I/O handle %d",
379 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800380 status = BAD_VALUE;
381 goto exit;
382 }
Eric Laurent951f4552014-05-20 10:48:17 -0700383 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700384 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
Eric Laurent054d9d32015-04-24 08:48:48 -0700385 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -0700386 param.addInt(String8(AudioParameter::keyRouting), (int)type);
Eric Laurent054d9d32015-04-24 08:48:48 -0700387
Mikhail Naganovdea53042018-04-26 13:10:21 -0700388 mAudioFlinger.broacastParametersToRecordThreads_l(param.toString());
Eric Laurent951f4552014-05-20 10:48:17 -0700389 }
390
Eric Laurent054d9d32015-04-24 08:48:48 -0700391 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent526aa572019-01-15 10:54:58 -0800392
393 // remove stale audio patch with same output as source if any
394 for (auto& iter : mPatches) {
395 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id()) {
396 mPatches.erase(iter.first);
397 break;
398 }
399 }
Eric Laurent951f4552014-05-20 10:48:17 -0700400 } break;
401 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700402 status = BAD_VALUE;
403 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700404 }
Eric Laurent83b88082014-06-20 18:31:16 -0700405exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700406 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700407 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700408 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
409 newPatch.mHalHandle = halHandle;
410 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700411 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
412 addSoftwarePatchToInsertedModules(insertedModule, *handle);
413 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700414 ALOGV("%s() added new patch handle %d halHandle %d", __func__, *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700415 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700416 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700417 }
418 return status;
419}
420
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700421AudioFlinger::PatchPanel::Patch::~Patch()
422{
423 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
424 mRecord.handle(), mPlayback.handle());
425}
426
Mikhail Naganovdea53042018-04-26 13:10:21 -0700427status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700428{
429 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700430 status_t status = panel->createAudioPatch(
431 PatchBuilder().addSource(mAudioPatch.sources[0]).
432 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
433 mRecord.handlePtr());
Eric Laurent83b88082014-06-20 18:31:16 -0700434 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700435 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700436 return status;
437 }
438
439 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700440 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700441 status = panel->createAudioPatch(
442 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
443 mPlayback.handlePtr());
Eric Laurentd60560a2015-04-10 11:31:20 -0700444 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700445 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700446 return status;
447 }
448 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700449 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700450 }
451
452 // use a pseudo LCM between input and output framecount
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700453 size_t playbackFrameCount = mPlayback.thread()->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700454 int playbackShift = __builtin_ctz(playbackFrameCount);
jiabin01c8f562018-07-19 17:47:28 -0700455 size_t recordFrameCount = mRecord.thread()->frameCount();
456 int shift = __builtin_ctz(recordFrameCount);
Eric Laurent83b88082014-06-20 18:31:16 -0700457 if (playbackShift < shift) {
458 shift = playbackShift;
459 }
jiabin01c8f562018-07-19 17:47:28 -0700460 size_t frameCount = (playbackFrameCount * recordFrameCount) >> shift;
461 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
462 __func__, playbackFrameCount, recordFrameCount, frameCount);
Eric Laurent83b88082014-06-20 18:31:16 -0700463
464 // 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 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700490 sp<RecordThread::PatchRecord> tempRecordTrack = new (std::nothrow) RecordThread::PatchRecord(
491 mRecord.thread().get(),
Eric Laurent83b88082014-06-20 18:31:16 -0700492 sampleRate,
493 inChannelMask,
494 format,
495 frameCount,
496 NULL,
Andy Hung8fe68032017-06-05 16:17:51 -0700497 (size_t)0 /* bufferSize */,
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700498 inputFlags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700499 status = mRecord.checkTrack(tempRecordTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700500 if (status != NO_ERROR) {
501 return status;
502 }
Eric Laurent83b88082014-06-20 18:31:16 -0700503
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700504 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
505 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700506 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
507 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
508 // "reuse one existing output mix" case
509 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
510 }
jiabin01c8f562018-07-19 17:47:28 -0700511 if (mPlayback.thread()->hasFastMixer()) {
512 // Create a fast track if the playback thread has fast mixer to get better performance.
513 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
514 }
515
Eric Laurent83b88082014-06-20 18:31:16 -0700516 // create a special playback track to render to playback thread.
517 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700518 sp<PlaybackThread::PatchTrack> tempPatchTrack = new (std::nothrow) PlaybackThread::PatchTrack(
519 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700520 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700521 sampleRate,
522 outChannelMask,
523 format,
524 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700525 tempRecordTrack->buffer(),
526 tempRecordTrack->bufferSize(),
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700527 outputFlags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700528 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700529 if (status != NO_ERROR) {
530 return status;
531 }
Eric Laurent83b88082014-06-20 18:31:16 -0700532
533 // tie playback and record tracks together
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700534 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack.get());
535 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700536
537 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700538 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
539 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700540
541 return status;
542}
543
Mikhail Naganovdea53042018-04-26 13:10:21 -0700544void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700545{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700546 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
547 __func__, mRecord.handle(), mPlayback.handle());
548 mRecord.stopTrack();
549 mPlayback.stopTrack();
550 mRecord.closeConnections(panel);
551 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700552}
553
Andy Hungc3ab7732018-06-01 13:46:29 -0700554status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
555{
556 if (!isSoftware()) return INVALID_OPERATION;
557
558 auto recordTrack = mRecord.const_track();
559 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
560
561 auto playbackTrack = mPlayback.const_track();
562 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
563
564 // Latency information for tracks may be called without obtaining
565 // the underlying thread lock.
566 //
567 // We use record server latency + playback track latency (generally smaller than the
568 // reverse due to internal biases).
569 //
570 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700571
Andy Hung30282562018-08-08 18:27:03 -0700572 // For PCM tracks get server latency.
573 if (audio_is_linear_pcm(recordTrack->format())) {
574 double recordServerLatencyMs, playbackTrackLatencyMs;
575 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
576 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
577 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
578 return OK;
579 }
580 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700581
Andy Hung30282562018-08-08 18:27:03 -0700582 // See if kernel latencies are available.
583 // If so, do a frame diff and time difference computation to estimate
584 // the total patch latency. This requires that frame counts are reported by the
585 // HAL are matched properly in the case of record overruns and playback underruns.
586 ThreadBase::TrackBase::FrameTime recordFT{}, playFT{};
587 recordTrack->getKernelFrameTime(&recordFT);
588 playbackTrack->getKernelFrameTime(&playFT);
589 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
590 const int64_t frameDiff = recordFT.frames - playFT.frames;
591 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
592
593 // It is possible that the patch track and patch record have a large time disparity because
594 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
595 // time difference based on how often we expect the timestamps to update in normal operation
596 // (typical should be no more than 50 ms).
597 //
598 // If the timestamps aren't sampled close enough, the patch latency is not
599 // considered valid.
600 //
601 // TODO: change this based on more experiments.
602 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
603 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
604 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
605 - timeDiffNs * 1e-6;
606 return OK;
607 }
608 }
609
610 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700611}
612
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700613String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700614{
Andy Hungc3ab7732018-06-01 13:46:29 -0700615 // TODO: Consider table dump form for patches, just like tracks.
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700616 String8 result = String8::format("Patch %d: thread %p => thread %p",
617 myHandle, mRecord.const_thread().get(), mPlayback.const_thread().get());
Andy Hungc3ab7732018-06-01 13:46:29 -0700618
619 // add latency if it exists
620 double latencyMs;
621 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700622 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700623 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700624 return result;
625}
626
Eric Laurent951f4552014-05-20 10:48:17 -0700627/* Disconnect a patch */
628status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
629{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700630 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700631 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700632
Mikhail Naganovdea53042018-04-26 13:10:21 -0700633 auto iter = mPatches.find(handle);
634 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700635 return BAD_VALUE;
636 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700637 Patch &removedPatch = iter->second;
638 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700639
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700640 const struct audio_port_config &src = patch.sources[0];
641 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700642 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700643 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
644 if (hwDevice == 0) {
645 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700646 status = BAD_VALUE;
647 break;
648 }
Eric Laurent83b88082014-06-20 18:31:16 -0700649
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700650 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700651 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700652 break;
653 }
654
Mikhail Naganovdea53042018-04-26 13:10:21 -0700655 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
656 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
657 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700658 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700659 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800660 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700661 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800662 status = BAD_VALUE;
663 break;
664 }
Eric Laurent951f4552014-05-20 10:48:17 -0700665 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700666 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700667 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700668 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700669 }
670 } break;
671 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700672 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
673 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700674 status = BAD_VALUE;
675 break;
676 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700677 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700678 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700679 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700680 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800681 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700682 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800683 status = BAD_VALUE;
684 break;
685 }
Eric Laurent951f4552014-05-20 10:48:17 -0700686 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700687 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700688 } break;
689 default:
690 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700691 }
692
Mikhail Naganovdea53042018-04-26 13:10:21 -0700693 mPatches.erase(iter);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700694 removeSoftwarePatchFromInsertedModules(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700695 return status;
696}
697
Eric Laurent951f4552014-05-20 10:48:17 -0700698/* List connected audio ports and they attributes */
699status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
700 struct audio_patch *patches __unused)
701{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700702 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700703 return NO_ERROR;
704}
705
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700706status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
707 audio_io_handle_t stream,
708 std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const
709{
710 for (const auto& module : mInsertedModules) {
711 if (module.second.streams.count(stream)) {
712 for (const auto& patchHandle : module.second.sw_patches) {
713 const auto& patch_iter = mPatches.find(patchHandle);
714 if (patch_iter != mPatches.end()) {
715 const Patch &patch = patch_iter->second;
716 patches->emplace_back(*this, patchHandle,
717 patch.mPlayback.const_thread()->id(),
718 patch.mRecord.const_thread()->id());
719 } else {
720 ALOGE("Stale patch handle in the cache: %d", patchHandle);
721 }
722 }
723 return OK;
724 }
725 }
726 // The stream is not associated with any of inserted modules.
727 return BAD_VALUE;
728}
729
730void AudioFlinger::PatchPanel::notifyStreamOpened(
731 AudioHwDevice *audioHwDevice, audio_io_handle_t stream)
732{
733 if (audioHwDevice->isInsert()) {
734 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
735 }
736}
737
738void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
739{
740 for (auto& module : mInsertedModules) {
741 module.second.streams.erase(stream);
742 }
743}
744
745AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700746{
747 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
748 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
749 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700750 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700751 return nullptr;
752 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700753 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700754}
755
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700756sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700757{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700758 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
759 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
760}
761
762void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
763 audio_module_handle_t module, audio_patch_handle_t handle)
764{
765 mInsertedModules[module].sw_patches.insert(handle);
766}
767
768void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
769 audio_patch_handle_t handle)
770{
771 for (auto& module : mInsertedModules) {
772 module.second.sw_patches.erase(handle);
773 }
774}
775
776void AudioFlinger::PatchPanel::dump(int fd) const
777{
778 String8 patchPanelDump;
779 const char *indent = " ";
780
Mikhail Naganov201369b2018-05-16 16:52:32 -0700781 // Only dump software patches.
782 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700783 for (const auto& iter : mPatches) {
Mikhail Naganov201369b2018-05-16 16:52:32 -0700784 if (iter.second.isSoftware()) {
785 if (!headerPrinted) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700786 patchPanelDump += "\nSoftware patches:\n";
Mikhail Naganov201369b2018-05-16 16:52:32 -0700787 headerPrinted = true;
788 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700789 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700790 }
791 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700792
793 headerPrinted = false;
794 for (const auto& module : mInsertedModules) {
795 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
796 if (!headerPrinted) {
797 patchPanelDump += "\nTracked inserted modules:\n";
798 headerPrinted = true;
799 }
800 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
801 for (const auto& stream : module.second.streams) {
802 moduleDump.appendFormat("%d ", stream);
803 }
804 moduleDump.append("; SW Patches: ");
805 for (const auto& patch : module.second.sw_patches) {
806 moduleDump.appendFormat("%d ", patch);
807 }
808 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
809 }
810 }
811
812 if (!patchPanelDump.isEmpty()) {
813 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700814 }
815}
816
Glenn Kasten63238ef2015-03-02 15:50:29 -0800817} // namespace android