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 | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 21 | #include <Utils.h> |
| 22 | #include <aidl/android/media/audio/common/AudioInputFlags.h> |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 23 | #include <aidl/android/media/audio/common/AudioOutputFlags.h> |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | #include <android/binder_ibinder_platform.h> |
| 26 | #include <error/expected_utils.h> |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 27 | |
| 28 | #include "core-impl/Module.h" |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 29 | #include "core-impl/ModuleBluetooth.h" |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 30 | #include "core-impl/ModulePrimary.h" |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 31 | #include "core-impl/ModuleRemoteSubmix.h" |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 32 | #include "core-impl/ModuleStub.h" |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 33 | #include "core-impl/ModuleUsb.h" |
Vlad Popa | 943b7e2 | 2022-12-08 14:24:12 +0100 | [diff] [blame] | 34 | #include "core-impl/SoundDose.h" |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 35 | #include "core-impl/utils.h" |
| 36 | |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 37 | using aidl::android::hardware::audio::common::getFrameSizeInBytes; |
| 38 | using aidl::android::hardware::audio::common::isBitPositionFlagSet; |
| 39 | using aidl::android::hardware::audio::common::isValidAudioMode; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 40 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 41 | using aidl::android::hardware::audio::common::SourceMetadata; |
Vlad Popa | 2afbd1e | 2022-12-28 17:04:58 +0100 | [diff] [blame] | 42 | using aidl::android::hardware::audio::core::sounddose::ISoundDose; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 43 | using aidl::android::media::audio::common::AudioChannelLayout; |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 44 | using aidl::android::media::audio::common::AudioDevice; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 45 | using aidl::android::media::audio::common::AudioFormatDescription; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 46 | using aidl::android::media::audio::common::AudioFormatType; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 47 | using aidl::android::media::audio::common::AudioInputFlags; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 48 | using aidl::android::media::audio::common::AudioIoFlags; |
jiabin | 9a8e686 | 2023-01-12 23:06:37 +0000 | [diff] [blame] | 49 | using aidl::android::media::audio::common::AudioMMapPolicy; |
| 50 | using aidl::android::media::audio::common::AudioMMapPolicyInfo; |
| 51 | using aidl::android::media::audio::common::AudioMMapPolicyType; |
Mikhail Naganov | 04ae822 | 2023-01-11 15:48:10 -0800 | [diff] [blame] | 52 | using aidl::android::media::audio::common::AudioMode; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 53 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 54 | using aidl::android::media::audio::common::AudioOutputFlags; |
| 55 | using aidl::android::media::audio::common::AudioPort; |
| 56 | using aidl::android::media::audio::common::AudioPortConfig; |
| 57 | using aidl::android::media::audio::common::AudioPortExt; |
| 58 | using aidl::android::media::audio::common::AudioProfile; |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 59 | using aidl::android::media::audio::common::Boolean; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 60 | using aidl::android::media::audio::common::Int; |
Mikhail Naganov | 6725ef5 | 2023-02-09 17:52:50 -0800 | [diff] [blame] | 61 | using aidl::android::media::audio::common::MicrophoneInfo; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 62 | using aidl::android::media::audio::common::PcmType; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 63 | |
| 64 | namespace aidl::android::hardware::audio::core { |
| 65 | |
| 66 | namespace { |
| 67 | |
| 68 | bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) { |
| 69 | *config = {}; |
| 70 | config->portId = port.id; |
| 71 | if (port.profiles.empty()) { |
| 72 | LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles"; |
| 73 | return false; |
| 74 | } |
| 75 | const auto& profile = port.profiles.begin(); |
| 76 | config->format = profile->format; |
| 77 | if (profile->channelMasks.empty()) { |
| 78 | LOG(ERROR) << __func__ << ": the first profile in port " << port.id |
| 79 | << " has no channel masks"; |
| 80 | return false; |
| 81 | } |
| 82 | config->channelMask = *profile->channelMasks.begin(); |
| 83 | if (profile->sampleRates.empty()) { |
| 84 | LOG(ERROR) << __func__ << ": the first profile in port " << port.id |
| 85 | << " has no sample rates"; |
| 86 | return false; |
| 87 | } |
| 88 | Int sampleRate; |
| 89 | sampleRate.value = *profile->sampleRates.begin(); |
| 90 | config->sampleRate = sampleRate; |
| 91 | config->flags = port.flags; |
| 92 | config->ext = port.ext; |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format, |
| 97 | AudioProfile* profile) { |
| 98 | if (auto profilesIt = |
| 99 | find_if(port.profiles.begin(), port.profiles.end(), |
| 100 | [&format](const auto& profile) { return profile.format == format; }); |
| 101 | profilesIt != port.profiles.end()) { |
| 102 | *profile = *profilesIt; |
| 103 | return true; |
| 104 | } |
| 105 | return false; |
| 106 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 107 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 108 | } // namespace |
| 109 | |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 110 | // static |
| 111 | std::shared_ptr<Module> Module::createInstance(Type type) { |
| 112 | switch (type) { |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 113 | case Type::DEFAULT: |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 114 | return ndk::SharedRefBase::make<ModulePrimary>(); |
| 115 | case Type::R_SUBMIX: |
| 116 | return ndk::SharedRefBase::make<ModuleRemoteSubmix>(); |
| 117 | case Type::STUB: |
| 118 | return ndk::SharedRefBase::make<ModuleStub>(); |
| 119 | case Type::USB: |
| 120 | return ndk::SharedRefBase::make<ModuleUsb>(); |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 121 | case Type::BLUETOOTH: |
| 122 | return ndk::SharedRefBase::make<ModuleBluetooth>(); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Mikhail Naganov | d5536d9 | 2023-03-24 18:27:58 -0700 | [diff] [blame] | 126 | std::ostream& operator<<(std::ostream& os, Module::Type t) { |
| 127 | switch (t) { |
| 128 | case Module::Type::DEFAULT: |
| 129 | os << "default"; |
| 130 | break; |
| 131 | case Module::Type::R_SUBMIX: |
| 132 | os << "r_submix"; |
| 133 | break; |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 134 | case Module::Type::STUB: |
| 135 | os << "stub"; |
| 136 | break; |
Mikhail Naganov | d5536d9 | 2023-03-24 18:27:58 -0700 | [diff] [blame] | 137 | case Module::Type::USB: |
| 138 | os << "usb"; |
| 139 | break; |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 140 | case Module::Type::BLUETOOTH: |
| 141 | os << "bluetooth"; |
| 142 | break; |
Mikhail Naganov | d5536d9 | 2023-03-24 18:27:58 -0700 | [diff] [blame] | 143 | } |
| 144 | return os; |
| 145 | } |
| 146 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 147 | void Module::cleanUpPatch(int32_t patchId) { |
| 148 | erase_all_values(mPatches, std::set<int32_t>{patchId}); |
| 149 | } |
| 150 | |
Mikhail Naganov | 8651b36 | 2023-01-06 23:15:27 +0000 | [diff] [blame] | 151 | ndk::ScopedAStatus Module::createStreamContext( |
| 152 | int32_t in_portConfigId, int64_t in_bufferSizeFrames, |
| 153 | std::shared_ptr<IStreamCallback> asyncCallback, |
| 154 | std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) { |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 155 | if (in_bufferSizeFrames <= 0) { |
| 156 | LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames; |
| 157 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 158 | } |
| 159 | if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) { |
| 160 | LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames |
| 161 | << ", must be at least " << kMinimumStreamBufferSizeFrames; |
| 162 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 163 | } |
| 164 | auto& configs = getConfig().portConfigs; |
| 165 | auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 166 | // Since this is a private method, it is assumed that |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 167 | // validity of the portConfigId has already been checked. |
| 168 | const size_t frameSize = |
| 169 | getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value()); |
| 170 | if (frameSize == 0) { |
| 171 | LOG(ERROR) << __func__ << ": could not calculate frame size for port config " |
| 172 | << portConfigIt->toString(); |
| 173 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 174 | } |
| 175 | LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes"; |
Mikhail Naganov | b511b8a | 2023-05-15 14:35:24 -0700 | [diff] [blame] | 176 | if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) { |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 177 | LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames |
| 178 | << " frames is too large, maximum size is " |
| 179 | << kMaximumStreamBufferSizeBytes / frameSize; |
| 180 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 181 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 182 | const auto& flags = portConfigIt->flags.value(); |
| 183 | if ((flags.getTag() == AudioIoFlags::Tag::input && |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 184 | !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(), |
| 185 | AudioInputFlags::MMAP_NOIRQ)) || |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 186 | (flags.getTag() == AudioIoFlags::Tag::output && |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 187 | !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(), |
| 188 | AudioOutputFlags::MMAP_NOIRQ))) { |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 189 | StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs, |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 190 | mVendorDebug.forceTransientBurst, |
| 191 | mVendorDebug.forceSynchronousDrain}; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 192 | StreamContext temp( |
| 193 | std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/), |
| 194 | std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/), |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 195 | portConfigIt->portId, portConfigIt->format.value(), |
| 196 | portConfigIt->channelMask.value(), portConfigIt->sampleRate.value().value, flags, |
Mikhail Naganov | b42a69e | 2023-06-16 12:38:25 -0700 | [diff] [blame] | 197 | portConfigIt->ext.get<AudioPortExt::mix>().handle, |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 198 | std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames), |
Mikhail Naganov | 8651b36 | 2023-01-06 23:15:27 +0000 | [diff] [blame] | 199 | asyncCallback, outEventCallback, params); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 200 | if (temp.isValid()) { |
| 201 | *out_context = std::move(temp); |
| 202 | } else { |
| 203 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 204 | } |
| 205 | } else { |
| 206 | // TODO: Implement simulation of MMAP buffer allocation |
| 207 | } |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 208 | return ndk::ScopedAStatus::ok(); |
| 209 | } |
| 210 | |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 211 | std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) { |
| 212 | std::vector<AudioDevice> result; |
| 213 | auto& ports = getConfig().ports; |
| 214 | auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId)); |
| 215 | for (auto it = portIds.begin(); it != portIds.end(); ++it) { |
| 216 | auto portIt = findById<AudioPort>(ports, *it); |
| 217 | if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) { |
| 218 | result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device); |
| 219 | } |
| 220 | } |
| 221 | return result; |
| 222 | } |
| 223 | |
| 224 | std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) { |
| 225 | std::set<int32_t> result; |
| 226 | auto patchIdsRange = mPatches.equal_range(portConfigId); |
| 227 | auto& patches = getConfig().patches; |
| 228 | for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) { |
| 229 | auto patchIt = findById<AudioPatch>(patches, it->second); |
| 230 | if (patchIt == patches.end()) { |
| 231 | LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches " |
| 232 | << "not found in the configuration"; |
| 233 | } |
| 234 | if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(), |
| 235 | portConfigId) != patchIt->sourcePortConfigIds.end()) { |
| 236 | result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end()); |
| 237 | } else { |
| 238 | result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end()); |
| 239 | } |
| 240 | } |
| 241 | return result; |
| 242 | } |
| 243 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 244 | ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) { |
| 245 | auto& configs = getConfig().portConfigs; |
| 246 | auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 247 | if (portConfigIt == configs.end()) { |
| 248 | LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found"; |
| 249 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 250 | } |
| 251 | const int32_t portId = portConfigIt->portId; |
| 252 | // In our implementation, configs of mix ports always have unique IDs. |
| 253 | CHECK(portId != in_portConfigId); |
| 254 | auto& ports = getConfig().ports; |
| 255 | auto portIt = findById<AudioPort>(ports, portId); |
| 256 | if (portIt == ports.end()) { |
| 257 | LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id " |
| 258 | << in_portConfigId << " not found"; |
| 259 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 260 | } |
| 261 | if (mStreams.count(in_portConfigId) != 0) { |
| 262 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 263 | << " already has a stream opened on it"; |
| 264 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 265 | } |
| 266 | if (portIt->ext.getTag() != AudioPortExt::Tag::mix) { |
| 267 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 268 | << " does not correspond to a mix port"; |
| 269 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 270 | } |
Mikhail Naganov | b511b8a | 2023-05-15 14:35:24 -0700 | [diff] [blame] | 271 | const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 272 | if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) { |
| 273 | LOG(ERROR) << __func__ << ": port id " << portId |
| 274 | << " has already reached maximum allowed opened stream count: " |
| 275 | << maxOpenStreamCount; |
| 276 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 277 | } |
| 278 | *port = &(*portIt); |
| 279 | return ndk::ScopedAStatus::ok(); |
| 280 | } |
| 281 | |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 282 | template <typename C> |
| 283 | std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) { |
| 284 | std::set<int32_t> result; |
| 285 | auto& portConfigs = getConfig().portConfigs; |
| 286 | for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) { |
| 287 | auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it); |
| 288 | if (portConfigIt != portConfigs.end()) { |
| 289 | result.insert(portConfigIt->portId); |
| 290 | } |
| 291 | } |
| 292 | return result; |
| 293 | } |
| 294 | |
Peter Yoon | 918a6a5 | 2023-07-13 17:04:37 +0900 | [diff] [blame] | 295 | std::unique_ptr<internal::Configuration> Module::initializeConfig() { |
| 296 | std::unique_ptr<internal::Configuration> config; |
| 297 | switch (getType()) { |
| 298 | case Type::DEFAULT: |
| 299 | config = std::move(internal::getPrimaryConfiguration()); |
| 300 | break; |
| 301 | case Type::R_SUBMIX: |
| 302 | config = std::move(internal::getRSubmixConfiguration()); |
| 303 | break; |
| 304 | case Type::STUB: |
| 305 | config = std::move(internal::getStubConfiguration()); |
| 306 | break; |
| 307 | case Type::USB: |
| 308 | config = std::move(internal::getUsbConfiguration()); |
| 309 | break; |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 310 | case Type::BLUETOOTH: |
| 311 | config = std::move(internal::getBluetoothConfiguration()); |
| 312 | break; |
Peter Yoon | 918a6a5 | 2023-07-13 17:04:37 +0900 | [diff] [blame] | 313 | } |
| 314 | return config; |
| 315 | } |
| 316 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 317 | internal::Configuration& Module::getConfig() { |
| 318 | if (!mConfig) { |
Peter Yoon | 918a6a5 | 2023-07-13 17:04:37 +0900 | [diff] [blame] | 319 | mConfig = std::move(initializeConfig()); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 320 | } |
| 321 | return *mConfig; |
| 322 | } |
| 323 | |
| 324 | void Module::registerPatch(const AudioPatch& patch) { |
| 325 | auto& configs = getConfig().portConfigs; |
| 326 | auto do_insert = [&](const std::vector<int32_t>& portConfigIds) { |
| 327 | for (auto portConfigId : portConfigIds) { |
| 328 | auto configIt = findById<AudioPortConfig>(configs, portConfigId); |
| 329 | if (configIt != configs.end()) { |
| 330 | mPatches.insert(std::pair{portConfigId, patch.id}); |
| 331 | if (configIt->portId != portConfigId) { |
| 332 | mPatches.insert(std::pair{configIt->portId, patch.id}); |
| 333 | } |
| 334 | } |
| 335 | }; |
| 336 | }; |
| 337 | do_insert(patch.sourcePortConfigIds); |
| 338 | do_insert(patch.sinkPortConfigIds); |
| 339 | } |
| 340 | |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 341 | ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch, |
| 342 | const AudioPatch& newPatch) { |
Mikhail Naganov | 89a8ea9 | 2023-09-29 17:02:12 -0700 | [diff] [blame] | 343 | // Notify streams about the new set of devices they are connected to. |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 344 | auto maybeFailure = ndk::ScopedAStatus::ok(); |
Mikhail Naganov | 89a8ea9 | 2023-09-29 17:02:12 -0700 | [diff] [blame] | 345 | using Connections = |
| 346 | std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>; |
| 347 | Connections oldConnections, newConnections; |
| 348 | auto fillConnectionsHelper = [&](Connections& connections, |
| 349 | const std::vector<int32_t>& mixPortCfgIds, |
| 350 | const std::vector<int32_t>& devicePortCfgIds) { |
| 351 | for (int32_t mixPortCfgId : mixPortCfgIds) { |
| 352 | connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end()); |
| 353 | } |
| 354 | }; |
| 355 | auto fillConnections = [&](Connections& connections, const AudioPatch& patch) { |
| 356 | if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(), |
| 357 | [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) != |
| 358 | patch.sourcePortConfigIds.end()) { |
| 359 | // Sources are mix ports. |
| 360 | fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds); |
| 361 | } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(), |
| 362 | [&](int32_t portConfigId) { |
| 363 | return mStreams.count(portConfigId) > 0; |
| 364 | }) != patch.sinkPortConfigIds.end()) { |
| 365 | // Sources are device ports. |
| 366 | fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds); |
| 367 | } // Otherwise, there are no streams to notify. |
| 368 | }; |
| 369 | fillConnections(oldConnections, oldPatch); |
| 370 | fillConnections(newConnections, newPatch); |
| 371 | |
| 372 | std::for_each(oldConnections.begin(), oldConnections.end(), [&](const auto& connectionPair) { |
| 373 | const int32_t mixPortConfigId = connectionPair.first; |
| 374 | if (auto it = newConnections.find(mixPortConfigId); |
| 375 | it == newConnections.end() || it->second != connectionPair.second) { |
| 376 | if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, {}); |
| 377 | status.isOk()) { |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 378 | LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id " |
Mikhail Naganov | 89a8ea9 | 2023-09-29 17:02:12 -0700 | [diff] [blame] | 379 | << mixPortConfigId << " has been disconnected"; |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 380 | } else { |
| 381 | // Disconnection is tricky to roll back, just register a failure. |
| 382 | maybeFailure = std::move(status); |
| 383 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 384 | } |
| 385 | }); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 386 | if (!maybeFailure.isOk()) return maybeFailure; |
Mikhail Naganov | 89a8ea9 | 2023-09-29 17:02:12 -0700 | [diff] [blame] | 387 | std::set<int32_t> idsToDisconnectOnFailure; |
| 388 | std::for_each(newConnections.begin(), newConnections.end(), [&](const auto& connectionPair) { |
| 389 | const int32_t mixPortConfigId = connectionPair.first; |
| 390 | if (auto it = oldConnections.find(mixPortConfigId); |
| 391 | it == oldConnections.end() || it->second != connectionPair.second) { |
| 392 | const auto connectedDevices = findConnectedDevices(mixPortConfigId); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 393 | if (connectedDevices.empty()) { |
| 394 | // This is important as workers use the vector size to derive the connection status. |
| 395 | LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port " |
| 396 | "config id " |
Mikhail Naganov | 89a8ea9 | 2023-09-29 17:02:12 -0700 | [diff] [blame] | 397 | << mixPortConfigId; |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 398 | } |
Mikhail Naganov | 89a8ea9 | 2023-09-29 17:02:12 -0700 | [diff] [blame] | 399 | if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, connectedDevices); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 400 | status.isOk()) { |
| 401 | LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id " |
Mikhail Naganov | 89a8ea9 | 2023-09-29 17:02:12 -0700 | [diff] [blame] | 402 | << mixPortConfigId << " has been connected to: " |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 403 | << ::android::internal::ToString(connectedDevices); |
| 404 | } else { |
| 405 | maybeFailure = std::move(status); |
Mikhail Naganov | 89a8ea9 | 2023-09-29 17:02:12 -0700 | [diff] [blame] | 406 | idsToDisconnectOnFailure.insert(mixPortConfigId); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 407 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 408 | } |
| 409 | }); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 410 | if (!maybeFailure.isOk()) { |
| 411 | LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids " |
| 412 | << ::android::internal::ToString(idsToDisconnectOnFailure); |
| 413 | std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(), |
| 414 | [&](const auto& portConfigId) { |
| 415 | auto status = mStreams.setStreamConnectedDevices(portConfigId, {}); |
| 416 | (void)status.isOk(); // Can't do much about a failure here. |
| 417 | }); |
| 418 | return maybeFailure; |
| 419 | } |
| 420 | return ndk::ScopedAStatus::ok(); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 423 | ndk::ScopedAStatus Module::setModuleDebug( |
| 424 | const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) { |
Mikhail Naganov | d5536d9 | 2023-03-24 18:27:58 -0700 | [diff] [blame] | 425 | LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString() |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 426 | << ", new flags: " << in_debug.toString(); |
| 427 | if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections && |
| 428 | !mConnectedDevicePorts.empty()) { |
Mikhail Naganov | d5536d9 | 2023-03-24 18:27:58 -0700 | [diff] [blame] | 429 | LOG(ERROR) << __func__ << ": " << mType |
| 430 | << ": attempting to change device connections simulation while having external " |
| 431 | << "devices connected"; |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 432 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 433 | } |
Mikhail Naganov | bd483c0 | 2022-11-17 20:33:39 +0000 | [diff] [blame] | 434 | if (in_debug.streamTransientStateDelayMs < 0) { |
Mikhail Naganov | d5536d9 | 2023-03-24 18:27:58 -0700 | [diff] [blame] | 435 | LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: " |
Mikhail Naganov | bd483c0 | 2022-11-17 20:33:39 +0000 | [diff] [blame] | 436 | << in_debug.streamTransientStateDelayMs; |
| 437 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 438 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 439 | mDebug = in_debug; |
| 440 | return ndk::ScopedAStatus::ok(); |
| 441 | } |
| 442 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 443 | ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) { |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 444 | *_aidl_return = nullptr; |
| 445 | LOG(DEBUG) << __func__ << ": returning null"; |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 446 | return ndk::ScopedAStatus::ok(); |
| 447 | } |
| 448 | |
Mikhail Naganov | 10c6fe2 | 2022-09-30 23:49:17 +0000 | [diff] [blame] | 449 | ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) { |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 450 | *_aidl_return = nullptr; |
| 451 | LOG(DEBUG) << __func__ << ": returning null"; |
Mikhail Naganov | 10c6fe2 | 2022-09-30 23:49:17 +0000 | [diff] [blame] | 452 | return ndk::ScopedAStatus::ok(); |
| 453 | } |
| 454 | |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 455 | ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) { |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 456 | *_aidl_return = nullptr; |
| 457 | LOG(DEBUG) << __func__ << ": returning null"; |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 458 | return ndk::ScopedAStatus::ok(); |
| 459 | } |
| 460 | |
Mikhail Naganov | b5647da | 2023-03-06 14:37:38 -0800 | [diff] [blame] | 461 | ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) { |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 462 | *_aidl_return = nullptr; |
| 463 | LOG(DEBUG) << __func__ << ": returning null"; |
Mikhail Naganov | b5647da | 2023-03-06 14:37:38 -0800 | [diff] [blame] | 464 | return ndk::ScopedAStatus::ok(); |
| 465 | } |
| 466 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 467 | ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData, |
| 468 | AudioPort* _aidl_return) { |
| 469 | const int32_t templateId = in_templateIdAndAdditionalData.id; |
| 470 | auto& ports = getConfig().ports; |
| 471 | AudioPort connectedPort; |
| 472 | { // Scope the template port so that we don't accidentally modify it. |
| 473 | auto templateIt = findById<AudioPort>(ports, templateId); |
| 474 | if (templateIt == ports.end()) { |
| 475 | LOG(ERROR) << __func__ << ": port id " << templateId << " not found"; |
| 476 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 477 | } |
| 478 | if (templateIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 479 | LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port"; |
| 480 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 481 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 482 | auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>(); |
| 483 | if (templateDevicePort.device.type.connection.empty()) { |
| 484 | LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached"; |
| 485 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 486 | } |
Mikhail Naganov | fcf980e | 2023-09-07 16:30:11 -0700 | [diff] [blame] | 487 | if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) { |
| 488 | LOG(ERROR) << __func__ << ": port id " << templateId << " is a connected device port"; |
| 489 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 490 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 491 | // Postpone id allocation until we ensure that there are no client errors. |
| 492 | connectedPort = *templateIt; |
| 493 | connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors; |
| 494 | const auto& inputDevicePort = |
| 495 | in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>(); |
| 496 | auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>(); |
| 497 | connectedDevicePort.device.address = inputDevicePort.device.address; |
| 498 | LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to " |
| 499 | << connectedDevicePort.device.toString(); |
| 500 | // Check if there is already a connected port with for the same external device. |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 501 | for (auto connectedPortPair : mConnectedDevicePorts) { |
| 502 | auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 503 | if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device == |
| 504 | connectedDevicePort.device) { |
| 505 | LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString() |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 506 | << " is already connected at the device port id " |
| 507 | << connectedPortPair.first; |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 508 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 513 | if (connectedPort.profiles.empty()) { |
Mikhail Naganov | fcf980e | 2023-09-07 16:30:11 -0700 | [diff] [blame] | 514 | if (!mDebug.simulateDeviceConnections) { |
| 515 | RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort)); |
| 516 | } else { |
| 517 | auto& connectedProfiles = getConfig().connectedProfiles; |
| 518 | if (auto connectedProfilesIt = connectedProfiles.find(templateId); |
| 519 | connectedProfilesIt != connectedProfiles.end()) { |
| 520 | connectedPort.profiles = connectedProfilesIt->second; |
| 521 | } |
| 522 | } |
| 523 | if (connectedPort.profiles.empty()) { |
| 524 | LOG(ERROR) << __func__ |
| 525 | << ": profiles of a connected port still empty after connecting external " |
| 526 | "device " |
| 527 | << connectedPort.toString(); |
| 528 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 529 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 532 | for (auto profile : connectedPort.profiles) { |
| 533 | if (profile.channelMasks.empty()) { |
| 534 | LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no channel masks"; |
| 535 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 536 | } |
| 537 | if (profile.sampleRates.empty()) { |
| 538 | LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no sample rates"; |
| 539 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 540 | } |
| 541 | } |
| 542 | |
Mikhail Naganov | dc41773 | 2023-09-29 15:49:35 -0700 | [diff] [blame] | 543 | connectedPort.id = getConfig().nextPortId++; |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 544 | auto [connectedPortsIt, _] = |
Mikhail Naganov | 0e128dd | 2023-09-13 18:01:18 -0700 | [diff] [blame] | 545 | mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>())); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 546 | LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, " |
| 547 | << "connected port ID " << connectedPort.id; |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 548 | ports.push_back(connectedPort); |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 549 | onExternalDeviceConnectionChanged(connectedPort, true /*connected*/); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 550 | |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 551 | std::vector<int32_t> routablePortIds; |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 552 | std::vector<AudioRoute> newRoutes; |
| 553 | auto& routes = getConfig().routes; |
| 554 | for (auto& r : routes) { |
| 555 | if (r.sinkPortId == templateId) { |
| 556 | AudioRoute newRoute; |
| 557 | newRoute.sourcePortIds = r.sourcePortIds; |
| 558 | newRoute.sinkPortId = connectedPort.id; |
| 559 | newRoute.isExclusive = r.isExclusive; |
| 560 | newRoutes.push_back(std::move(newRoute)); |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 561 | routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(), |
| 562 | r.sourcePortIds.end()); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 563 | } else { |
| 564 | auto& srcs = r.sourcePortIds; |
| 565 | if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) { |
| 566 | srcs.push_back(connectedPort.id); |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 567 | routablePortIds.push_back(r.sinkPortId); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | } |
| 571 | routes.insert(routes.end(), newRoutes.begin(), newRoutes.end()); |
| 572 | |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 573 | // Note: this is a simplistic approach assuming that a mix port can only be populated |
| 574 | // from a single device port. Implementing support for stuffing dynamic profiles with a superset |
| 575 | // of all profiles from all routable dynamic device ports would be more involved. |
| 576 | for (const auto mixPortId : routablePortIds) { |
| 577 | auto portsIt = findById<AudioPort>(ports, mixPortId); |
Mikhail Naganov | 0e128dd | 2023-09-13 18:01:18 -0700 | [diff] [blame] | 578 | if (portsIt != ports.end()) { |
| 579 | if (portsIt->profiles.empty()) { |
| 580 | portsIt->profiles = connectedPort.profiles; |
| 581 | connectedPortsIt->second.insert(portsIt->id); |
| 582 | } else { |
| 583 | // Check if profiles are non empty because they were populated by |
| 584 | // a previous connection. Otherwise, it means that they are not empty because |
| 585 | // the mix port has static profiles. |
| 586 | for (const auto cp : mConnectedDevicePorts) { |
| 587 | if (cp.second.count(portsIt->id) > 0) { |
| 588 | connectedPortsIt->second.insert(portsIt->id); |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | } |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | *_aidl_return = std::move(connectedPort); |
| 596 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 597 | return ndk::ScopedAStatus::ok(); |
| 598 | } |
| 599 | |
| 600 | ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) { |
| 601 | auto& ports = getConfig().ports; |
| 602 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 603 | if (portIt == ports.end()) { |
| 604 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 605 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 606 | } |
| 607 | if (portIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 608 | LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port"; |
| 609 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 610 | } |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 611 | auto connectedPortsIt = mConnectedDevicePorts.find(in_portId); |
| 612 | if (connectedPortsIt == mConnectedDevicePorts.end()) { |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 613 | LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port"; |
| 614 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 615 | } |
| 616 | auto& configs = getConfig().portConfigs; |
| 617 | auto& initials = getConfig().initialConfigs; |
| 618 | auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) { |
| 619 | if (config.portId == in_portId) { |
| 620 | // Check if the configuration was provided by the client. |
| 621 | const auto& initialIt = findById<AudioPortConfig>(initials, config.id); |
| 622 | return initialIt == initials.end() || config != *initialIt; |
| 623 | } |
| 624 | return false; |
| 625 | }); |
| 626 | if (configIt != configs.end()) { |
| 627 | LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id " |
| 628 | << configIt->id; |
| 629 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 630 | } |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 631 | onExternalDeviceConnectionChanged(*portIt, false /*connected*/); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 632 | ports.erase(portIt); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 633 | LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released"; |
| 634 | |
| 635 | auto& routes = getConfig().routes; |
| 636 | for (auto routesIt = routes.begin(); routesIt != routes.end();) { |
| 637 | if (routesIt->sinkPortId == in_portId) { |
| 638 | routesIt = routes.erase(routesIt); |
| 639 | } else { |
| 640 | // Note: the list of sourcePortIds can't become empty because there must |
| 641 | // be the id of the template port in the route. |
| 642 | erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; }); |
| 643 | ++routesIt; |
| 644 | } |
| 645 | } |
| 646 | |
Mikhail Naganov | 0e128dd | 2023-09-13 18:01:18 -0700 | [diff] [blame] | 647 | // Clear profiles for mix ports that are not connected to any other ports. |
| 648 | std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second); |
| 649 | mConnectedDevicePorts.erase(connectedPortsIt); |
| 650 | for (const auto& connectedPort : mConnectedDevicePorts) { |
| 651 | for (int32_t mixPortId : connectedPort.second) { |
| 652 | mixPortsToClear.erase(mixPortId); |
| 653 | } |
| 654 | } |
| 655 | for (int32_t mixPortId : mixPortsToClear) { |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 656 | auto mixPortIt = findById<AudioPort>(ports, mixPortId); |
| 657 | if (mixPortIt != ports.end()) { |
| 658 | mixPortIt->profiles = {}; |
| 659 | } |
| 660 | } |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 661 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 662 | return ndk::ScopedAStatus::ok(); |
| 663 | } |
| 664 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 665 | ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) { |
| 666 | *_aidl_return = getConfig().patches; |
| 667 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches"; |
| 668 | return ndk::ScopedAStatus::ok(); |
| 669 | } |
| 670 | |
| 671 | ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) { |
| 672 | auto& ports = getConfig().ports; |
| 673 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 674 | if (portIt != ports.end()) { |
| 675 | *_aidl_return = *portIt; |
| 676 | LOG(DEBUG) << __func__ << ": returning port by id " << in_portId; |
| 677 | return ndk::ScopedAStatus::ok(); |
| 678 | } |
| 679 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 680 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 681 | } |
| 682 | |
| 683 | ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) { |
| 684 | *_aidl_return = getConfig().portConfigs; |
| 685 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs"; |
| 686 | return ndk::ScopedAStatus::ok(); |
| 687 | } |
| 688 | |
| 689 | ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) { |
| 690 | *_aidl_return = getConfig().ports; |
| 691 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports"; |
| 692 | return ndk::ScopedAStatus::ok(); |
| 693 | } |
| 694 | |
| 695 | ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) { |
| 696 | *_aidl_return = getConfig().routes; |
| 697 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes"; |
| 698 | return ndk::ScopedAStatus::ok(); |
| 699 | } |
| 700 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 701 | ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId, |
| 702 | std::vector<AudioRoute>* _aidl_return) { |
| 703 | auto& ports = getConfig().ports; |
| 704 | if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) { |
| 705 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 706 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 707 | } |
| 708 | auto& routes = getConfig().routes; |
| 709 | std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return), |
| 710 | [&](const auto& r) { |
| 711 | const auto& srcs = r.sourcePortIds; |
| 712 | return r.sinkPortId == in_portId || |
| 713 | std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end(); |
| 714 | }); |
| 715 | return ndk::ScopedAStatus::ok(); |
| 716 | } |
| 717 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 718 | ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args, |
| 719 | OpenInputStreamReturn* _aidl_return) { |
| 720 | LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size " |
| 721 | << in_args.bufferSizeFrames << " frames"; |
| 722 | AudioPort* port = nullptr; |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 723 | RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 724 | if (port->flags.getTag() != AudioIoFlags::Tag::input) { |
| 725 | LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 726 | << " does not correspond to an input mix port"; |
| 727 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 728 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 729 | StreamContext context; |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 730 | RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, |
| 731 | nullptr, nullptr, &context)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 732 | context.fillDescriptor(&_aidl_return->desc); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 733 | std::shared_ptr<StreamIn> stream; |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 734 | RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata, |
Mikhail Naganov | 9d16a6a | 2023-06-26 17:21:04 -0700 | [diff] [blame] | 735 | mConfig->microphones, &stream)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 736 | StreamWrapper streamWrapper(stream); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 737 | if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) { |
| 738 | RETURN_STATUS_IF_ERROR( |
| 739 | streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId))); |
| 740 | } |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 741 | AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL, |
| 742 | ANDROID_PRIORITY_AUDIO); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 743 | mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 744 | _aidl_return->stream = std::move(stream); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 745 | return ndk::ScopedAStatus::ok(); |
| 746 | } |
| 747 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 748 | ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args, |
| 749 | OpenOutputStreamReturn* _aidl_return) { |
| 750 | LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? " |
| 751 | << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames |
| 752 | << " frames"; |
| 753 | AudioPort* port = nullptr; |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 754 | RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 755 | if (port->flags.getTag() != AudioIoFlags::Tag::output) { |
| 756 | LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 757 | << " does not correspond to an output mix port"; |
| 758 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 759 | } |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 760 | const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(), |
| 761 | AudioOutputFlags::COMPRESS_OFFLOAD); |
| 762 | if (isOffload && !in_args.offloadInfo.has_value()) { |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 763 | LOG(ERROR) << __func__ << ": port id " << port->id |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 764 | << " has COMPRESS_OFFLOAD flag set, requires offload info"; |
| 765 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 766 | } |
Mikhail Naganov | 30301a4 | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 767 | const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(), |
| 768 | AudioOutputFlags::NON_BLOCKING); |
| 769 | if (isNonBlocking && in_args.callback == nullptr) { |
| 770 | LOG(ERROR) << __func__ << ": port id " << port->id |
| 771 | << " has NON_BLOCKING flag set, requires async callback"; |
| 772 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 773 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 774 | StreamContext context; |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 775 | RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, |
| 776 | isNonBlocking ? in_args.callback : nullptr, |
| 777 | in_args.eventCallback, &context)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 778 | context.fillDescriptor(&_aidl_return->desc); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 779 | std::shared_ptr<StreamOut> stream; |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 780 | RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata, |
Mikhail Naganov | 9d16a6a | 2023-06-26 17:21:04 -0700 | [diff] [blame] | 781 | in_args.offloadInfo, &stream)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 782 | StreamWrapper streamWrapper(stream); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 783 | if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) { |
| 784 | RETURN_STATUS_IF_ERROR( |
| 785 | streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId))); |
| 786 | } |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 787 | AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL, |
| 788 | ANDROID_PRIORITY_AUDIO); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 789 | mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 790 | _aidl_return->stream = std::move(stream); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 791 | return ndk::ScopedAStatus::ok(); |
| 792 | } |
| 793 | |
Mikhail Naganov | 7492720 | 2022-12-19 16:37:14 +0000 | [diff] [blame] | 794 | ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors( |
| 795 | SupportedPlaybackRateFactors* _aidl_return) { |
| 796 | LOG(DEBUG) << __func__; |
| 797 | (void)_aidl_return; |
| 798 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 799 | } |
| 800 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 801 | ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) { |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 802 | LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString(); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 803 | if (in_requested.sourcePortConfigIds.empty()) { |
| 804 | LOG(ERROR) << __func__ << ": requested patch has empty sources list"; |
| 805 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 806 | } |
| 807 | if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) { |
| 808 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list"; |
| 809 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 810 | } |
| 811 | if (in_requested.sinkPortConfigIds.empty()) { |
| 812 | LOG(ERROR) << __func__ << ": requested patch has empty sinks list"; |
| 813 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 814 | } |
| 815 | if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) { |
| 816 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list"; |
| 817 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 818 | } |
| 819 | |
| 820 | auto& configs = getConfig().portConfigs; |
| 821 | std::vector<int32_t> missingIds; |
| 822 | auto sources = |
| 823 | selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds); |
| 824 | if (!missingIds.empty()) { |
| 825 | LOG(ERROR) << __func__ << ": following source port config ids not found: " |
| 826 | << ::android::internal::ToString(missingIds); |
| 827 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 828 | } |
| 829 | auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds); |
| 830 | if (!missingIds.empty()) { |
| 831 | LOG(ERROR) << __func__ << ": following sink port config ids not found: " |
| 832 | << ::android::internal::ToString(missingIds); |
| 833 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 834 | } |
| 835 | // bool indicates whether a non-exclusive route is available. |
| 836 | // If only an exclusive route is available, that means the patch can not be |
| 837 | // established if there is any other patch which currently uses the sink port. |
| 838 | std::map<int32_t, bool> allowedSinkPorts; |
| 839 | auto& routes = getConfig().routes; |
| 840 | for (auto src : sources) { |
| 841 | for (const auto& r : routes) { |
| 842 | const auto& srcs = r.sourcePortIds; |
| 843 | if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) { |
| 844 | if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive |
| 845 | allowedSinkPorts[r.sinkPortId] = !r.isExclusive; |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | for (auto sink : sinks) { |
| 851 | if (allowedSinkPorts.count(sink->portId) == 0) { |
| 852 | LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId; |
| 853 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 854 | } |
| 855 | } |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 856 | RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks)); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 857 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 858 | auto& patches = getConfig().patches; |
| 859 | auto existing = patches.end(); |
| 860 | std::optional<decltype(mPatches)> patchesBackup; |
| 861 | if (in_requested.id != 0) { |
| 862 | existing = findById<AudioPatch>(patches, in_requested.id); |
| 863 | if (existing != patches.end()) { |
| 864 | patchesBackup = mPatches; |
| 865 | cleanUpPatch(existing->id); |
| 866 | } else { |
| 867 | LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id; |
| 868 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 869 | } |
| 870 | } |
| 871 | // Validate the requested patch. |
| 872 | for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) { |
| 873 | if (!nonExclusive && mPatches.count(sinkPortId) != 0) { |
| 874 | LOG(ERROR) << __func__ << ": sink port id " << sinkPortId |
| 875 | << "is exclusive and is already used by some other patch"; |
| 876 | if (patchesBackup.has_value()) { |
| 877 | mPatches = std::move(*patchesBackup); |
| 878 | } |
| 879 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 880 | } |
| 881 | } |
| 882 | *_aidl_return = in_requested; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 883 | _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames; |
| 884 | _aidl_return->latenciesMs.clear(); |
| 885 | _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(), |
| 886 | _aidl_return->sinkPortConfigIds.size(), kLatencyMs); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 887 | AudioPatch oldPatch{}; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 888 | if (existing == patches.end()) { |
| 889 | _aidl_return->id = getConfig().nextPatchId++; |
| 890 | patches.push_back(*_aidl_return); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 891 | } else { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 892 | oldPatch = *existing; |
Mikhail Naganov | dc41773 | 2023-09-29 15:49:35 -0700 | [diff] [blame] | 893 | *existing = *_aidl_return; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 894 | } |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 895 | patchesBackup = mPatches; |
| 896 | registerPatch(*_aidl_return); |
| 897 | if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) { |
| 898 | mPatches = std::move(*patchesBackup); |
| 899 | if (existing == patches.end()) { |
| 900 | patches.pop_back(); |
| 901 | } else { |
| 902 | *existing = oldPatch; |
| 903 | } |
| 904 | return status; |
| 905 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 906 | |
| 907 | LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch " |
| 908 | << _aidl_return->toString(); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 909 | return ndk::ScopedAStatus::ok(); |
| 910 | } |
| 911 | |
| 912 | ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested, |
| 913 | AudioPortConfig* out_suggested, bool* _aidl_return) { |
| 914 | LOG(DEBUG) << __func__ << ": requested " << in_requested.toString(); |
| 915 | auto& configs = getConfig().portConfigs; |
| 916 | auto existing = configs.end(); |
| 917 | if (in_requested.id != 0) { |
| 918 | if (existing = findById<AudioPortConfig>(configs, in_requested.id); |
| 919 | existing == configs.end()) { |
| 920 | LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id |
| 921 | << " not found"; |
| 922 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | const int portId = existing != configs.end() ? existing->portId : in_requested.portId; |
| 927 | if (portId == 0) { |
| 928 | LOG(ERROR) << __func__ << ": input port config does not specify portId"; |
| 929 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 930 | } |
| 931 | auto& ports = getConfig().ports; |
| 932 | auto portIt = findById<AudioPort>(ports, portId); |
| 933 | if (portIt == ports.end()) { |
| 934 | LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId; |
| 935 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 936 | } |
| 937 | if (existing != configs.end()) { |
| 938 | *out_suggested = *existing; |
| 939 | } else { |
| 940 | AudioPortConfig newConfig; |
| 941 | if (generateDefaultPortConfig(*portIt, &newConfig)) { |
| 942 | *out_suggested = newConfig; |
| 943 | } else { |
| 944 | LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId; |
| 945 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 946 | } |
| 947 | } |
| 948 | // From this moment, 'out_suggested' is either an existing port config, |
| 949 | // or a new generated config. Now attempt to update it according to the specified |
| 950 | // fields of 'in_requested'. |
| 951 | |
| 952 | bool requestedIsValid = true, requestedIsFullySpecified = true; |
| 953 | |
| 954 | AudioIoFlags portFlags = portIt->flags; |
| 955 | if (in_requested.flags.has_value()) { |
| 956 | if (in_requested.flags.value() != portFlags) { |
| 957 | LOG(WARNING) << __func__ << ": requested flags " |
| 958 | << in_requested.flags.value().toString() << " do not match port's " |
| 959 | << portId << " flags " << portFlags.toString(); |
| 960 | requestedIsValid = false; |
| 961 | } |
| 962 | } else { |
| 963 | requestedIsFullySpecified = false; |
| 964 | } |
| 965 | |
| 966 | AudioProfile portProfile; |
| 967 | if (in_requested.format.has_value()) { |
| 968 | const auto& format = in_requested.format.value(); |
| 969 | if (findAudioProfile(*portIt, format, &portProfile)) { |
| 970 | out_suggested->format = format; |
| 971 | } else { |
| 972 | LOG(WARNING) << __func__ << ": requested format " << format.toString() |
| 973 | << " is not found in port's " << portId << " profiles"; |
| 974 | requestedIsValid = false; |
| 975 | } |
| 976 | } else { |
| 977 | requestedIsFullySpecified = false; |
| 978 | } |
| 979 | if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) { |
| 980 | LOG(ERROR) << __func__ << ": port " << portId << " does not support format " |
| 981 | << out_suggested->format.value().toString() << " anymore"; |
| 982 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 983 | } |
| 984 | |
| 985 | if (in_requested.channelMask.has_value()) { |
| 986 | const auto& channelMask = in_requested.channelMask.value(); |
| 987 | if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) != |
| 988 | portProfile.channelMasks.end()) { |
| 989 | out_suggested->channelMask = channelMask; |
| 990 | } else { |
| 991 | LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString() |
| 992 | << " is not supported for the format " << portProfile.format.toString() |
| 993 | << " by the port " << portId; |
| 994 | requestedIsValid = false; |
| 995 | } |
| 996 | } else { |
| 997 | requestedIsFullySpecified = false; |
| 998 | } |
| 999 | |
| 1000 | if (in_requested.sampleRate.has_value()) { |
| 1001 | const auto& sampleRate = in_requested.sampleRate.value(); |
| 1002 | if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(), |
| 1003 | sampleRate.value) != portProfile.sampleRates.end()) { |
| 1004 | out_suggested->sampleRate = sampleRate; |
| 1005 | } else { |
| 1006 | LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value |
| 1007 | << " is not supported for the format " << portProfile.format.toString() |
| 1008 | << " by the port " << portId; |
| 1009 | requestedIsValid = false; |
| 1010 | } |
| 1011 | } else { |
| 1012 | requestedIsFullySpecified = false; |
| 1013 | } |
| 1014 | |
| 1015 | if (in_requested.gain.has_value()) { |
| 1016 | // Let's pretend that gain can always be applied. |
| 1017 | out_suggested->gain = in_requested.gain.value(); |
| 1018 | } |
| 1019 | |
Mikhail Naganov | 248e950 | 2023-02-21 16:32:40 -0800 | [diff] [blame] | 1020 | if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) { |
| 1021 | if (in_requested.ext.getTag() == out_suggested->ext.getTag()) { |
| 1022 | if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) { |
| 1023 | // 'AudioMixPortExt.handle' is set by the client, copy from in_requested |
| 1024 | out_suggested->ext.get<AudioPortExt::Tag::mix>().handle = |
| 1025 | in_requested.ext.get<AudioPortExt::Tag::mix>().handle; |
| 1026 | } |
| 1027 | } else { |
| 1028 | LOG(WARNING) << __func__ << ": requested ext tag " |
| 1029 | << toString(in_requested.ext.getTag()) << " do not match port's tag " |
| 1030 | << toString(out_suggested->ext.getTag()); |
| 1031 | requestedIsValid = false; |
| 1032 | } |
| 1033 | } |
| 1034 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1035 | if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) { |
| 1036 | out_suggested->id = getConfig().nextPortId++; |
| 1037 | configs.push_back(*out_suggested); |
| 1038 | *_aidl_return = true; |
| 1039 | LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString(); |
| 1040 | } else if (existing != configs.end() && requestedIsValid) { |
| 1041 | *existing = *out_suggested; |
| 1042 | *_aidl_return = true; |
| 1043 | LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString(); |
| 1044 | } else { |
| 1045 | LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end()) |
| 1046 | << "; requested is valid? " << requestedIsValid << ", fully specified? " |
| 1047 | << requestedIsFullySpecified; |
| 1048 | *_aidl_return = false; |
| 1049 | } |
| 1050 | return ndk::ScopedAStatus::ok(); |
| 1051 | } |
| 1052 | |
| 1053 | ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) { |
| 1054 | auto& patches = getConfig().patches; |
| 1055 | auto patchIt = findById<AudioPatch>(patches, in_patchId); |
| 1056 | if (patchIt != patches.end()) { |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 1057 | auto patchesBackup = mPatches; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1058 | cleanUpPatch(patchIt->id); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 1059 | if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) { |
| 1060 | mPatches = std::move(patchesBackup); |
| 1061 | return status; |
| 1062 | } |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1063 | patches.erase(patchIt); |
| 1064 | LOG(DEBUG) << __func__ << ": erased patch " << in_patchId; |
| 1065 | return ndk::ScopedAStatus::ok(); |
| 1066 | } |
| 1067 | LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found"; |
| 1068 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1069 | } |
| 1070 | |
| 1071 | ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) { |
| 1072 | auto& configs = getConfig().portConfigs; |
| 1073 | auto configIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 1074 | if (configIt != configs.end()) { |
| 1075 | if (mStreams.count(in_portConfigId) != 0) { |
| 1076 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 1077 | << " has a stream opened on it"; |
| 1078 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 1079 | } |
| 1080 | auto patchIt = mPatches.find(in_portConfigId); |
| 1081 | if (patchIt != mPatches.end()) { |
| 1082 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 1083 | << " is used by the patch with id " << patchIt->second; |
| 1084 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 1085 | } |
| 1086 | auto& initials = getConfig().initialConfigs; |
| 1087 | auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId); |
| 1088 | if (initialIt == initials.end()) { |
| 1089 | configs.erase(configIt); |
| 1090 | LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId; |
| 1091 | } else if (*configIt != *initialIt) { |
| 1092 | *configIt = *initialIt; |
| 1093 | LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId; |
| 1094 | } |
| 1095 | return ndk::ScopedAStatus::ok(); |
| 1096 | } |
| 1097 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found"; |
| 1098 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1099 | } |
| 1100 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1101 | ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) { |
| 1102 | *_aidl_return = mMasterMute; |
| 1103 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1104 | return ndk::ScopedAStatus::ok(); |
| 1105 | } |
| 1106 | |
| 1107 | ndk::ScopedAStatus Module::setMasterMute(bool in_mute) { |
| 1108 | LOG(DEBUG) << __func__ << ": " << in_mute; |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 1109 | auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok() |
| 1110 | : onMasterMuteChanged(in_mute); |
| 1111 | if (result.isOk()) { |
| 1112 | mMasterMute = in_mute; |
| 1113 | } else { |
| 1114 | LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute |
| 1115 | << "), error=" << result; |
| 1116 | // Reset master mute if it failed. |
| 1117 | onMasterMuteChanged(mMasterMute); |
| 1118 | } |
| 1119 | return std::move(result); |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) { |
| 1123 | *_aidl_return = mMasterVolume; |
| 1124 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1125 | return ndk::ScopedAStatus::ok(); |
| 1126 | } |
| 1127 | |
| 1128 | ndk::ScopedAStatus Module::setMasterVolume(float in_volume) { |
| 1129 | LOG(DEBUG) << __func__ << ": " << in_volume; |
| 1130 | if (in_volume >= 0.0f && in_volume <= 1.0f) { |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 1131 | auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok() |
| 1132 | : onMasterVolumeChanged(in_volume); |
| 1133 | if (result.isOk()) { |
| 1134 | mMasterVolume = in_volume; |
| 1135 | } else { |
| 1136 | // Reset master volume if it failed. |
| 1137 | LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume |
| 1138 | << "), error=" << result; |
| 1139 | onMasterVolumeChanged(mMasterVolume); |
| 1140 | } |
| 1141 | return std::move(result); |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1142 | } |
| 1143 | LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume; |
| 1144 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1145 | } |
| 1146 | |
| 1147 | ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) { |
| 1148 | *_aidl_return = mMicMute; |
| 1149 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1150 | return ndk::ScopedAStatus::ok(); |
| 1151 | } |
| 1152 | |
| 1153 | ndk::ScopedAStatus Module::setMicMute(bool in_mute) { |
| 1154 | LOG(DEBUG) << __func__ << ": " << in_mute; |
| 1155 | mMicMute = in_mute; |
| 1156 | return ndk::ScopedAStatus::ok(); |
| 1157 | } |
| 1158 | |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 1159 | ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) { |
Pawan Wagh | 6f57cd9 | 2023-02-01 21:14:34 +0000 | [diff] [blame] | 1160 | *_aidl_return = getConfig().microphones; |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 1161 | LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return); |
| 1162 | return ndk::ScopedAStatus::ok(); |
| 1163 | } |
| 1164 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1165 | ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) { |
Mikhail Naganov | 04ae822 | 2023-01-11 15:48:10 -0800 | [diff] [blame] | 1166 | if (!isValidAudioMode(in_mode)) { |
| 1167 | LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode); |
| 1168 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1169 | } |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1170 | // No checks for supported audio modes here, it's an informative notification. |
| 1171 | LOG(DEBUG) << __func__ << ": " << toString(in_mode); |
| 1172 | return ndk::ScopedAStatus::ok(); |
| 1173 | } |
| 1174 | |
| 1175 | ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) { |
| 1176 | LOG(DEBUG) << __func__ << ": " << toString(in_rotation); |
| 1177 | return ndk::ScopedAStatus::ok(); |
| 1178 | } |
| 1179 | |
| 1180 | ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) { |
| 1181 | LOG(DEBUG) << __func__ << ": " << in_isTurnedOn; |
| 1182 | return ndk::ScopedAStatus::ok(); |
| 1183 | } |
| 1184 | |
Vlad Popa | 83a6d82 | 2022-11-07 13:53:57 +0100 | [diff] [blame] | 1185 | ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) { |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 1186 | if (!mSoundDose) { |
Vlad Popa | 2afbd1e | 2022-12-28 17:04:58 +0100 | [diff] [blame] | 1187 | mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>(); |
Vlad Popa | 943b7e2 | 2022-12-08 14:24:12 +0100 | [diff] [blame] | 1188 | } |
Mikhail Naganov | 780fefb | 2023-07-21 17:01:38 -0700 | [diff] [blame] | 1189 | *_aidl_return = mSoundDose.getInstance(); |
Vlad Popa | 943b7e2 | 2022-12-08 14:24:12 +0100 | [diff] [blame] | 1190 | LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get(); |
Vlad Popa | 83a6d82 | 2022-11-07 13:53:57 +0100 | [diff] [blame] | 1191 | return ndk::ScopedAStatus::ok(); |
| 1192 | } |
| 1193 | |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1194 | ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) { |
| 1195 | LOG(DEBUG) << __func__; |
| 1196 | (void)_aidl_return; |
| 1197 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1198 | } |
| 1199 | |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1200 | const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst"; |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1201 | const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain"; |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1202 | |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1203 | ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids, |
| 1204 | std::vector<VendorParameter>* _aidl_return) { |
| 1205 | LOG(DEBUG) << __func__ << ": id count: " << in_ids.size(); |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1206 | bool allParametersKnown = true; |
| 1207 | for (const auto& id : in_ids) { |
| 1208 | if (id == VendorDebug::kForceTransientBurstName) { |
| 1209 | VendorParameter forceTransientBurst{.id = id}; |
| 1210 | forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst}); |
| 1211 | _aidl_return->push_back(std::move(forceTransientBurst)); |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1212 | } else if (id == VendorDebug::kForceSynchronousDrainName) { |
| 1213 | VendorParameter forceSynchronousDrain{.id = id}; |
| 1214 | forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain}); |
| 1215 | _aidl_return->push_back(std::move(forceSynchronousDrain)); |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1216 | } else { |
| 1217 | allParametersKnown = false; |
| 1218 | LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\""; |
| 1219 | } |
| 1220 | } |
| 1221 | if (allParametersKnown) return ndk::ScopedAStatus::ok(); |
| 1222 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1225 | namespace { |
| 1226 | |
| 1227 | template <typename W> |
| 1228 | bool extractParameter(const VendorParameter& p, decltype(W::value)* v) { |
| 1229 | std::optional<W> value; |
| 1230 | binder_status_t result = p.ext.getParcelable(&value); |
| 1231 | if (result == STATUS_OK && value.has_value()) { |
| 1232 | *v = value.value().value; |
| 1233 | return true; |
| 1234 | } |
| 1235 | LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id |
| 1236 | << "\": " << result; |
| 1237 | return false; |
| 1238 | } |
| 1239 | |
| 1240 | } // namespace |
| 1241 | |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1242 | ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters, |
| 1243 | bool in_async) { |
| 1244 | LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size() |
| 1245 | << ", async: " << in_async; |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1246 | bool allParametersKnown = true; |
| 1247 | for (const auto& p : in_parameters) { |
| 1248 | if (p.id == VendorDebug::kForceTransientBurstName) { |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1249 | if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) { |
| 1250 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1251 | } |
| 1252 | } else if (p.id == VendorDebug::kForceSynchronousDrainName) { |
| 1253 | if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) { |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1254 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1255 | } |
| 1256 | } else { |
| 1257 | allParametersKnown = false; |
| 1258 | LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\""; |
| 1259 | } |
| 1260 | } |
| 1261 | if (allParametersKnown) return ndk::ScopedAStatus::ok(); |
| 1262 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Mikhail Naganov | fb1acde | 2022-12-12 18:57:36 +0000 | [diff] [blame] | 1265 | ndk::ScopedAStatus Module::addDeviceEffect( |
| 1266 | int32_t in_portConfigId, |
| 1267 | const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) { |
| 1268 | if (in_effect == nullptr) { |
| 1269 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect"; |
| 1270 | } else { |
| 1271 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder " |
| 1272 | << in_effect->asBinder().get(); |
| 1273 | } |
| 1274 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1275 | } |
| 1276 | |
| 1277 | ndk::ScopedAStatus Module::removeDeviceEffect( |
| 1278 | int32_t in_portConfigId, |
| 1279 | const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) { |
| 1280 | if (in_effect == nullptr) { |
| 1281 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect"; |
| 1282 | } else { |
| 1283 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder " |
| 1284 | << in_effect->asBinder().get(); |
| 1285 | } |
| 1286 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1287 | } |
| 1288 | |
jiabin | 9a8e686 | 2023-01-12 23:06:37 +0000 | [diff] [blame] | 1289 | ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType, |
| 1290 | std::vector<AudioMMapPolicyInfo>* _aidl_return) { |
| 1291 | LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType); |
| 1292 | std::set<int32_t> mmapSinks; |
| 1293 | std::set<int32_t> mmapSources; |
| 1294 | auto& ports = getConfig().ports; |
| 1295 | for (const auto& port : ports) { |
| 1296 | if (port.flags.getTag() == AudioIoFlags::Tag::input && |
| 1297 | isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(), |
| 1298 | AudioInputFlags::MMAP_NOIRQ)) { |
| 1299 | mmapSinks.insert(port.id); |
| 1300 | } else if (port.flags.getTag() == AudioIoFlags::Tag::output && |
| 1301 | isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(), |
| 1302 | AudioOutputFlags::MMAP_NOIRQ)) { |
| 1303 | mmapSources.insert(port.id); |
| 1304 | } |
| 1305 | } |
Mikhail Naganov | 8506491 | 2023-09-26 17:10:08 -0700 | [diff] [blame] | 1306 | if (mmapSources.empty() && mmapSinks.empty()) { |
| 1307 | AudioMMapPolicyInfo never; |
| 1308 | never.mmapPolicy = AudioMMapPolicy::NEVER; |
| 1309 | _aidl_return->push_back(never); |
| 1310 | return ndk::ScopedAStatus::ok(); |
| 1311 | } |
jiabin | 9a8e686 | 2023-01-12 23:06:37 +0000 | [diff] [blame] | 1312 | for (const auto& route : getConfig().routes) { |
| 1313 | if (mmapSinks.count(route.sinkPortId) != 0) { |
| 1314 | // The sink is a mix port, add the sources if they are device ports. |
| 1315 | for (int sourcePortId : route.sourcePortIds) { |
| 1316 | auto sourcePortIt = findById<AudioPort>(ports, sourcePortId); |
| 1317 | if (sourcePortIt == ports.end()) { |
| 1318 | // This must not happen |
| 1319 | LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found"; |
| 1320 | continue; |
| 1321 | } |
| 1322 | if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 1323 | // The source is not a device port, skip |
| 1324 | continue; |
| 1325 | } |
| 1326 | AudioMMapPolicyInfo policyInfo; |
| 1327 | policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device; |
| 1328 | // Always return AudioMMapPolicy.AUTO if the device supports mmap for |
| 1329 | // default implementation. |
| 1330 | policyInfo.mmapPolicy = AudioMMapPolicy::AUTO; |
| 1331 | _aidl_return->push_back(policyInfo); |
| 1332 | } |
| 1333 | } else { |
| 1334 | auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId); |
| 1335 | if (sinkPortIt == ports.end()) { |
| 1336 | // This must not happen |
| 1337 | LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found"; |
| 1338 | continue; |
| 1339 | } |
| 1340 | if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 1341 | // The sink is not a device port, skip |
| 1342 | continue; |
| 1343 | } |
| 1344 | if (count_any(mmapSources, route.sourcePortIds)) { |
| 1345 | AudioMMapPolicyInfo policyInfo; |
| 1346 | policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device; |
| 1347 | // Always return AudioMMapPolicy.AUTO if the device supports mmap for |
| 1348 | // default implementation. |
| 1349 | policyInfo.mmapPolicy = AudioMMapPolicy::AUTO; |
| 1350 | _aidl_return->push_back(policyInfo); |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | return ndk::ScopedAStatus::ok(); |
| 1355 | } |
| 1356 | |
Eric Laurent | e2432ea | 2023-01-12 17:47:31 +0100 | [diff] [blame] | 1357 | ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) { |
| 1358 | LOG(DEBUG) << __func__; |
| 1359 | *_aidl_return = false; |
| 1360 | return ndk::ScopedAStatus::ok(); |
| 1361 | } |
| 1362 | |
jiabin | b76981e | 2023-01-18 00:58:30 +0000 | [diff] [blame] | 1363 | ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) { |
| 1364 | if (!isMmapSupported()) { |
| 1365 | LOG(DEBUG) << __func__ << ": mmap is not supported "; |
| 1366 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1367 | } |
| 1368 | *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT; |
| 1369 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1370 | return ndk::ScopedAStatus::ok(); |
| 1371 | } |
| 1372 | |
| 1373 | ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) { |
| 1374 | if (!isMmapSupported()) { |
| 1375 | LOG(DEBUG) << __func__ << ": mmap is not supported "; |
| 1376 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1377 | } |
| 1378 | *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US; |
| 1379 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1380 | return ndk::ScopedAStatus::ok(); |
| 1381 | } |
| 1382 | |
| 1383 | bool Module::isMmapSupported() { |
| 1384 | if (mIsMmapSupported.has_value()) { |
| 1385 | return mIsMmapSupported.value(); |
| 1386 | } |
| 1387 | std::vector<AudioMMapPolicyInfo> mmapPolicyInfos; |
| 1388 | if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) { |
| 1389 | mIsMmapSupported = false; |
| 1390 | } else { |
| 1391 | mIsMmapSupported = |
| 1392 | std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) { |
| 1393 | return info.mmapPolicy == AudioMMapPolicy::AUTO || |
| 1394 | info.mmapPolicy == AudioMMapPolicy::ALWAYS; |
| 1395 | }) != mmapPolicyInfos.end(); |
| 1396 | } |
| 1397 | return mIsMmapSupported.value(); |
| 1398 | } |
| 1399 | |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 1400 | ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) { |
jiabin | 116d839 | 2023-03-01 22:52:57 +0000 | [diff] [blame] | 1401 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 1402 | return ndk::ScopedAStatus::ok(); |
| 1403 | } |
| 1404 | |
| 1405 | ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch( |
| 1406 | const std::vector<AudioPortConfig*>& sources __unused, |
| 1407 | const std::vector<AudioPortConfig*>& sinks __unused) { |
jiabin | 116d839 | 2023-03-01 22:52:57 +0000 | [diff] [blame] | 1408 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 1409 | return ndk::ScopedAStatus::ok(); |
| 1410 | } |
| 1411 | |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 1412 | void Module::onExternalDeviceConnectionChanged( |
| 1413 | const ::aidl::android::media::audio::common::AudioPort& audioPort __unused, |
| 1414 | bool connected __unused) { |
| 1415 | LOG(DEBUG) << __func__ << ": do nothing and return"; |
| 1416 | } |
| 1417 | |
| 1418 | ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) { |
| 1419 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
| 1420 | return ndk::ScopedAStatus::ok(); |
| 1421 | } |
| 1422 | |
| 1423 | ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) { |
| 1424 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
| 1425 | return ndk::ScopedAStatus::ok(); |
| 1426 | } |
| 1427 | |
Ram Mohan | 18f0d51 | 2023-07-01 00:47:09 +0530 | [diff] [blame] | 1428 | Module::BtProfileHandles Module::getBtProfileManagerHandles() { |
| 1429 | return std::make_tuple(std::weak_ptr<IBluetooth>(), std::weak_ptr<IBluetoothA2dp>(), |
| 1430 | std::weak_ptr<IBluetoothLe>()); |
| 1431 | } |
| 1432 | |
| 1433 | ndk::ScopedAStatus Module::bluetoothParametersUpdated() { |
| 1434 | return mStreams.bluetoothParametersUpdated(); |
| 1435 | } |
| 1436 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1437 | } // namespace aidl::android::hardware::audio::core |