blob: 4f0c6b1fe6abd889050a7bdb1d71f04fd4881a4a [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{
142 ALOGV("createAudioPatch() num_sources %d num_sinks %d handle %d",
143 patch->num_sources, patch->num_sinks, *handle);
144 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();
147 if (audioflinger == 0) {
148 return NO_INIT;
149 }
Eric Laurent83b88082014-06-20 18:31:16 -0700150
Eric Laurent951f4552014-05-20 10:48:17 -0700151 if (handle == NULL || patch == NULL) {
152 return BAD_VALUE;
153 }
Eric Laurent874c42872014-08-08 15:13:39 -0700154 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
Eric Laurent951f4552014-05-20 10:48:17 -0700155 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
156 return BAD_VALUE;
157 }
Eric Laurent874c42872014-08-08 15:13:39 -0700158 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
159 // only the audio policy manager can request a patch creation with 2 sources.
160 if (patch->num_sources > 2) {
161 return INVALID_OPERATION;
162 }
Eric Laurent951f4552014-05-20 10:48:17 -0700163
Eric Laurent83b88082014-06-20 18:31:16 -0700164 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
165 for (size_t index = 0; *handle != 0 && index < mPatches.size(); index++) {
166 if (*handle == mPatches[index]->mHandle) {
167 ALOGV("createAudioPatch() removing patch handle %d", *handle);
168 halHandle = mPatches[index]->mHalHandle;
soon1.choid8cd4772015-01-05 14:27:42 +0900169 Patch *removedPatch = mPatches[index];
Eric Laurent83b88082014-06-20 18:31:16 -0700170 mPatches.removeAt(index);
soon1.choid8cd4772015-01-05 14:27:42 +0900171 delete removedPatch;
Eric Laurent83b88082014-06-20 18:31:16 -0700172 break;
173 }
Eric Laurent951f4552014-05-20 10:48:17 -0700174 }
175 }
176
Eric Laurent83b88082014-06-20 18:31:16 -0700177 Patch *newPatch = new Patch(patch);
178
Eric Laurent951f4552014-05-20 10:48:17 -0700179 switch (patch->sources[0].type) {
180 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700181 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
182 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700183 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700184 ALOGW("createAudioPatch() bad src hw module %d", srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700185 status = BAD_VALUE;
186 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700187 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700188 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700189 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700190 // support only one sink if connection to a mix or across HW modules
191 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
192 patch->sinks[i].ext.mix.hw_module != srcModule) &&
193 patch->num_sinks > 1) {
194 status = INVALID_OPERATION;
195 goto exit;
196 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700197 // reject connection to different sink types
198 if (patch->sinks[i].type != patch->sinks[0].type) {
199 ALOGW("createAudioPatch() different sink types in same patch not supported");
Eric Laurent83b88082014-06-20 18:31:16 -0700200 status = BAD_VALUE;
201 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700202 }
Eric Laurent83b88082014-06-20 18:31:16 -0700203 // limit to connections between devices and input streams for HAL before 3.0
Eric Laurent874c42872014-08-08 15:13:39 -0700204 if (patch->sinks[i].ext.mix.hw_module == srcModule &&
Eric Laurent83b88082014-06-20 18:31:16 -0700205 (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0) &&
Eric Laurent6a94d692014-05-20 11:18:06 -0700206 (patch->sinks[i].type != AUDIO_PORT_TYPE_MIX)) {
207 ALOGW("createAudioPatch() invalid sink type %d for device source",
208 patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700209 status = BAD_VALUE;
210 goto exit;
Eric Laurent6a94d692014-05-20 11:18:06 -0700211 }
Eric Laurent951f4552014-05-20 10:48:17 -0700212 }
213
Eric Laurent874c42872014-08-08 15:13:39 -0700214 if (patch->sinks[0].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700215 // limit to device to device connection if not on same hw module
216 if (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE) {
217 ALOGW("createAudioPatch() invalid sink type for cross hw module");
218 status = INVALID_OPERATION;
219 goto exit;
220 }
221 // special case num sources == 2 -=> reuse an exiting output mix to connect to the
222 // sink
223 if (patch->num_sources == 2) {
224 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
225 patch->sinks[0].ext.device.hw_module !=
226 patch->sources[1].ext.mix.hw_module) {
227 ALOGW("createAudioPatch() invalid source combination");
228 status = INVALID_OPERATION;
229 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700230 }
Eric Laurent83b88082014-06-20 18:31:16 -0700231
232 sp<ThreadBase> thread =
233 audioflinger->checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
234 newPatch->mPlaybackThread = (MixerThread *)thread.get();
235 if (thread == 0) {
236 ALOGW("createAudioPatch() cannot get playback thread");
237 status = INVALID_OPERATION;
238 goto exit;
239 }
Eric Laurent951f4552014-05-20 10:48:17 -0700240 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700241 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
242 audio_devices_t device = patch->sinks[0].ext.device.type;
243 String8 address = String8(patch->sinks[0].ext.device.address);
244 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700245 newPatch->mPlaybackThread = audioflinger->openOutput_l(
246 patch->sinks[0].ext.device.hw_module,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700247 &output,
Eric Laurent83b88082014-06-20 18:31:16 -0700248 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700249 device,
250 address,
Eric Laurent83b88082014-06-20 18:31:16 -0700251 AUDIO_OUTPUT_FLAG_NONE);
252 ALOGV("audioflinger->openOutput_l() returned %p",
253 newPatch->mPlaybackThread.get());
254 if (newPatch->mPlaybackThread == 0) {
255 status = NO_MEMORY;
256 goto exit;
257 }
258 }
259 uint32_t channelCount = newPatch->mPlaybackThread->channelCount();
260 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700261 String8 address = String8(patch->sources[0].ext.device.address);
262 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent83b88082014-06-20 18:31:16 -0700263 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
264 config.sample_rate = newPatch->mPlaybackThread->sampleRate();
265 config.channel_mask = inChannelMask;
266 config.format = newPatch->mPlaybackThread->format();
Eric Laurentcf2c0212014-07-25 16:20:43 -0700267 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent874c42872014-08-08 15:13:39 -0700268 newPatch->mRecordThread = audioflinger->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);
275 ALOGV("audioflinger->openInput_l() returned %p inChannelMask %08x",
276 newPatch->mRecordThread.get(), inChannelMask);
277 if (newPatch->mRecordThread == 0) {
278 status = NO_MEMORY;
279 goto exit;
280 }
281 status = createPatchConnections(newPatch, patch);
282 if (status != NO_ERROR) {
283 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700284 }
285 } else {
Eric Laurent83b88082014-06-20 18:31:16 -0700286 if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
287 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
288 sp<ThreadBase> thread = audioflinger->checkRecordThread_l(
Eric Laurent874c42872014-08-08 15:13:39 -0700289 patch->sinks[0].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700290 if (thread == 0) {
291 ALOGW("createAudioPatch() bad capture I/O handle %d",
Eric Laurent874c42872014-08-08 15:13:39 -0700292 patch->sinks[0].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700293 status = BAD_VALUE;
294 goto exit;
295 }
296 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
297 } else {
298 audio_hw_device_t *hwDevice = audioHwDevice->hwDevice();
299 status = hwDevice->create_audio_patch(hwDevice,
300 patch->num_sources,
301 patch->sources,
302 patch->num_sinks,
303 patch->sinks,
304 &halHandle);
305 }
306 } else {
307 sp<ThreadBase> thread = audioflinger->checkRecordThread_l(
Eric Laurent874c42872014-08-08 15:13:39 -0700308 patch->sinks[0].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700309 if (thread == 0) {
310 ALOGW("createAudioPatch() bad capture I/O handle %d",
Eric Laurent874c42872014-08-08 15:13:39 -0700311 patch->sinks[0].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700312 status = BAD_VALUE;
313 goto exit;
314 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700315 char *address;
316 if (strcmp(patch->sources[0].ext.device.address, "") != 0) {
317 address = audio_device_address_to_parameter(
318 patch->sources[0].ext.device.type,
319 patch->sources[0].ext.device.address);
320 } else {
321 address = (char *)calloc(1, 1);
322 }
323 AudioParameter param = AudioParameter(String8(address));
324 free(address);
325 param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING),
Eric Laurent83b88082014-06-20 18:31:16 -0700326 (int)patch->sources[0].ext.device.type);
Eric Laurentcf2c0212014-07-25 16:20:43 -0700327 param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE),
328 (int)patch->sinks[0].ext.mix.usecase.source);
Eric Laurent83b88082014-06-20 18:31:16 -0700329 ALOGV("createAudioPatch() AUDIO_PORT_TYPE_DEVICE setParameters %s",
Eric Laurentcf2c0212014-07-25 16:20:43 -0700330 param.toString().string());
Eric Laurent83b88082014-06-20 18:31:16 -0700331 status = thread->setParameters(param.toString());
332 }
Eric Laurent951f4552014-05-20 10:48:17 -0700333 }
334 } break;
335 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700336 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
337 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700338 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700339 ALOGW("createAudioPatch() bad src hw module %d", srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700340 status = BAD_VALUE;
341 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700342 }
343 // limit to connections between devices and output streams
344 for (unsigned int i = 0; i < patch->num_sinks; i++) {
345 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Eric Laurent83b88082014-06-20 18:31:16 -0700346 ALOGW("createAudioPatch() invalid sink type %d for mix source",
Eric Laurent951f4552014-05-20 10:48:17 -0700347 patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700348 status = BAD_VALUE;
349 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700350 }
351 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700352 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700353 status = BAD_VALUE;
354 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700355 }
356 }
357 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
358 sp<ThreadBase> thread =
359 audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
360 if (thread == 0) {
361 ALOGW("createAudioPatch() bad playback I/O handle %d",
362 patch->sources[0].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700363 status = BAD_VALUE;
364 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700365 }
366 if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
367 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
368 } else {
369 audio_devices_t type = AUDIO_DEVICE_NONE;
370 for (unsigned int i = 0; i < patch->num_sinks; i++) {
371 type |= patch->sinks[i].ext.device.type;
372 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700373 char *address;
374 if (strcmp(patch->sinks[0].ext.device.address, "") != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700375 //FIXME: we only support address on first sink with HAL version < 3.0
Eric Laurentcf2c0212014-07-25 16:20:43 -0700376 address = audio_device_address_to_parameter(
377 patch->sinks[0].ext.device.type,
378 patch->sinks[0].ext.device.address);
379 } else {
380 address = (char *)calloc(1, 1);
381 }
382 AudioParameter param = AudioParameter(String8(address));
383 free(address);
384 param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type);
Eric Laurent951f4552014-05-20 10:48:17 -0700385 status = thread->setParameters(param.toString());
386 }
387
388 } break;
389 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700390 status = BAD_VALUE;
391 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700392 }
Eric Laurent83b88082014-06-20 18:31:16 -0700393exit:
Eric Laurent951f4552014-05-20 10:48:17 -0700394 ALOGV("createAudioPatch() status %d", status);
395 if (status == NO_ERROR) {
396 *handle = audioflinger->nextUniqueId();
Eric Laurent951f4552014-05-20 10:48:17 -0700397 newPatch->mHandle = *handle;
398 newPatch->mHalHandle = halHandle;
399 mPatches.add(newPatch);
400 ALOGV("createAudioPatch() added new patch handle %d halHandle %d", *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700401 } else {
402 clearPatchConnections(newPatch);
403 delete newPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700404 }
405 return status;
406}
407
Eric Laurent83b88082014-06-20 18:31:16 -0700408status_t AudioFlinger::PatchPanel::createPatchConnections(Patch *patch,
409 const struct audio_patch *audioPatch)
410{
411 // create patch from source device to record thread input
412 struct audio_patch subPatch;
413 subPatch.num_sources = 1;
414 subPatch.sources[0] = audioPatch->sources[0];
415 subPatch.num_sinks = 1;
416
417 patch->mRecordThread->getAudioPortConfig(&subPatch.sinks[0]);
418 subPatch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_MIC;
419
420 status_t status = createAudioPatch(&subPatch, &patch->mRecordPatchHandle);
421 if (status != NO_ERROR) {
422 patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE;
423 return status;
424 }
425
426 // create patch from playback thread output to sink device
427 patch->mPlaybackThread->getAudioPortConfig(&subPatch.sources[0]);
428 subPatch.sinks[0] = audioPatch->sinks[0];
429 status = createAudioPatch(&subPatch, &patch->mPlaybackPatchHandle);
430 if (status != NO_ERROR) {
431 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
432 return status;
433 }
434
435 // use a pseudo LCM between input and output framecount
436 size_t playbackFrameCount = patch->mPlaybackThread->frameCount();
437 int playbackShift = __builtin_ctz(playbackFrameCount);
438 size_t recordFramecount = patch->mRecordThread->frameCount();
439 int shift = __builtin_ctz(recordFramecount);
440 if (playbackShift < shift) {
441 shift = playbackShift;
442 }
443 size_t frameCount = (playbackFrameCount * recordFramecount) >> shift;
444 ALOGV("createPatchConnections() playframeCount %d recordFramecount %d frameCount %d ",
445 playbackFrameCount, recordFramecount, frameCount);
446
447 // create a special record track to capture from record thread
448 uint32_t channelCount = patch->mPlaybackThread->channelCount();
449 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
450 audio_channel_mask_t outChannelMask = patch->mPlaybackThread->channelMask();
451 uint32_t sampleRate = patch->mPlaybackThread->sampleRate();
452 audio_format_t format = patch->mPlaybackThread->format();
453
454 patch->mPatchRecord = new RecordThread::PatchRecord(
455 patch->mRecordThread.get(),
456 sampleRate,
457 inChannelMask,
458 format,
459 frameCount,
460 NULL,
461 IAudioFlinger::TRACK_DEFAULT);
462 if (patch->mPatchRecord == 0) {
463 return NO_MEMORY;
464 }
465 status = patch->mPatchRecord->initCheck();
466 if (status != NO_ERROR) {
467 return status;
468 }
469 patch->mRecordThread->addPatchRecord(patch->mPatchRecord);
470
471 // create a special playback track to render to playback thread.
472 // this track is given the same buffer as the PatchRecord buffer
473 patch->mPatchTrack = new PlaybackThread::PatchTrack(
474 patch->mPlaybackThread.get(),
475 sampleRate,
476 outChannelMask,
477 format,
478 frameCount,
479 patch->mPatchRecord->buffer(),
480 IAudioFlinger::TRACK_DEFAULT);
481 if (patch->mPatchTrack == 0) {
482 return NO_MEMORY;
483 }
484 status = patch->mPatchTrack->initCheck();
485 if (status != NO_ERROR) {
486 return status;
487 }
488 patch->mPlaybackThread->addPatchTrack(patch->mPatchTrack);
489
490 // tie playback and record tracks together
491 patch->mPatchRecord->setPeerProxy(patch->mPatchTrack.get());
492 patch->mPatchTrack->setPeerProxy(patch->mPatchRecord.get());
493
494 // start capture and playback
495 patch->mPatchRecord->start(AudioSystem::SYNC_EVENT_NONE, 0);
496 patch->mPatchTrack->start();
497
498 return status;
499}
500
501void AudioFlinger::PatchPanel::clearPatchConnections(Patch *patch)
502{
503 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
504 if (audioflinger == 0) {
505 return;
506 }
507
508 ALOGV("clearPatchConnections() patch->mRecordPatchHandle %d patch->mPlaybackPatchHandle %d",
509 patch->mRecordPatchHandle, patch->mPlaybackPatchHandle);
510
511 if (patch->mPatchRecord != 0) {
512 patch->mPatchRecord->stop();
513 }
514 if (patch->mPatchTrack != 0) {
515 patch->mPatchTrack->stop();
516 }
517 if (patch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
518 releaseAudioPatch(patch->mRecordPatchHandle);
519 patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE;
520 }
521 if (patch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
522 releaseAudioPatch(patch->mPlaybackPatchHandle);
523 patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE;
524 }
525 if (patch->mRecordThread != 0) {
526 if (patch->mPatchRecord != 0) {
527 patch->mRecordThread->deletePatchRecord(patch->mPatchRecord);
528 patch->mPatchRecord.clear();
529 }
530 audioflinger->closeInputInternal_l(patch->mRecordThread);
531 patch->mRecordThread.clear();
532 }
533 if (patch->mPlaybackThread != 0) {
534 if (patch->mPatchTrack != 0) {
535 patch->mPlaybackThread->deletePatchTrack(patch->mPatchTrack);
536 patch->mPatchTrack.clear();
537 }
538 // if num sources == 2 we are reusing an existing playback thread so we do not close it
539 if (patch->mAudioPatch.num_sources != 2) {
540 audioflinger->closeOutputInternal_l(patch->mPlaybackThread);
541 }
542 patch->mPlaybackThread.clear();
543 }
544}
545
Eric Laurent951f4552014-05-20 10:48:17 -0700546/* Disconnect a patch */
547status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
548{
549 ALOGV("releaseAudioPatch handle %d", handle);
550 status_t status = NO_ERROR;
551 size_t index;
552
553 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
554 if (audioflinger == 0) {
555 return NO_INIT;
556 }
557
558 for (index = 0; index < mPatches.size(); index++) {
559 if (handle == mPatches[index]->mHandle) {
560 break;
561 }
562 }
563 if (index == mPatches.size()) {
564 return BAD_VALUE;
565 }
Eric Laurent83b88082014-06-20 18:31:16 -0700566 Patch *removedPatch = mPatches[index];
567 mPatches.removeAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700568
Eric Laurent83b88082014-06-20 18:31:16 -0700569 struct audio_patch *patch = &removedPatch->mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700570
571 switch (patch->sources[0].type) {
572 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700573 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
574 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700575 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700576 ALOGW("releaseAudioPatch() bad src hw module %d", srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700577 status = BAD_VALUE;
578 break;
579 }
Eric Laurent83b88082014-06-20 18:31:16 -0700580
581 if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
Eric Laurent874c42872014-08-08 15:13:39 -0700582 patch->sinks[0].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700583 clearPatchConnections(removedPatch);
584 break;
585 }
586
Eric Laurent951f4552014-05-20 10:48:17 -0700587 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
588 if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
589 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
590 sp<ThreadBase> thread = audioflinger->checkRecordThread_l(
591 patch->sinks[0].ext.mix.handle);
592 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700593 ALOGW("releaseAudioPatch() bad capture I/O handle %d",
Eric Laurent951f4552014-05-20 10:48:17 -0700594 patch->sinks[0].ext.mix.handle);
595 status = BAD_VALUE;
596 break;
597 }
Eric Laurentf8fd8d62014-09-09 17:00:14 -0700598 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700599 } else {
600 audio_hw_device_t *hwDevice = audioHwDevice->hwDevice();
Eric Laurentf8fd8d62014-09-09 17:00:14 -0700601 status = hwDevice->release_audio_patch(hwDevice, removedPatch->mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700602 }
603 } else {
604 sp<ThreadBase> thread = audioflinger->checkRecordThread_l(
605 patch->sinks[0].ext.mix.handle);
606 if (thread == 0) {
607 ALOGW("releaseAudioPatch() bad capture I/O handle %d",
608 patch->sinks[0].ext.mix.handle);
609 status = BAD_VALUE;
610 break;
611 }
612 AudioParameter param;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700613 param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0);
Eric Laurent24478d42014-06-04 20:02:57 -0700614 ALOGV("releaseAudioPatch() AUDIO_PORT_TYPE_DEVICE setParameters %s",
Eric Laurent951f4552014-05-20 10:48:17 -0700615 param.toString().string());
616 status = thread->setParameters(param.toString());
617 }
618 } break;
619 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700620 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
621 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700622 if (index < 0) {
Eric Laurent874c42872014-08-08 15:13:39 -0700623 ALOGW("releaseAudioPatch() bad src hw module %d", srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700624 status = BAD_VALUE;
625 break;
626 }
627 sp<ThreadBase> thread =
628 audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
629 if (thread == 0) {
630 ALOGW("releaseAudioPatch() bad playback I/O handle %d",
631 patch->sources[0].ext.mix.handle);
632 status = BAD_VALUE;
633 break;
634 }
635 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
636 if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
Eric Laurentf8fd8d62014-09-09 17:00:14 -0700637 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700638 } else {
639 AudioParameter param;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700640 param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0);
Eric Laurent951f4552014-05-20 10:48:17 -0700641 status = thread->setParameters(param.toString());
642 }
643 } break;
644 default:
645 status = BAD_VALUE;
646 break;
647 }
648
Eric Laurent83b88082014-06-20 18:31:16 -0700649 delete removedPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700650 return status;
651}
652
653
654/* List connected audio ports and they attributes */
655status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
656 struct audio_patch *patches __unused)
657{
658 ALOGV("listAudioPatches");
659 return NO_ERROR;
660}
661
662/* Set audio port configuration */
Eric Laurente1715a42014-05-20 11:30:42 -0700663status_t AudioFlinger::PatchPanel::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent951f4552014-05-20 10:48:17 -0700664{
665 ALOGV("setAudioPortConfig");
Eric Laurente1715a42014-05-20 11:30:42 -0700666 status_t status = NO_ERROR;
667
668 sp<AudioFlinger> audioflinger = mAudioFlinger.promote();
669 if (audioflinger == 0) {
670 return NO_INIT;
671 }
672
673 audio_module_handle_t module;
674 if (config->type == AUDIO_PORT_TYPE_DEVICE) {
675 module = config->ext.device.hw_module;
676 } else {
677 module = config->ext.mix.hw_module;
678 }
679
680 ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(module);
681 if (index < 0) {
682 ALOGW("setAudioPortConfig() bad hw module %d", module);
683 return BAD_VALUE;
684 }
685
686 AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index);
687 if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
688 audio_hw_device_t *hwDevice = audioHwDevice->hwDevice();
689 return hwDevice->set_audio_port_config(hwDevice, config);
690 } else {
691 return INVALID_OPERATION;
692 }
Eric Laurent951f4552014-05-20 10:48:17 -0700693 return NO_ERROR;
694}
695
696
Eric Laurent951f4552014-05-20 10:48:17 -0700697}; // namespace android