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