Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [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 | #define LOG_TAG "Hal2AidlMapper" |
| 18 | // #define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <algorithm> |
| 21 | |
| 22 | #include <media/audiohal/StreamHalInterface.h> |
| 23 | #include <error/expected_utils.h> |
| 24 | #include <system/audio.h> // For AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS |
| 25 | #include <Utils.h> |
| 26 | #include <utils/Log.h> |
| 27 | |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 28 | #include "AidlUtils.h" |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 29 | #include "Hal2AidlMapper.h" |
| 30 | |
| 31 | using aidl::android::aidl_utils::statusTFromBinderStatus; |
| 32 | using aidl::android::media::audio::common::AudioChannelLayout; |
| 33 | using aidl::android::media::audio::common::AudioConfig; |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 34 | using aidl::android::media::audio::common::AudioConfigBase; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 35 | using aidl::android::media::audio::common::AudioDevice; |
| 36 | using aidl::android::media::audio::common::AudioDeviceAddress; |
| 37 | using aidl::android::media::audio::common::AudioDeviceDescription; |
| 38 | using aidl::android::media::audio::common::AudioDeviceType; |
| 39 | using aidl::android::media::audio::common::AudioFormatDescription; |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 40 | using aidl::android::media::audio::common::AudioFormatType; |
Mikhail Naganov | 1a2e0ff | 2024-06-11 15:53:46 -0700 | [diff] [blame] | 41 | using aidl::android::media::audio::common::AudioGainConfig; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 42 | using aidl::android::media::audio::common::AudioInputFlags; |
| 43 | using aidl::android::media::audio::common::AudioIoFlags; |
| 44 | using aidl::android::media::audio::common::AudioOutputFlags; |
| 45 | using aidl::android::media::audio::common::AudioPort; |
| 46 | using aidl::android::media::audio::common::AudioPortConfig; |
| 47 | using aidl::android::media::audio::common::AudioPortDeviceExt; |
| 48 | using aidl::android::media::audio::common::AudioPortExt; |
| 49 | using aidl::android::media::audio::common::AudioPortMixExt; |
| 50 | using aidl::android::media::audio::common::AudioPortMixExtUseCase; |
| 51 | using aidl::android::media::audio::common::AudioProfile; |
| 52 | using aidl::android::media::audio::common::AudioSource; |
| 53 | using aidl::android::media::audio::common::Int; |
| 54 | using aidl::android::hardware::audio::common::isBitPositionFlagSet; |
| 55 | using aidl::android::hardware::audio::common::isDefaultAudioFormat; |
| 56 | using aidl::android::hardware::audio::common::makeBitPositionFlagMask; |
| 57 | using aidl::android::hardware::audio::core::AudioPatch; |
| 58 | using aidl::android::hardware::audio::core::AudioRoute; |
| 59 | using aidl::android::hardware::audio::core::IModule; |
| 60 | |
| 61 | namespace android { |
| 62 | |
| 63 | namespace { |
| 64 | |
| 65 | bool isConfigEqualToPortConfig(const AudioConfig& config, const AudioPortConfig& portConfig) { |
| 66 | return portConfig.sampleRate.value().value == config.base.sampleRate && |
| 67 | portConfig.channelMask.value() == config.base.channelMask && |
| 68 | portConfig.format.value() == config.base.format; |
| 69 | } |
| 70 | |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 71 | AudioConfig* setConfigFromPortConfig(AudioConfig* config, const AudioPortConfig& portConfig) { |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 72 | config->base.sampleRate = portConfig.sampleRate.value().value; |
| 73 | config->base.channelMask = portConfig.channelMask.value(); |
| 74 | config->base.format = portConfig.format.value(); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 75 | return config; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void setPortConfigFromConfig(AudioPortConfig* portConfig, const AudioConfig& config) { |
| 79 | if (config.base.sampleRate != 0) { |
| 80 | portConfig->sampleRate = Int{ .value = config.base.sampleRate }; |
| 81 | } |
| 82 | if (config.base.channelMask != AudioChannelLayout{}) { |
| 83 | portConfig->channelMask = config.base.channelMask; |
| 84 | } |
| 85 | if (config.base.format != AudioFormatDescription{}) { |
| 86 | portConfig->format = config.base.format; |
| 87 | } |
| 88 | } |
| 89 | |
jiabin | 8079700 | 2023-12-01 22:02:41 +0000 | [diff] [blame] | 90 | bool containHapticChannel(AudioChannelLayout channel) { |
| 91 | return channel.getTag() == AudioChannelLayout::Tag::layoutMask && |
| 92 | ((channel.get<AudioChannelLayout::Tag::layoutMask>() |
| 93 | & AudioChannelLayout::CHANNEL_HAPTIC_A) |
| 94 | == AudioChannelLayout::CHANNEL_HAPTIC_A || |
| 95 | (channel.get<AudioChannelLayout::Tag::layoutMask>() |
| 96 | & AudioChannelLayout::CHANNEL_HAPTIC_B) |
| 97 | == AudioChannelLayout::CHANNEL_HAPTIC_B); |
| 98 | } |
| 99 | |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 100 | } // namespace |
| 101 | |
| 102 | Hal2AidlMapper::Hal2AidlMapper(const std::string& instance, const std::shared_ptr<IModule>& module) |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 103 | : ConversionHelperAidl("Hal2AidlMapper", instance), mModule(module) {} |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 104 | |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 105 | void Hal2AidlMapper::addStream( |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 106 | const sp<StreamHalInterface>& stream, int32_t mixPortConfigId, int32_t patchId) { |
| 107 | mStreams.insert(std::pair(stream, std::pair(mixPortConfigId, patchId))); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool Hal2AidlMapper::audioDeviceMatches(const AudioDevice& device, const AudioPort& p) { |
| 111 | if (p.ext.getTag() != AudioPortExt::Tag::device) return false; |
| 112 | return p.ext.get<AudioPortExt::Tag::device>().device == device; |
| 113 | } |
| 114 | |
| 115 | bool Hal2AidlMapper::audioDeviceMatches(const AudioDevice& device, const AudioPortConfig& p) { |
| 116 | if (p.ext.getTag() != AudioPortExt::Tag::device) return false; |
| 117 | if (device.type.type == AudioDeviceType::IN_DEFAULT) { |
| 118 | return p.portId == mDefaultInputPortId; |
| 119 | } else if (device.type.type == AudioDeviceType::OUT_DEFAULT) { |
| 120 | return p.portId == mDefaultOutputPortId; |
| 121 | } |
| 122 | return p.ext.get<AudioPortExt::Tag::device>().device == device; |
| 123 | } |
| 124 | |
| 125 | status_t Hal2AidlMapper::createOrUpdatePatch( |
| 126 | const std::vector<AudioPortConfig>& sources, |
| 127 | const std::vector<AudioPortConfig>& sinks, |
| 128 | int32_t* patchId, Cleanups* cleanups) { |
| 129 | auto existingPatchIt = *patchId != 0 ? mPatches.find(*patchId): mPatches.end(); |
| 130 | AudioPatch patch; |
| 131 | if (existingPatchIt != mPatches.end()) { |
| 132 | patch = existingPatchIt->second; |
| 133 | patch.sourcePortConfigIds.clear(); |
| 134 | patch.sinkPortConfigIds.clear(); |
| 135 | } |
| 136 | // The IDs will be found by 'fillPortConfigs', however the original 'sources' and |
| 137 | // 'sinks' will not be updated because 'setAudioPatch' only needs IDs. Here we log |
| 138 | // the source arguments, where only the audio configuration and device specifications |
| 139 | // are relevant. |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 140 | AUGMENT_LOG(D, "patch ID: %d, [disregard IDs] sources: %s, sinks: %s", *patchId, |
| 141 | ::android::internal::ToString(sources).c_str(), |
| 142 | ::android::internal::ToString(sinks).c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 143 | auto fillPortConfigs = [&]( |
| 144 | const std::vector<AudioPortConfig>& configs, |
| 145 | const std::set<int32_t>& destinationPortIds, |
| 146 | std::vector<int32_t>* ids, std::set<int32_t>* portIds) -> status_t { |
| 147 | for (const auto& s : configs) { |
| 148 | AudioPortConfig portConfig; |
Mikhail Naganov | 38220af | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 149 | if (status_t status = setPortConfig( |
| 150 | s, destinationPortIds, &portConfig, cleanups); status != OK) { |
| 151 | if (s.ext.getTag() == AudioPortExt::mix) { |
| 152 | // See b/315528763. Despite that the framework knows the actual format of |
| 153 | // the mix port, it still uses the original format. Luckily, there is |
| 154 | // the I/O handle which can be used to find the mix port. |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 155 | AUGMENT_LOG(I, |
| 156 | "fillPortConfigs: retrying to find a mix port config with" |
| 157 | " default configuration"); |
Mikhail Naganov | 38220af | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 158 | if (auto it = findPortConfig(std::nullopt, s.flags, |
| 159 | s.ext.get<AudioPortExt::mix>().handle); |
| 160 | it != mPortConfigs.end()) { |
| 161 | portConfig = it->second; |
| 162 | } else { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 163 | const std::string flags = |
| 164 | s.flags.has_value() ? s.flags->toString() : "<unspecified>"; |
| 165 | AUGMENT_LOG(E, |
| 166 | "fillPortConfigs: existing port config for flags %s, " |
| 167 | " handle %d not found", |
| 168 | flags.c_str(), s.ext.get<AudioPortExt::mix>().handle); |
Mikhail Naganov | 38220af | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 169 | return BAD_VALUE; |
| 170 | } |
| 171 | } else { |
| 172 | return status; |
| 173 | } |
| 174 | } |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 175 | LOG_ALWAYS_FATAL_IF(portConfig.id == 0, |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 176 | "fillPortConfigs: initial config: %s, port config: %s", |
| 177 | s.toString().c_str(), portConfig.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 178 | ids->push_back(portConfig.id); |
| 179 | if (portIds != nullptr) { |
| 180 | portIds->insert(portConfig.portId); |
| 181 | } |
| 182 | } |
| 183 | return OK; |
| 184 | }; |
| 185 | // When looking up port configs, the destinationPortId is only used for mix ports. |
| 186 | // Thus, we process device port configs first, and look up the destination port ID from them. |
Mikhail Naganov | 03b0a00 | 2024-05-03 20:22:58 +0000 | [diff] [blame] | 187 | const bool sourceIsDevice = std::any_of(sources.begin(), sources.end(), |
| 188 | [](const auto& config) { return config.ext.getTag() == AudioPortExt::device; }); |
| 189 | const bool sinkIsDevice = std::any_of(sinks.begin(), sinks.end(), |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 190 | [](const auto& config) { return config.ext.getTag() == AudioPortExt::device; }); |
| 191 | const std::vector<AudioPortConfig>& devicePortConfigs = |
| 192 | sourceIsDevice ? sources : sinks; |
| 193 | std::vector<int32_t>* devicePortConfigIds = |
| 194 | sourceIsDevice ? &patch.sourcePortConfigIds : &patch.sinkPortConfigIds; |
| 195 | const std::vector<AudioPortConfig>& mixPortConfigs = |
| 196 | sourceIsDevice ? sinks : sources; |
| 197 | std::vector<int32_t>* mixPortConfigIds = |
| 198 | sourceIsDevice ? &patch.sinkPortConfigIds : &patch.sourcePortConfigIds; |
| 199 | std::set<int32_t> devicePortIds; |
| 200 | RETURN_STATUS_IF_ERROR(fillPortConfigs( |
| 201 | devicePortConfigs, std::set<int32_t>(), devicePortConfigIds, &devicePortIds)); |
| 202 | RETURN_STATUS_IF_ERROR(fillPortConfigs( |
| 203 | mixPortConfigs, devicePortIds, mixPortConfigIds, nullptr)); |
| 204 | if (existingPatchIt != mPatches.end()) { |
| 205 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 206 | mModule->setAudioPatch(patch, &patch))); |
| 207 | existingPatchIt->second = patch; |
| 208 | } else { |
| 209 | bool created = false; |
Mikhail Naganov | 03b0a00 | 2024-05-03 20:22:58 +0000 | [diff] [blame] | 210 | // When the framework does not specify a patch ID, only the mix port config |
| 211 | // is used for finding an existing patch. That's because the framework assumes |
| 212 | // that there can only be one patch for an I/O thread. |
| 213 | PatchMatch match = sourceIsDevice && sinkIsDevice ? |
| 214 | MATCH_BOTH : (sourceIsDevice ? MATCH_SINKS : MATCH_SOURCES); |
Mikhail Naganov | 6ed595c | 2024-05-31 16:16:03 +0000 | [diff] [blame] | 215 | auto requestedPatch = patch; |
Mikhail Naganov | 03b0a00 | 2024-05-03 20:22:58 +0000 | [diff] [blame] | 216 | RETURN_STATUS_IF_ERROR(findOrCreatePatch(patch, match, |
| 217 | &patch, &created)); |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 218 | // No cleanup of the patch is needed, it is managed by the framework. |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 219 | *patchId = patch.id; |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 220 | if (!created) { |
Mikhail Naganov | 6ed595c | 2024-05-31 16:16:03 +0000 | [diff] [blame] | 221 | requestedPatch.id = patch.id; |
| 222 | if (patch != requestedPatch) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 223 | AUGMENT_LOG(I, "Updating transient patch. Current: %s, new: %s", |
| 224 | patch.toString().c_str(), requestedPatch.toString().c_str()); |
Mikhail Naganov | 6ed595c | 2024-05-31 16:16:03 +0000 | [diff] [blame] | 225 | // Since matching may be done by mix port only, update the patch if the device port |
| 226 | // config has changed. |
| 227 | patch = requestedPatch; |
| 228 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 229 | mModule->setAudioPatch(patch, &patch))); |
| 230 | existingPatchIt = mPatches.find(patch.id); |
| 231 | existingPatchIt->second = patch; |
| 232 | } |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 233 | // The framework might have "created" a patch which already existed due to |
| 234 | // stream creation. Need to release the ownership from the stream. |
| 235 | for (auto& s : mStreams) { |
| 236 | if (s.second.second == patch.id) s.second.second = -1; |
| 237 | } |
| 238 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 239 | } |
| 240 | return OK; |
| 241 | } |
| 242 | |
| 243 | status_t Hal2AidlMapper::createOrUpdatePortConfig( |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 244 | const AudioPortConfig& requestedPortConfig, AudioPortConfig* result, bool* created) { |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 245 | bool applied = false; |
| 246 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->setAudioPortConfig( |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 247 | requestedPortConfig, result, &applied))); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 248 | if (!applied) { |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 249 | result->id = 0; |
| 250 | *created = false; |
| 251 | return OK; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 254 | int32_t id = result->id; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 255 | if (requestedPortConfig.id != 0 && requestedPortConfig.id != id) { |
| 256 | LOG_ALWAYS_FATAL("%s: requested port config id %d changed to %d", __func__, |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 257 | requestedPortConfig.id, id); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 260 | auto [_, inserted] = mPortConfigs.insert_or_assign(id, *result); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 261 | *created = inserted; |
| 262 | return OK; |
| 263 | } |
| 264 | |
David Li | fef5d0c | 2024-01-11 16:57:17 +0000 | [diff] [blame] | 265 | status_t Hal2AidlMapper::createOrUpdatePortConfigRetry( |
| 266 | const AudioPortConfig& requestedPortConfig, AudioPortConfig* result, bool* created) { |
| 267 | AudioPortConfig suggestedOrAppliedPortConfig; |
| 268 | RETURN_STATUS_IF_ERROR(createOrUpdatePortConfig(requestedPortConfig, |
| 269 | &suggestedOrAppliedPortConfig, created)); |
| 270 | if (suggestedOrAppliedPortConfig.id == 0) { |
| 271 | // Try again with the suggested config |
| 272 | suggestedOrAppliedPortConfig.id = requestedPortConfig.id; |
| 273 | AudioPortConfig appliedPortConfig; |
| 274 | RETURN_STATUS_IF_ERROR(createOrUpdatePortConfig(suggestedOrAppliedPortConfig, |
| 275 | &appliedPortConfig, created)); |
| 276 | if (appliedPortConfig.id == 0) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 277 | AUGMENT_LOG(E, "did not apply suggested config %s", |
| 278 | suggestedOrAppliedPortConfig.toString().c_str()); |
David Li | fef5d0c | 2024-01-11 16:57:17 +0000 | [diff] [blame] | 279 | return NO_INIT; |
| 280 | } |
| 281 | *result = appliedPortConfig; |
| 282 | } else { |
| 283 | *result = suggestedOrAppliedPortConfig; |
| 284 | } |
| 285 | return OK; |
| 286 | } |
| 287 | |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 288 | void Hal2AidlMapper::eraseConnectedPort(int32_t portId) { |
| 289 | mPorts.erase(portId); |
| 290 | mConnectedPorts.erase(portId); |
| 291 | if (mDisconnectedPortReplacement.first == portId) { |
| 292 | const auto& port = mDisconnectedPortReplacement.second; |
| 293 | mPorts.insert(std::make_pair(port.id, port)); |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 294 | AUGMENT_LOG(D, "disconnected port replacement: %s", port.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 295 | mDisconnectedPortReplacement = std::pair<int32_t, AudioPort>(); |
| 296 | } |
jiabin | 255ff7f | 2024-01-11 00:24:47 +0000 | [diff] [blame] | 297 | updateDynamicMixPorts(); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | status_t Hal2AidlMapper::findOrCreatePatch( |
Mikhail Naganov | 03b0a00 | 2024-05-03 20:22:58 +0000 | [diff] [blame] | 301 | const AudioPatch& requestedPatch, PatchMatch match, AudioPatch* patch, bool* created) { |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 302 | std::set<int32_t> sourcePortConfigIds(requestedPatch.sourcePortConfigIds.begin(), |
| 303 | requestedPatch.sourcePortConfigIds.end()); |
| 304 | std::set<int32_t> sinkPortConfigIds(requestedPatch.sinkPortConfigIds.begin(), |
| 305 | requestedPatch.sinkPortConfigIds.end()); |
Mikhail Naganov | 03b0a00 | 2024-05-03 20:22:58 +0000 | [diff] [blame] | 306 | return findOrCreatePatch(sourcePortConfigIds, sinkPortConfigIds, match, patch, created); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | status_t Hal2AidlMapper::findOrCreatePatch( |
| 310 | const std::set<int32_t>& sourcePortConfigIds, const std::set<int32_t>& sinkPortConfigIds, |
Mikhail Naganov | 03b0a00 | 2024-05-03 20:22:58 +0000 | [diff] [blame] | 311 | PatchMatch match, AudioPatch* patch, bool* created) { |
| 312 | auto patchIt = findPatch(sourcePortConfigIds, sinkPortConfigIds, match); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 313 | if (patchIt == mPatches.end()) { |
| 314 | AudioPatch requestedPatch, appliedPatch; |
| 315 | requestedPatch.sourcePortConfigIds.insert(requestedPatch.sourcePortConfigIds.end(), |
| 316 | sourcePortConfigIds.begin(), sourcePortConfigIds.end()); |
| 317 | requestedPatch.sinkPortConfigIds.insert(requestedPatch.sinkPortConfigIds.end(), |
| 318 | sinkPortConfigIds.begin(), sinkPortConfigIds.end()); |
| 319 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->setAudioPatch( |
| 320 | requestedPatch, &appliedPatch))); |
| 321 | patchIt = mPatches.insert(mPatches.end(), std::make_pair(appliedPatch.id, appliedPatch)); |
| 322 | *created = true; |
| 323 | } else { |
| 324 | *created = false; |
| 325 | } |
| 326 | *patch = patchIt->second; |
| 327 | return OK; |
| 328 | } |
| 329 | |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 330 | status_t Hal2AidlMapper::findOrCreateDevicePortConfig( |
Mikhail Naganov | 1a2e0ff | 2024-06-11 15:53:46 -0700 | [diff] [blame] | 331 | const AudioDevice& device, const AudioConfig* config, const AudioGainConfig* gainConfig, |
| 332 | AudioPortConfig* portConfig, bool* created) { |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 333 | if (auto portConfigIt = findPortConfig(device); portConfigIt == mPortConfigs.end()) { |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 334 | auto portsIt = findPort(device); |
| 335 | if (portsIt == mPorts.end()) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 336 | AUGMENT_LOG(E, "device port for device %s is not found", device.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 337 | return BAD_VALUE; |
| 338 | } |
| 339 | AudioPortConfig requestedPortConfig; |
| 340 | requestedPortConfig.portId = portsIt->first; |
| 341 | if (config != nullptr) { |
| 342 | setPortConfigFromConfig(&requestedPortConfig, *config); |
| 343 | } |
Mikhail Naganov | 1a2e0ff | 2024-06-11 15:53:46 -0700 | [diff] [blame] | 344 | if (gainConfig != nullptr) { |
| 345 | requestedPortConfig.gain = *gainConfig; |
| 346 | } |
David Li | fef5d0c | 2024-01-11 16:57:17 +0000 | [diff] [blame] | 347 | return createOrUpdatePortConfigRetry(requestedPortConfig, portConfig, created); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 348 | } else { |
David Li | fef5d0c | 2024-01-11 16:57:17 +0000 | [diff] [blame] | 349 | AudioPortConfig requestedPortConfig = portConfigIt->second; |
| 350 | if (config != nullptr) { |
| 351 | setPortConfigFromConfig(&requestedPortConfig, *config); |
| 352 | } |
Mikhail Naganov | 1a2e0ff | 2024-06-11 15:53:46 -0700 | [diff] [blame] | 353 | if (gainConfig != nullptr) { |
| 354 | requestedPortConfig.gain = *gainConfig; |
| 355 | } |
David Li | fef5d0c | 2024-01-11 16:57:17 +0000 | [diff] [blame] | 356 | |
| 357 | if (requestedPortConfig != portConfigIt->second) { |
| 358 | return createOrUpdatePortConfigRetry(requestedPortConfig, portConfig, created); |
| 359 | } else { |
| 360 | *portConfig = portConfigIt->second; |
| 361 | *created = false; |
| 362 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 363 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 364 | return OK; |
| 365 | } |
| 366 | |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 367 | status_t Hal2AidlMapper::findOrCreateMixPortConfig( |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 368 | const AudioConfig& config, const std::optional<AudioIoFlags>& flags, int32_t ioHandle, |
| 369 | AudioSource source, const std::set<int32_t>& destinationPortIds, |
| 370 | AudioPortConfig* portConfig, bool* created) { |
| 371 | // These flags get removed one by one in this order when retrying port finding. |
| 372 | static const std::vector<AudioInputFlags> kOptionalInputFlags{ |
| 373 | AudioInputFlags::FAST, AudioInputFlags::RAW, AudioInputFlags::VOIP_TX }; |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 374 | if (auto portConfigIt = findPortConfig(config, flags, ioHandle); |
| 375 | portConfigIt == mPortConfigs.end() && flags.has_value()) { |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 376 | auto optionalInputFlagsIt = kOptionalInputFlags.begin(); |
| 377 | AudioIoFlags matchFlags = flags.value(); |
| 378 | auto portsIt = findPort(config, matchFlags, destinationPortIds); |
| 379 | while (portsIt == mPorts.end() && matchFlags.getTag() == AudioIoFlags::Tag::input |
| 380 | && optionalInputFlagsIt != kOptionalInputFlags.end()) { |
| 381 | if (!isBitPositionFlagSet( |
| 382 | matchFlags.get<AudioIoFlags::Tag::input>(), *optionalInputFlagsIt)) { |
| 383 | ++optionalInputFlagsIt; |
| 384 | continue; |
| 385 | } |
| 386 | matchFlags.set<AudioIoFlags::Tag::input>(matchFlags.get<AudioIoFlags::Tag::input>() & |
| 387 | ~makeBitPositionFlagMask(*optionalInputFlagsIt++)); |
| 388 | portsIt = findPort(config, matchFlags, destinationPortIds); |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 389 | AUGMENT_LOG(I, |
| 390 | "mix port for config %s, flags %s was not found" |
| 391 | "retried with flags %s", |
| 392 | config.toString().c_str(), flags.value().toString().c_str(), |
| 393 | matchFlags.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 394 | } |
| 395 | if (portsIt == mPorts.end()) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 396 | AUGMENT_LOG(E, "mix port for config %s, flags %s is not found", |
| 397 | config.toString().c_str(), matchFlags.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 398 | return BAD_VALUE; |
| 399 | } |
| 400 | AudioPortConfig requestedPortConfig; |
| 401 | requestedPortConfig.portId = portsIt->first; |
| 402 | setPortConfigFromConfig(&requestedPortConfig, config); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 403 | requestedPortConfig.flags = portsIt->second.flags; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 404 | requestedPortConfig.ext = AudioPortMixExt{ .handle = ioHandle }; |
| 405 | if (matchFlags.getTag() == AudioIoFlags::Tag::input |
| 406 | && source != AudioSource::SYS_RESERVED_INVALID) { |
| 407 | requestedPortConfig.ext.get<AudioPortExt::Tag::mix>().usecase = |
| 408 | AudioPortMixExtUseCase::make<AudioPortMixExtUseCase::Tag::source>(source); |
| 409 | } |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 410 | return createOrUpdatePortConfig(requestedPortConfig, portConfig, created); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 411 | } else if (portConfigIt == mPortConfigs.end() && !flags.has_value()) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 412 | AUGMENT_LOG(W, |
| 413 | "mix port config for %s, handle %d not found " |
| 414 | "and was not created as flags are not specified", |
| 415 | config.toString().c_str(), ioHandle); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 416 | return BAD_VALUE; |
| 417 | } else { |
| 418 | AudioPortConfig requestedPortConfig = portConfigIt->second; |
David Li | fef5d0c | 2024-01-11 16:57:17 +0000 | [diff] [blame] | 419 | setPortConfigFromConfig(&requestedPortConfig, config); |
| 420 | |
| 421 | AudioPortMixExt& mixExt = requestedPortConfig.ext.get<AudioPortExt::Tag::mix>(); |
| 422 | if (mixExt.usecase.getTag() == AudioPortMixExtUseCase::Tag::source && |
| 423 | source != AudioSource::SYS_RESERVED_INVALID) { |
| 424 | mixExt.usecase.get<AudioPortMixExtUseCase::Tag::source>() = source; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | if (requestedPortConfig != portConfigIt->second) { |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 428 | return createOrUpdatePortConfig(requestedPortConfig, portConfig, created); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 429 | } else { |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 430 | *portConfig = portConfigIt->second; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 431 | *created = false; |
| 432 | } |
| 433 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 434 | return OK; |
| 435 | } |
| 436 | |
| 437 | status_t Hal2AidlMapper::findOrCreatePortConfig( |
| 438 | const AudioPortConfig& requestedPortConfig, const std::set<int32_t>& destinationPortIds, |
| 439 | AudioPortConfig* portConfig, bool* created) { |
| 440 | using Tag = AudioPortExt::Tag; |
| 441 | if (requestedPortConfig.ext.getTag() == Tag::mix) { |
| 442 | if (const auto& p = requestedPortConfig; |
| 443 | !p.sampleRate.has_value() || !p.channelMask.has_value() || |
| 444 | !p.format.has_value()) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 445 | AUGMENT_LOG(W, "provided mix port config is not fully specified: %s", |
| 446 | p.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 447 | return BAD_VALUE; |
| 448 | } |
| 449 | AudioConfig config; |
| 450 | setConfigFromPortConfig(&config, requestedPortConfig); |
| 451 | AudioSource source = requestedPortConfig.ext.get<Tag::mix>().usecase.getTag() == |
| 452 | AudioPortMixExtUseCase::Tag::source ? |
| 453 | requestedPortConfig.ext.get<Tag::mix>().usecase. |
| 454 | get<AudioPortMixExtUseCase::Tag::source>() : AudioSource::SYS_RESERVED_INVALID; |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 455 | return findOrCreateMixPortConfig(config, requestedPortConfig.flags, |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 456 | requestedPortConfig.ext.get<Tag::mix>().handle, source, destinationPortIds, |
| 457 | portConfig, created); |
| 458 | } else if (requestedPortConfig.ext.getTag() == Tag::device) { |
Mikhail Naganov | 1a2e0ff | 2024-06-11 15:53:46 -0700 | [diff] [blame] | 459 | const auto& p = requestedPortConfig; |
| 460 | const bool hasAudioConfig = |
| 461 | p.sampleRate.has_value() && p.channelMask.has_value() && p.format.has_value(); |
| 462 | const bool hasGainConfig = p.gain.has_value(); |
| 463 | if (hasAudioConfig || hasGainConfig) { |
| 464 | AudioConfig config, *configPtr = nullptr; |
| 465 | if (hasAudioConfig) { |
| 466 | setConfigFromPortConfig(&config, requestedPortConfig); |
| 467 | configPtr = &config; |
| 468 | } |
| 469 | const AudioGainConfig* gainConfigPtr = nullptr; |
| 470 | if (hasGainConfig) gainConfigPtr = &(*(p.gain)); |
David Li | fef5d0c | 2024-01-11 16:57:17 +0000 | [diff] [blame] | 471 | return findOrCreateDevicePortConfig( |
Mikhail Naganov | 1a2e0ff | 2024-06-11 15:53:46 -0700 | [diff] [blame] | 472 | requestedPortConfig.ext.get<Tag::device>().device, configPtr, gainConfigPtr, |
David Li | fef5d0c | 2024-01-11 16:57:17 +0000 | [diff] [blame] | 473 | portConfig, created); |
| 474 | } else { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 475 | AUGMENT_LOG(D, "device port config does not have audio or gain config specified"); |
David Li | fef5d0c | 2024-01-11 16:57:17 +0000 | [diff] [blame] | 476 | return findOrCreateDevicePortConfig( |
| 477 | requestedPortConfig.ext.get<Tag::device>().device, nullptr /*config*/, |
Mikhail Naganov | 1a2e0ff | 2024-06-11 15:53:46 -0700 | [diff] [blame] | 478 | nullptr /*gainConfig*/, portConfig, created); |
David Li | fef5d0c | 2024-01-11 16:57:17 +0000 | [diff] [blame] | 479 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 480 | } |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 481 | AUGMENT_LOG(W, "unsupported audio port config: %s", requestedPortConfig.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 482 | return BAD_VALUE; |
| 483 | } |
| 484 | |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 485 | status_t Hal2AidlMapper::findPortConfig(const AudioDevice& device, AudioPortConfig* portConfig) { |
| 486 | if (auto it = findPortConfig(device); it != mPortConfigs.end()) { |
| 487 | *portConfig = it->second; |
| 488 | return OK; |
| 489 | } |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 490 | AUGMENT_LOG(E, "could not find a device port config for device %s", device.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 491 | return BAD_VALUE; |
| 492 | } |
| 493 | |
| 494 | Hal2AidlMapper::Patches::iterator Hal2AidlMapper::findPatch( |
Mikhail Naganov | 03b0a00 | 2024-05-03 20:22:58 +0000 | [diff] [blame] | 495 | const std::set<int32_t>& sourcePortConfigIds, const std::set<int32_t>& sinkPortConfigIds, |
| 496 | PatchMatch match) { |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 497 | return std::find_if(mPatches.begin(), mPatches.end(), |
| 498 | [&](const auto& pair) { |
| 499 | const auto& p = pair.second; |
| 500 | std::set<int32_t> patchSrcs( |
| 501 | p.sourcePortConfigIds.begin(), p.sourcePortConfigIds.end()); |
| 502 | std::set<int32_t> patchSinks( |
| 503 | p.sinkPortConfigIds.begin(), p.sinkPortConfigIds.end()); |
Mikhail Naganov | 03b0a00 | 2024-05-03 20:22:58 +0000 | [diff] [blame] | 504 | switch (match) { |
| 505 | case MATCH_SOURCES: |
| 506 | return sourcePortConfigIds == patchSrcs; |
| 507 | case MATCH_SINKS: |
| 508 | return sinkPortConfigIds == patchSinks; |
| 509 | case MATCH_BOTH: |
| 510 | return sourcePortConfigIds == patchSrcs && sinkPortConfigIds == patchSinks; |
| 511 | } |
| 512 | }); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | Hal2AidlMapper::Ports::iterator Hal2AidlMapper::findPort(const AudioDevice& device) { |
| 516 | if (device.type.type == AudioDeviceType::IN_DEFAULT) { |
| 517 | return mPorts.find(mDefaultInputPortId); |
| 518 | } else if (device.type.type == AudioDeviceType::OUT_DEFAULT) { |
| 519 | return mPorts.find(mDefaultOutputPortId); |
| 520 | } |
| 521 | if (device.address.getTag() != AudioDeviceAddress::id || |
| 522 | !device.address.get<AudioDeviceAddress::id>().empty()) { |
| 523 | return std::find_if(mPorts.begin(), mPorts.end(), |
| 524 | [&](const auto& pair) { return audioDeviceMatches(device, pair.second); }); |
| 525 | } |
| 526 | // For connection w/o an address, two ports can be found: the template port, |
| 527 | // and a connected port (if exists). Make sure we return the connected port. |
| 528 | Hal2AidlMapper::Ports::iterator portIt = mPorts.end(); |
| 529 | for (auto it = mPorts.begin(); it != mPorts.end(); ++it) { |
| 530 | if (audioDeviceMatches(device, it->second)) { |
| 531 | if (mConnectedPorts.find(it->first) != mConnectedPorts.end()) { |
| 532 | return it; |
| 533 | } else { |
| 534 | // Will return 'it' if there is no connected port. |
| 535 | portIt = it; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | return portIt; |
| 540 | } |
| 541 | |
| 542 | Hal2AidlMapper::Ports::iterator Hal2AidlMapper::findPort( |
| 543 | const AudioConfig& config, const AudioIoFlags& flags, |
| 544 | const std::set<int32_t>& destinationPortIds) { |
jiabin | 8079700 | 2023-12-01 22:02:41 +0000 | [diff] [blame] | 545 | auto channelMaskMatches = [](const std::vector<AudioChannelLayout>& channelMasks, |
| 546 | const AudioChannelLayout& channelMask) { |
| 547 | // Return true when 1) the channel mask is none and none of the channel mask from the |
| 548 | // collection contains haptic channel mask, or 2) the channel mask collection contains |
| 549 | // the queried channel mask. |
| 550 | return (channelMask.getTag() == AudioChannelLayout::none && |
| 551 | std::none_of(channelMasks.begin(), channelMasks.end(), |
| 552 | containHapticChannel)) || |
| 553 | std::find(channelMasks.begin(), channelMasks.end(), channelMask) |
| 554 | != channelMasks.end(); |
| 555 | }; |
| 556 | auto belongsToProfile = [&config, &channelMaskMatches](const AudioProfile& prof) { |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 557 | return (isDefaultAudioFormat(config.base.format) || prof.format == config.base.format) && |
jiabin | 8079700 | 2023-12-01 22:02:41 +0000 | [diff] [blame] | 558 | channelMaskMatches(prof.channelMasks, config.base.channelMask) && |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 559 | (config.base.sampleRate == 0 || |
| 560 | std::find(prof.sampleRates.begin(), prof.sampleRates.end(), |
| 561 | config.base.sampleRate) != prof.sampleRates.end()); |
| 562 | }; |
| 563 | static const std::vector<AudioOutputFlags> kOptionalOutputFlags{AudioOutputFlags::BIT_PERFECT}; |
| 564 | int optionalFlags = 0; |
| 565 | auto flagMatches = [&flags, &optionalFlags](const AudioIoFlags& portFlags) { |
| 566 | // Ports should be able to match if the optional flags are not requested. |
| 567 | return portFlags == flags || |
| 568 | (portFlags.getTag() == AudioIoFlags::Tag::output && |
| 569 | AudioIoFlags::make<AudioIoFlags::Tag::output>( |
| 570 | portFlags.get<AudioIoFlags::Tag::output>() & |
| 571 | ~optionalFlags) == flags); |
| 572 | }; |
| 573 | auto matcher = [&](const auto& pair) { |
| 574 | const auto& p = pair.second; |
| 575 | return p.ext.getTag() == AudioPortExt::Tag::mix && |
| 576 | flagMatches(p.flags) && |
| 577 | (destinationPortIds.empty() || |
| 578 | std::any_of(destinationPortIds.begin(), destinationPortIds.end(), |
| 579 | [&](const int32_t destId) { return mRoutingMatrix.count( |
| 580 | std::make_pair(p.id, destId)) != 0; })) && |
| 581 | (p.profiles.empty() || |
| 582 | std::find_if(p.profiles.begin(), p.profiles.end(), belongsToProfile) != |
| 583 | p.profiles.end()); }; |
| 584 | auto result = std::find_if(mPorts.begin(), mPorts.end(), matcher); |
| 585 | if (result == mPorts.end() && flags.getTag() == AudioIoFlags::Tag::output) { |
| 586 | auto optionalOutputFlagsIt = kOptionalOutputFlags.begin(); |
| 587 | while (result == mPorts.end() && optionalOutputFlagsIt != kOptionalOutputFlags.end()) { |
| 588 | if (isBitPositionFlagSet( |
| 589 | flags.get<AudioIoFlags::Tag::output>(), *optionalOutputFlagsIt)) { |
| 590 | // If the flag is set by the request, it must be matched. |
| 591 | ++optionalOutputFlagsIt; |
| 592 | continue; |
| 593 | } |
| 594 | optionalFlags |= makeBitPositionFlagMask(*optionalOutputFlagsIt++); |
| 595 | result = std::find_if(mPorts.begin(), mPorts.end(), matcher); |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 596 | AUGMENT_LOG(I, |
| 597 | "port for config %s, flags %s was not found " |
| 598 | "retried with excluding optional flags %#x", |
| 599 | config.toString().c_str(), flags.toString().c_str(), optionalFlags); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | return result; |
| 603 | } |
| 604 | |
| 605 | Hal2AidlMapper::PortConfigs::iterator Hal2AidlMapper::findPortConfig(const AudioDevice& device) { |
| 606 | return std::find_if(mPortConfigs.begin(), mPortConfigs.end(), |
| 607 | [&](const auto& pair) { return audioDeviceMatches(device, pair.second); }); |
| 608 | } |
| 609 | |
| 610 | Hal2AidlMapper::PortConfigs::iterator Hal2AidlMapper::findPortConfig( |
| 611 | const std::optional<AudioConfig>& config, |
| 612 | const std::optional<AudioIoFlags>& flags, |
| 613 | int32_t ioHandle) { |
| 614 | using Tag = AudioPortExt::Tag; |
| 615 | return std::find_if(mPortConfigs.begin(), mPortConfigs.end(), |
| 616 | [&](const auto& pair) { |
| 617 | const auto& p = pair.second; |
| 618 | LOG_ALWAYS_FATAL_IF(p.ext.getTag() == Tag::mix && |
| 619 | (!p.sampleRate.has_value() || !p.channelMask.has_value() || |
| 620 | !p.format.has_value() || !p.flags.has_value()), |
| 621 | "%s: stored mix port config is not fully specified: %s", |
| 622 | __func__, p.toString().c_str()); |
| 623 | return p.ext.getTag() == Tag::mix && |
| 624 | (!config.has_value() || |
| 625 | isConfigEqualToPortConfig(config.value(), p)) && |
| 626 | (!flags.has_value() || p.flags.value() == flags.value()) && |
| 627 | p.ext.template get<Tag::mix>().handle == ioHandle; }); |
| 628 | } |
| 629 | |
| 630 | status_t Hal2AidlMapper::getAudioMixPort(int32_t ioHandle, AudioPort* port) { |
| 631 | auto it = findPortConfig(std::nullopt /*config*/, std::nullopt /*flags*/, ioHandle); |
| 632 | if (it == mPortConfigs.end()) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 633 | AUGMENT_LOG(E, "cannot find mix port config for handle %u", ioHandle); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 634 | return BAD_VALUE; |
| 635 | } |
| 636 | return updateAudioPort(it->second.portId, port); |
| 637 | } |
| 638 | |
| 639 | status_t Hal2AidlMapper::getAudioPortCached( |
| 640 | const ::aidl::android::media::audio::common::AudioDevice& device, |
| 641 | ::aidl::android::media::audio::common::AudioPort* port) { |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 642 | if (auto portsIt = findPort(device); portsIt != mPorts.end()) { |
| 643 | *port = portsIt->second; |
| 644 | return OK; |
| 645 | } |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 646 | AUGMENT_LOG(E, "device port for device %s is not found", device.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 647 | return BAD_VALUE; |
| 648 | } |
| 649 | |
| 650 | status_t Hal2AidlMapper::initialize() { |
| 651 | std::vector<AudioPort> ports; |
| 652 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->getAudioPorts(&ports))); |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 653 | AUGMENT_LOG_IF(W, ports.empty(), "returned an empty list of audio ports"); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 654 | mDefaultInputPortId = mDefaultOutputPortId = -1; |
| 655 | const int defaultDeviceFlag = 1 << AudioPortDeviceExt::FLAG_INDEX_DEFAULT_DEVICE; |
| 656 | for (auto it = ports.begin(); it != ports.end(); ) { |
| 657 | const auto& port = *it; |
| 658 | if (port.ext.getTag() != AudioPortExt::Tag::device) { |
| 659 | ++it; |
| 660 | continue; |
| 661 | } |
| 662 | const AudioPortDeviceExt& deviceExt = port.ext.get<AudioPortExt::Tag::device>(); |
| 663 | if ((deviceExt.flags & defaultDeviceFlag) != 0) { |
| 664 | if (port.flags.getTag() == AudioIoFlags::Tag::input) { |
| 665 | mDefaultInputPortId = port.id; |
| 666 | } else if (port.flags.getTag() == AudioIoFlags::Tag::output) { |
| 667 | mDefaultOutputPortId = port.id; |
| 668 | } |
| 669 | } |
| 670 | // For compatibility with HIDL, hide "template" remote submix ports from ports list. |
| 671 | if (const auto& devDesc = deviceExt.device; |
| 672 | (devDesc.type.type == AudioDeviceType::IN_SUBMIX || |
| 673 | devDesc.type.type == AudioDeviceType::OUT_SUBMIX) && |
| 674 | devDesc.type.connection == AudioDeviceDescription::CONNECTION_VIRTUAL) { |
| 675 | if (devDesc.type.type == AudioDeviceType::IN_SUBMIX) { |
| 676 | mRemoteSubmixIn = port; |
| 677 | } else { |
| 678 | mRemoteSubmixOut = port; |
| 679 | } |
| 680 | it = ports.erase(it); |
| 681 | } else { |
| 682 | ++it; |
| 683 | } |
| 684 | } |
| 685 | if (mRemoteSubmixIn.has_value() != mRemoteSubmixOut.has_value()) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 686 | AUGMENT_LOG(E, |
| 687 | "The configuration only has input or output remote submix device, " |
| 688 | "must have both"); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 689 | mRemoteSubmixIn.reset(); |
| 690 | mRemoteSubmixOut.reset(); |
| 691 | } |
| 692 | if (mRemoteSubmixIn.has_value()) { |
| 693 | AudioPort connectedRSubmixIn = *mRemoteSubmixIn; |
| 694 | connectedRSubmixIn.ext.get<AudioPortExt::Tag::device>().device.address = |
| 695 | AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS; |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 696 | AUGMENT_LOG(D, "connecting remote submix input"); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 697 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->connectExternalDevice( |
| 698 | connectedRSubmixIn, &connectedRSubmixIn))); |
| 699 | // The template port for the remote submix input couldn't be "default" because it is not |
| 700 | // attached. The connected port can now be made default because we never disconnect it. |
| 701 | if (mDefaultInputPortId == -1) { |
| 702 | mDefaultInputPortId = connectedRSubmixIn.id; |
| 703 | } |
| 704 | ports.push_back(std::move(connectedRSubmixIn)); |
| 705 | |
| 706 | // Remote submix output must not be connected until the framework actually starts |
| 707 | // using it, however for legacy compatibility we need to provide an "augmented template" |
| 708 | // port with an address and profiles. It is obtained by connecting the output and then |
| 709 | // immediately disconnecting it. This is a cheap operation as we don't open any streams. |
| 710 | AudioPort tempConnectedRSubmixOut = *mRemoteSubmixOut; |
| 711 | tempConnectedRSubmixOut.ext.get<AudioPortExt::Tag::device>().device.address = |
| 712 | AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS; |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 713 | AUGMENT_LOG(D, "temporarily connecting and disconnecting remote submix output"); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 714 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->connectExternalDevice( |
| 715 | tempConnectedRSubmixOut, &tempConnectedRSubmixOut))); |
| 716 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->disconnectExternalDevice( |
| 717 | tempConnectedRSubmixOut.id))); |
| 718 | tempConnectedRSubmixOut.id = mRemoteSubmixOut->id; |
| 719 | ports.push_back(std::move(tempConnectedRSubmixOut)); |
| 720 | } |
| 721 | |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 722 | AUGMENT_LOG(I, "default port ids: input %d, output %d", mDefaultInputPortId, |
| 723 | mDefaultOutputPortId); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 724 | std::transform(ports.begin(), ports.end(), std::inserter(mPorts, mPorts.end()), |
| 725 | [](const auto& p) { return std::make_pair(p.id, p); }); |
| 726 | RETURN_STATUS_IF_ERROR(updateRoutes()); |
| 727 | std::vector<AudioPortConfig> portConfigs; |
| 728 | RETURN_STATUS_IF_ERROR( |
| 729 | statusTFromBinderStatus(mModule->getAudioPortConfigs(&portConfigs))); // OK if empty |
| 730 | std::transform(portConfigs.begin(), portConfigs.end(), |
| 731 | std::inserter(mPortConfigs, mPortConfigs.end()), |
| 732 | [](const auto& p) { return std::make_pair(p.id, p); }); |
| 733 | std::transform(mPortConfigs.begin(), mPortConfigs.end(), |
| 734 | std::inserter(mInitialPortConfigIds, mInitialPortConfigIds.end()), |
| 735 | [](const auto& pcPair) { return pcPair.first; }); |
| 736 | std::vector<AudioPatch> patches; |
| 737 | RETURN_STATUS_IF_ERROR( |
| 738 | statusTFromBinderStatus(mModule->getAudioPatches(&patches))); // OK if empty |
| 739 | std::transform(patches.begin(), patches.end(), |
| 740 | std::inserter(mPatches, mPatches.end()), |
| 741 | [](const auto& p) { return std::make_pair(p.id, p); }); |
| 742 | return OK; |
| 743 | } |
| 744 | |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 745 | std::set<int32_t> Hal2AidlMapper::getPatchIdsByPortId(int32_t portId) { |
| 746 | std::set<int32_t> result; |
| 747 | for (const auto& [patchId, patch] : mPatches) { |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 748 | for (int32_t id : patch.sourcePortConfigIds) { |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 749 | if (portConfigBelongsToPort(id, portId)) { |
| 750 | result.insert(patchId); |
| 751 | break; |
| 752 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 753 | } |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 754 | for (int32_t id : patch.sinkPortConfigIds) { |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 755 | if (portConfigBelongsToPort(id, portId)) { |
| 756 | result.insert(patchId); |
| 757 | break; |
| 758 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 761 | return result; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 762 | } |
| 763 | |
jiabin | 62750c2 | 2023-12-21 22:06:07 +0000 | [diff] [blame] | 764 | status_t Hal2AidlMapper::prepareToDisconnectExternalDevice(const AudioPort& devicePort) { |
| 765 | auto portsIt = findPort(devicePort.ext.get<AudioPortExt::device>().device); |
| 766 | if (portsIt == mPorts.end()) { |
| 767 | return BAD_VALUE; |
| 768 | } |
| 769 | return statusTFromBinderStatus(mModule->prepareToDisconnectExternalDevice(portsIt->second.id)); |
| 770 | } |
| 771 | |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 772 | status_t Hal2AidlMapper::prepareToOpenStream( |
| 773 | int32_t ioHandle, const AudioDevice& device, const AudioIoFlags& flags, |
| 774 | AudioSource source, Cleanups* cleanups, AudioConfig* config, |
| 775 | AudioPortConfig* mixPortConfig, AudioPatch* patch) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 776 | AUGMENT_LOG(D, "handle %d, device %s, flags %s, source %s, config %s, mixport config %s", |
| 777 | ioHandle, device.toString().c_str(), flags.toString().c_str(), |
| 778 | toString(source).c_str(), config->toString().c_str(), |
| 779 | mixPortConfig->toString().c_str()); |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 780 | resetUnusedPatchesAndPortConfigs(); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 781 | const AudioConfig initialConfig = *config; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 782 | // Find / create AudioPortConfigs for the device port and the mix port, |
| 783 | // then find / create a patch between them, and open a stream on the mix port. |
| 784 | AudioPortConfig devicePortConfig; |
| 785 | bool created = false; |
Mikhail Naganov | 1a2e0ff | 2024-06-11 15:53:46 -0700 | [diff] [blame] | 786 | RETURN_STATUS_IF_ERROR(findOrCreateDevicePortConfig(device, config, nullptr /*gainConfig*/, |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 787 | &devicePortConfig, &created)); |
| 788 | LOG_ALWAYS_FATAL_IF(devicePortConfig.id == 0); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 789 | if (created) { |
| 790 | cleanups->add(&Hal2AidlMapper::resetPortConfig, devicePortConfig.id); |
| 791 | } |
Mikhail Naganov | 38220af | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 792 | status_t status = prepareToOpenStreamHelper(ioHandle, devicePortConfig.portId, |
| 793 | devicePortConfig.id, flags, source, initialConfig, cleanups, config, |
| 794 | mixPortConfig, patch); |
| 795 | if (status != OK) { |
| 796 | // If using the client-provided config did not work out for establishing a mix port config |
| 797 | // or patching, try with the device port config. Note that in general device port config and |
| 798 | // mix port config are not required to be the same, however they must match if the HAL |
| 799 | // module can't perform audio stream conversions. |
| 800 | AudioConfig deviceConfig = initialConfig; |
| 801 | if (setConfigFromPortConfig(&deviceConfig, devicePortConfig)->base != initialConfig.base) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 802 | AUGMENT_LOG(D, "retrying with device port config: %s", |
| 803 | devicePortConfig.toString().c_str()); |
Mikhail Naganov | 38220af | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 804 | status = prepareToOpenStreamHelper(ioHandle, devicePortConfig.portId, |
| 805 | devicePortConfig.id, flags, source, initialConfig, cleanups, |
| 806 | &deviceConfig, mixPortConfig, patch); |
| 807 | if (status == OK) { |
| 808 | *config = deviceConfig; |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | return status; |
| 813 | } |
| 814 | |
| 815 | status_t Hal2AidlMapper::prepareToOpenStreamHelper( |
| 816 | int32_t ioHandle, int32_t devicePortId, int32_t devicePortConfigId, |
| 817 | const AudioIoFlags& flags, AudioSource source, const AudioConfig& initialConfig, |
| 818 | Cleanups* cleanups, AudioConfig* config, AudioPortConfig* mixPortConfig, |
| 819 | AudioPatch* patch) { |
| 820 | const bool isInput = flags.getTag() == AudioIoFlags::Tag::input; |
| 821 | bool created = false; |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 822 | RETURN_STATUS_IF_ERROR(findOrCreateMixPortConfig(*config, flags, ioHandle, source, |
Mikhail Naganov | 38220af | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 823 | std::set<int32_t>{devicePortId}, mixPortConfig, &created)); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 824 | if (created) { |
| 825 | cleanups->add(&Hal2AidlMapper::resetPortConfig, mixPortConfig->id); |
| 826 | } |
| 827 | setConfigFromPortConfig(config, *mixPortConfig); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 828 | bool retryWithSuggestedConfig = false; // By default, let the framework to retry. |
| 829 | if (mixPortConfig->id == 0 && config->base == AudioConfigBase{}) { |
| 830 | // The HAL proposes a default config, can retry here. |
| 831 | retryWithSuggestedConfig = true; |
| 832 | } else if (isInput && config->base != initialConfig.base) { |
| 833 | // If the resulting config is different, we must stop and provide the config to the |
| 834 | // framework so that it can retry. |
| 835 | mixPortConfig->id = 0; |
| 836 | } else if (!isInput && mixPortConfig->id == 0 && |
| 837 | (initialConfig.base.format.type == AudioFormatType::PCM || |
| 838 | !isBitPositionFlagSet(flags.get<AudioIoFlags::output>(), |
| 839 | AudioOutputFlags::DIRECT) || |
| 840 | isBitPositionFlagSet(flags.get<AudioIoFlags::output>(), |
| 841 | AudioOutputFlags::COMPRESS_OFFLOAD))) { |
| 842 | // The framework does not retry opening non-direct PCM and IEC61937 outputs, need to retry |
| 843 | // here (see 'AudioHwDevice::openOutputStream'). |
| 844 | retryWithSuggestedConfig = true; |
| 845 | } |
| 846 | if (mixPortConfig->id == 0 && retryWithSuggestedConfig) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 847 | AUGMENT_LOG(D, "retrying to find/create a mix port config using config %s", |
| 848 | config->toString().c_str()); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 849 | RETURN_STATUS_IF_ERROR(findOrCreateMixPortConfig(*config, flags, ioHandle, source, |
Mikhail Naganov | 38220af | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 850 | std::set<int32_t>{devicePortId}, mixPortConfig, &created)); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 851 | if (created) { |
| 852 | cleanups->add(&Hal2AidlMapper::resetPortConfig, mixPortConfig->id); |
| 853 | } |
| 854 | setConfigFromPortConfig(config, *mixPortConfig); |
| 855 | } |
| 856 | if (mixPortConfig->id == 0) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 857 | AUGMENT_LOG(D, "returning suggested config for the stream: %s", |
| 858 | config->toString().c_str()); |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 859 | return OK; |
| 860 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 861 | if (isInput) { |
| 862 | RETURN_STATUS_IF_ERROR(findOrCreatePatch( |
Mikhail Naganov | 03b0a00 | 2024-05-03 20:22:58 +0000 | [diff] [blame] | 863 | {devicePortConfigId}, {mixPortConfig->id}, MATCH_BOTH, patch, &created)); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 864 | } else { |
| 865 | RETURN_STATUS_IF_ERROR(findOrCreatePatch( |
Mikhail Naganov | 03b0a00 | 2024-05-03 20:22:58 +0000 | [diff] [blame] | 866 | {mixPortConfig->id}, {devicePortConfigId}, MATCH_BOTH, patch, &created)); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 867 | } |
| 868 | if (created) { |
| 869 | cleanups->add(&Hal2AidlMapper::resetPatch, patch->id); |
| 870 | } |
| 871 | if (config->frameCount <= 0) { |
| 872 | config->frameCount = patch->minimumStreamBufferSizeFrames; |
| 873 | } |
| 874 | return OK; |
| 875 | } |
| 876 | |
Mikhail Naganov | ca92a5c | 2023-12-07 14:00:48 -0800 | [diff] [blame] | 877 | status_t Hal2AidlMapper::setPortConfig( |
| 878 | const AudioPortConfig& requestedPortConfig, const std::set<int32_t>& destinationPortIds, |
| 879 | AudioPortConfig* portConfig, Cleanups* cleanups) { |
| 880 | bool created = false; |
| 881 | RETURN_STATUS_IF_ERROR(findOrCreatePortConfig( |
| 882 | requestedPortConfig, destinationPortIds, portConfig, &created)); |
| 883 | if (created && cleanups != nullptr) { |
| 884 | cleanups->add(&Hal2AidlMapper::resetPortConfig, portConfig->id); |
| 885 | } |
| 886 | return OK; |
| 887 | } |
| 888 | |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 889 | status_t Hal2AidlMapper::releaseAudioPatch(int32_t patchId) { |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 890 | return releaseAudioPatches({patchId}); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 893 | // Note: does not reset port configs. |
| 894 | status_t Hal2AidlMapper::releaseAudioPatch(Patches::iterator it) { |
| 895 | const int32_t patchId = it->first; |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 896 | AUGMENT_LOG(D, "patchId %d", patchId); |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 897 | if (ndk::ScopedAStatus status = mModule->resetAudioPatch(patchId); !status.isOk()) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 898 | AUGMENT_LOG(E, "error while resetting patch %d: %s", patchId, |
| 899 | status.getDescription().c_str()); |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 900 | return statusTFromBinderStatus(status); |
| 901 | } |
| 902 | mPatches.erase(it); |
| 903 | for (auto it = mFwkPatches.begin(); it != mFwkPatches.end(); ++it) { |
| 904 | if (it->second == patchId) { |
| 905 | mFwkPatches.erase(it); |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | return OK; |
| 910 | } |
| 911 | |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 912 | status_t Hal2AidlMapper::releaseAudioPatches(const std::set<int32_t>& patchIds) { |
| 913 | status_t result = OK; |
| 914 | for (const auto patchId : patchIds) { |
| 915 | if (auto it = mPatches.find(patchId); it != mPatches.end()) { |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 916 | releaseAudioPatch(it); |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 917 | } else { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 918 | AUGMENT_LOG(E, "patch id %d not found", patchId); |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 919 | result = BAD_VALUE; |
| 920 | } |
| 921 | } |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 922 | resetUnusedPortConfigs(); |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 923 | return result; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | void Hal2AidlMapper::resetPortConfig(int32_t portConfigId) { |
| 927 | if (auto it = mPortConfigs.find(portConfigId); it != mPortConfigs.end()) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 928 | AUGMENT_LOG(D, "%s", it->second.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 929 | if (ndk::ScopedAStatus status = mModule->resetAudioPortConfig(portConfigId); |
| 930 | !status.isOk()) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 931 | AUGMENT_LOG(E, "error while resetting port config %d: %s", portConfigId, |
| 932 | status.getDescription().c_str()); |
Qiang Chen | d6a604e | 2024-06-12 15:26:46 +0800 | [diff] [blame] | 933 | return; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 934 | } |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 935 | mPortConfigs.erase(it); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 936 | return; |
| 937 | } |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 938 | AUGMENT_LOG(E, "port config id %d not found", portConfigId); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 941 | void Hal2AidlMapper::resetUnusedPatchesAndPortConfigs() { |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 942 | // Since patches can be created independently of streams via 'createOrUpdatePatch', |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 943 | // here we only clean up patches for released streams. |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 944 | std::set<int32_t> patchesToRelease; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 945 | for (auto it = mStreams.begin(); it != mStreams.end(); ) { |
| 946 | if (auto streamSp = it->first.promote(); streamSp) { |
| 947 | ++it; |
| 948 | } else { |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 949 | if (const int32_t patchId = it->second.second; patchId != -1) { |
| 950 | patchesToRelease.insert(patchId); |
| 951 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 952 | it = mStreams.erase(it); |
| 953 | } |
| 954 | } |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 955 | // 'releaseAudioPatches' also resets unused port configs. |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 956 | releaseAudioPatches(patchesToRelease); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 959 | void Hal2AidlMapper::resetUnusedPortConfigs() { |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 960 | // The assumption is that port configs are used to create patches |
| 961 | // (or to open streams, but that involves creation of patches, too). Thus, |
| 962 | // orphaned port configs can and should be reset. |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 963 | std::set<int32_t> portConfigIdsToReset; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 964 | std::transform(mPortConfigs.begin(), mPortConfigs.end(), |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 965 | std::inserter(portConfigIdsToReset, portConfigIdsToReset.end()), |
| 966 | [](const auto& pcPair) { return pcPair.first; }); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 967 | for (const auto& p : mPatches) { |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 968 | for (int32_t id : p.second.sourcePortConfigIds) portConfigIdsToReset.erase(id); |
| 969 | for (int32_t id : p.second.sinkPortConfigIds) portConfigIdsToReset.erase(id); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 970 | } |
| 971 | for (int32_t id : mInitialPortConfigIds) { |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 972 | portConfigIdsToReset.erase(id); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 973 | } |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 974 | for (const auto& s : mStreams) { |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 975 | portConfigIdsToReset.erase(s.second.first); |
Mikhail Naganov | 78f7f9a | 2023-11-16 15:49:23 -0800 | [diff] [blame] | 976 | } |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 977 | for (const auto& portConfigId : portConfigIdsToReset) { |
| 978 | resetPortConfig(portConfigId); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
| 982 | status_t Hal2AidlMapper::setDevicePortConnectedState(const AudioPort& devicePort, bool connected) { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 983 | AUGMENT_LOG(D, "state %s, device %s", (connected ? "connected" : "disconnected"), |
| 984 | devicePort.toString().c_str()); |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 985 | resetUnusedPatchesAndPortConfigs(); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 986 | if (connected) { |
| 987 | AudioDevice matchDevice = devicePort.ext.get<AudioPortExt::device>().device; |
| 988 | std::optional<AudioPort> templatePort; |
| 989 | auto erasePortAfterConnectionIt = mPorts.end(); |
| 990 | // Connection of remote submix out with address "0" is a special case. Since there is |
| 991 | // already an "augmented template" port with this address in mPorts, we need to replace |
| 992 | // it with a connected port. |
| 993 | // Connection of remote submix outs with any other address is done as usual except that |
| 994 | // the template port is in `mRemoteSubmixOut`. |
| 995 | if (mRemoteSubmixOut.has_value() && matchDevice.type.type == AudioDeviceType::OUT_SUBMIX) { |
| 996 | if (matchDevice.address == AudioDeviceAddress::make<AudioDeviceAddress::id>( |
| 997 | AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS)) { |
| 998 | erasePortAfterConnectionIt = findPort(matchDevice); |
| 999 | } |
| 1000 | templatePort = mRemoteSubmixOut; |
| 1001 | } else if (mRemoteSubmixIn.has_value() && |
| 1002 | matchDevice.type.type == AudioDeviceType::IN_SUBMIX) { |
| 1003 | templatePort = mRemoteSubmixIn; |
| 1004 | } else { |
| 1005 | // Reset the device address to find the "template" port. |
| 1006 | matchDevice.address = AudioDeviceAddress::make<AudioDeviceAddress::id>(); |
| 1007 | } |
| 1008 | if (!templatePort.has_value()) { |
| 1009 | auto portsIt = findPort(matchDevice); |
| 1010 | if (portsIt == mPorts.end()) { |
| 1011 | // Since 'setConnectedState' is called for all modules, it is normal when the device |
| 1012 | // port not found in every one of them. |
| 1013 | return BAD_VALUE; |
| 1014 | } else { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 1015 | AUGMENT_LOG(D, "device port for device %s found", matchDevice.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1016 | } |
| 1017 | templatePort = portsIt->second; |
| 1018 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1019 | |
| 1020 | // Use the ID of the "template" port, use all the information from the provided port. |
| 1021 | AudioPort connectedPort = devicePort; |
| 1022 | connectedPort.id = templatePort->id; |
| 1023 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->connectExternalDevice( |
| 1024 | connectedPort, &connectedPort))); |
| 1025 | const auto [it, inserted] = mPorts.insert(std::make_pair(connectedPort.id, connectedPort)); |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 1026 | LOG_ALWAYS_FATAL_IF( |
| 1027 | !inserted, "%s duplicate port ID received from HAL: %s, existing port: %s", |
| 1028 | __func__, connectedPort.toString().c_str(), it->second.toString().c_str()); |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 1029 | mConnectedPorts.insert(connectedPort.id); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1030 | if (erasePortAfterConnectionIt != mPorts.end()) { |
| 1031 | mPorts.erase(erasePortAfterConnectionIt); |
| 1032 | } |
| 1033 | } else { // !connected |
| 1034 | AudioDevice matchDevice = devicePort.ext.get<AudioPortExt::device>().device; |
| 1035 | auto portsIt = findPort(matchDevice); |
| 1036 | if (portsIt == mPorts.end()) { |
| 1037 | // Since 'setConnectedState' is called for all modules, it is normal when the device |
| 1038 | // port not found in every one of them. |
| 1039 | return BAD_VALUE; |
| 1040 | } else { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 1041 | AUGMENT_LOG(D, "device port for device %s found", matchDevice.toString().c_str()); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1042 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1043 | |
| 1044 | // Disconnection of remote submix out with address "0" is a special case. We need to replace |
| 1045 | // the connected port entry with the "augmented template". |
| 1046 | const int32_t portId = portsIt->second.id; |
| 1047 | if (mRemoteSubmixOut.has_value() && matchDevice.type.type == AudioDeviceType::OUT_SUBMIX && |
| 1048 | matchDevice.address == AudioDeviceAddress::make<AudioDeviceAddress::id>( |
| 1049 | AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS)) { |
| 1050 | mDisconnectedPortReplacement = std::make_pair(portId, *mRemoteSubmixOut); |
| 1051 | auto& port = mDisconnectedPortReplacement.second; |
| 1052 | port.ext.get<AudioPortExt::Tag::device>().device = matchDevice; |
| 1053 | port.profiles = portsIt->second.profiles; |
| 1054 | } |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Patches may still exist, the framework may reset or update them later. |
| 1057 | // For disconnection to succeed, need to release these patches first. |
| 1058 | if (std::set<int32_t> patchIdsToRelease = getPatchIdsByPortId(portId); |
| 1059 | !patchIdsToRelease.empty()) { |
| 1060 | FwkPatches releasedPatches; |
| 1061 | status_t status = OK; |
| 1062 | for (int32_t patchId : patchIdsToRelease) { |
| 1063 | if (auto it = mPatches.find(patchId); it != mPatches.end()) { |
| 1064 | if (status = releaseAudioPatch(it); status != OK) break; |
| 1065 | releasedPatches.insert(std::make_pair(patchId, patchId)); |
| 1066 | } |
| 1067 | } |
| 1068 | resetUnusedPortConfigs(); |
Mikhail Naganov | 67c2f6d | 2024-03-18 09:48:27 -0700 | [diff] [blame] | 1069 | // Patches created by Hal2AidlMapper during stream creation and not "claimed" |
| 1070 | // by the framework must not be surfaced to it. |
| 1071 | for (auto& s : mStreams) { |
| 1072 | if (auto it = releasedPatches.find(s.second.second); it != releasedPatches.end()) { |
| 1073 | releasedPatches.erase(it); |
| 1074 | } |
| 1075 | } |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 1076 | mFwkPatches.merge(releasedPatches); |
| 1077 | LOG_ALWAYS_FATAL_IF(!releasedPatches.empty(), |
| 1078 | "mFwkPatches already contains some of released patches"); |
| 1079 | if (status != OK) return status; |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1080 | } |
Mikhail Naganov | a317a80 | 2024-03-15 18:03:10 +0000 | [diff] [blame] | 1081 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->disconnectExternalDevice(portId))); |
| 1082 | eraseConnectedPort(portId); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1083 | } |
| 1084 | return updateRoutes(); |
| 1085 | } |
| 1086 | |
| 1087 | status_t Hal2AidlMapper::updateAudioPort(int32_t portId, AudioPort* port) { |
| 1088 | const status_t status = statusTFromBinderStatus(mModule->getAudioPort(portId, port)); |
| 1089 | if (status == OK) { |
| 1090 | auto portIt = mPorts.find(portId); |
| 1091 | if (portIt != mPorts.end()) { |
jiabin | 255ff7f | 2024-01-11 00:24:47 +0000 | [diff] [blame] | 1092 | if (port->ext.getTag() == AudioPortExt::Tag::mix && portIt->second != *port) { |
| 1093 | mDynamicMixPortIds.insert(portId); |
| 1094 | } |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1095 | portIt->second = *port; |
| 1096 | } else { |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 1097 | AUGMENT_LOG(W, "port(%d) returned successfully from the HAL but not it is not cached", |
| 1098 | portId); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | return status; |
| 1102 | } |
| 1103 | |
| 1104 | status_t Hal2AidlMapper::updateRoutes() { |
| 1105 | RETURN_STATUS_IF_ERROR( |
| 1106 | statusTFromBinderStatus(mModule->getAudioRoutes(&mRoutes))); |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 1107 | AUGMENT_LOG_IF(W, mRoutes.empty(), "returned an empty list of audio routes"); |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1108 | if (mRemoteSubmixIn.has_value()) { |
| 1109 | // Remove mentions of the template remote submix input from routes. |
| 1110 | int32_t rSubmixInId = mRemoteSubmixIn->id; |
| 1111 | // Remove mentions of the template remote submix out only if it is not in mPorts |
| 1112 | // (that means there is a connected port in mPorts). |
| 1113 | int32_t rSubmixOutId = mPorts.find(mRemoteSubmixOut->id) == mPorts.end() ? |
| 1114 | mRemoteSubmixOut->id : -1; |
| 1115 | for (auto it = mRoutes.begin(); it != mRoutes.end();) { |
| 1116 | auto& route = *it; |
| 1117 | if (route.sinkPortId == rSubmixOutId) { |
| 1118 | it = mRoutes.erase(it); |
| 1119 | continue; |
| 1120 | } |
| 1121 | if (auto routeIt = std::find(route.sourcePortIds.begin(), route.sourcePortIds.end(), |
| 1122 | rSubmixInId); routeIt != route.sourcePortIds.end()) { |
| 1123 | route.sourcePortIds.erase(routeIt); |
| 1124 | if (route.sourcePortIds.empty()) { |
| 1125 | it = mRoutes.erase(it); |
| 1126 | continue; |
| 1127 | } |
| 1128 | } |
| 1129 | ++it; |
| 1130 | } |
| 1131 | } |
| 1132 | mRoutingMatrix.clear(); |
| 1133 | for (const auto& r : mRoutes) { |
| 1134 | for (auto portId : r.sourcePortIds) { |
| 1135 | mRoutingMatrix.emplace(r.sinkPortId, portId); |
| 1136 | mRoutingMatrix.emplace(portId, r.sinkPortId); |
| 1137 | } |
| 1138 | } |
| 1139 | return OK; |
| 1140 | } |
| 1141 | |
jiabin | 255ff7f | 2024-01-11 00:24:47 +0000 | [diff] [blame] | 1142 | void Hal2AidlMapper::updateDynamicMixPorts() { |
| 1143 | for (int32_t portId : mDynamicMixPortIds) { |
| 1144 | if (auto it = mPorts.find(portId); it != mPorts.end()) { |
| 1145 | updateAudioPort(portId, &it->second); |
| 1146 | } else { |
| 1147 | // This must not happen |
Jaideep Sharma | 145313e | 2024-08-14 14:51:24 +0530 | [diff] [blame^] | 1148 | AUGMENT_LOG(E, "cannot find port for id=%d", portId); |
jiabin | 255ff7f | 2024-01-11 00:24:47 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | |
Mikhail Naganov | ac9d4e7 | 2023-10-23 12:00:09 -0700 | [diff] [blame] | 1153 | } // namespace android |