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