blob: bf6763f679a29a0d544bd41752640f43be0c28b6 [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 Laurent83b88082014-06-20 18:31:16 -0700171 mPatches.removeAt(index);
soon1.choid8cd4772015-01-05 14:27:42 +0900172 delete removedPatch;
Eric Laurent83b88082014-06-20 18:31:16 -0700173 break;
174 }
Eric Laurent951f4552014-05-20 10:48:17 -0700175 }
176 }
177
Eric Laurent83b88082014-06-20 18:31:16 -0700178 Patch *newPatch = new Patch(patch);
179
Eric Laurent951f4552014-05-20 10:48:17 -0700180 switch (patch->sources[0].type) {
181 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700182 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
183 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700184 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700185 ALOGW("createAudioPatch() bad src hw module %d", srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700186 status = BAD_VALUE;
187 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700188 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700189 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700190 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700191 // support only one sink if connection to a mix or across HW modules
192 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
193 patch->sinks[i].ext.mix.hw_module != srcModule) &&
194 patch->num_sinks > 1) {
195 status = INVALID_OPERATION;
196 goto exit;
197 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700198 // reject connection to different sink types
199 if (patch->sinks[i].type != patch->sinks[0].type) {
200 ALOGW("createAudioPatch() different sink types in same patch not supported");
Eric Laurent83b88082014-06-20 18:31:16 -0700201 status = BAD_VALUE;
202 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700203 }
Eric Laurent951f4552014-05-20 10:48:17 -0700204 }
205
Eric Laurent3bcf8592015-04-03 12:13:24 -0700206 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700207 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700208 // - Device to device AND
209 // - source HW module != destination HW module OR
210 // - audio HAL version < 3.0
Eric Laurentd60560a2015-04-10 11:31:20 -0700211 if ((patch->num_sources == 2) ||
212 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
213 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
214 (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0)))) {
Eric Laurent83b88082014-06-20 18:31:16 -0700215 if (patch->num_sources == 2) {
216 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700217 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
218 patch->sources[1].ext.mix.hw_module)) {
Eric Laurent83b88082014-06-20 18:31:16 -0700219 ALOGW("createAudioPatch() invalid source combination");
220 status = INVALID_OPERATION;
221 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700222 }
Eric Laurent83b88082014-06-20 18:31:16 -0700223
224 sp<ThreadBase> thread =
225 audioflinger->checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
226 newPatch->mPlaybackThread = (MixerThread *)thread.get();
227 if (thread == 0) {
228 ALOGW("createAudioPatch() cannot get playback thread");
229 status = INVALID_OPERATION;
230 goto exit;
231 }
Eric Laurent951f4552014-05-20 10:48:17 -0700232 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700233 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
234 audio_devices_t device = patch->sinks[0].ext.device.type;
235 String8 address = String8(patch->sinks[0].ext.device.address);
236 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700237 newPatch->mPlaybackThread = audioflinger->openOutput_l(
238 patch->sinks[0].ext.device.hw_module,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700239 &output,
Eric Laurent83b88082014-06-20 18:31:16 -0700240 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700241 device,
242 address,
Eric Laurent83b88082014-06-20 18:31:16 -0700243 AUDIO_OUTPUT_FLAG_NONE);
244 ALOGV("audioflinger->openOutput_l() returned %p",
245 newPatch->mPlaybackThread.get());
246 if (newPatch->mPlaybackThread == 0) {
247 status = NO_MEMORY;
248 goto exit;
249 }
250 }
251 uint32_t channelCount = newPatch->mPlaybackThread->channelCount();
252 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700253 String8 address = String8(patch->sources[0].ext.device.address);
254 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent83b88082014-06-20 18:31:16 -0700255 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
256 config.sample_rate = newPatch->mPlaybackThread->sampleRate();
257 config.channel_mask = inChannelMask;
258 config.format = newPatch->mPlaybackThread->format();
Eric Laurentcf2c0212014-07-25 16:20:43 -0700259 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent874c42872014-08-08 15:13:39 -0700260 newPatch->mRecordThread = audioflinger->openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700261 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700262 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700263 device,
264 address,
265 AUDIO_SOURCE_MIC,
Eric Laurent83b88082014-06-20 18:31:16 -0700266 AUDIO_INPUT_FLAG_NONE);
267 ALOGV("audioflinger->openInput_l() returned %p inChannelMask %08x",
268 newPatch->mRecordThread.get(), inChannelMask);
269 if (newPatch->mRecordThread == 0) {
270 status = NO_MEMORY;
271 goto exit;
272 }
273 status = createPatchConnections(newPatch, patch);
274 if (status != NO_ERROR) {
275 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700276 }
277 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700278 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
279 sp<ThreadBase> thread = audioflinger->checkRecordThread_l(
280 patch->sinks[0].ext.mix.handle);
281 if (thread == 0) {
282 ALOGW("createAudioPatch() bad capture I/O handle %d",
283 patch->sinks[0].ext.mix.handle);
284 status = BAD_VALUE;
285 goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700286 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700287 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700288 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700289 if (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0) {
Eric Laurent3bcf8592015-04-03 12:13:24 -0700290 status = INVALID_OPERATION;
291 goto exit;
292 }
293
Eric Laurent054d9d32015-04-24 08:48:48 -0700294 audio_hw_device_t *hwDevice = audioHwDevice->hwDevice();
295 status = hwDevice->create_audio_patch(hwDevice,
296 patch->num_sources,
297 patch->sources,
298 patch->num_sinks,
299 patch->sinks,
300 &halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700301 }
Eric Laurent951f4552014-05-20 10:48:17 -0700302 }
303 } break;
304 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700305 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
306 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700307 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700308 ALOGW("createAudioPatch() bad src hw module %d", srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700309 status = BAD_VALUE;
310 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700311 }
312 // limit to connections between devices and output streams
Eric Laurent054d9d32015-04-24 08:48:48 -0700313 audio_devices_t type = AUDIO_DEVICE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700314 for (unsigned int i = 0; i < patch->num_sinks; i++) {
315 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent83b88082014-06-20 18:31:16 -0700316 ALOGW("createAudioPatch() invalid sink type %d for mix source",
Eric Laurent951f4552014-05-20 10:48:17 -0700317 patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700318 status = BAD_VALUE;
319 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700320 }
321 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700322 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700323 status = BAD_VALUE;
324 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700325 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700326 type |= patch->sinks[i].ext.device.type;
Eric Laurent951f4552014-05-20 10:48:17 -0700327 }
Eric Laurent951f4552014-05-20 10:48:17 -0700328 sp<ThreadBase> thread =
329 audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
330 if (thread == 0) {
331 ALOGW("createAudioPatch() bad playback I/O handle %d",
332 patch->sources[0].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700333 status = BAD_VALUE;
334 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700335 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700336 if (thread == audioflinger->primaryPlaybackThread_l()) {
337 AudioParameter param = AudioParameter();
Eric Laurentcf2c0212014-07-25 16:20:43 -0700338 param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type);
Eric Laurent054d9d32015-04-24 08:48:48 -0700339
340 audioflinger->broacastParametersToRecordThreads_l(param.toString());
Eric Laurent951f4552014-05-20 10:48:17 -0700341 }
342
Eric Laurent054d9d32015-04-24 08:48:48 -0700343 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700344 } break;
345 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700346 status = BAD_VALUE;
347 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700348 }
Eric Laurent83b88082014-06-20 18:31:16 -0700349exit:
Eric Laurent951f4552014-05-20 10:48:17 -0700350 ALOGV("createAudioPatch() status %d", status);
351 if (status == NO_ERROR) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800352 *handle = audioflinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
Eric Laurent951f4552014-05-20 10:48:17 -0700353 newPatch->mHandle = *handle;
354 newPatch->mHalHandle = halHandle;
355 mPatches.add(newPatch);
356 ALOGV("createAudioPatch() added new patch handle %d halHandle %d", *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700357 } else {
358 clearPatchConnections(newPatch);
359 delete newPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700360 }
361 return status;
362}
363
Eric Laurent83b88082014-06-20 18:31:16 -0700364status_t AudioFlinger::PatchPanel::createPatchConnections(Patch *patch,
365 const struct audio_patch *audioPatch)
366{
367 // create patch from source device to record thread input
368 struct audio_patch subPatch;
369 subPatch.num_sources = 1;
370 subPatch.sources[0] = audioPatch->sources[0];
371 subPatch.num_sinks = 1;
372
373 patch->mRecordThread->getAudioPortConfig(&subPatch.sinks[0]);
374 subPatch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_MIC;
375
376 status_t status = createAudioPatch(&subPatch, &patch->mRecordPatchHandle);
377 if (status != NO_ERROR) {
378 patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE;
379 return status;
380 }
381
382 // create patch from playback thread output to sink device
Eric Laurentd60560a2015-04-10 11:31:20 -0700383 if (audioPatch->num_sinks != 0) {
384 patch->mPlaybackThread->getAudioPortConfig(&subPatch.sources[0]);
385 subPatch.sinks[0] = audioPatch->sinks[0];
386 status = createAudioPatch(&subPatch, &patch->mPlaybackPatchHandle);
387 if (status != NO_ERROR) {
388 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
389 return status;
390 }
391 } else {
Eric Laurent83b88082014-06-20 18:31:16 -0700392 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700393 }
394
395 // use a pseudo LCM between input and output framecount
396 size_t playbackFrameCount = patch->mPlaybackThread->frameCount();
397 int playbackShift = __builtin_ctz(playbackFrameCount);
398 size_t recordFramecount = patch->mRecordThread->frameCount();
399 int shift = __builtin_ctz(recordFramecount);
400 if (playbackShift < shift) {
401 shift = playbackShift;
402 }
403 size_t frameCount = (playbackFrameCount * recordFramecount) >> shift;
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700404 ALOGV("createPatchConnections() playframeCount %zu recordFramecount %zu frameCount %zu",
Eric Laurent83b88082014-06-20 18:31:16 -0700405 playbackFrameCount, recordFramecount, frameCount);
406
407 // create a special record track to capture from record thread
408 uint32_t channelCount = patch->mPlaybackThread->channelCount();
409 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
410 audio_channel_mask_t outChannelMask = patch->mPlaybackThread->channelMask();
411 uint32_t sampleRate = patch->mPlaybackThread->sampleRate();
412 audio_format_t format = patch->mPlaybackThread->format();
413
414 patch->mPatchRecord = new RecordThread::PatchRecord(
415 patch->mRecordThread.get(),
416 sampleRate,
417 inChannelMask,
418 format,
419 frameCount,
420 NULL,
421 IAudioFlinger::TRACK_DEFAULT);
422 if (patch->mPatchRecord == 0) {
423 return NO_MEMORY;
424 }
425 status = patch->mPatchRecord->initCheck();
426 if (status != NO_ERROR) {
427 return status;
428 }
429 patch->mRecordThread->addPatchRecord(patch->mPatchRecord);
430
431 // create a special playback track to render to playback thread.
432 // this track is given the same buffer as the PatchRecord buffer
433 patch->mPatchTrack = new PlaybackThread::PatchTrack(
434 patch->mPlaybackThread.get(),
Eric Laurent3bcf8592015-04-03 12:13:24 -0700435 audioPatch->sources[1].ext.mix.usecase.stream,
Eric Laurent83b88082014-06-20 18:31:16 -0700436 sampleRate,
437 outChannelMask,
438 format,
439 frameCount,
440 patch->mPatchRecord->buffer(),
441 IAudioFlinger::TRACK_DEFAULT);
442 if (patch->mPatchTrack == 0) {
443 return NO_MEMORY;
444 }
445 status = patch->mPatchTrack->initCheck();
446 if (status != NO_ERROR) {
447 return status;
448 }
449 patch->mPlaybackThread->addPatchTrack(patch->mPatchTrack);
450
451 // tie playback and record tracks together
452 patch->mPatchRecord->setPeerProxy(patch->mPatchTrack.get());
453 patch->mPatchTrack->setPeerProxy(patch->mPatchRecord.get());
454
455 // start capture and playback
Glenn Kastend848eb42016-03-08 13:42:11 -0800456 patch->mPatchRecord->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
Eric Laurent83b88082014-06-20 18:31:16 -0700457 patch->mPatchTrack->start();
458
459 return status;
460}
461
462void AudioFlinger::PatchPanel::clearPatchConnections(Patch *patch)
463{
464 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
465 if (audioflinger == 0) {
466 return;
467 }
468
469 ALOGV("clearPatchConnections() patch->mRecordPatchHandle %d patch->mPlaybackPatchHandle %d",
470 patch->mRecordPatchHandle, patch->mPlaybackPatchHandle);
471
472 if (patch->mPatchRecord != 0) {
473 patch->mPatchRecord->stop();
474 }
475 if (patch->mPatchTrack != 0) {
476 patch->mPatchTrack->stop();
477 }
478 if (patch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
479 releaseAudioPatch(patch->mRecordPatchHandle);
480 patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE;
481 }
482 if (patch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
483 releaseAudioPatch(patch->mPlaybackPatchHandle);
484 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
485 }
486 if (patch->mRecordThread != 0) {
487 if (patch->mPatchRecord != 0) {
488 patch->mRecordThread->deletePatchRecord(patch->mPatchRecord);
Eric Laurent83b88082014-06-20 18:31:16 -0700489 }
490 audioflinger->closeInputInternal_l(patch->mRecordThread);
Eric Laurent83b88082014-06-20 18:31:16 -0700491 }
492 if (patch->mPlaybackThread != 0) {
493 if (patch->mPatchTrack != 0) {
494 patch->mPlaybackThread->deletePatchTrack(patch->mPatchTrack);
Eric Laurent83b88082014-06-20 18:31:16 -0700495 }
496 // if num sources == 2 we are reusing an existing playback thread so we do not close it
497 if (patch->mAudioPatch.num_sources != 2) {
498 audioflinger->closeOutputInternal_l(patch->mPlaybackThread);
499 }
Eric Laurenta0169a02015-07-06 18:32:01 -0700500 }
501 if (patch->mRecordThread != 0) {
502 if (patch->mPatchRecord != 0) {
503 patch->mPatchRecord.clear();
504 }
505 patch->mRecordThread.clear();
506 }
507 if (patch->mPlaybackThread != 0) {
508 if (patch->mPatchTrack != 0) {
509 patch->mPatchTrack.clear();
510 }
Eric Laurent83b88082014-06-20 18:31:16 -0700511 patch->mPlaybackThread.clear();
512 }
Eric Laurenta0169a02015-07-06 18:32:01 -0700513
Eric Laurent83b88082014-06-20 18:31:16 -0700514}
515
Eric Laurent951f4552014-05-20 10:48:17 -0700516/* Disconnect a patch */
517status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
518{
519 ALOGV("releaseAudioPatch handle %d", handle);
520 status_t status = NO_ERROR;
521 size_t index;
522
523 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
524 if (audioflinger == 0) {
525 return NO_INIT;
526 }
527
528 for (index = 0; index < mPatches.size(); index++) {
529 if (handle == mPatches[index]->mHandle) {
530 break;
531 }
532 }
533 if (index == mPatches.size()) {
534 return BAD_VALUE;
535 }
Eric Laurent83b88082014-06-20 18:31:16 -0700536 Patch *removedPatch = mPatches[index];
537 mPatches.removeAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700538
Eric Laurent83b88082014-06-20 18:31:16 -0700539 struct audio_patch *patch = &removedPatch->mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700540
541 switch (patch->sources[0].type) {
542 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700543 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
544 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700545 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700546 ALOGW("releaseAudioPatch() bad src hw module %d", srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700547 status = BAD_VALUE;
548 break;
549 }
Eric Laurent83b88082014-06-20 18:31:16 -0700550
Eric Laurent3bcf8592015-04-03 12:13:24 -0700551 if (removedPatch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE ||
552 removedPatch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
Eric Laurent83b88082014-06-20 18:31:16 -0700553 clearPatchConnections(removedPatch);
554 break;
555 }
556
Eric Laurent054d9d32015-04-24 08:48:48 -0700557 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent951f4552014-05-20 10:48:17 -0700558 sp<ThreadBase> thread = audioflinger->checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700559 patch->sinks[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700560 if (thread == 0) {
561 ALOGW("releaseAudioPatch() bad capture I/O handle %d",
Eric Laurent054d9d32015-04-24 08:48:48 -0700562 patch->sinks[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700563 status = BAD_VALUE;
564 break;
565 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700566 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle);
567 } else {
568 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
569 if (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0) {
570 status = INVALID_OPERATION;
571 break;
572 }
573 audio_hw_device_t *hwDevice = audioHwDevice->hwDevice();
574 status = hwDevice->release_audio_patch(hwDevice, removedPatch->mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700575 }
576 } break;
577 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700578 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
579 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700580 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700581 ALOGW("releaseAudioPatch() bad src hw module %d", srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700582 status = BAD_VALUE;
583 break;
584 }
585 sp<ThreadBase> thread =
586 audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
587 if (thread == 0) {
588 ALOGW("releaseAudioPatch() bad playback I/O handle %d",
589 patch->sources[0].ext.mix.handle);
590 status = BAD_VALUE;
591 break;
592 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700593 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700594 } break;
595 default:
596 status = BAD_VALUE;
597 break;
598 }
599
Eric Laurent83b88082014-06-20 18:31:16 -0700600 delete removedPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700601 return status;
602}
603
604
605/* List connected audio ports and they attributes */
606status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
607 struct audio_patch *patches __unused)
608{
609 ALOGV("listAudioPatches");
610 return NO_ERROR;
611}
612
613/* Set audio port configuration */
Eric Laurente1715a42014-05-20 11:30:42 -0700614status_t AudioFlinger::PatchPanel::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent951f4552014-05-20 10:48:17 -0700615{
616 ALOGV("setAudioPortConfig");
Eric Laurente1715a42014-05-20 11:30:42 -0700617
618 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
619 if (audioflinger == 0) {
620 return NO_INIT;
621 }
622
623 audio_module_handle_t module;
624 if (config->type == AUDIO_PORT_TYPE_DEVICE) {
625 module = config->ext.device.hw_module;
626 } else {
627 module = config->ext.mix.hw_module;
628 }
629
630 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(module);
631 if (index < 0) {
632 ALOGW("setAudioPortConfig() bad hw module %d", module);
633 return BAD_VALUE;
634 }
635
636 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
637 if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
638 audio_hw_device_t *hwDevice = audioHwDevice->hwDevice();
639 return hwDevice->set_audio_port_config(hwDevice, config);
640 } else {
641 return INVALID_OPERATION;
642 }
Eric Laurent951f4552014-05-20 10:48:17 -0700643 return NO_ERROR;
644}
645
Glenn Kasten63238ef2015-03-02 15:50:29 -0800646} // namespace android