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" |
| 21 | #define LOG_NDEBUG 0 |
| 22 | #include <android-base/logging.h> |
| 23 | |
| 24 | #include <aidl/android/media/audio/common/AudioOutputFlags.h> |
| 25 | |
| 26 | #include "core-impl/Module.h" |
| 27 | #include "core-impl/utils.h" |
| 28 | |
| 29 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 30 | using aidl::android::hardware::audio::common::SourceMetadata; |
| 31 | using aidl::android::media::audio::common::AudioFormatDescription; |
| 32 | using aidl::android::media::audio::common::AudioIoFlags; |
| 33 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 34 | using aidl::android::media::audio::common::AudioOutputFlags; |
| 35 | using aidl::android::media::audio::common::AudioPort; |
| 36 | using aidl::android::media::audio::common::AudioPortConfig; |
| 37 | using aidl::android::media::audio::common::AudioPortExt; |
| 38 | using aidl::android::media::audio::common::AudioProfile; |
| 39 | using aidl::android::media::audio::common::Int; |
| 40 | |
| 41 | namespace aidl::android::hardware::audio::core { |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) { |
| 46 | *config = {}; |
| 47 | config->portId = port.id; |
| 48 | if (port.profiles.empty()) { |
| 49 | LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles"; |
| 50 | return false; |
| 51 | } |
| 52 | const auto& profile = port.profiles.begin(); |
| 53 | config->format = profile->format; |
| 54 | if (profile->channelMasks.empty()) { |
| 55 | LOG(ERROR) << __func__ << ": the first profile in port " << port.id |
| 56 | << " has no channel masks"; |
| 57 | return false; |
| 58 | } |
| 59 | config->channelMask = *profile->channelMasks.begin(); |
| 60 | if (profile->sampleRates.empty()) { |
| 61 | LOG(ERROR) << __func__ << ": the first profile in port " << port.id |
| 62 | << " has no sample rates"; |
| 63 | return false; |
| 64 | } |
| 65 | Int sampleRate; |
| 66 | sampleRate.value = *profile->sampleRates.begin(); |
| 67 | config->sampleRate = sampleRate; |
| 68 | config->flags = port.flags; |
| 69 | config->ext = port.ext; |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format, |
| 74 | AudioProfile* profile) { |
| 75 | if (auto profilesIt = |
| 76 | find_if(port.profiles.begin(), port.profiles.end(), |
| 77 | [&format](const auto& profile) { return profile.format == format; }); |
| 78 | profilesIt != port.profiles.end()) { |
| 79 | *profile = *profilesIt; |
| 80 | return true; |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 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 | |
| 138 | ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) { |
| 139 | *_aidl_return = getConfig().patches; |
| 140 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches"; |
| 141 | return ndk::ScopedAStatus::ok(); |
| 142 | } |
| 143 | |
| 144 | ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) { |
| 145 | auto& ports = getConfig().ports; |
| 146 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 147 | if (portIt != ports.end()) { |
| 148 | *_aidl_return = *portIt; |
| 149 | LOG(DEBUG) << __func__ << ": returning port by id " << in_portId; |
| 150 | return ndk::ScopedAStatus::ok(); |
| 151 | } |
| 152 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 153 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 154 | } |
| 155 | |
| 156 | ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) { |
| 157 | *_aidl_return = getConfig().portConfigs; |
| 158 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs"; |
| 159 | return ndk::ScopedAStatus::ok(); |
| 160 | } |
| 161 | |
| 162 | ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) { |
| 163 | *_aidl_return = getConfig().ports; |
| 164 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports"; |
| 165 | return ndk::ScopedAStatus::ok(); |
| 166 | } |
| 167 | |
| 168 | ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) { |
| 169 | *_aidl_return = getConfig().routes; |
| 170 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes"; |
| 171 | return ndk::ScopedAStatus::ok(); |
| 172 | } |
| 173 | |
| 174 | ndk::ScopedAStatus Module::openInputStream(int32_t in_portConfigId, |
| 175 | const SinkMetadata& in_sinkMetadata, |
| 176 | std::shared_ptr<IStreamIn>* _aidl_return) { |
| 177 | auto& configs = getConfig().portConfigs; |
| 178 | auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 179 | if (portConfigIt == configs.end()) { |
| 180 | LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found"; |
| 181 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 182 | } |
| 183 | const int32_t portId = portConfigIt->portId; |
| 184 | // In our implementation, configs of mix ports always have unique IDs. |
| 185 | CHECK(portId != in_portConfigId); |
| 186 | auto& ports = getConfig().ports; |
| 187 | auto portIt = findById<AudioPort>(ports, portId); |
| 188 | if (portIt == ports.end()) { |
| 189 | LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id " |
| 190 | << in_portConfigId << " not found"; |
| 191 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 192 | } |
| 193 | if (portIt->flags.getTag() != AudioIoFlags::Tag::input || |
| 194 | portIt->ext.getTag() != AudioPortExt::Tag::mix) { |
| 195 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 196 | << " does not correspond to an input mix port"; |
| 197 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 198 | } |
| 199 | if (mStreams.count(in_portConfigId) != 0) { |
| 200 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 201 | << " already has a stream opened on it"; |
| 202 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 203 | } |
| 204 | const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount; |
| 205 | if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) { |
| 206 | LOG(ERROR) << __func__ << ": port id " << portId |
| 207 | << " has already reached maximum allowed opened stream count: " |
| 208 | << maxOpenStreamCount; |
| 209 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 210 | } |
| 211 | auto stream = ndk::SharedRefBase::make<StreamIn>(in_sinkMetadata); |
| 212 | mStreams.insert(portId, in_portConfigId, StreamWrapper(stream)); |
| 213 | *_aidl_return = std::move(stream); |
| 214 | return ndk::ScopedAStatus::ok(); |
| 215 | } |
| 216 | |
| 217 | ndk::ScopedAStatus Module::openOutputStream(int32_t in_portConfigId, |
| 218 | const SourceMetadata& in_sourceMetadata, |
| 219 | const std::optional<AudioOffloadInfo>& in_offloadInfo, |
| 220 | std::shared_ptr<IStreamOut>* _aidl_return) { |
| 221 | auto& configs = getConfig().portConfigs; |
| 222 | auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 223 | if (portConfigIt == configs.end()) { |
| 224 | LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found"; |
| 225 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 226 | } |
| 227 | const int32_t portId = portConfigIt->portId; |
| 228 | // In our implementation, configs of mix ports always have unique IDs. |
| 229 | CHECK(portId != in_portConfigId); |
| 230 | auto& ports = getConfig().ports; |
| 231 | auto portIt = findById<AudioPort>(ports, portId); |
| 232 | if (portIt == ports.end()) { |
| 233 | LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id " |
| 234 | << in_portConfigId << " not found"; |
| 235 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 236 | } |
| 237 | if (portIt->flags.getTag() != AudioIoFlags::Tag::output || |
| 238 | portIt->ext.getTag() != AudioPortExt::Tag::mix) { |
| 239 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 240 | << " does not correspond to an output mix port"; |
| 241 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 242 | } |
| 243 | if (mStreams.count(in_portConfigId) != 0) { |
| 244 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 245 | << " already has a stream opened on it"; |
| 246 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 247 | } |
| 248 | const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount; |
| 249 | if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) { |
| 250 | LOG(ERROR) << __func__ << ": port id " << portId |
| 251 | << " has already reached maximum allowed opened stream count: " |
| 252 | << maxOpenStreamCount; |
| 253 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 254 | } |
| 255 | auto stream = ndk::SharedRefBase::make<StreamOut>(in_sourceMetadata, in_offloadInfo); |
| 256 | mStreams.insert(portId, in_portConfigId, StreamWrapper(stream)); |
| 257 | *_aidl_return = std::move(stream); |
| 258 | return ndk::ScopedAStatus::ok(); |
| 259 | } |
| 260 | |
| 261 | ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) { |
| 262 | if (in_requested.sourcePortConfigIds.empty()) { |
| 263 | LOG(ERROR) << __func__ << ": requested patch has empty sources list"; |
| 264 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 265 | } |
| 266 | if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) { |
| 267 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list"; |
| 268 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 269 | } |
| 270 | if (in_requested.sinkPortConfigIds.empty()) { |
| 271 | LOG(ERROR) << __func__ << ": requested patch has empty sinks list"; |
| 272 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 273 | } |
| 274 | if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) { |
| 275 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list"; |
| 276 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 277 | } |
| 278 | |
| 279 | auto& configs = getConfig().portConfigs; |
| 280 | std::vector<int32_t> missingIds; |
| 281 | auto sources = |
| 282 | selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds); |
| 283 | if (!missingIds.empty()) { |
| 284 | LOG(ERROR) << __func__ << ": following source port config ids not found: " |
| 285 | << ::android::internal::ToString(missingIds); |
| 286 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 287 | } |
| 288 | auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds); |
| 289 | if (!missingIds.empty()) { |
| 290 | LOG(ERROR) << __func__ << ": following sink port config ids not found: " |
| 291 | << ::android::internal::ToString(missingIds); |
| 292 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 293 | } |
| 294 | // bool indicates whether a non-exclusive route is available. |
| 295 | // If only an exclusive route is available, that means the patch can not be |
| 296 | // established if there is any other patch which currently uses the sink port. |
| 297 | std::map<int32_t, bool> allowedSinkPorts; |
| 298 | auto& routes = getConfig().routes; |
| 299 | for (auto src : sources) { |
| 300 | for (const auto& r : routes) { |
| 301 | const auto& srcs = r.sourcePortIds; |
| 302 | if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) { |
| 303 | if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive |
| 304 | allowedSinkPorts[r.sinkPortId] = !r.isExclusive; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | for (auto sink : sinks) { |
| 310 | if (allowedSinkPorts.count(sink->portId) == 0) { |
| 311 | LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId; |
| 312 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | auto& patches = getConfig().patches; |
| 317 | auto existing = patches.end(); |
| 318 | std::optional<decltype(mPatches)> patchesBackup; |
| 319 | if (in_requested.id != 0) { |
| 320 | existing = findById<AudioPatch>(patches, in_requested.id); |
| 321 | if (existing != patches.end()) { |
| 322 | patchesBackup = mPatches; |
| 323 | cleanUpPatch(existing->id); |
| 324 | } else { |
| 325 | LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id; |
| 326 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 327 | } |
| 328 | } |
| 329 | // Validate the requested patch. |
| 330 | for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) { |
| 331 | if (!nonExclusive && mPatches.count(sinkPortId) != 0) { |
| 332 | LOG(ERROR) << __func__ << ": sink port id " << sinkPortId |
| 333 | << "is exclusive and is already used by some other patch"; |
| 334 | if (patchesBackup.has_value()) { |
| 335 | mPatches = std::move(*patchesBackup); |
| 336 | } |
| 337 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 338 | } |
| 339 | } |
| 340 | *_aidl_return = in_requested; |
| 341 | if (existing == patches.end()) { |
| 342 | _aidl_return->id = getConfig().nextPatchId++; |
| 343 | patches.push_back(*_aidl_return); |
| 344 | existing = patches.begin() + (patches.size() - 1); |
| 345 | } else { |
| 346 | *existing = *_aidl_return; |
| 347 | } |
| 348 | registerPatch(*existing); |
| 349 | LOG(DEBUG) << __func__ << ": created or updated patch id " << _aidl_return->id; |
| 350 | return ndk::ScopedAStatus::ok(); |
| 351 | } |
| 352 | |
| 353 | ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested, |
| 354 | AudioPortConfig* out_suggested, bool* _aidl_return) { |
| 355 | LOG(DEBUG) << __func__ << ": requested " << in_requested.toString(); |
| 356 | auto& configs = getConfig().portConfigs; |
| 357 | auto existing = configs.end(); |
| 358 | if (in_requested.id != 0) { |
| 359 | if (existing = findById<AudioPortConfig>(configs, in_requested.id); |
| 360 | existing == configs.end()) { |
| 361 | LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id |
| 362 | << " not found"; |
| 363 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | const int portId = existing != configs.end() ? existing->portId : in_requested.portId; |
| 368 | if (portId == 0) { |
| 369 | LOG(ERROR) << __func__ << ": input port config does not specify portId"; |
| 370 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 371 | } |
| 372 | auto& ports = getConfig().ports; |
| 373 | auto portIt = findById<AudioPort>(ports, portId); |
| 374 | if (portIt == ports.end()) { |
| 375 | LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId; |
| 376 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 377 | } |
| 378 | if (existing != configs.end()) { |
| 379 | *out_suggested = *existing; |
| 380 | } else { |
| 381 | AudioPortConfig newConfig; |
| 382 | if (generateDefaultPortConfig(*portIt, &newConfig)) { |
| 383 | *out_suggested = newConfig; |
| 384 | } else { |
| 385 | LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId; |
| 386 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 387 | } |
| 388 | } |
| 389 | // From this moment, 'out_suggested' is either an existing port config, |
| 390 | // or a new generated config. Now attempt to update it according to the specified |
| 391 | // fields of 'in_requested'. |
| 392 | |
| 393 | bool requestedIsValid = true, requestedIsFullySpecified = true; |
| 394 | |
| 395 | AudioIoFlags portFlags = portIt->flags; |
| 396 | if (in_requested.flags.has_value()) { |
| 397 | if (in_requested.flags.value() != portFlags) { |
| 398 | LOG(WARNING) << __func__ << ": requested flags " |
| 399 | << in_requested.flags.value().toString() << " do not match port's " |
| 400 | << portId << " flags " << portFlags.toString(); |
| 401 | requestedIsValid = false; |
| 402 | } |
| 403 | } else { |
| 404 | requestedIsFullySpecified = false; |
| 405 | } |
| 406 | |
| 407 | AudioProfile portProfile; |
| 408 | if (in_requested.format.has_value()) { |
| 409 | const auto& format = in_requested.format.value(); |
| 410 | if (findAudioProfile(*portIt, format, &portProfile)) { |
| 411 | out_suggested->format = format; |
| 412 | } else { |
| 413 | LOG(WARNING) << __func__ << ": requested format " << format.toString() |
| 414 | << " is not found in port's " << portId << " profiles"; |
| 415 | requestedIsValid = false; |
| 416 | } |
| 417 | } else { |
| 418 | requestedIsFullySpecified = false; |
| 419 | } |
| 420 | if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) { |
| 421 | LOG(ERROR) << __func__ << ": port " << portId << " does not support format " |
| 422 | << out_suggested->format.value().toString() << " anymore"; |
| 423 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 424 | } |
| 425 | |
| 426 | if (in_requested.channelMask.has_value()) { |
| 427 | const auto& channelMask = in_requested.channelMask.value(); |
| 428 | if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) != |
| 429 | portProfile.channelMasks.end()) { |
| 430 | out_suggested->channelMask = channelMask; |
| 431 | } else { |
| 432 | LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString() |
| 433 | << " is not supported for the format " << portProfile.format.toString() |
| 434 | << " by the port " << portId; |
| 435 | requestedIsValid = false; |
| 436 | } |
| 437 | } else { |
| 438 | requestedIsFullySpecified = false; |
| 439 | } |
| 440 | |
| 441 | if (in_requested.sampleRate.has_value()) { |
| 442 | const auto& sampleRate = in_requested.sampleRate.value(); |
| 443 | if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(), |
| 444 | sampleRate.value) != portProfile.sampleRates.end()) { |
| 445 | out_suggested->sampleRate = sampleRate; |
| 446 | } else { |
| 447 | LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value |
| 448 | << " is not supported for the format " << portProfile.format.toString() |
| 449 | << " by the port " << portId; |
| 450 | requestedIsValid = false; |
| 451 | } |
| 452 | } else { |
| 453 | requestedIsFullySpecified = false; |
| 454 | } |
| 455 | |
| 456 | if (in_requested.gain.has_value()) { |
| 457 | // Let's pretend that gain can always be applied. |
| 458 | out_suggested->gain = in_requested.gain.value(); |
| 459 | } |
| 460 | |
| 461 | if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) { |
| 462 | out_suggested->id = getConfig().nextPortId++; |
| 463 | configs.push_back(*out_suggested); |
| 464 | *_aidl_return = true; |
| 465 | LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString(); |
| 466 | } else if (existing != configs.end() && requestedIsValid) { |
| 467 | *existing = *out_suggested; |
| 468 | *_aidl_return = true; |
| 469 | LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString(); |
| 470 | } else { |
| 471 | LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end()) |
| 472 | << "; requested is valid? " << requestedIsValid << ", fully specified? " |
| 473 | << requestedIsFullySpecified; |
| 474 | *_aidl_return = false; |
| 475 | } |
| 476 | return ndk::ScopedAStatus::ok(); |
| 477 | } |
| 478 | |
| 479 | ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) { |
| 480 | auto& patches = getConfig().patches; |
| 481 | auto patchIt = findById<AudioPatch>(patches, in_patchId); |
| 482 | if (patchIt != patches.end()) { |
| 483 | cleanUpPatch(patchIt->id); |
| 484 | patches.erase(patchIt); |
| 485 | LOG(DEBUG) << __func__ << ": erased patch " << in_patchId; |
| 486 | return ndk::ScopedAStatus::ok(); |
| 487 | } |
| 488 | LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found"; |
| 489 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 490 | } |
| 491 | |
| 492 | ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) { |
| 493 | auto& configs = getConfig().portConfigs; |
| 494 | auto configIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 495 | if (configIt != configs.end()) { |
| 496 | if (mStreams.count(in_portConfigId) != 0) { |
| 497 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 498 | << " has a stream opened on it"; |
| 499 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 500 | } |
| 501 | auto patchIt = mPatches.find(in_portConfigId); |
| 502 | if (patchIt != mPatches.end()) { |
| 503 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 504 | << " is used by the patch with id " << patchIt->second; |
| 505 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 506 | } |
| 507 | auto& initials = getConfig().initialConfigs; |
| 508 | auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId); |
| 509 | if (initialIt == initials.end()) { |
| 510 | configs.erase(configIt); |
| 511 | LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId; |
| 512 | } else if (*configIt != *initialIt) { |
| 513 | *configIt = *initialIt; |
| 514 | LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId; |
| 515 | } |
| 516 | return ndk::ScopedAStatus::ok(); |
| 517 | } |
| 518 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found"; |
| 519 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 520 | } |
| 521 | |
| 522 | } // namespace aidl::android::hardware::audio::core |