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