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