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