blob: c94ea45de4578dd42c3616d54ee124f25c603c53 [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);
52 if (mPatchPanel != 0) {
53 return mPatchPanel->listAudioPorts(num_ports, ports);
54 }
55 return NO_INIT;
56}
57
58/* Get supported attributes for a given audio port */
59status_t AudioFlinger::getAudioPort(struct audio_port *port)
60{
61 Mutex::Autolock _l(mLock);
62 if (mPatchPanel != 0) {
63 return mPatchPanel->getAudioPort(port);
64 }
65 return NO_INIT;
66}
67
68
69/* Connect a patch between several source and sink ports */
70status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
71 audio_patch_handle_t *handle)
72{
73 Mutex::Autolock _l(mLock);
74 if (mPatchPanel != 0) {
75 return mPatchPanel->createAudioPatch(patch, handle);
76 }
77 return NO_INIT;
78}
79
80/* Disconnect a patch */
81status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
82{
83 Mutex::Autolock _l(mLock);
84 if (mPatchPanel != 0) {
85 return mPatchPanel->releaseAudioPatch(handle);
86 }
87 return NO_INIT;
88}
89
90
91/* List connected audio ports and they attributes */
92status_t AudioFlinger::listAudioPatches(unsigned int *num_patches,
93 struct audio_patch *patches)
94{
95 Mutex::Autolock _l(mLock);
96 if (mPatchPanel != 0) {
97 return mPatchPanel->listAudioPatches(num_patches, patches);
98 }
99 return NO_INIT;
100}
101
102/* Set audio port configuration */
103status_t AudioFlinger::setAudioPortConfig(const struct audio_port_config *config)
104{
105 Mutex::Autolock _l(mLock);
106 if (mPatchPanel != 0) {
107 return mPatchPanel->setAudioPortConfig(config);
108 }
109 return NO_INIT;
110}
111
112
113AudioFlinger::PatchPanel::PatchPanel(const sp<AudioFlinger>& audioFlinger)
114 : mAudioFlinger(audioFlinger)
115{
116}
117
118AudioFlinger::PatchPanel::~PatchPanel()
119{
120}
121
122/* List connected audio ports and their attributes */
123status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
124 struct audio_port *ports __unused)
125{
126 ALOGV("listAudioPorts");
127 return NO_ERROR;
128}
129
130/* Get supported attributes for a given audio port */
131status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port *port __unused)
132{
133 ALOGV("getAudioPort");
134 return NO_ERROR;
135}
136
137
138/* Connect a patch between several source and sink ports */
139status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
140 audio_patch_handle_t *handle)
141{
Eric Laurent951f4552014-05-20 10:48:17 -0700142 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700143 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700144 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
Greg Kaiserf27ce402016-03-14 13:43:14 -0700145 if (handle == NULL || patch == NULL) {
146 return BAD_VALUE;
147 }
148 ALOGV("createAudioPatch() num_sources %d num_sinks %d handle %d",
149 patch->num_sources, patch->num_sinks, *handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700150 if (audioflinger == 0) {
151 return NO_INIT;
152 }
Eric Laurent83b88082014-06-20 18:31:16 -0700153
Eric Laurent874c42872014-08-08 15:13:39 -0700154 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700155 (patch->num_sinks == 0 && patch->num_sources != 2) ||
156 patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
Eric Laurent951f4552014-05-20 10:48:17 -0700157 return BAD_VALUE;
158 }
Eric Laurent874c42872014-08-08 15:13:39 -0700159 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
160 // only the audio policy manager can request a patch creation with 2 sources.
161 if (patch->num_sources > 2) {
162 return INVALID_OPERATION;
163 }
Eric Laurent951f4552014-05-20 10:48:17 -0700164
Eric Laurent83b88082014-06-20 18:31:16 -0700165 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
166 for (size_t index = 0; *handle != 0 && index < mPatches.size(); index++) {
167 if (*handle == mPatches[index]->mHandle) {
168 ALOGV("createAudioPatch() removing patch handle %d", *handle);
169 halHandle = mPatches[index]->mHalHandle;
soon1.choid8cd4772015-01-05 14:27:42 +0900170 Patch *removedPatch = mPatches[index];
Eric Laurentb997d3a2016-06-07 18:23:45 -0700171 // free resources owned by the removed patch if applicable
172 // 1) if a software patch is present, release the playback and capture threads and
173 // tracks created. This will also release the corresponding audio HAL patches
Eric Laurent0666cc52015-12-15 10:14:06 -0800174 if ((removedPatch->mRecordPatchHandle
175 != AUDIO_PATCH_HANDLE_NONE) ||
176 (removedPatch->mPlaybackPatchHandle !=
177 AUDIO_PATCH_HANDLE_NONE)) {
178 clearPatchConnections(removedPatch);
179 }
Eric Laurentb997d3a2016-06-07 18:23:45 -0700180 // 2) if the new patch and old patch source or sink are devices from different
181 // hw modules, clear the audio HAL patches now because they will not be updated
182 // by call to create_audio_patch() below which will happen on a different HW module
183 if (halHandle != AUDIO_PATCH_HANDLE_NONE) {
184 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
185 if ((removedPatch->mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE) &&
186 ((patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE) ||
187 (removedPatch->mAudioPatch.sources[0].ext.device.hw_module !=
188 patch->sources[0].ext.device.hw_module))) {
189 hwModule = removedPatch->mAudioPatch.sources[0].ext.device.hw_module;
190 } else if ((patch->num_sinks == 0) ||
191 ((removedPatch->mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
192 ((patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE) ||
193 (removedPatch->mAudioPatch.sinks[0].ext.device.hw_module !=
194 patch->sinks[0].ext.device.hw_module)))) {
195 // Note on (patch->num_sinks == 0): this situation should not happen as
196 // these special patches are only created by the policy manager but just
197 // in case, systematically clear the HAL patch.
198 // Note that removedPatch->mAudioPatch.num_sinks cannot be 0 here because
199 // halHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
200 hwModule = removedPatch->mAudioPatch.sinks[0].ext.device.hw_module;
201 }
202 if (hwModule != AUDIO_MODULE_HANDLE_NONE) {
203 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(hwModule);
204 if (index >= 0) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700205 sp<DeviceHalInterface> hwDevice =
Eric Laurentb997d3a2016-06-07 18:23:45 -0700206 audioflinger->mAudioHwDevs.valueAt(index)->hwDevice();
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700207 hwDevice->releaseAudioPatch(halHandle);
Eric Laurentb997d3a2016-06-07 18:23:45 -0700208 }
209 }
210 }
Eric Laurent83b88082014-06-20 18:31:16 -0700211 mPatches.removeAt(index);
soon1.choid8cd4772015-01-05 14:27:42 +0900212 delete removedPatch;
Eric Laurent83b88082014-06-20 18:31:16 -0700213 break;
214 }
Eric Laurent951f4552014-05-20 10:48:17 -0700215 }
216 }
217
Eric Laurent83b88082014-06-20 18:31:16 -0700218 Patch *newPatch = new Patch(patch);
219
Eric Laurent951f4552014-05-20 10:48:17 -0700220 switch (patch->sources[0].type) {
221 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700222 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
223 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700224 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700225 ALOGW("createAudioPatch() bad src hw module %d", srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700226 status = BAD_VALUE;
227 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700228 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700229 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700230 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700231 // support only one sink if connection to a mix or across HW modules
232 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
233 patch->sinks[i].ext.mix.hw_module != srcModule) &&
234 patch->num_sinks > 1) {
235 status = INVALID_OPERATION;
236 goto exit;
237 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700238 // reject connection to different sink types
239 if (patch->sinks[i].type != patch->sinks[0].type) {
240 ALOGW("createAudioPatch() different sink types in same patch not supported");
Eric Laurent83b88082014-06-20 18:31:16 -0700241 status = BAD_VALUE;
242 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700243 }
Eric Laurent951f4552014-05-20 10:48:17 -0700244 }
245
Eric Laurent3bcf8592015-04-03 12:13:24 -0700246 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700247 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700248 // - Device to device AND
249 // - source HW module != destination HW module OR
250 // - audio HAL version < 3.0
Eric Laurentd60560a2015-04-10 11:31:20 -0700251 if ((patch->num_sources == 2) ||
252 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
253 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
254 (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0)))) {
Eric Laurent83b88082014-06-20 18:31:16 -0700255 if (patch->num_sources == 2) {
256 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700257 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
258 patch->sources[1].ext.mix.hw_module)) {
Eric Laurent83b88082014-06-20 18:31:16 -0700259 ALOGW("createAudioPatch() invalid source combination");
260 status = INVALID_OPERATION;
261 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700262 }
Eric Laurent83b88082014-06-20 18:31:16 -0700263
264 sp<ThreadBase> thread =
265 audioflinger->checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
266 newPatch->mPlaybackThread = (MixerThread *)thread.get();
267 if (thread == 0) {
268 ALOGW("createAudioPatch() cannot get playback thread");
269 status = INVALID_OPERATION;
270 goto exit;
271 }
Eric Laurent951f4552014-05-20 10:48:17 -0700272 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700273 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
274 audio_devices_t device = patch->sinks[0].ext.device.type;
275 String8 address = String8(patch->sinks[0].ext.device.address);
276 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700277 newPatch->mPlaybackThread = audioflinger->openOutput_l(
278 patch->sinks[0].ext.device.hw_module,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700279 &output,
Eric Laurent83b88082014-06-20 18:31:16 -0700280 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700281 device,
282 address,
Eric Laurent83b88082014-06-20 18:31:16 -0700283 AUDIO_OUTPUT_FLAG_NONE);
284 ALOGV("audioflinger->openOutput_l() returned %p",
285 newPatch->mPlaybackThread.get());
286 if (newPatch->mPlaybackThread == 0) {
287 status = NO_MEMORY;
288 goto exit;
289 }
290 }
Eric Laurent83b88082014-06-20 18:31:16 -0700291 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700292 String8 address = String8(patch->sources[0].ext.device.address);
293 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700294 // open input stream with source device audio properties if provided or
295 // default to peer output stream properties otherwise.
296 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
297 config.sample_rate = patch->sources[0].sample_rate;
298 } else {
299 config.sample_rate = newPatch->mPlaybackThread->sampleRate();
300 }
301 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
302 config.channel_mask = patch->sources[0].channel_mask;
303 } else {
304 config.channel_mask =
305 audio_channel_in_mask_from_count(newPatch->mPlaybackThread->channelCount());
306 }
307 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
308 config.format = patch->sources[0].format;
309 } else {
310 config.format = newPatch->mPlaybackThread->format();
311 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700312 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent874c42872014-08-08 15:13:39 -0700313 newPatch->mRecordThread = audioflinger->openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700314 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700315 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700316 device,
317 address,
318 AUDIO_SOURCE_MIC,
Eric Laurent83b88082014-06-20 18:31:16 -0700319 AUDIO_INPUT_FLAG_NONE);
320 ALOGV("audioflinger->openInput_l() returned %p inChannelMask %08x",
Eric Laurent8ae73122016-04-12 10:13:29 -0700321 newPatch->mRecordThread.get(), config.channel_mask);
Eric Laurent83b88082014-06-20 18:31:16 -0700322 if (newPatch->mRecordThread == 0) {
323 status = NO_MEMORY;
324 goto exit;
325 }
326 status = createPatchConnections(newPatch, patch);
327 if (status != NO_ERROR) {
328 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700329 }
330 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700331 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
332 sp<ThreadBase> thread = audioflinger->checkRecordThread_l(
333 patch->sinks[0].ext.mix.handle);
334 if (thread == 0) {
335 ALOGW("createAudioPatch() bad capture I/O handle %d",
336 patch->sinks[0].ext.mix.handle);
337 status = BAD_VALUE;
338 goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700339 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700340 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700341 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700342 if (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0) {
Eric Laurent3bcf8592015-04-03 12:13:24 -0700343 status = INVALID_OPERATION;
344 goto exit;
345 }
346
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700347 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
348 status = hwDevice->createAudioPatch(patch->num_sources,
349 patch->sources,
350 patch->num_sinks,
351 patch->sinks,
352 &halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700353 }
Eric Laurent951f4552014-05-20 10:48:17 -0700354 }
355 } break;
356 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700357 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
358 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700359 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700360 ALOGW("createAudioPatch() bad src hw module %d", srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700361 status = BAD_VALUE;
362 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700363 }
364 // limit to connections between devices and output streams
Eric Laurent054d9d32015-04-24 08:48:48 -0700365 audio_devices_t type = AUDIO_DEVICE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700366 for (unsigned int i = 0; i < patch->num_sinks; i++) {
367 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent83b88082014-06-20 18:31:16 -0700368 ALOGW("createAudioPatch() invalid sink type %d for mix source",
Eric Laurent951f4552014-05-20 10:48:17 -0700369 patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700370 status = BAD_VALUE;
371 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700372 }
373 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700374 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700375 status = BAD_VALUE;
376 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700377 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700378 type |= patch->sinks[i].ext.device.type;
Eric Laurent951f4552014-05-20 10:48:17 -0700379 }
Eric Laurent951f4552014-05-20 10:48:17 -0700380 sp<ThreadBase> thread =
381 audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
382 if (thread == 0) {
383 ALOGW("createAudioPatch() bad playback I/O handle %d",
384 patch->sources[0].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700385 status = BAD_VALUE;
386 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700387 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700388 if (thread == audioflinger->primaryPlaybackThread_l()) {
389 AudioParameter param = AudioParameter();
Eric Laurentcf2c0212014-07-25 16:20:43 -0700390 param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type);
Eric Laurent054d9d32015-04-24 08:48:48 -0700391
392 audioflinger->broacastParametersToRecordThreads_l(param.toString());
Eric Laurent951f4552014-05-20 10:48:17 -0700393 }
394
Eric Laurent054d9d32015-04-24 08:48:48 -0700395 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700396 } break;
397 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700398 status = BAD_VALUE;
399 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700400 }
Eric Laurent83b88082014-06-20 18:31:16 -0700401exit:
Eric Laurent951f4552014-05-20 10:48:17 -0700402 ALOGV("createAudioPatch() status %d", status);
403 if (status == NO_ERROR) {
Glenn Kastena13cde92016-03-28 15:26:02 -0700404 *handle = (audio_patch_handle_t) audioflinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
Eric Laurent951f4552014-05-20 10:48:17 -0700405 newPatch->mHandle = *handle;
406 newPatch->mHalHandle = halHandle;
407 mPatches.add(newPatch);
408 ALOGV("createAudioPatch() added new patch handle %d halHandle %d", *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700409 } else {
410 clearPatchConnections(newPatch);
411 delete newPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700412 }
413 return status;
414}
415
Eric Laurent83b88082014-06-20 18:31:16 -0700416status_t AudioFlinger::PatchPanel::createPatchConnections(Patch *patch,
417 const struct audio_patch *audioPatch)
418{
419 // create patch from source device to record thread input
420 struct audio_patch subPatch;
421 subPatch.num_sources = 1;
422 subPatch.sources[0] = audioPatch->sources[0];
423 subPatch.num_sinks = 1;
424
425 patch->mRecordThread->getAudioPortConfig(&subPatch.sinks[0]);
426 subPatch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_MIC;
427
428 status_t status = createAudioPatch(&subPatch, &patch->mRecordPatchHandle);
429 if (status != NO_ERROR) {
430 patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE;
431 return status;
432 }
433
434 // create patch from playback thread output to sink device
Eric Laurentd60560a2015-04-10 11:31:20 -0700435 if (audioPatch->num_sinks != 0) {
436 patch->mPlaybackThread->getAudioPortConfig(&subPatch.sources[0]);
437 subPatch.sinks[0] = audioPatch->sinks[0];
438 status = createAudioPatch(&subPatch, &patch->mPlaybackPatchHandle);
439 if (status != NO_ERROR) {
440 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
441 return status;
442 }
443 } else {
Eric Laurent83b88082014-06-20 18:31:16 -0700444 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700445 }
446
447 // use a pseudo LCM between input and output framecount
448 size_t playbackFrameCount = patch->mPlaybackThread->frameCount();
449 int playbackShift = __builtin_ctz(playbackFrameCount);
450 size_t recordFramecount = patch->mRecordThread->frameCount();
451 int shift = __builtin_ctz(recordFramecount);
452 if (playbackShift < shift) {
453 shift = playbackShift;
454 }
455 size_t frameCount = (playbackFrameCount * recordFramecount) >> shift;
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700456 ALOGV("createPatchConnections() playframeCount %zu recordFramecount %zu frameCount %zu",
Eric Laurent83b88082014-06-20 18:31:16 -0700457 playbackFrameCount, recordFramecount, frameCount);
458
459 // create a special record track to capture from record thread
460 uint32_t channelCount = patch->mPlaybackThread->channelCount();
461 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
462 audio_channel_mask_t outChannelMask = patch->mPlaybackThread->channelMask();
463 uint32_t sampleRate = patch->mPlaybackThread->sampleRate();
464 audio_format_t format = patch->mPlaybackThread->format();
465
466 patch->mPatchRecord = new RecordThread::PatchRecord(
467 patch->mRecordThread.get(),
468 sampleRate,
469 inChannelMask,
470 format,
471 frameCount,
472 NULL,
Eric Laurent05067782016-06-01 18:27:28 -0700473 AUDIO_INPUT_FLAG_NONE);
Eric Laurent83b88082014-06-20 18:31:16 -0700474 if (patch->mPatchRecord == 0) {
475 return NO_MEMORY;
476 }
477 status = patch->mPatchRecord->initCheck();
478 if (status != NO_ERROR) {
479 return status;
480 }
481 patch->mRecordThread->addPatchRecord(patch->mPatchRecord);
482
483 // create a special playback track to render to playback thread.
484 // this track is given the same buffer as the PatchRecord buffer
485 patch->mPatchTrack = new PlaybackThread::PatchTrack(
486 patch->mPlaybackThread.get(),
Eric Laurent3bcf8592015-04-03 12:13:24 -0700487 audioPatch->sources[1].ext.mix.usecase.stream,
Eric Laurent83b88082014-06-20 18:31:16 -0700488 sampleRate,
489 outChannelMask,
490 format,
491 frameCount,
492 patch->mPatchRecord->buffer(),
Eric Laurent05067782016-06-01 18:27:28 -0700493 AUDIO_OUTPUT_FLAG_NONE);
Eric Laurent83b88082014-06-20 18:31:16 -0700494 if (patch->mPatchTrack == 0) {
495 return NO_MEMORY;
496 }
497 status = patch->mPatchTrack->initCheck();
498 if (status != NO_ERROR) {
499 return status;
500 }
501 patch->mPlaybackThread->addPatchTrack(patch->mPatchTrack);
502
503 // tie playback and record tracks together
504 patch->mPatchRecord->setPeerProxy(patch->mPatchTrack.get());
505 patch->mPatchTrack->setPeerProxy(patch->mPatchRecord.get());
506
507 // start capture and playback
Glenn Kastend848eb42016-03-08 13:42:11 -0800508 patch->mPatchRecord->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
Eric Laurent83b88082014-06-20 18:31:16 -0700509 patch->mPatchTrack->start();
510
511 return status;
512}
513
514void AudioFlinger::PatchPanel::clearPatchConnections(Patch *patch)
515{
516 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
517 if (audioflinger == 0) {
518 return;
519 }
520
521 ALOGV("clearPatchConnections() patch->mRecordPatchHandle %d patch->mPlaybackPatchHandle %d",
522 patch->mRecordPatchHandle, patch->mPlaybackPatchHandle);
523
524 if (patch->mPatchRecord != 0) {
525 patch->mPatchRecord->stop();
526 }
527 if (patch->mPatchTrack != 0) {
528 patch->mPatchTrack->stop();
529 }
530 if (patch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
531 releaseAudioPatch(patch->mRecordPatchHandle);
532 patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE;
533 }
534 if (patch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
535 releaseAudioPatch(patch->mPlaybackPatchHandle);
536 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
537 }
538 if (patch->mRecordThread != 0) {
539 if (patch->mPatchRecord != 0) {
540 patch->mRecordThread->deletePatchRecord(patch->mPatchRecord);
Eric Laurent83b88082014-06-20 18:31:16 -0700541 }
542 audioflinger->closeInputInternal_l(patch->mRecordThread);
Eric Laurent83b88082014-06-20 18:31:16 -0700543 }
544 if (patch->mPlaybackThread != 0) {
545 if (patch->mPatchTrack != 0) {
546 patch->mPlaybackThread->deletePatchTrack(patch->mPatchTrack);
Eric Laurent83b88082014-06-20 18:31:16 -0700547 }
548 // if num sources == 2 we are reusing an existing playback thread so we do not close it
549 if (patch->mAudioPatch.num_sources != 2) {
550 audioflinger->closeOutputInternal_l(patch->mPlaybackThread);
551 }
Eric Laurenta0169a02015-07-06 18:32:01 -0700552 }
553 if (patch->mRecordThread != 0) {
554 if (patch->mPatchRecord != 0) {
555 patch->mPatchRecord.clear();
556 }
557 patch->mRecordThread.clear();
558 }
559 if (patch->mPlaybackThread != 0) {
560 if (patch->mPatchTrack != 0) {
561 patch->mPatchTrack.clear();
562 }
Eric Laurent83b88082014-06-20 18:31:16 -0700563 patch->mPlaybackThread.clear();
564 }
Eric Laurenta0169a02015-07-06 18:32:01 -0700565
Eric Laurent83b88082014-06-20 18:31:16 -0700566}
567
Eric Laurent951f4552014-05-20 10:48:17 -0700568/* Disconnect a patch */
569status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
570{
571 ALOGV("releaseAudioPatch handle %d", handle);
572 status_t status = NO_ERROR;
573 size_t index;
574
575 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
576 if (audioflinger == 0) {
577 return NO_INIT;
578 }
579
580 for (index = 0; index < mPatches.size(); index++) {
581 if (handle == mPatches[index]->mHandle) {
582 break;
583 }
584 }
585 if (index == mPatches.size()) {
586 return BAD_VALUE;
587 }
Eric Laurent83b88082014-06-20 18:31:16 -0700588 Patch *removedPatch = mPatches[index];
589 mPatches.removeAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700590
Eric Laurent83b88082014-06-20 18:31:16 -0700591 struct audio_patch *patch = &removedPatch->mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700592
593 switch (patch->sources[0].type) {
594 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700595 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
596 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700597 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700598 ALOGW("releaseAudioPatch() bad src hw module %d", srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700599 status = BAD_VALUE;
600 break;
601 }
Eric Laurent83b88082014-06-20 18:31:16 -0700602
Eric Laurent3bcf8592015-04-03 12:13:24 -0700603 if (removedPatch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE ||
604 removedPatch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurent83b88082014-06-20 18:31:16 -0700605 clearPatchConnections(removedPatch);
606 break;
607 }
608
Eric Laurent054d9d32015-04-24 08:48:48 -0700609 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent951f4552014-05-20 10:48:17 -0700610 sp<ThreadBase> thread = audioflinger->checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700611 patch->sinks[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700612 if (thread == 0) {
613 ALOGW("releaseAudioPatch() bad capture I/O handle %d",
Eric Laurent054d9d32015-04-24 08:48:48 -0700614 patch->sinks[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700615 status = BAD_VALUE;
616 break;
617 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700618 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle);
619 } else {
620 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
621 if (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0) {
622 status = INVALID_OPERATION;
623 break;
624 }
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700625 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
626 status = hwDevice->releaseAudioPatch(removedPatch->mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700627 }
628 } break;
629 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700630 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
631 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700632 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700633 ALOGW("releaseAudioPatch() bad src hw module %d", srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700634 status = BAD_VALUE;
635 break;
636 }
637 sp<ThreadBase> thread =
638 audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
639 if (thread == 0) {
640 ALOGW("releaseAudioPatch() bad playback I/O handle %d",
641 patch->sources[0].ext.mix.handle);
642 status = BAD_VALUE;
643 break;
644 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700645 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700646 } break;
647 default:
648 status = BAD_VALUE;
649 break;
650 }
651
Eric Laurent83b88082014-06-20 18:31:16 -0700652 delete removedPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700653 return status;
654}
655
656
657/* List connected audio ports and they attributes */
658status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
659 struct audio_patch *patches __unused)
660{
661 ALOGV("listAudioPatches");
662 return NO_ERROR;
663}
664
665/* Set audio port configuration */
Eric Laurente1715a42014-05-20 11:30:42 -0700666status_t AudioFlinger::PatchPanel::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent951f4552014-05-20 10:48:17 -0700667{
668 ALOGV("setAudioPortConfig");
Eric Laurente1715a42014-05-20 11:30:42 -0700669
670 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
671 if (audioflinger == 0) {
672 return NO_INIT;
673 }
674
675 audio_module_handle_t module;
676 if (config->type == AUDIO_PORT_TYPE_DEVICE) {
677 module = config->ext.device.hw_module;
678 } else {
679 module = config->ext.mix.hw_module;
680 }
681
682 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(module);
683 if (index < 0) {
684 ALOGW("setAudioPortConfig() bad hw module %d", module);
685 return BAD_VALUE;
686 }
687
688 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
689 if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700690 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
691 return hwDevice->setAudioPortConfig(config);
Eric Laurente1715a42014-05-20 11:30:42 -0700692 } else {
693 return INVALID_OPERATION;
694 }
Eric Laurent951f4552014-05-20 10:48:17 -0700695 return NO_ERROR;
696}
697
Glenn Kasten63238ef2015-03-02 15:50:29 -0800698} // namespace android