blob: aa624ff0eee298fe5fd47691f8d75861be882f67 [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.
Kuowei Li10a768a2024-12-09 17:44:40 +0800187 int32_t minimumStreamBufferSizeFrames = 0;
188 if (!calculateBufferSizeFrames(
189 portConfigIt->format.value(), nominalLatencyMs,
190 portConfigIt->sampleRate.value().value, &minimumStreamBufferSizeFrames).isOk()) {
191 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
192 }
Mikhail Naganov13501872023-10-18 16:15:46 -0700193 if (in_bufferSizeFrames < minimumStreamBufferSizeFrames) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530194 LOG(ERROR) << __func__ << ": " << mType << ": insufficient buffer size "
195 << in_bufferSizeFrames << ", must be at least " << minimumStreamBufferSizeFrames;
Mikhail Naganov13501872023-10-18 16:15:46 -0700196 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
197 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000198 const size_t frameSize =
199 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
200 if (frameSize == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530201 LOG(ERROR) << __func__ << ": " << mType
202 << ": could not calculate frame size for port config "
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000203 << portConfigIt->toString();
204 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
205 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530206 LOG(DEBUG) << __func__ << ": " << mType << ": frame size " << frameSize << " bytes";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700207 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530208 LOG(ERROR) << __func__ << ": " << mType << ": buffer size " << in_bufferSizeFrames
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000209 << " frames is too large, maximum size is "
210 << kMaximumStreamBufferSizeBytes / frameSize;
211 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
212 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000213 const auto& flags = portConfigIt->flags.value();
Mikhail Naganovefc2b322025-02-12 16:08:55 -0800214 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
215 mVendorDebug.forceTransientBurst,
216 mVendorDebug.forceSynchronousDrain};
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800217 std::shared_ptr<ISoundDose> soundDose;
218 if (!getSoundDose(&soundDose).isOk()) {
219 LOG(ERROR) << __func__ << ": could not create sound dose instance";
220 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
221 }
Mikhail Naganov78ad4822025-03-10 12:46:49 -0700222 StreamContext temp;
223 if (hasMmapFlag(flags)) {
224 MmapBufferDescriptor mmapDesc;
225 RETURN_STATUS_IF_ERROR(
226 createMmapBuffer(*portConfigIt, in_bufferSizeFrames, frameSize, &mmapDesc));
227 temp = StreamContext(
228 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
229 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
230 portConfigIt->format.value(), portConfigIt->channelMask.value(),
231 portConfigIt->sampleRate.value().value, flags, nominalLatencyMs,
232 portConfigIt->ext.get<AudioPortExt::mix>().handle, std::move(mmapDesc),
233 outEventCallback, mSoundDose.getInstance(), params);
234 } else {
235 temp = StreamContext(
236 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
237 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
238 portConfigIt->format.value(), portConfigIt->channelMask.value(),
239 portConfigIt->sampleRate.value().value, flags, nominalLatencyMs,
240 portConfigIt->ext.get<AudioPortExt::mix>().handle,
241 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
242 asyncCallback, outEventCallback, mSoundDose.getInstance(), params);
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800243 }
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800244 if (temp.isValid()) {
245 *out_context = std::move(temp);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000246 } else {
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800247 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000248 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000249 return ndk::ScopedAStatus::ok();
250}
251
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530252std::vector<AudioDevice> Module::getDevicesFromDevicePortConfigIds(
253 const std::set<int32_t>& devicePortConfigIds) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000254 std::vector<AudioDevice> result;
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530255 auto& configs = getConfig().portConfigs;
256 for (const auto& id : devicePortConfigIds) {
257 auto it = findById<AudioPortConfig>(configs, id);
258 if (it != configs.end() && it->ext.getTag() == AudioPortExt::Tag::device) {
259 result.push_back(it->ext.template get<AudioPortExt::Tag::device>().device);
260 } else {
261 LOG(FATAL) << __func__ << ": " << mType
262 << ": failed to find device for id" << id;
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000263 }
264 }
265 return result;
266}
267
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530268std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
269 return getDevicesFromDevicePortConfigIds(findConnectedPortConfigIds(portConfigId));
270}
271
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000272std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
273 std::set<int32_t> result;
274 auto patchIdsRange = mPatches.equal_range(portConfigId);
275 auto& patches = getConfig().patches;
276 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
277 auto patchIt = findById<AudioPatch>(patches, it->second);
278 if (patchIt == patches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530279 LOG(FATAL) << __func__ << ": " << mType << ": patch with id " << it->second
280 << " taken from mPatches "
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000281 << "not found in the configuration";
282 }
283 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
284 portConfigId) != patchIt->sourcePortConfigIds.end()) {
285 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
286 } else {
287 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
288 }
289 }
290 return result;
291}
292
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000293ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
294 auto& configs = getConfig().portConfigs;
295 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
296 if (portConfigIt == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530297 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id " << in_portConfigId
298 << " not found";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000299 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
300 }
301 const int32_t portId = portConfigIt->portId;
302 // In our implementation, configs of mix ports always have unique IDs.
303 CHECK(portId != in_portConfigId);
304 auto& ports = getConfig().ports;
305 auto portIt = findById<AudioPort>(ports, portId);
306 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530307 LOG(ERROR) << __func__ << ": " << mType << ": port id " << portId
308 << " used by port config id " << in_portConfigId << " not found";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000309 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
310 }
311 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530312 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000313 << " already has a stream opened on it";
314 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
315 }
316 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530317 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000318 << " does not correspond to a mix port";
319 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
320 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700321 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000322 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530323 LOG(ERROR) << __func__ << ": " << mType << ": port id " << portId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000324 << " has already reached maximum allowed opened stream count: "
325 << maxOpenStreamCount;
326 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
327 }
328 *port = &(*portIt);
329 return ndk::ScopedAStatus::ok();
330}
331
Mikhail Naganova92039a2023-12-20 14:27:22 -0800332bool Module::generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
333 const bool allowDynamicConfig = port.ext.getTag() == AudioPortExt::device;
334 for (const auto& profile : port.profiles) {
335 if (isDynamicProfile(profile)) continue;
336 config->format = profile.format;
337 config->channelMask = *profile.channelMasks.begin();
338 config->sampleRate = Int{.value = *profile.sampleRates.begin()};
339 config->flags = port.flags;
340 config->ext = port.ext;
341 return true;
342 }
343 if (allowDynamicConfig) {
344 config->format = AudioFormatDescription{};
345 config->channelMask = AudioChannelLayout{};
346 config->sampleRate = Int{.value = 0};
347 config->flags = port.flags;
348 config->ext = port.ext;
349 return true;
350 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530351 LOG(ERROR) << __func__ << ": " << mType << ": port " << port.id << " only has dynamic profiles";
Mikhail Naganova92039a2023-12-20 14:27:22 -0800352 return false;
353}
354
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000355void Module::populateConnectedProfiles() {
356 Configuration& config = getConfig();
357 for (const AudioPort& port : config.ports) {
358 if (port.ext.getTag() == AudioPortExt::device) {
359 if (auto devicePort = port.ext.get<AudioPortExt::device>();
360 !devicePort.device.type.connection.empty() && port.profiles.empty()) {
361 if (auto connIt = config.connectedProfiles.find(port.id);
362 connIt == config.connectedProfiles.end()) {
363 config.connectedProfiles.emplace(
364 port.id, internal::getStandard16And24BitPcmAudioProfiles());
365 }
366 }
367 }
368 }
369}
370
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000371template <typename C>
372std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
373 std::set<int32_t> result;
374 auto& portConfigs = getConfig().portConfigs;
375 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
376 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
377 if (portConfigIt != portConfigs.end()) {
378 result.insert(portConfigIt->portId);
379 }
380 }
381 return result;
382}
383
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000384std::unique_ptr<Module::Configuration> Module::initializeConfig() {
385 return internal::getConfiguration(getType());
Peter Yoon918a6a52023-07-13 17:04:37 +0900386}
387
Mikhail Naganov13501872023-10-18 16:15:46 -0700388int32_t Module::getNominalLatencyMs(const AudioPortConfig&) {
389 // Arbitrary value. Implementations must override this method to provide their actual latency.
390 static constexpr int32_t kLatencyMs = 5;
391 return kLatencyMs;
392}
393
Kuowei Li10a768a2024-12-09 17:44:40 +0800394ndk::ScopedAStatus Module::calculateBufferSizeFrames(
395 const ::aidl::android::media::audio::common::AudioFormatDescription &format,
396 int32_t latencyMs, int32_t sampleRateHz, int32_t *bufferSizeFrames) {
397 if (format.type == AudioFormatType::PCM) {
398 *bufferSizeFrames = calculateBufferSizeFramesForPcm(latencyMs, sampleRateHz);
399 return ndk::ScopedAStatus::ok();
400 }
401 LOG(ERROR) << __func__ << ": " << mType << ": format " << format.toString()
402 << " is not supported";
403 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
404}
405
Mikhail Naganov78ad4822025-03-10 12:46:49 -0700406ndk::ScopedAStatus Module::createMmapBuffer(const AudioPortConfig& portConfig __unused,
407 int32_t bufferSizeFrames __unused,
408 int32_t frameSizeBytes __unused,
409 MmapBufferDescriptor* desc __unused) {
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800410 LOG(ERROR) << __func__ << ": " << mType << ": is not implemented";
411 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
412}
413
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700414std::vector<AudioRoute*> Module::getAudioRoutesForAudioPortImpl(int32_t portId) {
415 std::vector<AudioRoute*> result;
416 auto& routes = getConfig().routes;
417 for (auto& r : routes) {
418 const auto& srcs = r.sourcePortIds;
419 if (r.sinkPortId == portId || std::find(srcs.begin(), srcs.end(), portId) != srcs.end()) {
420 result.push_back(&r);
421 }
422 }
423 return result;
424}
425
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000426Module::Configuration& Module::getConfig() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000427 if (!mConfig) {
Yi Konge62f97f2024-08-14 01:52:04 +0800428 mConfig = initializeConfig();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000429 }
430 return *mConfig;
431}
432
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700433std::set<int32_t> Module::getRoutableAudioPortIds(int32_t portId,
434 std::vector<AudioRoute*>* routes) {
435 std::vector<AudioRoute*> routesStorage;
436 if (routes == nullptr) {
437 routesStorage = getAudioRoutesForAudioPortImpl(portId);
438 routes = &routesStorage;
439 }
440 std::set<int32_t> result;
441 for (AudioRoute* r : *routes) {
442 if (r->sinkPortId == portId) {
443 result.insert(r->sourcePortIds.begin(), r->sourcePortIds.end());
444 } else {
445 result.insert(r->sinkPortId);
446 }
447 }
448 return result;
449}
450
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000451void Module::registerPatch(const AudioPatch& patch) {
452 auto& configs = getConfig().portConfigs;
453 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
454 for (auto portConfigId : portConfigIds) {
455 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
456 if (configIt != configs.end()) {
457 mPatches.insert(std::pair{portConfigId, patch.id});
458 if (configIt->portId != portConfigId) {
459 mPatches.insert(std::pair{configIt->portId, patch.id});
460 }
461 }
462 };
463 };
464 do_insert(patch.sourcePortConfigIds);
465 do_insert(patch.sinkPortConfigIds);
466}
467
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700468ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
469 const AudioPatch& newPatch) {
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700470 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700471 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700472 using Connections =
473 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
474 Connections oldConnections, newConnections;
475 auto fillConnectionsHelper = [&](Connections& connections,
476 const std::vector<int32_t>& mixPortCfgIds,
477 const std::vector<int32_t>& devicePortCfgIds) {
478 for (int32_t mixPortCfgId : mixPortCfgIds) {
479 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
480 }
481 };
482 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
483 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
484 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
485 patch.sourcePortConfigIds.end()) {
486 // Sources are mix ports.
487 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
488 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
489 [&](int32_t portConfigId) {
490 return mStreams.count(portConfigId) > 0;
491 }) != patch.sinkPortConfigIds.end()) {
492 // Sources are device ports.
493 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
494 } // Otherwise, there are no streams to notify.
495 };
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530496 auto restoreOldConnections = [&](const std::set<int32_t>& mixPortIds,
497 const bool continueWithEmptyDevices) {
498 for (const auto mixPort : mixPortIds) {
499 if (auto it = oldConnections.find(mixPort);
500 continueWithEmptyDevices || it != oldConnections.end()) {
501 const std::vector<AudioDevice> d =
502 it != oldConnections.end() ? getDevicesFromDevicePortConfigIds(it->second)
503 : std::vector<AudioDevice>();
504 if (auto status = mStreams.setStreamConnectedDevices(mixPort, d); status.isOk()) {
505 LOG(WARNING) << ":updateStreamsConnectedState: rollback: mix port config:"
506 << mixPort
507 << (d.empty() ? "; not connected"
508 : std::string("; connected to ") +
509 ::android::internal::ToString(d));
510 } else {
511 // can't do much about rollback failures
512 LOG(ERROR)
513 << ":updateStreamsConnectedState: rollback: failed for mix port config:"
514 << mixPort;
515 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700516 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000517 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530518 };
519 fillConnections(oldConnections, oldPatch);
520 fillConnections(newConnections, newPatch);
521 /**
522 * Illustration of oldConnections and newConnections
523 *
524 * oldConnections {
525 * a : {A,B,C},
526 * b : {D},
527 * d : {H,I,J},
528 * e : {N,O,P},
529 * f : {Q,R},
530 * g : {T,U,V},
531 * }
532 *
533 * newConnections {
534 * a : {A,B,C},
535 * c : {E,F,G},
536 * d : {K,L,M},
537 * e : {N,P},
538 * f : {Q,R,S},
539 * g : {U,V,W},
540 * }
541 *
542 * Expected routings:
543 * 'a': is ignored both in disconnect step and connect step,
544 * due to same devices both in oldConnections and newConnections.
545 * 'b': handled only in disconnect step with empty devices because 'b' is only present
546 * in oldConnections.
547 * 'c': handled only in connect step with {E,F,G} devices because 'c' is only present
548 * in newConnections.
549 * 'd': handled only in connect step with {K,L,M} devices because 'd' is also present
550 * in newConnections and it is ignored in disconnected step.
551 * 'e': handled only in connect step with {N,P} devices because 'e' is also present
552 * in newConnections and it is ignored in disconnect step. please note that there
553 * is no exclusive disconnection for device {O}.
554 * 'f': handled only in connect step with {Q,R,S} devices because 'f' is also present
555 * in newConnections and it is ignored in disconnect step. Even though stream is
556 * already connected with {Q,R} devices and connection happens with {Q,R,S}.
557 * 'g': handled only in connect step with {U,V,W} devices because 'g' is also present
558 * in newConnections and it is ignored in disconnect step. There is no exclusive
559 * disconnection with devices {T,U,V}.
560 *
561 * If, any failure, will lead to restoreOldConnections (rollback).
562 * The aim of the restoreOldConnections is to make connections back to oldConnections.
563 * Failures in restoreOldConnections aren't handled.
564 */
565
566 std::set<int32_t> idsToConnectBackOnFailure;
567 // disconnection step
568 for (const auto& [oldMixPortConfigId, oldDevicePortConfigIds] : oldConnections) {
569 if (auto it = newConnections.find(oldMixPortConfigId); it == newConnections.end()) {
570 idsToConnectBackOnFailure.insert(oldMixPortConfigId);
571 if (auto status = mStreams.setStreamConnectedDevices(oldMixPortConfigId, {});
572 status.isOk()) {
573 LOG(DEBUG) << __func__ << ": The stream on port config id " << oldMixPortConfigId
574 << " has been disconnected";
575 } else {
576 maybeFailure = std::move(status);
577 // proceed to rollback even on one failure
578 break;
579 }
580 }
581 }
582
583 if (!maybeFailure.isOk()) {
584 restoreOldConnections(idsToConnectBackOnFailure, false /*continueWithEmptyDevices*/);
585 LOG(WARNING) << __func__ << ": failed to disconnect from old patch. attempted rollback";
586 return maybeFailure;
587 }
588
589 std::set<int32_t> idsToRollbackOnFailure;
590 // connection step
591 for (const auto& [newMixPortConfigId, newDevicePortConfigIds] : newConnections) {
592 if (auto it = oldConnections.find(newMixPortConfigId);
593 it == oldConnections.end() || it->second != newDevicePortConfigIds) {
594 const auto connectedDevices = getDevicesFromDevicePortConfigIds(newDevicePortConfigIds);
595 idsToRollbackOnFailure.insert(newMixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700596 if (connectedDevices.empty()) {
597 // This is important as workers use the vector size to derive the connection status.
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530598 LOG(FATAL) << __func__ << ": No connected devices found for port config id "
599 << newMixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700600 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530601 if (auto status =
602 mStreams.setStreamConnectedDevices(newMixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700603 status.isOk()) {
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530604 LOG(DEBUG) << __func__ << ": The stream on port config id " << newMixPortConfigId
605 << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700606 << ::android::internal::ToString(connectedDevices);
607 } else {
608 maybeFailure = std::move(status);
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530609 // proceed to rollback even on one failure
610 break;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700611 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000612 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530613 }
614
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700615 if (!maybeFailure.isOk()) {
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530616 restoreOldConnections(idsToConnectBackOnFailure, false /*continueWithEmptyDevices*/);
617 restoreOldConnections(idsToRollbackOnFailure, true /*continueWithEmptyDevices*/);
618 LOG(WARNING) << __func__ << ": failed to connect for new patch. attempted rollback";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700619 return maybeFailure;
620 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530621
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700622 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000623}
624
Kuowei Li450f5402025-02-26 14:25:28 +0800625binder_status_t Module::dump(int fd, const char** args, uint32_t numArgs) {
626 for (const auto& portConfig : getConfig().portConfigs) {
627 if (portConfig.ext.getTag() == AudioPortExt::Tag::mix) {
628 getStreams().dump(portConfig.id, fd, args, numArgs);
629 }
630 }
631 return STATUS_OK;
632}
633
Mikhail Naganov00603d12022-05-02 22:52:13 +0000634ndk::ScopedAStatus Module::setModuleDebug(
635 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700636 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000637 << ", new flags: " << in_debug.toString();
638 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
639 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700640 LOG(ERROR) << __func__ << ": " << mType
Jaideep Sharma559a4912024-03-07 10:05:51 +0530641 << ": attempting to change device connections simulation while "
642 "having external "
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700643 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000644 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
645 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000646 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700647 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000648 << in_debug.streamTransientStateDelayMs;
649 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
650 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000651 mDebug = in_debug;
652 return ndk::ScopedAStatus::ok();
653}
654
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000655ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700656 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530657 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000658 return ndk::ScopedAStatus::ok();
659}
660
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000661ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700662 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530663 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000664 return ndk::ScopedAStatus::ok();
665}
666
Mikhail Naganov7499a002023-02-27 18:51:44 -0800667ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700668 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530669 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800670 return ndk::ScopedAStatus::ok();
671}
672
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800673ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700674 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530675 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800676 return ndk::ScopedAStatus::ok();
677}
678
Mikhail Naganov00603d12022-05-02 22:52:13 +0000679ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
680 AudioPort* _aidl_return) {
681 const int32_t templateId = in_templateIdAndAdditionalData.id;
682 auto& ports = getConfig().ports;
683 AudioPort connectedPort;
684 { // Scope the template port so that we don't accidentally modify it.
685 auto templateIt = findById<AudioPort>(ports, templateId);
686 if (templateIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530687 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000688 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
689 }
690 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530691 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
692 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000693 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
694 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000695 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
696 if (templateDevicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530697 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
698 << " is permanently attached";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000699 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
700 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700701 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530702 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
703 << " is a connected device port";
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700704 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
705 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000706 // Postpone id allocation until we ensure that there are no client errors.
707 connectedPort = *templateIt;
708 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
709 const auto& inputDevicePort =
710 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
711 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
712 connectedDevicePort.device.address = inputDevicePort.device.address;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530713 LOG(DEBUG) << __func__ << ": " << mType << ": device port " << connectedPort.id
714 << " device set to " << connectedDevicePort.device.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000715 // Check if there is already a connected port with for the same external device.
Jaideep Sharma559a4912024-03-07 10:05:51 +0530716
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700717 for (auto connectedPortPair : mConnectedDevicePorts) {
718 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000719 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
720 connectedDevicePort.device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530721 LOG(ERROR) << __func__ << ": " << mType << ": device "
722 << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700723 << " is already connected at the device port id "
724 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000725 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
726 }
727 }
728 }
729
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700730 // Two main cases are considered with regard to the profiles of the connected device port:
731 //
732 // 1. If the template device port has dynamic profiles, and at least one routable mix
733 // port also has dynamic profiles, it means that after connecting the device, the
734 // connected device port must have profiles populated with actual capabilities of
735 // the connected device, and dynamic of routable mix ports will be filled
736 // according to these capabilities. An example of this case is connection of an
737 // HDMI or USB device. For USB handled by ADSP, there can be mix ports with static
738 // profiles, and one dedicated mix port for "hi-fi" playback. The latter is left with
739 // dynamic profiles so that they can be populated with actual capabilities of
740 // the connected device.
741 //
742 // 2. If the template device port has dynamic profiles, while all routable mix ports
743 // have static profiles, it means that after connecting the device, the connected
744 // device port can be left with dynamic profiles, and profiles of mix ports are
745 // left untouched. An example of this case is connection of an analog wired
746 // headset, it should be treated in the same way as a speaker.
747 //
748 // Yet another possible case is when both the template device port and all routable
749 // mix ports have static profiles. This is allowed and handled correctly, however, it
750 // is not very practical, since these profiles are likely duplicates of each other.
751
752 std::vector<AudioRoute*> routesToMixPorts = getAudioRoutesForAudioPortImpl(templateId);
753 std::set<int32_t> routableMixPortIds = getRoutableAudioPortIds(templateId, &routesToMixPorts);
Mikhail Naganova92039a2023-12-20 14:27:22 -0800754 const int32_t nextPortId = getConfig().nextPortId++;
Mikhail Naganov55045b52023-10-24 17:03:50 -0700755 if (!mDebug.simulateDeviceConnections) {
756 // Even if the device port has static profiles, the HAL module might need to update
757 // them, or abort the connection process.
Mikhail Naganova92039a2023-12-20 14:27:22 -0800758 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort, nextPortId));
Mikhail Naganov55045b52023-10-24 17:03:50 -0700759 } else if (hasDynamicProfilesOnly(connectedPort.profiles)) {
760 auto& connectedProfiles = getConfig().connectedProfiles;
761 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
762 connectedProfilesIt != connectedProfiles.end()) {
763 connectedPort.profiles = connectedProfilesIt->second;
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700764 }
Mikhail Naganov55045b52023-10-24 17:03:50 -0700765 }
766 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
767 // Possible case 2. Check if all routable mix ports have static profiles.
768 if (auto dynamicMixPortIt = std::find_if(ports.begin(), ports.end(),
769 [&routableMixPortIds](const auto& p) {
770 return routableMixPortIds.count(p.id) > 0 &&
771 hasDynamicProfilesOnly(p.profiles);
772 });
773 dynamicMixPortIt != ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530774 LOG(ERROR) << __func__ << ": " << mType
775 << ": connected port only has dynamic profiles after connecting "
Mikhail Naganov55045b52023-10-24 17:03:50 -0700776 << "external device " << connectedPort.toString() << ", and there exist "
777 << "a routable mix port with dynamic profiles: "
778 << dynamicMixPortIt->toString();
779 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530780 }
781 }
782
Mikhail Naganova92039a2023-12-20 14:27:22 -0800783 connectedPort.id = nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700784 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700785 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Jaideep Sharma559a4912024-03-07 10:05:51 +0530786 LOG(DEBUG) << __func__ << ": " << mType << ": template port " << templateId
787 << " external device connected, "
Mikhail Naganov00603d12022-05-02 22:52:13 +0000788 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000789 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000790 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000791
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700792 // For routes where the template port is a source, add the connected port to sources,
793 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000794 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700795 for (AudioRoute* r : routesToMixPorts) {
796 if (r->sinkPortId == templateId) {
797 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
798 .sinkPortId = connectedPort.id,
799 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000800 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700801 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000802 }
803 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700804 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000805 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
806
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700807 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
808 // Note: this is a simplistic approach assuming that a mix port can only be populated
809 // from a single device port. Implementing support for stuffing dynamic profiles with
810 // a superset of all profiles from all routable dynamic device ports would be more involved.
811 for (auto& port : ports) {
812 if (routableMixPortIds.count(port.id) == 0) continue;
813 if (hasDynamicProfilesOnly(port.profiles)) {
814 port.profiles = connectedPort.profiles;
815 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700816 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700817 // Check if profiles are not all dynamic because they were populated by
818 // a previous connection. Otherwise, it means that they are actually static.
819 for (const auto& cp : mConnectedDevicePorts) {
820 if (cp.second.count(port.id) > 0) {
821 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700822 break;
823 }
824 }
825 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700826 }
827 }
828 *_aidl_return = std::move(connectedPort);
829
Mikhail Naganov00603d12022-05-02 22:52:13 +0000830 return ndk::ScopedAStatus::ok();
831}
832
833ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
834 auto& ports = getConfig().ports;
835 auto portIt = findById<AudioPort>(ports, in_portId);
836 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530837 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000838 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
839 }
840 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530841 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
842 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000843 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
844 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700845 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
846 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530847 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
848 << " is not a connected device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000849 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
850 }
851 auto& configs = getConfig().portConfigs;
852 auto& initials = getConfig().initialConfigs;
853 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
854 if (config.portId == in_portId) {
855 // Check if the configuration was provided by the client.
856 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
857 return initialIt == initials.end() || config != *initialIt;
858 }
859 return false;
860 });
861 if (configIt != configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530862 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
863 << " has a non-default config with id " << configIt->id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000864 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
865 }
jiabin783c48b2023-02-28 18:28:06 +0000866 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000867 ports.erase(portIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +0530868 LOG(DEBUG) << __func__ << ": " << mType << ": connected device port " << in_portId
869 << " released";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000870
871 auto& routes = getConfig().routes;
872 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
873 if (routesIt->sinkPortId == in_portId) {
874 routesIt = routes.erase(routesIt);
875 } else {
876 // Note: the list of sourcePortIds can't become empty because there must
877 // be the id of the template port in the route.
878 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
879 ++routesIt;
880 }
881 }
882
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700883 // Clear profiles for mix ports that are not connected to any other ports.
884 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
885 mConnectedDevicePorts.erase(connectedPortsIt);
886 for (const auto& connectedPort : mConnectedDevicePorts) {
887 for (int32_t mixPortId : connectedPort.second) {
888 mixPortsToClear.erase(mixPortId);
889 }
890 }
891 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700892 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
893 if (mixPortIt != ports.end()) {
894 mixPortIt->profiles = {};
895 }
896 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700897
Mikhail Naganov00603d12022-05-02 22:52:13 +0000898 return ndk::ScopedAStatus::ok();
899}
900
jiabindd23b0e2023-12-11 19:10:05 +0000901ndk::ScopedAStatus Module::prepareToDisconnectExternalDevice(int32_t in_portId) {
902 auto& ports = getConfig().ports;
903 auto portIt = findById<AudioPort>(ports, in_portId);
904 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530905 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
jiabindd23b0e2023-12-11 19:10:05 +0000906 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
907 }
908 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530909 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
910 << " is not a device port";
jiabindd23b0e2023-12-11 19:10:05 +0000911 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
912 }
913 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
914 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530915 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
916 << " is not a connected device port";
jiabindd23b0e2023-12-11 19:10:05 +0000917 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
918 }
919
920 onPrepareToDisconnectExternalDevice(*portIt);
921
922 return ndk::ScopedAStatus::ok();
923}
924
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000925ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
926 *_aidl_return = getConfig().patches;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530927 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " patches";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000928 return ndk::ScopedAStatus::ok();
929}
930
931ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
932 auto& ports = getConfig().ports;
933 auto portIt = findById<AudioPort>(ports, in_portId);
934 if (portIt != ports.end()) {
935 *_aidl_return = *portIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530936 LOG(DEBUG) << __func__ << ": " << mType << ": returning port by id " << in_portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000937 return ndk::ScopedAStatus::ok();
938 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530939 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000940 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
941}
942
943ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
944 *_aidl_return = getConfig().portConfigs;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530945 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size()
946 << " port configs";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000947 return ndk::ScopedAStatus::ok();
948}
949
950ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
951 *_aidl_return = getConfig().ports;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530952 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " ports";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000953 return ndk::ScopedAStatus::ok();
954}
955
956ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
957 *_aidl_return = getConfig().routes;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530958 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " routes";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000959 return ndk::ScopedAStatus::ok();
960}
961
Mikhail Naganov00603d12022-05-02 22:52:13 +0000962ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
963 std::vector<AudioRoute>* _aidl_return) {
964 auto& ports = getConfig().ports;
965 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530966 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000967 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
968 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700969 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
970 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
971 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000972 return ndk::ScopedAStatus::ok();
973}
974
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000975ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
976 OpenInputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530977 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
978 << ", buffer size " << 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::input) {
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 input mix port";
984 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
985 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000986 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700987 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
988 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000989 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000990 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700991 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000992 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000993 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700994 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
995 RETURN_STATUS_IF_ERROR(
996 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
997 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +0000998 auto streamBinder = streamWrapper.getBinder();
999 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
1000 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001001 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001002 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001003 return ndk::ScopedAStatus::ok();
1004}
1005
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001006ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
1007 OpenOutputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301008 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
1009 << ", has offload info? " << (in_args.offloadInfo.has_value()) << ", buffer size "
1010 << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001011 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001012 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001013 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301014 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001015 << " does not correspond to an output mix port";
1016 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1017 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +00001018 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
1019 AudioOutputFlags::COMPRESS_OFFLOAD);
1020 if (isOffload && !in_args.offloadInfo.has_value()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301021 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +00001022 << " has COMPRESS_OFFLOAD flag set, requires offload info";
1023 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1024 }
Mikhail Naganov30301a42022-09-13 01:20:45 +00001025 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
1026 AudioOutputFlags::NON_BLOCKING);
1027 if (isNonBlocking && in_args.callback == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301028 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov30301a42022-09-13 01:20:45 +00001029 << " has NON_BLOCKING flag set, requires async callback";
1030 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1031 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001032 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001033 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
1034 isNonBlocking ? in_args.callback : nullptr,
1035 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001036 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001037 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -07001038 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -07001039 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001040 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001041 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
1042 RETURN_STATUS_IF_ERROR(
1043 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
1044 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +00001045 auto streamBinder = streamWrapper.getBinder();
1046 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
1047 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001048 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001049 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001050 return ndk::ScopedAStatus::ok();
1051}
1052
Mikhail Naganov74927202022-12-19 16:37:14 +00001053ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
1054 SupportedPlaybackRateFactors* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301055 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganov74927202022-12-19 16:37:14 +00001056 (void)_aidl_return;
1057 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1058}
1059
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001060ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301061 LOG(DEBUG) << __func__ << ": " << mType << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001062 if (in_requested.sourcePortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301063 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001064 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1065 }
1066 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301067 LOG(ERROR) << __func__ << ": " << mType
1068 << ": requested patch has duplicate ids in the sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001069 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1070 }
1071 if (in_requested.sinkPortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301072 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001073 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1074 }
1075 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301076 LOG(ERROR) << __func__ << ": " << mType
1077 << ": requested patch has duplicate ids in the sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001078 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1079 }
1080
1081 auto& configs = getConfig().portConfigs;
1082 std::vector<int32_t> missingIds;
1083 auto sources =
1084 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
1085 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301086 LOG(ERROR) << __func__ << ": " << mType << ": following source port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001087 << ::android::internal::ToString(missingIds);
1088 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1089 }
1090 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
1091 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301092 LOG(ERROR) << __func__ << ": " << mType << ": following sink port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001093 << ::android::internal::ToString(missingIds);
1094 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1095 }
1096 // bool indicates whether a non-exclusive route is available.
1097 // If only an exclusive route is available, that means the patch can not be
1098 // established if there is any other patch which currently uses the sink port.
1099 std::map<int32_t, bool> allowedSinkPorts;
1100 auto& routes = getConfig().routes;
1101 for (auto src : sources) {
1102 for (const auto& r : routes) {
1103 const auto& srcs = r.sourcePortIds;
1104 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
1105 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
1106 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
1107 }
1108 }
1109 }
1110 }
1111 for (auto sink : sinks) {
1112 if (allowedSinkPorts.count(sink->portId) == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301113 LOG(ERROR) << __func__ << ": " << mType << ": there is no route to the sink port id "
1114 << sink->portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001115 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1116 }
1117 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001118 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +00001119
Priyanka Advani (xWF)781f66a2024-12-05 22:51:28 +00001120 auto& patches = getConfig().patches;
1121 auto existing = patches.end();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001122 std::optional<decltype(mPatches)> patchesBackup;
Priyanka Advani (xWF)781f66a2024-12-05 22:51:28 +00001123 if (in_requested.id != 0) {
1124 existing = findById<AudioPatch>(patches, in_requested.id);
1125 if (existing != patches.end()) {
1126 patchesBackup = mPatches;
1127 cleanUpPatch(existing->id);
1128 } else {
1129 LOG(ERROR) << __func__ << ": " << mType << ": not found existing patch id "
1130 << in_requested.id;
1131 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1132 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001133 }
1134 // Validate the requested patch.
1135 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
1136 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301137 LOG(ERROR) << __func__ << ": " << mType << ": sink port id " << sinkPortId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001138 << "is exclusive and is already used by some other patch";
1139 if (patchesBackup.has_value()) {
1140 mPatches = std::move(*patchesBackup);
1141 }
1142 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1143 }
1144 }
Mikhail Naganov13501872023-10-18 16:15:46 -07001145 // Find the highest sample rate among mix port configs.
1146 std::map<int32_t, AudioPortConfig*> sampleRates;
1147 std::vector<AudioPortConfig*>& mixPortConfigs =
1148 sources[0]->ext.getTag() == AudioPortExt::mix ? sources : sinks;
1149 for (auto mix : mixPortConfigs) {
1150 sampleRates.emplace(mix->sampleRate.value().value, mix);
1151 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001152 *_aidl_return = in_requested;
Mikhail Naganov13501872023-10-18 16:15:46 -07001153 auto maxSampleRateIt = std::max_element(sampleRates.begin(), sampleRates.end());
1154 const int32_t latencyMs = getNominalLatencyMs(*(maxSampleRateIt->second));
Kuowei Li10a768a2024-12-09 17:44:40 +08001155 if (!calculateBufferSizeFrames(
1156 maxSampleRateIt->second->format.value(), latencyMs, maxSampleRateIt->first,
1157 &_aidl_return->minimumStreamBufferSizeFrames).isOk()) {
1158 if (patchesBackup.has_value()) {
1159 mPatches = std::move(*patchesBackup);
1160 }
1161 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1162 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001163 _aidl_return->latenciesMs.clear();
1164 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
Mikhail Naganov13501872023-10-18 16:15:46 -07001165 _aidl_return->sinkPortConfigIds.size(), latencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001166 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001167 if (existing == patches.end()) {
1168 _aidl_return->id = getConfig().nextPatchId++;
1169 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001170 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001171 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -07001172 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001173 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001174 patchesBackup = mPatches;
1175 registerPatch(*_aidl_return);
1176 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
1177 mPatches = std::move(*patchesBackup);
1178 if (existing == patches.end()) {
1179 patches.pop_back();
1180 } else {
1181 *existing = oldPatch;
1182 }
1183 return status;
1184 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001185
Jaideep Sharma559a4912024-03-07 10:05:51 +05301186 LOG(DEBUG) << __func__ << ": " << mType << ": " << (oldPatch.id == 0 ? "created" : "updated")
1187 << " patch " << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001188 return ndk::ScopedAStatus::ok();
1189}
1190
1191ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
1192 AudioPortConfig* out_suggested, bool* _aidl_return) {
Mikhail Naganova92039a2023-12-20 14:27:22 -08001193 auto generate = [this](const AudioPort& port, AudioPortConfig* config) {
1194 return generateDefaultPortConfig(port, config);
1195 };
1196 return setAudioPortConfigImpl(in_requested, generate, out_suggested, _aidl_return);
1197}
1198
1199ndk::ScopedAStatus Module::setAudioPortConfigImpl(
1200 const AudioPortConfig& in_requested,
1201 const std::function<bool(const ::aidl::android::media::audio::common::AudioPort& port,
1202 ::aidl::android::media::audio::common::AudioPortConfig* config)>&
1203 fillPortConfig,
1204 AudioPortConfig* out_suggested, bool* applied) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301205 LOG(DEBUG) << __func__ << ": " << mType << ": requested " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001206 auto& configs = getConfig().portConfigs;
1207 auto existing = configs.end();
1208 if (in_requested.id != 0) {
1209 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1210 existing == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301211 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id "
1212 << in_requested.id << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001213 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1214 }
1215 }
1216
1217 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1218 if (portId == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301219 LOG(ERROR) << __func__ << ": " << mType
1220 << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001221 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1222 }
1223 auto& ports = getConfig().ports;
1224 auto portIt = findById<AudioPort>(ports, portId);
1225 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301226 LOG(ERROR) << __func__ << ": " << mType
1227 << ": requested port config points to non-existent portId " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001228 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1229 }
1230 if (existing != configs.end()) {
1231 *out_suggested = *existing;
1232 } else {
1233 AudioPortConfig newConfig;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001234 newConfig.portId = portIt->id;
1235 if (fillPortConfig(*portIt, &newConfig)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001236 *out_suggested = newConfig;
1237 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301238 LOG(ERROR) << __func__ << ": " << mType
1239 << ": unable generate a default config for port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001240 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1241 }
1242 }
1243 // From this moment, 'out_suggested' is either an existing port config,
1244 // or a new generated config. Now attempt to update it according to the specified
1245 // fields of 'in_requested'.
1246
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001247 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1248 // which takes care of their actual configuration automatically.
1249 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1250 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001251 bool requestedIsValid = true, requestedIsFullySpecified = true;
1252
1253 AudioIoFlags portFlags = portIt->flags;
1254 if (in_requested.flags.has_value()) {
1255 if (in_requested.flags.value() != portFlags) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301256 LOG(WARNING) << __func__ << ": " << mType << ": requested flags "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001257 << in_requested.flags.value().toString() << " do not match port's "
1258 << portId << " flags " << portFlags.toString();
1259 requestedIsValid = false;
1260 }
1261 } else {
1262 requestedIsFullySpecified = false;
1263 }
1264
1265 AudioProfile portProfile;
1266 if (in_requested.format.has_value()) {
1267 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001268 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1269 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001270 out_suggested->format = format;
1271 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301272 LOG(WARNING) << __func__ << ": " << mType << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001273 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001274 requestedIsValid = false;
1275 }
1276 } else {
1277 requestedIsFullySpecified = false;
1278 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001279 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1280 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301281 LOG(ERROR) << __func__ << ": " << mType << ": port " << portId
1282 << " does not support format " << out_suggested->format.value().toString()
1283 << " anymore";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001284 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1285 }
1286
1287 if (in_requested.channelMask.has_value()) {
1288 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001289 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1290 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1291 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001292 out_suggested->channelMask = channelMask;
1293 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301294 LOG(WARNING) << __func__ << ": " << mType << ": requested channel mask "
1295 << channelMask.toString() << " is not supported for the format "
1296 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001297 requestedIsValid = false;
1298 }
1299 } else {
1300 requestedIsFullySpecified = false;
1301 }
1302
1303 if (in_requested.sampleRate.has_value()) {
1304 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001305 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1306 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001307 sampleRate.value) != portProfile.sampleRates.end()) {
1308 out_suggested->sampleRate = sampleRate;
1309 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301310 LOG(WARNING) << __func__ << ": " << mType << ": requested sample rate "
1311 << sampleRate.value << " is not supported for the format "
1312 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001313 requestedIsValid = false;
1314 }
1315 } else {
1316 requestedIsFullySpecified = false;
1317 }
1318
1319 if (in_requested.gain.has_value()) {
Weilin Xua33bb5e2024-10-02 17:16:42 +00001320 if (!setAudioPortConfigGain(*portIt, in_requested.gain.value())) {
1321 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1322 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001323 out_suggested->gain = in_requested.gain.value();
1324 }
1325
Mikhail Naganov248e9502023-02-21 16:32:40 -08001326 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1327 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1328 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
Mikhail Naganovb06a4922024-03-06 16:39:50 -08001329 // 'AudioMixPortExt.handle' and '.usecase' are set by the client,
1330 // copy from in_requested.
1331 const auto& src = in_requested.ext.get<AudioPortExt::Tag::mix>();
1332 auto& dst = out_suggested->ext.get<AudioPortExt::Tag::mix>();
1333 dst.handle = src.handle;
1334 dst.usecase = src.usecase;
Mikhail Naganov248e9502023-02-21 16:32:40 -08001335 }
1336 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301337 LOG(WARNING) << __func__ << ": " << mType << ": requested ext tag "
Mikhail Naganov248e9502023-02-21 16:32:40 -08001338 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1339 << toString(out_suggested->ext.getTag());
1340 requestedIsValid = false;
1341 }
1342 }
1343
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001344 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1345 out_suggested->id = getConfig().nextPortId++;
1346 configs.push_back(*out_suggested);
Mikhail Naganova92039a2023-12-20 14:27:22 -08001347 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301348 LOG(DEBUG) << __func__ << ": " << mType << ": created new port config "
1349 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001350 } else if (existing != configs.end() && requestedIsValid) {
1351 *existing = *out_suggested;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001352 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301353 LOG(DEBUG) << __func__ << ": " << mType << ": updated port config "
1354 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001355 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301356 LOG(DEBUG) << __func__ << ": " << mType << ": not applied; existing config ? "
1357 << (existing != configs.end()) << "; requested is valid? " << requestedIsValid
1358 << ", fully specified? " << requestedIsFullySpecified;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001359 *applied = false;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001360 }
1361 return ndk::ScopedAStatus::ok();
1362}
1363
Weilin Xua33bb5e2024-10-02 17:16:42 +00001364bool Module::setAudioPortConfigGain(const AudioPort& port, const AudioGainConfig& gainRequested) {
1365 auto& ports = getConfig().ports;
1366 if (gainRequested.index < 0 || gainRequested.index >= (int)port.gains.size()) {
1367 LOG(ERROR) << __func__ << ": gains for port " << port.id << " is undefined";
1368 return false;
1369 }
1370 int stepValue = port.gains[gainRequested.index].stepValue;
1371 if (stepValue == 0) {
1372 LOG(ERROR) << __func__ << ": port gain step value is 0";
1373 return false;
1374 }
1375 int minValue = port.gains[gainRequested.index].minValue;
1376 int maxValue = port.gains[gainRequested.index].maxValue;
1377 if (gainRequested.values[0] > maxValue || gainRequested.values[0] < minValue) {
1378 LOG(ERROR) << __func__ << ": gain value " << gainRequested.values[0]
1379 << " out of range of min and max gain config";
1380 return false;
1381 }
1382 int gainIndex = (gainRequested.values[0] - minValue) / stepValue;
1383 int totalSteps = (maxValue - minValue) / stepValue;
1384 if (totalSteps == 0) {
1385 LOG(ERROR) << __func__ << ": difference between port gain min value " << minValue
1386 << " and max value " << maxValue << " is less than step value " << stepValue;
1387 return false;
1388 }
1389 // Root-power quantities are used in curve:
1390 // 10^((minMb / 100 + (maxMb / 100 - minMb / 100) * gainIndex / totalSteps) / (10 * 2))
1391 // where 100 is the conversion from mB to dB, 10 comes from the log 10 conversion from power
1392 // ratios, and 2 means are the square of amplitude.
1393 float gain =
1394 pow(10, (minValue + (maxValue - minValue) * (gainIndex / (float)totalSteps)) / 2000);
1395 if (gain < 0) {
1396 LOG(ERROR) << __func__ << ": gain " << gain << " is less than 0";
1397 return false;
1398 }
1399 for (const auto& route : getConfig().routes) {
1400 if (route.sinkPortId != port.id) {
1401 continue;
1402 }
1403 for (const auto sourcePortId : route.sourcePortIds) {
1404 mStreams.setGain(sourcePortId, gain);
1405 }
1406 }
1407 return true;
1408}
1409
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001410ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1411 auto& patches = getConfig().patches;
1412 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1413 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001414 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001415 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001416 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1417 mPatches = std::move(patchesBackup);
1418 return status;
1419 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001420 patches.erase(patchIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301421 LOG(DEBUG) << __func__ << ": " << mType << ": erased patch " << in_patchId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001422 return ndk::ScopedAStatus::ok();
1423 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301424 LOG(ERROR) << __func__ << ": " << mType << ": patch id " << in_patchId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001425 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1426}
1427
1428ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1429 auto& configs = getConfig().portConfigs;
1430 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1431 if (configIt != configs.end()) {
1432 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301433 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001434 << " has a stream opened on it";
1435 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1436 }
1437 auto patchIt = mPatches.find(in_portConfigId);
1438 if (patchIt != mPatches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301439 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001440 << " is used by the patch with id " << patchIt->second;
1441 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1442 }
1443 auto& initials = getConfig().initialConfigs;
1444 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1445 if (initialIt == initials.end()) {
1446 configs.erase(configIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301447 LOG(DEBUG) << __func__ << ": " << mType << ": erased port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001448 } else if (*configIt != *initialIt) {
1449 *configIt = *initialIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301450 LOG(DEBUG) << __func__ << ": " << mType << ": reset port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001451 }
1452 return ndk::ScopedAStatus::ok();
1453 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301454 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
1455 << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001456 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1457}
1458
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001459ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1460 *_aidl_return = mMasterMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301461 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001462 return ndk::ScopedAStatus::ok();
1463}
1464
1465ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301466 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001467 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1468 : onMasterMuteChanged(in_mute);
1469 if (result.isOk()) {
1470 mMasterMute = in_mute;
1471 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301472 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterMuteChanged("
1473 << in_mute << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001474 // Reset master mute if it failed.
1475 onMasterMuteChanged(mMasterMute);
1476 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001477 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001478}
1479
1480ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1481 *_aidl_return = mMasterVolume;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301482 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001483 return ndk::ScopedAStatus::ok();
1484}
1485
1486ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301487 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001488 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001489 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1490 : onMasterVolumeChanged(in_volume);
1491 if (result.isOk()) {
1492 mMasterVolume = in_volume;
1493 } else {
1494 // Reset master volume if it failed.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301495 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterVolumeChanged("
1496 << in_volume << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001497 onMasterVolumeChanged(mMasterVolume);
1498 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001499 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001500 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301501 LOG(ERROR) << __func__ << ": " << mType << ": invalid master volume value: " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001502 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1503}
1504
1505ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1506 *_aidl_return = mMicMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301507 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001508 return ndk::ScopedAStatus::ok();
1509}
1510
1511ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301512 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001513 mMicMute = in_mute;
1514 return ndk::ScopedAStatus::ok();
1515}
1516
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001517ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001518 *_aidl_return = getMicrophoneInfos();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301519 LOG(DEBUG) << __func__ << ": " << mType << ": returning "
1520 << ::android::internal::ToString(*_aidl_return);
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001521 return ndk::ScopedAStatus::ok();
1522}
1523
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001524ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001525 if (!isValidAudioMode(in_mode)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301526 LOG(ERROR) << __func__ << ": " << mType << ": invalid mode " << toString(in_mode);
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001527 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1528 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001529 // No checks for supported audio modes here, it's an informative notification.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301530 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_mode);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001531 return ndk::ScopedAStatus::ok();
1532}
1533
1534ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301535 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_rotation);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001536 return ndk::ScopedAStatus::ok();
1537}
1538
1539ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301540 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_isTurnedOn;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001541 return ndk::ScopedAStatus::ok();
1542}
1543
Vlad Popa83a6d822022-11-07 13:53:57 +01001544ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001545 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001546 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001547 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001548 *_aidl_return = mSoundDose.getInstance();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301549 LOG(DEBUG) << __func__ << ": " << mType
1550 << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001551 return ndk::ScopedAStatus::ok();
1552}
1553
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001554ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301555 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001556 (void)_aidl_return;
1557 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1558}
1559
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001560const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001561const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov1b8f65d2025-03-03 13:59:44 -08001562const std::string Module::kClipTransitionSupportName = "aosp.clipTransitionSupport";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001563
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001564ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1565 std::vector<VendorParameter>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301566 LOG(VERBOSE) << __func__ << ": " << mType << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001567 bool allParametersKnown = true;
1568 for (const auto& id : in_ids) {
1569 if (id == VendorDebug::kForceTransientBurstName) {
1570 VendorParameter forceTransientBurst{.id = id};
1571 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1572 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001573 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1574 VendorParameter forceSynchronousDrain{.id = id};
1575 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1576 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov1b8f65d2025-03-03 13:59:44 -08001577 } else if (id == kClipTransitionSupportName) {
1578 VendorParameter clipTransitionSupport{.id = id};
1579 clipTransitionSupport.ext.setParcelable(Boolean{true});
1580 _aidl_return->push_back(std::move(clipTransitionSupport));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001581 } else {
1582 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301583 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << id << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001584 }
1585 }
1586 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1587 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001588}
1589
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001590namespace {
1591
1592template <typename W>
1593bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1594 std::optional<W> value;
1595 binder_status_t result = p.ext.getParcelable(&value);
1596 if (result == STATUS_OK && value.has_value()) {
1597 *v = value.value().value;
1598 return true;
1599 }
1600 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1601 << "\": " << result;
1602 return false;
1603}
1604
1605} // namespace
1606
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001607ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1608 bool in_async) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301609 LOG(VERBOSE) << __func__ << ": " << mType << ": parameter count " << in_parameters.size()
1610 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001611 bool allParametersKnown = true;
1612 for (const auto& p : in_parameters) {
1613 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001614 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1615 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1616 }
1617 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1618 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001619 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1620 }
1621 } else {
1622 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301623 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << p.id
1624 << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001625 }
1626 }
1627 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1628 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001629}
1630
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001631ndk::ScopedAStatus Module::addDeviceEffect(
1632 int32_t in_portConfigId,
1633 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1634 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301635 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1636 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001637 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301638 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1639 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001640 }
1641 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1642}
1643
1644ndk::ScopedAStatus Module::removeDeviceEffect(
1645 int32_t in_portConfigId,
1646 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1647 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301648 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1649 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001650 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301651 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1652 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001653 }
1654 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1655}
1656
jiabin9a8e6862023-01-12 23:06:37 +00001657ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1658 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301659 LOG(DEBUG) << __func__ << ": " << mType << ": mmap policy type " << toString(mmapPolicyType);
jiabin9a8e6862023-01-12 23:06:37 +00001660 std::set<int32_t> mmapSinks;
1661 std::set<int32_t> mmapSources;
1662 auto& ports = getConfig().ports;
1663 for (const auto& port : ports) {
1664 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1665 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1666 AudioInputFlags::MMAP_NOIRQ)) {
1667 mmapSinks.insert(port.id);
1668 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1669 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1670 AudioOutputFlags::MMAP_NOIRQ)) {
1671 mmapSources.insert(port.id);
1672 }
1673 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001674 if (mmapSources.empty() && mmapSinks.empty()) {
1675 AudioMMapPolicyInfo never;
1676 never.mmapPolicy = AudioMMapPolicy::NEVER;
1677 _aidl_return->push_back(never);
1678 return ndk::ScopedAStatus::ok();
1679 }
jiabin9a8e6862023-01-12 23:06:37 +00001680 for (const auto& route : getConfig().routes) {
1681 if (mmapSinks.count(route.sinkPortId) != 0) {
1682 // The sink is a mix port, add the sources if they are device ports.
1683 for (int sourcePortId : route.sourcePortIds) {
1684 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1685 if (sourcePortIt == ports.end()) {
1686 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301687 LOG(ERROR) << __func__ << ": " << mType << ": port id " << sourcePortId
1688 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001689 continue;
1690 }
1691 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1692 // The source is not a device port, skip
1693 continue;
1694 }
1695 AudioMMapPolicyInfo policyInfo;
1696 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1697 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1698 // default implementation.
1699 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1700 _aidl_return->push_back(policyInfo);
1701 }
1702 } else {
1703 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1704 if (sinkPortIt == ports.end()) {
1705 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301706 LOG(ERROR) << __func__ << ": " << mType << ": port id " << route.sinkPortId
1707 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001708 continue;
1709 }
1710 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1711 // The sink is not a device port, skip
1712 continue;
1713 }
1714 if (count_any(mmapSources, route.sourcePortIds)) {
1715 AudioMMapPolicyInfo policyInfo;
1716 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1717 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1718 // default implementation.
1719 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1720 _aidl_return->push_back(policyInfo);
1721 }
1722 }
1723 }
1724 return ndk::ScopedAStatus::ok();
1725}
1726
Eric Laurente2432ea2023-01-12 17:47:31 +01001727ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301728 LOG(DEBUG) << __func__ << ": " << mType;
Eric Laurente2432ea2023-01-12 17:47:31 +01001729 *_aidl_return = false;
1730 return ndk::ScopedAStatus::ok();
1731}
1732
jiabinb76981e2023-01-18 00:58:30 +00001733ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1734 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301735 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001736 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1737 }
1738 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301739 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001740 return ndk::ScopedAStatus::ok();
1741}
1742
1743ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1744 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301745 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001746 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1747 }
1748 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301749 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001750 return ndk::ScopedAStatus::ok();
1751}
1752
1753bool Module::isMmapSupported() {
1754 if (mIsMmapSupported.has_value()) {
1755 return mIsMmapSupported.value();
1756 }
1757 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1758 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1759 mIsMmapSupported = false;
1760 } else {
1761 mIsMmapSupported =
1762 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1763 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1764 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1765 }) != mmapPolicyInfos.end();
1766 }
1767 return mIsMmapSupported.value();
1768}
1769
Mikhail Naganova92039a2023-12-20 14:27:22 -08001770ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001771 if (audioPort->ext.getTag() != AudioPortExt::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301772 LOG(ERROR) << __func__ << ": " << mType << ": not a device port: " << audioPort->toString();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001773 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1774 }
1775 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1776 if (!devicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301777 LOG(ERROR) << __func__ << ": " << mType << ": module implementation must override "
1778 "'populateConnectedDevicePort' "
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001779 << "to handle connection of external devices.";
1780 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1781 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301782 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001783 return ndk::ScopedAStatus::ok();
1784}
1785
1786ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1787 const std::vector<AudioPortConfig*>& sources __unused,
1788 const std::vector<AudioPortConfig*>& sinks __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301789 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001790 return ndk::ScopedAStatus::ok();
1791}
1792
jiabin783c48b2023-02-28 18:28:06 +00001793void Module::onExternalDeviceConnectionChanged(
1794 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1795 bool connected __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301796 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabin783c48b2023-02-28 18:28:06 +00001797}
1798
jiabindd23b0e2023-12-11 19:10:05 +00001799void Module::onPrepareToDisconnectExternalDevice(
1800 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301801 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabindd23b0e2023-12-11 19:10:05 +00001802}
1803
jiabin783c48b2023-02-28 18:28:06 +00001804ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301805 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001806 return ndk::ScopedAStatus::ok();
1807}
1808
1809ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301810 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001811 return ndk::ScopedAStatus::ok();
1812}
1813
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001814std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1815 std::vector<MicrophoneInfo> result;
1816 Configuration& config = getConfig();
1817 for (const AudioPort& port : config.ports) {
1818 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1819 const AudioDeviceType deviceType =
1820 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1821 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1822 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1823 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1824 // accordingly based on their physical microphone parameters.
1825 result.push_back(MicrophoneInfo{
1826 .id = port.name,
1827 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1828 .group = 0,
1829 .indexInTheGroup = 0,
1830 });
1831 }
1832 }
1833 }
1834 return result;
1835}
1836
Ram Mohan18f0d512023-07-01 00:47:09 +05301837ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1838 return mStreams.bluetoothParametersUpdated();
1839}
1840
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001841} // namespace aidl::android::hardware::audio::core