1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "APM::AudioInputDescriptor"
18 //#define LOG_NDEBUG 0
19
20 #include <audiomanager/AudioManager.h>
21 #include <media/AudioPolicy.h>
22 #include <policy.h>
23 #include <AudioPolicyInterface.h>
24 #include "AudioInputDescriptor.h"
25 #include "AudioGain.h"
26 #include "AudioPolicyMix.h"
27 #include "HwModule.h"
28
29 namespace android {
30
AudioInputDescriptor(const sp<IOProfile> & profile,AudioPolicyClientInterface * clientInterface)31 AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile,
32 AudioPolicyClientInterface *clientInterface)
33 : mProfile(profile)
34 , mClientInterface(clientInterface)
35 {
36 if (profile != NULL) {
37 profile->pickAudioProfile(mSamplingRate, mChannelMask, mFormat);
38 if (profile->mGains.size() > 0) {
39 profile->mGains[0]->getDefaultConfig(&mGain);
40 }
41 }
42 }
43
getModuleHandle() const44 audio_module_handle_t AudioInputDescriptor::getModuleHandle() const
45 {
46 if (mProfile == 0) {
47 return AUDIO_MODULE_HANDLE_NONE;
48 }
49 return mProfile->getModuleHandle();
50 }
51
getId() const52 audio_port_handle_t AudioInputDescriptor::getId() const
53 {
54 return mId;
55 }
56
source() const57 audio_source_t AudioInputDescriptor::source() const
58 {
59 return getHighestPriorityAttributes().source;
60 }
61
toAudioPortConfig(struct audio_port_config * dstConfig,const struct audio_port_config * srcConfig) const62 void AudioInputDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig,
63 const struct audio_port_config *srcConfig) const
64 {
65 ALOG_ASSERT(mProfile != 0,
66 "toAudioPortConfig() called on input with null profile %d", mIoHandle);
67 dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
68 AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
69 if (srcConfig != NULL) {
70 dstConfig->config_mask |= srcConfig->config_mask;
71 }
72
73 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
74
75 dstConfig->id = mId;
76 dstConfig->role = AUDIO_PORT_ROLE_SINK;
77 dstConfig->type = AUDIO_PORT_TYPE_MIX;
78 dstConfig->ext.mix.hw_module = getModuleHandle();
79 dstConfig->ext.mix.handle = mIoHandle;
80 dstConfig->ext.mix.usecase.source = source();
81 }
82
toAudioPort(struct audio_port * port) const83 void AudioInputDescriptor::toAudioPort(struct audio_port *port) const
84 {
85 ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle);
86
87 mProfile->toAudioPort(port);
88 port->id = mId;
89 toAudioPortConfig(&port->active_config);
90 port->ext.mix.hw_module = getModuleHandle();
91 port->ext.mix.handle = mIoHandle;
92 port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
93 }
94
setPreemptedSessions(const SortedVector<audio_session_t> & sessions)95 void AudioInputDescriptor::setPreemptedSessions(const SortedVector<audio_session_t>& sessions)
96 {
97 mPreemptedSessions = sessions;
98 }
99
getPreemptedSessions() const100 SortedVector<audio_session_t> AudioInputDescriptor::getPreemptedSessions() const
101 {
102 return mPreemptedSessions;
103 }
104
hasPreemptedSession(audio_session_t session) const105 bool AudioInputDescriptor::hasPreemptedSession(audio_session_t session) const
106 {
107 return (mPreemptedSessions.indexOf(session) >= 0);
108 }
109
clearPreemptedSessions()110 void AudioInputDescriptor::clearPreemptedSessions()
111 {
112 mPreemptedSessions.clear();
113 }
114
isSourceActive(audio_source_t source) const115 bool AudioInputDescriptor::isSourceActive(audio_source_t source) const
116 {
117 for (const auto &client : getClientIterable()) {
118 if (client->active() &&
119 ((client->source() == source) ||
120 ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
121 (client->source() == AUDIO_SOURCE_HOTWORD) &&
122 client->isSoundTrigger()))) {
123 return true;
124 }
125 }
126 return false;
127 }
128
getHighestPriorityAttributes() const129 audio_attributes_t AudioInputDescriptor::getHighestPriorityAttributes() const
130 {
131 audio_attributes_t attributes = { .source = AUDIO_SOURCE_DEFAULT };
132 sp<RecordClientDescriptor> topClient = getHighestPriorityClient();
133 return topClient ? topClient->attributes() : attributes;
134 }
135
getHighestPriorityClient() const136 sp<RecordClientDescriptor> AudioInputDescriptor::getHighestPriorityClient() const
137 {
138 sp<RecordClientDescriptor> topClient;
139
140 for (bool activeOnly : { true, false }) {
141 int32_t topPriority = -1;
142 app_state_t topState = APP_STATE_IDLE;
143 for (const auto &client : getClientIterable()) {
144 if (activeOnly && !client->active()) {
145 continue;
146 }
147 app_state_t curState = client->appState();
148 if (curState >= topState) {
149 int32_t curPriority = source_priority(client->source());
150 if (curPriority >= topPriority) {
151 topClient = client;
152 topPriority = curPriority;
153 }
154 topState = curState;
155 }
156 }
157 if (topClient != nullptr) {
158 break;
159 }
160 }
161 return topClient;
162 }
163
isSoundTrigger() const164 bool AudioInputDescriptor::isSoundTrigger() const {
165 // sound trigger and non sound trigger clients are not mixed on a given input
166 // so check only first client
167 if (getClientCount() == 0) {
168 return false;
169 }
170 return getClientIterable().begin()->isSoundTrigger();
171 }
172
getPatchHandle() const173 audio_patch_handle_t AudioInputDescriptor::getPatchHandle() const
174 {
175 return mPatchHandle;
176 }
177
setPatchHandle(audio_patch_handle_t handle)178 void AudioInputDescriptor::setPatchHandle(audio_patch_handle_t handle)
179 {
180 mPatchHandle = handle;
181 for (const auto &client : getClientIterable()) {
182 if (client->active()) {
183 updateClientRecordingConfiguration(
184 client->isLowLevel() ? RECORD_CONFIG_EVENT_START : RECORD_CONFIG_EVENT_UPDATE,
185 client);
186 }
187 }
188 }
189
getConfig() const190 audio_config_base_t AudioInputDescriptor::getConfig() const
191 {
192 const audio_config_base_t config = { .sample_rate = mSamplingRate, .channel_mask = mChannelMask,
193 .format = mFormat };
194 return config;
195 }
196
open(const audio_config_t * config,const sp<DeviceDescriptor> & device,audio_source_t source,audio_input_flags_t flags,audio_io_handle_t * input)197 status_t AudioInputDescriptor::open(const audio_config_t *config,
198 const sp<DeviceDescriptor> &device,
199 audio_source_t source,
200 audio_input_flags_t flags,
201 audio_io_handle_t *input)
202 {
203 audio_config_t lConfig;
204 if (config == nullptr) {
205 lConfig = AUDIO_CONFIG_INITIALIZER;
206 lConfig.sample_rate = mSamplingRate;
207 lConfig.channel_mask = mChannelMask;
208 lConfig.format = mFormat;
209 } else {
210 lConfig = *config;
211 }
212
213 mDevice = device;
214
215 ALOGV("opening input for device %s profile %p name %s",
216 mDevice->toString().c_str(), mProfile.get(), mProfile->getName().string());
217
218 audio_devices_t deviceType = mDevice->type();
219
220 status_t status = mClientInterface->openInput(mProfile->getModuleHandle(),
221 input,
222 &lConfig,
223 &deviceType,
224 mDevice->address(),
225 source,
226 flags);
227 LOG_ALWAYS_FATAL_IF(mDevice->type() != deviceType,
228 "%s openInput returned device %08x when given device %08x",
229 __FUNCTION__, mDevice->type(), deviceType);
230
231 if (status == NO_ERROR) {
232 LOG_ALWAYS_FATAL_IF(*input == AUDIO_IO_HANDLE_NONE,
233 "%s openInput returned input handle %d for device %s",
234 __FUNCTION__, *input, mDevice->toString().c_str());
235 mSamplingRate = lConfig.sample_rate;
236 mChannelMask = lConfig.channel_mask;
237 mFormat = lConfig.format;
238 mId = AudioPort::getNextUniqueId();
239 mIoHandle = *input;
240 mProfile->curOpenCount++;
241 }
242
243 return status;
244 }
245
start()246 status_t AudioInputDescriptor::start()
247 {
248 if (!isActive()) {
249 if (!mProfile->canStartNewIo()) {
250 ALOGI("%s mProfile->curActiveCount %d", __func__, mProfile->curActiveCount);
251 return INVALID_OPERATION;
252 }
253 mProfile->curActiveCount++;
254 }
255 return NO_ERROR;
256 }
257
stop()258 void AudioInputDescriptor::stop()
259 {
260 if (!isActive()) {
261 LOG_ALWAYS_FATAL_IF(mProfile->curActiveCount < 1,
262 "%s invalid profile active count %u",
263 __func__, mProfile->curActiveCount);
264 mProfile->curActiveCount--;
265 }
266 }
267
close()268 void AudioInputDescriptor::close()
269 {
270 if (mIoHandle != AUDIO_IO_HANDLE_NONE) {
271 // clean up active clients if any (can happen if close() is called to force
272 // clients to reconnect
273 for (const auto &client : getClientIterable()) {
274 if (client->active()) {
275 ALOGW("%s client with port ID %d still active on input %d",
276 __func__, client->portId(), mId);
277 setClientActive(client, false);
278 stop();
279 }
280 }
281
282 mClientInterface->closeInput(mIoHandle);
283 LOG_ALWAYS_FATAL_IF(mProfile->curOpenCount < 1, "%s profile open count %u",
284 __FUNCTION__, mProfile->curOpenCount);
285
286 mProfile->curOpenCount--;
287 LOG_ALWAYS_FATAL_IF(mProfile->curOpenCount < mProfile->curActiveCount,
288 "%s(%d): mProfile->curOpenCount %d < mProfile->curActiveCount %d.",
289 __func__, mId, mProfile->curOpenCount, mProfile->curActiveCount);
290 mIoHandle = AUDIO_IO_HANDLE_NONE;
291 }
292 }
293
addClient(const sp<RecordClientDescriptor> & client)294 void AudioInputDescriptor::addClient(const sp<RecordClientDescriptor> &client) {
295 ClientMapHandler<RecordClientDescriptor>::addClient(client);
296
297 for (size_t i = 0; i < mEnabledEffects.size(); i++) {
298 if (mEnabledEffects.valueAt(i)->mSession == client->session()) {
299 client->trackEffectEnabled(mEnabledEffects.valueAt(i), true);
300 }
301 }
302 }
303
setClientActive(const sp<RecordClientDescriptor> & client,bool active)304 void AudioInputDescriptor::setClientActive(const sp<RecordClientDescriptor>& client, bool active)
305 {
306 LOG_ALWAYS_FATAL_IF(getClient(client->portId()) == nullptr,
307 "%s(%d) does not exist on input descriptor", __func__, client->portId());
308 if (active == client->active()) {
309 return;
310 }
311
312 // Handle non-client-specific activity ref count
313 int32_t oldGlobalActiveCount = mGlobalActiveCount;
314 if (!active && mGlobalActiveCount < 1) {
315 LOG_ALWAYS_FATAL("%s(%d) invalid deactivation with globalActiveCount %d",
316 __func__, client->portId(), mGlobalActiveCount);
317 // mGlobalActiveCount = 1;
318 }
319 const int delta = active ? 1 : -1;
320 mGlobalActiveCount += delta;
321
322 sp<AudioPolicyMix> policyMix = mPolicyMix.promote();
323 if ((oldGlobalActiveCount == 0) && (mGlobalActiveCount > 0)) {
324 if ((policyMix != NULL) && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0))
325 {
326 mClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress,
327 MIX_STATE_MIXING);
328 }
329 } else if ((oldGlobalActiveCount > 0) && (mGlobalActiveCount == 0)) {
330 if ((policyMix != NULL) && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0))
331 {
332 mClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress,
333 MIX_STATE_IDLE);
334 }
335 }
336
337 client->setActive(active);
338
339 checkSuspendEffects();
340
341 int event = active ? RECORD_CONFIG_EVENT_START : RECORD_CONFIG_EVENT_STOP;
342 updateClientRecordingConfiguration(event, client);
343 }
344
updateClientRecordingConfiguration(int event,const sp<RecordClientDescriptor> & client)345 void AudioInputDescriptor::updateClientRecordingConfiguration(
346 int event, const sp<RecordClientDescriptor>& client)
347 {
348 ALOGV("%s riid %d uid %d port %d session %d event %d",
349 __func__, client->riid(), client->uid(), client->portId(), client->session(), event);
350 // do not send callback if starting and no device is selected yet to avoid
351 // double callbacks from startInput() before and after the device is selected
352 // "start" and "stop" events for "high level" clients (AudioRecord) are sent by the client side
353 if ((event == RECORD_CONFIG_EVENT_START && mPatchHandle == AUDIO_PATCH_HANDLE_NONE)
354 || (!client->isLowLevel()
355 && (event == RECORD_CONFIG_EVENT_START || event == RECORD_CONFIG_EVENT_STOP))) {
356 return;
357 }
358
359 const audio_config_base_t sessionConfig = client->config();
360 const record_client_info_t recordClientInfo{client->riid(), client->uid(), client->session(),
361 client->source(), client->portId(),
362 client->isSilenced()};
363 const audio_config_base_t config = getConfig();
364
365 std::vector<effect_descriptor_t> clientEffects;
366 EffectDescriptorCollection effectsList = client->getEnabledEffects();
367 for (size_t i = 0; i < effectsList.size(); i++) {
368 clientEffects.push_back(effectsList.valueAt(i)->mDesc);
369 }
370
371 std::vector<effect_descriptor_t> effects;
372 effectsList = getEnabledEffects();
373 for (size_t i = 0; i < effectsList.size(); i++) {
374 effects.push_back(effectsList.valueAt(i)->mDesc);
375 }
376
377 mClientInterface->onRecordingConfigurationUpdate(event, &recordClientInfo, &sessionConfig,
378 clientEffects, &config, effects,
379 mPatchHandle, source());
380 }
381
getClientsForSession(audio_session_t session)382 RecordClientVector AudioInputDescriptor::getClientsForSession(
383 audio_session_t session)
384 {
385 RecordClientVector clients;
386 for (const auto &client : getClientIterable()) {
387 if (client->session() == session) {
388 clients.push_back(client);
389 }
390 }
391 return clients;
392 }
393
clientsList(bool activeOnly,audio_source_t source,bool preferredDeviceOnly) const394 RecordClientVector AudioInputDescriptor::clientsList(bool activeOnly, audio_source_t source,
395 bool preferredDeviceOnly) const
396 {
397 RecordClientVector clients;
398 for (const auto &client : getClientIterable()) {
399 if ((!activeOnly || client->active())
400 && (source == AUDIO_SOURCE_DEFAULT || source == client->source())
401 && (!preferredDeviceOnly || client->hasPreferredDevice())) {
402 clients.push_back(client);
403 }
404 }
405 return clients;
406 }
407
trackEffectEnabled(const sp<EffectDescriptor> & effect,bool enabled)408 void AudioInputDescriptor::trackEffectEnabled(const sp<EffectDescriptor> &effect,
409 bool enabled)
410 {
411 if (enabled) {
412 mEnabledEffects.replaceValueFor(effect->mId, effect);
413 } else {
414 mEnabledEffects.removeItem(effect->mId);
415 // always exit from suspend when disabling an effect as only enabled effects
416 // are managed by checkSuspendEffects()
417 if (effect->mSuspended) {
418 effect->mSuspended = false;
419 mClientInterface->setEffectSuspended(effect->mId, effect->mSession, effect->mSuspended);
420 }
421 }
422
423 RecordClientVector clients = getClientsForSession((audio_session_t)effect->mSession);
424 RecordClientVector updatedClients;
425
426 for (const auto& client : clients) {
427 sp<EffectDescriptor> clientEffect = client->getEnabledEffects().getEffect(effect->mId);
428 bool changed = (enabled && clientEffect == nullptr)
429 || (!enabled && clientEffect != nullptr);
430 client->trackEffectEnabled(effect, enabled);
431 if (changed && client->active()) {
432 updatedClients.push_back(client);
433 }
434 }
435
436 checkSuspendEffects();
437
438 for (const auto& client : updatedClients) {
439 updateClientRecordingConfiguration(RECORD_CONFIG_EVENT_UPDATE, client);
440 }
441 }
442
getEnabledEffects() const443 EffectDescriptorCollection AudioInputDescriptor::getEnabledEffects() const
444 {
445 // report effects for highest priority active source as applied to all clients
446 EffectDescriptorCollection enabledEffects;
447 sp<RecordClientDescriptor> topClient = getHighestPriorityClient();
448 if (topClient != nullptr) {
449 enabledEffects = topClient->getEnabledEffects();
450 }
451 return enabledEffects;
452 }
453
setAppState(uid_t uid,app_state_t state)454 void AudioInputDescriptor::setAppState(uid_t uid, app_state_t state)
455 {
456 RecordClientVector clients = clientsList(false /*activeOnly*/);
457 RecordClientVector updatedClients;
458
459 for (const auto& client : clients) {
460 if (uid == client->uid()) {
461 bool wasSilenced = client->isSilenced();
462 client->setAppState(state);
463 if (client->active() && wasSilenced != client->isSilenced()) {
464 updatedClients.push_back(client);
465 }
466 }
467 }
468
469 checkSuspendEffects();
470
471 for (const auto& client : updatedClients) {
472 updateClientRecordingConfiguration(RECORD_CONFIG_EVENT_UPDATE, client);
473 }
474 }
475
checkSuspendEffects()476 void AudioInputDescriptor::checkSuspendEffects()
477 {
478 sp<RecordClientDescriptor> topClient = getHighestPriorityClient();
479 if (topClient == nullptr) {
480 return;
481 }
482
483 for (size_t i = 0; i < mEnabledEffects.size(); i++) {
484 sp<EffectDescriptor> effect = mEnabledEffects.valueAt(i);
485 if (effect->mSession == topClient->session()) {
486 if (effect->mSuspended) {
487 effect->mSuspended = false;
488 mClientInterface->setEffectSuspended(effect->mId,
489 effect->mSession,
490 effect->mSuspended);
491 }
492 } else if (!effect->mSuspended) {
493 effect->mSuspended = true;
494 mClientInterface->setEffectSuspended(effect->mId,
495 effect->mSession,
496 effect->mSuspended);
497 }
498 }
499 }
500
dump(String8 * dst) const501 void AudioInputDescriptor::dump(String8 *dst) const
502 {
503 dst->appendFormat(" ID: %d\n", getId());
504 dst->appendFormat(" Sampling rate: %d\n", mSamplingRate);
505 dst->appendFormat(" Format: %d\n", mFormat);
506 dst->appendFormat(" Channels: %08x\n", mChannelMask);
507 dst->appendFormat(" Devices %s\n", mDevice->toString().c_str());
508 mEnabledEffects.dump(dst, 1 /*spaces*/, false /*verbose*/);
509 dst->append(" AudioRecord Clients:\n");
510 ClientMapHandler<RecordClientDescriptor>::dump(dst);
511 dst->append("\n");
512 }
513
isSourceActive(audio_source_t source) const514 bool AudioInputCollection::isSourceActive(audio_source_t source) const
515 {
516 for (size_t i = 0; i < size(); i++) {
517 const sp<AudioInputDescriptor> inputDescriptor = valueAt(i);
518 if (inputDescriptor->isSourceActive(source)) {
519 return true;
520 }
521 }
522 return false;
523 }
524
getInputFromId(audio_port_handle_t id) const525 sp<AudioInputDescriptor> AudioInputCollection::getInputFromId(audio_port_handle_t id) const
526 {
527 for (size_t i = 0; i < size(); i++) {
528 const sp<AudioInputDescriptor> inputDescriptor = valueAt(i);
529 if (inputDescriptor->getId() == id) {
530 return inputDescriptor;
531 }
532 }
533 return NULL;
534 }
535
activeInputsCountOnDevices(const DeviceVector & devices) const536 uint32_t AudioInputCollection::activeInputsCountOnDevices(const DeviceVector &devices) const
537 {
538 uint32_t count = 0;
539 for (size_t i = 0; i < size(); i++) {
540 const sp<AudioInputDescriptor> inputDescriptor = valueAt(i);
541 if (inputDescriptor->isActive() &&
542 (devices.isEmpty() || devices.contains(inputDescriptor->getDevice()))) {
543 count++;
544 }
545 }
546 return count;
547 }
548
getActiveInputs()549 Vector<sp <AudioInputDescriptor> > AudioInputCollection::getActiveInputs()
550 {
551 Vector<sp <AudioInputDescriptor> > activeInputs;
552
553 for (size_t i = 0; i < size(); i++) {
554 const sp<AudioInputDescriptor> inputDescriptor = valueAt(i);
555 if (inputDescriptor->isActive()) {
556 activeInputs.add(inputDescriptor);
557 }
558 }
559 return activeInputs;
560 }
561
getInputForClient(audio_port_handle_t portId)562 sp<AudioInputDescriptor> AudioInputCollection::getInputForClient(audio_port_handle_t portId)
563 {
564 for (size_t i = 0; i < size(); i++) {
565 sp<AudioInputDescriptor> inputDesc = valueAt(i);
566 if (inputDesc->getClient(portId) != nullptr) {
567 return inputDesc;
568 }
569 }
570 return 0;
571 }
572
trackEffectEnabled(const sp<EffectDescriptor> & effect,bool enabled)573 void AudioInputCollection::trackEffectEnabled(const sp<EffectDescriptor> &effect,
574 bool enabled)
575 {
576 for (size_t i = 0; i < size(); i++) {
577 sp<AudioInputDescriptor> inputDesc = valueAt(i);
578 if (inputDesc->mIoHandle == effect->mIo) {
579 return inputDesc->trackEffectEnabled(effect, enabled);
580 }
581 }
582 }
583
clearSessionRoutesForDevice(const sp<DeviceDescriptor> & disconnectedDevice)584 void AudioInputCollection::clearSessionRoutesForDevice(
585 const sp<DeviceDescriptor> &disconnectedDevice)
586 {
587 for (size_t i = 0; i < size(); i++) {
588 sp<AudioInputDescriptor> inputDesc = valueAt(i);
589 for (const auto& client : inputDesc->getClientIterable()) {
590 if (client->preferredDeviceId() == disconnectedDevice->getId()) {
591 client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE);
592 }
593 }
594 }
595 }
596
dump(String8 * dst) const597 void AudioInputCollection::dump(String8 *dst) const
598 {
599 dst->append("\nInputs dump:\n");
600 for (size_t i = 0; i < size(); i++) {
601 dst->appendFormat("- Input %d dump:\n", keyAt(i));
602 valueAt(i)->dump(dst);
603 }
604 }
605
606 }; //namespace android
607