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