Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 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> |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 18 | #include <chrono> |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 19 | |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 20 | #include <Utils.h> |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 21 | #include <aidl/android/media/audio/common/AudioIoFlags.h> |
| 22 | #include <aidl/android/media/audio/common/AudioOutputFlags.h> |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 23 | |
| 24 | #include "ModuleConfig.h" |
| 25 | |
| 26 | using namespace android; |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 27 | using namespace std::chrono_literals; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 28 | |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 29 | using aidl::android::hardware::audio::core::IModule; |
| 30 | using aidl::android::media::audio::common::AudioChannelLayout; |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 31 | using aidl::android::media::audio::common::AudioEncapsulationMode; |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 32 | using aidl::android::media::audio::common::AudioFormatDescription; |
| 33 | using aidl::android::media::audio::common::AudioFormatType; |
| 34 | using aidl::android::media::audio::common::AudioIoFlags; |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 35 | using aidl::android::media::audio::common::AudioOffloadInfo; |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 36 | using aidl::android::media::audio::common::AudioOutputFlags; |
| 37 | using aidl::android::media::audio::common::AudioPort; |
| 38 | using aidl::android::media::audio::common::AudioPortConfig; |
| 39 | using aidl::android::media::audio::common::AudioPortExt; |
| 40 | using aidl::android::media::audio::common::AudioProfile; |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 41 | using aidl::android::media::audio::common::AudioUsage; |
Mikhail Naganov | f84d640 | 2022-06-16 00:35:31 +0000 | [diff] [blame] | 42 | using aidl::android::media::audio::common::Int; |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 43 | using android::hardware::audio::common::isBitPositionFlagSet; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 44 | |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 45 | // static |
| 46 | std::optional<AudioOffloadInfo> ModuleConfig::generateOffloadInfoIfNeeded( |
| 47 | const AudioPortConfig& portConfig) { |
| 48 | if (portConfig.flags.has_value() && |
| 49 | portConfig.flags.value().getTag() == AudioIoFlags::Tag::output && |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 50 | isBitPositionFlagSet(portConfig.flags.value().get<AudioIoFlags::Tag::output>(), |
| 51 | AudioOutputFlags::COMPRESS_OFFLOAD)) { |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 52 | AudioOffloadInfo offloadInfo; |
| 53 | offloadInfo.base.sampleRate = portConfig.sampleRate.value().value; |
| 54 | offloadInfo.base.channelMask = portConfig.channelMask.value(); |
| 55 | offloadInfo.base.format = portConfig.format.value(); |
| 56 | offloadInfo.bitRatePerSecond = 256; // Arbitrary value. |
| 57 | offloadInfo.durationUs = std::chrono::microseconds(1min).count(); // Arbitrary value. |
| 58 | offloadInfo.usage = AudioUsage::MEDIA; |
| 59 | offloadInfo.encapsulationMode = AudioEncapsulationMode::NONE; |
| 60 | return offloadInfo; |
| 61 | } |
| 62 | return {}; |
| 63 | } |
| 64 | |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 65 | template <typename T> |
| 66 | auto findById(const std::vector<T>& v, int32_t id) { |
| 67 | return std::find_if(v.begin(), v.end(), [&](const auto& p) { return p.id == id; }); |
| 68 | } |
| 69 | |
| 70 | ModuleConfig::ModuleConfig(IModule* module) { |
| 71 | mStatus = module->getAudioPorts(&mPorts); |
| 72 | if (!mStatus.isOk()) return; |
| 73 | for (const auto& port : mPorts) { |
| 74 | if (port.ext.getTag() != AudioPortExt::Tag::device) continue; |
| 75 | const auto& devicePort = port.ext.get<AudioPortExt::Tag::device>(); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 76 | if (devicePort.device.type.connection.empty()) { |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 77 | const bool isInput = port.flags.getTag() == AudioIoFlags::Tag::input; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 78 | // Permanently attached device. |
| 79 | if (isInput) { |
| 80 | mAttachedSourceDevicePorts.insert(port.id); |
| 81 | } else { |
| 82 | mAttachedSinkDevicePorts.insert(port.id); |
| 83 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 84 | } else if (port.profiles.empty()) { |
| 85 | mExternalDevicePorts.insert(port.id); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | if (!mStatus.isOk()) return; |
| 89 | mStatus = module->getAudioRoutes(&mRoutes); |
| 90 | if (!mStatus.isOk()) return; |
| 91 | mStatus = module->getAudioPortConfigs(&mInitialConfigs); |
| 92 | } |
| 93 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 94 | std::vector<AudioPort> ModuleConfig::getAttachedDevicePorts() const { |
| 95 | std::vector<AudioPort> result; |
| 96 | std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [&](const auto& port) { |
| 97 | return mAttachedSinkDevicePorts.count(port.id) != 0 || |
| 98 | mAttachedSourceDevicePorts.count(port.id) != 0; |
| 99 | }); |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | std::vector<AudioPort> ModuleConfig::getExternalDevicePorts() const { |
| 104 | std::vector<AudioPort> result; |
| 105 | std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), |
| 106 | [&](const auto& port) { return mExternalDevicePorts.count(port.id) != 0; }); |
| 107 | return result; |
| 108 | } |
| 109 | |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 110 | std::vector<AudioPort> ModuleConfig::getInputMixPorts() const { |
| 111 | std::vector<AudioPort> result; |
| 112 | std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [](const auto& port) { |
| 113 | return port.ext.getTag() == AudioPortExt::Tag::mix && |
| 114 | port.flags.getTag() == AudioIoFlags::Tag::input; |
| 115 | }); |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | std::vector<AudioPort> ModuleConfig::getOutputMixPorts() const { |
| 120 | std::vector<AudioPort> result; |
| 121 | std::copy_if(mPorts.begin(), mPorts.end(), std::back_inserter(result), [](const auto& port) { |
| 122 | return port.ext.getTag() == AudioPortExt::Tag::mix && |
| 123 | port.flags.getTag() == AudioIoFlags::Tag::output; |
| 124 | }); |
| 125 | return result; |
| 126 | } |
| 127 | |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 128 | std::vector<AudioPort> ModuleConfig::getOffloadMixPorts(bool attachedOnly, bool singlePort) const { |
| 129 | std::vector<AudioPort> result; |
| 130 | const auto mixPorts = getMixPorts(false /*isInput*/); |
| 131 | auto offloadPortIt = mixPorts.begin(); |
| 132 | while (offloadPortIt != mixPorts.end()) { |
| 133 | offloadPortIt = std::find_if(offloadPortIt, mixPorts.end(), [&](const AudioPort& port) { |
| 134 | return isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(), |
| 135 | AudioOutputFlags::COMPRESS_OFFLOAD) && |
| 136 | (!attachedOnly || !getAttachedSinkDevicesPortsForMixPort(port).empty()); |
| 137 | }); |
| 138 | if (offloadPortIt == mixPorts.end()) break; |
| 139 | result.push_back(*offloadPortIt++); |
| 140 | if (singlePort) break; |
| 141 | } |
| 142 | return result; |
| 143 | } |
| 144 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 145 | std::vector<AudioPort> ModuleConfig::getAttachedDevicesPortsForMixPort( |
| 146 | bool isInput, const AudioPortConfig& mixPortConfig) const { |
| 147 | const auto mixPortIt = findById<AudioPort>(mPorts, mixPortConfig.portId); |
| 148 | if (mixPortIt != mPorts.end()) { |
| 149 | return getAttachedDevicesPortsForMixPort(isInput, *mixPortIt); |
| 150 | } |
| 151 | return {}; |
| 152 | } |
| 153 | |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 154 | std::vector<AudioPort> ModuleConfig::getAttachedSinkDevicesPortsForMixPort( |
| 155 | const AudioPort& mixPort) const { |
| 156 | std::vector<AudioPort> result; |
| 157 | for (const auto& route : mRoutes) { |
| 158 | if (mAttachedSinkDevicePorts.count(route.sinkPortId) != 0 && |
| 159 | std::find(route.sourcePortIds.begin(), route.sourcePortIds.end(), mixPort.id) != |
| 160 | route.sourcePortIds.end()) { |
| 161 | const auto devicePortIt = findById<AudioPort>(mPorts, route.sinkPortId); |
| 162 | if (devicePortIt != mPorts.end()) result.push_back(*devicePortIt); |
| 163 | } |
| 164 | } |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | std::vector<AudioPort> ModuleConfig::getAttachedSourceDevicesPortsForMixPort( |
| 169 | const AudioPort& mixPort) const { |
| 170 | std::vector<AudioPort> result; |
| 171 | for (const auto& route : mRoutes) { |
| 172 | if (route.sinkPortId == mixPort.id) { |
| 173 | for (const auto srcId : route.sourcePortIds) { |
| 174 | if (mAttachedSourceDevicePorts.count(srcId) != 0) { |
| 175 | const auto devicePortIt = findById<AudioPort>(mPorts, srcId); |
| 176 | if (devicePortIt != mPorts.end()) result.push_back(*devicePortIt); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | std::optional<AudioPort> ModuleConfig::getSourceMixPortForAttachedDevice() const { |
| 185 | for (const auto& route : mRoutes) { |
| 186 | if (mAttachedSinkDevicePorts.count(route.sinkPortId) != 0) { |
| 187 | const auto mixPortIt = findById<AudioPort>(mPorts, route.sourcePortIds[0]); |
| 188 | if (mixPortIt != mPorts.end()) return *mixPortIt; |
| 189 | } |
| 190 | } |
| 191 | return {}; |
| 192 | } |
| 193 | |
| 194 | std::optional<ModuleConfig::SrcSinkPair> ModuleConfig::getNonRoutableSrcSinkPair( |
| 195 | bool isInput) const { |
| 196 | const auto mixPorts = getMixPorts(isInput); |
| 197 | std::set<std::pair<int32_t, int32_t>> allowedRoutes; |
| 198 | for (const auto& route : mRoutes) { |
| 199 | for (const auto srcPortId : route.sourcePortIds) { |
| 200 | allowedRoutes.emplace(std::make_pair(srcPortId, route.sinkPortId)); |
| 201 | } |
| 202 | } |
| 203 | auto make_pair = [isInput](auto& device, auto& mix) { |
| 204 | return isInput ? std::make_pair(device, mix) : std::make_pair(mix, device); |
| 205 | }; |
| 206 | for (const auto portId : isInput ? mAttachedSourceDevicePorts : mAttachedSinkDevicePorts) { |
| 207 | const auto devicePortIt = findById<AudioPort>(mPorts, portId); |
| 208 | if (devicePortIt == mPorts.end()) continue; |
| 209 | auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt); |
| 210 | for (const auto& mixPort : mixPorts) { |
| 211 | if (std::find(allowedRoutes.begin(), allowedRoutes.end(), |
| 212 | make_pair(portId, mixPort.id)) == allowedRoutes.end()) { |
| 213 | auto mixPortConfig = getSingleConfigForMixPort(isInput, mixPort); |
| 214 | if (mixPortConfig.has_value()) { |
| 215 | return make_pair(devicePortConfig, mixPortConfig.value()); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | return {}; |
| 221 | } |
| 222 | |
| 223 | std::optional<ModuleConfig::SrcSinkPair> ModuleConfig::getRoutableSrcSinkPair(bool isInput) const { |
| 224 | if (isInput) { |
| 225 | for (const auto& route : mRoutes) { |
| 226 | auto srcPortIdIt = std::find_if( |
| 227 | route.sourcePortIds.begin(), route.sourcePortIds.end(), |
| 228 | [&](const auto& portId) { return mAttachedSourceDevicePorts.count(portId); }); |
| 229 | if (srcPortIdIt == route.sourcePortIds.end()) continue; |
| 230 | const auto devicePortIt = findById<AudioPort>(mPorts, *srcPortIdIt); |
| 231 | const auto mixPortIt = findById<AudioPort>(mPorts, route.sinkPortId); |
| 232 | if (devicePortIt == mPorts.end() || mixPortIt == mPorts.end()) continue; |
| 233 | auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt); |
| 234 | auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt); |
| 235 | if (!mixPortConfig.has_value()) continue; |
| 236 | return std::make_pair(devicePortConfig, mixPortConfig.value()); |
| 237 | } |
| 238 | } else { |
| 239 | for (const auto& route : mRoutes) { |
| 240 | if (mAttachedSinkDevicePorts.count(route.sinkPortId) == 0) continue; |
| 241 | const auto mixPortIt = findById<AudioPort>(mPorts, route.sourcePortIds[0]); |
| 242 | const auto devicePortIt = findById<AudioPort>(mPorts, route.sinkPortId); |
| 243 | if (devicePortIt == mPorts.end() || mixPortIt == mPorts.end()) continue; |
| 244 | auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt); |
| 245 | auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt); |
| 246 | if (!mixPortConfig.has_value()) continue; |
| 247 | return std::make_pair(mixPortConfig.value(), devicePortConfig); |
| 248 | } |
| 249 | } |
| 250 | return {}; |
| 251 | } |
| 252 | |
| 253 | std::vector<ModuleConfig::SrcSinkGroup> ModuleConfig::getRoutableSrcSinkGroups(bool isInput) const { |
| 254 | std::vector<SrcSinkGroup> result; |
| 255 | if (isInput) { |
| 256 | for (const auto& route : mRoutes) { |
| 257 | std::vector<int32_t> srcPortIds; |
| 258 | std::copy_if(route.sourcePortIds.begin(), route.sourcePortIds.end(), |
| 259 | std::back_inserter(srcPortIds), [&](const auto& portId) { |
| 260 | return mAttachedSourceDevicePorts.count(portId); |
| 261 | }); |
| 262 | if (srcPortIds.empty()) continue; |
| 263 | const auto mixPortIt = findById<AudioPort>(mPorts, route.sinkPortId); |
| 264 | if (mixPortIt == mPorts.end()) continue; |
| 265 | auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt); |
| 266 | if (!mixPortConfig.has_value()) continue; |
| 267 | std::vector<SrcSinkPair> pairs; |
| 268 | for (const auto srcPortId : srcPortIds) { |
| 269 | const auto devicePortIt = findById<AudioPort>(mPorts, srcPortId); |
| 270 | if (devicePortIt == mPorts.end()) continue; |
| 271 | // Using all configs for every source would be too much. |
| 272 | auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt); |
| 273 | pairs.emplace_back(devicePortConfig, mixPortConfig.value()); |
| 274 | } |
| 275 | if (!pairs.empty()) { |
| 276 | result.emplace_back(route, std::move(pairs)); |
| 277 | } |
| 278 | } |
| 279 | } else { |
| 280 | for (const auto& route : mRoutes) { |
| 281 | if (mAttachedSinkDevicePorts.count(route.sinkPortId) == 0) continue; |
| 282 | const auto devicePortIt = findById<AudioPort>(mPorts, route.sinkPortId); |
| 283 | if (devicePortIt == mPorts.end()) continue; |
| 284 | auto devicePortConfig = getSingleConfigForDevicePort(*devicePortIt); |
| 285 | std::vector<SrcSinkPair> pairs; |
| 286 | for (const auto srcPortId : route.sourcePortIds) { |
| 287 | const auto mixPortIt = findById<AudioPort>(mPorts, srcPortId); |
| 288 | if (mixPortIt == mPorts.end()) continue; |
| 289 | // Using all configs for every source would be too much. |
| 290 | auto mixPortConfig = getSingleConfigForMixPort(isInput, *mixPortIt); |
| 291 | if (mixPortConfig.has_value()) { |
| 292 | pairs.emplace_back(mixPortConfig.value(), devicePortConfig); |
| 293 | } |
| 294 | } |
| 295 | if (!pairs.empty()) { |
| 296 | result.emplace_back(route, std::move(pairs)); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | return result; |
| 301 | } |
| 302 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 303 | std::string ModuleConfig::toString() const { |
| 304 | std::string result; |
| 305 | result.append("Ports: "); |
| 306 | result.append(android::internal::ToString(mPorts)); |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 307 | result.append("\nInitial configs: "); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 308 | result.append(android::internal::ToString(mInitialConfigs)); |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 309 | result.append("\nAttached sink device ports: "); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 310 | result.append(android::internal::ToString(mAttachedSinkDevicePorts)); |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 311 | result.append("\nAttached source device ports: "); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 312 | result.append(android::internal::ToString(mAttachedSourceDevicePorts)); |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 313 | result.append("\nExternal device ports: "); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 314 | result.append(android::internal::ToString(mExternalDevicePorts)); |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 315 | result.append("\nRoutes: "); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 316 | result.append(android::internal::ToString(mRoutes)); |
| 317 | return result; |
| 318 | } |
| 319 | |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 320 | static size_t combineAudioConfigs(const AudioPort& port, const AudioProfile& profile, |
| 321 | std::vector<AudioPortConfig>* result) { |
| 322 | const size_t newConfigCount = profile.channelMasks.size() * profile.sampleRates.size(); |
| 323 | result->reserve(result->capacity() + newConfigCount); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 324 | for (auto channelMask : profile.channelMasks) { |
| 325 | for (auto sampleRate : profile.sampleRates) { |
| 326 | AudioPortConfig config{}; |
| 327 | config.portId = port.id; |
| 328 | Int sr; |
| 329 | sr.value = sampleRate; |
| 330 | config.sampleRate = sr; |
| 331 | config.channelMask = channelMask; |
| 332 | config.format = profile.format; |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 333 | config.flags = port.flags; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 334 | config.ext = port.ext; |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 335 | result->push_back(std::move(config)); |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 338 | return newConfigCount; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 341 | static bool isDynamicProfile(const AudioProfile& profile) { |
| 342 | return (profile.format.type == AudioFormatType::DEFAULT && profile.format.encoding.empty()) || |
| 343 | profile.sampleRates.empty() || profile.channelMasks.empty(); |
| 344 | } |
| 345 | |
| 346 | std::vector<AudioPortConfig> ModuleConfig::generateAudioMixPortConfigs( |
| 347 | const std::vector<AudioPort>& ports, bool isInput, bool singleProfile) const { |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 348 | std::vector<AudioPortConfig> result; |
| 349 | for (const auto& mixPort : ports) { |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 350 | if (getAttachedDevicesPortsForMixPort(isInput, mixPort).empty()) { |
| 351 | continue; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 352 | } |
| 353 | for (const auto& profile : mixPort.profiles) { |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 354 | if (isDynamicProfile(profile)) continue; |
| 355 | combineAudioConfigs(mixPort, profile, &result); |
| 356 | if (singleProfile && !result.empty()) { |
| 357 | result.resize(1); |
| 358 | return result; |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | } |
| 362 | return result; |
| 363 | } |
| 364 | |
| 365 | std::vector<AudioPortConfig> ModuleConfig::generateAudioDevicePortConfigs( |
| 366 | const std::vector<AudioPort>& ports, bool singleProfile) const { |
| 367 | std::vector<AudioPortConfig> result; |
| 368 | for (const auto& devicePort : ports) { |
| 369 | const size_t resultSizeBefore = result.size(); |
| 370 | for (const auto& profile : devicePort.profiles) { |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 371 | combineAudioConfigs(devicePort, profile, &result); |
| 372 | if (singleProfile && !result.empty()) { |
| 373 | result.resize(1); |
| 374 | return result; |
| 375 | } |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 376 | } |
| 377 | if (resultSizeBefore == result.size()) { |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 378 | std::copy_if(mInitialConfigs.begin(), mInitialConfigs.end(), std::back_inserter(result), |
| 379 | [&](const auto& config) { return config.portId == devicePort.id; }); |
| 380 | if (resultSizeBefore == result.size()) { |
| 381 | AudioPortConfig empty; |
| 382 | empty.portId = devicePort.id; |
| 383 | empty.ext = devicePort.ext; |
| 384 | result.push_back(empty); |
| 385 | } |
Mikhail Naganov | e5d747e | 2021-11-16 01:31:03 +0000 | [diff] [blame] | 386 | } |
| 387 | if (singleProfile) return result; |
| 388 | } |
| 389 | return result; |
| 390 | } |