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