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