blob: 45ce5ef1d12965aa2f8b25b52be4884f3a6a6d7e [file] [log] [blame]
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001/*
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 Naganov4f5d3f12022-07-22 23:23:25 +000021#include <aidl/android/media/audio/common/AudioInputFlags.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000022#include <aidl/android/media/audio/common/AudioOutputFlags.h>
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -070023#include <android-base/logging.h>
24#include <android/binder_ibinder_platform.h>
25#include <error/expected_utils.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000026
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000027#include "core-impl/Configuration.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000028#include "core-impl/Module.h"
Mikhail Naganovb03b5c42023-07-26 13:13:35 -070029#include "core-impl/ModuleBluetooth.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070030#include "core-impl/ModulePrimary.h"
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053031#include "core-impl/ModuleRemoteSubmix.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070032#include "core-impl/ModuleStub.h"
jiabin253bd322023-01-25 23:57:31 +000033#include "core-impl/ModuleUsb.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010034#include "core-impl/SoundDose.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000035#include "core-impl/utils.h"
36
Mikhail Naganov55045b52023-10-24 17:03:50 -070037using aidl::android::hardware::audio::common::frameCountFromDurationMs;
Mikhail Naganov872d4a62023-03-09 18:19:01 -080038using aidl::android::hardware::audio::common::getFrameSizeInBytes;
Kuowei Li53a8d4d2024-06-24 14:35:07 +080039using aidl::android::hardware::audio::common::hasMmapFlag;
Mikhail Naganov872d4a62023-03-09 18:19:01 -080040using aidl::android::hardware::audio::common::isBitPositionFlagSet;
41using aidl::android::hardware::audio::common::isValidAudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000042using aidl::android::hardware::audio::common::SinkMetadata;
43using aidl::android::hardware::audio::common::SourceMetadata;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010044using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000045using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000046using aidl::android::media::audio::common::AudioDevice;
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +000047using aidl::android::media::audio::common::AudioDeviceType;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000048using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000049using aidl::android::media::audio::common::AudioFormatType;
Weilin Xua33bb5e2024-10-02 17:16:42 +000050using aidl::android::media::audio::common::AudioGainConfig;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000051using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000052using aidl::android::media::audio::common::AudioIoFlags;
jiabin9a8e6862023-01-12 23:06:37 +000053using aidl::android::media::audio::common::AudioMMapPolicy;
54using aidl::android::media::audio::common::AudioMMapPolicyInfo;
55using aidl::android::media::audio::common::AudioMMapPolicyType;
Mikhail Naganov04ae8222023-01-11 15:48:10 -080056using aidl::android::media::audio::common::AudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000057using aidl::android::media::audio::common::AudioOffloadInfo;
58using aidl::android::media::audio::common::AudioOutputFlags;
59using aidl::android::media::audio::common::AudioPort;
60using aidl::android::media::audio::common::AudioPortConfig;
61using aidl::android::media::audio::common::AudioPortExt;
62using aidl::android::media::audio::common::AudioProfile;
Mikhail Naganov20047bc2023-01-05 20:16:07 +000063using aidl::android::media::audio::common::Boolean;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000064using aidl::android::media::audio::common::Int;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080065using aidl::android::media::audio::common::MicrophoneInfo;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000066using aidl::android::media::audio::common::PcmType;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000067
68namespace aidl::android::hardware::audio::core {
69
70namespace {
71
Mikhail Naganov84bcc042023-10-05 17:36:57 -070072inline bool hasDynamicChannelMasks(const std::vector<AudioChannelLayout>& channelMasks) {
73 return channelMasks.empty() ||
74 std::all_of(channelMasks.begin(), channelMasks.end(),
75 [](const auto& channelMask) { return channelMask == AudioChannelLayout{}; });
76}
77
78inline bool hasDynamicFormat(const AudioFormatDescription& format) {
79 return format == AudioFormatDescription{};
80}
81
82inline bool hasDynamicSampleRates(const std::vector<int32_t>& sampleRates) {
83 return sampleRates.empty() ||
84 std::all_of(sampleRates.begin(), sampleRates.end(),
85 [](const auto& sampleRate) { return sampleRate == 0; });
86}
87
88inline bool isDynamicProfile(const AudioProfile& profile) {
89 return hasDynamicFormat(profile.format) || hasDynamicChannelMasks(profile.channelMasks) ||
90 hasDynamicSampleRates(profile.sampleRates);
91}
92
93bool hasDynamicProfilesOnly(const std::vector<AudioProfile>& profiles) {
94 if (profiles.empty()) return true;
95 return std::all_of(profiles.begin(), profiles.end(), isDynamicProfile);
96}
97
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000098bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
99 AudioProfile* profile) {
100 if (auto profilesIt =
101 find_if(port.profiles.begin(), port.profiles.end(),
102 [&format](const auto& profile) { return profile.format == format; });
103 profilesIt != port.profiles.end()) {
104 *profile = *profilesIt;
105 return true;
106 }
107 return false;
108}
Mikhail Naganov00603d12022-05-02 22:52:13 +0000109
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000110} // namespace
111
jiabin253bd322023-01-25 23:57:31 +0000112// static
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000113std::shared_ptr<Module> Module::createInstance(Type type, std::unique_ptr<Configuration>&& config) {
jiabin253bd322023-01-25 23:57:31 +0000114 switch (type) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530115 case Type::DEFAULT:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000116 return ndk::SharedRefBase::make<ModulePrimary>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700117 case Type::R_SUBMIX:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000118 return ndk::SharedRefBase::make<ModuleRemoteSubmix>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700119 case Type::STUB:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000120 return ndk::SharedRefBase::make<ModuleStub>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700121 case Type::USB:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000122 return ndk::SharedRefBase::make<ModuleUsb>(std::move(config));
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700123 case Type::BLUETOOTH:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000124 return ndk::SharedRefBase::make<ModuleBluetooth>(std::move(config));
jiabin253bd322023-01-25 23:57:31 +0000125 }
126}
127
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000128// static
129std::optional<Module::Type> Module::typeFromString(const std::string& type) {
130 if (type == "default")
131 return Module::Type::DEFAULT;
132 else if (type == "r_submix")
133 return Module::Type::R_SUBMIX;
134 else if (type == "stub")
135 return Module::Type::STUB;
136 else if (type == "usb")
137 return Module::Type::USB;
138 else if (type == "bluetooth")
139 return Module::Type::BLUETOOTH;
140 return {};
141}
142
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700143std::ostream& operator<<(std::ostream& os, Module::Type t) {
144 switch (t) {
145 case Module::Type::DEFAULT:
146 os << "default";
147 break;
148 case Module::Type::R_SUBMIX:
149 os << "r_submix";
150 break;
Mikhail Naganov521fc492023-07-11 17:24:08 -0700151 case Module::Type::STUB:
152 os << "stub";
153 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700154 case Module::Type::USB:
155 os << "usb";
156 break;
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700157 case Module::Type::BLUETOOTH:
158 os << "bluetooth";
159 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700160 }
161 return os;
162}
163
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000164Module::Module(Type type, std::unique_ptr<Configuration>&& config)
165 : mType(type), mConfig(std::move(config)) {
166 populateConnectedProfiles();
167}
168
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000169void Module::cleanUpPatch(int32_t patchId) {
170 erase_all_values(mPatches, std::set<int32_t>{patchId});
171}
172
Mikhail Naganov8651b362023-01-06 23:15:27 +0000173ndk::ScopedAStatus Module::createStreamContext(
174 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
175 std::shared_ptr<IStreamCallback> asyncCallback,
176 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000177 if (in_bufferSizeFrames <= 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530178 LOG(ERROR) << __func__ << ": " << mType << ": non-positive buffer size "
179 << in_bufferSizeFrames;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000180 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
181 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000182 auto& configs = getConfig().portConfigs;
183 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganova92039a2023-12-20 14:27:22 -0800184 const int32_t nominalLatencyMs = getNominalLatencyMs(*portConfigIt);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000185 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000186 // validity of the portConfigId has already been checked.
Mikhail Naganova92039a2023-12-20 14:27:22 -0800187 const int32_t minimumStreamBufferSizeFrames =
188 calculateBufferSizeFrames(nominalLatencyMs, portConfigIt->sampleRate.value().value);
Mikhail Naganov13501872023-10-18 16:15:46 -0700189 if (in_bufferSizeFrames < minimumStreamBufferSizeFrames) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530190 LOG(ERROR) << __func__ << ": " << mType << ": insufficient buffer size "
191 << in_bufferSizeFrames << ", must be at least " << minimumStreamBufferSizeFrames;
Mikhail Naganov13501872023-10-18 16:15:46 -0700192 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
193 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000194 const size_t frameSize =
195 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
196 if (frameSize == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530197 LOG(ERROR) << __func__ << ": " << mType
198 << ": could not calculate frame size for port config "
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000199 << portConfigIt->toString();
200 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
201 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530202 LOG(DEBUG) << __func__ << ": " << mType << ": frame size " << frameSize << " bytes";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700203 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530204 LOG(ERROR) << __func__ << ": " << mType << ": buffer size " << in_bufferSizeFrames
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000205 << " frames is too large, maximum size is "
206 << kMaximumStreamBufferSizeBytes / frameSize;
207 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
208 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000209 const auto& flags = portConfigIt->flags.value();
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800210 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
211 mVendorDebug.forceTransientBurst,
212 mVendorDebug.forceSynchronousDrain};
213 std::unique_ptr<StreamContext::DataMQ> dataMQ = nullptr;
214 std::shared_ptr<IStreamCallback> streamAsyncCallback = nullptr;
215 std::shared_ptr<ISoundDose> soundDose;
216 if (!getSoundDose(&soundDose).isOk()) {
217 LOG(ERROR) << __func__ << ": could not create sound dose instance";
218 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
219 }
220 if (!hasMmapFlag(flags)) {
221 dataMQ = std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames);
222 streamAsyncCallback = asyncCallback;
223 }
224 StreamContext temp(
225 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
226 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
227 portConfigIt->format.value(), portConfigIt->channelMask.value(),
228 portConfigIt->sampleRate.value().value, flags, nominalLatencyMs,
229 portConfigIt->ext.get<AudioPortExt::mix>().handle, std::move(dataMQ),
230 streamAsyncCallback, outEventCallback, mSoundDose.getInstance(), params);
231 if (temp.isValid()) {
232 *out_context = std::move(temp);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000233 } else {
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800234 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000235 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000236 return ndk::ScopedAStatus::ok();
237}
238
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000239std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
240 std::vector<AudioDevice> result;
241 auto& ports = getConfig().ports;
242 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
243 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
244 auto portIt = findById<AudioPort>(ports, *it);
245 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
246 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
247 }
248 }
249 return result;
250}
251
252std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
253 std::set<int32_t> result;
254 auto patchIdsRange = mPatches.equal_range(portConfigId);
255 auto& patches = getConfig().patches;
256 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
257 auto patchIt = findById<AudioPatch>(patches, it->second);
258 if (patchIt == patches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530259 LOG(FATAL) << __func__ << ": " << mType << ": patch with id " << it->second
260 << " taken from mPatches "
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000261 << "not found in the configuration";
262 }
263 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
264 portConfigId) != patchIt->sourcePortConfigIds.end()) {
265 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
266 } else {
267 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
268 }
269 }
270 return result;
271}
272
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000273ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
274 auto& configs = getConfig().portConfigs;
275 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
276 if (portConfigIt == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530277 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id " << in_portConfigId
278 << " not found";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000279 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
280 }
281 const int32_t portId = portConfigIt->portId;
282 // In our implementation, configs of mix ports always have unique IDs.
283 CHECK(portId != in_portConfigId);
284 auto& ports = getConfig().ports;
285 auto portIt = findById<AudioPort>(ports, portId);
286 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530287 LOG(ERROR) << __func__ << ": " << mType << ": port id " << portId
288 << " used by port config id " << in_portConfigId << " not found";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000289 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
290 }
291 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530292 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000293 << " already has a stream opened on it";
294 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
295 }
296 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530297 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000298 << " does not correspond to a mix port";
299 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
300 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700301 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000302 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530303 LOG(ERROR) << __func__ << ": " << mType << ": port id " << portId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000304 << " has already reached maximum allowed opened stream count: "
305 << maxOpenStreamCount;
306 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
307 }
308 *port = &(*portIt);
309 return ndk::ScopedAStatus::ok();
310}
311
Mikhail Naganova92039a2023-12-20 14:27:22 -0800312bool Module::generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
313 const bool allowDynamicConfig = port.ext.getTag() == AudioPortExt::device;
314 for (const auto& profile : port.profiles) {
315 if (isDynamicProfile(profile)) continue;
316 config->format = profile.format;
317 config->channelMask = *profile.channelMasks.begin();
318 config->sampleRate = Int{.value = *profile.sampleRates.begin()};
319 config->flags = port.flags;
320 config->ext = port.ext;
321 return true;
322 }
323 if (allowDynamicConfig) {
324 config->format = AudioFormatDescription{};
325 config->channelMask = AudioChannelLayout{};
326 config->sampleRate = Int{.value = 0};
327 config->flags = port.flags;
328 config->ext = port.ext;
329 return true;
330 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530331 LOG(ERROR) << __func__ << ": " << mType << ": port " << port.id << " only has dynamic profiles";
Mikhail Naganova92039a2023-12-20 14:27:22 -0800332 return false;
333}
334
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000335void Module::populateConnectedProfiles() {
336 Configuration& config = getConfig();
337 for (const AudioPort& port : config.ports) {
338 if (port.ext.getTag() == AudioPortExt::device) {
339 if (auto devicePort = port.ext.get<AudioPortExt::device>();
340 !devicePort.device.type.connection.empty() && port.profiles.empty()) {
341 if (auto connIt = config.connectedProfiles.find(port.id);
342 connIt == config.connectedProfiles.end()) {
343 config.connectedProfiles.emplace(
344 port.id, internal::getStandard16And24BitPcmAudioProfiles());
345 }
346 }
347 }
348 }
349}
350
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000351template <typename C>
352std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
353 std::set<int32_t> result;
354 auto& portConfigs = getConfig().portConfigs;
355 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
356 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
357 if (portConfigIt != portConfigs.end()) {
358 result.insert(portConfigIt->portId);
359 }
360 }
361 return result;
362}
363
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000364std::unique_ptr<Module::Configuration> Module::initializeConfig() {
365 return internal::getConfiguration(getType());
Peter Yoon918a6a52023-07-13 17:04:37 +0900366}
367
Mikhail Naganov13501872023-10-18 16:15:46 -0700368int32_t Module::getNominalLatencyMs(const AudioPortConfig&) {
369 // Arbitrary value. Implementations must override this method to provide their actual latency.
370 static constexpr int32_t kLatencyMs = 5;
371 return kLatencyMs;
372}
373
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800374ndk::ScopedAStatus Module::createMmapBuffer(
375 const ::aidl::android::hardware::audio::core::StreamContext& context __unused,
376 ::aidl::android::hardware::audio::core::StreamDescriptor* desc __unused) {
377 LOG(ERROR) << __func__ << ": " << mType << ": is not implemented";
378 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
379}
380
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700381std::vector<AudioRoute*> Module::getAudioRoutesForAudioPortImpl(int32_t portId) {
382 std::vector<AudioRoute*> result;
383 auto& routes = getConfig().routes;
384 for (auto& r : routes) {
385 const auto& srcs = r.sourcePortIds;
386 if (r.sinkPortId == portId || std::find(srcs.begin(), srcs.end(), portId) != srcs.end()) {
387 result.push_back(&r);
388 }
389 }
390 return result;
391}
392
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000393Module::Configuration& Module::getConfig() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000394 if (!mConfig) {
Yi Konge62f97f2024-08-14 01:52:04 +0800395 mConfig = initializeConfig();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000396 }
397 return *mConfig;
398}
399
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700400std::set<int32_t> Module::getRoutableAudioPortIds(int32_t portId,
401 std::vector<AudioRoute*>* routes) {
402 std::vector<AudioRoute*> routesStorage;
403 if (routes == nullptr) {
404 routesStorage = getAudioRoutesForAudioPortImpl(portId);
405 routes = &routesStorage;
406 }
407 std::set<int32_t> result;
408 for (AudioRoute* r : *routes) {
409 if (r->sinkPortId == portId) {
410 result.insert(r->sourcePortIds.begin(), r->sourcePortIds.end());
411 } else {
412 result.insert(r->sinkPortId);
413 }
414 }
415 return result;
416}
417
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000418void Module::registerPatch(const AudioPatch& patch) {
419 auto& configs = getConfig().portConfigs;
420 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
421 for (auto portConfigId : portConfigIds) {
422 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
423 if (configIt != configs.end()) {
424 mPatches.insert(std::pair{portConfigId, patch.id});
425 if (configIt->portId != portConfigId) {
426 mPatches.insert(std::pair{configIt->portId, patch.id});
427 }
428 }
429 };
430 };
431 do_insert(patch.sourcePortConfigIds);
432 do_insert(patch.sinkPortConfigIds);
433}
434
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700435ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
436 const AudioPatch& newPatch) {
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700437 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700438 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700439 using Connections =
440 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
441 Connections oldConnections, newConnections;
442 auto fillConnectionsHelper = [&](Connections& connections,
443 const std::vector<int32_t>& mixPortCfgIds,
444 const std::vector<int32_t>& devicePortCfgIds) {
445 for (int32_t mixPortCfgId : mixPortCfgIds) {
446 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
447 }
448 };
449 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
450 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
451 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
452 patch.sourcePortConfigIds.end()) {
453 // Sources are mix ports.
454 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
455 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
456 [&](int32_t portConfigId) {
457 return mStreams.count(portConfigId) > 0;
458 }) != patch.sinkPortConfigIds.end()) {
459 // Sources are device ports.
460 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
461 } // Otherwise, there are no streams to notify.
462 };
463 fillConnections(oldConnections, oldPatch);
464 fillConnections(newConnections, newPatch);
465
466 std::for_each(oldConnections.begin(), oldConnections.end(), [&](const auto& connectionPair) {
467 const int32_t mixPortConfigId = connectionPair.first;
468 if (auto it = newConnections.find(mixPortConfigId);
469 it == newConnections.end() || it->second != connectionPair.second) {
470 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, {});
471 status.isOk()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700472 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700473 << mixPortConfigId << " has been disconnected";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700474 } else {
475 // Disconnection is tricky to roll back, just register a failure.
476 maybeFailure = std::move(status);
477 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000478 }
479 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700480 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700481 std::set<int32_t> idsToDisconnectOnFailure;
482 std::for_each(newConnections.begin(), newConnections.end(), [&](const auto& connectionPair) {
483 const int32_t mixPortConfigId = connectionPair.first;
484 if (auto it = oldConnections.find(mixPortConfigId);
485 it == oldConnections.end() || it->second != connectionPair.second) {
486 const auto connectedDevices = findConnectedDevices(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700487 if (connectedDevices.empty()) {
488 // This is important as workers use the vector size to derive the connection status.
489 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
490 "config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700491 << mixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700492 }
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700493 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700494 status.isOk()) {
495 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700496 << mixPortConfigId << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700497 << ::android::internal::ToString(connectedDevices);
498 } else {
499 maybeFailure = std::move(status);
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700500 idsToDisconnectOnFailure.insert(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700501 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000502 }
503 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700504 if (!maybeFailure.isOk()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530505 LOG(WARNING) << __func__ << ": " << mType
506 << ": Due to a failure, disconnecting streams on port config ids "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700507 << ::android::internal::ToString(idsToDisconnectOnFailure);
508 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
509 [&](const auto& portConfigId) {
510 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
511 (void)status.isOk(); // Can't do much about a failure here.
512 });
513 return maybeFailure;
514 }
515 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000516}
517
Mikhail Naganov00603d12022-05-02 22:52:13 +0000518ndk::ScopedAStatus Module::setModuleDebug(
519 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700520 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000521 << ", new flags: " << in_debug.toString();
522 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
523 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700524 LOG(ERROR) << __func__ << ": " << mType
Jaideep Sharma559a4912024-03-07 10:05:51 +0530525 << ": attempting to change device connections simulation while "
526 "having external "
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700527 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000528 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
529 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000530 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700531 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000532 << in_debug.streamTransientStateDelayMs;
533 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
534 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000535 mDebug = in_debug;
536 return ndk::ScopedAStatus::ok();
537}
538
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000539ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700540 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530541 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000542 return ndk::ScopedAStatus::ok();
543}
544
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000545ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700546 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530547 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000548 return ndk::ScopedAStatus::ok();
549}
550
Mikhail Naganov7499a002023-02-27 18:51:44 -0800551ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700552 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530553 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800554 return ndk::ScopedAStatus::ok();
555}
556
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800557ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700558 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530559 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800560 return ndk::ScopedAStatus::ok();
561}
562
Mikhail Naganov00603d12022-05-02 22:52:13 +0000563ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
564 AudioPort* _aidl_return) {
565 const int32_t templateId = in_templateIdAndAdditionalData.id;
566 auto& ports = getConfig().ports;
567 AudioPort connectedPort;
568 { // Scope the template port so that we don't accidentally modify it.
569 auto templateIt = findById<AudioPort>(ports, templateId);
570 if (templateIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530571 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000572 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
573 }
574 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530575 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
576 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000577 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
578 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000579 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
580 if (templateDevicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530581 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
582 << " is permanently attached";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000583 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
584 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700585 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530586 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
587 << " is a connected device port";
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700588 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
589 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000590 // Postpone id allocation until we ensure that there are no client errors.
591 connectedPort = *templateIt;
592 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
593 const auto& inputDevicePort =
594 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
595 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
596 connectedDevicePort.device.address = inputDevicePort.device.address;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530597 LOG(DEBUG) << __func__ << ": " << mType << ": device port " << connectedPort.id
598 << " device set to " << connectedDevicePort.device.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000599 // Check if there is already a connected port with for the same external device.
Jaideep Sharma559a4912024-03-07 10:05:51 +0530600
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700601 for (auto connectedPortPair : mConnectedDevicePorts) {
602 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000603 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
604 connectedDevicePort.device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530605 LOG(ERROR) << __func__ << ": " << mType << ": device "
606 << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700607 << " is already connected at the device port id "
608 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000609 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
610 }
611 }
612 }
613
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700614 // Two main cases are considered with regard to the profiles of the connected device port:
615 //
616 // 1. If the template device port has dynamic profiles, and at least one routable mix
617 // port also has dynamic profiles, it means that after connecting the device, the
618 // connected device port must have profiles populated with actual capabilities of
619 // the connected device, and dynamic of routable mix ports will be filled
620 // according to these capabilities. An example of this case is connection of an
621 // HDMI or USB device. For USB handled by ADSP, there can be mix ports with static
622 // profiles, and one dedicated mix port for "hi-fi" playback. The latter is left with
623 // dynamic profiles so that they can be populated with actual capabilities of
624 // the connected device.
625 //
626 // 2. If the template device port has dynamic profiles, while all routable mix ports
627 // have static profiles, it means that after connecting the device, the connected
628 // device port can be left with dynamic profiles, and profiles of mix ports are
629 // left untouched. An example of this case is connection of an analog wired
630 // headset, it should be treated in the same way as a speaker.
631 //
632 // Yet another possible case is when both the template device port and all routable
633 // mix ports have static profiles. This is allowed and handled correctly, however, it
634 // is not very practical, since these profiles are likely duplicates of each other.
635
636 std::vector<AudioRoute*> routesToMixPorts = getAudioRoutesForAudioPortImpl(templateId);
637 std::set<int32_t> routableMixPortIds = getRoutableAudioPortIds(templateId, &routesToMixPorts);
Mikhail Naganova92039a2023-12-20 14:27:22 -0800638 const int32_t nextPortId = getConfig().nextPortId++;
Mikhail Naganov55045b52023-10-24 17:03:50 -0700639 if (!mDebug.simulateDeviceConnections) {
640 // Even if the device port has static profiles, the HAL module might need to update
641 // them, or abort the connection process.
Mikhail Naganova92039a2023-12-20 14:27:22 -0800642 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort, nextPortId));
Mikhail Naganov55045b52023-10-24 17:03:50 -0700643 } else if (hasDynamicProfilesOnly(connectedPort.profiles)) {
644 auto& connectedProfiles = getConfig().connectedProfiles;
645 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
646 connectedProfilesIt != connectedProfiles.end()) {
647 connectedPort.profiles = connectedProfilesIt->second;
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700648 }
Mikhail Naganov55045b52023-10-24 17:03:50 -0700649 }
650 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
651 // Possible case 2. Check if all routable mix ports have static profiles.
652 if (auto dynamicMixPortIt = std::find_if(ports.begin(), ports.end(),
653 [&routableMixPortIds](const auto& p) {
654 return routableMixPortIds.count(p.id) > 0 &&
655 hasDynamicProfilesOnly(p.profiles);
656 });
657 dynamicMixPortIt != ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530658 LOG(ERROR) << __func__ << ": " << mType
659 << ": connected port only has dynamic profiles after connecting "
Mikhail Naganov55045b52023-10-24 17:03:50 -0700660 << "external device " << connectedPort.toString() << ", and there exist "
661 << "a routable mix port with dynamic profiles: "
662 << dynamicMixPortIt->toString();
663 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530664 }
665 }
666
Mikhail Naganova92039a2023-12-20 14:27:22 -0800667 connectedPort.id = nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700668 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700669 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Jaideep Sharma559a4912024-03-07 10:05:51 +0530670 LOG(DEBUG) << __func__ << ": " << mType << ": template port " << templateId
671 << " external device connected, "
Mikhail Naganov00603d12022-05-02 22:52:13 +0000672 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000673 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000674 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000675
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700676 // For routes where the template port is a source, add the connected port to sources,
677 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000678 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700679 for (AudioRoute* r : routesToMixPorts) {
680 if (r->sinkPortId == templateId) {
681 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
682 .sinkPortId = connectedPort.id,
683 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000684 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700685 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000686 }
687 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700688 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000689 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
690
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700691 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
692 // Note: this is a simplistic approach assuming that a mix port can only be populated
693 // from a single device port. Implementing support for stuffing dynamic profiles with
694 // a superset of all profiles from all routable dynamic device ports would be more involved.
695 for (auto& port : ports) {
696 if (routableMixPortIds.count(port.id) == 0) continue;
697 if (hasDynamicProfilesOnly(port.profiles)) {
698 port.profiles = connectedPort.profiles;
699 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700700 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700701 // Check if profiles are not all dynamic because they were populated by
702 // a previous connection. Otherwise, it means that they are actually static.
703 for (const auto& cp : mConnectedDevicePorts) {
704 if (cp.second.count(port.id) > 0) {
705 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700706 break;
707 }
708 }
709 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700710 }
711 }
712 *_aidl_return = std::move(connectedPort);
713
Mikhail Naganov00603d12022-05-02 22:52:13 +0000714 return ndk::ScopedAStatus::ok();
715}
716
717ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
718 auto& ports = getConfig().ports;
719 auto portIt = findById<AudioPort>(ports, in_portId);
720 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530721 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000722 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
723 }
724 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530725 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
726 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000727 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
728 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700729 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
730 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530731 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
732 << " is not a connected device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000733 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
734 }
735 auto& configs = getConfig().portConfigs;
736 auto& initials = getConfig().initialConfigs;
737 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
738 if (config.portId == in_portId) {
739 // Check if the configuration was provided by the client.
740 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
741 return initialIt == initials.end() || config != *initialIt;
742 }
743 return false;
744 });
745 if (configIt != configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530746 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
747 << " has a non-default config with id " << configIt->id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000748 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
749 }
jiabin783c48b2023-02-28 18:28:06 +0000750 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000751 ports.erase(portIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +0530752 LOG(DEBUG) << __func__ << ": " << mType << ": connected device port " << in_portId
753 << " released";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000754
755 auto& routes = getConfig().routes;
756 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
757 if (routesIt->sinkPortId == in_portId) {
758 routesIt = routes.erase(routesIt);
759 } else {
760 // Note: the list of sourcePortIds can't become empty because there must
761 // be the id of the template port in the route.
762 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
763 ++routesIt;
764 }
765 }
766
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700767 // Clear profiles for mix ports that are not connected to any other ports.
768 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
769 mConnectedDevicePorts.erase(connectedPortsIt);
770 for (const auto& connectedPort : mConnectedDevicePorts) {
771 for (int32_t mixPortId : connectedPort.second) {
772 mixPortsToClear.erase(mixPortId);
773 }
774 }
775 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700776 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
777 if (mixPortIt != ports.end()) {
778 mixPortIt->profiles = {};
779 }
780 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700781
Mikhail Naganov00603d12022-05-02 22:52:13 +0000782 return ndk::ScopedAStatus::ok();
783}
784
jiabindd23b0e2023-12-11 19:10:05 +0000785ndk::ScopedAStatus Module::prepareToDisconnectExternalDevice(int32_t in_portId) {
786 auto& ports = getConfig().ports;
787 auto portIt = findById<AudioPort>(ports, in_portId);
788 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530789 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
jiabindd23b0e2023-12-11 19:10:05 +0000790 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
791 }
792 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530793 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
794 << " is not a device port";
jiabindd23b0e2023-12-11 19:10:05 +0000795 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
796 }
797 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
798 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530799 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
800 << " is not a connected device port";
jiabindd23b0e2023-12-11 19:10:05 +0000801 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
802 }
803
804 onPrepareToDisconnectExternalDevice(*portIt);
805
806 return ndk::ScopedAStatus::ok();
807}
808
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000809ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
810 *_aidl_return = getConfig().patches;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530811 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " patches";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000812 return ndk::ScopedAStatus::ok();
813}
814
815ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
816 auto& ports = getConfig().ports;
817 auto portIt = findById<AudioPort>(ports, in_portId);
818 if (portIt != ports.end()) {
819 *_aidl_return = *portIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530820 LOG(DEBUG) << __func__ << ": " << mType << ": returning port by id " << in_portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000821 return ndk::ScopedAStatus::ok();
822 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530823 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000824 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
825}
826
827ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
828 *_aidl_return = getConfig().portConfigs;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530829 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size()
830 << " port configs";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000831 return ndk::ScopedAStatus::ok();
832}
833
834ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
835 *_aidl_return = getConfig().ports;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530836 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " ports";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000837 return ndk::ScopedAStatus::ok();
838}
839
840ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
841 *_aidl_return = getConfig().routes;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530842 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " routes";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000843 return ndk::ScopedAStatus::ok();
844}
845
Mikhail Naganov00603d12022-05-02 22:52:13 +0000846ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
847 std::vector<AudioRoute>* _aidl_return) {
848 auto& ports = getConfig().ports;
849 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530850 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000851 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
852 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700853 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
854 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
855 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000856 return ndk::ScopedAStatus::ok();
857}
858
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000859ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
860 OpenInputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530861 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
862 << ", buffer size " << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000863 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700864 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000865 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530866 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000867 << " does not correspond to an input mix port";
868 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
869 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000870 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700871 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
872 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000873 context.fillDescriptor(&_aidl_return->desc);
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800874 if (hasMmapFlag(context.getFlags())) {
875 RETURN_STATUS_IF_ERROR(createMmapBuffer(context, &_aidl_return->desc));
876 }
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000877 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700878 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000879 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000880 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700881 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
882 RETURN_STATUS_IF_ERROR(
883 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
884 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +0000885 auto streamBinder = streamWrapper.getBinder();
886 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
887 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000888 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000889 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000890 return ndk::ScopedAStatus::ok();
891}
892
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000893ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
894 OpenOutputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530895 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
896 << ", has offload info? " << (in_args.offloadInfo.has_value()) << ", buffer size "
897 << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000898 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700899 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000900 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530901 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000902 << " does not correspond to an output mix port";
903 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
904 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000905 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
906 AudioOutputFlags::COMPRESS_OFFLOAD);
907 if (isOffload && !in_args.offloadInfo.has_value()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530908 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000909 << " has COMPRESS_OFFLOAD flag set, requires offload info";
910 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
911 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000912 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
913 AudioOutputFlags::NON_BLOCKING);
914 if (isNonBlocking && in_args.callback == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530915 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov30301a42022-09-13 01:20:45 +0000916 << " has NON_BLOCKING flag set, requires async callback";
917 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
918 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000919 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700920 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
921 isNonBlocking ? in_args.callback : nullptr,
922 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000923 context.fillDescriptor(&_aidl_return->desc);
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800924 if (hasMmapFlag(context.getFlags())) {
925 RETURN_STATUS_IF_ERROR(createMmapBuffer(context, &_aidl_return->desc));
926 }
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000927 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700928 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700929 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000930 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700931 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
932 RETURN_STATUS_IF_ERROR(
933 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
934 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +0000935 auto streamBinder = streamWrapper.getBinder();
936 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
937 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000938 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000939 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000940 return ndk::ScopedAStatus::ok();
941}
942
Mikhail Naganov74927202022-12-19 16:37:14 +0000943ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
944 SupportedPlaybackRateFactors* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530945 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganov74927202022-12-19 16:37:14 +0000946 (void)_aidl_return;
947 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
948}
949
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000950ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530951 LOG(DEBUG) << __func__ << ": " << mType << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000952 if (in_requested.sourcePortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530953 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000954 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
955 }
956 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530957 LOG(ERROR) << __func__ << ": " << mType
958 << ": requested patch has duplicate ids in the sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000959 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
960 }
961 if (in_requested.sinkPortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530962 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000963 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
964 }
965 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530966 LOG(ERROR) << __func__ << ": " << mType
967 << ": requested patch has duplicate ids in the sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000968 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
969 }
970
971 auto& configs = getConfig().portConfigs;
972 std::vector<int32_t> missingIds;
973 auto sources =
974 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
975 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530976 LOG(ERROR) << __func__ << ": " << mType << ": following source port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000977 << ::android::internal::ToString(missingIds);
978 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
979 }
980 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
981 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530982 LOG(ERROR) << __func__ << ": " << mType << ": following sink port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000983 << ::android::internal::ToString(missingIds);
984 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
985 }
986 // bool indicates whether a non-exclusive route is available.
987 // If only an exclusive route is available, that means the patch can not be
988 // established if there is any other patch which currently uses the sink port.
989 std::map<int32_t, bool> allowedSinkPorts;
990 auto& routes = getConfig().routes;
991 for (auto src : sources) {
992 for (const auto& r : routes) {
993 const auto& srcs = r.sourcePortIds;
994 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
995 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
996 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
997 }
998 }
999 }
1000 }
1001 for (auto sink : sinks) {
1002 if (allowedSinkPorts.count(sink->portId) == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301003 LOG(ERROR) << __func__ << ": " << mType << ": there is no route to the sink port id "
1004 << sink->portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001005 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1006 }
1007 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001008 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +00001009
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001010 auto& patches = getConfig().patches;
1011 auto existing = patches.end();
1012 std::optional<decltype(mPatches)> patchesBackup;
1013 if (in_requested.id != 0) {
1014 existing = findById<AudioPatch>(patches, in_requested.id);
1015 if (existing != patches.end()) {
1016 patchesBackup = mPatches;
1017 cleanUpPatch(existing->id);
1018 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301019 LOG(ERROR) << __func__ << ": " << mType << ": not found existing patch id "
1020 << in_requested.id;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001021 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1022 }
1023 }
1024 // Validate the requested patch.
1025 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
1026 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301027 LOG(ERROR) << __func__ << ": " << mType << ": sink port id " << sinkPortId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001028 << "is exclusive and is already used by some other patch";
1029 if (patchesBackup.has_value()) {
1030 mPatches = std::move(*patchesBackup);
1031 }
1032 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1033 }
1034 }
Mikhail Naganov13501872023-10-18 16:15:46 -07001035 // Find the highest sample rate among mix port configs.
1036 std::map<int32_t, AudioPortConfig*> sampleRates;
1037 std::vector<AudioPortConfig*>& mixPortConfigs =
1038 sources[0]->ext.getTag() == AudioPortExt::mix ? sources : sinks;
1039 for (auto mix : mixPortConfigs) {
1040 sampleRates.emplace(mix->sampleRate.value().value, mix);
1041 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001042 *_aidl_return = in_requested;
Mikhail Naganov13501872023-10-18 16:15:46 -07001043 auto maxSampleRateIt = std::max_element(sampleRates.begin(), sampleRates.end());
1044 const int32_t latencyMs = getNominalLatencyMs(*(maxSampleRateIt->second));
1045 _aidl_return->minimumStreamBufferSizeFrames =
1046 calculateBufferSizeFrames(latencyMs, maxSampleRateIt->first);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001047 _aidl_return->latenciesMs.clear();
1048 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
Mikhail Naganov13501872023-10-18 16:15:46 -07001049 _aidl_return->sinkPortConfigIds.size(), latencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001050 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001051 if (existing == patches.end()) {
1052 _aidl_return->id = getConfig().nextPatchId++;
1053 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001054 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001055 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -07001056 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001057 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001058 patchesBackup = mPatches;
1059 registerPatch(*_aidl_return);
1060 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
1061 mPatches = std::move(*patchesBackup);
1062 if (existing == patches.end()) {
1063 patches.pop_back();
1064 } else {
1065 *existing = oldPatch;
1066 }
1067 return status;
1068 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001069
Jaideep Sharma559a4912024-03-07 10:05:51 +05301070 LOG(DEBUG) << __func__ << ": " << mType << ": " << (oldPatch.id == 0 ? "created" : "updated")
1071 << " patch " << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001072 return ndk::ScopedAStatus::ok();
1073}
1074
1075ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
1076 AudioPortConfig* out_suggested, bool* _aidl_return) {
Mikhail Naganova92039a2023-12-20 14:27:22 -08001077 auto generate = [this](const AudioPort& port, AudioPortConfig* config) {
1078 return generateDefaultPortConfig(port, config);
1079 };
1080 return setAudioPortConfigImpl(in_requested, generate, out_suggested, _aidl_return);
1081}
1082
1083ndk::ScopedAStatus Module::setAudioPortConfigImpl(
1084 const AudioPortConfig& in_requested,
1085 const std::function<bool(const ::aidl::android::media::audio::common::AudioPort& port,
1086 ::aidl::android::media::audio::common::AudioPortConfig* config)>&
1087 fillPortConfig,
1088 AudioPortConfig* out_suggested, bool* applied) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301089 LOG(DEBUG) << __func__ << ": " << mType << ": requested " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001090 auto& configs = getConfig().portConfigs;
1091 auto existing = configs.end();
1092 if (in_requested.id != 0) {
1093 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1094 existing == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301095 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id "
1096 << in_requested.id << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001097 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1098 }
1099 }
1100
1101 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1102 if (portId == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301103 LOG(ERROR) << __func__ << ": " << mType
1104 << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001105 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1106 }
1107 auto& ports = getConfig().ports;
1108 auto portIt = findById<AudioPort>(ports, portId);
1109 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301110 LOG(ERROR) << __func__ << ": " << mType
1111 << ": requested port config points to non-existent portId " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001112 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1113 }
1114 if (existing != configs.end()) {
1115 *out_suggested = *existing;
1116 } else {
1117 AudioPortConfig newConfig;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001118 newConfig.portId = portIt->id;
1119 if (fillPortConfig(*portIt, &newConfig)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001120 *out_suggested = newConfig;
1121 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301122 LOG(ERROR) << __func__ << ": " << mType
1123 << ": unable generate a default config for port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001124 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1125 }
1126 }
1127 // From this moment, 'out_suggested' is either an existing port config,
1128 // or a new generated config. Now attempt to update it according to the specified
1129 // fields of 'in_requested'.
1130
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001131 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1132 // which takes care of their actual configuration automatically.
1133 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1134 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001135 bool requestedIsValid = true, requestedIsFullySpecified = true;
1136
1137 AudioIoFlags portFlags = portIt->flags;
1138 if (in_requested.flags.has_value()) {
1139 if (in_requested.flags.value() != portFlags) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301140 LOG(WARNING) << __func__ << ": " << mType << ": requested flags "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001141 << in_requested.flags.value().toString() << " do not match port's "
1142 << portId << " flags " << portFlags.toString();
1143 requestedIsValid = false;
1144 }
1145 } else {
1146 requestedIsFullySpecified = false;
1147 }
1148
1149 AudioProfile portProfile;
1150 if (in_requested.format.has_value()) {
1151 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001152 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1153 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001154 out_suggested->format = format;
1155 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301156 LOG(WARNING) << __func__ << ": " << mType << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001157 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001158 requestedIsValid = false;
1159 }
1160 } else {
1161 requestedIsFullySpecified = false;
1162 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001163 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1164 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301165 LOG(ERROR) << __func__ << ": " << mType << ": port " << portId
1166 << " does not support format " << out_suggested->format.value().toString()
1167 << " anymore";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001168 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1169 }
1170
1171 if (in_requested.channelMask.has_value()) {
1172 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001173 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1174 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1175 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001176 out_suggested->channelMask = channelMask;
1177 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301178 LOG(WARNING) << __func__ << ": " << mType << ": requested channel mask "
1179 << channelMask.toString() << " is not supported for the format "
1180 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001181 requestedIsValid = false;
1182 }
1183 } else {
1184 requestedIsFullySpecified = false;
1185 }
1186
1187 if (in_requested.sampleRate.has_value()) {
1188 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001189 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1190 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001191 sampleRate.value) != portProfile.sampleRates.end()) {
1192 out_suggested->sampleRate = sampleRate;
1193 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301194 LOG(WARNING) << __func__ << ": " << mType << ": requested sample rate "
1195 << sampleRate.value << " is not supported for the format "
1196 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001197 requestedIsValid = false;
1198 }
1199 } else {
1200 requestedIsFullySpecified = false;
1201 }
1202
1203 if (in_requested.gain.has_value()) {
Weilin Xua33bb5e2024-10-02 17:16:42 +00001204 if (!setAudioPortConfigGain(*portIt, in_requested.gain.value())) {
1205 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1206 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001207 out_suggested->gain = in_requested.gain.value();
1208 }
1209
Mikhail Naganov248e9502023-02-21 16:32:40 -08001210 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1211 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1212 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
Mikhail Naganovb06a4922024-03-06 16:39:50 -08001213 // 'AudioMixPortExt.handle' and '.usecase' are set by the client,
1214 // copy from in_requested.
1215 const auto& src = in_requested.ext.get<AudioPortExt::Tag::mix>();
1216 auto& dst = out_suggested->ext.get<AudioPortExt::Tag::mix>();
1217 dst.handle = src.handle;
1218 dst.usecase = src.usecase;
Mikhail Naganov248e9502023-02-21 16:32:40 -08001219 }
1220 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301221 LOG(WARNING) << __func__ << ": " << mType << ": requested ext tag "
Mikhail Naganov248e9502023-02-21 16:32:40 -08001222 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1223 << toString(out_suggested->ext.getTag());
1224 requestedIsValid = false;
1225 }
1226 }
1227
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001228 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1229 out_suggested->id = getConfig().nextPortId++;
1230 configs.push_back(*out_suggested);
Mikhail Naganova92039a2023-12-20 14:27:22 -08001231 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301232 LOG(DEBUG) << __func__ << ": " << mType << ": created new port config "
1233 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001234 } else if (existing != configs.end() && requestedIsValid) {
1235 *existing = *out_suggested;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001236 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301237 LOG(DEBUG) << __func__ << ": " << mType << ": updated port config "
1238 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001239 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301240 LOG(DEBUG) << __func__ << ": " << mType << ": not applied; existing config ? "
1241 << (existing != configs.end()) << "; requested is valid? " << requestedIsValid
1242 << ", fully specified? " << requestedIsFullySpecified;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001243 *applied = false;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001244 }
1245 return ndk::ScopedAStatus::ok();
1246}
1247
Weilin Xua33bb5e2024-10-02 17:16:42 +00001248bool Module::setAudioPortConfigGain(const AudioPort& port, const AudioGainConfig& gainRequested) {
1249 auto& ports = getConfig().ports;
1250 if (gainRequested.index < 0 || gainRequested.index >= (int)port.gains.size()) {
1251 LOG(ERROR) << __func__ << ": gains for port " << port.id << " is undefined";
1252 return false;
1253 }
1254 int stepValue = port.gains[gainRequested.index].stepValue;
1255 if (stepValue == 0) {
1256 LOG(ERROR) << __func__ << ": port gain step value is 0";
1257 return false;
1258 }
1259 int minValue = port.gains[gainRequested.index].minValue;
1260 int maxValue = port.gains[gainRequested.index].maxValue;
1261 if (gainRequested.values[0] > maxValue || gainRequested.values[0] < minValue) {
1262 LOG(ERROR) << __func__ << ": gain value " << gainRequested.values[0]
1263 << " out of range of min and max gain config";
1264 return false;
1265 }
1266 int gainIndex = (gainRequested.values[0] - minValue) / stepValue;
1267 int totalSteps = (maxValue - minValue) / stepValue;
1268 if (totalSteps == 0) {
1269 LOG(ERROR) << __func__ << ": difference between port gain min value " << minValue
1270 << " and max value " << maxValue << " is less than step value " << stepValue;
1271 return false;
1272 }
1273 // Root-power quantities are used in curve:
1274 // 10^((minMb / 100 + (maxMb / 100 - minMb / 100) * gainIndex / totalSteps) / (10 * 2))
1275 // where 100 is the conversion from mB to dB, 10 comes from the log 10 conversion from power
1276 // ratios, and 2 means are the square of amplitude.
1277 float gain =
1278 pow(10, (minValue + (maxValue - minValue) * (gainIndex / (float)totalSteps)) / 2000);
1279 if (gain < 0) {
1280 LOG(ERROR) << __func__ << ": gain " << gain << " is less than 0";
1281 return false;
1282 }
1283 for (const auto& route : getConfig().routes) {
1284 if (route.sinkPortId != port.id) {
1285 continue;
1286 }
1287 for (const auto sourcePortId : route.sourcePortIds) {
1288 mStreams.setGain(sourcePortId, gain);
1289 }
1290 }
1291 return true;
1292}
1293
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001294ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1295 auto& patches = getConfig().patches;
1296 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1297 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001298 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001299 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001300 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1301 mPatches = std::move(patchesBackup);
1302 return status;
1303 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001304 patches.erase(patchIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301305 LOG(DEBUG) << __func__ << ": " << mType << ": erased patch " << in_patchId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001306 return ndk::ScopedAStatus::ok();
1307 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301308 LOG(ERROR) << __func__ << ": " << mType << ": patch id " << in_patchId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001309 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1310}
1311
1312ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1313 auto& configs = getConfig().portConfigs;
1314 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1315 if (configIt != configs.end()) {
1316 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301317 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001318 << " has a stream opened on it";
1319 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1320 }
1321 auto patchIt = mPatches.find(in_portConfigId);
1322 if (patchIt != mPatches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301323 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001324 << " is used by the patch with id " << patchIt->second;
1325 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1326 }
1327 auto& initials = getConfig().initialConfigs;
1328 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1329 if (initialIt == initials.end()) {
1330 configs.erase(configIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301331 LOG(DEBUG) << __func__ << ": " << mType << ": erased port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001332 } else if (*configIt != *initialIt) {
1333 *configIt = *initialIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301334 LOG(DEBUG) << __func__ << ": " << mType << ": reset port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001335 }
1336 return ndk::ScopedAStatus::ok();
1337 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301338 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
1339 << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001340 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1341}
1342
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001343ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1344 *_aidl_return = mMasterMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301345 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001346 return ndk::ScopedAStatus::ok();
1347}
1348
1349ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301350 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001351 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1352 : onMasterMuteChanged(in_mute);
1353 if (result.isOk()) {
1354 mMasterMute = in_mute;
1355 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301356 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterMuteChanged("
1357 << in_mute << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001358 // Reset master mute if it failed.
1359 onMasterMuteChanged(mMasterMute);
1360 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001361 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001362}
1363
1364ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1365 *_aidl_return = mMasterVolume;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301366 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001367 return ndk::ScopedAStatus::ok();
1368}
1369
1370ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301371 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001372 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001373 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1374 : onMasterVolumeChanged(in_volume);
1375 if (result.isOk()) {
1376 mMasterVolume = in_volume;
1377 } else {
1378 // Reset master volume if it failed.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301379 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterVolumeChanged("
1380 << in_volume << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001381 onMasterVolumeChanged(mMasterVolume);
1382 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001383 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001384 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301385 LOG(ERROR) << __func__ << ": " << mType << ": invalid master volume value: " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001386 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1387}
1388
1389ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1390 *_aidl_return = mMicMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301391 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001392 return ndk::ScopedAStatus::ok();
1393}
1394
1395ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301396 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001397 mMicMute = in_mute;
1398 return ndk::ScopedAStatus::ok();
1399}
1400
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001401ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001402 *_aidl_return = getMicrophoneInfos();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301403 LOG(DEBUG) << __func__ << ": " << mType << ": returning "
1404 << ::android::internal::ToString(*_aidl_return);
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001405 return ndk::ScopedAStatus::ok();
1406}
1407
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001408ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001409 if (!isValidAudioMode(in_mode)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301410 LOG(ERROR) << __func__ << ": " << mType << ": invalid mode " << toString(in_mode);
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001411 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1412 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001413 // No checks for supported audio modes here, it's an informative notification.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301414 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_mode);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001415 return ndk::ScopedAStatus::ok();
1416}
1417
1418ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301419 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_rotation);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001420 return ndk::ScopedAStatus::ok();
1421}
1422
1423ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301424 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_isTurnedOn;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001425 return ndk::ScopedAStatus::ok();
1426}
1427
Vlad Popa83a6d822022-11-07 13:53:57 +01001428ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001429 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001430 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001431 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001432 *_aidl_return = mSoundDose.getInstance();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301433 LOG(DEBUG) << __func__ << ": " << mType
1434 << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001435 return ndk::ScopedAStatus::ok();
1436}
1437
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001438ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301439 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001440 (void)_aidl_return;
1441 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1442}
1443
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001444const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001445const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001446
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001447ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1448 std::vector<VendorParameter>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301449 LOG(VERBOSE) << __func__ << ": " << mType << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001450 bool allParametersKnown = true;
1451 for (const auto& id : in_ids) {
1452 if (id == VendorDebug::kForceTransientBurstName) {
1453 VendorParameter forceTransientBurst{.id = id};
1454 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1455 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001456 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1457 VendorParameter forceSynchronousDrain{.id = id};
1458 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1459 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001460 } else {
1461 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301462 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << id << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001463 }
1464 }
1465 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1466 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001467}
1468
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001469namespace {
1470
1471template <typename W>
1472bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1473 std::optional<W> value;
1474 binder_status_t result = p.ext.getParcelable(&value);
1475 if (result == STATUS_OK && value.has_value()) {
1476 *v = value.value().value;
1477 return true;
1478 }
1479 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1480 << "\": " << result;
1481 return false;
1482}
1483
1484} // namespace
1485
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001486ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1487 bool in_async) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301488 LOG(VERBOSE) << __func__ << ": " << mType << ": parameter count " << in_parameters.size()
1489 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001490 bool allParametersKnown = true;
1491 for (const auto& p : in_parameters) {
1492 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001493 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1494 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1495 }
1496 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1497 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001498 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1499 }
1500 } else {
1501 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301502 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << p.id
1503 << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001504 }
1505 }
1506 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1507 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001508}
1509
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001510ndk::ScopedAStatus Module::addDeviceEffect(
1511 int32_t in_portConfigId,
1512 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1513 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301514 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1515 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001516 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301517 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1518 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001519 }
1520 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1521}
1522
1523ndk::ScopedAStatus Module::removeDeviceEffect(
1524 int32_t in_portConfigId,
1525 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1526 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301527 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1528 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001529 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301530 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1531 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001532 }
1533 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1534}
1535
jiabin9a8e6862023-01-12 23:06:37 +00001536ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1537 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301538 LOG(DEBUG) << __func__ << ": " << mType << ": mmap policy type " << toString(mmapPolicyType);
jiabin9a8e6862023-01-12 23:06:37 +00001539 std::set<int32_t> mmapSinks;
1540 std::set<int32_t> mmapSources;
1541 auto& ports = getConfig().ports;
1542 for (const auto& port : ports) {
1543 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1544 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1545 AudioInputFlags::MMAP_NOIRQ)) {
1546 mmapSinks.insert(port.id);
1547 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1548 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1549 AudioOutputFlags::MMAP_NOIRQ)) {
1550 mmapSources.insert(port.id);
1551 }
1552 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001553 if (mmapSources.empty() && mmapSinks.empty()) {
1554 AudioMMapPolicyInfo never;
1555 never.mmapPolicy = AudioMMapPolicy::NEVER;
1556 _aidl_return->push_back(never);
1557 return ndk::ScopedAStatus::ok();
1558 }
jiabin9a8e6862023-01-12 23:06:37 +00001559 for (const auto& route : getConfig().routes) {
1560 if (mmapSinks.count(route.sinkPortId) != 0) {
1561 // The sink is a mix port, add the sources if they are device ports.
1562 for (int sourcePortId : route.sourcePortIds) {
1563 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1564 if (sourcePortIt == ports.end()) {
1565 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301566 LOG(ERROR) << __func__ << ": " << mType << ": port id " << sourcePortId
1567 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001568 continue;
1569 }
1570 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1571 // The source is not a device port, skip
1572 continue;
1573 }
1574 AudioMMapPolicyInfo policyInfo;
1575 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1576 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1577 // default implementation.
1578 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1579 _aidl_return->push_back(policyInfo);
1580 }
1581 } else {
1582 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1583 if (sinkPortIt == ports.end()) {
1584 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301585 LOG(ERROR) << __func__ << ": " << mType << ": port id " << route.sinkPortId
1586 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001587 continue;
1588 }
1589 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1590 // The sink is not a device port, skip
1591 continue;
1592 }
1593 if (count_any(mmapSources, route.sourcePortIds)) {
1594 AudioMMapPolicyInfo policyInfo;
1595 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1596 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1597 // default implementation.
1598 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1599 _aidl_return->push_back(policyInfo);
1600 }
1601 }
1602 }
1603 return ndk::ScopedAStatus::ok();
1604}
1605
Eric Laurente2432ea2023-01-12 17:47:31 +01001606ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301607 LOG(DEBUG) << __func__ << ": " << mType;
Eric Laurente2432ea2023-01-12 17:47:31 +01001608 *_aidl_return = false;
1609 return ndk::ScopedAStatus::ok();
1610}
1611
jiabinb76981e2023-01-18 00:58:30 +00001612ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1613 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301614 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001615 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1616 }
1617 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301618 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001619 return ndk::ScopedAStatus::ok();
1620}
1621
1622ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1623 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301624 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001625 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1626 }
1627 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301628 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001629 return ndk::ScopedAStatus::ok();
1630}
1631
1632bool Module::isMmapSupported() {
1633 if (mIsMmapSupported.has_value()) {
1634 return mIsMmapSupported.value();
1635 }
1636 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1637 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1638 mIsMmapSupported = false;
1639 } else {
1640 mIsMmapSupported =
1641 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1642 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1643 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1644 }) != mmapPolicyInfos.end();
1645 }
1646 return mIsMmapSupported.value();
1647}
1648
Mikhail Naganova92039a2023-12-20 14:27:22 -08001649ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001650 if (audioPort->ext.getTag() != AudioPortExt::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301651 LOG(ERROR) << __func__ << ": " << mType << ": not a device port: " << audioPort->toString();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001652 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1653 }
1654 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1655 if (!devicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301656 LOG(ERROR) << __func__ << ": " << mType << ": module implementation must override "
1657 "'populateConnectedDevicePort' "
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001658 << "to handle connection of external devices.";
1659 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1660 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301661 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001662 return ndk::ScopedAStatus::ok();
1663}
1664
1665ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1666 const std::vector<AudioPortConfig*>& sources __unused,
1667 const std::vector<AudioPortConfig*>& sinks __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301668 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001669 return ndk::ScopedAStatus::ok();
1670}
1671
jiabin783c48b2023-02-28 18:28:06 +00001672void Module::onExternalDeviceConnectionChanged(
1673 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1674 bool connected __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301675 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabin783c48b2023-02-28 18:28:06 +00001676}
1677
jiabindd23b0e2023-12-11 19:10:05 +00001678void Module::onPrepareToDisconnectExternalDevice(
1679 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301680 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabindd23b0e2023-12-11 19:10:05 +00001681}
1682
jiabin783c48b2023-02-28 18:28:06 +00001683ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301684 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001685 return ndk::ScopedAStatus::ok();
1686}
1687
1688ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301689 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001690 return ndk::ScopedAStatus::ok();
1691}
1692
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001693std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1694 std::vector<MicrophoneInfo> result;
1695 Configuration& config = getConfig();
1696 for (const AudioPort& port : config.ports) {
1697 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1698 const AudioDeviceType deviceType =
1699 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1700 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1701 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1702 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1703 // accordingly based on their physical microphone parameters.
1704 result.push_back(MicrophoneInfo{
1705 .id = port.name,
1706 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1707 .group = 0,
1708 .indexInTheGroup = 0,
1709 });
1710 }
1711 }
1712 }
1713 return result;
1714}
1715
Ram Mohan18f0d512023-07-01 00:47:09 +05301716ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1717 return mStreams.bluetoothParametersUpdated();
1718}
1719
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001720} // namespace aidl::android::hardware::audio::core