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