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