blob: f9fa7994e55e9729b65645f861591e8039450e37 [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 Naganove87a1b92024-11-12 15:31:55 -0800214 StreamContext::DebugParameters params{
215 mDebug.streamTransientStateDelayMs, mVendorDebug.forceTransientBurst,
216 mVendorDebug.forceSynchronousDrain, mVendorDebug.forceDrainToDraining};
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800217 std::unique_ptr<StreamContext::DataMQ> dataMQ = nullptr;
218 std::shared_ptr<IStreamCallback> streamAsyncCallback = nullptr;
219 std::shared_ptr<ISoundDose> soundDose;
220 if (!getSoundDose(&soundDose).isOk()) {
221 LOG(ERROR) << __func__ << ": could not create sound dose instance";
222 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
223 }
224 if (!hasMmapFlag(flags)) {
225 dataMQ = std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames);
226 streamAsyncCallback = asyncCallback;
227 }
228 StreamContext temp(
229 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
230 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
231 portConfigIt->format.value(), portConfigIt->channelMask.value(),
232 portConfigIt->sampleRate.value().value, flags, nominalLatencyMs,
233 portConfigIt->ext.get<AudioPortExt::mix>().handle, std::move(dataMQ),
234 streamAsyncCallback, outEventCallback, mSoundDose.getInstance(), params);
235 if (temp.isValid()) {
236 *out_context = std::move(temp);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000237 } else {
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800238 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000239 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000240 return ndk::ScopedAStatus::ok();
241}
242
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530243std::vector<AudioDevice> Module::getDevicesFromDevicePortConfigIds(
244 const std::set<int32_t>& devicePortConfigIds) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000245 std::vector<AudioDevice> result;
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530246 auto& configs = getConfig().portConfigs;
247 for (const auto& id : devicePortConfigIds) {
248 auto it = findById<AudioPortConfig>(configs, id);
249 if (it != configs.end() && it->ext.getTag() == AudioPortExt::Tag::device) {
250 result.push_back(it->ext.template get<AudioPortExt::Tag::device>().device);
251 } else {
252 LOG(FATAL) << __func__ << ": " << mType
253 << ": failed to find device for id" << id;
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000254 }
255 }
256 return result;
257}
258
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530259std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
260 return getDevicesFromDevicePortConfigIds(findConnectedPortConfigIds(portConfigId));
261}
262
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000263std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
264 std::set<int32_t> result;
265 auto patchIdsRange = mPatches.equal_range(portConfigId);
266 auto& patches = getConfig().patches;
267 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
268 auto patchIt = findById<AudioPatch>(patches, it->second);
269 if (patchIt == patches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530270 LOG(FATAL) << __func__ << ": " << mType << ": patch with id " << it->second
271 << " taken from mPatches "
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000272 << "not found in the configuration";
273 }
274 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
275 portConfigId) != patchIt->sourcePortConfigIds.end()) {
276 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
277 } else {
278 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
279 }
280 }
281 return result;
282}
283
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000284ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
285 auto& configs = getConfig().portConfigs;
286 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
287 if (portConfigIt == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530288 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id " << in_portConfigId
289 << " not found";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000290 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
291 }
292 const int32_t portId = portConfigIt->portId;
293 // In our implementation, configs of mix ports always have unique IDs.
294 CHECK(portId != in_portConfigId);
295 auto& ports = getConfig().ports;
296 auto portIt = findById<AudioPort>(ports, portId);
297 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530298 LOG(ERROR) << __func__ << ": " << mType << ": port id " << portId
299 << " used by port config id " << in_portConfigId << " not found";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000300 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
301 }
302 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530303 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000304 << " already has a stream opened on it";
305 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
306 }
307 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530308 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000309 << " does not correspond to a mix port";
310 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
311 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700312 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000313 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530314 LOG(ERROR) << __func__ << ": " << mType << ": port id " << portId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000315 << " has already reached maximum allowed opened stream count: "
316 << maxOpenStreamCount;
317 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
318 }
319 *port = &(*portIt);
320 return ndk::ScopedAStatus::ok();
321}
322
Mikhail Naganova92039a2023-12-20 14:27:22 -0800323bool Module::generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
324 const bool allowDynamicConfig = port.ext.getTag() == AudioPortExt::device;
325 for (const auto& profile : port.profiles) {
326 if (isDynamicProfile(profile)) continue;
327 config->format = profile.format;
328 config->channelMask = *profile.channelMasks.begin();
329 config->sampleRate = Int{.value = *profile.sampleRates.begin()};
330 config->flags = port.flags;
331 config->ext = port.ext;
332 return true;
333 }
334 if (allowDynamicConfig) {
335 config->format = AudioFormatDescription{};
336 config->channelMask = AudioChannelLayout{};
337 config->sampleRate = Int{.value = 0};
338 config->flags = port.flags;
339 config->ext = port.ext;
340 return true;
341 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530342 LOG(ERROR) << __func__ << ": " << mType << ": port " << port.id << " only has dynamic profiles";
Mikhail Naganova92039a2023-12-20 14:27:22 -0800343 return false;
344}
345
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000346void Module::populateConnectedProfiles() {
347 Configuration& config = getConfig();
348 for (const AudioPort& port : config.ports) {
349 if (port.ext.getTag() == AudioPortExt::device) {
350 if (auto devicePort = port.ext.get<AudioPortExt::device>();
351 !devicePort.device.type.connection.empty() && port.profiles.empty()) {
352 if (auto connIt = config.connectedProfiles.find(port.id);
353 connIt == config.connectedProfiles.end()) {
354 config.connectedProfiles.emplace(
355 port.id, internal::getStandard16And24BitPcmAudioProfiles());
356 }
357 }
358 }
359 }
360}
361
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000362template <typename C>
363std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
364 std::set<int32_t> result;
365 auto& portConfigs = getConfig().portConfigs;
366 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
367 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
368 if (portConfigIt != portConfigs.end()) {
369 result.insert(portConfigIt->portId);
370 }
371 }
372 return result;
373}
374
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000375std::unique_ptr<Module::Configuration> Module::initializeConfig() {
376 return internal::getConfiguration(getType());
Peter Yoon918a6a52023-07-13 17:04:37 +0900377}
378
Mikhail Naganov13501872023-10-18 16:15:46 -0700379int32_t Module::getNominalLatencyMs(const AudioPortConfig&) {
380 // Arbitrary value. Implementations must override this method to provide their actual latency.
381 static constexpr int32_t kLatencyMs = 5;
382 return kLatencyMs;
383}
384
Kuowei Li10a768a2024-12-09 17:44:40 +0800385ndk::ScopedAStatus Module::calculateBufferSizeFrames(
386 const ::aidl::android::media::audio::common::AudioFormatDescription &format,
387 int32_t latencyMs, int32_t sampleRateHz, int32_t *bufferSizeFrames) {
388 if (format.type == AudioFormatType::PCM) {
389 *bufferSizeFrames = calculateBufferSizeFramesForPcm(latencyMs, sampleRateHz);
390 return ndk::ScopedAStatus::ok();
391 }
392 LOG(ERROR) << __func__ << ": " << mType << ": format " << format.toString()
393 << " is not supported";
394 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
395}
396
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800397ndk::ScopedAStatus Module::createMmapBuffer(
398 const ::aidl::android::hardware::audio::core::StreamContext& context __unused,
399 ::aidl::android::hardware::audio::core::StreamDescriptor* desc __unused) {
400 LOG(ERROR) << __func__ << ": " << mType << ": is not implemented";
401 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
402}
403
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700404std::vector<AudioRoute*> Module::getAudioRoutesForAudioPortImpl(int32_t portId) {
405 std::vector<AudioRoute*> result;
406 auto& routes = getConfig().routes;
407 for (auto& r : routes) {
408 const auto& srcs = r.sourcePortIds;
409 if (r.sinkPortId == portId || std::find(srcs.begin(), srcs.end(), portId) != srcs.end()) {
410 result.push_back(&r);
411 }
412 }
413 return result;
414}
415
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000416Module::Configuration& Module::getConfig() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000417 if (!mConfig) {
Yi Konge62f97f2024-08-14 01:52:04 +0800418 mConfig = initializeConfig();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000419 }
420 return *mConfig;
421}
422
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700423std::set<int32_t> Module::getRoutableAudioPortIds(int32_t portId,
424 std::vector<AudioRoute*>* routes) {
425 std::vector<AudioRoute*> routesStorage;
426 if (routes == nullptr) {
427 routesStorage = getAudioRoutesForAudioPortImpl(portId);
428 routes = &routesStorage;
429 }
430 std::set<int32_t> result;
431 for (AudioRoute* r : *routes) {
432 if (r->sinkPortId == portId) {
433 result.insert(r->sourcePortIds.begin(), r->sourcePortIds.end());
434 } else {
435 result.insert(r->sinkPortId);
436 }
437 }
438 return result;
439}
440
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000441void Module::registerPatch(const AudioPatch& patch) {
442 auto& configs = getConfig().portConfigs;
443 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
444 for (auto portConfigId : portConfigIds) {
445 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
446 if (configIt != configs.end()) {
447 mPatches.insert(std::pair{portConfigId, patch.id});
448 if (configIt->portId != portConfigId) {
449 mPatches.insert(std::pair{configIt->portId, patch.id});
450 }
451 }
452 };
453 };
454 do_insert(patch.sourcePortConfigIds);
455 do_insert(patch.sinkPortConfigIds);
456}
457
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700458ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
459 const AudioPatch& newPatch) {
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700460 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700461 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700462 using Connections =
463 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
464 Connections oldConnections, newConnections;
465 auto fillConnectionsHelper = [&](Connections& connections,
466 const std::vector<int32_t>& mixPortCfgIds,
467 const std::vector<int32_t>& devicePortCfgIds) {
468 for (int32_t mixPortCfgId : mixPortCfgIds) {
469 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
470 }
471 };
472 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
473 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
474 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
475 patch.sourcePortConfigIds.end()) {
476 // Sources are mix ports.
477 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
478 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
479 [&](int32_t portConfigId) {
480 return mStreams.count(portConfigId) > 0;
481 }) != patch.sinkPortConfigIds.end()) {
482 // Sources are device ports.
483 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
484 } // Otherwise, there are no streams to notify.
485 };
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530486 auto restoreOldConnections = [&](const std::set<int32_t>& mixPortIds,
487 const bool continueWithEmptyDevices) {
488 for (const auto mixPort : mixPortIds) {
489 if (auto it = oldConnections.find(mixPort);
490 continueWithEmptyDevices || it != oldConnections.end()) {
491 const std::vector<AudioDevice> d =
492 it != oldConnections.end() ? getDevicesFromDevicePortConfigIds(it->second)
493 : std::vector<AudioDevice>();
494 if (auto status = mStreams.setStreamConnectedDevices(mixPort, d); status.isOk()) {
495 LOG(WARNING) << ":updateStreamsConnectedState: rollback: mix port config:"
496 << mixPort
497 << (d.empty() ? "; not connected"
498 : std::string("; connected to ") +
499 ::android::internal::ToString(d));
500 } else {
501 // can't do much about rollback failures
502 LOG(ERROR)
503 << ":updateStreamsConnectedState: rollback: failed for mix port config:"
504 << mixPort;
505 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700506 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000507 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530508 };
509 fillConnections(oldConnections, oldPatch);
510 fillConnections(newConnections, newPatch);
511 /**
512 * Illustration of oldConnections and newConnections
513 *
514 * oldConnections {
515 * a : {A,B,C},
516 * b : {D},
517 * d : {H,I,J},
518 * e : {N,O,P},
519 * f : {Q,R},
520 * g : {T,U,V},
521 * }
522 *
523 * newConnections {
524 * a : {A,B,C},
525 * c : {E,F,G},
526 * d : {K,L,M},
527 * e : {N,P},
528 * f : {Q,R,S},
529 * g : {U,V,W},
530 * }
531 *
532 * Expected routings:
533 * 'a': is ignored both in disconnect step and connect step,
534 * due to same devices both in oldConnections and newConnections.
535 * 'b': handled only in disconnect step with empty devices because 'b' is only present
536 * in oldConnections.
537 * 'c': handled only in connect step with {E,F,G} devices because 'c' is only present
538 * in newConnections.
539 * 'd': handled only in connect step with {K,L,M} devices because 'd' is also present
540 * in newConnections and it is ignored in disconnected step.
541 * 'e': handled only in connect step with {N,P} devices because 'e' is also present
542 * in newConnections and it is ignored in disconnect step. please note that there
543 * is no exclusive disconnection for device {O}.
544 * 'f': handled only in connect step with {Q,R,S} devices because 'f' is also present
545 * in newConnections and it is ignored in disconnect step. Even though stream is
546 * already connected with {Q,R} devices and connection happens with {Q,R,S}.
547 * 'g': handled only in connect step with {U,V,W} devices because 'g' is also present
548 * in newConnections and it is ignored in disconnect step. There is no exclusive
549 * disconnection with devices {T,U,V}.
550 *
551 * If, any failure, will lead to restoreOldConnections (rollback).
552 * The aim of the restoreOldConnections is to make connections back to oldConnections.
553 * Failures in restoreOldConnections aren't handled.
554 */
555
556 std::set<int32_t> idsToConnectBackOnFailure;
557 // disconnection step
558 for (const auto& [oldMixPortConfigId, oldDevicePortConfigIds] : oldConnections) {
559 if (auto it = newConnections.find(oldMixPortConfigId); it == newConnections.end()) {
560 idsToConnectBackOnFailure.insert(oldMixPortConfigId);
561 if (auto status = mStreams.setStreamConnectedDevices(oldMixPortConfigId, {});
562 status.isOk()) {
563 LOG(DEBUG) << __func__ << ": The stream on port config id " << oldMixPortConfigId
564 << " has been disconnected";
565 } else {
566 maybeFailure = std::move(status);
567 // proceed to rollback even on one failure
568 break;
569 }
570 }
571 }
572
573 if (!maybeFailure.isOk()) {
574 restoreOldConnections(idsToConnectBackOnFailure, false /*continueWithEmptyDevices*/);
575 LOG(WARNING) << __func__ << ": failed to disconnect from old patch. attempted rollback";
576 return maybeFailure;
577 }
578
579 std::set<int32_t> idsToRollbackOnFailure;
580 // connection step
581 for (const auto& [newMixPortConfigId, newDevicePortConfigIds] : newConnections) {
582 if (auto it = oldConnections.find(newMixPortConfigId);
583 it == oldConnections.end() || it->second != newDevicePortConfigIds) {
584 const auto connectedDevices = getDevicesFromDevicePortConfigIds(newDevicePortConfigIds);
585 idsToRollbackOnFailure.insert(newMixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700586 if (connectedDevices.empty()) {
587 // This is important as workers use the vector size to derive the connection status.
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530588 LOG(FATAL) << __func__ << ": No connected devices found for port config id "
589 << newMixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700590 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530591 if (auto status =
592 mStreams.setStreamConnectedDevices(newMixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700593 status.isOk()) {
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530594 LOG(DEBUG) << __func__ << ": The stream on port config id " << newMixPortConfigId
595 << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700596 << ::android::internal::ToString(connectedDevices);
597 } else {
598 maybeFailure = std::move(status);
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530599 // proceed to rollback even on one failure
600 break;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700601 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000602 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530603 }
604
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700605 if (!maybeFailure.isOk()) {
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530606 restoreOldConnections(idsToConnectBackOnFailure, false /*continueWithEmptyDevices*/);
607 restoreOldConnections(idsToRollbackOnFailure, true /*continueWithEmptyDevices*/);
608 LOG(WARNING) << __func__ << ": failed to connect for new patch. attempted rollback";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700609 return maybeFailure;
610 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530611
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700612 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000613}
614
Mikhail Naganov00603d12022-05-02 22:52:13 +0000615ndk::ScopedAStatus Module::setModuleDebug(
616 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700617 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000618 << ", new flags: " << in_debug.toString();
619 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
620 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700621 LOG(ERROR) << __func__ << ": " << mType
Jaideep Sharma559a4912024-03-07 10:05:51 +0530622 << ": attempting to change device connections simulation while "
623 "having external "
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700624 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000625 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
626 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000627 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700628 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000629 << in_debug.streamTransientStateDelayMs;
630 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
631 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000632 mDebug = in_debug;
633 return ndk::ScopedAStatus::ok();
634}
635
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000636ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700637 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530638 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000639 return ndk::ScopedAStatus::ok();
640}
641
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000642ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700643 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530644 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000645 return ndk::ScopedAStatus::ok();
646}
647
Mikhail Naganov7499a002023-02-27 18:51:44 -0800648ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700649 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530650 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800651 return ndk::ScopedAStatus::ok();
652}
653
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800654ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700655 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530656 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800657 return ndk::ScopedAStatus::ok();
658}
659
Mikhail Naganov00603d12022-05-02 22:52:13 +0000660ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
661 AudioPort* _aidl_return) {
662 const int32_t templateId = in_templateIdAndAdditionalData.id;
663 auto& ports = getConfig().ports;
664 AudioPort connectedPort;
665 { // Scope the template port so that we don't accidentally modify it.
666 auto templateIt = findById<AudioPort>(ports, templateId);
667 if (templateIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530668 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000669 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
670 }
671 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530672 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
673 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000674 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
675 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000676 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
677 if (templateDevicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530678 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
679 << " is permanently attached";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000680 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
681 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700682 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530683 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
684 << " is a connected device port";
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700685 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
686 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000687 // Postpone id allocation until we ensure that there are no client errors.
688 connectedPort = *templateIt;
689 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
690 const auto& inputDevicePort =
691 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
692 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
693 connectedDevicePort.device.address = inputDevicePort.device.address;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530694 LOG(DEBUG) << __func__ << ": " << mType << ": device port " << connectedPort.id
695 << " device set to " << connectedDevicePort.device.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000696 // Check if there is already a connected port with for the same external device.
Jaideep Sharma559a4912024-03-07 10:05:51 +0530697
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700698 for (auto connectedPortPair : mConnectedDevicePorts) {
699 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000700 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
701 connectedDevicePort.device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530702 LOG(ERROR) << __func__ << ": " << mType << ": device "
703 << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700704 << " is already connected at the device port id "
705 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000706 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
707 }
708 }
709 }
710
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700711 // Two main cases are considered with regard to the profiles of the connected device port:
712 //
713 // 1. If the template device port has dynamic profiles, and at least one routable mix
714 // port also has dynamic profiles, it means that after connecting the device, the
715 // connected device port must have profiles populated with actual capabilities of
716 // the connected device, and dynamic of routable mix ports will be filled
717 // according to these capabilities. An example of this case is connection of an
718 // HDMI or USB device. For USB handled by ADSP, there can be mix ports with static
719 // profiles, and one dedicated mix port for "hi-fi" playback. The latter is left with
720 // dynamic profiles so that they can be populated with actual capabilities of
721 // the connected device.
722 //
723 // 2. If the template device port has dynamic profiles, while all routable mix ports
724 // have static profiles, it means that after connecting the device, the connected
725 // device port can be left with dynamic profiles, and profiles of mix ports are
726 // left untouched. An example of this case is connection of an analog wired
727 // headset, it should be treated in the same way as a speaker.
728 //
729 // Yet another possible case is when both the template device port and all routable
730 // mix ports have static profiles. This is allowed and handled correctly, however, it
731 // is not very practical, since these profiles are likely duplicates of each other.
732
733 std::vector<AudioRoute*> routesToMixPorts = getAudioRoutesForAudioPortImpl(templateId);
734 std::set<int32_t> routableMixPortIds = getRoutableAudioPortIds(templateId, &routesToMixPorts);
Mikhail Naganova92039a2023-12-20 14:27:22 -0800735 const int32_t nextPortId = getConfig().nextPortId++;
Mikhail Naganov55045b52023-10-24 17:03:50 -0700736 if (!mDebug.simulateDeviceConnections) {
737 // Even if the device port has static profiles, the HAL module might need to update
738 // them, or abort the connection process.
Mikhail Naganova92039a2023-12-20 14:27:22 -0800739 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort, nextPortId));
Mikhail Naganov55045b52023-10-24 17:03:50 -0700740 } else if (hasDynamicProfilesOnly(connectedPort.profiles)) {
741 auto& connectedProfiles = getConfig().connectedProfiles;
742 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
743 connectedProfilesIt != connectedProfiles.end()) {
744 connectedPort.profiles = connectedProfilesIt->second;
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700745 }
Mikhail Naganov55045b52023-10-24 17:03:50 -0700746 }
747 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
748 // Possible case 2. Check if all routable mix ports have static profiles.
749 if (auto dynamicMixPortIt = std::find_if(ports.begin(), ports.end(),
750 [&routableMixPortIds](const auto& p) {
751 return routableMixPortIds.count(p.id) > 0 &&
752 hasDynamicProfilesOnly(p.profiles);
753 });
754 dynamicMixPortIt != ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530755 LOG(ERROR) << __func__ << ": " << mType
756 << ": connected port only has dynamic profiles after connecting "
Mikhail Naganov55045b52023-10-24 17:03:50 -0700757 << "external device " << connectedPort.toString() << ", and there exist "
758 << "a routable mix port with dynamic profiles: "
759 << dynamicMixPortIt->toString();
760 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530761 }
762 }
763
Mikhail Naganova92039a2023-12-20 14:27:22 -0800764 connectedPort.id = nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700765 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700766 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Jaideep Sharma559a4912024-03-07 10:05:51 +0530767 LOG(DEBUG) << __func__ << ": " << mType << ": template port " << templateId
768 << " external device connected, "
Mikhail Naganov00603d12022-05-02 22:52:13 +0000769 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000770 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000771 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000772
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700773 // For routes where the template port is a source, add the connected port to sources,
774 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000775 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700776 for (AudioRoute* r : routesToMixPorts) {
777 if (r->sinkPortId == templateId) {
778 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
779 .sinkPortId = connectedPort.id,
780 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000781 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700782 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000783 }
784 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700785 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000786 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
787
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700788 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
789 // Note: this is a simplistic approach assuming that a mix port can only be populated
790 // from a single device port. Implementing support for stuffing dynamic profiles with
791 // a superset of all profiles from all routable dynamic device ports would be more involved.
792 for (auto& port : ports) {
793 if (routableMixPortIds.count(port.id) == 0) continue;
794 if (hasDynamicProfilesOnly(port.profiles)) {
795 port.profiles = connectedPort.profiles;
796 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700797 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700798 // Check if profiles are not all dynamic because they were populated by
799 // a previous connection. Otherwise, it means that they are actually static.
800 for (const auto& cp : mConnectedDevicePorts) {
801 if (cp.second.count(port.id) > 0) {
802 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700803 break;
804 }
805 }
806 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700807 }
808 }
809 *_aidl_return = std::move(connectedPort);
810
Mikhail Naganov00603d12022-05-02 22:52:13 +0000811 return ndk::ScopedAStatus::ok();
812}
813
814ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
815 auto& ports = getConfig().ports;
816 auto portIt = findById<AudioPort>(ports, in_portId);
817 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530818 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000819 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
820 }
821 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530822 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
823 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000824 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
825 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700826 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
827 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530828 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
829 << " is not a connected device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000830 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
831 }
832 auto& configs = getConfig().portConfigs;
833 auto& initials = getConfig().initialConfigs;
834 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
835 if (config.portId == in_portId) {
836 // Check if the configuration was provided by the client.
837 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
838 return initialIt == initials.end() || config != *initialIt;
839 }
840 return false;
841 });
842 if (configIt != configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530843 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
844 << " has a non-default config with id " << configIt->id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000845 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
846 }
jiabin783c48b2023-02-28 18:28:06 +0000847 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000848 ports.erase(portIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +0530849 LOG(DEBUG) << __func__ << ": " << mType << ": connected device port " << in_portId
850 << " released";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000851
852 auto& routes = getConfig().routes;
853 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
854 if (routesIt->sinkPortId == in_portId) {
855 routesIt = routes.erase(routesIt);
856 } else {
857 // Note: the list of sourcePortIds can't become empty because there must
858 // be the id of the template port in the route.
859 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
860 ++routesIt;
861 }
862 }
863
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700864 // Clear profiles for mix ports that are not connected to any other ports.
865 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
866 mConnectedDevicePorts.erase(connectedPortsIt);
867 for (const auto& connectedPort : mConnectedDevicePorts) {
868 for (int32_t mixPortId : connectedPort.second) {
869 mixPortsToClear.erase(mixPortId);
870 }
871 }
872 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700873 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
874 if (mixPortIt != ports.end()) {
875 mixPortIt->profiles = {};
876 }
877 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700878
Mikhail Naganov00603d12022-05-02 22:52:13 +0000879 return ndk::ScopedAStatus::ok();
880}
881
jiabindd23b0e2023-12-11 19:10:05 +0000882ndk::ScopedAStatus Module::prepareToDisconnectExternalDevice(int32_t in_portId) {
883 auto& ports = getConfig().ports;
884 auto portIt = findById<AudioPort>(ports, in_portId);
885 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530886 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
jiabindd23b0e2023-12-11 19:10:05 +0000887 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
888 }
889 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530890 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
891 << " is not a device port";
jiabindd23b0e2023-12-11 19:10:05 +0000892 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
893 }
894 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
895 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530896 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
897 << " is not a connected device port";
jiabindd23b0e2023-12-11 19:10:05 +0000898 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
899 }
900
901 onPrepareToDisconnectExternalDevice(*portIt);
902
903 return ndk::ScopedAStatus::ok();
904}
905
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000906ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
907 *_aidl_return = getConfig().patches;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530908 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " patches";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000909 return ndk::ScopedAStatus::ok();
910}
911
912ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
913 auto& ports = getConfig().ports;
914 auto portIt = findById<AudioPort>(ports, in_portId);
915 if (portIt != ports.end()) {
916 *_aidl_return = *portIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530917 LOG(DEBUG) << __func__ << ": " << mType << ": returning port by id " << in_portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000918 return ndk::ScopedAStatus::ok();
919 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530920 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000921 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
922}
923
924ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
925 *_aidl_return = getConfig().portConfigs;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530926 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size()
927 << " port configs";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000928 return ndk::ScopedAStatus::ok();
929}
930
931ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
932 *_aidl_return = getConfig().ports;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530933 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " ports";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000934 return ndk::ScopedAStatus::ok();
935}
936
937ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
938 *_aidl_return = getConfig().routes;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530939 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " routes";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000940 return ndk::ScopedAStatus::ok();
941}
942
Mikhail Naganov00603d12022-05-02 22:52:13 +0000943ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
944 std::vector<AudioRoute>* _aidl_return) {
945 auto& ports = getConfig().ports;
946 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530947 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000948 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
949 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700950 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
951 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
952 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000953 return ndk::ScopedAStatus::ok();
954}
955
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000956ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
957 OpenInputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530958 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
959 << ", buffer size " << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000960 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700961 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000962 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530963 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000964 << " does not correspond to an input mix port";
965 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
966 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000967 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700968 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
969 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000970 context.fillDescriptor(&_aidl_return->desc);
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800971 if (hasMmapFlag(context.getFlags())) {
972 RETURN_STATUS_IF_ERROR(createMmapBuffer(context, &_aidl_return->desc));
973 }
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000974 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700975 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000976 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000977 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700978 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
979 RETURN_STATUS_IF_ERROR(
980 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
981 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +0000982 auto streamBinder = streamWrapper.getBinder();
983 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
984 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000985 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000986 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000987 return ndk::ScopedAStatus::ok();
988}
989
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000990ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
991 OpenOutputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530992 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
993 << ", has offload info? " << (in_args.offloadInfo.has_value()) << ", buffer size "
994 << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000995 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700996 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000997 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530998 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000999 << " does not correspond to an output mix port";
1000 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1001 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +00001002 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
1003 AudioOutputFlags::COMPRESS_OFFLOAD);
1004 if (isOffload && !in_args.offloadInfo.has_value()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301005 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +00001006 << " has COMPRESS_OFFLOAD flag set, requires offload info";
1007 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1008 }
Mikhail Naganov30301a42022-09-13 01:20:45 +00001009 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
1010 AudioOutputFlags::NON_BLOCKING);
1011 if (isNonBlocking && in_args.callback == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301012 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov30301a42022-09-13 01:20:45 +00001013 << " has NON_BLOCKING flag set, requires async callback";
1014 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1015 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001016 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001017 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
1018 isNonBlocking ? in_args.callback : nullptr,
1019 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001020 context.fillDescriptor(&_aidl_return->desc);
Kuowei Li53a8d4d2024-06-24 14:35:07 +08001021 if (hasMmapFlag(context.getFlags())) {
1022 RETURN_STATUS_IF_ERROR(createMmapBuffer(context, &_aidl_return->desc));
1023 }
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001024 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -07001025 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -07001026 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001027 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001028 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
1029 RETURN_STATUS_IF_ERROR(
1030 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
1031 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +00001032 auto streamBinder = streamWrapper.getBinder();
1033 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
1034 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001035 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001036 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001037 return ndk::ScopedAStatus::ok();
1038}
1039
Mikhail Naganov74927202022-12-19 16:37:14 +00001040ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
1041 SupportedPlaybackRateFactors* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301042 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganov74927202022-12-19 16:37:14 +00001043 (void)_aidl_return;
1044 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1045}
1046
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001047ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301048 LOG(DEBUG) << __func__ << ": " << mType << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001049 if (in_requested.sourcePortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301050 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001051 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1052 }
1053 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301054 LOG(ERROR) << __func__ << ": " << mType
1055 << ": requested patch has duplicate ids in the sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001056 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1057 }
1058 if (in_requested.sinkPortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301059 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001060 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1061 }
1062 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301063 LOG(ERROR) << __func__ << ": " << mType
1064 << ": requested patch has duplicate ids in the sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001065 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1066 }
1067
1068 auto& configs = getConfig().portConfigs;
1069 std::vector<int32_t> missingIds;
1070 auto sources =
1071 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
1072 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301073 LOG(ERROR) << __func__ << ": " << mType << ": following source port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001074 << ::android::internal::ToString(missingIds);
1075 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1076 }
1077 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
1078 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301079 LOG(ERROR) << __func__ << ": " << mType << ": following sink port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001080 << ::android::internal::ToString(missingIds);
1081 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1082 }
1083 // bool indicates whether a non-exclusive route is available.
1084 // If only an exclusive route is available, that means the patch can not be
1085 // established if there is any other patch which currently uses the sink port.
1086 std::map<int32_t, bool> allowedSinkPorts;
1087 auto& routes = getConfig().routes;
1088 for (auto src : sources) {
1089 for (const auto& r : routes) {
1090 const auto& srcs = r.sourcePortIds;
1091 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
1092 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
1093 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
1094 }
1095 }
1096 }
1097 }
1098 for (auto sink : sinks) {
1099 if (allowedSinkPorts.count(sink->portId) == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301100 LOG(ERROR) << __func__ << ": " << mType << ": there is no route to the sink port id "
1101 << sink->portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001102 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1103 }
1104 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001105 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +00001106
Priyanka Advani (xWF)781f66a2024-12-05 22:51:28 +00001107 auto& patches = getConfig().patches;
1108 auto existing = patches.end();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001109 std::optional<decltype(mPatches)> patchesBackup;
Priyanka Advani (xWF)781f66a2024-12-05 22:51:28 +00001110 if (in_requested.id != 0) {
1111 existing = findById<AudioPatch>(patches, in_requested.id);
1112 if (existing != patches.end()) {
1113 patchesBackup = mPatches;
1114 cleanUpPatch(existing->id);
1115 } else {
1116 LOG(ERROR) << __func__ << ": " << mType << ": not found existing patch id "
1117 << in_requested.id;
1118 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1119 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001120 }
1121 // Validate the requested patch.
1122 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
1123 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301124 LOG(ERROR) << __func__ << ": " << mType << ": sink port id " << sinkPortId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001125 << "is exclusive and is already used by some other patch";
1126 if (patchesBackup.has_value()) {
1127 mPatches = std::move(*patchesBackup);
1128 }
1129 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1130 }
1131 }
Mikhail Naganov13501872023-10-18 16:15:46 -07001132 // Find the highest sample rate among mix port configs.
1133 std::map<int32_t, AudioPortConfig*> sampleRates;
1134 std::vector<AudioPortConfig*>& mixPortConfigs =
1135 sources[0]->ext.getTag() == AudioPortExt::mix ? sources : sinks;
1136 for (auto mix : mixPortConfigs) {
1137 sampleRates.emplace(mix->sampleRate.value().value, mix);
1138 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001139 *_aidl_return = in_requested;
Mikhail Naganov13501872023-10-18 16:15:46 -07001140 auto maxSampleRateIt = std::max_element(sampleRates.begin(), sampleRates.end());
1141 const int32_t latencyMs = getNominalLatencyMs(*(maxSampleRateIt->second));
Kuowei Li10a768a2024-12-09 17:44:40 +08001142 if (!calculateBufferSizeFrames(
1143 maxSampleRateIt->second->format.value(), latencyMs, maxSampleRateIt->first,
1144 &_aidl_return->minimumStreamBufferSizeFrames).isOk()) {
1145 if (patchesBackup.has_value()) {
1146 mPatches = std::move(*patchesBackup);
1147 }
1148 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1149 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001150 _aidl_return->latenciesMs.clear();
1151 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
Mikhail Naganov13501872023-10-18 16:15:46 -07001152 _aidl_return->sinkPortConfigIds.size(), latencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001153 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001154 if (existing == patches.end()) {
1155 _aidl_return->id = getConfig().nextPatchId++;
1156 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001157 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001158 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -07001159 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001160 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001161 patchesBackup = mPatches;
1162 registerPatch(*_aidl_return);
1163 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
1164 mPatches = std::move(*patchesBackup);
1165 if (existing == patches.end()) {
1166 patches.pop_back();
1167 } else {
1168 *existing = oldPatch;
1169 }
1170 return status;
1171 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001172
Jaideep Sharma559a4912024-03-07 10:05:51 +05301173 LOG(DEBUG) << __func__ << ": " << mType << ": " << (oldPatch.id == 0 ? "created" : "updated")
1174 << " patch " << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001175 return ndk::ScopedAStatus::ok();
1176}
1177
1178ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
1179 AudioPortConfig* out_suggested, bool* _aidl_return) {
Mikhail Naganova92039a2023-12-20 14:27:22 -08001180 auto generate = [this](const AudioPort& port, AudioPortConfig* config) {
1181 return generateDefaultPortConfig(port, config);
1182 };
1183 return setAudioPortConfigImpl(in_requested, generate, out_suggested, _aidl_return);
1184}
1185
1186ndk::ScopedAStatus Module::setAudioPortConfigImpl(
1187 const AudioPortConfig& in_requested,
1188 const std::function<bool(const ::aidl::android::media::audio::common::AudioPort& port,
1189 ::aidl::android::media::audio::common::AudioPortConfig* config)>&
1190 fillPortConfig,
1191 AudioPortConfig* out_suggested, bool* applied) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301192 LOG(DEBUG) << __func__ << ": " << mType << ": requested " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001193 auto& configs = getConfig().portConfigs;
1194 auto existing = configs.end();
1195 if (in_requested.id != 0) {
1196 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1197 existing == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301198 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id "
1199 << in_requested.id << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001200 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1201 }
1202 }
1203
1204 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1205 if (portId == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301206 LOG(ERROR) << __func__ << ": " << mType
1207 << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001208 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1209 }
1210 auto& ports = getConfig().ports;
1211 auto portIt = findById<AudioPort>(ports, portId);
1212 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301213 LOG(ERROR) << __func__ << ": " << mType
1214 << ": requested port config points to non-existent portId " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001215 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1216 }
1217 if (existing != configs.end()) {
1218 *out_suggested = *existing;
1219 } else {
1220 AudioPortConfig newConfig;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001221 newConfig.portId = portIt->id;
1222 if (fillPortConfig(*portIt, &newConfig)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001223 *out_suggested = newConfig;
1224 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301225 LOG(ERROR) << __func__ << ": " << mType
1226 << ": unable generate a default config for port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001227 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1228 }
1229 }
1230 // From this moment, 'out_suggested' is either an existing port config,
1231 // or a new generated config. Now attempt to update it according to the specified
1232 // fields of 'in_requested'.
1233
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001234 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1235 // which takes care of their actual configuration automatically.
1236 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1237 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001238 bool requestedIsValid = true, requestedIsFullySpecified = true;
1239
1240 AudioIoFlags portFlags = portIt->flags;
1241 if (in_requested.flags.has_value()) {
1242 if (in_requested.flags.value() != portFlags) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301243 LOG(WARNING) << __func__ << ": " << mType << ": requested flags "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001244 << in_requested.flags.value().toString() << " do not match port's "
1245 << portId << " flags " << portFlags.toString();
1246 requestedIsValid = false;
1247 }
1248 } else {
1249 requestedIsFullySpecified = false;
1250 }
1251
1252 AudioProfile portProfile;
1253 if (in_requested.format.has_value()) {
1254 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001255 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1256 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001257 out_suggested->format = format;
1258 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301259 LOG(WARNING) << __func__ << ": " << mType << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001260 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001261 requestedIsValid = false;
1262 }
1263 } else {
1264 requestedIsFullySpecified = false;
1265 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001266 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1267 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301268 LOG(ERROR) << __func__ << ": " << mType << ": port " << portId
1269 << " does not support format " << out_suggested->format.value().toString()
1270 << " anymore";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001271 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1272 }
1273
1274 if (in_requested.channelMask.has_value()) {
1275 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001276 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1277 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1278 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001279 out_suggested->channelMask = channelMask;
1280 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301281 LOG(WARNING) << __func__ << ": " << mType << ": requested channel mask "
1282 << channelMask.toString() << " is not supported for the format "
1283 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001284 requestedIsValid = false;
1285 }
1286 } else {
1287 requestedIsFullySpecified = false;
1288 }
1289
1290 if (in_requested.sampleRate.has_value()) {
1291 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001292 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1293 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001294 sampleRate.value) != portProfile.sampleRates.end()) {
1295 out_suggested->sampleRate = sampleRate;
1296 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301297 LOG(WARNING) << __func__ << ": " << mType << ": requested sample rate "
1298 << sampleRate.value << " is not supported for the format "
1299 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001300 requestedIsValid = false;
1301 }
1302 } else {
1303 requestedIsFullySpecified = false;
1304 }
1305
1306 if (in_requested.gain.has_value()) {
Weilin Xua33bb5e2024-10-02 17:16:42 +00001307 if (!setAudioPortConfigGain(*portIt, in_requested.gain.value())) {
1308 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1309 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001310 out_suggested->gain = in_requested.gain.value();
1311 }
1312
Mikhail Naganov248e9502023-02-21 16:32:40 -08001313 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1314 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1315 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
Mikhail Naganovb06a4922024-03-06 16:39:50 -08001316 // 'AudioMixPortExt.handle' and '.usecase' are set by the client,
1317 // copy from in_requested.
1318 const auto& src = in_requested.ext.get<AudioPortExt::Tag::mix>();
1319 auto& dst = out_suggested->ext.get<AudioPortExt::Tag::mix>();
1320 dst.handle = src.handle;
1321 dst.usecase = src.usecase;
Mikhail Naganov248e9502023-02-21 16:32:40 -08001322 }
1323 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301324 LOG(WARNING) << __func__ << ": " << mType << ": requested ext tag "
Mikhail Naganov248e9502023-02-21 16:32:40 -08001325 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1326 << toString(out_suggested->ext.getTag());
1327 requestedIsValid = false;
1328 }
1329 }
1330
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001331 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1332 out_suggested->id = getConfig().nextPortId++;
1333 configs.push_back(*out_suggested);
Mikhail Naganova92039a2023-12-20 14:27:22 -08001334 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301335 LOG(DEBUG) << __func__ << ": " << mType << ": created new port config "
1336 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001337 } else if (existing != configs.end() && requestedIsValid) {
1338 *existing = *out_suggested;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001339 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301340 LOG(DEBUG) << __func__ << ": " << mType << ": updated port config "
1341 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001342 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301343 LOG(DEBUG) << __func__ << ": " << mType << ": not applied; existing config ? "
1344 << (existing != configs.end()) << "; requested is valid? " << requestedIsValid
1345 << ", fully specified? " << requestedIsFullySpecified;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001346 *applied = false;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001347 }
1348 return ndk::ScopedAStatus::ok();
1349}
1350
Weilin Xua33bb5e2024-10-02 17:16:42 +00001351bool Module::setAudioPortConfigGain(const AudioPort& port, const AudioGainConfig& gainRequested) {
1352 auto& ports = getConfig().ports;
1353 if (gainRequested.index < 0 || gainRequested.index >= (int)port.gains.size()) {
1354 LOG(ERROR) << __func__ << ": gains for port " << port.id << " is undefined";
1355 return false;
1356 }
1357 int stepValue = port.gains[gainRequested.index].stepValue;
1358 if (stepValue == 0) {
1359 LOG(ERROR) << __func__ << ": port gain step value is 0";
1360 return false;
1361 }
1362 int minValue = port.gains[gainRequested.index].minValue;
1363 int maxValue = port.gains[gainRequested.index].maxValue;
1364 if (gainRequested.values[0] > maxValue || gainRequested.values[0] < minValue) {
1365 LOG(ERROR) << __func__ << ": gain value " << gainRequested.values[0]
1366 << " out of range of min and max gain config";
1367 return false;
1368 }
1369 int gainIndex = (gainRequested.values[0] - minValue) / stepValue;
1370 int totalSteps = (maxValue - minValue) / stepValue;
1371 if (totalSteps == 0) {
1372 LOG(ERROR) << __func__ << ": difference between port gain min value " << minValue
1373 << " and max value " << maxValue << " is less than step value " << stepValue;
1374 return false;
1375 }
1376 // Root-power quantities are used in curve:
1377 // 10^((minMb / 100 + (maxMb / 100 - minMb / 100) * gainIndex / totalSteps) / (10 * 2))
1378 // where 100 is the conversion from mB to dB, 10 comes from the log 10 conversion from power
1379 // ratios, and 2 means are the square of amplitude.
1380 float gain =
1381 pow(10, (minValue + (maxValue - minValue) * (gainIndex / (float)totalSteps)) / 2000);
1382 if (gain < 0) {
1383 LOG(ERROR) << __func__ << ": gain " << gain << " is less than 0";
1384 return false;
1385 }
1386 for (const auto& route : getConfig().routes) {
1387 if (route.sinkPortId != port.id) {
1388 continue;
1389 }
1390 for (const auto sourcePortId : route.sourcePortIds) {
1391 mStreams.setGain(sourcePortId, gain);
1392 }
1393 }
1394 return true;
1395}
1396
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001397ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1398 auto& patches = getConfig().patches;
1399 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1400 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001401 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001402 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001403 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1404 mPatches = std::move(patchesBackup);
1405 return status;
1406 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001407 patches.erase(patchIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301408 LOG(DEBUG) << __func__ << ": " << mType << ": erased patch " << in_patchId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001409 return ndk::ScopedAStatus::ok();
1410 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301411 LOG(ERROR) << __func__ << ": " << mType << ": patch id " << in_patchId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001412 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1413}
1414
1415ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1416 auto& configs = getConfig().portConfigs;
1417 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1418 if (configIt != configs.end()) {
1419 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301420 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001421 << " has a stream opened on it";
1422 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1423 }
1424 auto patchIt = mPatches.find(in_portConfigId);
1425 if (patchIt != mPatches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301426 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001427 << " is used by the patch with id " << patchIt->second;
1428 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1429 }
1430 auto& initials = getConfig().initialConfigs;
1431 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1432 if (initialIt == initials.end()) {
1433 configs.erase(configIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301434 LOG(DEBUG) << __func__ << ": " << mType << ": erased port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001435 } else if (*configIt != *initialIt) {
1436 *configIt = *initialIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301437 LOG(DEBUG) << __func__ << ": " << mType << ": reset port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001438 }
1439 return ndk::ScopedAStatus::ok();
1440 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301441 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
1442 << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001443 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1444}
1445
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001446ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1447 *_aidl_return = mMasterMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301448 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001449 return ndk::ScopedAStatus::ok();
1450}
1451
1452ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301453 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001454 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1455 : onMasterMuteChanged(in_mute);
1456 if (result.isOk()) {
1457 mMasterMute = in_mute;
1458 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301459 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterMuteChanged("
1460 << in_mute << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001461 // Reset master mute if it failed.
1462 onMasterMuteChanged(mMasterMute);
1463 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001464 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001465}
1466
1467ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1468 *_aidl_return = mMasterVolume;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301469 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001470 return ndk::ScopedAStatus::ok();
1471}
1472
1473ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301474 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001475 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001476 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1477 : onMasterVolumeChanged(in_volume);
1478 if (result.isOk()) {
1479 mMasterVolume = in_volume;
1480 } else {
1481 // Reset master volume if it failed.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301482 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterVolumeChanged("
1483 << in_volume << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001484 onMasterVolumeChanged(mMasterVolume);
1485 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001486 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001487 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301488 LOG(ERROR) << __func__ << ": " << mType << ": invalid master volume value: " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001489 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1490}
1491
1492ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1493 *_aidl_return = mMicMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301494 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001495 return ndk::ScopedAStatus::ok();
1496}
1497
1498ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301499 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001500 mMicMute = in_mute;
1501 return ndk::ScopedAStatus::ok();
1502}
1503
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001504ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001505 *_aidl_return = getMicrophoneInfos();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301506 LOG(DEBUG) << __func__ << ": " << mType << ": returning "
1507 << ::android::internal::ToString(*_aidl_return);
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001508 return ndk::ScopedAStatus::ok();
1509}
1510
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001511ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001512 if (!isValidAudioMode(in_mode)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301513 LOG(ERROR) << __func__ << ": " << mType << ": invalid mode " << toString(in_mode);
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001514 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1515 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001516 // No checks for supported audio modes here, it's an informative notification.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301517 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_mode);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001518 return ndk::ScopedAStatus::ok();
1519}
1520
1521ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301522 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_rotation);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001523 return ndk::ScopedAStatus::ok();
1524}
1525
1526ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301527 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_isTurnedOn;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001528 return ndk::ScopedAStatus::ok();
1529}
1530
Vlad Popa83a6d822022-11-07 13:53:57 +01001531ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001532 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001533 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001534 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001535 *_aidl_return = mSoundDose.getInstance();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301536 LOG(DEBUG) << __func__ << ": " << mType
1537 << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001538 return ndk::ScopedAStatus::ok();
1539}
1540
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001541ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301542 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001543 (void)_aidl_return;
1544 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1545}
1546
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001547const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001548const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganove87a1b92024-11-12 15:31:55 -08001549const std::string Module::VendorDebug::kForceDrainToDrainingName = "aosp.forceDrainToDraining";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001550
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001551ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1552 std::vector<VendorParameter>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301553 LOG(VERBOSE) << __func__ << ": " << mType << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001554 bool allParametersKnown = true;
1555 for (const auto& id : in_ids) {
1556 if (id == VendorDebug::kForceTransientBurstName) {
1557 VendorParameter forceTransientBurst{.id = id};
1558 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1559 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001560 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1561 VendorParameter forceSynchronousDrain{.id = id};
1562 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1563 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganove87a1b92024-11-12 15:31:55 -08001564 } else if (id == VendorDebug::kForceDrainToDrainingName) {
1565 VendorParameter forceDrainToDraining{.id = id};
1566 forceDrainToDraining.ext.setParcelable(Boolean{mVendorDebug.forceDrainToDraining});
1567 _aidl_return->push_back(std::move(forceDrainToDraining));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001568 } else {
1569 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301570 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << id << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001571 }
1572 }
1573 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1574 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001575}
1576
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001577namespace {
1578
1579template <typename W>
1580bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1581 std::optional<W> value;
1582 binder_status_t result = p.ext.getParcelable(&value);
1583 if (result == STATUS_OK && value.has_value()) {
1584 *v = value.value().value;
1585 return true;
1586 }
1587 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1588 << "\": " << result;
1589 return false;
1590}
1591
1592} // namespace
1593
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001594ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1595 bool in_async) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301596 LOG(VERBOSE) << __func__ << ": " << mType << ": parameter count " << in_parameters.size()
1597 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001598 bool allParametersKnown = true;
1599 for (const auto& p : in_parameters) {
1600 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001601 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1602 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1603 }
1604 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1605 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001606 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1607 }
Mikhail Naganove87a1b92024-11-12 15:31:55 -08001608 } else if (p.id == VendorDebug::kForceDrainToDrainingName) {
1609 if (!extractParameter<Boolean>(p, &mVendorDebug.forceDrainToDraining)) {
1610 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1611 }
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001612 } else {
1613 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301614 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << p.id
1615 << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001616 }
1617 }
1618 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1619 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001620}
1621
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001622ndk::ScopedAStatus Module::addDeviceEffect(
1623 int32_t in_portConfigId,
1624 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1625 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301626 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1627 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001628 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301629 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1630 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001631 }
1632 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1633}
1634
1635ndk::ScopedAStatus Module::removeDeviceEffect(
1636 int32_t in_portConfigId,
1637 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1638 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301639 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1640 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001641 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301642 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1643 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001644 }
1645 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1646}
1647
jiabin9a8e6862023-01-12 23:06:37 +00001648ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1649 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301650 LOG(DEBUG) << __func__ << ": " << mType << ": mmap policy type " << toString(mmapPolicyType);
jiabin9a8e6862023-01-12 23:06:37 +00001651 std::set<int32_t> mmapSinks;
1652 std::set<int32_t> mmapSources;
1653 auto& ports = getConfig().ports;
1654 for (const auto& port : ports) {
1655 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1656 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1657 AudioInputFlags::MMAP_NOIRQ)) {
1658 mmapSinks.insert(port.id);
1659 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1660 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1661 AudioOutputFlags::MMAP_NOIRQ)) {
1662 mmapSources.insert(port.id);
1663 }
1664 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001665 if (mmapSources.empty() && mmapSinks.empty()) {
1666 AudioMMapPolicyInfo never;
1667 never.mmapPolicy = AudioMMapPolicy::NEVER;
1668 _aidl_return->push_back(never);
1669 return ndk::ScopedAStatus::ok();
1670 }
jiabin9a8e6862023-01-12 23:06:37 +00001671 for (const auto& route : getConfig().routes) {
1672 if (mmapSinks.count(route.sinkPortId) != 0) {
1673 // The sink is a mix port, add the sources if they are device ports.
1674 for (int sourcePortId : route.sourcePortIds) {
1675 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1676 if (sourcePortIt == ports.end()) {
1677 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301678 LOG(ERROR) << __func__ << ": " << mType << ": port id " << sourcePortId
1679 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001680 continue;
1681 }
1682 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1683 // The source is not a device port, skip
1684 continue;
1685 }
1686 AudioMMapPolicyInfo policyInfo;
1687 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1688 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1689 // default implementation.
1690 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1691 _aidl_return->push_back(policyInfo);
1692 }
1693 } else {
1694 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1695 if (sinkPortIt == ports.end()) {
1696 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301697 LOG(ERROR) << __func__ << ": " << mType << ": port id " << route.sinkPortId
1698 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001699 continue;
1700 }
1701 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1702 // The sink is not a device port, skip
1703 continue;
1704 }
1705 if (count_any(mmapSources, route.sourcePortIds)) {
1706 AudioMMapPolicyInfo policyInfo;
1707 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1708 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1709 // default implementation.
1710 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1711 _aidl_return->push_back(policyInfo);
1712 }
1713 }
1714 }
1715 return ndk::ScopedAStatus::ok();
1716}
1717
Eric Laurente2432ea2023-01-12 17:47:31 +01001718ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301719 LOG(DEBUG) << __func__ << ": " << mType;
Eric Laurente2432ea2023-01-12 17:47:31 +01001720 *_aidl_return = false;
1721 return ndk::ScopedAStatus::ok();
1722}
1723
jiabinb76981e2023-01-18 00:58:30 +00001724ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1725 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301726 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001727 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1728 }
1729 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301730 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001731 return ndk::ScopedAStatus::ok();
1732}
1733
1734ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1735 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301736 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001737 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1738 }
1739 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301740 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001741 return ndk::ScopedAStatus::ok();
1742}
1743
1744bool Module::isMmapSupported() {
1745 if (mIsMmapSupported.has_value()) {
1746 return mIsMmapSupported.value();
1747 }
1748 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1749 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1750 mIsMmapSupported = false;
1751 } else {
1752 mIsMmapSupported =
1753 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1754 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1755 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1756 }) != mmapPolicyInfos.end();
1757 }
1758 return mIsMmapSupported.value();
1759}
1760
Mikhail Naganova92039a2023-12-20 14:27:22 -08001761ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001762 if (audioPort->ext.getTag() != AudioPortExt::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301763 LOG(ERROR) << __func__ << ": " << mType << ": not a device port: " << audioPort->toString();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001764 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1765 }
1766 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1767 if (!devicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301768 LOG(ERROR) << __func__ << ": " << mType << ": module implementation must override "
1769 "'populateConnectedDevicePort' "
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001770 << "to handle connection of external devices.";
1771 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1772 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301773 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001774 return ndk::ScopedAStatus::ok();
1775}
1776
1777ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1778 const std::vector<AudioPortConfig*>& sources __unused,
1779 const std::vector<AudioPortConfig*>& sinks __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301780 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001781 return ndk::ScopedAStatus::ok();
1782}
1783
jiabin783c48b2023-02-28 18:28:06 +00001784void Module::onExternalDeviceConnectionChanged(
1785 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1786 bool connected __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301787 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabin783c48b2023-02-28 18:28:06 +00001788}
1789
jiabindd23b0e2023-12-11 19:10:05 +00001790void Module::onPrepareToDisconnectExternalDevice(
1791 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301792 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabindd23b0e2023-12-11 19:10:05 +00001793}
1794
jiabin783c48b2023-02-28 18:28:06 +00001795ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301796 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001797 return ndk::ScopedAStatus::ok();
1798}
1799
1800ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301801 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001802 return ndk::ScopedAStatus::ok();
1803}
1804
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001805std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1806 std::vector<MicrophoneInfo> result;
1807 Configuration& config = getConfig();
1808 for (const AudioPort& port : config.ports) {
1809 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1810 const AudioDeviceType deviceType =
1811 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1812 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1813 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1814 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1815 // accordingly based on their physical microphone parameters.
1816 result.push_back(MicrophoneInfo{
1817 .id = port.name,
1818 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1819 .group = 0,
1820 .indexInTheGroup = 0,
1821 });
1822 }
1823 }
1824 }
1825 return result;
1826}
1827
Ram Mohan18f0d512023-07-01 00:47:09 +05301828ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1829 return mStreams.bluetoothParametersUpdated();
1830}
1831
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001832} // namespace aidl::android::hardware::audio::core