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