blob: a08da96dd5400d93ecca2a9baac4d760bad41b13 [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
86/* List connected audio ports and their attributes */
87status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
88 struct audio_port *ports __unused)
89{
Mikhail Naganovdea53042018-04-26 13:10:21 -070090 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -070091 return NO_ERROR;
92}
93
94/* Get supported attributes for a given audio port */
95status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port *port __unused)
96{
Mikhail Naganovdea53042018-04-26 13:10:21 -070097 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -070098 return NO_ERROR;
99}
100
Eric Laurent951f4552014-05-20 10:48:17 -0700101/* Connect a patch between several source and sink ports */
102status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
103 audio_patch_handle_t *handle)
104{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700105 if (handle == NULL || patch == NULL) {
106 return BAD_VALUE;
107 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700108 ALOGV("%s() num_sources %d num_sinks %d handle %d",
109 __func__, patch->num_sources, patch->num_sinks, *handle);
110 status_t status = NO_ERROR;
111 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700112
Eric Laurent874c42872014-08-08 15:13:39 -0700113 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700114 (patch->num_sinks == 0 && patch->num_sources != 2) ||
115 patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
Eric Laurent951f4552014-05-20 10:48:17 -0700116 return BAD_VALUE;
117 }
Eric Laurent874c42872014-08-08 15:13:39 -0700118 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
119 // only the audio policy manager can request a patch creation with 2 sources.
120 if (patch->num_sources > 2) {
121 return INVALID_OPERATION;
122 }
Eric Laurent951f4552014-05-20 10:48:17 -0700123
Eric Laurent83b88082014-06-20 18:31:16 -0700124 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700125 auto iter = mPatches.find(*handle);
126 if (iter != mPatches.end()) {
127 ALOGV("%s() removing patch handle %d", __func__, *handle);
128 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700129 // free resources owned by the removed patch if applicable
130 // 1) if a software patch is present, release the playback and capture threads and
131 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700132 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700133 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700134 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700135 // 2) if the new patch and old patch source or sink are devices from different
136 // hw modules, clear the audio HAL patches now because they will not be updated
137 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700138 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700139 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
140 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
141 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
142 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
143 oldPatch.sources[0].ext.device.hw_module !=
144 patch->sources[0].ext.device.hw_module)) {
145 hwModule = oldPatch.sources[0].ext.device.hw_module;
146 } else if (patch->num_sinks == 0 ||
147 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
148 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
149 oldPatch.sinks[0].ext.device.hw_module !=
150 patch->sinks[0].ext.device.hw_module))) {
151 // Note on (patch->num_sinks == 0): this situation should not happen as
152 // these special patches are only created by the policy manager but just
153 // in case, systematically clear the HAL patch.
154 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700155 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700156 hwModule = oldPatch.sinks[0].ext.device.hw_module;
157 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700158 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
159 if (hwDevice != 0) {
160 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700161 }
162 }
163 mPatches.erase(iter);
Eric Laurent951f4552014-05-20 10:48:17 -0700164 }
165 }
166
Mikhail Naganovdea53042018-04-26 13:10:21 -0700167 Patch newPatch{*patch};
Eric Laurent83b88082014-06-20 18:31:16 -0700168
Eric Laurent951f4552014-05-20 10:48:17 -0700169 switch (patch->sources[0].type) {
170 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700171 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700172 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700173 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700174 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700175 status = BAD_VALUE;
176 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700177 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700178 AudioHwDevice *audioHwDevice = mAudioFlinger.mAudioHwDevs.valueAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700179 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700180 // support only one sink if connection to a mix or across HW modules
181 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700182 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
183 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700184 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700185 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700186 status = INVALID_OPERATION;
187 goto exit;
188 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700189 // reject connection to different sink types
190 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700191 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700192 status = BAD_VALUE;
193 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700194 }
Eric Laurent951f4552014-05-20 10:48:17 -0700195 }
196
Eric Laurent3bcf8592015-04-03 12:13:24 -0700197 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700198 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700199 // - Device to device AND
200 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700201 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700202 if ((patch->num_sources == 2) ||
203 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
204 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700205 !audioHwDevice->supportsAudioPatches()))) {
Eric Laurent83b88082014-06-20 18:31:16 -0700206 if (patch->num_sources == 2) {
207 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700208 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
209 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700210 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700211 status = INVALID_OPERATION;
212 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700213 }
Eric Laurent83b88082014-06-20 18:31:16 -0700214
215 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700216 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700217 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700218 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700219 status = INVALID_OPERATION;
220 goto exit;
221 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700222 // existing playback thread is reused, so it is not closed when patch is cleared
223 newPatch.mPlayback.setThread(
224 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700225 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700226 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
227 audio_devices_t device = patch->sinks[0].ext.device.type;
228 String8 address = String8(patch->sinks[0].ext.device.address);
229 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700230 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800231 patch->sinks[0].ext.device.hw_module,
232 &output,
233 &config,
234 device,
235 address,
236 AUDIO_OUTPUT_FLAG_NONE);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700237 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
238 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700239 status = NO_MEMORY;
240 goto exit;
241 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700242 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Eric Laurent83b88082014-06-20 18:31:16 -0700243 }
Eric Laurent83b88082014-06-20 18:31:16 -0700244 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700245 String8 address = String8(patch->sources[0].ext.device.address);
246 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700247 // open input stream with source device audio properties if provided or
248 // default to peer output stream properties otherwise.
249 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
250 config.sample_rate = patch->sources[0].sample_rate;
251 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700252 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700253 }
254 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
255 config.channel_mask = patch->sources[0].channel_mask;
256 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700257 config.channel_mask = audio_channel_in_mask_from_count(
258 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700259 }
260 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
261 config.format = patch->sources[0].format;
262 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700263 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700264 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700265 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700266 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700267 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700268 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700269 device,
270 address,
271 AUDIO_SOURCE_MIC,
Eric Laurent83b88082014-06-20 18:31:16 -0700272 AUDIO_INPUT_FLAG_NONE);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700273 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700274 thread.get(), config.channel_mask);
275 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700276 status = NO_MEMORY;
277 goto exit;
278 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700279 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700280 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700281 if (status != NO_ERROR) {
282 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700283 }
284 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700285 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700286 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700287 patch->sinks[0].ext.mix.handle);
288 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700289 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800290 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700291 ALOGW("%s() bad capture I/O handle %d",
292 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800293 status = BAD_VALUE;
294 goto exit;
295 }
Eric Laurent83b88082014-06-20 18:31:16 -0700296 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700297 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700298 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700299 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
300 status = hwDevice->createAudioPatch(patch->num_sources,
301 patch->sources,
302 patch->num_sinks,
303 patch->sinks,
304 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700305 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700306 }
Eric Laurent951f4552014-05-20 10:48:17 -0700307 }
308 } break;
309 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700310 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700311 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700312 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700313 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700314 status = BAD_VALUE;
315 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700316 }
317 // limit to connections between devices and output streams
Eric Laurent054d9d32015-04-24 08:48:48 -0700318 audio_devices_t type = AUDIO_DEVICE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700319 for (unsigned int i = 0; i < patch->num_sinks; i++) {
320 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700321 ALOGW("%s() invalid sink type %d for mix source",
322 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700323 status = BAD_VALUE;
324 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700325 }
326 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700327 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700328 status = BAD_VALUE;
329 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700330 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700331 type |= patch->sinks[i].ext.device.type;
Eric Laurent951f4552014-05-20 10:48:17 -0700332 }
Eric Laurent951f4552014-05-20 10:48:17 -0700333 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700334 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700335 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700336 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800337 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700338 ALOGW("%s() bad playback I/O handle %d",
339 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800340 status = BAD_VALUE;
341 goto exit;
342 }
Eric Laurent951f4552014-05-20 10:48:17 -0700343 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700344 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
Eric Laurent054d9d32015-04-24 08:48:48 -0700345 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -0700346 param.addInt(String8(AudioParameter::keyRouting), (int)type);
Eric Laurent054d9d32015-04-24 08:48:48 -0700347
Mikhail Naganovdea53042018-04-26 13:10:21 -0700348 mAudioFlinger.broacastParametersToRecordThreads_l(param.toString());
Eric Laurent951f4552014-05-20 10:48:17 -0700349 }
350
Eric Laurent054d9d32015-04-24 08:48:48 -0700351 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700352 } break;
353 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700354 status = BAD_VALUE;
355 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700356 }
Eric Laurent83b88082014-06-20 18:31:16 -0700357exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700358 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700359 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700360 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
361 newPatch.mHalHandle = halHandle;
362 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
363 ALOGV("%s() added new patch handle %d halHandle %d", __func__, *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700364 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700365 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700366 }
367 return status;
368}
369
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700370AudioFlinger::PatchPanel::Patch::~Patch()
371{
372 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
373 mRecord.handle(), mPlayback.handle());
374}
375
Mikhail Naganovdea53042018-04-26 13:10:21 -0700376status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700377{
378 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700379 status_t status = panel->createAudioPatch(
380 PatchBuilder().addSource(mAudioPatch.sources[0]).
381 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
382 mRecord.handlePtr());
Eric Laurent83b88082014-06-20 18:31:16 -0700383 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700384 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700385 return status;
386 }
387
388 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700389 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700390 status = panel->createAudioPatch(
391 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
392 mPlayback.handlePtr());
Eric Laurentd60560a2015-04-10 11:31:20 -0700393 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700394 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700395 return status;
396 }
397 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700398 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700399 }
400
401 // use a pseudo LCM between input and output framecount
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700402 size_t playbackFrameCount = mPlayback.thread()->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700403 int playbackShift = __builtin_ctz(playbackFrameCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700404 size_t recordFramecount = mRecord.thread()->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700405 int shift = __builtin_ctz(recordFramecount);
406 if (playbackShift < shift) {
407 shift = playbackShift;
408 }
409 size_t frameCount = (playbackFrameCount * recordFramecount) >> shift;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700410 ALOGV("%s() playframeCount %zu recordFramecount %zu frameCount %zu",
411 __func__, playbackFrameCount, recordFramecount, frameCount);
Eric Laurent83b88082014-06-20 18:31:16 -0700412
413 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700414 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700415 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700416 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
417 uint32_t sampleRate = mPlayback.thread()->sampleRate();
418 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700419
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700420 sp<RecordThread::PatchRecord> tempRecordTrack = new (std::nothrow) RecordThread::PatchRecord(
421 mRecord.thread().get(),
Eric Laurent83b88082014-06-20 18:31:16 -0700422 sampleRate,
423 inChannelMask,
424 format,
425 frameCount,
426 NULL,
Andy Hung8fe68032017-06-05 16:17:51 -0700427 (size_t)0 /* bufferSize */,
Eric Laurent05067782016-06-01 18:27:28 -0700428 AUDIO_INPUT_FLAG_NONE);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700429 status = mRecord.checkTrack(tempRecordTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700430 if (status != NO_ERROR) {
431 return status;
432 }
Eric Laurent83b88082014-06-20 18:31:16 -0700433
434 // create a special playback track to render to playback thread.
435 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700436 sp<PlaybackThread::PatchTrack> tempPatchTrack = new (std::nothrow) PlaybackThread::PatchTrack(
437 mPlayback.thread().get(),
Mikhail Naganovdea53042018-04-26 13:10:21 -0700438 mAudioPatch.sources[1].ext.mix.usecase.stream,
Eric Laurent83b88082014-06-20 18:31:16 -0700439 sampleRate,
440 outChannelMask,
441 format,
442 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700443 tempRecordTrack->buffer(),
444 tempRecordTrack->bufferSize(),
Eric Laurent05067782016-06-01 18:27:28 -0700445 AUDIO_OUTPUT_FLAG_NONE);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700446 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700447 if (status != NO_ERROR) {
448 return status;
449 }
Eric Laurent83b88082014-06-20 18:31:16 -0700450
451 // tie playback and record tracks together
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700452 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack.get());
453 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700454
455 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700456 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
457 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700458
459 return status;
460}
461
Mikhail Naganovdea53042018-04-26 13:10:21 -0700462void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700463{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700464 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
465 __func__, mRecord.handle(), mPlayback.handle());
466 mRecord.stopTrack();
467 mPlayback.stopTrack();
468 mRecord.closeConnections(panel);
469 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700470}
471
Mikhail Naganov201369b2018-05-16 16:52:32 -0700472String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle)
473{
474 String8 result;
475 result.appendFormat("Patch %d: thread %p => thread %p\n",
476 myHandle, mRecord.thread().get(), mPlayback.thread().get());
477 return result;
478}
479
Eric Laurent951f4552014-05-20 10:48:17 -0700480/* Disconnect a patch */
481status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
482{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700483 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700484 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700485
Mikhail Naganovdea53042018-04-26 13:10:21 -0700486 auto iter = mPatches.find(handle);
487 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700488 return BAD_VALUE;
489 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700490 Patch &removedPatch = iter->second;
491 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700492
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700493 const struct audio_port_config &src = patch.sources[0];
494 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700495 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700496 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
497 if (hwDevice == 0) {
498 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700499 status = BAD_VALUE;
500 break;
501 }
Eric Laurent83b88082014-06-20 18:31:16 -0700502
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700503 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700504 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700505 break;
506 }
507
Mikhail Naganovdea53042018-04-26 13:10:21 -0700508 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
509 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
510 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700511 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700512 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800513 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700514 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800515 status = BAD_VALUE;
516 break;
517 }
Eric Laurent951f4552014-05-20 10:48:17 -0700518 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700519 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700520 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700521 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700522 }
523 } break;
524 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700525 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
526 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700527 status = BAD_VALUE;
528 break;
529 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700530 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700531 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700532 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700533 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800534 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700535 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800536 status = BAD_VALUE;
537 break;
538 }
Eric Laurent951f4552014-05-20 10:48:17 -0700539 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700540 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700541 } break;
542 default:
543 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700544 }
545
Mikhail Naganovdea53042018-04-26 13:10:21 -0700546 mPatches.erase(iter);
Eric Laurent951f4552014-05-20 10:48:17 -0700547 return status;
548}
549
Eric Laurent951f4552014-05-20 10:48:17 -0700550/* List connected audio ports and they attributes */
551status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
552 struct audio_patch *patches __unused)
553{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700554 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700555 return NO_ERROR;
556}
557
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700558sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
559{
560 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
561 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
562 if (index < 0) {
563 return nullptr;
564 }
565 return mAudioFlinger.mAudioHwDevs.valueAt(index)->hwDevice();
566}
567
Mikhail Naganov201369b2018-05-16 16:52:32 -0700568void AudioFlinger::PatchPanel::dump(int fd)
569{
570 // Only dump software patches.
571 bool headerPrinted = false;
572 for (auto& iter : mPatches) {
573 if (iter.second.isSoftware()) {
574 if (!headerPrinted) {
575 String8 header("\nSoftware patches:\n");
576 write(fd, header.string(), header.size());
577 headerPrinted = true;
578 }
579 String8 patchDump(" ");
580 patchDump.append(iter.second.dump(iter.first));
581 write(fd, patchDump.string(), patchDump.size());
582 }
583 }
584 if (headerPrinted) {
585 String8 trailing("\n");
586 write(fd, trailing.string(), trailing.size());
587 }
588}
589
Glenn Kasten63238ef2015-03-02 15:50:29 -0800590} // namespace android