blob: 47fcd279afc9774327aaecae19f94ceaa639020e [file] [log] [blame]
Mikhail Naganovac9d4e72023-10-23 12:00:09 -07001/*
2 * Copyright (C) 2022 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 "Hal2AidlMapper"
18// #define LOG_NDEBUG 0
19
20#include <algorithm>
21
22#include <media/audiohal/StreamHalInterface.h>
23#include <error/expected_utils.h>
24#include <system/audio.h> // For AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS
25#include <Utils.h>
26#include <utils/Log.h>
27
28#include "Hal2AidlMapper.h"
29
30using aidl::android::aidl_utils::statusTFromBinderStatus;
31using aidl::android::media::audio::common::AudioChannelLayout;
32using aidl::android::media::audio::common::AudioConfig;
33using aidl::android::media::audio::common::AudioDevice;
34using aidl::android::media::audio::common::AudioDeviceAddress;
35using aidl::android::media::audio::common::AudioDeviceDescription;
36using aidl::android::media::audio::common::AudioDeviceType;
37using aidl::android::media::audio::common::AudioFormatDescription;
38using aidl::android::media::audio::common::AudioInputFlags;
39using aidl::android::media::audio::common::AudioIoFlags;
40using aidl::android::media::audio::common::AudioOutputFlags;
41using aidl::android::media::audio::common::AudioPort;
42using aidl::android::media::audio::common::AudioPortConfig;
43using aidl::android::media::audio::common::AudioPortDeviceExt;
44using aidl::android::media::audio::common::AudioPortExt;
45using aidl::android::media::audio::common::AudioPortMixExt;
46using aidl::android::media::audio::common::AudioPortMixExtUseCase;
47using aidl::android::media::audio::common::AudioProfile;
48using aidl::android::media::audio::common::AudioSource;
49using aidl::android::media::audio::common::Int;
50using aidl::android::hardware::audio::common::isBitPositionFlagSet;
51using aidl::android::hardware::audio::common::isDefaultAudioFormat;
52using aidl::android::hardware::audio::common::makeBitPositionFlagMask;
53using aidl::android::hardware::audio::core::AudioPatch;
54using aidl::android::hardware::audio::core::AudioRoute;
55using aidl::android::hardware::audio::core::IModule;
56
57namespace android {
58
59namespace {
60
61bool isConfigEqualToPortConfig(const AudioConfig& config, const AudioPortConfig& portConfig) {
62 return portConfig.sampleRate.value().value == config.base.sampleRate &&
63 portConfig.channelMask.value() == config.base.channelMask &&
64 portConfig.format.value() == config.base.format;
65}
66
67void setConfigFromPortConfig(AudioConfig* config, const AudioPortConfig& portConfig) {
68 config->base.sampleRate = portConfig.sampleRate.value().value;
69 config->base.channelMask = portConfig.channelMask.value();
70 config->base.format = portConfig.format.value();
71}
72
73void setPortConfigFromConfig(AudioPortConfig* portConfig, const AudioConfig& config) {
74 if (config.base.sampleRate != 0) {
75 portConfig->sampleRate = Int{ .value = config.base.sampleRate };
76 }
77 if (config.base.channelMask != AudioChannelLayout{}) {
78 portConfig->channelMask = config.base.channelMask;
79 }
80 if (config.base.format != AudioFormatDescription{}) {
81 portConfig->format = config.base.format;
82 }
83}
84
jiabin80797002023-12-01 22:02:41 +000085bool containHapticChannel(AudioChannelLayout channel) {
86 return channel.getTag() == AudioChannelLayout::Tag::layoutMask &&
87 ((channel.get<AudioChannelLayout::Tag::layoutMask>()
88 & AudioChannelLayout::CHANNEL_HAPTIC_A)
89 == AudioChannelLayout::CHANNEL_HAPTIC_A ||
90 (channel.get<AudioChannelLayout::Tag::layoutMask>()
91 & AudioChannelLayout::CHANNEL_HAPTIC_B)
92 == AudioChannelLayout::CHANNEL_HAPTIC_B);
93}
94
Mikhail Naganovac9d4e72023-10-23 12:00:09 -070095} // namespace
96
97Hal2AidlMapper::Hal2AidlMapper(const std::string& instance, const std::shared_ptr<IModule>& module)
98 : mInstance(instance), mModule(module) {
99}
100
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800101void Hal2AidlMapper::addStream(
102 const sp<StreamHalInterface>& stream, int32_t portConfigId, int32_t patchId) {
103 mStreams.insert(std::pair(stream, std::pair(portConfigId, patchId)));
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700104}
105
106bool Hal2AidlMapper::audioDeviceMatches(const AudioDevice& device, const AudioPort& p) {
107 if (p.ext.getTag() != AudioPortExt::Tag::device) return false;
108 return p.ext.get<AudioPortExt::Tag::device>().device == device;
109}
110
111bool Hal2AidlMapper::audioDeviceMatches(const AudioDevice& device, const AudioPortConfig& p) {
112 if (p.ext.getTag() != AudioPortExt::Tag::device) return false;
113 if (device.type.type == AudioDeviceType::IN_DEFAULT) {
114 return p.portId == mDefaultInputPortId;
115 } else if (device.type.type == AudioDeviceType::OUT_DEFAULT) {
116 return p.portId == mDefaultOutputPortId;
117 }
118 return p.ext.get<AudioPortExt::Tag::device>().device == device;
119}
120
121status_t Hal2AidlMapper::createOrUpdatePatch(
122 const std::vector<AudioPortConfig>& sources,
123 const std::vector<AudioPortConfig>& sinks,
124 int32_t* patchId, Cleanups* cleanups) {
125 auto existingPatchIt = *patchId != 0 ? mPatches.find(*patchId): mPatches.end();
126 AudioPatch patch;
127 if (existingPatchIt != mPatches.end()) {
128 patch = existingPatchIt->second;
129 patch.sourcePortConfigIds.clear();
130 patch.sinkPortConfigIds.clear();
131 }
132 // The IDs will be found by 'fillPortConfigs', however the original 'sources' and
133 // 'sinks' will not be updated because 'setAudioPatch' only needs IDs. Here we log
134 // the source arguments, where only the audio configuration and device specifications
135 // are relevant.
136 ALOGD("%s: [disregard IDs] sources: %s, sinks: %s",
137 __func__, ::android::internal::ToString(sources).c_str(),
138 ::android::internal::ToString(sinks).c_str());
139 auto fillPortConfigs = [&](
140 const std::vector<AudioPortConfig>& configs,
141 const std::set<int32_t>& destinationPortIds,
142 std::vector<int32_t>* ids, std::set<int32_t>* portIds) -> status_t {
143 for (const auto& s : configs) {
144 AudioPortConfig portConfig;
145 RETURN_STATUS_IF_ERROR(findOrCreatePortConfig(
146 s, destinationPortIds, &portConfig, cleanups));
147 ids->push_back(portConfig.id);
148 if (portIds != nullptr) {
149 portIds->insert(portConfig.portId);
150 }
151 }
152 return OK;
153 };
154 // When looking up port configs, the destinationPortId is only used for mix ports.
155 // Thus, we process device port configs first, and look up the destination port ID from them.
156 bool sourceIsDevice = std::any_of(sources.begin(), sources.end(),
157 [](const auto& config) { return config.ext.getTag() == AudioPortExt::device; });
158 const std::vector<AudioPortConfig>& devicePortConfigs =
159 sourceIsDevice ? sources : sinks;
160 std::vector<int32_t>* devicePortConfigIds =
161 sourceIsDevice ? &patch.sourcePortConfigIds : &patch.sinkPortConfigIds;
162 const std::vector<AudioPortConfig>& mixPortConfigs =
163 sourceIsDevice ? sinks : sources;
164 std::vector<int32_t>* mixPortConfigIds =
165 sourceIsDevice ? &patch.sinkPortConfigIds : &patch.sourcePortConfigIds;
166 std::set<int32_t> devicePortIds;
167 RETURN_STATUS_IF_ERROR(fillPortConfigs(
168 devicePortConfigs, std::set<int32_t>(), devicePortConfigIds, &devicePortIds));
169 RETURN_STATUS_IF_ERROR(fillPortConfigs(
170 mixPortConfigs, devicePortIds, mixPortConfigIds, nullptr));
171 if (existingPatchIt != mPatches.end()) {
172 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
173 mModule->setAudioPatch(patch, &patch)));
174 existingPatchIt->second = patch;
175 } else {
176 bool created = false;
177 RETURN_STATUS_IF_ERROR(findOrCreatePatch(patch, &patch, &created));
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800178 // No cleanup of the patch is needed, it is managed by the framework.
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700179 *patchId = patch.id;
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800180 if (!created) {
181 // The framework might have "created" a patch which already existed due to
182 // stream creation. Need to release the ownership from the stream.
183 for (auto& s : mStreams) {
184 if (s.second.second == patch.id) s.second.second = -1;
185 }
186 }
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700187 }
188 return OK;
189}
190
191status_t Hal2AidlMapper::createOrUpdatePortConfig(
192 const AudioPortConfig& requestedPortConfig, PortConfigs::iterator* result, bool* created) {
193 AudioPortConfig appliedPortConfig;
194 bool applied = false;
195 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->setAudioPortConfig(
196 requestedPortConfig, &appliedPortConfig, &applied)));
197 if (!applied) {
198 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->setAudioPortConfig(
199 appliedPortConfig, &appliedPortConfig, &applied)));
200 if (!applied) {
201 ALOGE("%s: module %s did not apply suggested config %s",
202 __func__, mInstance.c_str(), appliedPortConfig.toString().c_str());
203 return NO_INIT;
204 }
205 }
206
207 int32_t id = appliedPortConfig.id;
208 if (requestedPortConfig.id != 0 && requestedPortConfig.id != id) {
209 LOG_ALWAYS_FATAL("%s: requested port config id %d changed to %d", __func__,
210 requestedPortConfig.id, id);
211 }
212
213 auto [it, inserted] = mPortConfigs.insert_or_assign(std::move(id),
214 std::move(appliedPortConfig));
215 *result = it;
216 *created = inserted;
217 return OK;
218}
219
220void Hal2AidlMapper::eraseConnectedPort(int32_t portId) {
221 mPorts.erase(portId);
222 mConnectedPorts.erase(portId);
223 if (mDisconnectedPortReplacement.first == portId) {
224 const auto& port = mDisconnectedPortReplacement.second;
225 mPorts.insert(std::make_pair(port.id, port));
226 ALOGD("%s: disconnected port replacement: %s", __func__, port.toString().c_str());
227 mDisconnectedPortReplacement = std::pair<int32_t, AudioPort>();
228 }
229}
230
231status_t Hal2AidlMapper::findOrCreatePatch(
232 const AudioPatch& requestedPatch, AudioPatch* patch, bool* created) {
233 std::set<int32_t> sourcePortConfigIds(requestedPatch.sourcePortConfigIds.begin(),
234 requestedPatch.sourcePortConfigIds.end());
235 std::set<int32_t> sinkPortConfigIds(requestedPatch.sinkPortConfigIds.begin(),
236 requestedPatch.sinkPortConfigIds.end());
237 return findOrCreatePatch(sourcePortConfigIds, sinkPortConfigIds, patch, created);
238}
239
240status_t Hal2AidlMapper::findOrCreatePatch(
241 const std::set<int32_t>& sourcePortConfigIds, const std::set<int32_t>& sinkPortConfigIds,
242 AudioPatch* patch, bool* created) {
243 auto patchIt = findPatch(sourcePortConfigIds, sinkPortConfigIds);
244 if (patchIt == mPatches.end()) {
245 AudioPatch requestedPatch, appliedPatch;
246 requestedPatch.sourcePortConfigIds.insert(requestedPatch.sourcePortConfigIds.end(),
247 sourcePortConfigIds.begin(), sourcePortConfigIds.end());
248 requestedPatch.sinkPortConfigIds.insert(requestedPatch.sinkPortConfigIds.end(),
249 sinkPortConfigIds.begin(), sinkPortConfigIds.end());
250 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->setAudioPatch(
251 requestedPatch, &appliedPatch)));
252 patchIt = mPatches.insert(mPatches.end(), std::make_pair(appliedPatch.id, appliedPatch));
253 *created = true;
254 } else {
255 *created = false;
256 }
257 *patch = patchIt->second;
258 return OK;
259}
260
261status_t Hal2AidlMapper::findOrCreatePortConfig(
262 const AudioDevice& device, const AudioConfig* config, AudioPortConfig* portConfig,
263 bool* created) {
264 auto portConfigIt = findPortConfig(device);
265 if (portConfigIt == mPortConfigs.end()) {
266 auto portsIt = findPort(device);
267 if (portsIt == mPorts.end()) {
268 ALOGE("%s: device port for device %s is not found in the module %s",
269 __func__, device.toString().c_str(), mInstance.c_str());
270 return BAD_VALUE;
271 }
272 AudioPortConfig requestedPortConfig;
273 requestedPortConfig.portId = portsIt->first;
274 if (config != nullptr) {
275 setPortConfigFromConfig(&requestedPortConfig, *config);
276 }
277 RETURN_STATUS_IF_ERROR(createOrUpdatePortConfig(requestedPortConfig, &portConfigIt,
278 created));
279 } else {
280 *created = false;
281 }
282 *portConfig = portConfigIt->second;
283 return OK;
284}
285
286status_t Hal2AidlMapper::findOrCreatePortConfig(
287 const AudioConfig& config, const std::optional<AudioIoFlags>& flags, int32_t ioHandle,
288 AudioSource source, const std::set<int32_t>& destinationPortIds,
289 AudioPortConfig* portConfig, bool* created) {
290 // These flags get removed one by one in this order when retrying port finding.
291 static const std::vector<AudioInputFlags> kOptionalInputFlags{
292 AudioInputFlags::FAST, AudioInputFlags::RAW, AudioInputFlags::VOIP_TX };
293 auto portConfigIt = findPortConfig(config, flags, ioHandle);
294 if (portConfigIt == mPortConfigs.end() && flags.has_value()) {
295 auto optionalInputFlagsIt = kOptionalInputFlags.begin();
296 AudioIoFlags matchFlags = flags.value();
297 auto portsIt = findPort(config, matchFlags, destinationPortIds);
298 while (portsIt == mPorts.end() && matchFlags.getTag() == AudioIoFlags::Tag::input
299 && optionalInputFlagsIt != kOptionalInputFlags.end()) {
300 if (!isBitPositionFlagSet(
301 matchFlags.get<AudioIoFlags::Tag::input>(), *optionalInputFlagsIt)) {
302 ++optionalInputFlagsIt;
303 continue;
304 }
305 matchFlags.set<AudioIoFlags::Tag::input>(matchFlags.get<AudioIoFlags::Tag::input>() &
306 ~makeBitPositionFlagMask(*optionalInputFlagsIt++));
307 portsIt = findPort(config, matchFlags, destinationPortIds);
308 ALOGI("%s: mix port for config %s, flags %s was not found in the module %s, "
309 "retried with flags %s", __func__, config.toString().c_str(),
310 flags.value().toString().c_str(), mInstance.c_str(),
311 matchFlags.toString().c_str());
312 }
313 if (portsIt == mPorts.end()) {
314 ALOGE("%s: mix port for config %s, flags %s is not found in the module %s",
315 __func__, config.toString().c_str(), matchFlags.toString().c_str(),
316 mInstance.c_str());
317 return BAD_VALUE;
318 }
319 AudioPortConfig requestedPortConfig;
320 requestedPortConfig.portId = portsIt->first;
321 setPortConfigFromConfig(&requestedPortConfig, config);
322 requestedPortConfig.ext = AudioPortMixExt{ .handle = ioHandle };
323 if (matchFlags.getTag() == AudioIoFlags::Tag::input
324 && source != AudioSource::SYS_RESERVED_INVALID) {
325 requestedPortConfig.ext.get<AudioPortExt::Tag::mix>().usecase =
326 AudioPortMixExtUseCase::make<AudioPortMixExtUseCase::Tag::source>(source);
327 }
328 RETURN_STATUS_IF_ERROR(createOrUpdatePortConfig(requestedPortConfig, &portConfigIt,
329 created));
330 } else if (portConfigIt == mPortConfigs.end() && !flags.has_value()) {
331 ALOGW("%s: mix port config for %s, handle %d not found in the module %s, "
332 "and was not created as flags are not specified",
333 __func__, config.toString().c_str(), ioHandle, mInstance.c_str());
334 return BAD_VALUE;
335 } else {
336 AudioPortConfig requestedPortConfig = portConfigIt->second;
337 if (requestedPortConfig.ext.getTag() == AudioPortExt::Tag::mix) {
338 AudioPortMixExt& mixExt = requestedPortConfig.ext.get<AudioPortExt::Tag::mix>();
339 if (mixExt.usecase.getTag() == AudioPortMixExtUseCase::Tag::source &&
340 source != AudioSource::SYS_RESERVED_INVALID) {
341 mixExt.usecase.get<AudioPortMixExtUseCase::Tag::source>() = source;
342 }
343 }
344
345 if (requestedPortConfig != portConfigIt->second) {
346 RETURN_STATUS_IF_ERROR(createOrUpdatePortConfig(requestedPortConfig, &portConfigIt,
347 created));
348 } else {
349 *created = false;
350 }
351 }
352 *portConfig = portConfigIt->second;
353 return OK;
354}
355
356status_t Hal2AidlMapper::findOrCreatePortConfig(
357 const AudioPortConfig& requestedPortConfig, const std::set<int32_t>& destinationPortIds,
358 AudioPortConfig* portConfig, bool* created) {
359 using Tag = AudioPortExt::Tag;
360 if (requestedPortConfig.ext.getTag() == Tag::mix) {
361 if (const auto& p = requestedPortConfig;
362 !p.sampleRate.has_value() || !p.channelMask.has_value() ||
363 !p.format.has_value()) {
364 ALOGW("%s: provided mix port config is not fully specified: %s",
365 __func__, p.toString().c_str());
366 return BAD_VALUE;
367 }
368 AudioConfig config;
369 setConfigFromPortConfig(&config, requestedPortConfig);
370 AudioSource source = requestedPortConfig.ext.get<Tag::mix>().usecase.getTag() ==
371 AudioPortMixExtUseCase::Tag::source ?
372 requestedPortConfig.ext.get<Tag::mix>().usecase.
373 get<AudioPortMixExtUseCase::Tag::source>() : AudioSource::SYS_RESERVED_INVALID;
374 return findOrCreatePortConfig(config, requestedPortConfig.flags,
375 requestedPortConfig.ext.get<Tag::mix>().handle, source, destinationPortIds,
376 portConfig, created);
377 } else if (requestedPortConfig.ext.getTag() == Tag::device) {
378 return findOrCreatePortConfig(
379 requestedPortConfig.ext.get<Tag::device>().device, nullptr /*config*/,
380 portConfig, created);
381 }
382 ALOGW("%s: unsupported audio port config: %s",
383 __func__, requestedPortConfig.toString().c_str());
384 return BAD_VALUE;
385}
386
387status_t Hal2AidlMapper::findOrCreatePortConfig(
388 const AudioPortConfig& requestedPortConfig, const std::set<int32_t>& destinationPortIds,
389 AudioPortConfig* portConfig, Cleanups* cleanups) {
390 bool created = false;
391 RETURN_STATUS_IF_ERROR(findOrCreatePortConfig(
392 requestedPortConfig, destinationPortIds, portConfig, &created));
393 if (created && cleanups != nullptr) {
394 cleanups->add(&Hal2AidlMapper::resetPortConfig, portConfig->id);
395 }
396 return OK;
397}
398
399status_t Hal2AidlMapper::findPortConfig(const AudioDevice& device, AudioPortConfig* portConfig) {
400 if (auto it = findPortConfig(device); it != mPortConfigs.end()) {
401 *portConfig = it->second;
402 return OK;
403 }
404 ALOGE("%s: could not find a configured device port for device %s",
405 __func__, device.toString().c_str());
406 return BAD_VALUE;
407}
408
409Hal2AidlMapper::Patches::iterator Hal2AidlMapper::findPatch(
410 const std::set<int32_t>& sourcePortConfigIds, const std::set<int32_t>& sinkPortConfigIds) {
411 return std::find_if(mPatches.begin(), mPatches.end(),
412 [&](const auto& pair) {
413 const auto& p = pair.second;
414 std::set<int32_t> patchSrcs(
415 p.sourcePortConfigIds.begin(), p.sourcePortConfigIds.end());
416 std::set<int32_t> patchSinks(
417 p.sinkPortConfigIds.begin(), p.sinkPortConfigIds.end());
418 return sourcePortConfigIds == patchSrcs && sinkPortConfigIds == patchSinks; });
419}
420
421Hal2AidlMapper::Ports::iterator Hal2AidlMapper::findPort(const AudioDevice& device) {
422 if (device.type.type == AudioDeviceType::IN_DEFAULT) {
423 return mPorts.find(mDefaultInputPortId);
424 } else if (device.type.type == AudioDeviceType::OUT_DEFAULT) {
425 return mPorts.find(mDefaultOutputPortId);
426 }
427 if (device.address.getTag() != AudioDeviceAddress::id ||
428 !device.address.get<AudioDeviceAddress::id>().empty()) {
429 return std::find_if(mPorts.begin(), mPorts.end(),
430 [&](const auto& pair) { return audioDeviceMatches(device, pair.second); });
431 }
432 // For connection w/o an address, two ports can be found: the template port,
433 // and a connected port (if exists). Make sure we return the connected port.
434 Hal2AidlMapper::Ports::iterator portIt = mPorts.end();
435 for (auto it = mPorts.begin(); it != mPorts.end(); ++it) {
436 if (audioDeviceMatches(device, it->second)) {
437 if (mConnectedPorts.find(it->first) != mConnectedPorts.end()) {
438 return it;
439 } else {
440 // Will return 'it' if there is no connected port.
441 portIt = it;
442 }
443 }
444 }
445 return portIt;
446}
447
448Hal2AidlMapper::Ports::iterator Hal2AidlMapper::findPort(
449 const AudioConfig& config, const AudioIoFlags& flags,
450 const std::set<int32_t>& destinationPortIds) {
jiabin80797002023-12-01 22:02:41 +0000451 auto channelMaskMatches = [](const std::vector<AudioChannelLayout>& channelMasks,
452 const AudioChannelLayout& channelMask) {
453 // Return true when 1) the channel mask is none and none of the channel mask from the
454 // collection contains haptic channel mask, or 2) the channel mask collection contains
455 // the queried channel mask.
456 return (channelMask.getTag() == AudioChannelLayout::none &&
457 std::none_of(channelMasks.begin(), channelMasks.end(),
458 containHapticChannel)) ||
459 std::find(channelMasks.begin(), channelMasks.end(), channelMask)
460 != channelMasks.end();
461 };
462 auto belongsToProfile = [&config, &channelMaskMatches](const AudioProfile& prof) {
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700463 return (isDefaultAudioFormat(config.base.format) || prof.format == config.base.format) &&
jiabin80797002023-12-01 22:02:41 +0000464 channelMaskMatches(prof.channelMasks, config.base.channelMask) &&
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700465 (config.base.sampleRate == 0 ||
466 std::find(prof.sampleRates.begin(), prof.sampleRates.end(),
467 config.base.sampleRate) != prof.sampleRates.end());
468 };
469 static const std::vector<AudioOutputFlags> kOptionalOutputFlags{AudioOutputFlags::BIT_PERFECT};
470 int optionalFlags = 0;
471 auto flagMatches = [&flags, &optionalFlags](const AudioIoFlags& portFlags) {
472 // Ports should be able to match if the optional flags are not requested.
473 return portFlags == flags ||
474 (portFlags.getTag() == AudioIoFlags::Tag::output &&
475 AudioIoFlags::make<AudioIoFlags::Tag::output>(
476 portFlags.get<AudioIoFlags::Tag::output>() &
477 ~optionalFlags) == flags);
478 };
479 auto matcher = [&](const auto& pair) {
480 const auto& p = pair.second;
481 return p.ext.getTag() == AudioPortExt::Tag::mix &&
482 flagMatches(p.flags) &&
483 (destinationPortIds.empty() ||
484 std::any_of(destinationPortIds.begin(), destinationPortIds.end(),
485 [&](const int32_t destId) { return mRoutingMatrix.count(
486 std::make_pair(p.id, destId)) != 0; })) &&
487 (p.profiles.empty() ||
488 std::find_if(p.profiles.begin(), p.profiles.end(), belongsToProfile) !=
489 p.profiles.end()); };
490 auto result = std::find_if(mPorts.begin(), mPorts.end(), matcher);
491 if (result == mPorts.end() && flags.getTag() == AudioIoFlags::Tag::output) {
492 auto optionalOutputFlagsIt = kOptionalOutputFlags.begin();
493 while (result == mPorts.end() && optionalOutputFlagsIt != kOptionalOutputFlags.end()) {
494 if (isBitPositionFlagSet(
495 flags.get<AudioIoFlags::Tag::output>(), *optionalOutputFlagsIt)) {
496 // If the flag is set by the request, it must be matched.
497 ++optionalOutputFlagsIt;
498 continue;
499 }
500 optionalFlags |= makeBitPositionFlagMask(*optionalOutputFlagsIt++);
501 result = std::find_if(mPorts.begin(), mPorts.end(), matcher);
502 ALOGI("%s: port for config %s, flags %s was not found in the module %s, "
503 "retried with excluding optional flags %#x", __func__, config.toString().c_str(),
504 flags.toString().c_str(), mInstance.c_str(), optionalFlags);
505 }
506 }
507 return result;
508}
509
510Hal2AidlMapper::PortConfigs::iterator Hal2AidlMapper::findPortConfig(const AudioDevice& device) {
511 return std::find_if(mPortConfigs.begin(), mPortConfigs.end(),
512 [&](const auto& pair) { return audioDeviceMatches(device, pair.second); });
513}
514
515Hal2AidlMapper::PortConfigs::iterator Hal2AidlMapper::findPortConfig(
516 const std::optional<AudioConfig>& config,
517 const std::optional<AudioIoFlags>& flags,
518 int32_t ioHandle) {
519 using Tag = AudioPortExt::Tag;
520 return std::find_if(mPortConfigs.begin(), mPortConfigs.end(),
521 [&](const auto& pair) {
522 const auto& p = pair.second;
523 LOG_ALWAYS_FATAL_IF(p.ext.getTag() == Tag::mix &&
524 (!p.sampleRate.has_value() || !p.channelMask.has_value() ||
525 !p.format.has_value() || !p.flags.has_value()),
526 "%s: stored mix port config is not fully specified: %s",
527 __func__, p.toString().c_str());
528 return p.ext.getTag() == Tag::mix &&
529 (!config.has_value() ||
530 isConfigEqualToPortConfig(config.value(), p)) &&
531 (!flags.has_value() || p.flags.value() == flags.value()) &&
532 p.ext.template get<Tag::mix>().handle == ioHandle; });
533}
534
535status_t Hal2AidlMapper::getAudioMixPort(int32_t ioHandle, AudioPort* port) {
536 auto it = findPortConfig(std::nullopt /*config*/, std::nullopt /*flags*/, ioHandle);
537 if (it == mPortConfigs.end()) {
538 ALOGE("%s, cannot find mix port config for handle %u", __func__, ioHandle);
539 return BAD_VALUE;
540 }
541 return updateAudioPort(it->second.portId, port);
542}
543
544status_t Hal2AidlMapper::getAudioPortCached(
545 const ::aidl::android::media::audio::common::AudioDevice& device,
546 ::aidl::android::media::audio::common::AudioPort* port) {
547
548 if (auto portsIt = findPort(device); portsIt != mPorts.end()) {
549 *port = portsIt->second;
550 return OK;
551 }
552 ALOGE("%s: device port for device %s is not found in the module %s",
553 __func__, device.toString().c_str(), mInstance.c_str());
554 return BAD_VALUE;
555}
556
557status_t Hal2AidlMapper::initialize() {
558 std::vector<AudioPort> ports;
559 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->getAudioPorts(&ports)));
560 ALOGW_IF(ports.empty(), "%s: module %s returned an empty list of audio ports",
561 __func__, mInstance.c_str());
562 mDefaultInputPortId = mDefaultOutputPortId = -1;
563 const int defaultDeviceFlag = 1 << AudioPortDeviceExt::FLAG_INDEX_DEFAULT_DEVICE;
564 for (auto it = ports.begin(); it != ports.end(); ) {
565 const auto& port = *it;
566 if (port.ext.getTag() != AudioPortExt::Tag::device) {
567 ++it;
568 continue;
569 }
570 const AudioPortDeviceExt& deviceExt = port.ext.get<AudioPortExt::Tag::device>();
571 if ((deviceExt.flags & defaultDeviceFlag) != 0) {
572 if (port.flags.getTag() == AudioIoFlags::Tag::input) {
573 mDefaultInputPortId = port.id;
574 } else if (port.flags.getTag() == AudioIoFlags::Tag::output) {
575 mDefaultOutputPortId = port.id;
576 }
577 }
578 // For compatibility with HIDL, hide "template" remote submix ports from ports list.
579 if (const auto& devDesc = deviceExt.device;
580 (devDesc.type.type == AudioDeviceType::IN_SUBMIX ||
581 devDesc.type.type == AudioDeviceType::OUT_SUBMIX) &&
582 devDesc.type.connection == AudioDeviceDescription::CONNECTION_VIRTUAL) {
583 if (devDesc.type.type == AudioDeviceType::IN_SUBMIX) {
584 mRemoteSubmixIn = port;
585 } else {
586 mRemoteSubmixOut = port;
587 }
588 it = ports.erase(it);
589 } else {
590 ++it;
591 }
592 }
593 if (mRemoteSubmixIn.has_value() != mRemoteSubmixOut.has_value()) {
594 ALOGE("%s: The configuration only has input or output remote submix device, must have both",
595 __func__);
596 mRemoteSubmixIn.reset();
597 mRemoteSubmixOut.reset();
598 }
599 if (mRemoteSubmixIn.has_value()) {
600 AudioPort connectedRSubmixIn = *mRemoteSubmixIn;
601 connectedRSubmixIn.ext.get<AudioPortExt::Tag::device>().device.address =
602 AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS;
603 ALOGD("%s: connecting remote submix input", __func__);
604 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->connectExternalDevice(
605 connectedRSubmixIn, &connectedRSubmixIn)));
606 // The template port for the remote submix input couldn't be "default" because it is not
607 // attached. The connected port can now be made default because we never disconnect it.
608 if (mDefaultInputPortId == -1) {
609 mDefaultInputPortId = connectedRSubmixIn.id;
610 }
611 ports.push_back(std::move(connectedRSubmixIn));
612
613 // Remote submix output must not be connected until the framework actually starts
614 // using it, however for legacy compatibility we need to provide an "augmented template"
615 // port with an address and profiles. It is obtained by connecting the output and then
616 // immediately disconnecting it. This is a cheap operation as we don't open any streams.
617 AudioPort tempConnectedRSubmixOut = *mRemoteSubmixOut;
618 tempConnectedRSubmixOut.ext.get<AudioPortExt::Tag::device>().device.address =
619 AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS;
620 ALOGD("%s: temporarily connecting and disconnecting remote submix output", __func__);
621 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->connectExternalDevice(
622 tempConnectedRSubmixOut, &tempConnectedRSubmixOut)));
623 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->disconnectExternalDevice(
624 tempConnectedRSubmixOut.id)));
625 tempConnectedRSubmixOut.id = mRemoteSubmixOut->id;
626 ports.push_back(std::move(tempConnectedRSubmixOut));
627 }
628
629 ALOGI("%s: module %s default port ids: input %d, output %d",
630 __func__, mInstance.c_str(), mDefaultInputPortId, mDefaultOutputPortId);
631 std::transform(ports.begin(), ports.end(), std::inserter(mPorts, mPorts.end()),
632 [](const auto& p) { return std::make_pair(p.id, p); });
633 RETURN_STATUS_IF_ERROR(updateRoutes());
634 std::vector<AudioPortConfig> portConfigs;
635 RETURN_STATUS_IF_ERROR(
636 statusTFromBinderStatus(mModule->getAudioPortConfigs(&portConfigs))); // OK if empty
637 std::transform(portConfigs.begin(), portConfigs.end(),
638 std::inserter(mPortConfigs, mPortConfigs.end()),
639 [](const auto& p) { return std::make_pair(p.id, p); });
640 std::transform(mPortConfigs.begin(), mPortConfigs.end(),
641 std::inserter(mInitialPortConfigIds, mInitialPortConfigIds.end()),
642 [](const auto& pcPair) { return pcPair.first; });
643 std::vector<AudioPatch> patches;
644 RETURN_STATUS_IF_ERROR(
645 statusTFromBinderStatus(mModule->getAudioPatches(&patches))); // OK if empty
646 std::transform(patches.begin(), patches.end(),
647 std::inserter(mPatches, mPatches.end()),
648 [](const auto& p) { return std::make_pair(p.id, p); });
649 return OK;
650}
651
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800652bool Hal2AidlMapper::isPortBeingHeld(int32_t portId) {
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700653 // It is assumed that mStreams has already been cleaned up.
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800654 for (const auto& s : mStreams) {
655 if (portConfigBelongsToPort(s.second.first, portId)) return true;
656 }
657 for (const auto& [_, patch] : mPatches) {
658 for (int32_t id : patch.sourcePortConfigIds) {
659 if (portConfigBelongsToPort(id, portId)) return true;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700660 }
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800661 for (int32_t id : patch.sinkPortConfigIds) {
662 if (portConfigBelongsToPort(id, portId)) return true;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700663 }
664 }
665 return false;
666}
667
668status_t Hal2AidlMapper::prepareToOpenStream(
669 int32_t ioHandle, const AudioDevice& device, const AudioIoFlags& flags,
670 AudioSource source, Cleanups* cleanups, AudioConfig* config,
671 AudioPortConfig* mixPortConfig, AudioPatch* patch) {
672 ALOGD("%p %s: handle %d, device %s, flags %s, source %s, config %s, mix port config %s",
673 this, __func__, ioHandle, device.toString().c_str(),
674 flags.toString().c_str(), toString(source).c_str(),
675 config->toString().c_str(), mixPortConfig->toString().c_str());
676 resetUnusedPatchesAndPortConfigs();
677 const bool isInput = flags.getTag() == AudioIoFlags::Tag::input;
678 // Find / create AudioPortConfigs for the device port and the mix port,
679 // then find / create a patch between them, and open a stream on the mix port.
680 AudioPortConfig devicePortConfig;
681 bool created = false;
682 RETURN_STATUS_IF_ERROR(findOrCreatePortConfig(device, config,
683 &devicePortConfig, &created));
684 if (created) {
685 cleanups->add(&Hal2AidlMapper::resetPortConfig, devicePortConfig.id);
686 }
687 RETURN_STATUS_IF_ERROR(findOrCreatePortConfig(*config, flags, ioHandle, source,
688 std::set<int32_t>{devicePortConfig.portId}, mixPortConfig, &created));
689 if (created) {
690 cleanups->add(&Hal2AidlMapper::resetPortConfig, mixPortConfig->id);
691 }
692 setConfigFromPortConfig(config, *mixPortConfig);
693 if (isInput) {
694 RETURN_STATUS_IF_ERROR(findOrCreatePatch(
695 {devicePortConfig.id}, {mixPortConfig->id}, patch, &created));
696 } else {
697 RETURN_STATUS_IF_ERROR(findOrCreatePatch(
698 {mixPortConfig->id}, {devicePortConfig.id}, patch, &created));
699 }
700 if (created) {
701 cleanups->add(&Hal2AidlMapper::resetPatch, patch->id);
702 }
703 if (config->frameCount <= 0) {
704 config->frameCount = patch->minimumStreamBufferSizeFrames;
705 }
706 return OK;
707}
708
709status_t Hal2AidlMapper::releaseAudioPatch(int32_t patchId) {
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800710 return releaseAudioPatches({patchId});
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700711}
712
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800713status_t Hal2AidlMapper::releaseAudioPatches(const std::set<int32_t>& patchIds) {
714 status_t result = OK;
715 for (const auto patchId : patchIds) {
716 if (auto it = mPatches.find(patchId); it != mPatches.end()) {
717 mPatches.erase(it);
718 if (ndk::ScopedAStatus status = mModule->resetAudioPatch(patchId); !status.isOk()) {
719 ALOGE("%s: error while resetting patch %d: %s",
720 __func__, patchId, status.getDescription().c_str());
721 result = statusTFromBinderStatus(status);
722 }
723 } else {
724 ALOGE("%s: patch id %d not found", __func__, patchId);
725 result = BAD_VALUE;
726 }
727 }
728 resetUnusedPortConfigs();
729 return result;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700730}
731
732void Hal2AidlMapper::resetPortConfig(int32_t portConfigId) {
733 if (auto it = mPortConfigs.find(portConfigId); it != mPortConfigs.end()) {
734 mPortConfigs.erase(it);
735 if (ndk::ScopedAStatus status = mModule->resetAudioPortConfig(portConfigId);
736 !status.isOk()) {
737 ALOGE("%s: error while resetting port config %d: %s",
738 __func__, portConfigId, status.getDescription().c_str());
739 }
740 return;
741 }
742 ALOGE("%s: port config id %d not found", __func__, portConfigId);
743}
744
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800745void Hal2AidlMapper::resetUnusedPatchesAndPortConfigs() {
746 // Since patches can be created independently of streams via 'createOrUpdatePatch',
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700747 // here we only clean up patches for released streams.
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800748 std::set<int32_t> patchesToRelease;
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700749 for (auto it = mStreams.begin(); it != mStreams.end(); ) {
750 if (auto streamSp = it->first.promote(); streamSp) {
751 ++it;
752 } else {
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800753 if (const int32_t patchId = it->second.second; patchId != -1) {
754 patchesToRelease.insert(patchId);
755 }
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700756 it = mStreams.erase(it);
757 }
758 }
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800759 // 'releaseAudioPatches' also resets unused port configs.
760 releaseAudioPatches(patchesToRelease);
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700761}
762
763void Hal2AidlMapper::resetUnusedPortConfigs() {
764 // The assumption is that port configs are used to create patches
765 // (or to open streams, but that involves creation of patches, too). Thus,
766 // orphaned port configs can and should be reset.
767 std::map<int32_t, int32_t /*portID*/> portConfigIds;
768 std::transform(mPortConfigs.begin(), mPortConfigs.end(),
769 std::inserter(portConfigIds, portConfigIds.end()),
770 [](const auto& pcPair) { return std::make_pair(pcPair.first, pcPair.second.portId); });
771 for (const auto& p : mPatches) {
772 for (int32_t id : p.second.sourcePortConfigIds) portConfigIds.erase(id);
773 for (int32_t id : p.second.sinkPortConfigIds) portConfigIds.erase(id);
774 }
775 for (int32_t id : mInitialPortConfigIds) {
776 portConfigIds.erase(id);
777 }
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800778 for (const auto& s : mStreams) {
779 portConfigIds.erase(s.second.first);
780 }
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700781 std::set<int32_t> retryDeviceDisconnection;
782 for (const auto& portConfigAndIdPair : portConfigIds) {
783 resetPortConfig(portConfigAndIdPair.first);
784 if (const auto it = mConnectedPorts.find(portConfigAndIdPair.second);
785 it != mConnectedPorts.end() && it->second) {
786 retryDeviceDisconnection.insert(portConfigAndIdPair.second);
787 }
788 }
789 for (int32_t portId : retryDeviceDisconnection) {
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800790 if (!isPortBeingHeld(portId)) {
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700791 if (auto status = mModule->disconnectExternalDevice(portId); status.isOk()) {
792 eraseConnectedPort(portId);
793 ALOGD("%s: executed postponed external device disconnection for port ID %d",
794 __func__, portId);
795 }
796 }
797 }
798 if (!retryDeviceDisconnection.empty()) {
799 updateRoutes();
800 }
801}
802
803status_t Hal2AidlMapper::setDevicePortConnectedState(const AudioPort& devicePort, bool connected) {
804 if (connected) {
805 AudioDevice matchDevice = devicePort.ext.get<AudioPortExt::device>().device;
806 std::optional<AudioPort> templatePort;
807 auto erasePortAfterConnectionIt = mPorts.end();
808 // Connection of remote submix out with address "0" is a special case. Since there is
809 // already an "augmented template" port with this address in mPorts, we need to replace
810 // it with a connected port.
811 // Connection of remote submix outs with any other address is done as usual except that
812 // the template port is in `mRemoteSubmixOut`.
813 if (mRemoteSubmixOut.has_value() && matchDevice.type.type == AudioDeviceType::OUT_SUBMIX) {
814 if (matchDevice.address == AudioDeviceAddress::make<AudioDeviceAddress::id>(
815 AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS)) {
816 erasePortAfterConnectionIt = findPort(matchDevice);
817 }
818 templatePort = mRemoteSubmixOut;
819 } else if (mRemoteSubmixIn.has_value() &&
820 matchDevice.type.type == AudioDeviceType::IN_SUBMIX) {
821 templatePort = mRemoteSubmixIn;
822 } else {
823 // Reset the device address to find the "template" port.
824 matchDevice.address = AudioDeviceAddress::make<AudioDeviceAddress::id>();
825 }
826 if (!templatePort.has_value()) {
827 auto portsIt = findPort(matchDevice);
828 if (portsIt == mPorts.end()) {
829 // Since 'setConnectedState' is called for all modules, it is normal when the device
830 // port not found in every one of them.
831 return BAD_VALUE;
832 } else {
833 ALOGD("%s: device port for device %s found in the module %s",
834 __func__, matchDevice.toString().c_str(), mInstance.c_str());
835 }
836 templatePort = portsIt->second;
837 }
838 resetUnusedPatchesAndPortConfigs();
839
840 // Use the ID of the "template" port, use all the information from the provided port.
841 AudioPort connectedPort = devicePort;
842 connectedPort.id = templatePort->id;
843 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->connectExternalDevice(
844 connectedPort, &connectedPort)));
845 const auto [it, inserted] = mPorts.insert(std::make_pair(connectedPort.id, connectedPort));
846 LOG_ALWAYS_FATAL_IF(!inserted,
847 "%s: module %s, duplicate port ID received from HAL: %s, existing port: %s",
848 __func__, mInstance.c_str(), connectedPort.toString().c_str(),
849 it->second.toString().c_str());
850 mConnectedPorts[connectedPort.id] = false;
851 if (erasePortAfterConnectionIt != mPorts.end()) {
852 mPorts.erase(erasePortAfterConnectionIt);
853 }
854 } else { // !connected
855 AudioDevice matchDevice = devicePort.ext.get<AudioPortExt::device>().device;
856 auto portsIt = findPort(matchDevice);
857 if (portsIt == mPorts.end()) {
858 // Since 'setConnectedState' is called for all modules, it is normal when the device
859 // port not found in every one of them.
860 return BAD_VALUE;
861 } else {
862 ALOGD("%s: device port for device %s found in the module %s",
863 __func__, matchDevice.toString().c_str(), mInstance.c_str());
864 }
865 resetUnusedPatchesAndPortConfigs();
866
867 // Disconnection of remote submix out with address "0" is a special case. We need to replace
868 // the connected port entry with the "augmented template".
869 const int32_t portId = portsIt->second.id;
870 if (mRemoteSubmixOut.has_value() && matchDevice.type.type == AudioDeviceType::OUT_SUBMIX &&
871 matchDevice.address == AudioDeviceAddress::make<AudioDeviceAddress::id>(
872 AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS)) {
873 mDisconnectedPortReplacement = std::make_pair(portId, *mRemoteSubmixOut);
874 auto& port = mDisconnectedPortReplacement.second;
875 port.ext.get<AudioPortExt::Tag::device>().device = matchDevice;
876 port.profiles = portsIt->second.profiles;
877 }
878 // Streams are closed by AudioFlinger independently from device disconnections.
879 // It is possible that the stream has not been closed yet.
Mikhail Naganov78f7f9a2023-11-16 15:49:23 -0800880 if (!isPortBeingHeld(portId)) {
Mikhail Naganovac9d4e72023-10-23 12:00:09 -0700881 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
882 mModule->disconnectExternalDevice(portId)));
883 eraseConnectedPort(portId);
884 } else {
885 ALOGD("%s: since device port ID %d is used by a stream, "
886 "external device disconnection postponed", __func__, portId);
887 mConnectedPorts[portId] = true;
888 }
889 }
890 return updateRoutes();
891}
892
893status_t Hal2AidlMapper::updateAudioPort(int32_t portId, AudioPort* port) {
894 const status_t status = statusTFromBinderStatus(mModule->getAudioPort(portId, port));
895 if (status == OK) {
896 auto portIt = mPorts.find(portId);
897 if (portIt != mPorts.end()) {
898 portIt->second = *port;
899 } else {
900 ALOGW("%s, port(%d) returned successfully from the HAL but not it is not cached",
901 __func__, portId);
902 }
903 }
904 return status;
905}
906
907status_t Hal2AidlMapper::updateRoutes() {
908 RETURN_STATUS_IF_ERROR(
909 statusTFromBinderStatus(mModule->getAudioRoutes(&mRoutes)));
910 ALOGW_IF(mRoutes.empty(), "%s: module %s returned an empty list of audio routes",
911 __func__, mInstance.c_str());
912 if (mRemoteSubmixIn.has_value()) {
913 // Remove mentions of the template remote submix input from routes.
914 int32_t rSubmixInId = mRemoteSubmixIn->id;
915 // Remove mentions of the template remote submix out only if it is not in mPorts
916 // (that means there is a connected port in mPorts).
917 int32_t rSubmixOutId = mPorts.find(mRemoteSubmixOut->id) == mPorts.end() ?
918 mRemoteSubmixOut->id : -1;
919 for (auto it = mRoutes.begin(); it != mRoutes.end();) {
920 auto& route = *it;
921 if (route.sinkPortId == rSubmixOutId) {
922 it = mRoutes.erase(it);
923 continue;
924 }
925 if (auto routeIt = std::find(route.sourcePortIds.begin(), route.sourcePortIds.end(),
926 rSubmixInId); routeIt != route.sourcePortIds.end()) {
927 route.sourcePortIds.erase(routeIt);
928 if (route.sourcePortIds.empty()) {
929 it = mRoutes.erase(it);
930 continue;
931 }
932 }
933 ++it;
934 }
935 }
936 mRoutingMatrix.clear();
937 for (const auto& r : mRoutes) {
938 for (auto portId : r.sourcePortIds) {
939 mRoutingMatrix.emplace(r.sinkPortId, portId);
940 mRoutingMatrix.emplace(portId, r.sinkPortId);
941 }
942 }
943 return OK;
944}
945
946} // namespace android