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