blob: eaf1120c68e12e3b0a8898dbfb1029dafc77d011 [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"
27#include "ServiceUtilities.h"
28#include <media/AudioParameter.h>
29
30// ----------------------------------------------------------------------------
31
32// Note: the following macro is used for extremely verbose logging message. In
33// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
34// 0; but one side effect of this is to turn all LOGV's as well. Some messages
35// are so verbose that we want to suppress them even when we have ALOG_ASSERT
36// turned on. Do not uncomment the #def below unless you really know what you
37// are doing and want to see all of the extremely verbose messages.
38//#define VERY_VERY_VERBOSE_LOGGING
39#ifdef VERY_VERY_VERBOSE_LOGGING
40#define ALOGVV ALOGV
41#else
42#define ALOGVV(a...) do { } while(0)
43#endif
44
45namespace android {
46
47/* List connected audio ports and their attributes */
48status_t AudioFlinger::listAudioPorts(unsigned int *num_ports,
49 struct audio_port *ports)
50{
51 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070052 return mPatchPanel.listAudioPorts(num_ports, ports);
Eric Laurent951f4552014-05-20 10:48:17 -070053}
54
55/* Get supported attributes for a given audio port */
56status_t AudioFlinger::getAudioPort(struct audio_port *port)
57{
58 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070059 return mPatchPanel.getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -070060}
61
Eric Laurent951f4552014-05-20 10:48:17 -070062/* Connect a patch between several source and sink ports */
63status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
64 audio_patch_handle_t *handle)
65{
66 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070067 return mPatchPanel.createAudioPatch(patch, handle);
Eric Laurent951f4552014-05-20 10:48:17 -070068}
69
70/* Disconnect a patch */
71status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
72{
73 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070074 return mPatchPanel.releaseAudioPatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -070075}
76
Eric Laurent951f4552014-05-20 10:48:17 -070077/* List connected audio ports and they attributes */
78status_t AudioFlinger::listAudioPatches(unsigned int *num_patches,
79 struct audio_patch *patches)
80{
81 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070082 return mPatchPanel.listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070083}
84
85/* List connected audio ports and their attributes */
86status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
87 struct audio_port *ports __unused)
88{
Mikhail Naganovdea53042018-04-26 13:10:21 -070089 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -070090 return NO_ERROR;
91}
92
93/* Get supported attributes for a given audio port */
94status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port *port __unused)
95{
Mikhail Naganovdea53042018-04-26 13:10:21 -070096 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -070097 return NO_ERROR;
98}
99
Eric Laurent951f4552014-05-20 10:48:17 -0700100/* Connect a patch between several source and sink ports */
101status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
102 audio_patch_handle_t *handle)
103{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700104 if (handle == NULL || patch == NULL) {
105 return BAD_VALUE;
106 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700107 ALOGV("%s() num_sources %d num_sinks %d handle %d",
108 __func__, patch->num_sources, patch->num_sinks, *handle);
109 status_t status = NO_ERROR;
110 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700111
Eric Laurent874c42872014-08-08 15:13:39 -0700112 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700113 (patch->num_sinks == 0 && patch->num_sources != 2) ||
114 patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
Eric Laurent951f4552014-05-20 10:48:17 -0700115 return BAD_VALUE;
116 }
Eric Laurent874c42872014-08-08 15:13:39 -0700117 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
118 // only the audio policy manager can request a patch creation with 2 sources.
119 if (patch->num_sources > 2) {
120 return INVALID_OPERATION;
121 }
Eric Laurent951f4552014-05-20 10:48:17 -0700122
Eric Laurent83b88082014-06-20 18:31:16 -0700123 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700124 auto iter = mPatches.find(*handle);
125 if (iter != mPatches.end()) {
126 ALOGV("%s() removing patch handle %d", __func__, *handle);
127 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700128 // free resources owned by the removed patch if applicable
129 // 1) if a software patch is present, release the playback and capture threads and
130 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700131 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700132 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700133 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700134 // 2) if the new patch and old patch source or sink are devices from different
135 // hw modules, clear the audio HAL patches now because they will not be updated
136 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700137 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700138 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
139 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
140 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
141 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
142 oldPatch.sources[0].ext.device.hw_module !=
143 patch->sources[0].ext.device.hw_module)) {
144 hwModule = oldPatch.sources[0].ext.device.hw_module;
145 } else if (patch->num_sinks == 0 ||
146 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
147 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
148 oldPatch.sinks[0].ext.device.hw_module !=
149 patch->sinks[0].ext.device.hw_module))) {
150 // Note on (patch->num_sinks == 0): this situation should not happen as
151 // these special patches are only created by the policy manager but just
152 // in case, systematically clear the HAL patch.
153 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700154 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700155 hwModule = oldPatch.sinks[0].ext.device.hw_module;
156 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700157 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
158 if (hwDevice != 0) {
159 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700160 }
161 }
162 mPatches.erase(iter);
Eric Laurent951f4552014-05-20 10:48:17 -0700163 }
164 }
165
Mikhail Naganovdea53042018-04-26 13:10:21 -0700166 Patch newPatch{*patch};
Eric Laurent83b88082014-06-20 18:31:16 -0700167
Eric Laurent951f4552014-05-20 10:48:17 -0700168 switch (patch->sources[0].type) {
169 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700170 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700171 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700172 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700173 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700174 status = BAD_VALUE;
175 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700176 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700177 AudioHwDevice *audioHwDevice = mAudioFlinger.mAudioHwDevs.valueAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700178 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700179 // support only one sink if connection to a mix or across HW modules
180 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
181 patch->sinks[i].ext.mix.hw_module != srcModule) &&
182 patch->num_sinks > 1) {
183 status = INVALID_OPERATION;
184 goto exit;
185 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700186 // reject connection to different sink types
187 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700188 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700189 status = BAD_VALUE;
190 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700191 }
Eric Laurent951f4552014-05-20 10:48:17 -0700192 }
193
Eric Laurent3bcf8592015-04-03 12:13:24 -0700194 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700195 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700196 // - Device to device AND
197 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700198 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700199 if ((patch->num_sources == 2) ||
200 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
201 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700202 !audioHwDevice->supportsAudioPatches()))) {
Eric Laurent83b88082014-06-20 18:31:16 -0700203 if (patch->num_sources == 2) {
204 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700205 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
206 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700207 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700208 status = INVALID_OPERATION;
209 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700210 }
Eric Laurent83b88082014-06-20 18:31:16 -0700211
212 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700213 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700214 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700215 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700216 status = INVALID_OPERATION;
217 goto exit;
218 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700219 // existing playback thread is reused, so it is not closed when patch is cleared
220 newPatch.mPlayback.setThread(
221 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700222 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700223 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
224 audio_devices_t device = patch->sinks[0].ext.device.type;
225 String8 address = String8(patch->sinks[0].ext.device.address);
226 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700227 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800228 patch->sinks[0].ext.device.hw_module,
229 &output,
230 &config,
231 device,
232 address,
233 AUDIO_OUTPUT_FLAG_NONE);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700234 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
235 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700236 status = NO_MEMORY;
237 goto exit;
238 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700239 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Eric Laurent83b88082014-06-20 18:31:16 -0700240 }
Eric Laurent83b88082014-06-20 18:31:16 -0700241 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700242 String8 address = String8(patch->sources[0].ext.device.address);
243 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700244 // open input stream with source device audio properties if provided or
245 // default to peer output stream properties otherwise.
246 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
247 config.sample_rate = patch->sources[0].sample_rate;
248 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700249 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700250 }
251 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
252 config.channel_mask = patch->sources[0].channel_mask;
253 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700254 config.channel_mask = audio_channel_in_mask_from_count(
255 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700256 }
257 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
258 config.format = patch->sources[0].format;
259 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700260 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700261 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700262 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700263 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700264 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700265 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700266 device,
267 address,
268 AUDIO_SOURCE_MIC,
Eric Laurent83b88082014-06-20 18:31:16 -0700269 AUDIO_INPUT_FLAG_NONE);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700270 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700271 thread.get(), config.channel_mask);
272 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700273 status = NO_MEMORY;
274 goto exit;
275 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700276 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700277 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700278 if (status != NO_ERROR) {
279 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700280 }
281 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700282 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700283 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700284 patch->sinks[0].ext.mix.handle);
285 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700286 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800287 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700288 ALOGW("%s() bad capture I/O handle %d",
289 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800290 status = BAD_VALUE;
291 goto exit;
292 }
Eric Laurent83b88082014-06-20 18:31:16 -0700293 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700294 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700295 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700296 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
297 status = hwDevice->createAudioPatch(patch->num_sources,
298 patch->sources,
299 patch->num_sinks,
300 patch->sinks,
301 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700302 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700303 }
Eric Laurent951f4552014-05-20 10:48:17 -0700304 }
305 } break;
306 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700307 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700308 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700309 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700310 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700311 status = BAD_VALUE;
312 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700313 }
314 // limit to connections between devices and output streams
Eric Laurent054d9d32015-04-24 08:48:48 -0700315 audio_devices_t type = AUDIO_DEVICE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700316 for (unsigned int i = 0; i < patch->num_sinks; i++) {
317 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700318 ALOGW("%s() invalid sink type %d for mix source",
319 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700320 status = BAD_VALUE;
321 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700322 }
323 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700324 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700325 status = BAD_VALUE;
326 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700327 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700328 type |= patch->sinks[i].ext.device.type;
Eric Laurent951f4552014-05-20 10:48:17 -0700329 }
Eric Laurent951f4552014-05-20 10:48:17 -0700330 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700331 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700332 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700333 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800334 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700335 ALOGW("%s() bad playback I/O handle %d",
336 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800337 status = BAD_VALUE;
338 goto exit;
339 }
Eric Laurent951f4552014-05-20 10:48:17 -0700340 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700341 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
Eric Laurent054d9d32015-04-24 08:48:48 -0700342 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -0700343 param.addInt(String8(AudioParameter::keyRouting), (int)type);
Eric Laurent054d9d32015-04-24 08:48:48 -0700344
Mikhail Naganovdea53042018-04-26 13:10:21 -0700345 mAudioFlinger.broacastParametersToRecordThreads_l(param.toString());
Eric Laurent951f4552014-05-20 10:48:17 -0700346 }
347
Eric Laurent054d9d32015-04-24 08:48:48 -0700348 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700349 } break;
350 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700351 status = BAD_VALUE;
352 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700353 }
Eric Laurent83b88082014-06-20 18:31:16 -0700354exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700355 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700356 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700357 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
358 newPatch.mHalHandle = halHandle;
359 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
360 ALOGV("%s() added new patch handle %d halHandle %d", __func__, *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700361 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700362 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700363 }
364 return status;
365}
366
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700367AudioFlinger::PatchPanel::Patch::~Patch()
368{
369 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
370 mRecord.handle(), mPlayback.handle());
371}
372
Mikhail Naganovdea53042018-04-26 13:10:21 -0700373status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700374{
375 // create patch from source device to record thread input
376 struct audio_patch subPatch;
377 subPatch.num_sources = 1;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700378 subPatch.sources[0] = mAudioPatch.sources[0];
Eric Laurent83b88082014-06-20 18:31:16 -0700379 subPatch.num_sinks = 1;
380
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700381 mRecord.thread()->getAudioPortConfig(&subPatch.sinks[0]);
Eric Laurent83b88082014-06-20 18:31:16 -0700382 subPatch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_MIC;
383
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700384 status_t status = panel->createAudioPatch(&subPatch, mRecord.handlePtr());
Eric Laurent83b88082014-06-20 18:31:16 -0700385 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700386 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700387 return status;
388 }
389
390 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700391 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700392 mPlayback.thread()->getAudioPortConfig(&subPatch.sources[0]);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700393 subPatch.sinks[0] = mAudioPatch.sinks[0];
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700394 status = panel->createAudioPatch(&subPatch, mPlayback.handlePtr());
Eric Laurentd60560a2015-04-10 11:31:20 -0700395 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700396 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700397 return status;
398 }
399 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700400 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700401 }
402
403 // use a pseudo LCM between input and output framecount
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700404 size_t playbackFrameCount = mPlayback.thread()->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700405 int playbackShift = __builtin_ctz(playbackFrameCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700406 size_t recordFramecount = mRecord.thread()->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700407 int shift = __builtin_ctz(recordFramecount);
408 if (playbackShift < shift) {
409 shift = playbackShift;
410 }
411 size_t frameCount = (playbackFrameCount * recordFramecount) >> shift;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700412 ALOGV("%s() playframeCount %zu recordFramecount %zu frameCount %zu",
413 __func__, playbackFrameCount, recordFramecount, frameCount);
Eric Laurent83b88082014-06-20 18:31:16 -0700414
415 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700416 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700417 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700418 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
419 uint32_t sampleRate = mPlayback.thread()->sampleRate();
420 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700421
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700422 sp<RecordThread::PatchRecord> tempRecordTrack = new (std::nothrow) RecordThread::PatchRecord(
423 mRecord.thread().get(),
Eric Laurent83b88082014-06-20 18:31:16 -0700424 sampleRate,
425 inChannelMask,
426 format,
427 frameCount,
428 NULL,
Andy Hung8fe68032017-06-05 16:17:51 -0700429 (size_t)0 /* bufferSize */,
Eric Laurent05067782016-06-01 18:27:28 -0700430 AUDIO_INPUT_FLAG_NONE);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700431 status = mRecord.checkTrack(tempRecordTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700432 if (status != NO_ERROR) {
433 return status;
434 }
Eric Laurent83b88082014-06-20 18:31:16 -0700435
436 // create a special playback track to render to playback thread.
437 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700438 sp<PlaybackThread::PatchTrack> tempPatchTrack = new (std::nothrow) PlaybackThread::PatchTrack(
439 mPlayback.thread().get(),
Mikhail Naganovdea53042018-04-26 13:10:21 -0700440 mAudioPatch.sources[1].ext.mix.usecase.stream,
Eric Laurent83b88082014-06-20 18:31:16 -0700441 sampleRate,
442 outChannelMask,
443 format,
444 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700445 tempRecordTrack->buffer(),
446 tempRecordTrack->bufferSize(),
Eric Laurent05067782016-06-01 18:27:28 -0700447 AUDIO_OUTPUT_FLAG_NONE);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700448 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700449 if (status != NO_ERROR) {
450 return status;
451 }
Eric Laurent83b88082014-06-20 18:31:16 -0700452
453 // tie playback and record tracks together
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700454 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack.get());
455 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700456
457 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700458 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
459 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700460
461 return status;
462}
463
Mikhail Naganovdea53042018-04-26 13:10:21 -0700464void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700465{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700466 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
467 __func__, mRecord.handle(), mPlayback.handle());
468 mRecord.stopTrack();
469 mPlayback.stopTrack();
470 mRecord.closeConnections(panel);
471 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700472}
473
Eric Laurent951f4552014-05-20 10:48:17 -0700474/* Disconnect a patch */
475status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
476{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700477 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700478 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700479
Mikhail Naganovdea53042018-04-26 13:10:21 -0700480 auto iter = mPatches.find(handle);
481 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700482 return BAD_VALUE;
483 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700484 Patch &removedPatch = iter->second;
485 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700486
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700487 const struct audio_port_config &src = patch.sources[0];
488 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700489 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700490 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
491 if (hwDevice == 0) {
492 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700493 status = BAD_VALUE;
494 break;
495 }
Eric Laurent83b88082014-06-20 18:31:16 -0700496
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700497 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700498 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700499 break;
500 }
501
Mikhail Naganovdea53042018-04-26 13:10:21 -0700502 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
503 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
504 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700505 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700506 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800507 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700508 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800509 status = BAD_VALUE;
510 break;
511 }
Eric Laurent951f4552014-05-20 10:48:17 -0700512 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700513 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700514 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700515 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700516 }
517 } break;
518 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700519 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
520 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700521 status = BAD_VALUE;
522 break;
523 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700524 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700525 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700526 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700527 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800528 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700529 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800530 status = BAD_VALUE;
531 break;
532 }
Eric Laurent951f4552014-05-20 10:48:17 -0700533 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700534 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700535 } break;
536 default:
537 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700538 }
539
Mikhail Naganovdea53042018-04-26 13:10:21 -0700540 mPatches.erase(iter);
Eric Laurent951f4552014-05-20 10:48:17 -0700541 return status;
542}
543
Eric Laurent951f4552014-05-20 10:48:17 -0700544/* List connected audio ports and they attributes */
545status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
546 struct audio_patch *patches __unused)
547{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700548 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700549 return NO_ERROR;
550}
551
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700552sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
553{
554 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
555 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
556 if (index < 0) {
557 return nullptr;
558 }
559 return mAudioFlinger.mAudioHwDevs.valueAt(index)->hwDevice();
560}
561
Glenn Kasten63238ef2015-03-02 15:50:29 -0800562} // namespace android