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