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 | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 343 | // Streams from the old patch need to be disconnected, streams from the new |
| 344 | // patch need to be connected. If the stream belongs to both patches, no need |
| 345 | // to update it. |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 346 | auto maybeFailure = ndk::ScopedAStatus::ok(); |
| 347 | std::set<int32_t> idsToDisconnect, idsToConnect, idsToDisconnectOnFailure; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 348 | idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(), |
| 349 | oldPatch.sourcePortConfigIds.end()); |
| 350 | idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end()); |
| 351 | idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end()); |
| 352 | idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end()); |
| 353 | std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) { |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 354 | if (idsToConnect.count(portConfigId) == 0 && mStreams.count(portConfigId) != 0) { |
| 355 | if (auto status = mStreams.setStreamConnectedDevices(portConfigId, {}); status.isOk()) { |
| 356 | LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id " |
| 357 | << portConfigId << " has been disconnected"; |
| 358 | } else { |
| 359 | // Disconnection is tricky to roll back, just register a failure. |
| 360 | maybeFailure = std::move(status); |
| 361 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 362 | } |
| 363 | }); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 364 | if (!maybeFailure.isOk()) return maybeFailure; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 365 | std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) { |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 366 | if (idsToDisconnect.count(portConfigId) == 0 && mStreams.count(portConfigId) != 0) { |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 367 | const auto connectedDevices = findConnectedDevices(portConfigId); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 368 | if (connectedDevices.empty()) { |
| 369 | // This is important as workers use the vector size to derive the connection status. |
| 370 | LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port " |
| 371 | "config id " |
| 372 | << portConfigId; |
| 373 | } |
| 374 | if (auto status = mStreams.setStreamConnectedDevices(portConfigId, connectedDevices); |
| 375 | status.isOk()) { |
| 376 | LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id " |
| 377 | << portConfigId << " has been connected to: " |
| 378 | << ::android::internal::ToString(connectedDevices); |
| 379 | } else { |
| 380 | maybeFailure = std::move(status); |
| 381 | idsToDisconnectOnFailure.insert(portConfigId); |
| 382 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 383 | } |
| 384 | }); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 385 | if (!maybeFailure.isOk()) { |
| 386 | LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids " |
| 387 | << ::android::internal::ToString(idsToDisconnectOnFailure); |
| 388 | std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(), |
| 389 | [&](const auto& portConfigId) { |
| 390 | auto status = mStreams.setStreamConnectedDevices(portConfigId, {}); |
| 391 | (void)status.isOk(); // Can't do much about a failure here. |
| 392 | }); |
| 393 | return maybeFailure; |
| 394 | } |
| 395 | return ndk::ScopedAStatus::ok(); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 398 | ndk::ScopedAStatus Module::setModuleDebug( |
| 399 | const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) { |
Mikhail Naganov | d5536d9 | 2023-03-24 18:27:58 -0700 | [diff] [blame] | 400 | LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString() |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 401 | << ", new flags: " << in_debug.toString(); |
| 402 | if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections && |
| 403 | !mConnectedDevicePorts.empty()) { |
Mikhail Naganov | d5536d9 | 2023-03-24 18:27:58 -0700 | [diff] [blame] | 404 | LOG(ERROR) << __func__ << ": " << mType |
| 405 | << ": attempting to change device connections simulation while having external " |
| 406 | << "devices connected"; |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 407 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 408 | } |
Mikhail Naganov | bd483c0 | 2022-11-17 20:33:39 +0000 | [diff] [blame] | 409 | if (in_debug.streamTransientStateDelayMs < 0) { |
Mikhail Naganov | d5536d9 | 2023-03-24 18:27:58 -0700 | [diff] [blame] | 410 | LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: " |
Mikhail Naganov | bd483c0 | 2022-11-17 20:33:39 +0000 | [diff] [blame] | 411 | << in_debug.streamTransientStateDelayMs; |
| 412 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 413 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 414 | mDebug = in_debug; |
| 415 | return ndk::ScopedAStatus::ok(); |
| 416 | } |
| 417 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 418 | ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) { |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 419 | *_aidl_return = nullptr; |
| 420 | LOG(DEBUG) << __func__ << ": returning null"; |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 421 | return ndk::ScopedAStatus::ok(); |
| 422 | } |
| 423 | |
Mikhail Naganov | 10c6fe2 | 2022-09-30 23:49:17 +0000 | [diff] [blame] | 424 | ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) { |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 425 | *_aidl_return = nullptr; |
| 426 | LOG(DEBUG) << __func__ << ": returning null"; |
Mikhail Naganov | 10c6fe2 | 2022-09-30 23:49:17 +0000 | [diff] [blame] | 427 | return ndk::ScopedAStatus::ok(); |
| 428 | } |
| 429 | |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 430 | ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) { |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 431 | *_aidl_return = nullptr; |
| 432 | LOG(DEBUG) << __func__ << ": returning null"; |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 433 | return ndk::ScopedAStatus::ok(); |
| 434 | } |
| 435 | |
Mikhail Naganov | b5647da | 2023-03-06 14:37:38 -0800 | [diff] [blame] | 436 | ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) { |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 437 | *_aidl_return = nullptr; |
| 438 | LOG(DEBUG) << __func__ << ": returning null"; |
Mikhail Naganov | b5647da | 2023-03-06 14:37:38 -0800 | [diff] [blame] | 439 | return ndk::ScopedAStatus::ok(); |
| 440 | } |
| 441 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 442 | ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData, |
| 443 | AudioPort* _aidl_return) { |
| 444 | const int32_t templateId = in_templateIdAndAdditionalData.id; |
| 445 | auto& ports = getConfig().ports; |
| 446 | AudioPort connectedPort; |
| 447 | { // Scope the template port so that we don't accidentally modify it. |
| 448 | auto templateIt = findById<AudioPort>(ports, templateId); |
| 449 | if (templateIt == ports.end()) { |
| 450 | LOG(ERROR) << __func__ << ": port id " << templateId << " not found"; |
| 451 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 452 | } |
| 453 | if (templateIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 454 | LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port"; |
| 455 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 456 | } |
| 457 | if (!templateIt->profiles.empty()) { |
| 458 | LOG(ERROR) << __func__ << ": port id " << templateId |
| 459 | << " does not have dynamic profiles"; |
| 460 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 461 | } |
| 462 | auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>(); |
| 463 | if (templateDevicePort.device.type.connection.empty()) { |
| 464 | LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached"; |
| 465 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 466 | } |
| 467 | // Postpone id allocation until we ensure that there are no client errors. |
| 468 | connectedPort = *templateIt; |
| 469 | connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors; |
| 470 | const auto& inputDevicePort = |
| 471 | in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>(); |
| 472 | auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>(); |
| 473 | connectedDevicePort.device.address = inputDevicePort.device.address; |
| 474 | LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to " |
| 475 | << connectedDevicePort.device.toString(); |
| 476 | // 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] | 477 | for (auto connectedPortPair : mConnectedDevicePorts) { |
| 478 | auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 479 | if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device == |
| 480 | connectedDevicePort.device) { |
| 481 | LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString() |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 482 | << " is already connected at the device port id " |
| 483 | << connectedPortPair.first; |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 484 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (!mDebug.simulateDeviceConnections) { |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 490 | RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort)); |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 491 | } else { |
| 492 | auto& connectedProfiles = getConfig().connectedProfiles; |
| 493 | if (auto connectedProfilesIt = connectedProfiles.find(templateId); |
| 494 | connectedProfilesIt != connectedProfiles.end()) { |
| 495 | connectedPort.profiles = connectedProfilesIt->second; |
| 496 | } |
| 497 | } |
| 498 | if (connectedPort.profiles.empty()) { |
| 499 | LOG(ERROR) << "Profiles of a connected port still empty after connecting external device " |
| 500 | << connectedPort.toString(); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 501 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 502 | } |
| 503 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 504 | for (auto profile : connectedPort.profiles) { |
| 505 | if (profile.channelMasks.empty()) { |
| 506 | LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no channel masks"; |
| 507 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 508 | } |
| 509 | if (profile.sampleRates.empty()) { |
| 510 | LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no sample rates"; |
| 511 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 512 | } |
| 513 | } |
| 514 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 515 | connectedPort.id = ++getConfig().nextPortId; |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 516 | auto [connectedPortsIt, _] = |
| 517 | mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::vector<int32_t>())); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 518 | LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, " |
| 519 | << "connected port ID " << connectedPort.id; |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 520 | ports.push_back(connectedPort); |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 521 | onExternalDeviceConnectionChanged(connectedPort, true /*connected*/); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 522 | |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 523 | std::vector<int32_t> routablePortIds; |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 524 | std::vector<AudioRoute> newRoutes; |
| 525 | auto& routes = getConfig().routes; |
| 526 | for (auto& r : routes) { |
| 527 | if (r.sinkPortId == templateId) { |
| 528 | AudioRoute newRoute; |
| 529 | newRoute.sourcePortIds = r.sourcePortIds; |
| 530 | newRoute.sinkPortId = connectedPort.id; |
| 531 | newRoute.isExclusive = r.isExclusive; |
| 532 | newRoutes.push_back(std::move(newRoute)); |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 533 | routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(), |
| 534 | r.sourcePortIds.end()); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 535 | } else { |
| 536 | auto& srcs = r.sourcePortIds; |
| 537 | if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) { |
| 538 | srcs.push_back(connectedPort.id); |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 539 | routablePortIds.push_back(r.sinkPortId); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | } |
| 543 | routes.insert(routes.end(), newRoutes.begin(), newRoutes.end()); |
| 544 | |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 545 | // Note: this is a simplistic approach assuming that a mix port can only be populated |
| 546 | // from a single device port. Implementing support for stuffing dynamic profiles with a superset |
| 547 | // of all profiles from all routable dynamic device ports would be more involved. |
| 548 | for (const auto mixPortId : routablePortIds) { |
| 549 | auto portsIt = findById<AudioPort>(ports, mixPortId); |
| 550 | if (portsIt != ports.end() && portsIt->profiles.empty()) { |
| 551 | portsIt->profiles = connectedPort.profiles; |
| 552 | connectedPortsIt->second.push_back(portsIt->id); |
| 553 | } |
| 554 | } |
| 555 | *_aidl_return = std::move(connectedPort); |
| 556 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 557 | return ndk::ScopedAStatus::ok(); |
| 558 | } |
| 559 | |
| 560 | ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) { |
| 561 | auto& ports = getConfig().ports; |
| 562 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 563 | if (portIt == ports.end()) { |
| 564 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 565 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 566 | } |
| 567 | if (portIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 568 | LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port"; |
| 569 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 570 | } |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 571 | auto connectedPortsIt = mConnectedDevicePorts.find(in_portId); |
| 572 | if (connectedPortsIt == mConnectedDevicePorts.end()) { |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 573 | LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port"; |
| 574 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 575 | } |
| 576 | auto& configs = getConfig().portConfigs; |
| 577 | auto& initials = getConfig().initialConfigs; |
| 578 | auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) { |
| 579 | if (config.portId == in_portId) { |
| 580 | // Check if the configuration was provided by the client. |
| 581 | const auto& initialIt = findById<AudioPortConfig>(initials, config.id); |
| 582 | return initialIt == initials.end() || config != *initialIt; |
| 583 | } |
| 584 | return false; |
| 585 | }); |
| 586 | if (configIt != configs.end()) { |
| 587 | LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id " |
| 588 | << configIt->id; |
| 589 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 590 | } |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 591 | onExternalDeviceConnectionChanged(*portIt, false /*connected*/); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 592 | ports.erase(portIt); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 593 | LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released"; |
| 594 | |
| 595 | auto& routes = getConfig().routes; |
| 596 | for (auto routesIt = routes.begin(); routesIt != routes.end();) { |
| 597 | if (routesIt->sinkPortId == in_portId) { |
| 598 | routesIt = routes.erase(routesIt); |
| 599 | } else { |
| 600 | // Note: the list of sourcePortIds can't become empty because there must |
| 601 | // be the id of the template port in the route. |
| 602 | erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; }); |
| 603 | ++routesIt; |
| 604 | } |
| 605 | } |
| 606 | |
Mikhail Naganov | 7b2d12b | 2023-03-24 18:29:14 -0700 | [diff] [blame] | 607 | for (const auto mixPortId : connectedPortsIt->second) { |
| 608 | auto mixPortIt = findById<AudioPort>(ports, mixPortId); |
| 609 | if (mixPortIt != ports.end()) { |
| 610 | mixPortIt->profiles = {}; |
| 611 | } |
| 612 | } |
| 613 | mConnectedDevicePorts.erase(connectedPortsIt); |
| 614 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 615 | return ndk::ScopedAStatus::ok(); |
| 616 | } |
| 617 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 618 | ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) { |
| 619 | *_aidl_return = getConfig().patches; |
| 620 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches"; |
| 621 | return ndk::ScopedAStatus::ok(); |
| 622 | } |
| 623 | |
| 624 | ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) { |
| 625 | auto& ports = getConfig().ports; |
| 626 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 627 | if (portIt != ports.end()) { |
| 628 | *_aidl_return = *portIt; |
| 629 | LOG(DEBUG) << __func__ << ": returning port by id " << in_portId; |
| 630 | return ndk::ScopedAStatus::ok(); |
| 631 | } |
| 632 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 633 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 634 | } |
| 635 | |
| 636 | ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) { |
| 637 | *_aidl_return = getConfig().portConfigs; |
| 638 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs"; |
| 639 | return ndk::ScopedAStatus::ok(); |
| 640 | } |
| 641 | |
| 642 | ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) { |
| 643 | *_aidl_return = getConfig().ports; |
| 644 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports"; |
| 645 | return ndk::ScopedAStatus::ok(); |
| 646 | } |
| 647 | |
| 648 | ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) { |
| 649 | *_aidl_return = getConfig().routes; |
| 650 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes"; |
| 651 | return ndk::ScopedAStatus::ok(); |
| 652 | } |
| 653 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 654 | ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId, |
| 655 | std::vector<AudioRoute>* _aidl_return) { |
| 656 | auto& ports = getConfig().ports; |
| 657 | if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) { |
| 658 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 659 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 660 | } |
| 661 | auto& routes = getConfig().routes; |
| 662 | std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return), |
| 663 | [&](const auto& r) { |
| 664 | const auto& srcs = r.sourcePortIds; |
| 665 | return r.sinkPortId == in_portId || |
| 666 | std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end(); |
| 667 | }); |
| 668 | return ndk::ScopedAStatus::ok(); |
| 669 | } |
| 670 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 671 | ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args, |
| 672 | OpenInputStreamReturn* _aidl_return) { |
| 673 | LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size " |
| 674 | << in_args.bufferSizeFrames << " frames"; |
| 675 | AudioPort* port = nullptr; |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 676 | RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 677 | if (port->flags.getTag() != AudioIoFlags::Tag::input) { |
| 678 | LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 679 | << " does not correspond to an input mix port"; |
| 680 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 681 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 682 | StreamContext context; |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 683 | RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, |
| 684 | nullptr, nullptr, &context)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 685 | context.fillDescriptor(&_aidl_return->desc); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 686 | std::shared_ptr<StreamIn> stream; |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 687 | RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata, |
Mikhail Naganov | 9d16a6a | 2023-06-26 17:21:04 -0700 | [diff] [blame] | 688 | mConfig->microphones, &stream)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 689 | StreamWrapper streamWrapper(stream); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 690 | if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) { |
| 691 | RETURN_STATUS_IF_ERROR( |
| 692 | streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId))); |
| 693 | } |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 694 | AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL, |
| 695 | ANDROID_PRIORITY_AUDIO); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 696 | mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 697 | _aidl_return->stream = std::move(stream); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 698 | return ndk::ScopedAStatus::ok(); |
| 699 | } |
| 700 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 701 | ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args, |
| 702 | OpenOutputStreamReturn* _aidl_return) { |
| 703 | LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? " |
| 704 | << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames |
| 705 | << " frames"; |
| 706 | AudioPort* port = nullptr; |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 707 | RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 708 | if (port->flags.getTag() != AudioIoFlags::Tag::output) { |
| 709 | LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 710 | << " does not correspond to an output mix port"; |
| 711 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 712 | } |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 713 | const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(), |
| 714 | AudioOutputFlags::COMPRESS_OFFLOAD); |
| 715 | if (isOffload && !in_args.offloadInfo.has_value()) { |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 716 | LOG(ERROR) << __func__ << ": port id " << port->id |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 717 | << " has COMPRESS_OFFLOAD flag set, requires offload info"; |
| 718 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 719 | } |
Mikhail Naganov | 30301a4 | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 720 | const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(), |
| 721 | AudioOutputFlags::NON_BLOCKING); |
| 722 | if (isNonBlocking && in_args.callback == nullptr) { |
| 723 | LOG(ERROR) << __func__ << ": port id " << port->id |
| 724 | << " has NON_BLOCKING flag set, requires async callback"; |
| 725 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 726 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 727 | StreamContext context; |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 728 | RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, |
| 729 | isNonBlocking ? in_args.callback : nullptr, |
| 730 | in_args.eventCallback, &context)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 731 | context.fillDescriptor(&_aidl_return->desc); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 732 | std::shared_ptr<StreamOut> stream; |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 733 | RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata, |
Mikhail Naganov | 9d16a6a | 2023-06-26 17:21:04 -0700 | [diff] [blame] | 734 | in_args.offloadInfo, &stream)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 735 | StreamWrapper streamWrapper(stream); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 736 | if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) { |
| 737 | RETURN_STATUS_IF_ERROR( |
| 738 | streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId))); |
| 739 | } |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 740 | AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL, |
| 741 | ANDROID_PRIORITY_AUDIO); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 742 | mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 743 | _aidl_return->stream = std::move(stream); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 744 | return ndk::ScopedAStatus::ok(); |
| 745 | } |
| 746 | |
Mikhail Naganov | 7492720 | 2022-12-19 16:37:14 +0000 | [diff] [blame] | 747 | ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors( |
| 748 | SupportedPlaybackRateFactors* _aidl_return) { |
| 749 | LOG(DEBUG) << __func__; |
| 750 | (void)_aidl_return; |
| 751 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 752 | } |
| 753 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 754 | ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) { |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 755 | LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString(); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 756 | if (in_requested.sourcePortConfigIds.empty()) { |
| 757 | LOG(ERROR) << __func__ << ": requested patch has empty sources list"; |
| 758 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 759 | } |
| 760 | if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) { |
| 761 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list"; |
| 762 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 763 | } |
| 764 | if (in_requested.sinkPortConfigIds.empty()) { |
| 765 | LOG(ERROR) << __func__ << ": requested patch has empty sinks list"; |
| 766 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 767 | } |
| 768 | if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) { |
| 769 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list"; |
| 770 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 771 | } |
| 772 | |
| 773 | auto& configs = getConfig().portConfigs; |
| 774 | std::vector<int32_t> missingIds; |
| 775 | auto sources = |
| 776 | selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds); |
| 777 | if (!missingIds.empty()) { |
| 778 | LOG(ERROR) << __func__ << ": following source port config ids not found: " |
| 779 | << ::android::internal::ToString(missingIds); |
| 780 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 781 | } |
| 782 | auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds); |
| 783 | if (!missingIds.empty()) { |
| 784 | LOG(ERROR) << __func__ << ": following sink port config ids not found: " |
| 785 | << ::android::internal::ToString(missingIds); |
| 786 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 787 | } |
| 788 | // bool indicates whether a non-exclusive route is available. |
| 789 | // If only an exclusive route is available, that means the patch can not be |
| 790 | // established if there is any other patch which currently uses the sink port. |
| 791 | std::map<int32_t, bool> allowedSinkPorts; |
| 792 | auto& routes = getConfig().routes; |
| 793 | for (auto src : sources) { |
| 794 | for (const auto& r : routes) { |
| 795 | const auto& srcs = r.sourcePortIds; |
| 796 | if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) { |
| 797 | if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive |
| 798 | allowedSinkPorts[r.sinkPortId] = !r.isExclusive; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | for (auto sink : sinks) { |
| 804 | if (allowedSinkPorts.count(sink->portId) == 0) { |
| 805 | LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId; |
| 806 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 807 | } |
| 808 | } |
Mikhail Naganov | 26dc9ad | 2023-06-23 13:55:37 -0700 | [diff] [blame] | 809 | RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks)); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 810 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 811 | auto& patches = getConfig().patches; |
| 812 | auto existing = patches.end(); |
| 813 | std::optional<decltype(mPatches)> patchesBackup; |
| 814 | if (in_requested.id != 0) { |
| 815 | existing = findById<AudioPatch>(patches, in_requested.id); |
| 816 | if (existing != patches.end()) { |
| 817 | patchesBackup = mPatches; |
| 818 | cleanUpPatch(existing->id); |
| 819 | } else { |
| 820 | LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id; |
| 821 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 822 | } |
| 823 | } |
| 824 | // Validate the requested patch. |
| 825 | for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) { |
| 826 | if (!nonExclusive && mPatches.count(sinkPortId) != 0) { |
| 827 | LOG(ERROR) << __func__ << ": sink port id " << sinkPortId |
| 828 | << "is exclusive and is already used by some other patch"; |
| 829 | if (patchesBackup.has_value()) { |
| 830 | mPatches = std::move(*patchesBackup); |
| 831 | } |
| 832 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 833 | } |
| 834 | } |
| 835 | *_aidl_return = in_requested; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 836 | _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames; |
| 837 | _aidl_return->latenciesMs.clear(); |
| 838 | _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(), |
| 839 | _aidl_return->sinkPortConfigIds.size(), kLatencyMs); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 840 | AudioPatch oldPatch{}; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 841 | if (existing == patches.end()) { |
| 842 | _aidl_return->id = getConfig().nextPatchId++; |
| 843 | patches.push_back(*_aidl_return); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 844 | } else { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 845 | oldPatch = *existing; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 846 | } |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 847 | patchesBackup = mPatches; |
| 848 | registerPatch(*_aidl_return); |
| 849 | if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) { |
| 850 | mPatches = std::move(*patchesBackup); |
| 851 | if (existing == patches.end()) { |
| 852 | patches.pop_back(); |
| 853 | } else { |
| 854 | *existing = oldPatch; |
| 855 | } |
| 856 | return status; |
| 857 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 858 | |
| 859 | LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch " |
| 860 | << _aidl_return->toString(); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 861 | return ndk::ScopedAStatus::ok(); |
| 862 | } |
| 863 | |
| 864 | ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested, |
| 865 | AudioPortConfig* out_suggested, bool* _aidl_return) { |
| 866 | LOG(DEBUG) << __func__ << ": requested " << in_requested.toString(); |
| 867 | auto& configs = getConfig().portConfigs; |
| 868 | auto existing = configs.end(); |
| 869 | if (in_requested.id != 0) { |
| 870 | if (existing = findById<AudioPortConfig>(configs, in_requested.id); |
| 871 | existing == configs.end()) { |
| 872 | LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id |
| 873 | << " not found"; |
| 874 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | const int portId = existing != configs.end() ? existing->portId : in_requested.portId; |
| 879 | if (portId == 0) { |
| 880 | LOG(ERROR) << __func__ << ": input port config does not specify portId"; |
| 881 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 882 | } |
| 883 | auto& ports = getConfig().ports; |
| 884 | auto portIt = findById<AudioPort>(ports, portId); |
| 885 | if (portIt == ports.end()) { |
| 886 | LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId; |
| 887 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 888 | } |
| 889 | if (existing != configs.end()) { |
| 890 | *out_suggested = *existing; |
| 891 | } else { |
| 892 | AudioPortConfig newConfig; |
| 893 | if (generateDefaultPortConfig(*portIt, &newConfig)) { |
| 894 | *out_suggested = newConfig; |
| 895 | } else { |
| 896 | LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId; |
| 897 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 898 | } |
| 899 | } |
| 900 | // From this moment, 'out_suggested' is either an existing port config, |
| 901 | // or a new generated config. Now attempt to update it according to the specified |
| 902 | // fields of 'in_requested'. |
| 903 | |
| 904 | bool requestedIsValid = true, requestedIsFullySpecified = true; |
| 905 | |
| 906 | AudioIoFlags portFlags = portIt->flags; |
| 907 | if (in_requested.flags.has_value()) { |
| 908 | if (in_requested.flags.value() != portFlags) { |
| 909 | LOG(WARNING) << __func__ << ": requested flags " |
| 910 | << in_requested.flags.value().toString() << " do not match port's " |
| 911 | << portId << " flags " << portFlags.toString(); |
| 912 | requestedIsValid = false; |
| 913 | } |
| 914 | } else { |
| 915 | requestedIsFullySpecified = false; |
| 916 | } |
| 917 | |
| 918 | AudioProfile portProfile; |
| 919 | if (in_requested.format.has_value()) { |
| 920 | const auto& format = in_requested.format.value(); |
| 921 | if (findAudioProfile(*portIt, format, &portProfile)) { |
| 922 | out_suggested->format = format; |
| 923 | } else { |
| 924 | LOG(WARNING) << __func__ << ": requested format " << format.toString() |
| 925 | << " is not found in port's " << portId << " profiles"; |
| 926 | requestedIsValid = false; |
| 927 | } |
| 928 | } else { |
| 929 | requestedIsFullySpecified = false; |
| 930 | } |
| 931 | if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) { |
| 932 | LOG(ERROR) << __func__ << ": port " << portId << " does not support format " |
| 933 | << out_suggested->format.value().toString() << " anymore"; |
| 934 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 935 | } |
| 936 | |
| 937 | if (in_requested.channelMask.has_value()) { |
| 938 | const auto& channelMask = in_requested.channelMask.value(); |
| 939 | if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) != |
| 940 | portProfile.channelMasks.end()) { |
| 941 | out_suggested->channelMask = channelMask; |
| 942 | } else { |
| 943 | LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString() |
| 944 | << " is not supported for the format " << portProfile.format.toString() |
| 945 | << " by the port " << portId; |
| 946 | requestedIsValid = false; |
| 947 | } |
| 948 | } else { |
| 949 | requestedIsFullySpecified = false; |
| 950 | } |
| 951 | |
| 952 | if (in_requested.sampleRate.has_value()) { |
| 953 | const auto& sampleRate = in_requested.sampleRate.value(); |
| 954 | if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(), |
| 955 | sampleRate.value) != portProfile.sampleRates.end()) { |
| 956 | out_suggested->sampleRate = sampleRate; |
| 957 | } else { |
| 958 | LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value |
| 959 | << " is not supported for the format " << portProfile.format.toString() |
| 960 | << " by the port " << portId; |
| 961 | requestedIsValid = false; |
| 962 | } |
| 963 | } else { |
| 964 | requestedIsFullySpecified = false; |
| 965 | } |
| 966 | |
| 967 | if (in_requested.gain.has_value()) { |
| 968 | // Let's pretend that gain can always be applied. |
| 969 | out_suggested->gain = in_requested.gain.value(); |
| 970 | } |
| 971 | |
Mikhail Naganov | 248e950 | 2023-02-21 16:32:40 -0800 | [diff] [blame] | 972 | if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) { |
| 973 | if (in_requested.ext.getTag() == out_suggested->ext.getTag()) { |
| 974 | if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) { |
| 975 | // 'AudioMixPortExt.handle' is set by the client, copy from in_requested |
| 976 | out_suggested->ext.get<AudioPortExt::Tag::mix>().handle = |
| 977 | in_requested.ext.get<AudioPortExt::Tag::mix>().handle; |
| 978 | } |
| 979 | } else { |
| 980 | LOG(WARNING) << __func__ << ": requested ext tag " |
| 981 | << toString(in_requested.ext.getTag()) << " do not match port's tag " |
| 982 | << toString(out_suggested->ext.getTag()); |
| 983 | requestedIsValid = false; |
| 984 | } |
| 985 | } |
| 986 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 987 | if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) { |
| 988 | out_suggested->id = getConfig().nextPortId++; |
| 989 | configs.push_back(*out_suggested); |
| 990 | *_aidl_return = true; |
| 991 | LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString(); |
| 992 | } else if (existing != configs.end() && requestedIsValid) { |
| 993 | *existing = *out_suggested; |
| 994 | *_aidl_return = true; |
| 995 | LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString(); |
| 996 | } else { |
| 997 | LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end()) |
| 998 | << "; requested is valid? " << requestedIsValid << ", fully specified? " |
| 999 | << requestedIsFullySpecified; |
| 1000 | *_aidl_return = false; |
| 1001 | } |
| 1002 | return ndk::ScopedAStatus::ok(); |
| 1003 | } |
| 1004 | |
| 1005 | ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) { |
| 1006 | auto& patches = getConfig().patches; |
| 1007 | auto patchIt = findById<AudioPatch>(patches, in_patchId); |
| 1008 | if (patchIt != patches.end()) { |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 1009 | auto patchesBackup = mPatches; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1010 | cleanUpPatch(patchIt->id); |
Mikhail Naganov | 75b59df | 2023-06-23 13:39:40 -0700 | [diff] [blame] | 1011 | if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) { |
| 1012 | mPatches = std::move(patchesBackup); |
| 1013 | return status; |
| 1014 | } |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1015 | patches.erase(patchIt); |
| 1016 | LOG(DEBUG) << __func__ << ": erased patch " << in_patchId; |
| 1017 | return ndk::ScopedAStatus::ok(); |
| 1018 | } |
| 1019 | LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found"; |
| 1020 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1021 | } |
| 1022 | |
| 1023 | ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) { |
| 1024 | auto& configs = getConfig().portConfigs; |
| 1025 | auto configIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 1026 | if (configIt != configs.end()) { |
| 1027 | if (mStreams.count(in_portConfigId) != 0) { |
| 1028 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 1029 | << " has a stream opened on it"; |
| 1030 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 1031 | } |
| 1032 | auto patchIt = mPatches.find(in_portConfigId); |
| 1033 | if (patchIt != mPatches.end()) { |
| 1034 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 1035 | << " is used by the patch with id " << patchIt->second; |
| 1036 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 1037 | } |
| 1038 | auto& initials = getConfig().initialConfigs; |
| 1039 | auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId); |
| 1040 | if (initialIt == initials.end()) { |
| 1041 | configs.erase(configIt); |
| 1042 | LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId; |
| 1043 | } else if (*configIt != *initialIt) { |
| 1044 | *configIt = *initialIt; |
| 1045 | LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId; |
| 1046 | } |
| 1047 | return ndk::ScopedAStatus::ok(); |
| 1048 | } |
| 1049 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found"; |
| 1050 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1051 | } |
| 1052 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1053 | ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) { |
| 1054 | *_aidl_return = mMasterMute; |
| 1055 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1056 | return ndk::ScopedAStatus::ok(); |
| 1057 | } |
| 1058 | |
| 1059 | ndk::ScopedAStatus Module::setMasterMute(bool in_mute) { |
| 1060 | LOG(DEBUG) << __func__ << ": " << in_mute; |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 1061 | auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok() |
| 1062 | : onMasterMuteChanged(in_mute); |
| 1063 | if (result.isOk()) { |
| 1064 | mMasterMute = in_mute; |
| 1065 | } else { |
| 1066 | LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute |
| 1067 | << "), error=" << result; |
| 1068 | // Reset master mute if it failed. |
| 1069 | onMasterMuteChanged(mMasterMute); |
| 1070 | } |
| 1071 | return std::move(result); |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) { |
| 1075 | *_aidl_return = mMasterVolume; |
| 1076 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1077 | return ndk::ScopedAStatus::ok(); |
| 1078 | } |
| 1079 | |
| 1080 | ndk::ScopedAStatus Module::setMasterVolume(float in_volume) { |
| 1081 | LOG(DEBUG) << __func__ << ": " << in_volume; |
| 1082 | if (in_volume >= 0.0f && in_volume <= 1.0f) { |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 1083 | auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok() |
| 1084 | : onMasterVolumeChanged(in_volume); |
| 1085 | if (result.isOk()) { |
| 1086 | mMasterVolume = in_volume; |
| 1087 | } else { |
| 1088 | // Reset master volume if it failed. |
| 1089 | LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume |
| 1090 | << "), error=" << result; |
| 1091 | onMasterVolumeChanged(mMasterVolume); |
| 1092 | } |
| 1093 | return std::move(result); |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1094 | } |
| 1095 | LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume; |
| 1096 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1097 | } |
| 1098 | |
| 1099 | ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) { |
| 1100 | *_aidl_return = mMicMute; |
| 1101 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1102 | return ndk::ScopedAStatus::ok(); |
| 1103 | } |
| 1104 | |
| 1105 | ndk::ScopedAStatus Module::setMicMute(bool in_mute) { |
| 1106 | LOG(DEBUG) << __func__ << ": " << in_mute; |
| 1107 | mMicMute = in_mute; |
| 1108 | return ndk::ScopedAStatus::ok(); |
| 1109 | } |
| 1110 | |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 1111 | ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) { |
Pawan Wagh | 6f57cd9 | 2023-02-01 21:14:34 +0000 | [diff] [blame] | 1112 | *_aidl_return = getConfig().microphones; |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 1113 | LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return); |
| 1114 | return ndk::ScopedAStatus::ok(); |
| 1115 | } |
| 1116 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1117 | ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) { |
Mikhail Naganov | 04ae822 | 2023-01-11 15:48:10 -0800 | [diff] [blame] | 1118 | if (!isValidAudioMode(in_mode)) { |
| 1119 | LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode); |
| 1120 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1121 | } |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1122 | // No checks for supported audio modes here, it's an informative notification. |
| 1123 | LOG(DEBUG) << __func__ << ": " << toString(in_mode); |
| 1124 | return ndk::ScopedAStatus::ok(); |
| 1125 | } |
| 1126 | |
| 1127 | ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) { |
| 1128 | LOG(DEBUG) << __func__ << ": " << toString(in_rotation); |
| 1129 | return ndk::ScopedAStatus::ok(); |
| 1130 | } |
| 1131 | |
| 1132 | ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) { |
| 1133 | LOG(DEBUG) << __func__ << ": " << in_isTurnedOn; |
| 1134 | return ndk::ScopedAStatus::ok(); |
| 1135 | } |
| 1136 | |
Vlad Popa | 83a6d82 | 2022-11-07 13:53:57 +0100 | [diff] [blame] | 1137 | ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) { |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 1138 | if (!mSoundDose) { |
Vlad Popa | 2afbd1e | 2022-12-28 17:04:58 +0100 | [diff] [blame] | 1139 | mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>(); |
Vlad Popa | 943b7e2 | 2022-12-08 14:24:12 +0100 | [diff] [blame] | 1140 | } |
Mikhail Naganov | 780fefb | 2023-07-21 17:01:38 -0700 | [diff] [blame] | 1141 | *_aidl_return = mSoundDose.getInstance(); |
Vlad Popa | 943b7e2 | 2022-12-08 14:24:12 +0100 | [diff] [blame] | 1142 | LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get(); |
Vlad Popa | 83a6d82 | 2022-11-07 13:53:57 +0100 | [diff] [blame] | 1143 | return ndk::ScopedAStatus::ok(); |
| 1144 | } |
| 1145 | |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1146 | ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) { |
| 1147 | LOG(DEBUG) << __func__; |
| 1148 | (void)_aidl_return; |
| 1149 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1150 | } |
| 1151 | |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1152 | const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst"; |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1153 | const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain"; |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1154 | |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1155 | ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids, |
| 1156 | std::vector<VendorParameter>* _aidl_return) { |
| 1157 | LOG(DEBUG) << __func__ << ": id count: " << in_ids.size(); |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1158 | bool allParametersKnown = true; |
| 1159 | for (const auto& id : in_ids) { |
| 1160 | if (id == VendorDebug::kForceTransientBurstName) { |
| 1161 | VendorParameter forceTransientBurst{.id = id}; |
| 1162 | forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst}); |
| 1163 | _aidl_return->push_back(std::move(forceTransientBurst)); |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1164 | } else if (id == VendorDebug::kForceSynchronousDrainName) { |
| 1165 | VendorParameter forceSynchronousDrain{.id = id}; |
| 1166 | forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain}); |
| 1167 | _aidl_return->push_back(std::move(forceSynchronousDrain)); |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1168 | } else { |
| 1169 | allParametersKnown = false; |
| 1170 | LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\""; |
| 1171 | } |
| 1172 | } |
| 1173 | if (allParametersKnown) return ndk::ScopedAStatus::ok(); |
| 1174 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1177 | namespace { |
| 1178 | |
| 1179 | template <typename W> |
| 1180 | bool extractParameter(const VendorParameter& p, decltype(W::value)* v) { |
| 1181 | std::optional<W> value; |
| 1182 | binder_status_t result = p.ext.getParcelable(&value); |
| 1183 | if (result == STATUS_OK && value.has_value()) { |
| 1184 | *v = value.value().value; |
| 1185 | return true; |
| 1186 | } |
| 1187 | LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id |
| 1188 | << "\": " << result; |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
| 1192 | } // namespace |
| 1193 | |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1194 | ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters, |
| 1195 | bool in_async) { |
| 1196 | LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size() |
| 1197 | << ", async: " << in_async; |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1198 | bool allParametersKnown = true; |
| 1199 | for (const auto& p : in_parameters) { |
| 1200 | if (p.id == VendorDebug::kForceTransientBurstName) { |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1201 | if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) { |
| 1202 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1203 | } |
| 1204 | } else if (p.id == VendorDebug::kForceSynchronousDrainName) { |
| 1205 | if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) { |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1206 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1207 | } |
| 1208 | } else { |
| 1209 | allParametersKnown = false; |
| 1210 | LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\""; |
| 1211 | } |
| 1212 | } |
| 1213 | if (allParametersKnown) return ndk::ScopedAStatus::ok(); |
| 1214 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Mikhail Naganov | fb1acde | 2022-12-12 18:57:36 +0000 | [diff] [blame] | 1217 | ndk::ScopedAStatus Module::addDeviceEffect( |
| 1218 | int32_t in_portConfigId, |
| 1219 | const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) { |
| 1220 | if (in_effect == nullptr) { |
| 1221 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect"; |
| 1222 | } else { |
| 1223 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder " |
| 1224 | << in_effect->asBinder().get(); |
| 1225 | } |
| 1226 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1227 | } |
| 1228 | |
| 1229 | ndk::ScopedAStatus Module::removeDeviceEffect( |
| 1230 | int32_t in_portConfigId, |
| 1231 | const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) { |
| 1232 | if (in_effect == nullptr) { |
| 1233 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect"; |
| 1234 | } else { |
| 1235 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder " |
| 1236 | << in_effect->asBinder().get(); |
| 1237 | } |
| 1238 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1239 | } |
| 1240 | |
jiabin | 9a8e686 | 2023-01-12 23:06:37 +0000 | [diff] [blame] | 1241 | ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType, |
| 1242 | std::vector<AudioMMapPolicyInfo>* _aidl_return) { |
| 1243 | LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType); |
| 1244 | std::set<int32_t> mmapSinks; |
| 1245 | std::set<int32_t> mmapSources; |
| 1246 | auto& ports = getConfig().ports; |
| 1247 | for (const auto& port : ports) { |
| 1248 | if (port.flags.getTag() == AudioIoFlags::Tag::input && |
| 1249 | isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(), |
| 1250 | AudioInputFlags::MMAP_NOIRQ)) { |
| 1251 | mmapSinks.insert(port.id); |
| 1252 | } else if (port.flags.getTag() == AudioIoFlags::Tag::output && |
| 1253 | isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(), |
| 1254 | AudioOutputFlags::MMAP_NOIRQ)) { |
| 1255 | mmapSources.insert(port.id); |
| 1256 | } |
| 1257 | } |
| 1258 | for (const auto& route : getConfig().routes) { |
| 1259 | if (mmapSinks.count(route.sinkPortId) != 0) { |
| 1260 | // The sink is a mix port, add the sources if they are device ports. |
| 1261 | for (int sourcePortId : route.sourcePortIds) { |
| 1262 | auto sourcePortIt = findById<AudioPort>(ports, sourcePortId); |
| 1263 | if (sourcePortIt == ports.end()) { |
| 1264 | // This must not happen |
| 1265 | LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found"; |
| 1266 | continue; |
| 1267 | } |
| 1268 | if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 1269 | // The source is not a device port, skip |
| 1270 | continue; |
| 1271 | } |
| 1272 | AudioMMapPolicyInfo policyInfo; |
| 1273 | policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device; |
| 1274 | // Always return AudioMMapPolicy.AUTO if the device supports mmap for |
| 1275 | // default implementation. |
| 1276 | policyInfo.mmapPolicy = AudioMMapPolicy::AUTO; |
| 1277 | _aidl_return->push_back(policyInfo); |
| 1278 | } |
| 1279 | } else { |
| 1280 | auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId); |
| 1281 | if (sinkPortIt == ports.end()) { |
| 1282 | // This must not happen |
| 1283 | LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found"; |
| 1284 | continue; |
| 1285 | } |
| 1286 | if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 1287 | // The sink is not a device port, skip |
| 1288 | continue; |
| 1289 | } |
| 1290 | if (count_any(mmapSources, route.sourcePortIds)) { |
| 1291 | AudioMMapPolicyInfo policyInfo; |
| 1292 | policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device; |
| 1293 | // Always return AudioMMapPolicy.AUTO if the device supports mmap for |
| 1294 | // default implementation. |
| 1295 | policyInfo.mmapPolicy = AudioMMapPolicy::AUTO; |
| 1296 | _aidl_return->push_back(policyInfo); |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | return ndk::ScopedAStatus::ok(); |
| 1301 | } |
| 1302 | |
Eric Laurent | e2432ea | 2023-01-12 17:47:31 +0100 | [diff] [blame] | 1303 | ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) { |
| 1304 | LOG(DEBUG) << __func__; |
| 1305 | *_aidl_return = false; |
| 1306 | return ndk::ScopedAStatus::ok(); |
| 1307 | } |
| 1308 | |
jiabin | b76981e | 2023-01-18 00:58:30 +0000 | [diff] [blame] | 1309 | ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) { |
| 1310 | if (!isMmapSupported()) { |
| 1311 | LOG(DEBUG) << __func__ << ": mmap is not supported "; |
| 1312 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1313 | } |
| 1314 | *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT; |
| 1315 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1316 | return ndk::ScopedAStatus::ok(); |
| 1317 | } |
| 1318 | |
| 1319 | ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) { |
| 1320 | if (!isMmapSupported()) { |
| 1321 | LOG(DEBUG) << __func__ << ": mmap is not supported "; |
| 1322 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1323 | } |
| 1324 | *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US; |
| 1325 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1326 | return ndk::ScopedAStatus::ok(); |
| 1327 | } |
| 1328 | |
| 1329 | bool Module::isMmapSupported() { |
| 1330 | if (mIsMmapSupported.has_value()) { |
| 1331 | return mIsMmapSupported.value(); |
| 1332 | } |
| 1333 | std::vector<AudioMMapPolicyInfo> mmapPolicyInfos; |
| 1334 | if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) { |
| 1335 | mIsMmapSupported = false; |
| 1336 | } else { |
| 1337 | mIsMmapSupported = |
| 1338 | std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) { |
| 1339 | return info.mmapPolicy == AudioMMapPolicy::AUTO || |
| 1340 | info.mmapPolicy == AudioMMapPolicy::ALWAYS; |
| 1341 | }) != mmapPolicyInfos.end(); |
| 1342 | } |
| 1343 | return mIsMmapSupported.value(); |
| 1344 | } |
| 1345 | |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 1346 | ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) { |
jiabin | 116d839 | 2023-03-01 22:52:57 +0000 | [diff] [blame] | 1347 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 1348 | return ndk::ScopedAStatus::ok(); |
| 1349 | } |
| 1350 | |
| 1351 | ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch( |
| 1352 | const std::vector<AudioPortConfig*>& sources __unused, |
| 1353 | const std::vector<AudioPortConfig*>& sinks __unused) { |
jiabin | 116d839 | 2023-03-01 22:52:57 +0000 | [diff] [blame] | 1354 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 1355 | return ndk::ScopedAStatus::ok(); |
| 1356 | } |
| 1357 | |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 1358 | void Module::onExternalDeviceConnectionChanged( |
| 1359 | const ::aidl::android::media::audio::common::AudioPort& audioPort __unused, |
| 1360 | bool connected __unused) { |
| 1361 | LOG(DEBUG) << __func__ << ": do nothing and return"; |
| 1362 | } |
| 1363 | |
| 1364 | ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) { |
| 1365 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
| 1366 | return ndk::ScopedAStatus::ok(); |
| 1367 | } |
| 1368 | |
| 1369 | ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) { |
| 1370 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
| 1371 | return ndk::ScopedAStatus::ok(); |
| 1372 | } |
| 1373 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1374 | } // namespace aidl::android::hardware::audio::core |