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