blob: 53e2dd5f51d0b5e8d744fce8c18ae1457637be74 [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 */
58status_t AudioFlinger::getAudioPort(struct audio_port *port)
59{
60 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070061 return mPatchPanel.getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -070062}
63
Eric Laurent951f4552014-05-20 10:48:17 -070064/* Connect a patch between several source and sink ports */
65status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
66 audio_patch_handle_t *handle)
67{
68 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070069 return mPatchPanel.createAudioPatch(patch, handle);
Eric Laurent951f4552014-05-20 10:48:17 -070070}
71
72/* Disconnect a patch */
73status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
74{
75 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070076 return mPatchPanel.releaseAudioPatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -070077}
78
Eric Laurent951f4552014-05-20 10:48:17 -070079/* List connected audio ports and they attributes */
80status_t AudioFlinger::listAudioPatches(unsigned int *num_patches,
81 struct audio_patch *patches)
82{
83 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070084 return mPatchPanel.listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070085}
86
Mikhail Naganovadca70f2018-07-09 12:49:25 -070087status_t AudioFlinger::PatchPanel::SoftwarePatch::getLatencyMs_l(double *latencyMs) const
88{
89 const auto& iter = mPatchPanel.mPatches.find(mPatchHandle);
90 if (iter != mPatchPanel.mPatches.end()) {
91 return iter->second.getLatencyMs(latencyMs);
92 } else {
93 return BAD_VALUE;
94 }
95}
96
Eric Laurent951f4552014-05-20 10:48:17 -070097/* List connected audio ports and their attributes */
98status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
99 struct audio_port *ports __unused)
100{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700101 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700102 return NO_ERROR;
103}
104
105/* Get supported attributes for a given audio port */
106status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port *port __unused)
107{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700108 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700109 return NO_ERROR;
110}
111
Eric Laurent951f4552014-05-20 10:48:17 -0700112/* Connect a patch between several source and sink ports */
113status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
114 audio_patch_handle_t *handle)
115{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700116 if (handle == NULL || patch == NULL) {
117 return BAD_VALUE;
118 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700119 ALOGV("%s() num_sources %d num_sinks %d handle %d",
120 __func__, patch->num_sources, patch->num_sinks, *handle);
121 status_t status = NO_ERROR;
122 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700123
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700124 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700125 return BAD_VALUE;
126 }
Eric Laurent874c42872014-08-08 15:13:39 -0700127 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
128 // only the audio policy manager can request a patch creation with 2 sources.
129 if (patch->num_sources > 2) {
130 return INVALID_OPERATION;
131 }
Eric Laurent951f4552014-05-20 10:48:17 -0700132
Eric Laurent83b88082014-06-20 18:31:16 -0700133 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700134 auto iter = mPatches.find(*handle);
135 if (iter != mPatches.end()) {
136 ALOGV("%s() removing patch handle %d", __func__, *handle);
137 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700138 // free resources owned by the removed patch if applicable
139 // 1) if a software patch is present, release the playback and capture threads and
140 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700141 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700142 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700143 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700144 // 2) if the new patch and old patch source or sink are devices from different
145 // hw modules, clear the audio HAL patches now because they will not be updated
146 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700147 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700148 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
149 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
150 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
151 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
152 oldPatch.sources[0].ext.device.hw_module !=
153 patch->sources[0].ext.device.hw_module)) {
154 hwModule = oldPatch.sources[0].ext.device.hw_module;
155 } else if (patch->num_sinks == 0 ||
156 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
157 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
158 oldPatch.sinks[0].ext.device.hw_module !=
159 patch->sinks[0].ext.device.hw_module))) {
160 // Note on (patch->num_sinks == 0): this situation should not happen as
161 // these special patches are only created by the policy manager but just
162 // in case, systematically clear the HAL patch.
163 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700164 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700165 hwModule = oldPatch.sinks[0].ext.device.hw_module;
166 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700167 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
168 if (hwDevice != 0) {
169 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700170 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800171 halHandle = removedPatch.mHalHandle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700172 }
173 mPatches.erase(iter);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700174 removeSoftwarePatchFromInsertedModules(*handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700175 }
176 }
177
Mikhail Naganovdea53042018-04-26 13:10:21 -0700178 Patch newPatch{*patch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700179 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700180
Eric Laurent951f4552014-05-20 10:48:17 -0700181 switch (patch->sources[0].type) {
182 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700183 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700184 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
185 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700186 status = BAD_VALUE;
187 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700188 }
189 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700190 // support only one sink if connection to a mix or across HW modules
191 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700192 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
193 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700194 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700195 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700196 status = INVALID_OPERATION;
197 goto exit;
198 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700199 // reject connection to different sink types
200 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700201 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700202 status = BAD_VALUE;
203 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700204 }
Eric Laurent951f4552014-05-20 10:48:17 -0700205 }
206
Eric Laurent3bcf8592015-04-03 12:13:24 -0700207 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700208 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700209 // - Device to device AND
210 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700211 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700212 if ((patch->num_sources == 2) ||
213 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
214 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700215 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800216 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
217 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700218 if (patch->num_sources == 2) {
219 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700220 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
221 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700222 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700223 status = INVALID_OPERATION;
224 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700225 }
Eric Laurent83b88082014-06-20 18:31:16 -0700226
227 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700228 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700229 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700230 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700231 status = INVALID_OPERATION;
232 goto exit;
233 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700234 // existing playback thread is reused, so it is not closed when patch is cleared
235 newPatch.mPlayback.setThread(
236 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700237 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700238 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700239 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,
juyuchen2224c5a2019-01-21 12:00:58 +0800257 outputDevice,
258 outputDeviceAddress,
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()));
Eric Laurent83b88082014-06-20 18:31:16 -0700266 }
Eric Laurent83b88082014-06-20 18:31:16 -0700267 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700268 String8 address = String8(patch->sources[0].ext.device.address);
269 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700270 // open input stream with source device audio properties if provided or
271 // default to peer output stream properties otherwise.
272 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
273 config.sample_rate = patch->sources[0].sample_rate;
274 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700275 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700276 }
277 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
278 config.channel_mask = patch->sources[0].channel_mask;
279 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700280 config.channel_mask = audio_channel_in_mask_from_count(
281 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700282 }
283 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
284 config.format = patch->sources[0].format;
285 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700286 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700287 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700288 audio_input_flags_t flags =
289 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
290 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700291 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700292 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700293 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700294 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700295 device,
296 address,
297 AUDIO_SOURCE_MIC,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800298 flags,
299 outputDevice,
300 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700301 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700302 thread.get(), config.channel_mask);
303 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700304 status = NO_MEMORY;
305 goto exit;
306 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700307 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700308 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700309 if (status != NO_ERROR) {
310 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700311 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700312 if (audioHwDevice->isInsert()) {
313 insertedModule = audioHwDevice->handle();
314 }
Eric Laurent951f4552014-05-20 10:48:17 -0700315 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700316 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700317 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700318 patch->sinks[0].ext.mix.handle);
319 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700320 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800321 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700322 ALOGW("%s() bad capture I/O handle %d",
323 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800324 status = BAD_VALUE;
325 goto exit;
326 }
Eric Laurent83b88082014-06-20 18:31:16 -0700327 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700328 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent526aa572019-01-15 10:54:58 -0800329 // remove stale audio patch with same input as sink if any
330 for (auto& iter : mPatches) {
331 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
332 mPatches.erase(iter.first);
333 break;
334 }
335 }
Eric Laurent83b88082014-06-20 18:31:16 -0700336 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700337 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
338 status = hwDevice->createAudioPatch(patch->num_sources,
339 patch->sources,
340 patch->num_sinks,
341 patch->sinks,
342 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700343 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700344 }
Eric Laurent951f4552014-05-20 10:48:17 -0700345 }
346 } break;
347 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700348 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700349 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700350 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700351 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700352 status = BAD_VALUE;
353 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700354 }
355 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700356 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700357 for (unsigned int i = 0; i < patch->num_sinks; i++) {
358 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700359 ALOGW("%s() invalid sink type %d for mix source",
360 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700361 status = BAD_VALUE;
362 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700363 }
364 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700365 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700366 status = BAD_VALUE;
367 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700368 }
jiabinc52b1ff2019-10-31 17:20:42 -0700369 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
370 patch->sinks[i].ext.device.type);
371 device->setAddress(patch->sinks[i].ext.device.address);
372 device->applyAudioPortConfig(&patch->sinks[i]);
373 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700374 }
Eric Laurent951f4552014-05-20 10:48:17 -0700375 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700376 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700377 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700378 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800379 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700380 ALOGW("%s() bad playback I/O handle %d",
381 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800382 status = BAD_VALUE;
383 goto exit;
384 }
Eric Laurent951f4552014-05-20 10:48:17 -0700385 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700386 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700387 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700388 }
389
Eric Laurent054d9d32015-04-24 08:48:48 -0700390 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent526aa572019-01-15 10:54:58 -0800391
392 // remove stale audio patch with same output as source if any
393 for (auto& iter : mPatches) {
394 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id()) {
395 mPatches.erase(iter.first);
396 break;
397 }
398 }
Eric Laurent951f4552014-05-20 10:48:17 -0700399 } break;
400 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700401 status = BAD_VALUE;
402 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700403 }
Eric Laurent83b88082014-06-20 18:31:16 -0700404exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700405 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700406 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700407 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
408 newPatch.mHalHandle = halHandle;
409 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700410 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
411 addSoftwarePatchToInsertedModules(insertedModule, *handle);
412 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700413 ALOGV("%s() added new patch handle %d halHandle %d", __func__, *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700414 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700415 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700416 }
417 return status;
418}
419
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700420AudioFlinger::PatchPanel::Patch::~Patch()
421{
422 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
423 mRecord.handle(), mPlayback.handle());
424}
425
Mikhail Naganovdea53042018-04-26 13:10:21 -0700426status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700427{
428 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700429 status_t status = panel->createAudioPatch(
430 PatchBuilder().addSource(mAudioPatch.sources[0]).
431 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
432 mRecord.handlePtr());
Eric Laurent83b88082014-06-20 18:31:16 -0700433 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700434 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700435 return status;
436 }
437
438 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700439 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700440 status = panel->createAudioPatch(
441 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
442 mPlayback.handlePtr());
Eric Laurentd60560a2015-04-10 11:31:20 -0700443 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700444 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700445 return status;
446 }
447 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700448 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700449 }
450
451 // use a pseudo LCM between input and output framecount
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700452 size_t playbackFrameCount = mPlayback.thread()->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700453 int playbackShift = __builtin_ctz(playbackFrameCount);
jiabin01c8f562018-07-19 17:47:28 -0700454 size_t recordFrameCount = mRecord.thread()->frameCount();
455 int shift = __builtin_ctz(recordFrameCount);
Eric Laurent83b88082014-06-20 18:31:16 -0700456 if (playbackShift < shift) {
457 shift = playbackShift;
458 }
jiabin01c8f562018-07-19 17:47:28 -0700459 size_t frameCount = (playbackFrameCount * recordFrameCount) >> shift;
460 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
461 __func__, playbackFrameCount, recordFrameCount, frameCount);
Eric Laurent83b88082014-06-20 18:31:16 -0700462
463 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700464 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700465 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700466 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
467 uint32_t sampleRate = mPlayback.thread()->sampleRate();
468 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700469
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700470 audio_format_t inputFormat = mRecord.thread()->format();
471 if (!audio_is_linear_pcm(inputFormat)) {
472 // The playbackThread format will say PCM for IEC61937 packetized stream.
473 // Use recordThread format.
474 format = inputFormat;
475 }
476 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
477 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700478 if (sampleRate == mRecord.thread()->sampleRate() &&
479 inChannelMask == mRecord.thread()->channelMask() &&
480 mRecord.thread()->fastTrackAvailable() &&
481 mRecord.thread()->hasFastCapture()) {
482 // Create a fast track if the record thread has fast capture to get better performance.
483 // Only enable fast mode when there is no resample needed.
484 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
485 } else {
486 // Fast mode is not available in this case.
487 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
488 }
Eric Laurent83b88082014-06-20 18:31:16 -0700489
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700490 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
491 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700492 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
493 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
494 // "reuse one existing output mix" case
495 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
496 }
jiabin01c8f562018-07-19 17:47:28 -0700497 if (mPlayback.thread()->hasFastMixer()) {
498 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700499 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700500 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700501 } else {
502 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700503 }
504
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700505 sp<RecordThread::PatchRecord> tempRecordTrack;
506 if ((inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
507 tempRecordTrack = new RecordThread::PassthruPatchRecord(
508 mRecord.thread().get(),
509 sampleRate,
510 inChannelMask,
511 format,
512 frameCount,
513 inputFlags);
514 } else {
515 tempRecordTrack = new RecordThread::PatchRecord(
516 mRecord.thread().get(),
517 sampleRate,
518 inChannelMask,
519 format,
520 frameCount,
521 nullptr,
522 (size_t)0 /* bufferSize */,
523 inputFlags);
524 }
525 status = mRecord.checkTrack(tempRecordTrack.get());
526 if (status != NO_ERROR) {
527 return status;
528 }
529
Eric Laurent83b88082014-06-20 18:31:16 -0700530 // create a special playback track to render to playback thread.
531 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700532 sp<PlaybackThread::PatchTrack> tempPatchTrack = new PlaybackThread::PatchTrack(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700533 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700534 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700535 sampleRate,
536 outChannelMask,
537 format,
538 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700539 tempRecordTrack->buffer(),
540 tempRecordTrack->bufferSize(),
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700541 outputFlags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700542 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700543 if (status != NO_ERROR) {
544 return status;
545 }
Eric Laurent83b88082014-06-20 18:31:16 -0700546
547 // tie playback and record tracks together
Andy Hungabfab202019-03-07 19:45:54 -0800548 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack);
549 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack);
Eric Laurent83b88082014-06-20 18:31:16 -0700550
551 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700552 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
553 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700554
555 return status;
556}
557
Mikhail Naganovdea53042018-04-26 13:10:21 -0700558void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700559{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700560 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
561 __func__, mRecord.handle(), mPlayback.handle());
562 mRecord.stopTrack();
563 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700564 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700565 mRecord.closeConnections(panel);
566 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700567}
568
Andy Hungc3ab7732018-06-01 13:46:29 -0700569status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
570{
571 if (!isSoftware()) return INVALID_OPERATION;
572
573 auto recordTrack = mRecord.const_track();
574 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
575
576 auto playbackTrack = mPlayback.const_track();
577 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
578
579 // Latency information for tracks may be called without obtaining
580 // the underlying thread lock.
581 //
582 // We use record server latency + playback track latency (generally smaller than the
583 // reverse due to internal biases).
584 //
585 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700586
Andy Hung30282562018-08-08 18:27:03 -0700587 // For PCM tracks get server latency.
588 if (audio_is_linear_pcm(recordTrack->format())) {
589 double recordServerLatencyMs, playbackTrackLatencyMs;
590 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
591 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
592 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
593 return OK;
594 }
595 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700596
Andy Hung30282562018-08-08 18:27:03 -0700597 // See if kernel latencies are available.
598 // If so, do a frame diff and time difference computation to estimate
599 // the total patch latency. This requires that frame counts are reported by the
600 // HAL are matched properly in the case of record overruns and playback underruns.
601 ThreadBase::TrackBase::FrameTime recordFT{}, playFT{};
602 recordTrack->getKernelFrameTime(&recordFT);
603 playbackTrack->getKernelFrameTime(&playFT);
604 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
605 const int64_t frameDiff = recordFT.frames - playFT.frames;
606 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
607
608 // It is possible that the patch track and patch record have a large time disparity because
609 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
610 // time difference based on how often we expect the timestamps to update in normal operation
611 // (typical should be no more than 50 ms).
612 //
613 // If the timestamps aren't sampled close enough, the patch latency is not
614 // considered valid.
615 //
616 // TODO: change this based on more experiments.
617 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
618 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
619 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
620 - timeDiffNs * 1e-6;
621 return OK;
622 }
623 }
624
625 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700626}
627
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700628String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700629{
Andy Hungc3ab7732018-06-01 13:46:29 -0700630 // TODO: Consider table dump form for patches, just like tracks.
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700631 String8 result = String8::format("Patch %d: thread %p => thread %p",
632 myHandle, mRecord.const_thread().get(), mPlayback.const_thread().get());
Andy Hungc3ab7732018-06-01 13:46:29 -0700633
634 // add latency if it exists
635 double latencyMs;
636 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700637 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700638 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700639 return result;
640}
641
Eric Laurent951f4552014-05-20 10:48:17 -0700642/* Disconnect a patch */
643status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
644{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700645 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700646 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700647
Mikhail Naganovdea53042018-04-26 13:10:21 -0700648 auto iter = mPatches.find(handle);
649 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700650 return BAD_VALUE;
651 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700652 Patch &removedPatch = iter->second;
653 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700654
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700655 const struct audio_port_config &src = patch.sources[0];
656 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700657 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700658 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
659 if (hwDevice == 0) {
660 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700661 status = BAD_VALUE;
662 break;
663 }
Eric Laurent83b88082014-06-20 18:31:16 -0700664
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700665 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700666 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700667 break;
668 }
669
Mikhail Naganovdea53042018-04-26 13:10:21 -0700670 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
671 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
672 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700673 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700674 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800675 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700676 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800677 status = BAD_VALUE;
678 break;
679 }
Eric Laurent951f4552014-05-20 10:48:17 -0700680 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700681 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700682 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700683 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700684 }
685 } break;
686 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700687 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
688 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700689 status = BAD_VALUE;
690 break;
691 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700692 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700693 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700694 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700695 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800696 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700697 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800698 status = BAD_VALUE;
699 break;
700 }
Eric Laurent951f4552014-05-20 10:48:17 -0700701 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700702 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700703 } break;
704 default:
705 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700706 }
707
Mikhail Naganovdea53042018-04-26 13:10:21 -0700708 mPatches.erase(iter);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700709 removeSoftwarePatchFromInsertedModules(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700710 return status;
711}
712
Eric Laurent951f4552014-05-20 10:48:17 -0700713/* List connected audio ports and they attributes */
714status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
715 struct audio_patch *patches __unused)
716{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700717 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700718 return NO_ERROR;
719}
720
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700721status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
722 audio_io_handle_t stream,
723 std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const
724{
725 for (const auto& module : mInsertedModules) {
726 if (module.second.streams.count(stream)) {
727 for (const auto& patchHandle : module.second.sw_patches) {
728 const auto& patch_iter = mPatches.find(patchHandle);
729 if (patch_iter != mPatches.end()) {
730 const Patch &patch = patch_iter->second;
731 patches->emplace_back(*this, patchHandle,
732 patch.mPlayback.const_thread()->id(),
733 patch.mRecord.const_thread()->id());
734 } else {
735 ALOGE("Stale patch handle in the cache: %d", patchHandle);
736 }
737 }
738 return OK;
739 }
740 }
741 // The stream is not associated with any of inserted modules.
742 return BAD_VALUE;
743}
744
745void AudioFlinger::PatchPanel::notifyStreamOpened(
746 AudioHwDevice *audioHwDevice, audio_io_handle_t stream)
747{
748 if (audioHwDevice->isInsert()) {
749 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
750 }
751}
752
753void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
754{
755 for (auto& module : mInsertedModules) {
756 module.second.streams.erase(stream);
757 }
758}
759
760AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700761{
762 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
763 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
764 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700765 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700766 return nullptr;
767 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700768 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700769}
770
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700771sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700772{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700773 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
774 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
775}
776
777void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
778 audio_module_handle_t module, audio_patch_handle_t handle)
779{
780 mInsertedModules[module].sw_patches.insert(handle);
781}
782
783void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
784 audio_patch_handle_t handle)
785{
786 for (auto& module : mInsertedModules) {
787 module.second.sw_patches.erase(handle);
788 }
789}
790
791void AudioFlinger::PatchPanel::dump(int fd) const
792{
793 String8 patchPanelDump;
794 const char *indent = " ";
795
Mikhail Naganov201369b2018-05-16 16:52:32 -0700796 // Only dump software patches.
797 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700798 for (const auto& iter : mPatches) {
Mikhail Naganov201369b2018-05-16 16:52:32 -0700799 if (iter.second.isSoftware()) {
800 if (!headerPrinted) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700801 patchPanelDump += "\nSoftware patches:\n";
Mikhail Naganov201369b2018-05-16 16:52:32 -0700802 headerPrinted = true;
803 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700804 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700805 }
806 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700807
808 headerPrinted = false;
809 for (const auto& module : mInsertedModules) {
810 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
811 if (!headerPrinted) {
812 patchPanelDump += "\nTracked inserted modules:\n";
813 headerPrinted = true;
814 }
815 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
816 for (const auto& stream : module.second.streams) {
817 moduleDump.appendFormat("%d ", stream);
818 }
819 moduleDump.append("; SW Patches: ");
820 for (const auto& patch : module.second.sw_patches) {
821 moduleDump.appendFormat("%d ", patch);
822 }
823 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
824 }
825 }
826
827 if (!patchPanelDump.isEmpty()) {
828 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700829 }
830}
831
Glenn Kasten63238ef2015-03-02 15:50:29 -0800832} // namespace android