Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <set> |
| 19 | |
| 20 | #define LOG_TAG "AHAL_Module" |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | |
| 23 | #include <aidl/android/media/audio/common/AudioOutputFlags.h> |
| 24 | |
| 25 | #include "core-impl/Module.h" |
| 26 | #include "core-impl/utils.h" |
| 27 | |
| 28 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 29 | using aidl::android::hardware::audio::common::SourceMetadata; |
| 30 | using aidl::android::media::audio::common::AudioFormatDescription; |
| 31 | using aidl::android::media::audio::common::AudioIoFlags; |
| 32 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 33 | using aidl::android::media::audio::common::AudioOutputFlags; |
| 34 | using aidl::android::media::audio::common::AudioPort; |
| 35 | using aidl::android::media::audio::common::AudioPortConfig; |
| 36 | using aidl::android::media::audio::common::AudioPortExt; |
| 37 | using aidl::android::media::audio::common::AudioProfile; |
| 38 | using aidl::android::media::audio::common::Int; |
| 39 | |
| 40 | namespace aidl::android::hardware::audio::core { |
| 41 | |
| 42 | namespace { |
| 43 | |
| 44 | bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) { |
| 45 | *config = {}; |
| 46 | config->portId = port.id; |
| 47 | if (port.profiles.empty()) { |
| 48 | LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles"; |
| 49 | return false; |
| 50 | } |
| 51 | const auto& profile = port.profiles.begin(); |
| 52 | config->format = profile->format; |
| 53 | if (profile->channelMasks.empty()) { |
| 54 | LOG(ERROR) << __func__ << ": the first profile in port " << port.id |
| 55 | << " has no channel masks"; |
| 56 | return false; |
| 57 | } |
| 58 | config->channelMask = *profile->channelMasks.begin(); |
| 59 | if (profile->sampleRates.empty()) { |
| 60 | LOG(ERROR) << __func__ << ": the first profile in port " << port.id |
| 61 | << " has no sample rates"; |
| 62 | return false; |
| 63 | } |
| 64 | Int sampleRate; |
| 65 | sampleRate.value = *profile->sampleRates.begin(); |
| 66 | config->sampleRate = sampleRate; |
| 67 | config->flags = port.flags; |
| 68 | config->ext = port.ext; |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format, |
| 73 | AudioProfile* profile) { |
| 74 | if (auto profilesIt = |
| 75 | find_if(port.profiles.begin(), port.profiles.end(), |
| 76 | [&format](const auto& profile) { return profile.format == format; }); |
| 77 | profilesIt != port.profiles.end()) { |
| 78 | *profile = *profilesIt; |
| 79 | return true; |
| 80 | } |
| 81 | return false; |
| 82 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 83 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 84 | } // namespace |
| 85 | |
| 86 | void Module::cleanUpPatch(int32_t patchId) { |
| 87 | erase_all_values(mPatches, std::set<int32_t>{patchId}); |
| 88 | } |
| 89 | |
| 90 | void Module::cleanUpPatches(int32_t portConfigId) { |
| 91 | auto& patches = getConfig().patches; |
| 92 | if (patches.size() == 0) return; |
| 93 | auto range = mPatches.equal_range(portConfigId); |
| 94 | for (auto it = range.first; it != range.second; ++it) { |
| 95 | auto patchIt = findById<AudioPatch>(patches, it->second); |
| 96 | if (patchIt != patches.end()) { |
| 97 | erase_if(patchIt->sourcePortConfigIds, |
| 98 | [portConfigId](auto e) { return e == portConfigId; }); |
| 99 | erase_if(patchIt->sinkPortConfigIds, |
| 100 | [portConfigId](auto e) { return e == portConfigId; }); |
| 101 | } |
| 102 | } |
| 103 | std::set<int32_t> erasedPatches; |
| 104 | for (size_t i = patches.size() - 1; i != 0; --i) { |
| 105 | const auto& patch = patches[i]; |
| 106 | if (patch.sourcePortConfigIds.empty() || patch.sinkPortConfigIds.empty()) { |
| 107 | erasedPatches.insert(patch.id); |
| 108 | patches.erase(patches.begin() + i); |
| 109 | } |
| 110 | } |
| 111 | erase_all_values(mPatches, erasedPatches); |
| 112 | } |
| 113 | |
| 114 | internal::Configuration& Module::getConfig() { |
| 115 | if (!mConfig) { |
| 116 | mConfig.reset(new internal::Configuration(internal::getNullPrimaryConfiguration())); |
| 117 | } |
| 118 | return *mConfig; |
| 119 | } |
| 120 | |
| 121 | void Module::registerPatch(const AudioPatch& patch) { |
| 122 | auto& configs = getConfig().portConfigs; |
| 123 | auto do_insert = [&](const std::vector<int32_t>& portConfigIds) { |
| 124 | for (auto portConfigId : portConfigIds) { |
| 125 | auto configIt = findById<AudioPortConfig>(configs, portConfigId); |
| 126 | if (configIt != configs.end()) { |
| 127 | mPatches.insert(std::pair{portConfigId, patch.id}); |
| 128 | if (configIt->portId != portConfigId) { |
| 129 | mPatches.insert(std::pair{configIt->portId, patch.id}); |
| 130 | } |
| 131 | } |
| 132 | }; |
| 133 | }; |
| 134 | do_insert(patch.sourcePortConfigIds); |
| 135 | do_insert(patch.sinkPortConfigIds); |
| 136 | } |
| 137 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 138 | ndk::ScopedAStatus Module::setModuleDebug( |
| 139 | const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) { |
| 140 | LOG(DEBUG) << __func__ << ": old flags:" << mDebug.toString() |
| 141 | << ", new flags: " << in_debug.toString(); |
| 142 | if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections && |
| 143 | !mConnectedDevicePorts.empty()) { |
| 144 | LOG(ERROR) << __func__ << ": attempting to change device connections simulation " |
| 145 | << "while having external devices connected"; |
| 146 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 147 | } |
| 148 | mDebug = in_debug; |
| 149 | return ndk::ScopedAStatus::ok(); |
| 150 | } |
| 151 | |
| 152 | ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData, |
| 153 | AudioPort* _aidl_return) { |
| 154 | const int32_t templateId = in_templateIdAndAdditionalData.id; |
| 155 | auto& ports = getConfig().ports; |
| 156 | AudioPort connectedPort; |
| 157 | { // Scope the template port so that we don't accidentally modify it. |
| 158 | auto templateIt = findById<AudioPort>(ports, templateId); |
| 159 | if (templateIt == ports.end()) { |
| 160 | LOG(ERROR) << __func__ << ": port id " << templateId << " not found"; |
| 161 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 162 | } |
| 163 | if (templateIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 164 | LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port"; |
| 165 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 166 | } |
| 167 | if (!templateIt->profiles.empty()) { |
| 168 | LOG(ERROR) << __func__ << ": port id " << templateId |
| 169 | << " does not have dynamic profiles"; |
| 170 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 171 | } |
| 172 | auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>(); |
| 173 | if (templateDevicePort.device.type.connection.empty()) { |
| 174 | LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached"; |
| 175 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 176 | } |
| 177 | // Postpone id allocation until we ensure that there are no client errors. |
| 178 | connectedPort = *templateIt; |
| 179 | connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors; |
| 180 | const auto& inputDevicePort = |
| 181 | in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>(); |
| 182 | auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>(); |
| 183 | connectedDevicePort.device.address = inputDevicePort.device.address; |
| 184 | LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to " |
| 185 | << connectedDevicePort.device.toString(); |
| 186 | // Check if there is already a connected port with for the same external device. |
| 187 | for (auto connectedPortId : mConnectedDevicePorts) { |
| 188 | auto connectedPortIt = findById<AudioPort>(ports, connectedPortId); |
| 189 | if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device == |
| 190 | connectedDevicePort.device) { |
| 191 | LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString() |
| 192 | << " is already connected at the device port id " << connectedPortId; |
| 193 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if (!mDebug.simulateDeviceConnections) { |
| 199 | // In a real HAL here we would attempt querying the profiles from the device. |
| 200 | LOG(ERROR) << __func__ << ": failed to query supported device profiles"; |
| 201 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 202 | } |
| 203 | |
| 204 | connectedPort.id = ++getConfig().nextPortId; |
| 205 | mConnectedDevicePorts.insert(connectedPort.id); |
| 206 | LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, " |
| 207 | << "connected port ID " << connectedPort.id; |
| 208 | auto& connectedProfiles = getConfig().connectedProfiles; |
| 209 | if (auto connectedProfilesIt = connectedProfiles.find(templateId); |
| 210 | connectedProfilesIt != connectedProfiles.end()) { |
| 211 | connectedPort.profiles = connectedProfilesIt->second; |
| 212 | } |
| 213 | ports.push_back(connectedPort); |
| 214 | *_aidl_return = std::move(connectedPort); |
| 215 | |
| 216 | std::vector<AudioRoute> newRoutes; |
| 217 | auto& routes = getConfig().routes; |
| 218 | for (auto& r : routes) { |
| 219 | if (r.sinkPortId == templateId) { |
| 220 | AudioRoute newRoute; |
| 221 | newRoute.sourcePortIds = r.sourcePortIds; |
| 222 | newRoute.sinkPortId = connectedPort.id; |
| 223 | newRoute.isExclusive = r.isExclusive; |
| 224 | newRoutes.push_back(std::move(newRoute)); |
| 225 | } else { |
| 226 | auto& srcs = r.sourcePortIds; |
| 227 | if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) { |
| 228 | srcs.push_back(connectedPort.id); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | routes.insert(routes.end(), newRoutes.begin(), newRoutes.end()); |
| 233 | |
| 234 | return ndk::ScopedAStatus::ok(); |
| 235 | } |
| 236 | |
| 237 | ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) { |
| 238 | auto& ports = getConfig().ports; |
| 239 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 240 | if (portIt == ports.end()) { |
| 241 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 242 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 243 | } |
| 244 | if (portIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 245 | LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port"; |
| 246 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 247 | } |
| 248 | if (mConnectedDevicePorts.count(in_portId) == 0) { |
| 249 | LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port"; |
| 250 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 251 | } |
| 252 | auto& configs = getConfig().portConfigs; |
| 253 | auto& initials = getConfig().initialConfigs; |
| 254 | auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) { |
| 255 | if (config.portId == in_portId) { |
| 256 | // Check if the configuration was provided by the client. |
| 257 | const auto& initialIt = findById<AudioPortConfig>(initials, config.id); |
| 258 | return initialIt == initials.end() || config != *initialIt; |
| 259 | } |
| 260 | return false; |
| 261 | }); |
| 262 | if (configIt != configs.end()) { |
| 263 | LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id " |
| 264 | << configIt->id; |
| 265 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 266 | } |
| 267 | ports.erase(portIt); |
| 268 | mConnectedDevicePorts.erase(in_portId); |
| 269 | LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released"; |
| 270 | |
| 271 | auto& routes = getConfig().routes; |
| 272 | for (auto routesIt = routes.begin(); routesIt != routes.end();) { |
| 273 | if (routesIt->sinkPortId == in_portId) { |
| 274 | routesIt = routes.erase(routesIt); |
| 275 | } else { |
| 276 | // Note: the list of sourcePortIds can't become empty because there must |
| 277 | // be the id of the template port in the route. |
| 278 | erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; }); |
| 279 | ++routesIt; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return ndk::ScopedAStatus::ok(); |
| 284 | } |
| 285 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 286 | ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) { |
| 287 | *_aidl_return = getConfig().patches; |
| 288 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches"; |
| 289 | return ndk::ScopedAStatus::ok(); |
| 290 | } |
| 291 | |
| 292 | ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) { |
| 293 | auto& ports = getConfig().ports; |
| 294 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 295 | if (portIt != ports.end()) { |
| 296 | *_aidl_return = *portIt; |
| 297 | LOG(DEBUG) << __func__ << ": returning port by id " << in_portId; |
| 298 | return ndk::ScopedAStatus::ok(); |
| 299 | } |
| 300 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 301 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 302 | } |
| 303 | |
| 304 | ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) { |
| 305 | *_aidl_return = getConfig().portConfigs; |
| 306 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs"; |
| 307 | return ndk::ScopedAStatus::ok(); |
| 308 | } |
| 309 | |
| 310 | ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) { |
| 311 | *_aidl_return = getConfig().ports; |
| 312 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports"; |
| 313 | return ndk::ScopedAStatus::ok(); |
| 314 | } |
| 315 | |
| 316 | ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) { |
| 317 | *_aidl_return = getConfig().routes; |
| 318 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes"; |
| 319 | return ndk::ScopedAStatus::ok(); |
| 320 | } |
| 321 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 322 | ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId, |
| 323 | std::vector<AudioRoute>* _aidl_return) { |
| 324 | auto& ports = getConfig().ports; |
| 325 | if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) { |
| 326 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 327 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 328 | } |
| 329 | auto& routes = getConfig().routes; |
| 330 | std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return), |
| 331 | [&](const auto& r) { |
| 332 | const auto& srcs = r.sourcePortIds; |
| 333 | return r.sinkPortId == in_portId || |
| 334 | std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end(); |
| 335 | }); |
| 336 | return ndk::ScopedAStatus::ok(); |
| 337 | } |
| 338 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 339 | ndk::ScopedAStatus Module::openInputStream(int32_t in_portConfigId, |
| 340 | const SinkMetadata& in_sinkMetadata, |
| 341 | std::shared_ptr<IStreamIn>* _aidl_return) { |
| 342 | auto& configs = getConfig().portConfigs; |
| 343 | auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 344 | if (portConfigIt == configs.end()) { |
| 345 | LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found"; |
| 346 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 347 | } |
| 348 | const int32_t portId = portConfigIt->portId; |
| 349 | // In our implementation, configs of mix ports always have unique IDs. |
| 350 | CHECK(portId != in_portConfigId); |
| 351 | auto& ports = getConfig().ports; |
| 352 | auto portIt = findById<AudioPort>(ports, portId); |
| 353 | if (portIt == ports.end()) { |
| 354 | LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id " |
| 355 | << in_portConfigId << " not found"; |
| 356 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 357 | } |
| 358 | if (portIt->flags.getTag() != AudioIoFlags::Tag::input || |
| 359 | portIt->ext.getTag() != AudioPortExt::Tag::mix) { |
| 360 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 361 | << " does not correspond to an input mix port"; |
| 362 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 363 | } |
| 364 | if (mStreams.count(in_portConfigId) != 0) { |
| 365 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 366 | << " already has a stream opened on it"; |
| 367 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 368 | } |
| 369 | const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount; |
| 370 | if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) { |
| 371 | LOG(ERROR) << __func__ << ": port id " << portId |
| 372 | << " has already reached maximum allowed opened stream count: " |
| 373 | << maxOpenStreamCount; |
| 374 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 375 | } |
| 376 | auto stream = ndk::SharedRefBase::make<StreamIn>(in_sinkMetadata); |
| 377 | mStreams.insert(portId, in_portConfigId, StreamWrapper(stream)); |
| 378 | *_aidl_return = std::move(stream); |
| 379 | return ndk::ScopedAStatus::ok(); |
| 380 | } |
| 381 | |
| 382 | ndk::ScopedAStatus Module::openOutputStream(int32_t in_portConfigId, |
| 383 | const SourceMetadata& in_sourceMetadata, |
| 384 | const std::optional<AudioOffloadInfo>& in_offloadInfo, |
| 385 | std::shared_ptr<IStreamOut>* _aidl_return) { |
| 386 | auto& configs = getConfig().portConfigs; |
| 387 | auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 388 | if (portConfigIt == configs.end()) { |
| 389 | LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found"; |
| 390 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 391 | } |
| 392 | const int32_t portId = portConfigIt->portId; |
| 393 | // In our implementation, configs of mix ports always have unique IDs. |
| 394 | CHECK(portId != in_portConfigId); |
| 395 | auto& ports = getConfig().ports; |
| 396 | auto portIt = findById<AudioPort>(ports, portId); |
| 397 | if (portIt == ports.end()) { |
| 398 | LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id " |
| 399 | << in_portConfigId << " not found"; |
| 400 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 401 | } |
| 402 | if (portIt->flags.getTag() != AudioIoFlags::Tag::output || |
| 403 | portIt->ext.getTag() != AudioPortExt::Tag::mix) { |
| 404 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 405 | << " does not correspond to an output mix port"; |
| 406 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 407 | } |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 408 | if (portConfigIt->flags.has_value() && |
| 409 | ((portConfigIt->flags.value().get<AudioIoFlags::Tag::output>() & |
| 410 | 1 << static_cast<int32_t>(AudioOutputFlags::COMPRESS_OFFLOAD)) != 0) && |
| 411 | !in_offloadInfo.has_value()) { |
| 412 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 413 | << " has COMPRESS_OFFLOAD flag set, requires offload info"; |
| 414 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 415 | } |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 416 | if (mStreams.count(in_portConfigId) != 0) { |
| 417 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 418 | << " already has a stream opened on it"; |
| 419 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 420 | } |
| 421 | const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount; |
| 422 | if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) { |
| 423 | LOG(ERROR) << __func__ << ": port id " << portId |
| 424 | << " has already reached maximum allowed opened stream count: " |
| 425 | << maxOpenStreamCount; |
| 426 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 427 | } |
| 428 | auto stream = ndk::SharedRefBase::make<StreamOut>(in_sourceMetadata, in_offloadInfo); |
| 429 | mStreams.insert(portId, in_portConfigId, StreamWrapper(stream)); |
| 430 | *_aidl_return = std::move(stream); |
| 431 | return ndk::ScopedAStatus::ok(); |
| 432 | } |
| 433 | |
| 434 | ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) { |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 435 | LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString(); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 436 | if (in_requested.sourcePortConfigIds.empty()) { |
| 437 | LOG(ERROR) << __func__ << ": requested patch has empty sources list"; |
| 438 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 439 | } |
| 440 | if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) { |
| 441 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list"; |
| 442 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 443 | } |
| 444 | if (in_requested.sinkPortConfigIds.empty()) { |
| 445 | LOG(ERROR) << __func__ << ": requested patch has empty sinks list"; |
| 446 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 447 | } |
| 448 | if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) { |
| 449 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list"; |
| 450 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 451 | } |
| 452 | |
| 453 | auto& configs = getConfig().portConfigs; |
| 454 | std::vector<int32_t> missingIds; |
| 455 | auto sources = |
| 456 | selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds); |
| 457 | if (!missingIds.empty()) { |
| 458 | LOG(ERROR) << __func__ << ": following source port config ids not found: " |
| 459 | << ::android::internal::ToString(missingIds); |
| 460 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 461 | } |
| 462 | auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds); |
| 463 | if (!missingIds.empty()) { |
| 464 | LOG(ERROR) << __func__ << ": following sink port config ids not found: " |
| 465 | << ::android::internal::ToString(missingIds); |
| 466 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 467 | } |
| 468 | // bool indicates whether a non-exclusive route is available. |
| 469 | // If only an exclusive route is available, that means the patch can not be |
| 470 | // established if there is any other patch which currently uses the sink port. |
| 471 | std::map<int32_t, bool> allowedSinkPorts; |
| 472 | auto& routes = getConfig().routes; |
| 473 | for (auto src : sources) { |
| 474 | for (const auto& r : routes) { |
| 475 | const auto& srcs = r.sourcePortIds; |
| 476 | if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) { |
| 477 | if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive |
| 478 | allowedSinkPorts[r.sinkPortId] = !r.isExclusive; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | for (auto sink : sinks) { |
| 484 | if (allowedSinkPorts.count(sink->portId) == 0) { |
| 485 | LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId; |
| 486 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | auto& patches = getConfig().patches; |
| 491 | auto existing = patches.end(); |
| 492 | std::optional<decltype(mPatches)> patchesBackup; |
| 493 | if (in_requested.id != 0) { |
| 494 | existing = findById<AudioPatch>(patches, in_requested.id); |
| 495 | if (existing != patches.end()) { |
| 496 | patchesBackup = mPatches; |
| 497 | cleanUpPatch(existing->id); |
| 498 | } else { |
| 499 | LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id; |
| 500 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 501 | } |
| 502 | } |
| 503 | // Validate the requested patch. |
| 504 | for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) { |
| 505 | if (!nonExclusive && mPatches.count(sinkPortId) != 0) { |
| 506 | LOG(ERROR) << __func__ << ": sink port id " << sinkPortId |
| 507 | << "is exclusive and is already used by some other patch"; |
| 508 | if (patchesBackup.has_value()) { |
| 509 | mPatches = std::move(*patchesBackup); |
| 510 | } |
| 511 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 512 | } |
| 513 | } |
| 514 | *_aidl_return = in_requested; |
| 515 | if (existing == patches.end()) { |
| 516 | _aidl_return->id = getConfig().nextPatchId++; |
| 517 | patches.push_back(*_aidl_return); |
| 518 | existing = patches.begin() + (patches.size() - 1); |
| 519 | } else { |
| 520 | *existing = *_aidl_return; |
| 521 | } |
| 522 | registerPatch(*existing); |
| 523 | LOG(DEBUG) << __func__ << ": created or updated patch id " << _aidl_return->id; |
| 524 | return ndk::ScopedAStatus::ok(); |
| 525 | } |
| 526 | |
| 527 | ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested, |
| 528 | AudioPortConfig* out_suggested, bool* _aidl_return) { |
| 529 | LOG(DEBUG) << __func__ << ": requested " << in_requested.toString(); |
| 530 | auto& configs = getConfig().portConfigs; |
| 531 | auto existing = configs.end(); |
| 532 | if (in_requested.id != 0) { |
| 533 | if (existing = findById<AudioPortConfig>(configs, in_requested.id); |
| 534 | existing == configs.end()) { |
| 535 | LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id |
| 536 | << " not found"; |
| 537 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | const int portId = existing != configs.end() ? existing->portId : in_requested.portId; |
| 542 | if (portId == 0) { |
| 543 | LOG(ERROR) << __func__ << ": input port config does not specify portId"; |
| 544 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 545 | } |
| 546 | auto& ports = getConfig().ports; |
| 547 | auto portIt = findById<AudioPort>(ports, portId); |
| 548 | if (portIt == ports.end()) { |
| 549 | LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId; |
| 550 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 551 | } |
| 552 | if (existing != configs.end()) { |
| 553 | *out_suggested = *existing; |
| 554 | } else { |
| 555 | AudioPortConfig newConfig; |
| 556 | if (generateDefaultPortConfig(*portIt, &newConfig)) { |
| 557 | *out_suggested = newConfig; |
| 558 | } else { |
| 559 | LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId; |
| 560 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 561 | } |
| 562 | } |
| 563 | // From this moment, 'out_suggested' is either an existing port config, |
| 564 | // or a new generated config. Now attempt to update it according to the specified |
| 565 | // fields of 'in_requested'. |
| 566 | |
| 567 | bool requestedIsValid = true, requestedIsFullySpecified = true; |
| 568 | |
| 569 | AudioIoFlags portFlags = portIt->flags; |
| 570 | if (in_requested.flags.has_value()) { |
| 571 | if (in_requested.flags.value() != portFlags) { |
| 572 | LOG(WARNING) << __func__ << ": requested flags " |
| 573 | << in_requested.flags.value().toString() << " do not match port's " |
| 574 | << portId << " flags " << portFlags.toString(); |
| 575 | requestedIsValid = false; |
| 576 | } |
| 577 | } else { |
| 578 | requestedIsFullySpecified = false; |
| 579 | } |
| 580 | |
| 581 | AudioProfile portProfile; |
| 582 | if (in_requested.format.has_value()) { |
| 583 | const auto& format = in_requested.format.value(); |
| 584 | if (findAudioProfile(*portIt, format, &portProfile)) { |
| 585 | out_suggested->format = format; |
| 586 | } else { |
| 587 | LOG(WARNING) << __func__ << ": requested format " << format.toString() |
| 588 | << " is not found in port's " << portId << " profiles"; |
| 589 | requestedIsValid = false; |
| 590 | } |
| 591 | } else { |
| 592 | requestedIsFullySpecified = false; |
| 593 | } |
| 594 | if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) { |
| 595 | LOG(ERROR) << __func__ << ": port " << portId << " does not support format " |
| 596 | << out_suggested->format.value().toString() << " anymore"; |
| 597 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 598 | } |
| 599 | |
| 600 | if (in_requested.channelMask.has_value()) { |
| 601 | const auto& channelMask = in_requested.channelMask.value(); |
| 602 | if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) != |
| 603 | portProfile.channelMasks.end()) { |
| 604 | out_suggested->channelMask = channelMask; |
| 605 | } else { |
| 606 | LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString() |
| 607 | << " is not supported for the format " << portProfile.format.toString() |
| 608 | << " by the port " << portId; |
| 609 | requestedIsValid = false; |
| 610 | } |
| 611 | } else { |
| 612 | requestedIsFullySpecified = false; |
| 613 | } |
| 614 | |
| 615 | if (in_requested.sampleRate.has_value()) { |
| 616 | const auto& sampleRate = in_requested.sampleRate.value(); |
| 617 | if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(), |
| 618 | sampleRate.value) != portProfile.sampleRates.end()) { |
| 619 | out_suggested->sampleRate = sampleRate; |
| 620 | } else { |
| 621 | LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value |
| 622 | << " is not supported for the format " << portProfile.format.toString() |
| 623 | << " by the port " << portId; |
| 624 | requestedIsValid = false; |
| 625 | } |
| 626 | } else { |
| 627 | requestedIsFullySpecified = false; |
| 628 | } |
| 629 | |
| 630 | if (in_requested.gain.has_value()) { |
| 631 | // Let's pretend that gain can always be applied. |
| 632 | out_suggested->gain = in_requested.gain.value(); |
| 633 | } |
| 634 | |
| 635 | if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) { |
| 636 | out_suggested->id = getConfig().nextPortId++; |
| 637 | configs.push_back(*out_suggested); |
| 638 | *_aidl_return = true; |
| 639 | LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString(); |
| 640 | } else if (existing != configs.end() && requestedIsValid) { |
| 641 | *existing = *out_suggested; |
| 642 | *_aidl_return = true; |
| 643 | LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString(); |
| 644 | } else { |
| 645 | LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end()) |
| 646 | << "; requested is valid? " << requestedIsValid << ", fully specified? " |
| 647 | << requestedIsFullySpecified; |
| 648 | *_aidl_return = false; |
| 649 | } |
| 650 | return ndk::ScopedAStatus::ok(); |
| 651 | } |
| 652 | |
| 653 | ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) { |
| 654 | auto& patches = getConfig().patches; |
| 655 | auto patchIt = findById<AudioPatch>(patches, in_patchId); |
| 656 | if (patchIt != patches.end()) { |
| 657 | cleanUpPatch(patchIt->id); |
| 658 | patches.erase(patchIt); |
| 659 | LOG(DEBUG) << __func__ << ": erased patch " << in_patchId; |
| 660 | return ndk::ScopedAStatus::ok(); |
| 661 | } |
| 662 | LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found"; |
| 663 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 664 | } |
| 665 | |
| 666 | ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) { |
| 667 | auto& configs = getConfig().portConfigs; |
| 668 | auto configIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 669 | if (configIt != configs.end()) { |
| 670 | if (mStreams.count(in_portConfigId) != 0) { |
| 671 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 672 | << " has a stream opened on it"; |
| 673 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 674 | } |
| 675 | auto patchIt = mPatches.find(in_portConfigId); |
| 676 | if (patchIt != mPatches.end()) { |
| 677 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 678 | << " is used by the patch with id " << patchIt->second; |
| 679 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 680 | } |
| 681 | auto& initials = getConfig().initialConfigs; |
| 682 | auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId); |
| 683 | if (initialIt == initials.end()) { |
| 684 | configs.erase(configIt); |
| 685 | LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId; |
| 686 | } else if (*configIt != *initialIt) { |
| 687 | *configIt = *initialIt; |
| 688 | LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId; |
| 689 | } |
| 690 | return ndk::ScopedAStatus::ok(); |
| 691 | } |
| 692 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found"; |
| 693 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 694 | } |
| 695 | |
| 696 | } // namespace aidl::android::hardware::audio::core |