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