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