1 /*
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 #include <algorithm>
18 #include <chrono>
19
20 #define LOG_TAG "VtsHalAudio.ModuleConfig"
21 #include <android-base/logging.h>
22
23 #include <Utils.h>
24 #include <aidl/android/media/audio/common/AudioInputFlags.h>
25 #include <aidl/android/media/audio/common/AudioIoFlags.h>
26 #include <aidl/android/media/audio/common/AudioOutputFlags.h>
27 #include <error/expected_utils.h>
28
29 #include "ModuleConfig.h"
30
31 using namespace android;
32 using namespace std::chrono_literals;
33
34 using aidl::android::hardware::audio::common::isBitPositionFlagSet;
35 using aidl::android::hardware::audio::core::IModule;
36 using aidl::android::media::audio::common::AudioChannelLayout;
37 using aidl::android::media::audio::common::AudioDeviceDescription;
38 using aidl::android::media::audio::common::AudioDeviceType;
39 using aidl::android::media::audio::common::AudioEncapsulationMode;
40 using aidl::android::media::audio::common::AudioFormatDescription;
41 using aidl::android::media::audio::common::AudioFormatType;
42 using aidl::android::media::audio::common::AudioInputFlags;
43 using aidl::android::media::audio::common::AudioIoFlags;
44 using aidl::android::media::audio::common::AudioOffloadInfo;
45 using aidl::android::media::audio::common::AudioOutputFlags;
46 using aidl::android::media::audio::common::AudioPort;
47 using aidl::android::media::audio::common::AudioPortConfig;
48 using aidl::android::media::audio::common::AudioPortExt;
49 using aidl::android::media::audio::common::AudioProfile;
50 using aidl::android::media::audio::common::AudioUsage;
51 using aidl::android::media::audio::common::Int;
52
53 // static
generateOffloadInfoIfNeeded(const AudioPortConfig & portConfig)54 std::optional<AudioOffloadInfo> ModuleConfig::generateOffloadInfoIfNeeded(
55 const AudioPortConfig& portConfig) {
56 if (portConfig.flags.has_value() &&
57 portConfig.flags.value().getTag() == AudioIoFlags::Tag::output &&
58 isBitPositionFlagSet(portConfig.flags.value().get<AudioIoFlags::Tag::output>(),
59 AudioOutputFlags::COMPRESS_OFFLOAD)) {
60 AudioOffloadInfo offloadInfo;
61 offloadInfo.base.sampleRate = portConfig.sampleRate.value().value;
62 offloadInfo.base.channelMask = portConfig.channelMask.value();
63 offloadInfo.base.format = portConfig.format.value();
64 offloadInfo.bitRatePerSecond = 256000; // Arbitrary value.
65 offloadInfo.durationUs = std::chrono::microseconds(1min).count(); // Arbitrary value.
66 offloadInfo.usage = AudioUsage::MEDIA;
67 offloadInfo.encapsulationMode = AudioEncapsulationMode::NONE;
68 return offloadInfo;
69 }
70 return {};
71 }
72
73 // static
74 std::vector<aidl::android::media::audio::common::AudioPort>
getAudioPortsForDeviceTypes(const std::vector<aidl::android::media::audio::common::AudioPort> & ports,const std::vector<AudioDeviceType> & deviceTypes,const std::string & connection)75 ModuleConfig::getAudioPortsForDeviceTypes(
76 const std::vector<aidl::android::media::audio::common::AudioPort>& ports,
77 const std::vector<AudioDeviceType>& deviceTypes, const std::string& connection) {
78 std::vector<AudioPort> result;
79 for (const auto& port : ports) {
80 if (port.ext.getTag() != AudioPortExt::Tag::device) continue;
81 const auto type = port.ext.get<AudioPortExt::Tag::device>().device.type;
82 if (type.connection == connection) {
83 for (auto deviceType : deviceTypes) {
84 if (type.type == deviceType) {
85 result.push_back(port);
86 }
87 }
88 }
89 }
90 return result;
91 }
92
93 // static
getBuiltInMicPorts(const std::vector<aidl::android::media::audio::common::AudioPort> & ports)94 std::vector<aidl::android::media::audio::common::AudioPort> ModuleConfig::getBuiltInMicPorts(
95 const std::vector<aidl::android::media::audio::common::AudioPort>& ports) {
96 return getAudioPortsForDeviceTypes(
97 ports, std::vector<AudioDeviceType>{AudioDeviceType::IN_MICROPHONE,
98 AudioDeviceType::IN_MICROPHONE_BACK});
99 }
100
101 template <typename T>
findById(const std::vector<T> & v,int32_t id)102 auto findById(const std::vector<T>& v, int32_t id) {
103 return std::find_if(v.begin(), v.end(), [&](const auto& p) { return p.id == id; });
104 }
105
ModuleConfig(IModule * module)106 ModuleConfig::ModuleConfig(IModule* module) {
107 mStatus = module->getAudioPorts(&mPorts);
108 if (!mStatus.isOk()) return;
109 for (const auto& port : mPorts) {
110 if (port.ext.getTag() != AudioPortExt::Tag::device) continue;
111 const auto& devicePort = port.ext.get<AudioPortExt::Tag::device>();
112 if (devicePort.device.type.connection.empty()) {
113 const bool isInput = port.flags.getTag() == AudioIoFlags::Tag::input;
114 // Permanently attached device.
115 if (isInput) {
116 mAttachedSourceDevicePorts.insert(port.id);
117 } else {
118 mAttachedSinkDevicePorts.insert(port.id);
119 }
120 } else {
121 mExternalDevicePorts.insert(port.id);
122 }
123 }
124 if (!mStatus.isOk()) return;
125 mStatus = module->getAudioRoutes(&mRoutes);
126 if (!mStatus.isOk()) return;
127 mStatus = module->getAudioPortConfigs(&mInitialConfigs);
128 }
129
getAttachedDevicePorts() const130 std::vector<AudioPort> ModuleConfig::getAttachedDevicePorts() const {
131 std::vector<AudioPort> result;
132 std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) {
133 return mAttachedSinkDevicePorts.count(port.id) != 0 ||
134 mAttachedSourceDevicePorts.count(port.id) != 0;
135 });
136 return result;
137 }
138
139 std::vector<aidl::android::media::audio::common::AudioPort>
getAudioPortsForDeviceTypes(const std::vector<AudioDeviceType> & deviceTypes,const std::string & connection) const140 ModuleConfig::getAudioPortsForDeviceTypes(const std::vector<AudioDeviceType>& deviceTypes,
141 const std::string& connection) const {
142 return getAudioPortsForDeviceTypes(mPorts, deviceTypes, connection);
143 }
144
getConnectedExternalDevicePorts() const145 std::vector<AudioPort> ModuleConfig::getConnectedExternalDevicePorts() const {
146 std::vector<AudioPort> result;
147 std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) {
148 return mConnectedExternalSinkDevicePorts.count(port.id) != 0 ||
149 mConnectedExternalSourceDevicePorts.count(port.id) != 0;
150 });
151 return result;
152 }
153
getConnectedSinkDevicePorts() const154 std::set<int32_t> ModuleConfig::getConnectedSinkDevicePorts() const {
155 std::set<int32_t> result;
156 result.insert(mAttachedSinkDevicePorts.begin(), mAttachedSinkDevicePorts.end());
157 result.insert(mConnectedExternalSinkDevicePorts.begin(),
158 mConnectedExternalSinkDevicePorts.end());
159 return result;
160 }
161
getConnectedSourceDevicePorts() const162 std::set<int32_t> ModuleConfig::getConnectedSourceDevicePorts() const {
163 std::set<int32_t> result;
164 result.insert(mAttachedSourceDevicePorts.begin(), mAttachedSourceDevicePorts.end());
165 result.insert(mConnectedExternalSourceDevicePorts.begin(),
166 mConnectedExternalSourceDevicePorts.end());
167 return result;
168 }
169
getExternalDevicePorts() const170 std::vector<AudioPort> ModuleConfig::getExternalDevicePorts() const {
171 std::vector<AudioPort> result;
172 std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result),
173 [&](const auto& port) { return mExternalDevicePorts.count(port.id) != 0; });
174 return result;
175 }
176
getInputMixPorts(bool connectedOnly) const177 std::vector<AudioPort> ModuleConfig::getInputMixPorts(bool connectedOnly) const {
178 std::vector<AudioPort> result;
179 std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) {
180 return port.ext.getTag() == AudioPortExt::Tag::mix &&
181 port.flags.getTag() == AudioIoFlags::Tag::input &&
182 (!connectedOnly || !getConnectedSourceDevicesPortsForMixPort(port).empty());
183 });
184 return result;
185 }
186
getOutputMixPorts(bool connectedOnly) const187 std::vector<AudioPort> ModuleConfig::getOutputMixPorts(bool connectedOnly) const {
188 std::vector<AudioPort> result;
189 std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) {
190 return port.ext.getTag() == AudioPortExt::Tag::mix &&
191 port.flags.getTag() == AudioIoFlags::Tag::output &&
192 (!connectedOnly || !getConnectedSinkDevicesPortsForMixPort(port).empty());
193 });
194 return result;
195 }
196
getNonBlockingMixPorts(bool connectedOnly,bool singlePort) const197 std::vector<AudioPort> ModuleConfig::getNonBlockingMixPorts(bool connectedOnly,
198 bool singlePort) const {
199 return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) {
200 return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
201 AudioOutputFlags::NON_BLOCKING);
202 });
203 }
204
getOffloadMixPorts(bool connectedOnly,bool singlePort) const205 std::vector<AudioPort> ModuleConfig::getOffloadMixPorts(bool connectedOnly, bool singlePort) const {
206 return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) {
207 return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
208 AudioOutputFlags::COMPRESS_OFFLOAD);
209 });
210 }
211
getPrimaryMixPorts(bool connectedOnly,bool singlePort) const212 std::vector<AudioPort> ModuleConfig::getPrimaryMixPorts(bool connectedOnly, bool singlePort) const {
213 return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) {
214 return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
215 AudioOutputFlags::PRIMARY);
216 });
217 }
218
getMmapOutMixPorts(bool connectedOnly,bool singlePort) const219 std::vector<AudioPort> ModuleConfig::getMmapOutMixPorts(bool connectedOnly, bool singlePort) const {
220 return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) {
221 return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
222 AudioOutputFlags::MMAP_NOIRQ);
223 });
224 }
225
getMmapInMixPorts(bool connectedOnly,bool singlePort) const226 std::vector<AudioPort> ModuleConfig::getMmapInMixPorts(bool connectedOnly, bool singlePort) const {
227 return findMixPorts(true /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) {
228 return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
229 AudioInputFlags::MMAP_NOIRQ);
230 });
231 }
232
getRemoteSubmixPorts(bool isInput,bool singlePort) const233 std::vector<AudioPort> ModuleConfig::getRemoteSubmixPorts(bool isInput, bool singlePort) const {
234 AudioDeviceType deviceType = isInput ? AudioDeviceType::IN_SUBMIX : AudioDeviceType::OUT_SUBMIX;
235 auto ports = getAudioPortsForDeviceTypes(std::vector<AudioDeviceType>{deviceType},
236 AudioDeviceDescription::CONNECTION_VIRTUAL);
237 if (singlePort) {
238 if (!ports.empty()) ports.resize(1);
239 }
240 return ports;
241 }
242
getConnectedDevicesPortsForMixPort(bool isInput,const AudioPortConfig & mixPortConfig) const243 std::vector<AudioPort> ModuleConfig::getConnectedDevicesPortsForMixPort(
244 bool isInput, const AudioPortConfig& mixPortConfig) const {
245 const auto mixPortIt = findById<AudioPort>(mPorts, mixPortConfig.portId);
246 if (mixPortIt != mPorts.end()) {
247 return getConnectedDevicesPortsForMixPort(isInput, *mixPortIt);
248 }
249 return {};
250 }
251
getConnectedSinkDevicesPortsForMixPort(const AudioPort & mixPort) const252 std::vector<AudioPort> ModuleConfig::getConnectedSinkDevicesPortsForMixPort(
253 const AudioPort& mixPort) const {
254 std::vector<AudioPort> result;
255 std::set<int32_t> connectedSinkDevicePorts = getConnectedSinkDevicePorts();
256 for (const auto& route : mRoutes) {
257 if ((connectedSinkDevicePorts.count(route.sinkPortId) != 0) &&
258 std::find(route.sourcePortIds.begin(), route.sourcePortIds.end(), mixPort.id) !=
259 route.sourcePortIds.end()) {
260 const auto devicePortIt = findById<AudioPort>(mPorts, route.sinkPortId);
261 if (devicePortIt != mPorts.end()) result.push_back(*devicePortIt);
262 }
263 }
264 return result;
265 }
266
getConnectedSourceDevicesPortsForMixPort(const AudioPort & mixPort) const267 std::vector<AudioPort> ModuleConfig::getConnectedSourceDevicesPortsForMixPort(
268 const AudioPort& mixPort) const {
269 std::vector<AudioPort> result;
270 std::set<int32_t> connectedSourceDevicePorts = getConnectedSourceDevicePorts();
271 for (const auto& route : mRoutes) {
272 if (route.sinkPortId == mixPort.id) {
273 for (const auto srcId : route.sourcePortIds) {
274 if (connectedSourceDevicePorts.count(srcId) != 0) {
275 const auto devicePortIt = findById<AudioPort>(mPorts, srcId);
276 if (devicePortIt != mPorts.end()) result.push_back(*devicePortIt);
277 }
278 }
279 }
280 }
281 return result;
282 }
283
getSourceMixPortForConnectedDevice() const284 std::optional<AudioPort> ModuleConfig::getSourceMixPortForConnectedDevice() const {
285 std::set<int32_t> connectedSinkDevicePorts = getConnectedSinkDevicePorts();
286 for (const auto& route : mRoutes) {
287 if (connectedSinkDevicePorts.count(route.sinkPortId) != 0) {
288 const auto mixPortIt = findById<AudioPort>(mPorts, route.sourcePortIds[0]);
289 if (mixPortIt != mPorts.end()) return *mixPortIt;
290 }
291 }
292 return {};
293 }
294
getRoutableDevicePortsForMixPort(const AudioPort & port,bool connectedOnly) const295 std::vector<AudioPort> ModuleConfig::getRoutableDevicePortsForMixPort(const AudioPort& port,
296 bool connectedOnly) const {
297 std::set<int32_t> portIds = findRoutablePortIds(port.id);
298 const bool isInput = port.flags.getTag() == AudioIoFlags::input;
299 std::set<int32_t> devicePortIds;
300 if (connectedOnly) {
301 devicePortIds = isInput ? getConnectedSourceDevicePorts() : getConnectedSinkDevicePorts();
302 } else {
303 devicePortIds = portIds;
304 }
305 std::vector<AudioPort> result;
306 std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) {
307 return port.ext.getTag() == AudioPortExt::Tag::device && portIds.count(port.id) > 0 &&
308 devicePortIds.count(port.id) > 0;
309 });
310 return result;
311 }
312
getRoutableMixPortsForDevicePort(const AudioPort & port,bool connectedOnly) const313 std::vector<AudioPort> ModuleConfig::getRoutableMixPortsForDevicePort(const AudioPort& port,
314 bool connectedOnly) const {
315 std::set<int32_t> portIds = findRoutablePortIds(port.id);
316 const bool isInput = port.flags.getTag() == AudioIoFlags::input;
317 return findMixPorts(isInput, connectedOnly, false /*singlePort*/,
318 [&portIds](const AudioPort& p) { return portIds.count(p.id) > 0; });
319 }
320
getNonRoutableSrcSinkPair(bool isInput) const321 std::optional<ModuleConfig::SrcSinkPair> ModuleConfig::getNonRoutableSrcSinkPair(
322 bool isInput) const {
323 const auto mixPorts = getMixPorts(isInput, false /*connectedOnly*/);
324 std::set<std::pair<int32_t, int32_t>> allowedRoutes;
325 for (const auto& route : mRoutes) {
326 for (const auto srcPortId : route.sourcePortIds) {
327 allowedRoutes.emplace(std::make_pair(srcPortId, route.sinkPortId));
328 }
329 }
330 auto make_pair = [isInput](auto& device, auto& mix) {
331 return isInput ? std::make_pair(device, mix) : std::make_pair(mix, device);
332 };
333 for (const auto portId :
334 isInput ? getConnectedSourceDevicePorts() : getConnectedSinkDevicePorts()) {
335 const auto devicePortIt = findById<AudioPort>(mPorts, portId);
336 if (devicePortIt == mPorts.end()) continue;
337 auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt);
338 for (const auto& mixPort : mixPorts) {
339 if (std::find(allowedRoutes.begin(), allowedRoutes.end(),
340 make_pair(portId, mixPort.id)) == allowedRoutes.end()) {
341 auto mixPortConfig = getSingleConfigForMixPort(isInput, mixPort);
342 if (mixPortConfig.has_value()) {
343 return make_pair(devicePortConfig, mixPortConfig.value());
344 }
345 }
346 }
347 }
348 return {};
349 }
350
getRoutableSrcSinkPair(bool isInput) const351 std::optional<ModuleConfig::SrcSinkPair> ModuleConfig::getRoutableSrcSinkPair(bool isInput) const {
352 if (isInput) {
353 std::set<int32_t> connectedSourceDevicePorts = getConnectedSourceDevicePorts();
354 for (const auto& route : mRoutes) {
355 auto srcPortIdIt = std::find_if(
356 route.sourcePortIds.begin(), route.sourcePortIds.end(),
357 [&](const auto& portId) { return connectedSourceDevicePorts.count(portId); });
358 if (srcPortIdIt == route.sourcePortIds.end()) continue;
359 const auto devicePortIt = findById<AudioPort>(mPorts, *srcPortIdIt);
360 const auto mixPortIt = findById<AudioPort>(mPorts, route.sinkPortId);
361 if (devicePortIt == mPorts.end() || mixPortIt == mPorts.end()) continue;
362 auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt);
363 auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt);
364 if (!mixPortConfig.has_value()) continue;
365 return std::make_pair(devicePortConfig, mixPortConfig.value());
366 }
367 } else {
368 std::set<int32_t> connectedSinkDevicePorts = getConnectedSinkDevicePorts();
369 for (const auto& route : mRoutes) {
370 if (connectedSinkDevicePorts.count(route.sinkPortId) == 0) continue;
371 const auto mixPortIt = findById<AudioPort>(mPorts, route.sourcePortIds[0]);
372 const auto devicePortIt = findById<AudioPort>(mPorts, route.sinkPortId);
373 if (devicePortIt == mPorts.end() || mixPortIt == mPorts.end()) continue;
374 auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt);
375 auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt);
376 if (!mixPortConfig.has_value()) continue;
377 return std::make_pair(mixPortConfig.value(), devicePortConfig);
378 }
379 }
380 return {};
381 }
382
getRoutableSrcSinkGroups(bool isInput) const383 std::vector<ModuleConfig::SrcSinkGroup> ModuleConfig::getRoutableSrcSinkGroups(bool isInput) const {
384 std::vector<SrcSinkGroup> result;
385 if (isInput) {
386 std::set<int32_t> connectedSourceDevicePorts = getConnectedSourceDevicePorts();
387 for (const auto& route : mRoutes) {
388 std::vector<int32_t> srcPortIds;
389 std::copy_if(route.sourcePortIds.begin(), route.sourcePortIds.end(),
390 std::back_inserter(srcPortIds), [&](const auto& portId) {
391 return connectedSourceDevicePorts.count(portId);
392 });
393 if (srcPortIds.empty()) continue;
394 const auto mixPortIt = findById<AudioPort>(mPorts, route.sinkPortId);
395 if (mixPortIt == mPorts.end()) continue;
396 auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt);
397 if (!mixPortConfig.has_value()) continue;
398 std::vector<SrcSinkPair> pairs;
399 for (const auto srcPortId : srcPortIds) {
400 const auto devicePortIt = findById<AudioPort>(mPorts, srcPortId);
401 if (devicePortIt == mPorts.end()) continue;
402 // Using all configs for every source would be too much.
403 auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt);
404 pairs.emplace_back(devicePortConfig, mixPortConfig.value());
405 }
406 if (!pairs.empty()) {
407 result.emplace_back(route, std::move(pairs));
408 }
409 }
410 } else {
411 std::set<int32_t> connectedSinkDevicePorts = getConnectedSinkDevicePorts();
412 for (const auto& route : mRoutes) {
413 if (connectedSinkDevicePorts.count(route.sinkPortId) == 0) continue;
414 const auto devicePortIt = findById<AudioPort>(mPorts, route.sinkPortId);
415 if (devicePortIt == mPorts.end()) continue;
416 auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt);
417 std::vector<SrcSinkPair> pairs;
418 for (const auto srcPortId : route.sourcePortIds) {
419 const auto mixPortIt = findById<AudioPort>(mPorts, srcPortId);
420 if (mixPortIt == mPorts.end()) continue;
421 // Using all configs for every source would be too much.
422 auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt);
423 if (mixPortConfig.has_value()) {
424 pairs.emplace_back(mixPortConfig.value(), devicePortConfig);
425 }
426 }
427 if (!pairs.empty()) {
428 result.emplace_back(route, std::move(pairs));
429 }
430 }
431 }
432 return result;
433 }
434
toString() const435 std::string ModuleConfig::toString() const {
436 std::string result;
437 result.append("Ports: ");
438 result.append(android::internal::ToString(mPorts));
439 result.append("\nInitial configs: ");
440 result.append(android::internal::ToString(mInitialConfigs));
441 result.append("\nAttached sink device ports: ");
442 result.append(android::internal::ToString(mAttachedSinkDevicePorts));
443 result.append("\nAttached source device ports: ");
444 result.append(android::internal::ToString(mAttachedSourceDevicePorts));
445 result.append("\nExternal device ports: ");
446 result.append(android::internal::ToString(mExternalDevicePorts));
447 result.append("\nConnected external device ports: ");
448 result.append(android::internal::ToString(getConnectedExternalDevicePorts()));
449 result.append("\nRoutes: ");
450 result.append(android::internal::ToString(mRoutes));
451 return result;
452 }
453
combineAudioConfigs(const AudioPort & port,const AudioProfile & profile,std::vector<AudioPortConfig> * result)454 static size_t combineAudioConfigs(const AudioPort& port, const AudioProfile& profile,
455 std::vector<AudioPortConfig>* result) {
456 const size_t newConfigCount = profile.channelMasks.size() * profile.sampleRates.size();
457 result->reserve(result->capacity() + newConfigCount);
458 for (auto channelMask : profile.channelMasks) {
459 for (auto sampleRate : profile.sampleRates) {
460 AudioPortConfig config{};
461 config.portId = port.id;
462 Int sr;
463 sr.value = sampleRate;
464 config.sampleRate = sr;
465 config.channelMask = channelMask;
466 config.format = profile.format;
467 config.flags = port.flags;
468 config.ext = port.ext;
469 result->push_back(std::move(config));
470 }
471 }
472 return newConfigCount;
473 }
474
isDynamicProfile(const AudioProfile & profile)475 static bool isDynamicProfile(const AudioProfile& profile) {
476 return (profile.format.type == AudioFormatType::DEFAULT && profile.format.encoding.empty()) ||
477 profile.sampleRates.empty() || profile.channelMasks.empty();
478 }
479
findMixPorts(bool isInput,bool connectedOnly,bool singlePort,const std::function<bool (const AudioPort &)> & pred) const480 std::vector<AudioPort> ModuleConfig::findMixPorts(
481 bool isInput, bool connectedOnly, bool singlePort,
482 const std::function<bool(const AudioPort&)>& pred) const {
483 std::vector<AudioPort> result;
484 const auto mixPorts = getMixPorts(isInput, connectedOnly);
485 for (auto mixPortIt = mixPorts.begin(); mixPortIt != mixPorts.end();) {
486 mixPortIt = std::find_if(mixPortIt, mixPorts.end(), pred);
487 if (mixPortIt == mixPorts.end()) break;
488 result.push_back(*mixPortIt++);
489 if (singlePort) break;
490 }
491 return result;
492 }
493
findRoutablePortIds(int32_t portId) const494 std::set<int32_t> ModuleConfig::findRoutablePortIds(int32_t portId) const {
495 std::set<int32_t> portIds;
496 for (const auto& route : mRoutes) {
497 if (portId == route.sinkPortId) {
498 portIds.insert(route.sourcePortIds.begin(), route.sourcePortIds.end());
499 } else if (auto it = std::find(route.sourcePortIds.begin(), route.sourcePortIds.end(),
500 portId);
501 it != route.sourcePortIds.end()) {
502 portIds.insert(route.sinkPortId);
503 }
504 }
505 return portIds;
506 }
507
generateAudioMixPortConfigs(const std::vector<AudioPort> & ports,bool isInput,bool singleProfile) const508 std::vector<AudioPortConfig> ModuleConfig::generateAudioMixPortConfigs(
509 const std::vector<AudioPort>& ports, bool isInput, bool singleProfile) const {
510 std::vector<AudioPortConfig> result;
511 for (const auto& mixPort : ports) {
512 if (getConnectedDevicesPortsForMixPort(isInput, mixPort).empty()) {
513 continue;
514 }
515 for (const auto& profile : mixPort.profiles) {
516 if (isDynamicProfile(profile)) continue;
517 combineAudioConfigs(mixPort, profile, &result);
518 if (singleProfile && !result.empty()) {
519 result.resize(1);
520 return result;
521 }
522 }
523 }
524 return result;
525 }
526
generateAudioDevicePortConfigs(const std::vector<AudioPort> & ports,bool singleProfile) const527 std::vector<AudioPortConfig> ModuleConfig::generateAudioDevicePortConfigs(
528 const std::vector<AudioPort>& ports, bool singleProfile) const {
529 std::vector<AudioPortConfig> result;
530 for (const auto& devicePort : ports) {
531 const size_t resultSizeBefore = result.size();
532 for (const auto& profile : devicePort.profiles) {
533 combineAudioConfigs(devicePort, profile, &result);
534 if (singleProfile && !result.empty()) {
535 result.resize(1);
536 return result;
537 }
538 }
539 if (resultSizeBefore == result.size()) {
540 std::copy_if(mInitialConfigs.begin(), mInitialConfigs.end(), std::back_inserter(result),
541 [&](const auto& config) { return config.portId == devicePort.id; });
542 if (resultSizeBefore == result.size()) {
543 AudioPortConfig empty;
544 empty.portId = devicePort.id;
545 empty.ext = devicePort.ext;
546 result.push_back(empty);
547 }
548 }
549 if (singleProfile) return result;
550 }
551 return result;
552 }
553
getPort(int32_t portId)554 std::optional<AudioPort> ModuleConfig::getPort(int32_t portId) {
555 auto portsIt = findById(mPorts, portId);
556 return portsIt != mPorts.end() ? std::optional<AudioPort>(*portsIt) : std::nullopt;
557 }
558
onExternalDeviceConnected(IModule * module,const AudioPort & port)559 ndk::ScopedAStatus ModuleConfig::onExternalDeviceConnected(IModule* module, const AudioPort& port) {
560 RETURN_STATUS_IF_ERROR(module->getAudioPorts(&mPorts));
561 RETURN_STATUS_IF_ERROR(module->getAudioRoutes(&mRoutes));
562
563 // Validate port is present in module
564 if (std::find(mPorts.begin(), mPorts.end(), port) == mPorts.end()) {
565 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
566 }
567
568 if (port.flags.getTag() == aidl::android::media::audio::common::AudioIoFlags::Tag::input) {
569 mConnectedExternalSourceDevicePorts.insert(port.id);
570 } else {
571 mConnectedExternalSinkDevicePorts.insert(port.id);
572 }
573 return ndk::ScopedAStatus::ok();
574 }
575
onExternalDeviceDisconnected(IModule * module,const AudioPort & port)576 ndk::ScopedAStatus ModuleConfig::onExternalDeviceDisconnected(IModule* module,
577 const AudioPort& port) {
578 RETURN_STATUS_IF_ERROR(module->getAudioPorts(&mPorts));
579 RETURN_STATUS_IF_ERROR(module->getAudioRoutes(&mRoutes));
580
581 if (port.flags.getTag() == aidl::android::media::audio::common::AudioIoFlags::Tag::input) {
582 mConnectedExternalSourceDevicePorts.erase(port.id);
583 } else {
584 mConnectedExternalSinkDevicePorts.erase(port.id);
585 }
586 return ndk::ScopedAStatus::ok();
587 }
588
isMmapSupported() const589 bool ModuleConfig::isMmapSupported() const {
590 const std::vector<AudioPort> mmapOutMixPorts =
591 getMmapOutMixPorts(false /*connectedOnly*/, false /*singlePort*/);
592 const std::vector<AudioPort> mmapInMixPorts =
593 getMmapInMixPorts(false /*connectedOnly*/, false /*singlePort*/);
594 return !mmapOutMixPorts.empty() || !mmapInMixPorts.empty();
595 }
596