xref: /aosp_15_r20/frameworks/av/services/audiopolicy/common/managerdefinitions/src/AudioInputDescriptor.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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 <android-base/stringprintf.h>
21 
22 #include <audiomanager/AudioManager.h>
23 #include <media/AudioPolicy.h>
24 #include <policy.h>
25 #include <AudioPolicyInterface.h>
26 #include "AudioInputDescriptor.h"
27 #include "AudioPolicyMix.h"
28 #include "HwModule.h"
29 
30 namespace android {
31 
AudioInputDescriptor(const sp<IOProfile> & profile,AudioPolicyClientInterface * clientInterface,bool isPreemptor)32 AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile,
33                                            AudioPolicyClientInterface *clientInterface,
34                                            bool isPreemptor)
35     : mProfile(profile)
36     ,  mClientInterface(clientInterface), mIsPreemptor(isPreemptor)
37 {
38     if (profile != NULL) {
39         profile->pickAudioProfile(mSamplingRate, mChannelMask, mFormat);
40         if (profile->getGains().size() > 0) {
41             profile->getGains()[0]->getDefaultConfig(&mGain);
42         }
43         mFlags = (audio_input_flags_t)profile->getFlags();
44     }
45 }
46 
getModuleHandle() const47 audio_module_handle_t AudioInputDescriptor::getModuleHandle() const
48 {
49     if (mProfile == 0) {
50         return AUDIO_MODULE_HANDLE_NONE;
51     }
52     return mProfile->getModuleHandle();
53 }
54 
source() const55 audio_source_t AudioInputDescriptor::source() const
56 {
57     return getHighestPriorityAttributes().source;
58 }
59 
applyAudioPortConfig(const struct audio_port_config * config,audio_port_config * backupConfig)60 status_t AudioInputDescriptor::applyAudioPortConfig(const struct audio_port_config *config,
61                                                     audio_port_config *backupConfig)
62 {
63     struct audio_port_config localBackupConfig = { .config_mask = config->config_mask };
64     status_t status = NO_ERROR;
65 
66     toAudioPortConfig(&localBackupConfig);
67     if ((status = validationBeforeApplyConfig(config)) == NO_ERROR) {
68         AudioPortConfig::applyAudioPortConfig(config, backupConfig);
69     }
70 
71     if (backupConfig != NULL) {
72         *backupConfig = localBackupConfig;
73     }
74     return status;
75 }
76 
toAudioPortConfig(struct audio_port_config * dstConfig,const struct audio_port_config * srcConfig) const77 void AudioInputDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig,
78                                              const struct audio_port_config *srcConfig) const
79 {
80     ALOG_ASSERT(mProfile != 0,
81                 "toAudioPortConfig() called on input with null profile %d", mIoHandle);
82     dstConfig->config_mask = AUDIO_PORT_CONFIG_ALL;
83     if (srcConfig != NULL) {
84         dstConfig->config_mask |= srcConfig->config_mask;
85     }
86 
87     AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
88 
89     dstConfig->role = AUDIO_PORT_ROLE_SINK;
90     dstConfig->type = AUDIO_PORT_TYPE_MIX;
91     dstConfig->ext.mix.hw_module = getModuleHandle();
92     dstConfig->ext.mix.handle = mIoHandle;
93     dstConfig->ext.mix.usecase.source = source();
94 }
95 
toAudioPort(struct audio_port_v7 * port) const96 void AudioInputDescriptor::toAudioPort(struct audio_port_v7 *port) const
97 {
98     ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle);
99 
100     mProfile->toAudioPort(port);
101     port->id = mId;
102     toAudioPortConfig(&port->active_config);
103     port->ext.mix.hw_module = getModuleHandle();
104     port->ext.mix.handle = mIoHandle;
105     port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
106 }
107 
setPreemptedSessions(const SortedVector<audio_session_t> & sessions)108 void AudioInputDescriptor::setPreemptedSessions(const SortedVector<audio_session_t>& sessions)
109 {
110     mPreemptedSessions = sessions;
111 }
112 
getPreemptedSessions() const113 SortedVector<audio_session_t> AudioInputDescriptor::getPreemptedSessions() const
114 {
115     return mPreemptedSessions;
116 }
117 
hasPreemptedSession(audio_session_t session) const118 bool AudioInputDescriptor::hasPreemptedSession(audio_session_t session) const
119 {
120     return (mPreemptedSessions.indexOf(session) >= 0);
121 }
122 
clearPreemptedSessions()123 void AudioInputDescriptor::clearPreemptedSessions()
124 {
125     mPreemptedSessions.clear();
126 }
127 
isSourceActive(audio_source_t source) const128 bool AudioInputDescriptor::isSourceActive(audio_source_t source) const
129 {
130     for (const auto &client : getClientIterable()) {
131         if (client->active() &&
132             ((client->source() == source) ||
133                 ((source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
134                     (client->source() == AUDIO_SOURCE_HOTWORD) &&
135                     client->isSoundTrigger()))) {
136             return true;
137         }
138     }
139     return false;
140 }
141 
getHighestPriorityAttributes() const142 audio_attributes_t AudioInputDescriptor::getHighestPriorityAttributes() const
143 {
144     audio_attributes_t attributes = { .source = AUDIO_SOURCE_DEFAULT };
145     sp<RecordClientDescriptor> topClient = getHighestPriorityClient();
146     return topClient ? topClient->attributes() : attributes;
147 }
148 
getHighestPriorityClient() const149 sp<RecordClientDescriptor> AudioInputDescriptor::getHighestPriorityClient() const
150 {
151     sp<RecordClientDescriptor> topClient;
152 
153     for (bool activeOnly : { true, false }) {
154         int32_t topPriority = -1;
155         app_state_t topState = APP_STATE_IDLE;
156         for (const auto &client : getClientIterable()) {
157             if (activeOnly && !client->active()) {
158               continue;
159             }
160             app_state_t curState = client->appState();
161             if (curState >= topState) {
162                 int32_t curPriority = source_priority(client->source());
163                 if (curPriority >= topPriority) {
164                     topClient = client;
165                     topPriority = curPriority;
166                 }
167                 topState = curState;
168             }
169         }
170         if (topClient != nullptr) {
171             break;
172         }
173     }
174     return topClient;
175 }
176 
isSoundTrigger() const177 bool AudioInputDescriptor::isSoundTrigger() const {
178     // sound trigger and non sound trigger clients are not mixed on a given input
179     // so check only first client
180     if (getClientCount() == 0) {
181         return false;
182     }
183     return getClientIterable().begin()->isSoundTrigger();
184 }
185 
getPatchHandle() const186 audio_patch_handle_t AudioInputDescriptor::getPatchHandle() const
187 {
188     return mPatchHandle;
189 }
190 
setPatchHandle(audio_patch_handle_t handle)191 void AudioInputDescriptor::setPatchHandle(audio_patch_handle_t handle)
192 {
193     mPatchHandle = handle;
194     for (const auto &client : getClientIterable()) {
195         if (client->active()) {
196             updateClientRecordingConfiguration(
197                     client->isLowLevel() ? RECORD_CONFIG_EVENT_START : RECORD_CONFIG_EVENT_UPDATE,
198                     client);
199         }
200     }
201 }
202 
getConfig() const203 audio_config_base_t AudioInputDescriptor::getConfig() const
204 {
205     const audio_config_base_t config = { .sample_rate = mSamplingRate, .channel_mask = mChannelMask,
206             .format = mFormat };
207     return config;
208 }
209 
open(const audio_config_t * config,const sp<DeviceDescriptor> & device,audio_source_t source,audio_input_flags_t flags,audio_io_handle_t * input)210 status_t AudioInputDescriptor::open(const audio_config_t *config,
211                                        const sp<DeviceDescriptor> &device,
212                                        audio_source_t source,
213                                        audio_input_flags_t flags,
214                                        audio_io_handle_t *input)
215 {
216     audio_config_t lConfig;
217     if (config == nullptr) {
218         lConfig = AUDIO_CONFIG_INITIALIZER;
219         lConfig.sample_rate = mSamplingRate;
220         lConfig.channel_mask = mChannelMask;
221         lConfig.format = mFormat;
222     } else {
223         lConfig = *config;
224     }
225 
226     mDevice = device;
227 
228     ALOGV("opening input for device %s profile %p name %s",
229           mDevice->toString().c_str(), mProfile.get(), mProfile->getName().c_str());
230 
231     audio_devices_t deviceType = mDevice->type();
232 
233     status_t status = mClientInterface->openInput(mProfile->getModuleHandle(),
234                                                   input,
235                                                   &lConfig,
236                                                   &deviceType,
237                                                   String8(mDevice->address().c_str()),
238                                                   source,
239                                                   static_cast<audio_input_flags_t>(
240                                                           flags & mProfile->getFlags()));
241     LOG_ALWAYS_FATAL_IF(mDevice->type() != deviceType,
242                         "%s openInput returned device %08x when given device %08x",
243                         __FUNCTION__, mDevice->type(), deviceType);
244 
245     if (status == NO_ERROR) {
246         LOG_ALWAYS_FATAL_IF(*input == AUDIO_IO_HANDLE_NONE,
247                             "%s openInput returned input handle %d for device %s",
248                             __FUNCTION__, *input, mDevice->toString().c_str());
249         mSamplingRate = lConfig.sample_rate;
250         mChannelMask = lConfig.channel_mask;
251         mFormat = lConfig.format;
252         mId = PolicyAudioPort::getNextUniqueId();
253         mIoHandle = *input;
254         mProfile->curOpenCount++;
255     }
256 
257     return status;
258 }
259 
start()260 status_t AudioInputDescriptor::start()
261 {
262     if (!isActive()) {
263         if (!mProfile->canStartNewIo()) {
264             ALOGI("%s mProfile->curActiveCount %d", __func__, mProfile->curActiveCount);
265             return INVALID_OPERATION;
266         }
267         mProfile->curActiveCount++;
268     }
269     return NO_ERROR;
270 }
271 
stop()272 void AudioInputDescriptor::stop()
273 {
274     if (!isActive()) {
275         LOG_ALWAYS_FATAL_IF(mProfile->curActiveCount < 1,
276                             "%s invalid profile active count %u",
277                             __func__, mProfile->curActiveCount);
278         mProfile->curActiveCount--;
279         // allow preemption again now that at least one client was able to
280         // capture on this input
281         mIsPreemptor = false;
282     }
283 }
284 
close()285 void AudioInputDescriptor::close()
286 {
287     if (mIoHandle != AUDIO_IO_HANDLE_NONE) {
288         // clean up active clients if any (can happen if close() is called to force
289         // clients to reconnect
290         for (const auto &client : getClientIterable()) {
291             if (client->active()) {
292                 ALOGW("%s client with port ID %d still active on input %d",
293                     __func__, client->portId(), mId);
294                 setClientActive(client, false);
295                 stop();
296             }
297         }
298 
299         mClientInterface->closeInput(mIoHandle);
300         LOG_ALWAYS_FATAL_IF(mProfile->curOpenCount < 1, "%s profile open count %u",
301                             __FUNCTION__, mProfile->curOpenCount);
302 
303         mProfile->curOpenCount--;
304         LOG_ALWAYS_FATAL_IF(mProfile->curOpenCount <  mProfile->curActiveCount,
305                 "%s(%d): mProfile->curOpenCount %d < mProfile->curActiveCount %d.",
306                 __func__, mId, mProfile->curOpenCount, mProfile->curActiveCount);
307         mIoHandle = AUDIO_IO_HANDLE_NONE;
308     }
309 }
310 
addClient(const sp<RecordClientDescriptor> & client)311 void AudioInputDescriptor::addClient(const sp<RecordClientDescriptor> &client) {
312     ClientMapHandler<RecordClientDescriptor>::addClient(client);
313 
314     for (size_t i = 0; i < mEnabledEffects.size(); i++) {
315         if (mEnabledEffects.valueAt(i)->mSession == client->session()) {
316             client->trackEffectEnabled(mEnabledEffects.valueAt(i), true);
317         }
318     }
319 }
320 
setClientActive(const sp<RecordClientDescriptor> & client,bool active)321 void AudioInputDescriptor::setClientActive(const sp<RecordClientDescriptor>& client, bool active)
322 {
323     LOG_ALWAYS_FATAL_IF(getClient(client->portId()) == nullptr,
324         "%s(%d) does not exist on input descriptor", __func__, client->portId());
325     if (active == client->active()) {
326         return;
327     }
328 
329     // Handle non-client-specific activity ref count
330     int32_t oldGlobalActiveCount = mGlobalActiveCount;
331     if (!active && mGlobalActiveCount < 1) {
332         LOG_ALWAYS_FATAL("%s(%d) invalid deactivation with globalActiveCount %d",
333                __func__, client->portId(), mGlobalActiveCount);
334         // mGlobalActiveCount = 1;
335     }
336     const int delta = active ? 1 : -1;
337     mGlobalActiveCount += delta;
338 
339     sp<AudioPolicyMix> policyMix = mPolicyMix.promote();
340     if ((oldGlobalActiveCount == 0) && (mGlobalActiveCount > 0)) {
341         if ((policyMix != NULL) && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0))
342         {
343             mClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress,
344                                                             MIX_STATE_MIXING);
345         }
346     } else if ((oldGlobalActiveCount > 0) && (mGlobalActiveCount == 0)) {
347         if ((policyMix != NULL) && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0))
348         {
349             mClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress,
350                                                             MIX_STATE_IDLE);
351         }
352     }
353 
354     client->setActive(active);
355 
356     checkSuspendEffects();
357 
358     int event = active ? RECORD_CONFIG_EVENT_START : RECORD_CONFIG_EVENT_STOP;
359     updateClientRecordingConfiguration(event, client);
360 }
361 
updateClientRecordingConfiguration(int event,const sp<RecordClientDescriptor> & client)362 void AudioInputDescriptor::updateClientRecordingConfiguration(
363     int event, const sp<RecordClientDescriptor>& client)
364 {
365     ALOGV("%s riid %d uid %d port %d session %d event %d",
366             __func__, client->riid(), client->uid(), client->portId(), client->session(), event);
367     // do not send callback if starting and no device is selected yet to avoid
368     // double callbacks from startInput() before and after the device is selected
369     // "start" and "stop" events for "high level" clients (AudioRecord) are sent by the client side
370     if ((event == RECORD_CONFIG_EVENT_START && mPatchHandle == AUDIO_PATCH_HANDLE_NONE)
371             || (!client->isLowLevel()
372                     && (event == RECORD_CONFIG_EVENT_START || event == RECORD_CONFIG_EVENT_STOP))) {
373         return;
374     }
375 
376     const audio_config_base_t sessionConfig = client->config();
377     const record_client_info_t recordClientInfo{client->riid(), client->uid(), client->session(),
378                                                 client->source(), client->portId(),
379                                                 client->isSilenced()};
380     const audio_config_base_t config = getConfig();
381 
382     std::vector<effect_descriptor_t> clientEffects;
383     EffectDescriptorCollection effectsList = client->getEnabledEffects();
384     for (size_t i = 0; i < effectsList.size(); i++) {
385         clientEffects.push_back(effectsList.valueAt(i)->mDesc);
386     }
387 
388     std::vector<effect_descriptor_t> effects;
389     effectsList = getEnabledEffects();
390     for (size_t i = 0; i < effectsList.size(); i++) {
391         effects.push_back(effectsList.valueAt(i)->mDesc);
392     }
393 
394     mClientInterface->onRecordingConfigurationUpdate(event, &recordClientInfo, &sessionConfig,
395                                                      clientEffects, &config, effects,
396                                                      mPatchHandle, source());
397 }
398 
getClientsForSession(audio_session_t session)399 RecordClientVector AudioInputDescriptor::getClientsForSession(
400     audio_session_t session)
401 {
402     RecordClientVector clients;
403     for (const auto &client : getClientIterable()) {
404         if (client->session() == session) {
405             clients.push_back(client);
406         }
407     }
408     return clients;
409 }
410 
clientsList(bool activeOnly,audio_source_t source,bool preferredDeviceOnly) const411 RecordClientVector AudioInputDescriptor::clientsList(bool activeOnly, audio_source_t source,
412                                                      bool preferredDeviceOnly) const
413 {
414     RecordClientVector clients;
415     for (const auto &client : getClientIterable()) {
416         if ((!activeOnly || client->active())
417             && (source == AUDIO_SOURCE_DEFAULT || source == client->source())
418             && (!preferredDeviceOnly || client->hasPreferredDevice())) {
419             clients.push_back(client);
420         }
421     }
422     return clients;
423 }
424 
trackEffectEnabled(const sp<EffectDescriptor> & effect,bool enabled)425 void AudioInputDescriptor::trackEffectEnabled(const sp<EffectDescriptor> &effect,
426                                               bool enabled)
427 {
428     if (enabled) {
429         mEnabledEffects.replaceValueFor(effect->mId, effect);
430     } else {
431         mEnabledEffects.removeItem(effect->mId);
432         // always exit from suspend when disabling an effect as only enabled effects
433         // are managed by checkSuspendEffects()
434         if (effect->mSuspended) {
435             effect->mSuspended = false;
436             mClientInterface->setEffectSuspended(effect->mId, effect->mSession, effect->mSuspended);
437         }
438     }
439 
440     RecordClientVector clients = getClientsForSession((audio_session_t)effect->mSession);
441     RecordClientVector updatedClients;
442 
443     for (const auto& client : clients) {
444         sp<EffectDescriptor> clientEffect = client->getEnabledEffects().getEffect(effect->mId);
445         bool changed = (enabled && clientEffect == nullptr)
446                 || (!enabled && clientEffect != nullptr);
447         client->trackEffectEnabled(effect, enabled);
448         if (changed && client->active()) {
449             updatedClients.push_back(client);
450         }
451     }
452 
453     checkSuspendEffects();
454 
455     for (const auto& client : updatedClients) {
456         updateClientRecordingConfiguration(RECORD_CONFIG_EVENT_UPDATE, client);
457     }
458 }
459 
getEnabledEffects() const460 EffectDescriptorCollection AudioInputDescriptor::getEnabledEffects() const
461 {
462     // report effects for highest priority active source as applied to all clients
463     EffectDescriptorCollection enabledEffects;
464     sp<RecordClientDescriptor> topClient = getHighestPriorityClient();
465     if (topClient != nullptr) {
466         enabledEffects = topClient->getEnabledEffects();
467     }
468     return enabledEffects;
469 }
470 
setAppState(audio_port_handle_t portId,app_state_t state)471 void AudioInputDescriptor::setAppState(audio_port_handle_t portId, app_state_t state)
472 {
473     RecordClientVector clients = clientsList(false /*activeOnly*/);
474     RecordClientVector updatedClients;
475 
476     for (const auto& client : clients) {
477         if (portId == client->portId()) {
478             bool wasSilenced = client->isSilenced();
479             client->setAppState(state);
480             if (client->active() && wasSilenced != client->isSilenced()) {
481                 updatedClients.push_back(client);
482             }
483         }
484     }
485 
486     checkSuspendEffects();
487 
488     for (const auto& client : updatedClients) {
489         updateClientRecordingConfiguration(RECORD_CONFIG_EVENT_UPDATE, client);
490     }
491 }
492 
checkSuspendEffects()493 void AudioInputDescriptor::checkSuspendEffects()
494 {
495     sp<RecordClientDescriptor> topClient = getHighestPriorityClient();
496     if (topClient == nullptr) {
497         return;
498     }
499 
500     for (size_t i = 0; i < mEnabledEffects.size(); i++) {
501         sp<EffectDescriptor> effect = mEnabledEffects.valueAt(i);
502         if (effect->mSession == topClient->session()) {
503             if (effect->mSuspended) {
504                 effect->mSuspended = false;
505                 mClientInterface->setEffectSuspended(effect->mId,
506                                                      effect->mSession,
507                                                      effect->mSuspended);
508             }
509         } else if (!effect->mSuspended) {
510             effect->mSuspended = true;
511             mClientInterface->setEffectSuspended(effect->mId,
512                                                  effect->mSession,
513                                                  effect->mSuspended);
514         }
515     }
516 }
517 
dump(String8 * dst,int spaces,const char * extraInfo) const518 void AudioInputDescriptor::dump(String8 *dst, int spaces, const char* extraInfo) const
519 {
520     std::string flagsLiteral = toString(mFlags);
521     if (!flagsLiteral.empty()) {
522         flagsLiteral = " (" + flagsLiteral + ")";
523     }
524     dst->appendFormat("Port ID: %d; 0x%04x%s%s%s\n",
525             getId(), mFlags, flagsLiteral.c_str(),
526             extraInfo != nullptr ? "; " : "", extraInfo != nullptr ? extraInfo : "");
527     dst->appendFormat("%*s%s; %d; Channel mask: 0x%x\n", spaces, "",
528             audio_format_to_string(mFormat), mSamplingRate, mChannelMask);
529     dst->appendFormat("%*sDevices: %s\n", spaces, "",
530             mDevice->toString(true /*includeSensitiveInfo*/).c_str());
531     mEnabledEffects.dump(dst, spaces /*spaces*/, false /*verbose*/);
532     if (getClientCount() != 0) {
533         dst->appendFormat("%*sAudioRecord Clients (%zu):\n", spaces, "", getClientCount());
534         ClientMapHandler<RecordClientDescriptor>::dump(dst, spaces);
535         dst->append("\n");
536     }
537 }
538 
isSourceActive(audio_source_t source) const539 bool AudioInputCollection::isSourceActive(audio_source_t source) const
540 {
541     for (size_t i = 0; i < size(); i++) {
542         const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
543         if (inputDescriptor->isSourceActive(source)) {
544             return true;
545         }
546     }
547     return false;
548 }
549 
getInputFromId(audio_port_handle_t id) const550 sp<AudioInputDescriptor> AudioInputCollection::getInputFromId(audio_port_handle_t id) const
551 {
552     for (size_t i = 0; i < size(); i++) {
553         const sp<AudioInputDescriptor> inputDescriptor = valueAt(i);
554         if (inputDescriptor->getId() == id) {
555             return inputDescriptor;
556         }
557     }
558     return NULL;
559 }
560 
activeInputsCountOnDevices(const DeviceVector & devices) const561 uint32_t AudioInputCollection::activeInputsCountOnDevices(const DeviceVector &devices) const
562 {
563     uint32_t count = 0;
564     for (size_t i = 0; i < size(); i++) {
565         const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
566         if (inputDescriptor->isActive() &&
567                 (devices.isEmpty() || devices.contains(inputDescriptor->getDevice()))) {
568             count++;
569         }
570     }
571     return count;
572 }
573 
getActiveInputs()574 Vector<sp <AudioInputDescriptor> > AudioInputCollection::getActiveInputs()
575 {
576     Vector<sp <AudioInputDescriptor> > activeInputs;
577 
578     for (size_t i = 0; i < size(); i++) {
579         const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
580         if (inputDescriptor->isActive()) {
581             activeInputs.add(inputDescriptor);
582         }
583     }
584     return activeInputs;
585 }
586 
getInputForClient(audio_port_handle_t portId)587 sp<AudioInputDescriptor> AudioInputCollection::getInputForClient(audio_port_handle_t portId)
588 {
589     for (size_t i = 0; i < size(); i++) {
590         sp<AudioInputDescriptor> inputDesc = valueAt(i);
591         if (inputDesc->getClient(portId) != nullptr) {
592             return inputDesc;
593         }
594     }
595     return 0;
596 }
597 
trackEffectEnabled(const sp<EffectDescriptor> & effect,bool enabled)598 void AudioInputCollection::trackEffectEnabled(const sp<EffectDescriptor> &effect,
599                                             bool enabled)
600 {
601     for (size_t i = 0; i < size(); i++) {
602         sp<AudioInputDescriptor> inputDesc = valueAt(i);
603         if (inputDesc->mIoHandle == effect->mIo) {
604             return inputDesc->trackEffectEnabled(effect, enabled);
605         }
606     }
607 }
608 
clearSessionRoutesForDevice(const sp<DeviceDescriptor> & disconnectedDevice)609 void AudioInputCollection::clearSessionRoutesForDevice(
610     const sp<DeviceDescriptor> &disconnectedDevice)
611 {
612     for (size_t i = 0; i < size(); i++) {
613         sp<AudioInputDescriptor> inputDesc = valueAt(i);
614         for (const auto& client : inputDesc->getClientIterable()) {
615             if (client->preferredDeviceId() == disconnectedDevice->getId()) {
616                 client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE);
617             }
618         }
619     }
620 }
621 
dump(String8 * dst) const622 void AudioInputCollection::dump(String8 *dst) const
623 {
624     dst->appendFormat("\n Inputs (%zu):\n", size());
625     for (size_t i = 0; i < size(); i++) {
626         const std::string prefix = base::StringPrintf("  %zu. ", i + 1);
627         const std::string extraInfo = base::StringPrintf("I/O handle: %d", keyAt(i));
628         dst->appendFormat("%s", prefix.c_str());
629         valueAt(i)->dump(dst, prefix.size(), extraInfo.c_str());
630     }
631 }
632 
633 }; //namespace android
634