blob: 2be7f519fee357e76f649cf467c51bbc13d0dc65 [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();
Mikhail Naganove87a1b92024-11-12 15:31:55 -0800210 StreamContext::DebugParameters params{
211 mDebug.streamTransientStateDelayMs, mVendorDebug.forceTransientBurst,
212 mVendorDebug.forceSynchronousDrain, mVendorDebug.forceDrainToDraining};
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800213 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 };
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530470 auto restoreOldConnections = [&](const std::set<int32_t>& mixPortIds,
471 const bool continueWithEmptyDevices) {
472 for (const auto mixPort : mixPortIds) {
473 if (auto it = oldConnections.find(mixPort);
474 continueWithEmptyDevices || it != oldConnections.end()) {
475 const std::vector<AudioDevice> d =
476 it != oldConnections.end() ? getDevicesFromDevicePortConfigIds(it->second)
477 : std::vector<AudioDevice>();
478 if (auto status = mStreams.setStreamConnectedDevices(mixPort, d); status.isOk()) {
479 LOG(WARNING) << ":updateStreamsConnectedState: rollback: mix port config:"
480 << mixPort
481 << (d.empty() ? "; not connected"
482 : std::string("; connected to ") +
483 ::android::internal::ToString(d));
484 } else {
485 // can't do much about rollback failures
486 LOG(ERROR)
487 << ":updateStreamsConnectedState: rollback: failed for mix port config:"
488 << mixPort;
489 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700490 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000491 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530492 };
493 fillConnections(oldConnections, oldPatch);
494 fillConnections(newConnections, newPatch);
495 /**
496 * Illustration of oldConnections and newConnections
497 *
498 * oldConnections {
499 * a : {A,B,C},
500 * b : {D},
501 * d : {H,I,J},
502 * e : {N,O,P},
503 * f : {Q,R},
504 * g : {T,U,V},
505 * }
506 *
507 * newConnections {
508 * a : {A,B,C},
509 * c : {E,F,G},
510 * d : {K,L,M},
511 * e : {N,P},
512 * f : {Q,R,S},
513 * g : {U,V,W},
514 * }
515 *
516 * Expected routings:
517 * 'a': is ignored both in disconnect step and connect step,
518 * due to same devices both in oldConnections and newConnections.
519 * 'b': handled only in disconnect step with empty devices because 'b' is only present
520 * in oldConnections.
521 * 'c': handled only in connect step with {E,F,G} devices because 'c' is only present
522 * in newConnections.
523 * 'd': handled only in connect step with {K,L,M} devices because 'd' is also present
524 * in newConnections and it is ignored in disconnected step.
525 * 'e': handled only in connect step with {N,P} devices because 'e' is also present
526 * in newConnections and it is ignored in disconnect step. please note that there
527 * is no exclusive disconnection for device {O}.
528 * 'f': handled only in connect step with {Q,R,S} devices because 'f' is also present
529 * in newConnections and it is ignored in disconnect step. Even though stream is
530 * already connected with {Q,R} devices and connection happens with {Q,R,S}.
531 * 'g': handled only in connect step with {U,V,W} devices because 'g' is also present
532 * in newConnections and it is ignored in disconnect step. There is no exclusive
533 * disconnection with devices {T,U,V}.
534 *
535 * If, any failure, will lead to restoreOldConnections (rollback).
536 * The aim of the restoreOldConnections is to make connections back to oldConnections.
537 * Failures in restoreOldConnections aren't handled.
538 */
539
540 std::set<int32_t> idsToConnectBackOnFailure;
541 // disconnection step
542 for (const auto& [oldMixPortConfigId, oldDevicePortConfigIds] : oldConnections) {
543 if (auto it = newConnections.find(oldMixPortConfigId); it == newConnections.end()) {
544 idsToConnectBackOnFailure.insert(oldMixPortConfigId);
545 if (auto status = mStreams.setStreamConnectedDevices(oldMixPortConfigId, {});
546 status.isOk()) {
547 LOG(DEBUG) << __func__ << ": The stream on port config id " << oldMixPortConfigId
548 << " has been disconnected";
549 } else {
550 maybeFailure = std::move(status);
551 // proceed to rollback even on one failure
552 break;
553 }
554 }
555 }
556
557 if (!maybeFailure.isOk()) {
558 restoreOldConnections(idsToConnectBackOnFailure, false /*continueWithEmptyDevices*/);
559 LOG(WARNING) << __func__ << ": failed to disconnect from old patch. attempted rollback";
560 return maybeFailure;
561 }
562
563 std::set<int32_t> idsToRollbackOnFailure;
564 // connection step
565 for (const auto& [newMixPortConfigId, newDevicePortConfigIds] : newConnections) {
566 if (auto it = oldConnections.find(newMixPortConfigId);
567 it == oldConnections.end() || it->second != newDevicePortConfigIds) {
568 const auto connectedDevices = getDevicesFromDevicePortConfigIds(newDevicePortConfigIds);
569 idsToRollbackOnFailure.insert(newMixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700570 if (connectedDevices.empty()) {
571 // This is important as workers use the vector size to derive the connection status.
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530572 LOG(FATAL) << __func__ << ": No connected devices found for port config id "
573 << newMixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700574 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530575 if (auto status =
576 mStreams.setStreamConnectedDevices(newMixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700577 status.isOk()) {
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530578 LOG(DEBUG) << __func__ << ": The stream on port config id " << newMixPortConfigId
579 << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700580 << ::android::internal::ToString(connectedDevices);
581 } else {
582 maybeFailure = std::move(status);
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530583 // proceed to rollback even on one failure
584 break;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700585 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000586 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530587 }
588
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700589 if (!maybeFailure.isOk()) {
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530590 restoreOldConnections(idsToConnectBackOnFailure, false /*continueWithEmptyDevices*/);
591 restoreOldConnections(idsToRollbackOnFailure, true /*continueWithEmptyDevices*/);
592 LOG(WARNING) << __func__ << ": failed to connect for new patch. attempted rollback";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700593 return maybeFailure;
594 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530595
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700596 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000597}
598
Mikhail Naganov00603d12022-05-02 22:52:13 +0000599ndk::ScopedAStatus Module::setModuleDebug(
600 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700601 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000602 << ", new flags: " << in_debug.toString();
603 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
604 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700605 LOG(ERROR) << __func__ << ": " << mType
Jaideep Sharma559a4912024-03-07 10:05:51 +0530606 << ": attempting to change device connections simulation while "
607 "having external "
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700608 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000609 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
610 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000611 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700612 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000613 << in_debug.streamTransientStateDelayMs;
614 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
615 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000616 mDebug = in_debug;
617 return ndk::ScopedAStatus::ok();
618}
619
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000620ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700621 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530622 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000623 return ndk::ScopedAStatus::ok();
624}
625
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000626ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700627 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530628 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000629 return ndk::ScopedAStatus::ok();
630}
631
Mikhail Naganov7499a002023-02-27 18:51:44 -0800632ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700633 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530634 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800635 return ndk::ScopedAStatus::ok();
636}
637
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800638ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700639 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530640 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800641 return ndk::ScopedAStatus::ok();
642}
643
Mikhail Naganov00603d12022-05-02 22:52:13 +0000644ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
645 AudioPort* _aidl_return) {
646 const int32_t templateId = in_templateIdAndAdditionalData.id;
647 auto& ports = getConfig().ports;
648 AudioPort connectedPort;
649 { // Scope the template port so that we don't accidentally modify it.
650 auto templateIt = findById<AudioPort>(ports, templateId);
651 if (templateIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530652 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000653 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
654 }
655 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530656 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
657 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000658 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
659 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000660 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
661 if (templateDevicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530662 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
663 << " is permanently attached";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000664 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
665 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700666 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530667 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
668 << " is a connected device port";
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700669 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
670 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000671 // Postpone id allocation until we ensure that there are no client errors.
672 connectedPort = *templateIt;
673 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
674 const auto& inputDevicePort =
675 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
676 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
677 connectedDevicePort.device.address = inputDevicePort.device.address;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530678 LOG(DEBUG) << __func__ << ": " << mType << ": device port " << connectedPort.id
679 << " device set to " << connectedDevicePort.device.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000680 // Check if there is already a connected port with for the same external device.
Jaideep Sharma559a4912024-03-07 10:05:51 +0530681
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700682 for (auto connectedPortPair : mConnectedDevicePorts) {
683 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000684 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
685 connectedDevicePort.device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530686 LOG(ERROR) << __func__ << ": " << mType << ": device "
687 << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700688 << " is already connected at the device port id "
689 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000690 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
691 }
692 }
693 }
694
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700695 // Two main cases are considered with regard to the profiles of the connected device port:
696 //
697 // 1. If the template device port has dynamic profiles, and at least one routable mix
698 // port also has dynamic profiles, it means that after connecting the device, the
699 // connected device port must have profiles populated with actual capabilities of
700 // the connected device, and dynamic of routable mix ports will be filled
701 // according to these capabilities. An example of this case is connection of an
702 // HDMI or USB device. For USB handled by ADSP, there can be mix ports with static
703 // profiles, and one dedicated mix port for "hi-fi" playback. The latter is left with
704 // dynamic profiles so that they can be populated with actual capabilities of
705 // the connected device.
706 //
707 // 2. If the template device port has dynamic profiles, while all routable mix ports
708 // have static profiles, it means that after connecting the device, the connected
709 // device port can be left with dynamic profiles, and profiles of mix ports are
710 // left untouched. An example of this case is connection of an analog wired
711 // headset, it should be treated in the same way as a speaker.
712 //
713 // Yet another possible case is when both the template device port and all routable
714 // mix ports have static profiles. This is allowed and handled correctly, however, it
715 // is not very practical, since these profiles are likely duplicates of each other.
716
717 std::vector<AudioRoute*> routesToMixPorts = getAudioRoutesForAudioPortImpl(templateId);
718 std::set<int32_t> routableMixPortIds = getRoutableAudioPortIds(templateId, &routesToMixPorts);
Mikhail Naganova92039a2023-12-20 14:27:22 -0800719 const int32_t nextPortId = getConfig().nextPortId++;
Mikhail Naganov55045b52023-10-24 17:03:50 -0700720 if (!mDebug.simulateDeviceConnections) {
721 // Even if the device port has static profiles, the HAL module might need to update
722 // them, or abort the connection process.
Mikhail Naganova92039a2023-12-20 14:27:22 -0800723 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort, nextPortId));
Mikhail Naganov55045b52023-10-24 17:03:50 -0700724 } else if (hasDynamicProfilesOnly(connectedPort.profiles)) {
725 auto& connectedProfiles = getConfig().connectedProfiles;
726 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
727 connectedProfilesIt != connectedProfiles.end()) {
728 connectedPort.profiles = connectedProfilesIt->second;
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700729 }
Mikhail Naganov55045b52023-10-24 17:03:50 -0700730 }
731 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
732 // Possible case 2. Check if all routable mix ports have static profiles.
733 if (auto dynamicMixPortIt = std::find_if(ports.begin(), ports.end(),
734 [&routableMixPortIds](const auto& p) {
735 return routableMixPortIds.count(p.id) > 0 &&
736 hasDynamicProfilesOnly(p.profiles);
737 });
738 dynamicMixPortIt != ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530739 LOG(ERROR) << __func__ << ": " << mType
740 << ": connected port only has dynamic profiles after connecting "
Mikhail Naganov55045b52023-10-24 17:03:50 -0700741 << "external device " << connectedPort.toString() << ", and there exist "
742 << "a routable mix port with dynamic profiles: "
743 << dynamicMixPortIt->toString();
744 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530745 }
746 }
747
Mikhail Naganova92039a2023-12-20 14:27:22 -0800748 connectedPort.id = nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700749 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700750 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Jaideep Sharma559a4912024-03-07 10:05:51 +0530751 LOG(DEBUG) << __func__ << ": " << mType << ": template port " << templateId
752 << " external device connected, "
Mikhail Naganov00603d12022-05-02 22:52:13 +0000753 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000754 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000755 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000756
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700757 // For routes where the template port is a source, add the connected port to sources,
758 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000759 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700760 for (AudioRoute* r : routesToMixPorts) {
761 if (r->sinkPortId == templateId) {
762 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
763 .sinkPortId = connectedPort.id,
764 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000765 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700766 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000767 }
768 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700769 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000770 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
771
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700772 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
773 // Note: this is a simplistic approach assuming that a mix port can only be populated
774 // from a single device port. Implementing support for stuffing dynamic profiles with
775 // a superset of all profiles from all routable dynamic device ports would be more involved.
776 for (auto& port : ports) {
777 if (routableMixPortIds.count(port.id) == 0) continue;
778 if (hasDynamicProfilesOnly(port.profiles)) {
779 port.profiles = connectedPort.profiles;
780 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700781 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700782 // Check if profiles are not all dynamic because they were populated by
783 // a previous connection. Otherwise, it means that they are actually static.
784 for (const auto& cp : mConnectedDevicePorts) {
785 if (cp.second.count(port.id) > 0) {
786 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700787 break;
788 }
789 }
790 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700791 }
792 }
793 *_aidl_return = std::move(connectedPort);
794
Mikhail Naganov00603d12022-05-02 22:52:13 +0000795 return ndk::ScopedAStatus::ok();
796}
797
798ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
799 auto& ports = getConfig().ports;
800 auto portIt = findById<AudioPort>(ports, in_portId);
801 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530802 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000803 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
804 }
805 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530806 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
807 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000808 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
809 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700810 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
811 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530812 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
813 << " is not a connected device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000814 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
815 }
816 auto& configs = getConfig().portConfigs;
817 auto& initials = getConfig().initialConfigs;
818 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
819 if (config.portId == in_portId) {
820 // Check if the configuration was provided by the client.
821 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
822 return initialIt == initials.end() || config != *initialIt;
823 }
824 return false;
825 });
826 if (configIt != configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530827 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
828 << " has a non-default config with id " << configIt->id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000829 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
830 }
jiabin783c48b2023-02-28 18:28:06 +0000831 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000832 ports.erase(portIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +0530833 LOG(DEBUG) << __func__ << ": " << mType << ": connected device port " << in_portId
834 << " released";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000835
836 auto& routes = getConfig().routes;
837 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
838 if (routesIt->sinkPortId == in_portId) {
839 routesIt = routes.erase(routesIt);
840 } else {
841 // Note: the list of sourcePortIds can't become empty because there must
842 // be the id of the template port in the route.
843 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
844 ++routesIt;
845 }
846 }
847
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700848 // Clear profiles for mix ports that are not connected to any other ports.
849 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
850 mConnectedDevicePorts.erase(connectedPortsIt);
851 for (const auto& connectedPort : mConnectedDevicePorts) {
852 for (int32_t mixPortId : connectedPort.second) {
853 mixPortsToClear.erase(mixPortId);
854 }
855 }
856 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700857 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
858 if (mixPortIt != ports.end()) {
859 mixPortIt->profiles = {};
860 }
861 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700862
Mikhail Naganov00603d12022-05-02 22:52:13 +0000863 return ndk::ScopedAStatus::ok();
864}
865
jiabindd23b0e2023-12-11 19:10:05 +0000866ndk::ScopedAStatus Module::prepareToDisconnectExternalDevice(int32_t in_portId) {
867 auto& ports = getConfig().ports;
868 auto portIt = findById<AudioPort>(ports, in_portId);
869 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530870 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
jiabindd23b0e2023-12-11 19:10:05 +0000871 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
872 }
873 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530874 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
875 << " is not a device port";
jiabindd23b0e2023-12-11 19:10:05 +0000876 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
877 }
878 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
879 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530880 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
881 << " is not a connected device port";
jiabindd23b0e2023-12-11 19:10:05 +0000882 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
883 }
884
885 onPrepareToDisconnectExternalDevice(*portIt);
886
887 return ndk::ScopedAStatus::ok();
888}
889
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000890ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
891 *_aidl_return = getConfig().patches;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530892 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " patches";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000893 return ndk::ScopedAStatus::ok();
894}
895
896ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
897 auto& ports = getConfig().ports;
898 auto portIt = findById<AudioPort>(ports, in_portId);
899 if (portIt != ports.end()) {
900 *_aidl_return = *portIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530901 LOG(DEBUG) << __func__ << ": " << mType << ": returning port by id " << in_portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000902 return ndk::ScopedAStatus::ok();
903 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530904 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000905 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
906}
907
908ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
909 *_aidl_return = getConfig().portConfigs;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530910 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size()
911 << " port configs";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000912 return ndk::ScopedAStatus::ok();
913}
914
915ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
916 *_aidl_return = getConfig().ports;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530917 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " ports";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000918 return ndk::ScopedAStatus::ok();
919}
920
921ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
922 *_aidl_return = getConfig().routes;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530923 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " routes";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000924 return ndk::ScopedAStatus::ok();
925}
926
Mikhail Naganov00603d12022-05-02 22:52:13 +0000927ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
928 std::vector<AudioRoute>* _aidl_return) {
929 auto& ports = getConfig().ports;
930 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530931 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000932 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
933 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700934 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
935 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
936 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000937 return ndk::ScopedAStatus::ok();
938}
939
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000940ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
941 OpenInputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530942 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
943 << ", buffer size " << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000944 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700945 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000946 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530947 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000948 << " does not correspond to an input mix port";
949 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
950 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000951 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700952 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
953 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000954 context.fillDescriptor(&_aidl_return->desc);
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800955 if (hasMmapFlag(context.getFlags())) {
956 RETURN_STATUS_IF_ERROR(createMmapBuffer(context, &_aidl_return->desc));
957 }
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000958 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700959 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000960 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000961 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700962 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
963 RETURN_STATUS_IF_ERROR(
964 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
965 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +0000966 auto streamBinder = streamWrapper.getBinder();
967 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
968 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000969 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000970 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000971 return ndk::ScopedAStatus::ok();
972}
973
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000974ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
975 OpenOutputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530976 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
977 << ", has offload info? " << (in_args.offloadInfo.has_value()) << ", buffer size "
978 << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000979 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700980 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000981 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530982 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000983 << " does not correspond to an output mix port";
984 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
985 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000986 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
987 AudioOutputFlags::COMPRESS_OFFLOAD);
988 if (isOffload && !in_args.offloadInfo.has_value()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530989 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000990 << " has COMPRESS_OFFLOAD flag set, requires offload info";
991 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
992 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000993 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
994 AudioOutputFlags::NON_BLOCKING);
995 if (isNonBlocking && in_args.callback == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530996 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov30301a42022-09-13 01:20:45 +0000997 << " has NON_BLOCKING flag set, requires async callback";
998 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
999 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001000 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001001 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
1002 isNonBlocking ? in_args.callback : nullptr,
1003 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001004 context.fillDescriptor(&_aidl_return->desc);
Kuowei Li53a8d4d2024-06-24 14:35:07 +08001005 if (hasMmapFlag(context.getFlags())) {
1006 RETURN_STATUS_IF_ERROR(createMmapBuffer(context, &_aidl_return->desc));
1007 }
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001008 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -07001009 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -07001010 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001011 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001012 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
1013 RETURN_STATUS_IF_ERROR(
1014 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
1015 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +00001016 auto streamBinder = streamWrapper.getBinder();
1017 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
1018 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001019 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001020 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001021 return ndk::ScopedAStatus::ok();
1022}
1023
Mikhail Naganov74927202022-12-19 16:37:14 +00001024ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
1025 SupportedPlaybackRateFactors* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301026 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganov74927202022-12-19 16:37:14 +00001027 (void)_aidl_return;
1028 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1029}
1030
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001031ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301032 LOG(DEBUG) << __func__ << ": " << mType << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001033 if (in_requested.sourcePortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301034 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001035 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1036 }
1037 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301038 LOG(ERROR) << __func__ << ": " << mType
1039 << ": requested patch has duplicate ids in the sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001040 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1041 }
1042 if (in_requested.sinkPortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301043 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001044 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1045 }
1046 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301047 LOG(ERROR) << __func__ << ": " << mType
1048 << ": requested patch has duplicate ids in the sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001049 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1050 }
1051
Ajender Reddydac3abf2024-12-04 17:21:46 +05301052 auto& patches = getConfig().patches;
1053 auto existing = patches.end();
1054 if (in_requested.id != 0) {
1055 existing = findById<AudioPatch>(patches, in_requested.id);
1056 if (existing == patches.end()) {
1057 LOG(ERROR) << __func__ << ": " << mType << ": not found existing patch id "
1058 << in_requested.id;
1059 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1060 }
1061 if (in_requested == *existing) {
1062 LOG(INFO) << __func__ << ": " << mType
1063 << ": requested patch has no changes compared to already existing one, id "
1064 << in_requested.id;
1065 return ndk::ScopedAStatus::ok();
1066 }
1067 }
1068
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001069 auto& configs = getConfig().portConfigs;
1070 std::vector<int32_t> missingIds;
1071 auto sources =
1072 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
1073 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301074 LOG(ERROR) << __func__ << ": " << mType << ": following source port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001075 << ::android::internal::ToString(missingIds);
1076 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1077 }
1078 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
1079 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301080 LOG(ERROR) << __func__ << ": " << mType << ": following sink port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001081 << ::android::internal::ToString(missingIds);
1082 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1083 }
1084 // bool indicates whether a non-exclusive route is available.
1085 // If only an exclusive route is available, that means the patch can not be
1086 // established if there is any other patch which currently uses the sink port.
1087 std::map<int32_t, bool> allowedSinkPorts;
1088 auto& routes = getConfig().routes;
1089 for (auto src : sources) {
1090 for (const auto& r : routes) {
1091 const auto& srcs = r.sourcePortIds;
1092 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
1093 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
1094 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
1095 }
1096 }
1097 }
1098 }
1099 for (auto sink : sinks) {
1100 if (allowedSinkPorts.count(sink->portId) == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301101 LOG(ERROR) << __func__ << ": " << mType << ": there is no route to the sink port id "
1102 << sink->portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001103 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1104 }
1105 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001106 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +00001107
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001108 std::optional<decltype(mPatches)> patchesBackup;
Ajender Reddydac3abf2024-12-04 17:21:46 +05301109 if (existing != patches.end()) {
1110 patchesBackup = mPatches;
1111 cleanUpPatch(existing->id);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001112 }
Ajender Reddydac3abf2024-12-04 17:21:46 +05301113
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001114 // Validate the requested patch.
1115 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
1116 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301117 LOG(ERROR) << __func__ << ": " << mType << ": sink port id " << sinkPortId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001118 << "is exclusive and is already used by some other patch";
1119 if (patchesBackup.has_value()) {
1120 mPatches = std::move(*patchesBackup);
1121 }
1122 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1123 }
1124 }
Mikhail Naganov13501872023-10-18 16:15:46 -07001125 // Find the highest sample rate among mix port configs.
1126 std::map<int32_t, AudioPortConfig*> sampleRates;
1127 std::vector<AudioPortConfig*>& mixPortConfigs =
1128 sources[0]->ext.getTag() == AudioPortExt::mix ? sources : sinks;
1129 for (auto mix : mixPortConfigs) {
1130 sampleRates.emplace(mix->sampleRate.value().value, mix);
1131 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001132 *_aidl_return = in_requested;
Mikhail Naganov13501872023-10-18 16:15:46 -07001133 auto maxSampleRateIt = std::max_element(sampleRates.begin(), sampleRates.end());
1134 const int32_t latencyMs = getNominalLatencyMs(*(maxSampleRateIt->second));
1135 _aidl_return->minimumStreamBufferSizeFrames =
1136 calculateBufferSizeFrames(latencyMs, maxSampleRateIt->first);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001137 _aidl_return->latenciesMs.clear();
1138 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
Mikhail Naganov13501872023-10-18 16:15:46 -07001139 _aidl_return->sinkPortConfigIds.size(), latencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001140 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001141 if (existing == patches.end()) {
1142 _aidl_return->id = getConfig().nextPatchId++;
1143 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001144 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001145 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -07001146 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001147 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001148 patchesBackup = mPatches;
1149 registerPatch(*_aidl_return);
1150 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
1151 mPatches = std::move(*patchesBackup);
1152 if (existing == patches.end()) {
1153 patches.pop_back();
1154 } else {
1155 *existing = oldPatch;
1156 }
1157 return status;
1158 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001159
Jaideep Sharma559a4912024-03-07 10:05:51 +05301160 LOG(DEBUG) << __func__ << ": " << mType << ": " << (oldPatch.id == 0 ? "created" : "updated")
1161 << " patch " << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001162 return ndk::ScopedAStatus::ok();
1163}
1164
1165ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
1166 AudioPortConfig* out_suggested, bool* _aidl_return) {
Mikhail Naganova92039a2023-12-20 14:27:22 -08001167 auto generate = [this](const AudioPort& port, AudioPortConfig* config) {
1168 return generateDefaultPortConfig(port, config);
1169 };
1170 return setAudioPortConfigImpl(in_requested, generate, out_suggested, _aidl_return);
1171}
1172
1173ndk::ScopedAStatus Module::setAudioPortConfigImpl(
1174 const AudioPortConfig& in_requested,
1175 const std::function<bool(const ::aidl::android::media::audio::common::AudioPort& port,
1176 ::aidl::android::media::audio::common::AudioPortConfig* config)>&
1177 fillPortConfig,
1178 AudioPortConfig* out_suggested, bool* applied) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301179 LOG(DEBUG) << __func__ << ": " << mType << ": requested " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001180 auto& configs = getConfig().portConfigs;
1181 auto existing = configs.end();
1182 if (in_requested.id != 0) {
1183 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1184 existing == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301185 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id "
1186 << in_requested.id << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001187 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1188 }
1189 }
1190
1191 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1192 if (portId == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301193 LOG(ERROR) << __func__ << ": " << mType
1194 << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001195 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1196 }
1197 auto& ports = getConfig().ports;
1198 auto portIt = findById<AudioPort>(ports, portId);
1199 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301200 LOG(ERROR) << __func__ << ": " << mType
1201 << ": requested port config points to non-existent portId " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001202 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1203 }
1204 if (existing != configs.end()) {
1205 *out_suggested = *existing;
1206 } else {
1207 AudioPortConfig newConfig;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001208 newConfig.portId = portIt->id;
1209 if (fillPortConfig(*portIt, &newConfig)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001210 *out_suggested = newConfig;
1211 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301212 LOG(ERROR) << __func__ << ": " << mType
1213 << ": unable generate a default config for port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001214 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1215 }
1216 }
1217 // From this moment, 'out_suggested' is either an existing port config,
1218 // or a new generated config. Now attempt to update it according to the specified
1219 // fields of 'in_requested'.
1220
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001221 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1222 // which takes care of their actual configuration automatically.
1223 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1224 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001225 bool requestedIsValid = true, requestedIsFullySpecified = true;
1226
1227 AudioIoFlags portFlags = portIt->flags;
1228 if (in_requested.flags.has_value()) {
1229 if (in_requested.flags.value() != portFlags) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301230 LOG(WARNING) << __func__ << ": " << mType << ": requested flags "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001231 << in_requested.flags.value().toString() << " do not match port's "
1232 << portId << " flags " << portFlags.toString();
1233 requestedIsValid = false;
1234 }
1235 } else {
1236 requestedIsFullySpecified = false;
1237 }
1238
1239 AudioProfile portProfile;
1240 if (in_requested.format.has_value()) {
1241 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001242 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1243 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001244 out_suggested->format = format;
1245 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301246 LOG(WARNING) << __func__ << ": " << mType << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001247 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001248 requestedIsValid = false;
1249 }
1250 } else {
1251 requestedIsFullySpecified = false;
1252 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001253 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1254 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301255 LOG(ERROR) << __func__ << ": " << mType << ": port " << portId
1256 << " does not support format " << out_suggested->format.value().toString()
1257 << " anymore";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001258 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1259 }
1260
1261 if (in_requested.channelMask.has_value()) {
1262 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001263 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1264 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1265 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001266 out_suggested->channelMask = channelMask;
1267 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301268 LOG(WARNING) << __func__ << ": " << mType << ": requested channel mask "
1269 << channelMask.toString() << " is not supported for the format "
1270 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001271 requestedIsValid = false;
1272 }
1273 } else {
1274 requestedIsFullySpecified = false;
1275 }
1276
1277 if (in_requested.sampleRate.has_value()) {
1278 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001279 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1280 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001281 sampleRate.value) != portProfile.sampleRates.end()) {
1282 out_suggested->sampleRate = sampleRate;
1283 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301284 LOG(WARNING) << __func__ << ": " << mType << ": requested sample rate "
1285 << sampleRate.value << " is not supported for the format "
1286 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001287 requestedIsValid = false;
1288 }
1289 } else {
1290 requestedIsFullySpecified = false;
1291 }
1292
1293 if (in_requested.gain.has_value()) {
Weilin Xua33bb5e2024-10-02 17:16:42 +00001294 if (!setAudioPortConfigGain(*portIt, in_requested.gain.value())) {
1295 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1296 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001297 out_suggested->gain = in_requested.gain.value();
1298 }
1299
Mikhail Naganov248e9502023-02-21 16:32:40 -08001300 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1301 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1302 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
Mikhail Naganovb06a4922024-03-06 16:39:50 -08001303 // 'AudioMixPortExt.handle' and '.usecase' are set by the client,
1304 // copy from in_requested.
1305 const auto& src = in_requested.ext.get<AudioPortExt::Tag::mix>();
1306 auto& dst = out_suggested->ext.get<AudioPortExt::Tag::mix>();
1307 dst.handle = src.handle;
1308 dst.usecase = src.usecase;
Mikhail Naganov248e9502023-02-21 16:32:40 -08001309 }
1310 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301311 LOG(WARNING) << __func__ << ": " << mType << ": requested ext tag "
Mikhail Naganov248e9502023-02-21 16:32:40 -08001312 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1313 << toString(out_suggested->ext.getTag());
1314 requestedIsValid = false;
1315 }
1316 }
1317
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001318 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1319 out_suggested->id = getConfig().nextPortId++;
1320 configs.push_back(*out_suggested);
Mikhail Naganova92039a2023-12-20 14:27:22 -08001321 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301322 LOG(DEBUG) << __func__ << ": " << mType << ": created new port config "
1323 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001324 } else if (existing != configs.end() && requestedIsValid) {
1325 *existing = *out_suggested;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001326 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301327 LOG(DEBUG) << __func__ << ": " << mType << ": updated port config "
1328 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001329 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301330 LOG(DEBUG) << __func__ << ": " << mType << ": not applied; existing config ? "
1331 << (existing != configs.end()) << "; requested is valid? " << requestedIsValid
1332 << ", fully specified? " << requestedIsFullySpecified;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001333 *applied = false;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001334 }
1335 return ndk::ScopedAStatus::ok();
1336}
1337
Weilin Xua33bb5e2024-10-02 17:16:42 +00001338bool Module::setAudioPortConfigGain(const AudioPort& port, const AudioGainConfig& gainRequested) {
1339 auto& ports = getConfig().ports;
1340 if (gainRequested.index < 0 || gainRequested.index >= (int)port.gains.size()) {
1341 LOG(ERROR) << __func__ << ": gains for port " << port.id << " is undefined";
1342 return false;
1343 }
1344 int stepValue = port.gains[gainRequested.index].stepValue;
1345 if (stepValue == 0) {
1346 LOG(ERROR) << __func__ << ": port gain step value is 0";
1347 return false;
1348 }
1349 int minValue = port.gains[gainRequested.index].minValue;
1350 int maxValue = port.gains[gainRequested.index].maxValue;
1351 if (gainRequested.values[0] > maxValue || gainRequested.values[0] < minValue) {
1352 LOG(ERROR) << __func__ << ": gain value " << gainRequested.values[0]
1353 << " out of range of min and max gain config";
1354 return false;
1355 }
1356 int gainIndex = (gainRequested.values[0] - minValue) / stepValue;
1357 int totalSteps = (maxValue - minValue) / stepValue;
1358 if (totalSteps == 0) {
1359 LOG(ERROR) << __func__ << ": difference between port gain min value " << minValue
1360 << " and max value " << maxValue << " is less than step value " << stepValue;
1361 return false;
1362 }
1363 // Root-power quantities are used in curve:
1364 // 10^((minMb / 100 + (maxMb / 100 - minMb / 100) * gainIndex / totalSteps) / (10 * 2))
1365 // where 100 is the conversion from mB to dB, 10 comes from the log 10 conversion from power
1366 // ratios, and 2 means are the square of amplitude.
1367 float gain =
1368 pow(10, (minValue + (maxValue - minValue) * (gainIndex / (float)totalSteps)) / 2000);
1369 if (gain < 0) {
1370 LOG(ERROR) << __func__ << ": gain " << gain << " is less than 0";
1371 return false;
1372 }
1373 for (const auto& route : getConfig().routes) {
1374 if (route.sinkPortId != port.id) {
1375 continue;
1376 }
1377 for (const auto sourcePortId : route.sourcePortIds) {
1378 mStreams.setGain(sourcePortId, gain);
1379 }
1380 }
1381 return true;
1382}
1383
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001384ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1385 auto& patches = getConfig().patches;
1386 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1387 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001388 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001389 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001390 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1391 mPatches = std::move(patchesBackup);
1392 return status;
1393 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001394 patches.erase(patchIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301395 LOG(DEBUG) << __func__ << ": " << mType << ": erased patch " << in_patchId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001396 return ndk::ScopedAStatus::ok();
1397 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301398 LOG(ERROR) << __func__ << ": " << mType << ": patch id " << in_patchId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001399 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1400}
1401
1402ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1403 auto& configs = getConfig().portConfigs;
1404 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1405 if (configIt != configs.end()) {
1406 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301407 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001408 << " has a stream opened on it";
1409 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1410 }
1411 auto patchIt = mPatches.find(in_portConfigId);
1412 if (patchIt != mPatches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301413 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001414 << " is used by the patch with id " << patchIt->second;
1415 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1416 }
1417 auto& initials = getConfig().initialConfigs;
1418 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1419 if (initialIt == initials.end()) {
1420 configs.erase(configIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301421 LOG(DEBUG) << __func__ << ": " << mType << ": erased port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001422 } else if (*configIt != *initialIt) {
1423 *configIt = *initialIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301424 LOG(DEBUG) << __func__ << ": " << mType << ": reset port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001425 }
1426 return ndk::ScopedAStatus::ok();
1427 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301428 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
1429 << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001430 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1431}
1432
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001433ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1434 *_aidl_return = mMasterMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301435 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001436 return ndk::ScopedAStatus::ok();
1437}
1438
1439ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301440 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001441 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1442 : onMasterMuteChanged(in_mute);
1443 if (result.isOk()) {
1444 mMasterMute = in_mute;
1445 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301446 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterMuteChanged("
1447 << in_mute << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001448 // Reset master mute if it failed.
1449 onMasterMuteChanged(mMasterMute);
1450 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001451 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001452}
1453
1454ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1455 *_aidl_return = mMasterVolume;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301456 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001457 return ndk::ScopedAStatus::ok();
1458}
1459
1460ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301461 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001462 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001463 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1464 : onMasterVolumeChanged(in_volume);
1465 if (result.isOk()) {
1466 mMasterVolume = in_volume;
1467 } else {
1468 // Reset master volume if it failed.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301469 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterVolumeChanged("
1470 << in_volume << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001471 onMasterVolumeChanged(mMasterVolume);
1472 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001473 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001474 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301475 LOG(ERROR) << __func__ << ": " << mType << ": invalid master volume value: " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001476 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1477}
1478
1479ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1480 *_aidl_return = mMicMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301481 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001482 return ndk::ScopedAStatus::ok();
1483}
1484
1485ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301486 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001487 mMicMute = in_mute;
1488 return ndk::ScopedAStatus::ok();
1489}
1490
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001491ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001492 *_aidl_return = getMicrophoneInfos();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301493 LOG(DEBUG) << __func__ << ": " << mType << ": returning "
1494 << ::android::internal::ToString(*_aidl_return);
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001495 return ndk::ScopedAStatus::ok();
1496}
1497
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001498ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001499 if (!isValidAudioMode(in_mode)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301500 LOG(ERROR) << __func__ << ": " << mType << ": invalid mode " << toString(in_mode);
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001501 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1502 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001503 // No checks for supported audio modes here, it's an informative notification.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301504 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_mode);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001505 return ndk::ScopedAStatus::ok();
1506}
1507
1508ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301509 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_rotation);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001510 return ndk::ScopedAStatus::ok();
1511}
1512
1513ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301514 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_isTurnedOn;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001515 return ndk::ScopedAStatus::ok();
1516}
1517
Vlad Popa83a6d822022-11-07 13:53:57 +01001518ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001519 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001520 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001521 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001522 *_aidl_return = mSoundDose.getInstance();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301523 LOG(DEBUG) << __func__ << ": " << mType
1524 << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001525 return ndk::ScopedAStatus::ok();
1526}
1527
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001528ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301529 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001530 (void)_aidl_return;
1531 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1532}
1533
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001534const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001535const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganove87a1b92024-11-12 15:31:55 -08001536const std::string Module::VendorDebug::kForceDrainToDrainingName = "aosp.forceDrainToDraining";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001537
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001538ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1539 std::vector<VendorParameter>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301540 LOG(VERBOSE) << __func__ << ": " << mType << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001541 bool allParametersKnown = true;
1542 for (const auto& id : in_ids) {
1543 if (id == VendorDebug::kForceTransientBurstName) {
1544 VendorParameter forceTransientBurst{.id = id};
1545 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1546 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001547 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1548 VendorParameter forceSynchronousDrain{.id = id};
1549 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1550 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganove87a1b92024-11-12 15:31:55 -08001551 } else if (id == VendorDebug::kForceDrainToDrainingName) {
1552 VendorParameter forceDrainToDraining{.id = id};
1553 forceDrainToDraining.ext.setParcelable(Boolean{mVendorDebug.forceDrainToDraining});
1554 _aidl_return->push_back(std::move(forceDrainToDraining));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001555 } else {
1556 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301557 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << id << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001558 }
1559 }
1560 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1561 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001562}
1563
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001564namespace {
1565
1566template <typename W>
1567bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1568 std::optional<W> value;
1569 binder_status_t result = p.ext.getParcelable(&value);
1570 if (result == STATUS_OK && value.has_value()) {
1571 *v = value.value().value;
1572 return true;
1573 }
1574 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1575 << "\": " << result;
1576 return false;
1577}
1578
1579} // namespace
1580
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001581ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1582 bool in_async) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301583 LOG(VERBOSE) << __func__ << ": " << mType << ": parameter count " << in_parameters.size()
1584 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001585 bool allParametersKnown = true;
1586 for (const auto& p : in_parameters) {
1587 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001588 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1589 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1590 }
1591 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1592 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001593 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1594 }
Mikhail Naganove87a1b92024-11-12 15:31:55 -08001595 } else if (p.id == VendorDebug::kForceDrainToDrainingName) {
1596 if (!extractParameter<Boolean>(p, &mVendorDebug.forceDrainToDraining)) {
1597 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1598 }
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001599 } else {
1600 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301601 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << p.id
1602 << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001603 }
1604 }
1605 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1606 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001607}
1608
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001609ndk::ScopedAStatus Module::addDeviceEffect(
1610 int32_t in_portConfigId,
1611 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1612 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301613 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1614 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001615 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301616 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1617 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001618 }
1619 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1620}
1621
1622ndk::ScopedAStatus Module::removeDeviceEffect(
1623 int32_t in_portConfigId,
1624 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1625 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301626 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1627 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001628 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301629 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1630 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001631 }
1632 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1633}
1634
jiabin9a8e6862023-01-12 23:06:37 +00001635ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1636 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301637 LOG(DEBUG) << __func__ << ": " << mType << ": mmap policy type " << toString(mmapPolicyType);
jiabin9a8e6862023-01-12 23:06:37 +00001638 std::set<int32_t> mmapSinks;
1639 std::set<int32_t> mmapSources;
1640 auto& ports = getConfig().ports;
1641 for (const auto& port : ports) {
1642 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1643 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1644 AudioInputFlags::MMAP_NOIRQ)) {
1645 mmapSinks.insert(port.id);
1646 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1647 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1648 AudioOutputFlags::MMAP_NOIRQ)) {
1649 mmapSources.insert(port.id);
1650 }
1651 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001652 if (mmapSources.empty() && mmapSinks.empty()) {
1653 AudioMMapPolicyInfo never;
1654 never.mmapPolicy = AudioMMapPolicy::NEVER;
1655 _aidl_return->push_back(never);
1656 return ndk::ScopedAStatus::ok();
1657 }
jiabin9a8e6862023-01-12 23:06:37 +00001658 for (const auto& route : getConfig().routes) {
1659 if (mmapSinks.count(route.sinkPortId) != 0) {
1660 // The sink is a mix port, add the sources if they are device ports.
1661 for (int sourcePortId : route.sourcePortIds) {
1662 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1663 if (sourcePortIt == ports.end()) {
1664 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301665 LOG(ERROR) << __func__ << ": " << mType << ": port id " << sourcePortId
1666 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001667 continue;
1668 }
1669 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1670 // The source is not a device port, skip
1671 continue;
1672 }
1673 AudioMMapPolicyInfo policyInfo;
1674 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1675 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1676 // default implementation.
1677 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1678 _aidl_return->push_back(policyInfo);
1679 }
1680 } else {
1681 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1682 if (sinkPortIt == ports.end()) {
1683 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301684 LOG(ERROR) << __func__ << ": " << mType << ": port id " << route.sinkPortId
1685 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001686 continue;
1687 }
1688 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1689 // The sink is not a device port, skip
1690 continue;
1691 }
1692 if (count_any(mmapSources, route.sourcePortIds)) {
1693 AudioMMapPolicyInfo policyInfo;
1694 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1695 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1696 // default implementation.
1697 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1698 _aidl_return->push_back(policyInfo);
1699 }
1700 }
1701 }
1702 return ndk::ScopedAStatus::ok();
1703}
1704
Eric Laurente2432ea2023-01-12 17:47:31 +01001705ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301706 LOG(DEBUG) << __func__ << ": " << mType;
Eric Laurente2432ea2023-01-12 17:47:31 +01001707 *_aidl_return = false;
1708 return ndk::ScopedAStatus::ok();
1709}
1710
jiabinb76981e2023-01-18 00:58:30 +00001711ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1712 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301713 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001714 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1715 }
1716 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301717 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001718 return ndk::ScopedAStatus::ok();
1719}
1720
1721ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1722 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301723 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001724 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1725 }
1726 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301727 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001728 return ndk::ScopedAStatus::ok();
1729}
1730
1731bool Module::isMmapSupported() {
1732 if (mIsMmapSupported.has_value()) {
1733 return mIsMmapSupported.value();
1734 }
1735 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1736 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1737 mIsMmapSupported = false;
1738 } else {
1739 mIsMmapSupported =
1740 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1741 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1742 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1743 }) != mmapPolicyInfos.end();
1744 }
1745 return mIsMmapSupported.value();
1746}
1747
Mikhail Naganova92039a2023-12-20 14:27:22 -08001748ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001749 if (audioPort->ext.getTag() != AudioPortExt::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301750 LOG(ERROR) << __func__ << ": " << mType << ": not a device port: " << audioPort->toString();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001751 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1752 }
1753 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1754 if (!devicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301755 LOG(ERROR) << __func__ << ": " << mType << ": module implementation must override "
1756 "'populateConnectedDevicePort' "
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001757 << "to handle connection of external devices.";
1758 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1759 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301760 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001761 return ndk::ScopedAStatus::ok();
1762}
1763
1764ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1765 const std::vector<AudioPortConfig*>& sources __unused,
1766 const std::vector<AudioPortConfig*>& sinks __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301767 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001768 return ndk::ScopedAStatus::ok();
1769}
1770
jiabin783c48b2023-02-28 18:28:06 +00001771void Module::onExternalDeviceConnectionChanged(
1772 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1773 bool connected __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301774 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabin783c48b2023-02-28 18:28:06 +00001775}
1776
jiabindd23b0e2023-12-11 19:10:05 +00001777void Module::onPrepareToDisconnectExternalDevice(
1778 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301779 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabindd23b0e2023-12-11 19:10:05 +00001780}
1781
jiabin783c48b2023-02-28 18:28:06 +00001782ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301783 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001784 return ndk::ScopedAStatus::ok();
1785}
1786
1787ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301788 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001789 return ndk::ScopedAStatus::ok();
1790}
1791
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001792std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1793 std::vector<MicrophoneInfo> result;
1794 Configuration& config = getConfig();
1795 for (const AudioPort& port : config.ports) {
1796 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1797 const AudioDeviceType deviceType =
1798 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1799 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1800 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1801 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1802 // accordingly based on their physical microphone parameters.
1803 result.push_back(MicrophoneInfo{
1804 .id = port.name,
1805 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1806 .group = 0,
1807 .indexInTheGroup = 0,
1808 });
1809 }
1810 }
1811 }
1812 return result;
1813}
1814
Ram Mohan18f0d512023-07-01 00:47:09 +05301815ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1816 return mStreams.bluetoothParametersUpdated();
1817}
1818
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001819} // namespace aidl::android::hardware::audio::core