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