• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <Utils.h>
21 #include <aidl/android/media/audio/common/AudioInputFlags.h>
22 #include <aidl/android/media/audio/common/AudioIoFlags.h>
23 #include <aidl/android/media/audio/common/AudioOutputFlags.h>
24 
25 #include "ModuleConfig.h"
26 
27 using namespace android;
28 using namespace std::chrono_literals;
29 
30 using aidl::android::hardware::audio::common::isBitPositionFlagSet;
31 using aidl::android::hardware::audio::core::IModule;
32 using aidl::android::media::audio::common::AudioChannelLayout;
33 using aidl::android::media::audio::common::AudioDeviceDescription;
34 using aidl::android::media::audio::common::AudioDeviceType;
35 using aidl::android::media::audio::common::AudioEncapsulationMode;
36 using aidl::android::media::audio::common::AudioFormatDescription;
37 using aidl::android::media::audio::common::AudioFormatType;
38 using aidl::android::media::audio::common::AudioInputFlags;
39 using aidl::android::media::audio::common::AudioIoFlags;
40 using aidl::android::media::audio::common::AudioOffloadInfo;
41 using aidl::android::media::audio::common::AudioOutputFlags;
42 using aidl::android::media::audio::common::AudioPort;
43 using aidl::android::media::audio::common::AudioPortConfig;
44 using aidl::android::media::audio::common::AudioPortExt;
45 using aidl::android::media::audio::common::AudioProfile;
46 using aidl::android::media::audio::common::AudioUsage;
47 using aidl::android::media::audio::common::Int;
48 
49 // static
generateOffloadInfoIfNeeded(const AudioPortConfig & portConfig)50 std::optional<AudioOffloadInfo> ModuleConfig::generateOffloadInfoIfNeeded(
51         const AudioPortConfig& portConfig) {
52     if (portConfig.flags.has_value() &&
53         portConfig.flags.value().getTag() == AudioIoFlags::Tag::output &&
54         isBitPositionFlagSet(portConfig.flags.value().get<AudioIoFlags::Tag::output>(),
55                              AudioOutputFlags::COMPRESS_OFFLOAD)) {
56         AudioOffloadInfo offloadInfo;
57         offloadInfo.base.sampleRate = portConfig.sampleRate.value().value;
58         offloadInfo.base.channelMask = portConfig.channelMask.value();
59         offloadInfo.base.format = portConfig.format.value();
60         offloadInfo.bitRatePerSecond = 256000;                             // Arbitrary value.
61         offloadInfo.durationUs = std::chrono::microseconds(1min).count();  // Arbitrary value.
62         offloadInfo.usage = AudioUsage::MEDIA;
63         offloadInfo.encapsulationMode = AudioEncapsulationMode::NONE;
64         return offloadInfo;
65     }
66     return {};
67 }
68 
69 std::vector<aidl::android::media::audio::common::AudioPort>
getAudioPortsForDeviceTypes(const std::vector<AudioDeviceType> & deviceTypes,const std::string & connection)70 ModuleConfig::getAudioPortsForDeviceTypes(const std::vector<AudioDeviceType>& deviceTypes,
71                                           const std::string& connection) {
72     return getAudioPortsForDeviceTypes(mPorts, deviceTypes, connection);
73 }
74 
75 // static
getBuiltInMicPorts(const std::vector<aidl::android::media::audio::common::AudioPort> & ports)76 std::vector<aidl::android::media::audio::common::AudioPort> ModuleConfig::getBuiltInMicPorts(
77         const std::vector<aidl::android::media::audio::common::AudioPort>& ports) {
78     return getAudioPortsForDeviceTypes(
79             ports, std::vector<AudioDeviceType>{AudioDeviceType::IN_MICROPHONE,
80                                                 AudioDeviceType::IN_MICROPHONE_BACK});
81 }
82 
83 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)84 ModuleConfig::getAudioPortsForDeviceTypes(
85         const std::vector<aidl::android::media::audio::common::AudioPort>& ports,
86         const std::vector<AudioDeviceType>& deviceTypes, const std::string& connection) {
87     std::vector<AudioPort> result;
88     for (const auto& port : ports) {
89         if (port.ext.getTag() != AudioPortExt::Tag::device) continue;
90         const auto type = port.ext.get<AudioPortExt::Tag::device>().device.type;
91         if (type.connection == connection) {
92             for (auto deviceType : deviceTypes) {
93                 if (type.type == deviceType) {
94                     result.push_back(port);
95                 }
96             }
97         }
98     }
99     return result;
100 }
101 
102 template <typename T>
findById(const std::vector<T> & v,int32_t id)103 auto findById(const std::vector<T>& v, int32_t id) {
104     return std::find_if(v.begin(), v.end(), [&](const auto& p) { return p.id == id; });
105 }
106 
ModuleConfig(IModule * module)107 ModuleConfig::ModuleConfig(IModule* module) {
108     mStatus = module->getAudioPorts(&mPorts);
109     if (!mStatus.isOk()) return;
110     for (const auto& port : mPorts) {
111         if (port.ext.getTag() != AudioPortExt::Tag::device) continue;
112         const auto& devicePort = port.ext.get<AudioPortExt::Tag::device>();
113         if (devicePort.device.type.connection.empty()) {
114             const bool isInput = port.flags.getTag() == AudioIoFlags::Tag::input;
115             // Permanently attached device.
116             if (isInput) {
117                 mAttachedSourceDevicePorts.insert(port.id);
118             } else {
119                 mAttachedSinkDevicePorts.insert(port.id);
120             }
121         } else if (devicePort.device.type.connection != AudioDeviceDescription::CONNECTION_VIRTUAL
122                    // The "virtual" connection is used for remote submix which is a dynamic
123                    // device but it can be connected and used w/o external hardware.
124                    && port.profiles.empty()) {
125             mExternalDevicePorts.insert(port.id);
126         }
127     }
128     if (!mStatus.isOk()) return;
129     mStatus = module->getAudioRoutes(&mRoutes);
130     if (!mStatus.isOk()) return;
131     mStatus = module->getAudioPortConfigs(&mInitialConfigs);
132 }
133 
getAttachedDevicePorts() const134 std::vector<AudioPort> ModuleConfig::getAttachedDevicePorts() const {
135     std::vector<AudioPort> result;
136     std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) {
137         return mAttachedSinkDevicePorts.count(port.id) != 0 ||
138                mAttachedSourceDevicePorts.count(port.id) != 0;
139     });
140     return result;
141 }
142 
getConnectedExternalDevicePorts() const143 std::vector<AudioPort> ModuleConfig::getConnectedExternalDevicePorts() const {
144     std::vector<AudioPort> result;
145     std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) {
146         return mConnectedExternalSinkDevicePorts.count(port.id) != 0 ||
147                mConnectedExternalSourceDevicePorts.count(port.id) != 0;
148     });
149     return result;
150 }
151 
getConnectedSinkDevicePorts() const152 std::set<int32_t> ModuleConfig::getConnectedSinkDevicePorts() const {
153     std::set<int32_t> result;
154     result.insert(mAttachedSinkDevicePorts.begin(), mAttachedSinkDevicePorts.end());
155     result.insert(mConnectedExternalSinkDevicePorts.begin(),
156                   mConnectedExternalSinkDevicePorts.end());
157     return result;
158 }
159 
getConnectedSourceDevicePorts() const160 std::set<int32_t> ModuleConfig::getConnectedSourceDevicePorts() const {
161     std::set<int32_t> result;
162     result.insert(mAttachedSourceDevicePorts.begin(), mAttachedSourceDevicePorts.end());
163     result.insert(mConnectedExternalSourceDevicePorts.begin(),
164                   mConnectedExternalSourceDevicePorts.end());
165     return result;
166 }
167 
getExternalDevicePorts() const168 std::vector<AudioPort> ModuleConfig::getExternalDevicePorts() const {
169     std::vector<AudioPort> result;
170     std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result),
171                  [&](const auto& port) { return mExternalDevicePorts.count(port.id) != 0; });
172     return result;
173 }
174 
getInputMixPorts(bool connectedOnly) const175 std::vector<AudioPort> ModuleConfig::getInputMixPorts(bool connectedOnly) const {
176     std::vector<AudioPort> result;
177     std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) {
178         return port.ext.getTag() == AudioPortExt::Tag::mix &&
179                port.flags.getTag() == AudioIoFlags::Tag::input &&
180                (!connectedOnly || !getConnectedSourceDevicesPortsForMixPort(port).empty());
181     });
182     return result;
183 }
184 
getOutputMixPorts(bool connectedOnly) const185 std::vector<AudioPort> ModuleConfig::getOutputMixPorts(bool connectedOnly) const {
186     std::vector<AudioPort> result;
187     std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) {
188         return port.ext.getTag() == AudioPortExt::Tag::mix &&
189                port.flags.getTag() == AudioIoFlags::Tag::output &&
190                (!connectedOnly || !getConnectedSinkDevicesPortsForMixPort(port).empty());
191     });
192     return result;
193 }
194 
getNonBlockingMixPorts(bool connectedOnly,bool singlePort) const195 std::vector<AudioPort> ModuleConfig::getNonBlockingMixPorts(bool connectedOnly,
196                                                             bool singlePort) const {
197     return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) {
198         return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
199                                     AudioOutputFlags::NON_BLOCKING);
200     });
201 }
202 
getOffloadMixPorts(bool connectedOnly,bool singlePort) const203 std::vector<AudioPort> ModuleConfig::getOffloadMixPorts(bool connectedOnly, bool singlePort) const {
204     return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) {
205         return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
206                                     AudioOutputFlags::COMPRESS_OFFLOAD);
207     });
208 }
209 
getPrimaryMixPorts(bool connectedOnly,bool singlePort) const210 std::vector<AudioPort> ModuleConfig::getPrimaryMixPorts(bool connectedOnly, bool singlePort) const {
211     return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) {
212         return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
213                                     AudioOutputFlags::PRIMARY);
214     });
215 }
216 
getMmapOutMixPorts(bool connectedOnly,bool singlePort) const217 std::vector<AudioPort> ModuleConfig::getMmapOutMixPorts(bool connectedOnly, bool singlePort) const {
218     return findMixPorts(false /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) {
219         return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
220                                     AudioOutputFlags::MMAP_NOIRQ);
221     });
222 }
223 
getMmapInMixPorts(bool connectedOnly,bool singlePort) const224 std::vector<AudioPort> ModuleConfig::getMmapInMixPorts(bool connectedOnly, bool singlePort) const {
225     return findMixPorts(true /*isInput*/, connectedOnly, singlePort, [&](const AudioPort& port) {
226         return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
227                                     AudioInputFlags::MMAP_NOIRQ);
228     });
229 }
230 
getConnectedDevicesPortsForMixPort(bool isInput,const AudioPortConfig & mixPortConfig) const231 std::vector<AudioPort> ModuleConfig::getConnectedDevicesPortsForMixPort(
232         bool isInput, const AudioPortConfig& mixPortConfig) const {
233     const auto mixPortIt = findById<AudioPort>(mPorts, mixPortConfig.portId);
234     if (mixPortIt != mPorts.end()) {
235         return getConnectedDevicesPortsForMixPort(isInput, *mixPortIt);
236     }
237     return {};
238 }
239 
getConnectedSinkDevicesPortsForMixPort(const AudioPort & mixPort) const240 std::vector<AudioPort> ModuleConfig::getConnectedSinkDevicesPortsForMixPort(
241         const AudioPort& mixPort) const {
242     std::vector<AudioPort> result;
243     std::set<int32_t> connectedSinkDevicePorts = getConnectedSinkDevicePorts();
244     for (const auto& route : mRoutes) {
245         if ((connectedSinkDevicePorts.count(route.sinkPortId) != 0) &&
246             std::find(route.sourcePortIds.begin(), route.sourcePortIds.end(), mixPort.id) !=
247                     route.sourcePortIds.end()) {
248             const auto devicePortIt = findById<AudioPort>(mPorts, route.sinkPortId);
249             if (devicePortIt != mPorts.end()) result.push_back(*devicePortIt);
250         }
251     }
252     return result;
253 }
254 
getConnectedSourceDevicesPortsForMixPort(const AudioPort & mixPort) const255 std::vector<AudioPort> ModuleConfig::getConnectedSourceDevicesPortsForMixPort(
256         const AudioPort& mixPort) const {
257     std::vector<AudioPort> result;
258     std::set<int32_t> connectedSourceDevicePorts = getConnectedSourceDevicePorts();
259     for (const auto& route : mRoutes) {
260         if (route.sinkPortId == mixPort.id) {
261             for (const auto srcId : route.sourcePortIds) {
262                 if (connectedSourceDevicePorts.count(srcId) != 0) {
263                     const auto devicePortIt = findById<AudioPort>(mPorts, srcId);
264                     if (devicePortIt != mPorts.end()) result.push_back(*devicePortIt);
265                 }
266             }
267         }
268     }
269     return result;
270 }
271 
getSourceMixPortForConnectedDevice() const272 std::optional<AudioPort> ModuleConfig::getSourceMixPortForConnectedDevice() const {
273     std::set<int32_t> connectedSinkDevicePorts = getConnectedSinkDevicePorts();
274     for (const auto& route : mRoutes) {
275         if (connectedSinkDevicePorts.count(route.sinkPortId) != 0) {
276             const auto mixPortIt = findById<AudioPort>(mPorts, route.sourcePortIds[0]);
277             if (mixPortIt != mPorts.end()) return *mixPortIt;
278         }
279     }
280     return {};
281 }
282 
getNonRoutableSrcSinkPair(bool isInput) const283 std::optional<ModuleConfig::SrcSinkPair> ModuleConfig::getNonRoutableSrcSinkPair(
284         bool isInput) const {
285     const auto mixPorts = getMixPorts(isInput, false /*connectedOnly*/);
286     std::set<std::pair<int32_t, int32_t>> allowedRoutes;
287     for (const auto& route : mRoutes) {
288         for (const auto srcPortId : route.sourcePortIds) {
289             allowedRoutes.emplace(std::make_pair(srcPortId, route.sinkPortId));
290         }
291     }
292     auto make_pair = [isInput](auto& device, auto& mix) {
293         return isInput ? std::make_pair(device, mix) : std::make_pair(mix, device);
294     };
295     for (const auto portId :
296          isInput ? getConnectedSourceDevicePorts() : getConnectedSinkDevicePorts()) {
297         const auto devicePortIt = findById<AudioPort>(mPorts, portId);
298         if (devicePortIt == mPorts.end()) continue;
299         auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt);
300         for (const auto& mixPort : mixPorts) {
301             if (std::find(allowedRoutes.begin(), allowedRoutes.end(),
302                           make_pair(portId, mixPort.id)) == allowedRoutes.end()) {
303                 auto mixPortConfig = getSingleConfigForMixPort(isInput, mixPort);
304                 if (mixPortConfig.has_value()) {
305                     return make_pair(devicePortConfig, mixPortConfig.value());
306                 }
307             }
308         }
309     }
310     return {};
311 }
312 
getRoutableSrcSinkPair(bool isInput) const313 std::optional<ModuleConfig::SrcSinkPair> ModuleConfig::getRoutableSrcSinkPair(bool isInput) const {
314     if (isInput) {
315         std::set<int32_t> connectedSourceDevicePorts = getConnectedSourceDevicePorts();
316         for (const auto& route : mRoutes) {
317             auto srcPortIdIt = std::find_if(
318                     route.sourcePortIds.begin(), route.sourcePortIds.end(),
319                     [&](const auto& portId) { return connectedSourceDevicePorts.count(portId); });
320             if (srcPortIdIt == route.sourcePortIds.end()) continue;
321             const auto devicePortIt = findById<AudioPort>(mPorts, *srcPortIdIt);
322             const auto mixPortIt = findById<AudioPort>(mPorts, route.sinkPortId);
323             if (devicePortIt == mPorts.end() || mixPortIt == mPorts.end()) continue;
324             auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt);
325             auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt);
326             if (!mixPortConfig.has_value()) continue;
327             return std::make_pair(devicePortConfig, mixPortConfig.value());
328         }
329     } else {
330         std::set<int32_t> connectedSinkDevicePorts = getConnectedSinkDevicePorts();
331         for (const auto& route : mRoutes) {
332             if (connectedSinkDevicePorts.count(route.sinkPortId) == 0) continue;
333             const auto mixPortIt = findById<AudioPort>(mPorts, route.sourcePortIds[0]);
334             const auto devicePortIt = findById<AudioPort>(mPorts, route.sinkPortId);
335             if (devicePortIt == mPorts.end() || mixPortIt == mPorts.end()) continue;
336             auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt);
337             auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt);
338             if (!mixPortConfig.has_value()) continue;
339             return std::make_pair(mixPortConfig.value(), devicePortConfig);
340         }
341     }
342     return {};
343 }
344 
getRoutableSrcSinkGroups(bool isInput) const345 std::vector<ModuleConfig::SrcSinkGroup> ModuleConfig::getRoutableSrcSinkGroups(bool isInput) const {
346     std::vector<SrcSinkGroup> result;
347     if (isInput) {
348         std::set<int32_t> connectedSourceDevicePorts = getConnectedSourceDevicePorts();
349         for (const auto& route : mRoutes) {
350             std::vector<int32_t> srcPortIds;
351             std::copy_if(route.sourcePortIds.begin(), route.sourcePortIds.end(),
352                          std::back_inserter(srcPortIds), [&](const auto& portId) {
353                              return connectedSourceDevicePorts.count(portId);
354                          });
355             if (srcPortIds.empty()) continue;
356             const auto mixPortIt = findById<AudioPort>(mPorts, route.sinkPortId);
357             if (mixPortIt == mPorts.end()) continue;
358             auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt);
359             if (!mixPortConfig.has_value()) continue;
360             std::vector<SrcSinkPair> pairs;
361             for (const auto srcPortId : srcPortIds) {
362                 const auto devicePortIt = findById<AudioPort>(mPorts, srcPortId);
363                 if (devicePortIt == mPorts.end()) continue;
364                 // Using all configs for every source would be too much.
365                 auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt);
366                 pairs.emplace_back(devicePortConfig, mixPortConfig.value());
367             }
368             if (!pairs.empty()) {
369                 result.emplace_back(route, std::move(pairs));
370             }
371         }
372     } else {
373         std::set<int32_t> connectedSinkDevicePorts = getConnectedSinkDevicePorts();
374         for (const auto& route : mRoutes) {
375             if (connectedSinkDevicePorts.count(route.sinkPortId) == 0) continue;
376             const auto devicePortIt = findById<AudioPort>(mPorts, route.sinkPortId);
377             if (devicePortIt == mPorts.end()) continue;
378             auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt);
379             std::vector<SrcSinkPair> pairs;
380             for (const auto srcPortId : route.sourcePortIds) {
381                 const auto mixPortIt = findById<AudioPort>(mPorts, srcPortId);
382                 if (mixPortIt == mPorts.end()) continue;
383                 // Using all configs for every source would be too much.
384                 auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt);
385                 if (mixPortConfig.has_value()) {
386                     pairs.emplace_back(mixPortConfig.value(), devicePortConfig);
387                 }
388             }
389             if (!pairs.empty()) {
390                 result.emplace_back(route, std::move(pairs));
391             }
392         }
393     }
394     return result;
395 }
396 
toString() const397 std::string ModuleConfig::toString() const {
398     std::string result;
399     result.append("Ports: ");
400     result.append(android::internal::ToString(mPorts));
401     result.append("\nInitial configs: ");
402     result.append(android::internal::ToString(mInitialConfigs));
403     result.append("\nAttached sink device ports: ");
404     result.append(android::internal::ToString(mAttachedSinkDevicePorts));
405     result.append("\nAttached source device ports: ");
406     result.append(android::internal::ToString(mAttachedSourceDevicePorts));
407     result.append("\nExternal device ports: ");
408     result.append(android::internal::ToString(mExternalDevicePorts));
409     result.append("\nConnected external device ports: ");
410     result.append(android::internal::ToString(getConnectedExternalDevicePorts()));
411     result.append("\nRoutes: ");
412     result.append(android::internal::ToString(mRoutes));
413     return result;
414 }
415 
combineAudioConfigs(const AudioPort & port,const AudioProfile & profile,std::vector<AudioPortConfig> * result)416 static size_t combineAudioConfigs(const AudioPort& port, const AudioProfile& profile,
417                                   std::vector<AudioPortConfig>* result) {
418     const size_t newConfigCount = profile.channelMasks.size() * profile.sampleRates.size();
419     result->reserve(result->capacity() + newConfigCount);
420     for (auto channelMask : profile.channelMasks) {
421         for (auto sampleRate : profile.sampleRates) {
422             AudioPortConfig config{};
423             config.portId = port.id;
424             Int sr;
425             sr.value = sampleRate;
426             config.sampleRate = sr;
427             config.channelMask = channelMask;
428             config.format = profile.format;
429             config.flags = port.flags;
430             config.ext = port.ext;
431             result->push_back(std::move(config));
432         }
433     }
434     return newConfigCount;
435 }
436 
isDynamicProfile(const AudioProfile & profile)437 static bool isDynamicProfile(const AudioProfile& profile) {
438     return (profile.format.type == AudioFormatType::DEFAULT && profile.format.encoding.empty()) ||
439            profile.sampleRates.empty() || profile.channelMasks.empty();
440 }
441 
findMixPorts(bool isInput,bool connectedOnly,bool singlePort,const std::function<bool (const AudioPort &)> & pred) const442 std::vector<AudioPort> ModuleConfig::findMixPorts(
443         bool isInput, bool connectedOnly, bool singlePort,
444         const std::function<bool(const AudioPort&)>& pred) const {
445     std::vector<AudioPort> result;
446     const auto mixPorts = getMixPorts(isInput, connectedOnly);
447     for (auto mixPortIt = mixPorts.begin(); mixPortIt != mixPorts.end();) {
448         mixPortIt = std::find_if(mixPortIt, mixPorts.end(), pred);
449         if (mixPortIt == mixPorts.end()) break;
450         result.push_back(*mixPortIt++);
451         if (singlePort) break;
452     }
453     return result;
454 }
455 
generateAudioMixPortConfigs(const std::vector<AudioPort> & ports,bool isInput,bool singleProfile) const456 std::vector<AudioPortConfig> ModuleConfig::generateAudioMixPortConfigs(
457         const std::vector<AudioPort>& ports, bool isInput, bool singleProfile) const {
458     std::vector<AudioPortConfig> result;
459     for (const auto& mixPort : ports) {
460         if (getConnectedDevicesPortsForMixPort(isInput, mixPort).empty()) {
461             continue;
462         }
463         for (const auto& profile : mixPort.profiles) {
464             if (isDynamicProfile(profile)) continue;
465             combineAudioConfigs(mixPort, profile, &result);
466             if (singleProfile && !result.empty()) {
467                 result.resize(1);
468                 return result;
469             }
470         }
471     }
472     return result;
473 }
474 
generateAudioDevicePortConfigs(const std::vector<AudioPort> & ports,bool singleProfile) const475 std::vector<AudioPortConfig> ModuleConfig::generateAudioDevicePortConfigs(
476         const std::vector<AudioPort>& ports, bool singleProfile) const {
477     std::vector<AudioPortConfig> result;
478     for (const auto& devicePort : ports) {
479         const size_t resultSizeBefore = result.size();
480         for (const auto& profile : devicePort.profiles) {
481             combineAudioConfigs(devicePort, profile, &result);
482             if (singleProfile && !result.empty()) {
483                 result.resize(1);
484                 return result;
485             }
486         }
487         if (resultSizeBefore == result.size()) {
488             std::copy_if(mInitialConfigs.begin(), mInitialConfigs.end(), std::back_inserter(result),
489                          [&](const auto& config) { return config.portId == devicePort.id; });
490             if (resultSizeBefore == result.size()) {
491                 AudioPortConfig empty;
492                 empty.portId = devicePort.id;
493                 empty.ext = devicePort.ext;
494                 result.push_back(empty);
495             }
496         }
497         if (singleProfile) return result;
498     }
499     return result;
500 }
501 
onExternalDeviceConnected(IModule * module,const AudioPort & port)502 const ndk::ScopedAStatus& ModuleConfig::onExternalDeviceConnected(IModule* module,
503                                                                   const AudioPort& port) {
504     // Update ports and routes
505     mStatus = module->getAudioPorts(&mPorts);
506     if (!mStatus.isOk()) return mStatus;
507     mStatus = module->getAudioRoutes(&mRoutes);
508     if (!mStatus.isOk()) return mStatus;
509 
510     // Validate port is present in module
511     if (std::find(mPorts.begin(), mPorts.end(), port) == mPorts.end()) {
512         mStatus = ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
513         return mStatus;
514     }
515 
516     if (port.flags.getTag() == aidl::android::media::audio::common::AudioIoFlags::Tag::input) {
517         mConnectedExternalSourceDevicePorts.insert(port.id);
518     } else {
519         mConnectedExternalSinkDevicePorts.insert(port.id);
520     }
521     return mStatus;
522 }
523 
onExternalDeviceDisconnected(IModule * module,const AudioPort & port)524 const ndk::ScopedAStatus& ModuleConfig::onExternalDeviceDisconnected(IModule* module,
525                                                                      const AudioPort& port) {
526     // Update ports and routes
527     mStatus = module->getAudioPorts(&mPorts);
528     if (!mStatus.isOk()) return mStatus;
529     mStatus = module->getAudioRoutes(&mRoutes);
530     if (!mStatus.isOk()) return mStatus;
531 
532     if (port.flags.getTag() == aidl::android::media::audio::common::AudioIoFlags::Tag::input) {
533         mConnectedExternalSourceDevicePorts.erase(port.id);
534     } else {
535         mConnectedExternalSinkDevicePorts.erase(port.id);
536     }
537     return mStatus;
538 }
539 
isMmapSupported() const540 bool ModuleConfig::isMmapSupported() const {
541     const std::vector<AudioPort> mmapOutMixPorts =
542             getMmapOutMixPorts(false /*connectedOnly*/, false /*singlePort*/);
543     const std::vector<AudioPort> mmapInMixPorts =
544             getMmapInMixPorts(false /*connectedOnly*/, false /*singlePort*/);
545     return !mmapOutMixPorts.empty() || !mmapInMixPorts.empty();
546 }
547