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