blob: a2a357ce8862c446705119c93121d330f0c6b4c8 [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
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530239std::vector<AudioDevice> Module::getDevicesFromDevicePortConfigIds(
240 const std::set<int32_t>& devicePortConfigIds) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000241 std::vector<AudioDevice> result;
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530242 auto& configs = getConfig().portConfigs;
243 for (const auto& id : devicePortConfigIds) {
244 auto it = findById<AudioPortConfig>(configs, id);
245 if (it != configs.end() && it->ext.getTag() == AudioPortExt::Tag::device) {
246 result.push_back(it->ext.template get<AudioPortExt::Tag::device>().device);
247 } else {
248 LOG(FATAL) << __func__ << ": " << mType
249 << ": failed to find device for id" << id;
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000250 }
251 }
252 return result;
253}
254
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530255std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
256 return getDevicesFromDevicePortConfigIds(findConnectedPortConfigIds(portConfigId));
257}
258
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000259std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
260 std::set<int32_t> result;
261 auto patchIdsRange = mPatches.equal_range(portConfigId);
262 auto& patches = getConfig().patches;
263 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
264 auto patchIt = findById<AudioPatch>(patches, it->second);
265 if (patchIt == patches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530266 LOG(FATAL) << __func__ << ": " << mType << ": patch with id " << it->second
267 << " taken from mPatches "
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000268 << "not found in the configuration";
269 }
270 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
271 portConfigId) != patchIt->sourcePortConfigIds.end()) {
272 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
273 } else {
274 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
275 }
276 }
277 return result;
278}
279
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000280ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
281 auto& configs = getConfig().portConfigs;
282 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
283 if (portConfigIt == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530284 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id " << in_portConfigId
285 << " not found";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000286 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
287 }
288 const int32_t portId = portConfigIt->portId;
289 // In our implementation, configs of mix ports always have unique IDs.
290 CHECK(portId != in_portConfigId);
291 auto& ports = getConfig().ports;
292 auto portIt = findById<AudioPort>(ports, portId);
293 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530294 LOG(ERROR) << __func__ << ": " << mType << ": port id " << portId
295 << " used by port config id " << in_portConfigId << " not found";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000296 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
297 }
298 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530299 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000300 << " already has a stream opened on it";
301 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
302 }
303 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530304 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000305 << " does not correspond to a mix port";
306 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
307 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700308 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000309 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530310 LOG(ERROR) << __func__ << ": " << mType << ": port id " << portId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000311 << " has already reached maximum allowed opened stream count: "
312 << maxOpenStreamCount;
313 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
314 }
315 *port = &(*portIt);
316 return ndk::ScopedAStatus::ok();
317}
318
Mikhail Naganova92039a2023-12-20 14:27:22 -0800319bool Module::generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
320 const bool allowDynamicConfig = port.ext.getTag() == AudioPortExt::device;
321 for (const auto& profile : port.profiles) {
322 if (isDynamicProfile(profile)) continue;
323 config->format = profile.format;
324 config->channelMask = *profile.channelMasks.begin();
325 config->sampleRate = Int{.value = *profile.sampleRates.begin()};
326 config->flags = port.flags;
327 config->ext = port.ext;
328 return true;
329 }
330 if (allowDynamicConfig) {
331 config->format = AudioFormatDescription{};
332 config->channelMask = AudioChannelLayout{};
333 config->sampleRate = Int{.value = 0};
334 config->flags = port.flags;
335 config->ext = port.ext;
336 return true;
337 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530338 LOG(ERROR) << __func__ << ": " << mType << ": port " << port.id << " only has dynamic profiles";
Mikhail Naganova92039a2023-12-20 14:27:22 -0800339 return false;
340}
341
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000342void Module::populateConnectedProfiles() {
343 Configuration& config = getConfig();
344 for (const AudioPort& port : config.ports) {
345 if (port.ext.getTag() == AudioPortExt::device) {
346 if (auto devicePort = port.ext.get<AudioPortExt::device>();
347 !devicePort.device.type.connection.empty() && port.profiles.empty()) {
348 if (auto connIt = config.connectedProfiles.find(port.id);
349 connIt == config.connectedProfiles.end()) {
350 config.connectedProfiles.emplace(
351 port.id, internal::getStandard16And24BitPcmAudioProfiles());
352 }
353 }
354 }
355 }
356}
357
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000358template <typename C>
359std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
360 std::set<int32_t> result;
361 auto& portConfigs = getConfig().portConfigs;
362 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
363 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
364 if (portConfigIt != portConfigs.end()) {
365 result.insert(portConfigIt->portId);
366 }
367 }
368 return result;
369}
370
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000371std::unique_ptr<Module::Configuration> Module::initializeConfig() {
372 return internal::getConfiguration(getType());
Peter Yoon918a6a52023-07-13 17:04:37 +0900373}
374
Mikhail Naganov13501872023-10-18 16:15:46 -0700375int32_t Module::getNominalLatencyMs(const AudioPortConfig&) {
376 // Arbitrary value. Implementations must override this method to provide their actual latency.
377 static constexpr int32_t kLatencyMs = 5;
378 return kLatencyMs;
379}
380
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800381ndk::ScopedAStatus Module::createMmapBuffer(
382 const ::aidl::android::hardware::audio::core::StreamContext& context __unused,
383 ::aidl::android::hardware::audio::core::StreamDescriptor* desc __unused) {
384 LOG(ERROR) << __func__ << ": " << mType << ": is not implemented";
385 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
386}
387
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700388std::vector<AudioRoute*> Module::getAudioRoutesForAudioPortImpl(int32_t portId) {
389 std::vector<AudioRoute*> result;
390 auto& routes = getConfig().routes;
391 for (auto& r : routes) {
392 const auto& srcs = r.sourcePortIds;
393 if (r.sinkPortId == portId || std::find(srcs.begin(), srcs.end(), portId) != srcs.end()) {
394 result.push_back(&r);
395 }
396 }
397 return result;
398}
399
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000400Module::Configuration& Module::getConfig() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000401 if (!mConfig) {
Yi Konge62f97f2024-08-14 01:52:04 +0800402 mConfig = initializeConfig();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000403 }
404 return *mConfig;
405}
406
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700407std::set<int32_t> Module::getRoutableAudioPortIds(int32_t portId,
408 std::vector<AudioRoute*>* routes) {
409 std::vector<AudioRoute*> routesStorage;
410 if (routes == nullptr) {
411 routesStorage = getAudioRoutesForAudioPortImpl(portId);
412 routes = &routesStorage;
413 }
414 std::set<int32_t> result;
415 for (AudioRoute* r : *routes) {
416 if (r->sinkPortId == portId) {
417 result.insert(r->sourcePortIds.begin(), r->sourcePortIds.end());
418 } else {
419 result.insert(r->sinkPortId);
420 }
421 }
422 return result;
423}
424
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000425void Module::registerPatch(const AudioPatch& patch) {
426 auto& configs = getConfig().portConfigs;
427 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
428 for (auto portConfigId : portConfigIds) {
429 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
430 if (configIt != configs.end()) {
431 mPatches.insert(std::pair{portConfigId, patch.id});
432 if (configIt->portId != portConfigId) {
433 mPatches.insert(std::pair{configIt->portId, patch.id});
434 }
435 }
436 };
437 };
438 do_insert(patch.sourcePortConfigIds);
439 do_insert(patch.sinkPortConfigIds);
440}
441
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700442ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
443 const AudioPatch& newPatch) {
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700444 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700445 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700446 using Connections =
447 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
448 Connections oldConnections, newConnections;
449 auto fillConnectionsHelper = [&](Connections& connections,
450 const std::vector<int32_t>& mixPortCfgIds,
451 const std::vector<int32_t>& devicePortCfgIds) {
452 for (int32_t mixPortCfgId : mixPortCfgIds) {
453 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
454 }
455 };
456 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
457 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
458 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
459 patch.sourcePortConfigIds.end()) {
460 // Sources are mix ports.
461 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
462 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
463 [&](int32_t portConfigId) {
464 return mStreams.count(portConfigId) > 0;
465 }) != patch.sinkPortConfigIds.end()) {
466 // Sources are device ports.
467 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
468 } // Otherwise, there are no streams to notify.
469 };
470 fillConnections(oldConnections, oldPatch);
471 fillConnections(newConnections, newPatch);
472
473 std::for_each(oldConnections.begin(), oldConnections.end(), [&](const auto& connectionPair) {
474 const int32_t mixPortConfigId = connectionPair.first;
475 if (auto it = newConnections.find(mixPortConfigId);
476 it == newConnections.end() || it->second != connectionPair.second) {
477 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, {});
478 status.isOk()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700479 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700480 << mixPortConfigId << " has been disconnected";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700481 } else {
482 // Disconnection is tricky to roll back, just register a failure.
483 maybeFailure = std::move(status);
484 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000485 }
486 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700487 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700488 std::set<int32_t> idsToDisconnectOnFailure;
489 std::for_each(newConnections.begin(), newConnections.end(), [&](const auto& connectionPair) {
490 const int32_t mixPortConfigId = connectionPair.first;
491 if (auto it = oldConnections.find(mixPortConfigId);
492 it == oldConnections.end() || it->second != connectionPair.second) {
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530493 const auto connectedDevices = getDevicesFromDevicePortConfigIds(connectionPair.second);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700494 if (connectedDevices.empty()) {
495 // This is important as workers use the vector size to derive the connection status.
496 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
497 "config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700498 << mixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700499 }
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700500 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700501 status.isOk()) {
502 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700503 << mixPortConfigId << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700504 << ::android::internal::ToString(connectedDevices);
505 } else {
506 maybeFailure = std::move(status);
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700507 idsToDisconnectOnFailure.insert(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700508 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000509 }
510 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700511 if (!maybeFailure.isOk()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530512 LOG(WARNING) << __func__ << ": " << mType
513 << ": Due to a failure, disconnecting streams on port config ids "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700514 << ::android::internal::ToString(idsToDisconnectOnFailure);
515 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
516 [&](const auto& portConfigId) {
517 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
518 (void)status.isOk(); // Can't do much about a failure here.
519 });
520 return maybeFailure;
521 }
522 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000523}
524
Mikhail Naganov00603d12022-05-02 22:52:13 +0000525ndk::ScopedAStatus Module::setModuleDebug(
526 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700527 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000528 << ", new flags: " << in_debug.toString();
529 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
530 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700531 LOG(ERROR) << __func__ << ": " << mType
Jaideep Sharma559a4912024-03-07 10:05:51 +0530532 << ": attempting to change device connections simulation while "
533 "having external "
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700534 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000535 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
536 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000537 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700538 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000539 << in_debug.streamTransientStateDelayMs;
540 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
541 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000542 mDebug = in_debug;
543 return ndk::ScopedAStatus::ok();
544}
545
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000546ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700547 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530548 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000549 return ndk::ScopedAStatus::ok();
550}
551
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000552ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700553 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530554 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000555 return ndk::ScopedAStatus::ok();
556}
557
Mikhail Naganov7499a002023-02-27 18:51:44 -0800558ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700559 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530560 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800561 return ndk::ScopedAStatus::ok();
562}
563
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800564ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700565 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530566 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800567 return ndk::ScopedAStatus::ok();
568}
569
Mikhail Naganov00603d12022-05-02 22:52:13 +0000570ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
571 AudioPort* _aidl_return) {
572 const int32_t templateId = in_templateIdAndAdditionalData.id;
573 auto& ports = getConfig().ports;
574 AudioPort connectedPort;
575 { // Scope the template port so that we don't accidentally modify it.
576 auto templateIt = findById<AudioPort>(ports, templateId);
577 if (templateIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530578 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000579 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
580 }
581 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530582 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
583 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000584 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
585 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000586 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
587 if (templateDevicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530588 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
589 << " is permanently attached";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000590 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
591 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700592 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530593 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
594 << " is a connected device port";
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700595 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
596 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000597 // Postpone id allocation until we ensure that there are no client errors.
598 connectedPort = *templateIt;
599 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
600 const auto& inputDevicePort =
601 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
602 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
603 connectedDevicePort.device.address = inputDevicePort.device.address;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530604 LOG(DEBUG) << __func__ << ": " << mType << ": device port " << connectedPort.id
605 << " device set to " << connectedDevicePort.device.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000606 // Check if there is already a connected port with for the same external device.
Jaideep Sharma559a4912024-03-07 10:05:51 +0530607
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700608 for (auto connectedPortPair : mConnectedDevicePorts) {
609 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000610 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
611 connectedDevicePort.device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530612 LOG(ERROR) << __func__ << ": " << mType << ": device "
613 << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700614 << " is already connected at the device port id "
615 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000616 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
617 }
618 }
619 }
620
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700621 // Two main cases are considered with regard to the profiles of the connected device port:
622 //
623 // 1. If the template device port has dynamic profiles, and at least one routable mix
624 // port also has dynamic profiles, it means that after connecting the device, the
625 // connected device port must have profiles populated with actual capabilities of
626 // the connected device, and dynamic of routable mix ports will be filled
627 // according to these capabilities. An example of this case is connection of an
628 // HDMI or USB device. For USB handled by ADSP, there can be mix ports with static
629 // profiles, and one dedicated mix port for "hi-fi" playback. The latter is left with
630 // dynamic profiles so that they can be populated with actual capabilities of
631 // the connected device.
632 //
633 // 2. If the template device port has dynamic profiles, while all routable mix ports
634 // have static profiles, it means that after connecting the device, the connected
635 // device port can be left with dynamic profiles, and profiles of mix ports are
636 // left untouched. An example of this case is connection of an analog wired
637 // headset, it should be treated in the same way as a speaker.
638 //
639 // Yet another possible case is when both the template device port and all routable
640 // mix ports have static profiles. This is allowed and handled correctly, however, it
641 // is not very practical, since these profiles are likely duplicates of each other.
642
643 std::vector<AudioRoute*> routesToMixPorts = getAudioRoutesForAudioPortImpl(templateId);
644 std::set<int32_t> routableMixPortIds = getRoutableAudioPortIds(templateId, &routesToMixPorts);
Mikhail Naganova92039a2023-12-20 14:27:22 -0800645 const int32_t nextPortId = getConfig().nextPortId++;
Mikhail Naganov55045b52023-10-24 17:03:50 -0700646 if (!mDebug.simulateDeviceConnections) {
647 // Even if the device port has static profiles, the HAL module might need to update
648 // them, or abort the connection process.
Mikhail Naganova92039a2023-12-20 14:27:22 -0800649 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort, nextPortId));
Mikhail Naganov55045b52023-10-24 17:03:50 -0700650 } else if (hasDynamicProfilesOnly(connectedPort.profiles)) {
651 auto& connectedProfiles = getConfig().connectedProfiles;
652 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
653 connectedProfilesIt != connectedProfiles.end()) {
654 connectedPort.profiles = connectedProfilesIt->second;
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700655 }
Mikhail Naganov55045b52023-10-24 17:03:50 -0700656 }
657 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
658 // Possible case 2. Check if all routable mix ports have static profiles.
659 if (auto dynamicMixPortIt = std::find_if(ports.begin(), ports.end(),
660 [&routableMixPortIds](const auto& p) {
661 return routableMixPortIds.count(p.id) > 0 &&
662 hasDynamicProfilesOnly(p.profiles);
663 });
664 dynamicMixPortIt != ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530665 LOG(ERROR) << __func__ << ": " << mType
666 << ": connected port only has dynamic profiles after connecting "
Mikhail Naganov55045b52023-10-24 17:03:50 -0700667 << "external device " << connectedPort.toString() << ", and there exist "
668 << "a routable mix port with dynamic profiles: "
669 << dynamicMixPortIt->toString();
670 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530671 }
672 }
673
Mikhail Naganova92039a2023-12-20 14:27:22 -0800674 connectedPort.id = nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700675 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700676 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Jaideep Sharma559a4912024-03-07 10:05:51 +0530677 LOG(DEBUG) << __func__ << ": " << mType << ": template port " << templateId
678 << " external device connected, "
Mikhail Naganov00603d12022-05-02 22:52:13 +0000679 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000680 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000681 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000682
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700683 // For routes where the template port is a source, add the connected port to sources,
684 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000685 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700686 for (AudioRoute* r : routesToMixPorts) {
687 if (r->sinkPortId == templateId) {
688 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
689 .sinkPortId = connectedPort.id,
690 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000691 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700692 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000693 }
694 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700695 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000696 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
697
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700698 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
699 // Note: this is a simplistic approach assuming that a mix port can only be populated
700 // from a single device port. Implementing support for stuffing dynamic profiles with
701 // a superset of all profiles from all routable dynamic device ports would be more involved.
702 for (auto& port : ports) {
703 if (routableMixPortIds.count(port.id) == 0) continue;
704 if (hasDynamicProfilesOnly(port.profiles)) {
705 port.profiles = connectedPort.profiles;
706 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700707 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700708 // Check if profiles are not all dynamic because they were populated by
709 // a previous connection. Otherwise, it means that they are actually static.
710 for (const auto& cp : mConnectedDevicePorts) {
711 if (cp.second.count(port.id) > 0) {
712 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700713 break;
714 }
715 }
716 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700717 }
718 }
719 *_aidl_return = std::move(connectedPort);
720
Mikhail Naganov00603d12022-05-02 22:52:13 +0000721 return ndk::ScopedAStatus::ok();
722}
723
724ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
725 auto& ports = getConfig().ports;
726 auto portIt = findById<AudioPort>(ports, in_portId);
727 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530728 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000729 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
730 }
731 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530732 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
733 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000734 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
735 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700736 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
737 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530738 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
739 << " is not a connected device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000740 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
741 }
742 auto& configs = getConfig().portConfigs;
743 auto& initials = getConfig().initialConfigs;
744 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
745 if (config.portId == in_portId) {
746 // Check if the configuration was provided by the client.
747 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
748 return initialIt == initials.end() || config != *initialIt;
749 }
750 return false;
751 });
752 if (configIt != configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530753 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
754 << " has a non-default config with id " << configIt->id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000755 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
756 }
jiabin783c48b2023-02-28 18:28:06 +0000757 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000758 ports.erase(portIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +0530759 LOG(DEBUG) << __func__ << ": " << mType << ": connected device port " << in_portId
760 << " released";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000761
762 auto& routes = getConfig().routes;
763 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
764 if (routesIt->sinkPortId == in_portId) {
765 routesIt = routes.erase(routesIt);
766 } else {
767 // Note: the list of sourcePortIds can't become empty because there must
768 // be the id of the template port in the route.
769 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
770 ++routesIt;
771 }
772 }
773
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700774 // Clear profiles for mix ports that are not connected to any other ports.
775 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
776 mConnectedDevicePorts.erase(connectedPortsIt);
777 for (const auto& connectedPort : mConnectedDevicePorts) {
778 for (int32_t mixPortId : connectedPort.second) {
779 mixPortsToClear.erase(mixPortId);
780 }
781 }
782 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700783 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
784 if (mixPortIt != ports.end()) {
785 mixPortIt->profiles = {};
786 }
787 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700788
Mikhail Naganov00603d12022-05-02 22:52:13 +0000789 return ndk::ScopedAStatus::ok();
790}
791
jiabindd23b0e2023-12-11 19:10:05 +0000792ndk::ScopedAStatus Module::prepareToDisconnectExternalDevice(int32_t in_portId) {
793 auto& ports = getConfig().ports;
794 auto portIt = findById<AudioPort>(ports, in_portId);
795 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530796 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
jiabindd23b0e2023-12-11 19:10:05 +0000797 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
798 }
799 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530800 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
801 << " is not a device port";
jiabindd23b0e2023-12-11 19:10:05 +0000802 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
803 }
804 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
805 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530806 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
807 << " is not a connected device port";
jiabindd23b0e2023-12-11 19:10:05 +0000808 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
809 }
810
811 onPrepareToDisconnectExternalDevice(*portIt);
812
813 return ndk::ScopedAStatus::ok();
814}
815
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000816ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
817 *_aidl_return = getConfig().patches;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530818 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " patches";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000819 return ndk::ScopedAStatus::ok();
820}
821
822ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
823 auto& ports = getConfig().ports;
824 auto portIt = findById<AudioPort>(ports, in_portId);
825 if (portIt != ports.end()) {
826 *_aidl_return = *portIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530827 LOG(DEBUG) << __func__ << ": " << mType << ": returning port by id " << in_portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000828 return ndk::ScopedAStatus::ok();
829 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530830 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000831 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
832}
833
834ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
835 *_aidl_return = getConfig().portConfigs;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530836 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size()
837 << " port configs";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000838 return ndk::ScopedAStatus::ok();
839}
840
841ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
842 *_aidl_return = getConfig().ports;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530843 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " ports";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000844 return ndk::ScopedAStatus::ok();
845}
846
847ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
848 *_aidl_return = getConfig().routes;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530849 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " routes";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000850 return ndk::ScopedAStatus::ok();
851}
852
Mikhail Naganov00603d12022-05-02 22:52:13 +0000853ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
854 std::vector<AudioRoute>* _aidl_return) {
855 auto& ports = getConfig().ports;
856 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530857 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000858 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
859 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700860 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
861 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
862 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000863 return ndk::ScopedAStatus::ok();
864}
865
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000866ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
867 OpenInputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530868 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
869 << ", buffer size " << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000870 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700871 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000872 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530873 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000874 << " does not correspond to an input mix port";
875 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
876 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000877 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700878 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
879 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000880 context.fillDescriptor(&_aidl_return->desc);
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800881 if (hasMmapFlag(context.getFlags())) {
882 RETURN_STATUS_IF_ERROR(createMmapBuffer(context, &_aidl_return->desc));
883 }
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000884 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700885 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000886 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000887 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700888 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
889 RETURN_STATUS_IF_ERROR(
890 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
891 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +0000892 auto streamBinder = streamWrapper.getBinder();
893 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
894 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000895 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000896 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000897 return ndk::ScopedAStatus::ok();
898}
899
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000900ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
901 OpenOutputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530902 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
903 << ", has offload info? " << (in_args.offloadInfo.has_value()) << ", buffer size "
904 << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000905 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700906 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000907 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530908 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000909 << " does not correspond to an output mix port";
910 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
911 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000912 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
913 AudioOutputFlags::COMPRESS_OFFLOAD);
914 if (isOffload && !in_args.offloadInfo.has_value()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530915 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000916 << " has COMPRESS_OFFLOAD flag set, requires offload info";
917 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
918 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000919 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
920 AudioOutputFlags::NON_BLOCKING);
921 if (isNonBlocking && in_args.callback == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530922 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov30301a42022-09-13 01:20:45 +0000923 << " has NON_BLOCKING flag set, requires async callback";
924 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
925 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000926 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700927 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
928 isNonBlocking ? in_args.callback : nullptr,
929 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000930 context.fillDescriptor(&_aidl_return->desc);
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800931 if (hasMmapFlag(context.getFlags())) {
932 RETURN_STATUS_IF_ERROR(createMmapBuffer(context, &_aidl_return->desc));
933 }
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000934 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700935 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700936 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000937 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700938 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
939 RETURN_STATUS_IF_ERROR(
940 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
941 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +0000942 auto streamBinder = streamWrapper.getBinder();
943 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
944 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000945 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000946 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000947 return ndk::ScopedAStatus::ok();
948}
949
Mikhail Naganov74927202022-12-19 16:37:14 +0000950ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
951 SupportedPlaybackRateFactors* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530952 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganov74927202022-12-19 16:37:14 +0000953 (void)_aidl_return;
954 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
955}
956
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000957ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530958 LOG(DEBUG) << __func__ << ": " << mType << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000959 if (in_requested.sourcePortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530960 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000961 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
962 }
963 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530964 LOG(ERROR) << __func__ << ": " << mType
965 << ": requested patch has duplicate ids in the sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000966 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
967 }
968 if (in_requested.sinkPortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530969 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000970 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
971 }
972 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530973 LOG(ERROR) << __func__ << ": " << mType
974 << ": requested patch has duplicate ids in the sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000975 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
976 }
977
978 auto& configs = getConfig().portConfigs;
979 std::vector<int32_t> missingIds;
980 auto sources =
981 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
982 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530983 LOG(ERROR) << __func__ << ": " << mType << ": following source port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000984 << ::android::internal::ToString(missingIds);
985 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
986 }
987 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
988 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530989 LOG(ERROR) << __func__ << ": " << mType << ": following sink port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000990 << ::android::internal::ToString(missingIds);
991 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
992 }
993 // bool indicates whether a non-exclusive route is available.
994 // If only an exclusive route is available, that means the patch can not be
995 // established if there is any other patch which currently uses the sink port.
996 std::map<int32_t, bool> allowedSinkPorts;
997 auto& routes = getConfig().routes;
998 for (auto src : sources) {
999 for (const auto& r : routes) {
1000 const auto& srcs = r.sourcePortIds;
1001 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
1002 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
1003 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
1004 }
1005 }
1006 }
1007 }
1008 for (auto sink : sinks) {
1009 if (allowedSinkPorts.count(sink->portId) == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301010 LOG(ERROR) << __func__ << ": " << mType << ": there is no route to the sink port id "
1011 << sink->portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001012 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1013 }
1014 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001015 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +00001016
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001017 auto& patches = getConfig().patches;
1018 auto existing = patches.end();
1019 std::optional<decltype(mPatches)> patchesBackup;
1020 if (in_requested.id != 0) {
1021 existing = findById<AudioPatch>(patches, in_requested.id);
1022 if (existing != patches.end()) {
1023 patchesBackup = mPatches;
1024 cleanUpPatch(existing->id);
1025 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301026 LOG(ERROR) << __func__ << ": " << mType << ": not found existing patch id "
1027 << in_requested.id;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001028 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1029 }
1030 }
1031 // Validate the requested patch.
1032 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
1033 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301034 LOG(ERROR) << __func__ << ": " << mType << ": sink port id " << sinkPortId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001035 << "is exclusive and is already used by some other patch";
1036 if (patchesBackup.has_value()) {
1037 mPatches = std::move(*patchesBackup);
1038 }
1039 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1040 }
1041 }
Mikhail Naganov13501872023-10-18 16:15:46 -07001042 // Find the highest sample rate among mix port configs.
1043 std::map<int32_t, AudioPortConfig*> sampleRates;
1044 std::vector<AudioPortConfig*>& mixPortConfigs =
1045 sources[0]->ext.getTag() == AudioPortExt::mix ? sources : sinks;
1046 for (auto mix : mixPortConfigs) {
1047 sampleRates.emplace(mix->sampleRate.value().value, mix);
1048 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001049 *_aidl_return = in_requested;
Mikhail Naganov13501872023-10-18 16:15:46 -07001050 auto maxSampleRateIt = std::max_element(sampleRates.begin(), sampleRates.end());
1051 const int32_t latencyMs = getNominalLatencyMs(*(maxSampleRateIt->second));
1052 _aidl_return->minimumStreamBufferSizeFrames =
1053 calculateBufferSizeFrames(latencyMs, maxSampleRateIt->first);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001054 _aidl_return->latenciesMs.clear();
1055 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
Mikhail Naganov13501872023-10-18 16:15:46 -07001056 _aidl_return->sinkPortConfigIds.size(), latencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001057 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001058 if (existing == patches.end()) {
1059 _aidl_return->id = getConfig().nextPatchId++;
1060 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001061 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001062 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -07001063 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001064 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001065 patchesBackup = mPatches;
1066 registerPatch(*_aidl_return);
1067 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
1068 mPatches = std::move(*patchesBackup);
1069 if (existing == patches.end()) {
1070 patches.pop_back();
1071 } else {
1072 *existing = oldPatch;
1073 }
1074 return status;
1075 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001076
Jaideep Sharma559a4912024-03-07 10:05:51 +05301077 LOG(DEBUG) << __func__ << ": " << mType << ": " << (oldPatch.id == 0 ? "created" : "updated")
1078 << " patch " << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001079 return ndk::ScopedAStatus::ok();
1080}
1081
1082ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
1083 AudioPortConfig* out_suggested, bool* _aidl_return) {
Mikhail Naganova92039a2023-12-20 14:27:22 -08001084 auto generate = [this](const AudioPort& port, AudioPortConfig* config) {
1085 return generateDefaultPortConfig(port, config);
1086 };
1087 return setAudioPortConfigImpl(in_requested, generate, out_suggested, _aidl_return);
1088}
1089
1090ndk::ScopedAStatus Module::setAudioPortConfigImpl(
1091 const AudioPortConfig& in_requested,
1092 const std::function<bool(const ::aidl::android::media::audio::common::AudioPort& port,
1093 ::aidl::android::media::audio::common::AudioPortConfig* config)>&
1094 fillPortConfig,
1095 AudioPortConfig* out_suggested, bool* applied) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301096 LOG(DEBUG) << __func__ << ": " << mType << ": requested " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001097 auto& configs = getConfig().portConfigs;
1098 auto existing = configs.end();
1099 if (in_requested.id != 0) {
1100 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1101 existing == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301102 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id "
1103 << in_requested.id << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001104 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1105 }
1106 }
1107
1108 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1109 if (portId == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301110 LOG(ERROR) << __func__ << ": " << mType
1111 << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001112 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1113 }
1114 auto& ports = getConfig().ports;
1115 auto portIt = findById<AudioPort>(ports, portId);
1116 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301117 LOG(ERROR) << __func__ << ": " << mType
1118 << ": requested port config points to non-existent portId " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001119 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1120 }
1121 if (existing != configs.end()) {
1122 *out_suggested = *existing;
1123 } else {
1124 AudioPortConfig newConfig;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001125 newConfig.portId = portIt->id;
1126 if (fillPortConfig(*portIt, &newConfig)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001127 *out_suggested = newConfig;
1128 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301129 LOG(ERROR) << __func__ << ": " << mType
1130 << ": unable generate a default config for port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001131 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1132 }
1133 }
1134 // From this moment, 'out_suggested' is either an existing port config,
1135 // or a new generated config. Now attempt to update it according to the specified
1136 // fields of 'in_requested'.
1137
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001138 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1139 // which takes care of their actual configuration automatically.
1140 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1141 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001142 bool requestedIsValid = true, requestedIsFullySpecified = true;
1143
1144 AudioIoFlags portFlags = portIt->flags;
1145 if (in_requested.flags.has_value()) {
1146 if (in_requested.flags.value() != portFlags) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301147 LOG(WARNING) << __func__ << ": " << mType << ": requested flags "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001148 << in_requested.flags.value().toString() << " do not match port's "
1149 << portId << " flags " << portFlags.toString();
1150 requestedIsValid = false;
1151 }
1152 } else {
1153 requestedIsFullySpecified = false;
1154 }
1155
1156 AudioProfile portProfile;
1157 if (in_requested.format.has_value()) {
1158 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001159 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1160 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001161 out_suggested->format = format;
1162 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301163 LOG(WARNING) << __func__ << ": " << mType << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001164 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001165 requestedIsValid = false;
1166 }
1167 } else {
1168 requestedIsFullySpecified = false;
1169 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001170 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1171 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301172 LOG(ERROR) << __func__ << ": " << mType << ": port " << portId
1173 << " does not support format " << out_suggested->format.value().toString()
1174 << " anymore";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001175 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1176 }
1177
1178 if (in_requested.channelMask.has_value()) {
1179 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001180 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1181 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1182 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001183 out_suggested->channelMask = channelMask;
1184 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301185 LOG(WARNING) << __func__ << ": " << mType << ": requested channel mask "
1186 << channelMask.toString() << " is not supported for the format "
1187 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001188 requestedIsValid = false;
1189 }
1190 } else {
1191 requestedIsFullySpecified = false;
1192 }
1193
1194 if (in_requested.sampleRate.has_value()) {
1195 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001196 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1197 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001198 sampleRate.value) != portProfile.sampleRates.end()) {
1199 out_suggested->sampleRate = sampleRate;
1200 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301201 LOG(WARNING) << __func__ << ": " << mType << ": requested sample rate "
1202 << sampleRate.value << " is not supported for the format "
1203 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001204 requestedIsValid = false;
1205 }
1206 } else {
1207 requestedIsFullySpecified = false;
1208 }
1209
1210 if (in_requested.gain.has_value()) {
Weilin Xua33bb5e2024-10-02 17:16:42 +00001211 if (!setAudioPortConfigGain(*portIt, in_requested.gain.value())) {
1212 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1213 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001214 out_suggested->gain = in_requested.gain.value();
1215 }
1216
Mikhail Naganov248e9502023-02-21 16:32:40 -08001217 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1218 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1219 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
Mikhail Naganovb06a4922024-03-06 16:39:50 -08001220 // 'AudioMixPortExt.handle' and '.usecase' are set by the client,
1221 // copy from in_requested.
1222 const auto& src = in_requested.ext.get<AudioPortExt::Tag::mix>();
1223 auto& dst = out_suggested->ext.get<AudioPortExt::Tag::mix>();
1224 dst.handle = src.handle;
1225 dst.usecase = src.usecase;
Mikhail Naganov248e9502023-02-21 16:32:40 -08001226 }
1227 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301228 LOG(WARNING) << __func__ << ": " << mType << ": requested ext tag "
Mikhail Naganov248e9502023-02-21 16:32:40 -08001229 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1230 << toString(out_suggested->ext.getTag());
1231 requestedIsValid = false;
1232 }
1233 }
1234
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001235 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1236 out_suggested->id = getConfig().nextPortId++;
1237 configs.push_back(*out_suggested);
Mikhail Naganova92039a2023-12-20 14:27:22 -08001238 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301239 LOG(DEBUG) << __func__ << ": " << mType << ": created new port config "
1240 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001241 } else if (existing != configs.end() && requestedIsValid) {
1242 *existing = *out_suggested;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001243 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301244 LOG(DEBUG) << __func__ << ": " << mType << ": updated port config "
1245 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001246 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301247 LOG(DEBUG) << __func__ << ": " << mType << ": not applied; existing config ? "
1248 << (existing != configs.end()) << "; requested is valid? " << requestedIsValid
1249 << ", fully specified? " << requestedIsFullySpecified;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001250 *applied = false;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001251 }
1252 return ndk::ScopedAStatus::ok();
1253}
1254
Weilin Xua33bb5e2024-10-02 17:16:42 +00001255bool Module::setAudioPortConfigGain(const AudioPort& port, const AudioGainConfig& gainRequested) {
1256 auto& ports = getConfig().ports;
1257 if (gainRequested.index < 0 || gainRequested.index >= (int)port.gains.size()) {
1258 LOG(ERROR) << __func__ << ": gains for port " << port.id << " is undefined";
1259 return false;
1260 }
1261 int stepValue = port.gains[gainRequested.index].stepValue;
1262 if (stepValue == 0) {
1263 LOG(ERROR) << __func__ << ": port gain step value is 0";
1264 return false;
1265 }
1266 int minValue = port.gains[gainRequested.index].minValue;
1267 int maxValue = port.gains[gainRequested.index].maxValue;
1268 if (gainRequested.values[0] > maxValue || gainRequested.values[0] < minValue) {
1269 LOG(ERROR) << __func__ << ": gain value " << gainRequested.values[0]
1270 << " out of range of min and max gain config";
1271 return false;
1272 }
1273 int gainIndex = (gainRequested.values[0] - minValue) / stepValue;
1274 int totalSteps = (maxValue - minValue) / stepValue;
1275 if (totalSteps == 0) {
1276 LOG(ERROR) << __func__ << ": difference between port gain min value " << minValue
1277 << " and max value " << maxValue << " is less than step value " << stepValue;
1278 return false;
1279 }
1280 // Root-power quantities are used in curve:
1281 // 10^((minMb / 100 + (maxMb / 100 - minMb / 100) * gainIndex / totalSteps) / (10 * 2))
1282 // where 100 is the conversion from mB to dB, 10 comes from the log 10 conversion from power
1283 // ratios, and 2 means are the square of amplitude.
1284 float gain =
1285 pow(10, (minValue + (maxValue - minValue) * (gainIndex / (float)totalSteps)) / 2000);
1286 if (gain < 0) {
1287 LOG(ERROR) << __func__ << ": gain " << gain << " is less than 0";
1288 return false;
1289 }
1290 for (const auto& route : getConfig().routes) {
1291 if (route.sinkPortId != port.id) {
1292 continue;
1293 }
1294 for (const auto sourcePortId : route.sourcePortIds) {
1295 mStreams.setGain(sourcePortId, gain);
1296 }
1297 }
1298 return true;
1299}
1300
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001301ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1302 auto& patches = getConfig().patches;
1303 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1304 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001305 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001306 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001307 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1308 mPatches = std::move(patchesBackup);
1309 return status;
1310 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001311 patches.erase(patchIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301312 LOG(DEBUG) << __func__ << ": " << mType << ": erased patch " << in_patchId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001313 return ndk::ScopedAStatus::ok();
1314 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301315 LOG(ERROR) << __func__ << ": " << mType << ": patch id " << in_patchId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001316 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1317}
1318
1319ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1320 auto& configs = getConfig().portConfigs;
1321 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1322 if (configIt != configs.end()) {
1323 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301324 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001325 << " has a stream opened on it";
1326 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1327 }
1328 auto patchIt = mPatches.find(in_portConfigId);
1329 if (patchIt != mPatches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301330 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001331 << " is used by the patch with id " << patchIt->second;
1332 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1333 }
1334 auto& initials = getConfig().initialConfigs;
1335 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1336 if (initialIt == initials.end()) {
1337 configs.erase(configIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301338 LOG(DEBUG) << __func__ << ": " << mType << ": erased port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001339 } else if (*configIt != *initialIt) {
1340 *configIt = *initialIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301341 LOG(DEBUG) << __func__ << ": " << mType << ": reset port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001342 }
1343 return ndk::ScopedAStatus::ok();
1344 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301345 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
1346 << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001347 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1348}
1349
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001350ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1351 *_aidl_return = mMasterMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301352 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001353 return ndk::ScopedAStatus::ok();
1354}
1355
1356ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301357 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001358 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1359 : onMasterMuteChanged(in_mute);
1360 if (result.isOk()) {
1361 mMasterMute = in_mute;
1362 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301363 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterMuteChanged("
1364 << in_mute << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001365 // Reset master mute if it failed.
1366 onMasterMuteChanged(mMasterMute);
1367 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001368 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001369}
1370
1371ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1372 *_aidl_return = mMasterVolume;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301373 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001374 return ndk::ScopedAStatus::ok();
1375}
1376
1377ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301378 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001379 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001380 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1381 : onMasterVolumeChanged(in_volume);
1382 if (result.isOk()) {
1383 mMasterVolume = in_volume;
1384 } else {
1385 // Reset master volume if it failed.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301386 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterVolumeChanged("
1387 << in_volume << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001388 onMasterVolumeChanged(mMasterVolume);
1389 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001390 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001391 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301392 LOG(ERROR) << __func__ << ": " << mType << ": invalid master volume value: " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001393 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1394}
1395
1396ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1397 *_aidl_return = mMicMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301398 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001399 return ndk::ScopedAStatus::ok();
1400}
1401
1402ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301403 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001404 mMicMute = in_mute;
1405 return ndk::ScopedAStatus::ok();
1406}
1407
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001408ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001409 *_aidl_return = getMicrophoneInfos();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301410 LOG(DEBUG) << __func__ << ": " << mType << ": returning "
1411 << ::android::internal::ToString(*_aidl_return);
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001412 return ndk::ScopedAStatus::ok();
1413}
1414
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001415ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001416 if (!isValidAudioMode(in_mode)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301417 LOG(ERROR) << __func__ << ": " << mType << ": invalid mode " << toString(in_mode);
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001418 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1419 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001420 // No checks for supported audio modes here, it's an informative notification.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301421 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_mode);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001422 return ndk::ScopedAStatus::ok();
1423}
1424
1425ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301426 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_rotation);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001427 return ndk::ScopedAStatus::ok();
1428}
1429
1430ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301431 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_isTurnedOn;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001432 return ndk::ScopedAStatus::ok();
1433}
1434
Vlad Popa83a6d822022-11-07 13:53:57 +01001435ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001436 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001437 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001438 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001439 *_aidl_return = mSoundDose.getInstance();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301440 LOG(DEBUG) << __func__ << ": " << mType
1441 << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001442 return ndk::ScopedAStatus::ok();
1443}
1444
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001445ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301446 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001447 (void)_aidl_return;
1448 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1449}
1450
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001451const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001452const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001453
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001454ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1455 std::vector<VendorParameter>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301456 LOG(VERBOSE) << __func__ << ": " << mType << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001457 bool allParametersKnown = true;
1458 for (const auto& id : in_ids) {
1459 if (id == VendorDebug::kForceTransientBurstName) {
1460 VendorParameter forceTransientBurst{.id = id};
1461 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1462 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001463 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1464 VendorParameter forceSynchronousDrain{.id = id};
1465 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1466 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001467 } else {
1468 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301469 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << id << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001470 }
1471 }
1472 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1473 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001474}
1475
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001476namespace {
1477
1478template <typename W>
1479bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1480 std::optional<W> value;
1481 binder_status_t result = p.ext.getParcelable(&value);
1482 if (result == STATUS_OK && value.has_value()) {
1483 *v = value.value().value;
1484 return true;
1485 }
1486 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1487 << "\": " << result;
1488 return false;
1489}
1490
1491} // namespace
1492
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001493ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1494 bool in_async) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301495 LOG(VERBOSE) << __func__ << ": " << mType << ": parameter count " << in_parameters.size()
1496 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001497 bool allParametersKnown = true;
1498 for (const auto& p : in_parameters) {
1499 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001500 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1501 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1502 }
1503 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1504 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001505 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1506 }
1507 } else {
1508 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301509 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << p.id
1510 << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001511 }
1512 }
1513 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1514 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001515}
1516
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001517ndk::ScopedAStatus Module::addDeviceEffect(
1518 int32_t in_portConfigId,
1519 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1520 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301521 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1522 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001523 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301524 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1525 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001526 }
1527 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1528}
1529
1530ndk::ScopedAStatus Module::removeDeviceEffect(
1531 int32_t in_portConfigId,
1532 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1533 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301534 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1535 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001536 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301537 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1538 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001539 }
1540 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1541}
1542
jiabin9a8e6862023-01-12 23:06:37 +00001543ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1544 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301545 LOG(DEBUG) << __func__ << ": " << mType << ": mmap policy type " << toString(mmapPolicyType);
jiabin9a8e6862023-01-12 23:06:37 +00001546 std::set<int32_t> mmapSinks;
1547 std::set<int32_t> mmapSources;
1548 auto& ports = getConfig().ports;
1549 for (const auto& port : ports) {
1550 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1551 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1552 AudioInputFlags::MMAP_NOIRQ)) {
1553 mmapSinks.insert(port.id);
1554 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1555 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1556 AudioOutputFlags::MMAP_NOIRQ)) {
1557 mmapSources.insert(port.id);
1558 }
1559 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001560 if (mmapSources.empty() && mmapSinks.empty()) {
1561 AudioMMapPolicyInfo never;
1562 never.mmapPolicy = AudioMMapPolicy::NEVER;
1563 _aidl_return->push_back(never);
1564 return ndk::ScopedAStatus::ok();
1565 }
jiabin9a8e6862023-01-12 23:06:37 +00001566 for (const auto& route : getConfig().routes) {
1567 if (mmapSinks.count(route.sinkPortId) != 0) {
1568 // The sink is a mix port, add the sources if they are device ports.
1569 for (int sourcePortId : route.sourcePortIds) {
1570 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1571 if (sourcePortIt == ports.end()) {
1572 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301573 LOG(ERROR) << __func__ << ": " << mType << ": port id " << sourcePortId
1574 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001575 continue;
1576 }
1577 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1578 // The source is not a device port, skip
1579 continue;
1580 }
1581 AudioMMapPolicyInfo policyInfo;
1582 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1583 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1584 // default implementation.
1585 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1586 _aidl_return->push_back(policyInfo);
1587 }
1588 } else {
1589 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1590 if (sinkPortIt == ports.end()) {
1591 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301592 LOG(ERROR) << __func__ << ": " << mType << ": port id " << route.sinkPortId
1593 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001594 continue;
1595 }
1596 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1597 // The sink is not a device port, skip
1598 continue;
1599 }
1600 if (count_any(mmapSources, route.sourcePortIds)) {
1601 AudioMMapPolicyInfo policyInfo;
1602 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1603 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1604 // default implementation.
1605 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1606 _aidl_return->push_back(policyInfo);
1607 }
1608 }
1609 }
1610 return ndk::ScopedAStatus::ok();
1611}
1612
Eric Laurente2432ea2023-01-12 17:47:31 +01001613ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301614 LOG(DEBUG) << __func__ << ": " << mType;
Eric Laurente2432ea2023-01-12 17:47:31 +01001615 *_aidl_return = false;
1616 return ndk::ScopedAStatus::ok();
1617}
1618
jiabinb76981e2023-01-18 00:58:30 +00001619ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1620 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301621 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001622 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1623 }
1624 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301625 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001626 return ndk::ScopedAStatus::ok();
1627}
1628
1629ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1630 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301631 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001632 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1633 }
1634 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301635 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001636 return ndk::ScopedAStatus::ok();
1637}
1638
1639bool Module::isMmapSupported() {
1640 if (mIsMmapSupported.has_value()) {
1641 return mIsMmapSupported.value();
1642 }
1643 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1644 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1645 mIsMmapSupported = false;
1646 } else {
1647 mIsMmapSupported =
1648 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1649 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1650 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1651 }) != mmapPolicyInfos.end();
1652 }
1653 return mIsMmapSupported.value();
1654}
1655
Mikhail Naganova92039a2023-12-20 14:27:22 -08001656ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001657 if (audioPort->ext.getTag() != AudioPortExt::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301658 LOG(ERROR) << __func__ << ": " << mType << ": not a device port: " << audioPort->toString();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001659 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1660 }
1661 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1662 if (!devicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301663 LOG(ERROR) << __func__ << ": " << mType << ": module implementation must override "
1664 "'populateConnectedDevicePort' "
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001665 << "to handle connection of external devices.";
1666 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1667 }
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
1672ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1673 const std::vector<AudioPortConfig*>& sources __unused,
1674 const std::vector<AudioPortConfig*>& sinks __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301675 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001676 return ndk::ScopedAStatus::ok();
1677}
1678
jiabin783c48b2023-02-28 18:28:06 +00001679void Module::onExternalDeviceConnectionChanged(
1680 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1681 bool connected __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301682 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabin783c48b2023-02-28 18:28:06 +00001683}
1684
jiabindd23b0e2023-12-11 19:10:05 +00001685void Module::onPrepareToDisconnectExternalDevice(
1686 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301687 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabindd23b0e2023-12-11 19:10:05 +00001688}
1689
jiabin783c48b2023-02-28 18:28:06 +00001690ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301691 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001692 return ndk::ScopedAStatus::ok();
1693}
1694
1695ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301696 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001697 return ndk::ScopedAStatus::ok();
1698}
1699
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001700std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1701 std::vector<MicrophoneInfo> result;
1702 Configuration& config = getConfig();
1703 for (const AudioPort& port : config.ports) {
1704 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1705 const AudioDeviceType deviceType =
1706 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1707 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1708 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1709 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1710 // accordingly based on their physical microphone parameters.
1711 result.push_back(MicrophoneInfo{
1712 .id = port.name,
1713 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1714 .group = 0,
1715 .indexInTheGroup = 0,
1716 });
1717 }
1718 }
1719 }
1720 return result;
1721}
1722
Ram Mohan18f0d512023-07-01 00:47:09 +05301723ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1724 return mStreams.bluetoothParametersUpdated();
1725}
1726
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001727} // namespace aidl::android::hardware::audio::core