blob: 3ae198bc8407fa9a56f12a55a0cc4ea006e9a0e1 [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;
128 halHandle = removedPatch.mHalHandle;
129 // 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
132 if ((removedPatch.mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE) ||
133 (removedPatch.mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE)) {
134 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700135 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700136 // 2) if the new patch and old patch source or sink are devices from different
137 // hw modules, clear the audio HAL patches now because they will not be updated
138 // by call to create_audio_patch() below which will happen on a different HW module
139 if (halHandle != AUDIO_PATCH_HANDLE_NONE) {
140 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
141 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
142 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
143 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
144 oldPatch.sources[0].ext.device.hw_module !=
145 patch->sources[0].ext.device.hw_module)) {
146 hwModule = oldPatch.sources[0].ext.device.hw_module;
147 } else if (patch->num_sinks == 0 ||
148 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
149 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
150 oldPatch.sinks[0].ext.device.hw_module !=
151 patch->sinks[0].ext.device.hw_module))) {
152 // Note on (patch->num_sinks == 0): this situation should not happen as
153 // these special patches are only created by the policy manager but just
154 // in case, systematically clear the HAL patch.
155 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
156 // halHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
157 hwModule = oldPatch.sinks[0].ext.device.hw_module;
158 }
159 if (hwModule != AUDIO_MODULE_HANDLE_NONE) {
160 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(hwModule);
161 if (index >= 0) {
162 sp<DeviceHalInterface> hwDevice =
163 mAudioFlinger.mAudioHwDevs.valueAt(index)->hwDevice();
164 hwDevice->releaseAudioPatch(halHandle);
165 }
166 }
167 }
168 mPatches.erase(iter);
Eric Laurent951f4552014-05-20 10:48:17 -0700169 }
170 }
171
Mikhail Naganovdea53042018-04-26 13:10:21 -0700172 Patch newPatch{*patch};
Eric Laurent83b88082014-06-20 18:31:16 -0700173
Eric Laurent951f4552014-05-20 10:48:17 -0700174 switch (patch->sources[0].type) {
175 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700176 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700177 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700178 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700179 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700180 status = BAD_VALUE;
181 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700182 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700183 AudioHwDevice *audioHwDevice = mAudioFlinger.mAudioHwDevs.valueAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700184 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700185 // support only one sink if connection to a mix or across HW modules
186 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
187 patch->sinks[i].ext.mix.hw_module != srcModule) &&
188 patch->num_sinks > 1) {
189 status = INVALID_OPERATION;
190 goto exit;
191 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700192 // reject connection to different sink types
193 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700194 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700195 status = BAD_VALUE;
196 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700197 }
Eric Laurent951f4552014-05-20 10:48:17 -0700198 }
199
Eric Laurent3bcf8592015-04-03 12:13:24 -0700200 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700201 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700202 // - Device to device AND
203 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700204 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700205 if ((patch->num_sources == 2) ||
206 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
207 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700208 !audioHwDevice->supportsAudioPatches()))) {
Eric Laurent83b88082014-06-20 18:31:16 -0700209 if (patch->num_sources == 2) {
210 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700211 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
212 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700213 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700214 status = INVALID_OPERATION;
215 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700216 }
Eric Laurent83b88082014-06-20 18:31:16 -0700217
218 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700219 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
220 newPatch.mPlaybackThread = (MixerThread *)thread.get();
Eric Laurent83b88082014-06-20 18:31:16 -0700221 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700222 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700223 status = INVALID_OPERATION;
224 goto exit;
225 }
Eric Laurent951f4552014-05-20 10:48:17 -0700226 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700227 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
228 audio_devices_t device = patch->sinks[0].ext.device.type;
229 String8 address = String8(patch->sinks[0].ext.device.address);
230 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700231 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800232 patch->sinks[0].ext.device.hw_module,
233 &output,
234 &config,
235 device,
236 address,
237 AUDIO_OUTPUT_FLAG_NONE);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700238 newPatch.mPlaybackThread = (PlaybackThread *)thread.get();
239 ALOGV("mAudioFlinger.openOutput_l() returned %p",
240 newPatch.mPlaybackThread.get());
241 if (newPatch.mPlaybackThread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700242 status = NO_MEMORY;
243 goto exit;
244 }
245 }
Eric Laurent83b88082014-06-20 18:31:16 -0700246 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700247 String8 address = String8(patch->sources[0].ext.device.address);
248 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700249 // open input stream with source device audio properties if provided or
250 // default to peer output stream properties otherwise.
251 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
252 config.sample_rate = patch->sources[0].sample_rate;
253 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700254 config.sample_rate = newPatch.mPlaybackThread->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700255 }
256 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
257 config.channel_mask = patch->sources[0].channel_mask;
258 } else {
259 config.channel_mask =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700260 audio_channel_in_mask_from_count(newPatch.mPlaybackThread->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700261 }
262 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
263 config.format = patch->sources[0].format;
264 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700265 config.format = newPatch.mPlaybackThread->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700266 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700267 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700268 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700269 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700270 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700271 device,
272 address,
273 AUDIO_SOURCE_MIC,
Eric Laurent83b88082014-06-20 18:31:16 -0700274 AUDIO_INPUT_FLAG_NONE);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700275 newPatch.mRecordThread = (RecordThread *)thread.get();
276 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
277 newPatch.mRecordThread.get(), config.channel_mask);
278 if (newPatch.mRecordThread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700279 status = NO_MEMORY;
280 goto exit;
281 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700282 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700283 if (status != NO_ERROR) {
284 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700285 }
286 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700287 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700288 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700289 patch->sinks[0].ext.mix.handle);
290 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700291 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800292 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700293 ALOGW("%s() bad capture I/O handle %d",
294 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800295 status = BAD_VALUE;
296 goto exit;
297 }
Eric Laurent83b88082014-06-20 18:31:16 -0700298 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700299 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700300 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700301 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
302 status = hwDevice->createAudioPatch(patch->num_sources,
303 patch->sources,
304 patch->num_sinks,
305 patch->sinks,
306 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700307 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700308 }
Eric Laurent951f4552014-05-20 10:48:17 -0700309 }
310 } break;
311 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700312 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700313 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700314 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700315 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700316 status = BAD_VALUE;
317 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700318 }
319 // limit to connections between devices and output streams
Eric Laurent054d9d32015-04-24 08:48:48 -0700320 audio_devices_t type = AUDIO_DEVICE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700321 for (unsigned int i = 0; i < patch->num_sinks; i++) {
322 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700323 ALOGW("%s() invalid sink type %d for mix source",
324 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700325 status = BAD_VALUE;
326 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700327 }
328 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700329 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700330 status = BAD_VALUE;
331 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700332 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700333 type |= patch->sinks[i].ext.device.type;
Eric Laurent951f4552014-05-20 10:48:17 -0700334 }
Eric Laurent951f4552014-05-20 10:48:17 -0700335 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700336 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700337 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700338 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800339 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700340 ALOGW("%s() bad playback I/O handle %d",
341 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800342 status = BAD_VALUE;
343 goto exit;
344 }
Eric Laurent951f4552014-05-20 10:48:17 -0700345 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700346 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
Eric Laurent054d9d32015-04-24 08:48:48 -0700347 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -0700348 param.addInt(String8(AudioParameter::keyRouting), (int)type);
Eric Laurent054d9d32015-04-24 08:48:48 -0700349
Mikhail Naganovdea53042018-04-26 13:10:21 -0700350 mAudioFlinger.broacastParametersToRecordThreads_l(param.toString());
Eric Laurent951f4552014-05-20 10:48:17 -0700351 }
352
Eric Laurent054d9d32015-04-24 08:48:48 -0700353 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700354 } break;
355 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700356 status = BAD_VALUE;
357 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700358 }
Eric Laurent83b88082014-06-20 18:31:16 -0700359exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700360 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700361 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700362 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
363 newPatch.mHalHandle = halHandle;
364 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
365 ALOGV("%s() added new patch handle %d halHandle %d", __func__, *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700366 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700367 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700368 }
369 return status;
370}
371
Mikhail Naganovdea53042018-04-26 13:10:21 -0700372status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700373{
374 // create patch from source device to record thread input
375 struct audio_patch subPatch;
376 subPatch.num_sources = 1;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700377 subPatch.sources[0] = mAudioPatch.sources[0];
Eric Laurent83b88082014-06-20 18:31:16 -0700378 subPatch.num_sinks = 1;
379
Mikhail Naganovdea53042018-04-26 13:10:21 -0700380 mRecordThread->getAudioPortConfig(&subPatch.sinks[0]);
Eric Laurent83b88082014-06-20 18:31:16 -0700381 subPatch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_MIC;
382
Mikhail Naganovdea53042018-04-26 13:10:21 -0700383 status_t status = panel->createAudioPatch(&subPatch, &mRecordPatchHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700384 if (status != NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700385 mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700386 return status;
387 }
388
389 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700390 if (mAudioPatch.num_sinks != 0) {
391 mPlaybackThread->getAudioPortConfig(&subPatch.sources[0]);
392 subPatch.sinks[0] = mAudioPatch.sinks[0];
393 status = panel->createAudioPatch(&subPatch, &mPlaybackPatchHandle);
Eric Laurentd60560a2015-04-10 11:31:20 -0700394 if (status != NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700395 mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700396 return status;
397 }
398 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700399 mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700400 }
401
402 // use a pseudo LCM between input and output framecount
Mikhail Naganovdea53042018-04-26 13:10:21 -0700403 size_t playbackFrameCount = mPlaybackThread->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700404 int playbackShift = __builtin_ctz(playbackFrameCount);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700405 size_t recordFramecount = mRecordThread->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700406 int shift = __builtin_ctz(recordFramecount);
407 if (playbackShift < shift) {
408 shift = playbackShift;
409 }
410 size_t frameCount = (playbackFrameCount * recordFramecount) >> shift;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700411 ALOGV("%s() playframeCount %zu recordFramecount %zu frameCount %zu",
412 __func__, playbackFrameCount, recordFramecount, frameCount);
Eric Laurent83b88082014-06-20 18:31:16 -0700413
414 // create a special record track to capture from record thread
Mikhail Naganovdea53042018-04-26 13:10:21 -0700415 uint32_t channelCount = mPlaybackThread->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700416 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700417 audio_channel_mask_t outChannelMask = mPlaybackThread->channelMask();
418 uint32_t sampleRate = mPlaybackThread->sampleRate();
419 audio_format_t format = mPlaybackThread->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700420
Mikhail Naganovdea53042018-04-26 13:10:21 -0700421 mPatchRecord = new RecordThread::PatchRecord(
422 mRecordThread.get(),
Eric Laurent83b88082014-06-20 18:31:16 -0700423 sampleRate,
424 inChannelMask,
425 format,
426 frameCount,
427 NULL,
Andy Hung8fe68032017-06-05 16:17:51 -0700428 (size_t)0 /* bufferSize */,
Eric Laurent05067782016-06-01 18:27:28 -0700429 AUDIO_INPUT_FLAG_NONE);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700430 if (mPatchRecord == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700431 return NO_MEMORY;
432 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700433 status = mPatchRecord->initCheck();
Eric Laurent83b88082014-06-20 18:31:16 -0700434 if (status != NO_ERROR) {
435 return status;
436 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700437 mRecordThread->addPatchRecord(mPatchRecord);
Eric Laurent83b88082014-06-20 18:31:16 -0700438
439 // create a special playback track to render to playback thread.
440 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganovdea53042018-04-26 13:10:21 -0700441 mPatchTrack = new PlaybackThread::PatchTrack(
442 mPlaybackThread.get(),
443 mAudioPatch.sources[1].ext.mix.usecase.stream,
Eric Laurent83b88082014-06-20 18:31:16 -0700444 sampleRate,
445 outChannelMask,
446 format,
447 frameCount,
Mikhail Naganovdea53042018-04-26 13:10:21 -0700448 mPatchRecord->buffer(),
449 mPatchRecord->bufferSize(),
Eric Laurent05067782016-06-01 18:27:28 -0700450 AUDIO_OUTPUT_FLAG_NONE);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700451 status = mPatchTrack->initCheck();
Eric Laurent83b88082014-06-20 18:31:16 -0700452 if (status != NO_ERROR) {
453 return status;
454 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700455 mPlaybackThread->addPatchTrack(mPatchTrack);
Eric Laurent83b88082014-06-20 18:31:16 -0700456
457 // tie playback and record tracks together
Mikhail Naganovdea53042018-04-26 13:10:21 -0700458 mPatchRecord->setPeerProxy(mPatchTrack.get());
459 mPatchTrack->setPeerProxy(mPatchRecord.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700460
461 // start capture and playback
Mikhail Naganovdea53042018-04-26 13:10:21 -0700462 mPatchRecord->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
463 mPatchTrack->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700464
465 return status;
466}
467
Mikhail Naganovdea53042018-04-26 13:10:21 -0700468void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700469{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700470 ALOGV("%s() mRecordPatchHandle %d mPlaybackPatchHandle %d",
471 __func__, mRecordPatchHandle, mPlaybackPatchHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700472
Mikhail Naganovdea53042018-04-26 13:10:21 -0700473 if (mPatchRecord != 0) {
474 mPatchRecord->stop();
Eric Laurent83b88082014-06-20 18:31:16 -0700475 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700476 if (mPatchTrack != 0) {
477 mPatchTrack->stop();
Eric Laurent83b88082014-06-20 18:31:16 -0700478 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700479 if (mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
480 panel->releaseAudioPatch(mRecordPatchHandle);
481 mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700482 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700483 if (mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
484 panel->releaseAudioPatch(mPlaybackPatchHandle);
485 mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700486 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700487 if (mRecordThread != 0) {
488 if (mPatchRecord != 0) {
489 mRecordThread->deletePatchRecord(mPatchRecord);
Eric Laurent83b88082014-06-20 18:31:16 -0700490 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700491 panel->mAudioFlinger.closeInputInternal_l(mRecordThread);
Eric Laurent83b88082014-06-20 18:31:16 -0700492 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700493 if (mPlaybackThread != 0) {
494 if (mPatchTrack != 0) {
495 mPlaybackThread->deletePatchTrack(mPatchTrack);
Eric Laurent83b88082014-06-20 18:31:16 -0700496 }
497 // if num sources == 2 we are reusing an existing playback thread so we do not close it
Mikhail Naganovdea53042018-04-26 13:10:21 -0700498 if (mAudioPatch.num_sources != 2) {
499 panel->mAudioFlinger.closeOutputInternal_l(mPlaybackThread);
Eric Laurent83b88082014-06-20 18:31:16 -0700500 }
Eric Laurenta0169a02015-07-06 18:32:01 -0700501 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700502 if (mRecordThread != 0) {
503 if (mPatchRecord != 0) {
504 mPatchRecord.clear();
Eric Laurenta0169a02015-07-06 18:32:01 -0700505 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700506 mRecordThread.clear();
Eric Laurenta0169a02015-07-06 18:32:01 -0700507 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700508 if (mPlaybackThread != 0) {
509 if (mPatchTrack != 0) {
510 mPatchTrack.clear();
Eric Laurenta0169a02015-07-06 18:32:01 -0700511 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700512 mPlaybackThread.clear();
Eric Laurent83b88082014-06-20 18:31:16 -0700513 }
Eric Laurenta0169a02015-07-06 18:32:01 -0700514
Eric Laurent83b88082014-06-20 18:31:16 -0700515}
516
Eric Laurent951f4552014-05-20 10:48:17 -0700517/* Disconnect a patch */
518status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
519{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700520 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700521 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700522
Mikhail Naganovdea53042018-04-26 13:10:21 -0700523 auto iter = mPatches.find(handle);
524 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700525 return BAD_VALUE;
526 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700527 Patch &removedPatch = iter->second;
528 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700529
Mikhail Naganovdea53042018-04-26 13:10:21 -0700530 switch (patch.sources[0].type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700531 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700532 audio_module_handle_t srcModule = patch.sources[0].ext.device.hw_module;
533 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700534 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700535 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700536 status = BAD_VALUE;
537 break;
538 }
Eric Laurent83b88082014-06-20 18:31:16 -0700539
Mikhail Naganovdea53042018-04-26 13:10:21 -0700540 if (removedPatch.mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE ||
541 removedPatch.mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
542 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700543 break;
544 }
545
Mikhail Naganovdea53042018-04-26 13:10:21 -0700546 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
547 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
548 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700549 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700550 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800551 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700552 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800553 status = BAD_VALUE;
554 break;
555 }
Eric Laurent951f4552014-05-20 10:48:17 -0700556 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700557 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700558 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700559 AudioHwDevice *audioHwDevice = mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700560 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700561 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700562 }
563 } break;
564 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700565 audio_module_handle_t srcModule = patch.sources[0].ext.mix.hw_module;
566 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700567 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700568 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700569 status = BAD_VALUE;
570 break;
571 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700572 audio_io_handle_t ioHandle = patch.sources[0].ext.mix.handle;
573 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700574 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700575 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800576 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700577 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800578 status = BAD_VALUE;
579 break;
580 }
Eric Laurent951f4552014-05-20 10:48:17 -0700581 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700582 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700583 } break;
584 default:
585 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700586 }
587
Mikhail Naganovdea53042018-04-26 13:10:21 -0700588 mPatches.erase(iter);
Eric Laurent951f4552014-05-20 10:48:17 -0700589 return status;
590}
591
Eric Laurent951f4552014-05-20 10:48:17 -0700592/* List connected audio ports and they attributes */
593status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
594 struct audio_patch *patches __unused)
595{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700596 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700597 return NO_ERROR;
598}
599
Glenn Kasten63238ef2015-03-02 15:50:29 -0800600} // namespace android