blob: 93f634ac3b85ec9cfa3a10db7bee07fd934d248e [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
Mikhail Naganov00260b52016-10-13 12:54:24 -070030#include <hardware/audio.h> // for AUDIO_DEVICE_API_VERSION_...
31
Eric Laurent951f4552014-05-20 10:48:17 -070032// ----------------------------------------------------------------------------
33
34// Note: the following macro is used for extremely verbose logging message. In
35// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
36// 0; but one side effect of this is to turn all LOGV's as well. Some messages
37// are so verbose that we want to suppress them even when we have ALOG_ASSERT
38// turned on. Do not uncomment the #def below unless you really know what you
39// are doing and want to see all of the extremely verbose messages.
40//#define VERY_VERY_VERBOSE_LOGGING
41#ifdef VERY_VERY_VERBOSE_LOGGING
42#define ALOGVV ALOGV
43#else
44#define ALOGVV(a...) do { } while(0)
45#endif
46
47namespace android {
48
49/* List connected audio ports and their attributes */
50status_t AudioFlinger::listAudioPorts(unsigned int *num_ports,
51 struct audio_port *ports)
52{
53 Mutex::Autolock _l(mLock);
54 if (mPatchPanel != 0) {
55 return mPatchPanel->listAudioPorts(num_ports, ports);
56 }
57 return NO_INIT;
58}
59
60/* Get supported attributes for a given audio port */
61status_t AudioFlinger::getAudioPort(struct audio_port *port)
62{
63 Mutex::Autolock _l(mLock);
64 if (mPatchPanel != 0) {
65 return mPatchPanel->getAudioPort(port);
66 }
67 return NO_INIT;
68}
69
70
71/* Connect a patch between several source and sink ports */
72status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
73 audio_patch_handle_t *handle)
74{
75 Mutex::Autolock _l(mLock);
76 if (mPatchPanel != 0) {
77 return mPatchPanel->createAudioPatch(patch, handle);
78 }
79 return NO_INIT;
80}
81
82/* Disconnect a patch */
83status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
84{
85 Mutex::Autolock _l(mLock);
86 if (mPatchPanel != 0) {
87 return mPatchPanel->releaseAudioPatch(handle);
88 }
89 return NO_INIT;
90}
91
92
93/* List connected audio ports and they attributes */
94status_t AudioFlinger::listAudioPatches(unsigned int *num_patches,
95 struct audio_patch *patches)
96{
97 Mutex::Autolock _l(mLock);
98 if (mPatchPanel != 0) {
99 return mPatchPanel->listAudioPatches(num_patches, patches);
100 }
101 return NO_INIT;
102}
103
104/* Set audio port configuration */
105status_t AudioFlinger::setAudioPortConfig(const struct audio_port_config *config)
106{
107 Mutex::Autolock _l(mLock);
108 if (mPatchPanel != 0) {
109 return mPatchPanel->setAudioPortConfig(config);
110 }
111 return NO_INIT;
112}
113
114
115AudioFlinger::PatchPanel::PatchPanel(const sp<AudioFlinger>& audioFlinger)
116 : mAudioFlinger(audioFlinger)
117{
118}
119
120AudioFlinger::PatchPanel::~PatchPanel()
121{
122}
123
124/* List connected audio ports and their attributes */
125status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
126 struct audio_port *ports __unused)
127{
128 ALOGV("listAudioPorts");
129 return NO_ERROR;
130}
131
132/* Get supported attributes for a given audio port */
133status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port *port __unused)
134{
135 ALOGV("getAudioPort");
136 return NO_ERROR;
137}
138
139
140/* Connect a patch between several source and sink ports */
141status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
142 audio_patch_handle_t *handle)
143{
Eric Laurent951f4552014-05-20 10:48:17 -0700144 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700145 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700146 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
Greg Kaiserf27ce402016-03-14 13:43:14 -0700147 if (handle == NULL || patch == NULL) {
148 return BAD_VALUE;
149 }
150 ALOGV("createAudioPatch() num_sources %d num_sinks %d handle %d",
151 patch->num_sources, patch->num_sinks, *handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700152 if (audioflinger == 0) {
153 return NO_INIT;
154 }
Eric Laurent83b88082014-06-20 18:31:16 -0700155
Eric Laurent874c42872014-08-08 15:13:39 -0700156 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700157 (patch->num_sinks == 0 && patch->num_sources != 2) ||
158 patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
Eric Laurent951f4552014-05-20 10:48:17 -0700159 return BAD_VALUE;
160 }
Eric Laurent874c42872014-08-08 15:13:39 -0700161 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
162 // only the audio policy manager can request a patch creation with 2 sources.
163 if (patch->num_sources > 2) {
164 return INVALID_OPERATION;
165 }
Eric Laurent951f4552014-05-20 10:48:17 -0700166
Eric Laurent83b88082014-06-20 18:31:16 -0700167 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
168 for (size_t index = 0; *handle != 0 && index < mPatches.size(); index++) {
169 if (*handle == mPatches[index]->mHandle) {
170 ALOGV("createAudioPatch() removing patch handle %d", *handle);
171 halHandle = mPatches[index]->mHalHandle;
soon1.choid8cd4772015-01-05 14:27:42 +0900172 Patch *removedPatch = mPatches[index];
Eric Laurentb997d3a2016-06-07 18:23:45 -0700173 // free resources owned by the removed patch if applicable
174 // 1) if a software patch is present, release the playback and capture threads and
175 // tracks created. This will also release the corresponding audio HAL patches
Eric Laurent0666cc52015-12-15 10:14:06 -0800176 if ((removedPatch->mRecordPatchHandle
177 != AUDIO_PATCH_HANDLE_NONE) ||
178 (removedPatch->mPlaybackPatchHandle !=
179 AUDIO_PATCH_HANDLE_NONE)) {
180 clearPatchConnections(removedPatch);
181 }
Eric Laurentb997d3a2016-06-07 18:23:45 -0700182 // 2) if the new patch and old patch source or sink are devices from different
183 // hw modules, clear the audio HAL patches now because they will not be updated
184 // by call to create_audio_patch() below which will happen on a different HW module
185 if (halHandle != AUDIO_PATCH_HANDLE_NONE) {
186 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
187 if ((removedPatch->mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE) &&
188 ((patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE) ||
189 (removedPatch->mAudioPatch.sources[0].ext.device.hw_module !=
190 patch->sources[0].ext.device.hw_module))) {
191 hwModule = removedPatch->mAudioPatch.sources[0].ext.device.hw_module;
192 } else if ((patch->num_sinks == 0) ||
193 ((removedPatch->mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
194 ((patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE) ||
195 (removedPatch->mAudioPatch.sinks[0].ext.device.hw_module !=
196 patch->sinks[0].ext.device.hw_module)))) {
197 // Note on (patch->num_sinks == 0): this situation should not happen as
198 // these special patches are only created by the policy manager but just
199 // in case, systematically clear the HAL patch.
200 // Note that removedPatch->mAudioPatch.num_sinks cannot be 0 here because
201 // halHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
202 hwModule = removedPatch->mAudioPatch.sinks[0].ext.device.hw_module;
203 }
204 if (hwModule != AUDIO_MODULE_HANDLE_NONE) {
205 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(hwModule);
206 if (index >= 0) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700207 sp<DeviceHalInterface> hwDevice =
Eric Laurentb997d3a2016-06-07 18:23:45 -0700208 audioflinger->mAudioHwDevs.valueAt(index)->hwDevice();
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700209 hwDevice->releaseAudioPatch(halHandle);
Eric Laurentb997d3a2016-06-07 18:23:45 -0700210 }
211 }
212 }
Eric Laurent83b88082014-06-20 18:31:16 -0700213 mPatches.removeAt(index);
soon1.choid8cd4772015-01-05 14:27:42 +0900214 delete removedPatch;
Eric Laurent83b88082014-06-20 18:31:16 -0700215 break;
216 }
Eric Laurent951f4552014-05-20 10:48:17 -0700217 }
218 }
219
Eric Laurent83b88082014-06-20 18:31:16 -0700220 Patch *newPatch = new Patch(patch);
221
Eric Laurent951f4552014-05-20 10:48:17 -0700222 switch (patch->sources[0].type) {
223 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700224 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
225 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700226 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700227 ALOGW("createAudioPatch() bad src hw module %d", srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700228 status = BAD_VALUE;
229 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700230 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700231 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700232 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700233 // support only one sink if connection to a mix or across HW modules
234 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
235 patch->sinks[i].ext.mix.hw_module != srcModule) &&
236 patch->num_sinks > 1) {
237 status = INVALID_OPERATION;
238 goto exit;
239 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700240 // reject connection to different sink types
241 if (patch->sinks[i].type != patch->sinks[0].type) {
242 ALOGW("createAudioPatch() different sink types in same patch not supported");
Eric Laurent83b88082014-06-20 18:31:16 -0700243 status = BAD_VALUE;
244 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700245 }
Eric Laurent951f4552014-05-20 10:48:17 -0700246 }
247
Eric Laurent3bcf8592015-04-03 12:13:24 -0700248 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700249 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700250 // - Device to device AND
251 // - source HW module != destination HW module OR
252 // - audio HAL version < 3.0
Eric Laurentd60560a2015-04-10 11:31:20 -0700253 if ((patch->num_sources == 2) ||
254 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
255 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
256 (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0)))) {
Eric Laurent83b88082014-06-20 18:31:16 -0700257 if (patch->num_sources == 2) {
258 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700259 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
260 patch->sources[1].ext.mix.hw_module)) {
Eric Laurent83b88082014-06-20 18:31:16 -0700261 ALOGW("createAudioPatch() invalid source combination");
262 status = INVALID_OPERATION;
263 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700264 }
Eric Laurent83b88082014-06-20 18:31:16 -0700265
266 sp<ThreadBase> thread =
267 audioflinger->checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
268 newPatch->mPlaybackThread = (MixerThread *)thread.get();
269 if (thread == 0) {
270 ALOGW("createAudioPatch() cannot get playback thread");
271 status = INVALID_OPERATION;
272 goto exit;
273 }
Eric Laurent951f4552014-05-20 10:48:17 -0700274 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700275 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
276 audio_devices_t device = patch->sinks[0].ext.device.type;
277 String8 address = String8(patch->sinks[0].ext.device.address);
278 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700279 newPatch->mPlaybackThread = audioflinger->openOutput_l(
280 patch->sinks[0].ext.device.hw_module,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700281 &output,
Eric Laurent83b88082014-06-20 18:31:16 -0700282 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700283 device,
284 address,
Eric Laurent83b88082014-06-20 18:31:16 -0700285 AUDIO_OUTPUT_FLAG_NONE);
286 ALOGV("audioflinger->openOutput_l() returned %p",
287 newPatch->mPlaybackThread.get());
288 if (newPatch->mPlaybackThread == 0) {
289 status = NO_MEMORY;
290 goto exit;
291 }
292 }
Eric Laurent83b88082014-06-20 18:31:16 -0700293 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700294 String8 address = String8(patch->sources[0].ext.device.address);
295 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700296 // open input stream with source device audio properties if provided or
297 // default to peer output stream properties otherwise.
298 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
299 config.sample_rate = patch->sources[0].sample_rate;
300 } else {
301 config.sample_rate = newPatch->mPlaybackThread->sampleRate();
302 }
303 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
304 config.channel_mask = patch->sources[0].channel_mask;
305 } else {
306 config.channel_mask =
307 audio_channel_in_mask_from_count(newPatch->mPlaybackThread->channelCount());
308 }
309 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
310 config.format = patch->sources[0].format;
311 } else {
312 config.format = newPatch->mPlaybackThread->format();
313 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700314 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent874c42872014-08-08 15:13:39 -0700315 newPatch->mRecordThread = audioflinger->openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700316 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700317 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700318 device,
319 address,
320 AUDIO_SOURCE_MIC,
Eric Laurent83b88082014-06-20 18:31:16 -0700321 AUDIO_INPUT_FLAG_NONE);
322 ALOGV("audioflinger->openInput_l() returned %p inChannelMask %08x",
Eric Laurent8ae73122016-04-12 10:13:29 -0700323 newPatch->mRecordThread.get(), config.channel_mask);
Eric Laurent83b88082014-06-20 18:31:16 -0700324 if (newPatch->mRecordThread == 0) {
325 status = NO_MEMORY;
326 goto exit;
327 }
328 status = createPatchConnections(newPatch, patch);
329 if (status != NO_ERROR) {
330 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700331 }
332 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700333 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
334 sp<ThreadBase> thread = audioflinger->checkRecordThread_l(
335 patch->sinks[0].ext.mix.handle);
336 if (thread == 0) {
337 ALOGW("createAudioPatch() bad capture I/O handle %d",
338 patch->sinks[0].ext.mix.handle);
339 status = BAD_VALUE;
340 goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700341 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700342 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700343 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700344 if (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0) {
Eric Laurent3bcf8592015-04-03 12:13:24 -0700345 status = INVALID_OPERATION;
346 goto exit;
347 }
348
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700349 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
350 status = hwDevice->createAudioPatch(patch->num_sources,
351 patch->sources,
352 patch->num_sinks,
353 patch->sinks,
354 &halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700355 }
Eric Laurent951f4552014-05-20 10:48:17 -0700356 }
357 } break;
358 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700359 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
360 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700361 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700362 ALOGW("createAudioPatch() bad src hw module %d", srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700363 status = BAD_VALUE;
364 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700365 }
366 // limit to connections between devices and output streams
Eric Laurent054d9d32015-04-24 08:48:48 -0700367 audio_devices_t type = AUDIO_DEVICE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700368 for (unsigned int i = 0; i < patch->num_sinks; i++) {
369 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent83b88082014-06-20 18:31:16 -0700370 ALOGW("createAudioPatch() invalid sink type %d for mix source",
Eric Laurent951f4552014-05-20 10:48:17 -0700371 patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700372 status = BAD_VALUE;
373 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700374 }
375 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700376 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700377 status = BAD_VALUE;
378 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700379 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700380 type |= patch->sinks[i].ext.device.type;
Eric Laurent951f4552014-05-20 10:48:17 -0700381 }
Eric Laurent951f4552014-05-20 10:48:17 -0700382 sp<ThreadBase> thread =
383 audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
384 if (thread == 0) {
385 ALOGW("createAudioPatch() bad playback I/O handle %d",
386 patch->sources[0].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700387 status = BAD_VALUE;
388 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700389 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700390 if (thread == audioflinger->primaryPlaybackThread_l()) {
391 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -0700392 param.addInt(String8(AudioParameter::keyRouting), (int)type);
Eric Laurent054d9d32015-04-24 08:48:48 -0700393
394 audioflinger->broacastParametersToRecordThreads_l(param.toString());
Eric Laurent951f4552014-05-20 10:48:17 -0700395 }
396
Eric Laurent054d9d32015-04-24 08:48:48 -0700397 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700398 } break;
399 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700400 status = BAD_VALUE;
401 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700402 }
Eric Laurent83b88082014-06-20 18:31:16 -0700403exit:
Eric Laurent951f4552014-05-20 10:48:17 -0700404 ALOGV("createAudioPatch() status %d", status);
405 if (status == NO_ERROR) {
Glenn Kastena13cde92016-03-28 15:26:02 -0700406 *handle = (audio_patch_handle_t) audioflinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
Eric Laurent951f4552014-05-20 10:48:17 -0700407 newPatch->mHandle = *handle;
408 newPatch->mHalHandle = halHandle;
409 mPatches.add(newPatch);
410 ALOGV("createAudioPatch() added new patch handle %d halHandle %d", *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700411 } else {
412 clearPatchConnections(newPatch);
413 delete newPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700414 }
415 return status;
416}
417
Eric Laurent83b88082014-06-20 18:31:16 -0700418status_t AudioFlinger::PatchPanel::createPatchConnections(Patch *patch,
419 const struct audio_patch *audioPatch)
420{
421 // create patch from source device to record thread input
422 struct audio_patch subPatch;
423 subPatch.num_sources = 1;
424 subPatch.sources[0] = audioPatch->sources[0];
425 subPatch.num_sinks = 1;
426
427 patch->mRecordThread->getAudioPortConfig(&subPatch.sinks[0]);
428 subPatch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_MIC;
429
430 status_t status = createAudioPatch(&subPatch, &patch->mRecordPatchHandle);
431 if (status != NO_ERROR) {
432 patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE;
433 return status;
434 }
435
436 // create patch from playback thread output to sink device
Eric Laurentd60560a2015-04-10 11:31:20 -0700437 if (audioPatch->num_sinks != 0) {
438 patch->mPlaybackThread->getAudioPortConfig(&subPatch.sources[0]);
439 subPatch.sinks[0] = audioPatch->sinks[0];
440 status = createAudioPatch(&subPatch, &patch->mPlaybackPatchHandle);
441 if (status != NO_ERROR) {
442 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
443 return status;
444 }
445 } else {
Eric Laurent83b88082014-06-20 18:31:16 -0700446 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700447 }
448
449 // use a pseudo LCM between input and output framecount
450 size_t playbackFrameCount = patch->mPlaybackThread->frameCount();
451 int playbackShift = __builtin_ctz(playbackFrameCount);
452 size_t recordFramecount = patch->mRecordThread->frameCount();
453 int shift = __builtin_ctz(recordFramecount);
454 if (playbackShift < shift) {
455 shift = playbackShift;
456 }
457 size_t frameCount = (playbackFrameCount * recordFramecount) >> shift;
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700458 ALOGV("createPatchConnections() playframeCount %zu recordFramecount %zu frameCount %zu",
Eric Laurent83b88082014-06-20 18:31:16 -0700459 playbackFrameCount, recordFramecount, frameCount);
460
461 // create a special record track to capture from record thread
462 uint32_t channelCount = patch->mPlaybackThread->channelCount();
463 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
464 audio_channel_mask_t outChannelMask = patch->mPlaybackThread->channelMask();
465 uint32_t sampleRate = patch->mPlaybackThread->sampleRate();
466 audio_format_t format = patch->mPlaybackThread->format();
467
468 patch->mPatchRecord = new RecordThread::PatchRecord(
469 patch->mRecordThread.get(),
470 sampleRate,
471 inChannelMask,
472 format,
473 frameCount,
474 NULL,
Eric Laurent05067782016-06-01 18:27:28 -0700475 AUDIO_INPUT_FLAG_NONE);
Eric Laurent83b88082014-06-20 18:31:16 -0700476 if (patch->mPatchRecord == 0) {
477 return NO_MEMORY;
478 }
479 status = patch->mPatchRecord->initCheck();
480 if (status != NO_ERROR) {
481 return status;
482 }
483 patch->mRecordThread->addPatchRecord(patch->mPatchRecord);
484
485 // create a special playback track to render to playback thread.
486 // this track is given the same buffer as the PatchRecord buffer
487 patch->mPatchTrack = new PlaybackThread::PatchTrack(
488 patch->mPlaybackThread.get(),
Eric Laurent3bcf8592015-04-03 12:13:24 -0700489 audioPatch->sources[1].ext.mix.usecase.stream,
Eric Laurent83b88082014-06-20 18:31:16 -0700490 sampleRate,
491 outChannelMask,
492 format,
493 frameCount,
494 patch->mPatchRecord->buffer(),
Eric Laurent05067782016-06-01 18:27:28 -0700495 AUDIO_OUTPUT_FLAG_NONE);
Eric Laurent83b88082014-06-20 18:31:16 -0700496 if (patch->mPatchTrack == 0) {
497 return NO_MEMORY;
498 }
499 status = patch->mPatchTrack->initCheck();
500 if (status != NO_ERROR) {
501 return status;
502 }
503 patch->mPlaybackThread->addPatchTrack(patch->mPatchTrack);
504
505 // tie playback and record tracks together
506 patch->mPatchRecord->setPeerProxy(patch->mPatchTrack.get());
507 patch->mPatchTrack->setPeerProxy(patch->mPatchRecord.get());
508
509 // start capture and playback
Glenn Kastend848eb42016-03-08 13:42:11 -0800510 patch->mPatchRecord->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
Eric Laurent83b88082014-06-20 18:31:16 -0700511 patch->mPatchTrack->start();
512
513 return status;
514}
515
516void AudioFlinger::PatchPanel::clearPatchConnections(Patch *patch)
517{
518 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
519 if (audioflinger == 0) {
520 return;
521 }
522
523 ALOGV("clearPatchConnections() patch->mRecordPatchHandle %d patch->mPlaybackPatchHandle %d",
524 patch->mRecordPatchHandle, patch->mPlaybackPatchHandle);
525
526 if (patch->mPatchRecord != 0) {
527 patch->mPatchRecord->stop();
528 }
529 if (patch->mPatchTrack != 0) {
530 patch->mPatchTrack->stop();
531 }
532 if (patch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
533 releaseAudioPatch(patch->mRecordPatchHandle);
534 patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE;
535 }
536 if (patch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
537 releaseAudioPatch(patch->mPlaybackPatchHandle);
538 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
539 }
540 if (patch->mRecordThread != 0) {
541 if (patch->mPatchRecord != 0) {
542 patch->mRecordThread->deletePatchRecord(patch->mPatchRecord);
Eric Laurent83b88082014-06-20 18:31:16 -0700543 }
544 audioflinger->closeInputInternal_l(patch->mRecordThread);
Eric Laurent83b88082014-06-20 18:31:16 -0700545 }
546 if (patch->mPlaybackThread != 0) {
547 if (patch->mPatchTrack != 0) {
548 patch->mPlaybackThread->deletePatchTrack(patch->mPatchTrack);
Eric Laurent83b88082014-06-20 18:31:16 -0700549 }
550 // if num sources == 2 we are reusing an existing playback thread so we do not close it
551 if (patch->mAudioPatch.num_sources != 2) {
552 audioflinger->closeOutputInternal_l(patch->mPlaybackThread);
553 }
Eric Laurenta0169a02015-07-06 18:32:01 -0700554 }
555 if (patch->mRecordThread != 0) {
556 if (patch->mPatchRecord != 0) {
557 patch->mPatchRecord.clear();
558 }
559 patch->mRecordThread.clear();
560 }
561 if (patch->mPlaybackThread != 0) {
562 if (patch->mPatchTrack != 0) {
563 patch->mPatchTrack.clear();
564 }
Eric Laurent83b88082014-06-20 18:31:16 -0700565 patch->mPlaybackThread.clear();
566 }
Eric Laurenta0169a02015-07-06 18:32:01 -0700567
Eric Laurent83b88082014-06-20 18:31:16 -0700568}
569
Eric Laurent951f4552014-05-20 10:48:17 -0700570/* Disconnect a patch */
571status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
572{
573 ALOGV("releaseAudioPatch handle %d", handle);
574 status_t status = NO_ERROR;
575 size_t index;
576
577 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
578 if (audioflinger == 0) {
579 return NO_INIT;
580 }
581
582 for (index = 0; index < mPatches.size(); index++) {
583 if (handle == mPatches[index]->mHandle) {
584 break;
585 }
586 }
587 if (index == mPatches.size()) {
588 return BAD_VALUE;
589 }
Eric Laurent83b88082014-06-20 18:31:16 -0700590 Patch *removedPatch = mPatches[index];
591 mPatches.removeAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700592
Eric Laurent83b88082014-06-20 18:31:16 -0700593 struct audio_patch *patch = &removedPatch->mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700594
595 switch (patch->sources[0].type) {
596 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700597 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
598 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700599 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700600 ALOGW("releaseAudioPatch() bad src hw module %d", srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700601 status = BAD_VALUE;
602 break;
603 }
Eric Laurent83b88082014-06-20 18:31:16 -0700604
Eric Laurent3bcf8592015-04-03 12:13:24 -0700605 if (removedPatch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE ||
606 removedPatch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurent83b88082014-06-20 18:31:16 -0700607 clearPatchConnections(removedPatch);
608 break;
609 }
610
Eric Laurent054d9d32015-04-24 08:48:48 -0700611 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent951f4552014-05-20 10:48:17 -0700612 sp<ThreadBase> thread = audioflinger->checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700613 patch->sinks[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700614 if (thread == 0) {
615 ALOGW("releaseAudioPatch() bad capture I/O handle %d",
Eric Laurent054d9d32015-04-24 08:48:48 -0700616 patch->sinks[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700617 status = BAD_VALUE;
618 break;
619 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700620 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle);
621 } else {
622 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
623 if (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0) {
624 status = INVALID_OPERATION;
625 break;
626 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700627 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
628 status = hwDevice->releaseAudioPatch(removedPatch->mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700629 }
630 } break;
631 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700632 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
633 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700634 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700635 ALOGW("releaseAudioPatch() bad src hw module %d", srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700636 status = BAD_VALUE;
637 break;
638 }
639 sp<ThreadBase> thread =
640 audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
641 if (thread == 0) {
642 ALOGW("releaseAudioPatch() bad playback I/O handle %d",
643 patch->sources[0].ext.mix.handle);
644 status = BAD_VALUE;
645 break;
646 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700647 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700648 } break;
649 default:
650 status = BAD_VALUE;
651 break;
652 }
653
Eric Laurent83b88082014-06-20 18:31:16 -0700654 delete removedPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700655 return status;
656}
657
658
659/* List connected audio ports and they attributes */
660status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
661 struct audio_patch *patches __unused)
662{
663 ALOGV("listAudioPatches");
664 return NO_ERROR;
665}
666
667/* Set audio port configuration */
Eric Laurente1715a42014-05-20 11:30:42 -0700668status_t AudioFlinger::PatchPanel::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent951f4552014-05-20 10:48:17 -0700669{
670 ALOGV("setAudioPortConfig");
Eric Laurente1715a42014-05-20 11:30:42 -0700671
672 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
673 if (audioflinger == 0) {
674 return NO_INIT;
675 }
676
677 audio_module_handle_t module;
678 if (config->type == AUDIO_PORT_TYPE_DEVICE) {
679 module = config->ext.device.hw_module;
680 } else {
681 module = config->ext.mix.hw_module;
682 }
683
684 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(module);
685 if (index < 0) {
686 ALOGW("setAudioPortConfig() bad hw module %d", module);
687 return BAD_VALUE;
688 }
689
690 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
691 if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700692 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
693 return hwDevice->setAudioPortConfig(config);
Eric Laurente1715a42014-05-20 11:30:42 -0700694 } else {
695 return INVALID_OPERATION;
696 }
Eric Laurent951f4552014-05-20 10:48:17 -0700697 return NO_ERROR;
698}
699
Glenn Kasten63238ef2015-03-02 15:50:29 -0800700} // namespace android