blob: 51b608570027230c50cf0ae8cfa8aa53423afc34 [file] [log] [blame]
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <algorithm>
18#include <set>
19
20#define LOG_TAG "AHAL_Module"
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000021#include <aidl/android/media/audio/common/AudioInputFlags.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000022#include <aidl/android/media/audio/common/AudioOutputFlags.h>
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -070023#include <android-base/logging.h>
24#include <android/binder_ibinder_platform.h>
25#include <error/expected_utils.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000026
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000027#include "core-impl/Configuration.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000028#include "core-impl/Module.h"
Mikhail Naganovb03b5c42023-07-26 13:13:35 -070029#include "core-impl/ModuleBluetooth.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070030#include "core-impl/ModulePrimary.h"
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053031#include "core-impl/ModuleRemoteSubmix.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070032#include "core-impl/ModuleStub.h"
jiabin253bd322023-01-25 23:57:31 +000033#include "core-impl/ModuleUsb.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010034#include "core-impl/SoundDose.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000035#include "core-impl/utils.h"
36
Mikhail Naganov55045b52023-10-24 17:03:50 -070037using aidl::android::hardware::audio::common::frameCountFromDurationMs;
Mikhail Naganov872d4a62023-03-09 18:19:01 -080038using aidl::android::hardware::audio::common::getFrameSizeInBytes;
Kuowei Li53a8d4d2024-06-24 14:35:07 +080039using aidl::android::hardware::audio::common::hasMmapFlag;
Mikhail Naganov872d4a62023-03-09 18:19:01 -080040using aidl::android::hardware::audio::common::isBitPositionFlagSet;
41using aidl::android::hardware::audio::common::isValidAudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000042using aidl::android::hardware::audio::common::SinkMetadata;
43using aidl::android::hardware::audio::common::SourceMetadata;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010044using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000045using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000046using aidl::android::media::audio::common::AudioDevice;
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +000047using aidl::android::media::audio::common::AudioDeviceType;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000048using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000049using aidl::android::media::audio::common::AudioFormatType;
Weilin Xua33bb5e2024-10-02 17:16:42 +000050using aidl::android::media::audio::common::AudioGainConfig;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000051using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000052using aidl::android::media::audio::common::AudioIoFlags;
jiabin9a8e6862023-01-12 23:06:37 +000053using aidl::android::media::audio::common::AudioMMapPolicy;
54using aidl::android::media::audio::common::AudioMMapPolicyInfo;
55using aidl::android::media::audio::common::AudioMMapPolicyType;
Mikhail Naganov04ae8222023-01-11 15:48:10 -080056using aidl::android::media::audio::common::AudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000057using aidl::android::media::audio::common::AudioOffloadInfo;
58using aidl::android::media::audio::common::AudioOutputFlags;
59using aidl::android::media::audio::common::AudioPort;
60using aidl::android::media::audio::common::AudioPortConfig;
61using aidl::android::media::audio::common::AudioPortExt;
62using aidl::android::media::audio::common::AudioProfile;
Mikhail Naganov20047bc2023-01-05 20:16:07 +000063using aidl::android::media::audio::common::Boolean;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000064using aidl::android::media::audio::common::Int;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080065using aidl::android::media::audio::common::MicrophoneInfo;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000066using aidl::android::media::audio::common::PcmType;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000067
68namespace aidl::android::hardware::audio::core {
69
70namespace {
71
Mikhail Naganov84bcc042023-10-05 17:36:57 -070072inline bool hasDynamicChannelMasks(const std::vector<AudioChannelLayout>& channelMasks) {
73 return channelMasks.empty() ||
74 std::all_of(channelMasks.begin(), channelMasks.end(),
75 [](const auto& channelMask) { return channelMask == AudioChannelLayout{}; });
76}
77
78inline bool hasDynamicFormat(const AudioFormatDescription& format) {
79 return format == AudioFormatDescription{};
80}
81
82inline bool hasDynamicSampleRates(const std::vector<int32_t>& sampleRates) {
83 return sampleRates.empty() ||
84 std::all_of(sampleRates.begin(), sampleRates.end(),
85 [](const auto& sampleRate) { return sampleRate == 0; });
86}
87
88inline bool isDynamicProfile(const AudioProfile& profile) {
89 return hasDynamicFormat(profile.format) || hasDynamicChannelMasks(profile.channelMasks) ||
90 hasDynamicSampleRates(profile.sampleRates);
91}
92
93bool hasDynamicProfilesOnly(const std::vector<AudioProfile>& profiles) {
94 if (profiles.empty()) return true;
95 return std::all_of(profiles.begin(), profiles.end(), isDynamicProfile);
96}
97
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000098bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
99 AudioProfile* profile) {
100 if (auto profilesIt =
101 find_if(port.profiles.begin(), port.profiles.end(),
102 [&format](const auto& profile) { return profile.format == format; });
103 profilesIt != port.profiles.end()) {
104 *profile = *profilesIt;
105 return true;
106 }
107 return false;
108}
Mikhail Naganov00603d12022-05-02 22:52:13 +0000109
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000110} // namespace
111
jiabin253bd322023-01-25 23:57:31 +0000112// static
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000113std::shared_ptr<Module> Module::createInstance(Type type, std::unique_ptr<Configuration>&& config) {
jiabin253bd322023-01-25 23:57:31 +0000114 switch (type) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530115 case Type::DEFAULT:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000116 return ndk::SharedRefBase::make<ModulePrimary>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700117 case Type::R_SUBMIX:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000118 return ndk::SharedRefBase::make<ModuleRemoteSubmix>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700119 case Type::STUB:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000120 return ndk::SharedRefBase::make<ModuleStub>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700121 case Type::USB:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000122 return ndk::SharedRefBase::make<ModuleUsb>(std::move(config));
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700123 case Type::BLUETOOTH:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000124 return ndk::SharedRefBase::make<ModuleBluetooth>(std::move(config));
jiabin253bd322023-01-25 23:57:31 +0000125 }
126}
127
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000128// static
129std::optional<Module::Type> Module::typeFromString(const std::string& type) {
130 if (type == "default")
131 return Module::Type::DEFAULT;
132 else if (type == "r_submix")
133 return Module::Type::R_SUBMIX;
134 else if (type == "stub")
135 return Module::Type::STUB;
136 else if (type == "usb")
137 return Module::Type::USB;
138 else if (type == "bluetooth")
139 return Module::Type::BLUETOOTH;
140 return {};
141}
142
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700143std::ostream& operator<<(std::ostream& os, Module::Type t) {
144 switch (t) {
145 case Module::Type::DEFAULT:
146 os << "default";
147 break;
148 case Module::Type::R_SUBMIX:
149 os << "r_submix";
150 break;
Mikhail Naganov521fc492023-07-11 17:24:08 -0700151 case Module::Type::STUB:
152 os << "stub";
153 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700154 case Module::Type::USB:
155 os << "usb";
156 break;
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700157 case Module::Type::BLUETOOTH:
158 os << "bluetooth";
159 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700160 }
161 return os;
162}
163
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000164Module::Module(Type type, std::unique_ptr<Configuration>&& config)
165 : mType(type), mConfig(std::move(config)) {
166 populateConnectedProfiles();
167}
168
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000169void Module::cleanUpPatch(int32_t patchId) {
170 erase_all_values(mPatches, std::set<int32_t>{patchId});
171}
172
Mikhail Naganov8651b362023-01-06 23:15:27 +0000173ndk::ScopedAStatus Module::createStreamContext(
174 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
175 std::shared_ptr<IStreamCallback> asyncCallback,
176 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000177 if (in_bufferSizeFrames <= 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530178 LOG(ERROR) << __func__ << ": " << mType << ": non-positive buffer size "
179 << in_bufferSizeFrames;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000180 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
181 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000182 auto& configs = getConfig().portConfigs;
183 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganova92039a2023-12-20 14:27:22 -0800184 const int32_t nominalLatencyMs = getNominalLatencyMs(*portConfigIt);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000185 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000186 // validity of the portConfigId has already been checked.
Mikhail Naganova92039a2023-12-20 14:27:22 -0800187 const int32_t minimumStreamBufferSizeFrames =
188 calculateBufferSizeFrames(nominalLatencyMs, portConfigIt->sampleRate.value().value);
Mikhail Naganov13501872023-10-18 16:15:46 -0700189 if (in_bufferSizeFrames < minimumStreamBufferSizeFrames) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530190 LOG(ERROR) << __func__ << ": " << mType << ": insufficient buffer size "
191 << in_bufferSizeFrames << ", must be at least " << minimumStreamBufferSizeFrames;
Mikhail Naganov13501872023-10-18 16:15:46 -0700192 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
193 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000194 const size_t frameSize =
195 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
196 if (frameSize == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530197 LOG(ERROR) << __func__ << ": " << mType
198 << ": could not calculate frame size for port config "
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000199 << portConfigIt->toString();
200 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
201 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530202 LOG(DEBUG) << __func__ << ": " << mType << ": frame size " << frameSize << " bytes";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700203 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530204 LOG(ERROR) << __func__ << ": " << mType << ": buffer size " << in_bufferSizeFrames
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000205 << " frames is too large, maximum size is "
206 << kMaximumStreamBufferSizeBytes / frameSize;
207 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
208 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000209 const auto& flags = portConfigIt->flags.value();
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800210 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
211 mVendorDebug.forceTransientBurst,
212 mVendorDebug.forceSynchronousDrain};
213 std::unique_ptr<StreamContext::DataMQ> dataMQ = nullptr;
214 std::shared_ptr<IStreamCallback> streamAsyncCallback = nullptr;
215 std::shared_ptr<ISoundDose> soundDose;
216 if (!getSoundDose(&soundDose).isOk()) {
217 LOG(ERROR) << __func__ << ": could not create sound dose instance";
218 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
219 }
220 if (!hasMmapFlag(flags)) {
221 dataMQ = std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames);
222 streamAsyncCallback = asyncCallback;
223 }
224 StreamContext temp(
225 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
226 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
227 portConfigIt->format.value(), portConfigIt->channelMask.value(),
228 portConfigIt->sampleRate.value().value, flags, nominalLatencyMs,
229 portConfigIt->ext.get<AudioPortExt::mix>().handle, std::move(dataMQ),
230 streamAsyncCallback, outEventCallback, mSoundDose.getInstance(), params);
231 if (temp.isValid()) {
232 *out_context = std::move(temp);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000233 } else {
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800234 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000235 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000236 return ndk::ScopedAStatus::ok();
237}
238
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530239std::vector<AudioDevice> Module::getDevicesFromDevicePortConfigIds(
240 const std::set<int32_t>& devicePortConfigIds) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000241 std::vector<AudioDevice> result;
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530242 auto& configs = getConfig().portConfigs;
243 for (const auto& id : devicePortConfigIds) {
244 auto it = findById<AudioPortConfig>(configs, id);
245 if (it != configs.end() && it->ext.getTag() == AudioPortExt::Tag::device) {
246 result.push_back(it->ext.template get<AudioPortExt::Tag::device>().device);
247 } else {
248 LOG(FATAL) << __func__ << ": " << mType
249 << ": failed to find device for id" << id;
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000250 }
251 }
252 return result;
253}
254
Ajender Reddy3d248fd2024-10-29 19:10:04 +0530255std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
256 return getDevicesFromDevicePortConfigIds(findConnectedPortConfigIds(portConfigId));
257}
258
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000259std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
260 std::set<int32_t> result;
261 auto patchIdsRange = mPatches.equal_range(portConfigId);
262 auto& patches = getConfig().patches;
263 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
264 auto patchIt = findById<AudioPatch>(patches, it->second);
265 if (patchIt == patches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530266 LOG(FATAL) << __func__ << ": " << mType << ": patch with id " << it->second
267 << " taken from mPatches "
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000268 << "not found in the configuration";
269 }
270 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
271 portConfigId) != patchIt->sourcePortConfigIds.end()) {
272 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
273 } else {
274 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
275 }
276 }
277 return result;
278}
279
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000280ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
281 auto& configs = getConfig().portConfigs;
282 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
283 if (portConfigIt == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530284 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id " << in_portConfigId
285 << " not found";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000286 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
287 }
288 const int32_t portId = portConfigIt->portId;
289 // In our implementation, configs of mix ports always have unique IDs.
290 CHECK(portId != in_portConfigId);
291 auto& ports = getConfig().ports;
292 auto portIt = findById<AudioPort>(ports, portId);
293 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530294 LOG(ERROR) << __func__ << ": " << mType << ": port id " << portId
295 << " used by port config id " << in_portConfigId << " not found";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000296 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
297 }
298 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530299 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000300 << " already has a stream opened on it";
301 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
302 }
303 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530304 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000305 << " does not correspond to a mix port";
306 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
307 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700308 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000309 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530310 LOG(ERROR) << __func__ << ": " << mType << ": port id " << portId
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000311 << " has already reached maximum allowed opened stream count: "
312 << maxOpenStreamCount;
313 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
314 }
315 *port = &(*portIt);
316 return ndk::ScopedAStatus::ok();
317}
318
Mikhail Naganova92039a2023-12-20 14:27:22 -0800319bool Module::generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
320 const bool allowDynamicConfig = port.ext.getTag() == AudioPortExt::device;
321 for (const auto& profile : port.profiles) {
322 if (isDynamicProfile(profile)) continue;
323 config->format = profile.format;
324 config->channelMask = *profile.channelMasks.begin();
325 config->sampleRate = Int{.value = *profile.sampleRates.begin()};
326 config->flags = port.flags;
327 config->ext = port.ext;
328 return true;
329 }
330 if (allowDynamicConfig) {
331 config->format = AudioFormatDescription{};
332 config->channelMask = AudioChannelLayout{};
333 config->sampleRate = Int{.value = 0};
334 config->flags = port.flags;
335 config->ext = port.ext;
336 return true;
337 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530338 LOG(ERROR) << __func__ << ": " << mType << ": port " << port.id << " only has dynamic profiles";
Mikhail Naganova92039a2023-12-20 14:27:22 -0800339 return false;
340}
341
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000342void Module::populateConnectedProfiles() {
343 Configuration& config = getConfig();
344 for (const AudioPort& port : config.ports) {
345 if (port.ext.getTag() == AudioPortExt::device) {
346 if (auto devicePort = port.ext.get<AudioPortExt::device>();
347 !devicePort.device.type.connection.empty() && port.profiles.empty()) {
348 if (auto connIt = config.connectedProfiles.find(port.id);
349 connIt == config.connectedProfiles.end()) {
350 config.connectedProfiles.emplace(
351 port.id, internal::getStandard16And24BitPcmAudioProfiles());
352 }
353 }
354 }
355 }
356}
357
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000358template <typename C>
359std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
360 std::set<int32_t> result;
361 auto& portConfigs = getConfig().portConfigs;
362 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
363 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
364 if (portConfigIt != portConfigs.end()) {
365 result.insert(portConfigIt->portId);
366 }
367 }
368 return result;
369}
370
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000371std::unique_ptr<Module::Configuration> Module::initializeConfig() {
372 return internal::getConfiguration(getType());
Peter Yoon918a6a52023-07-13 17:04:37 +0900373}
374
Mikhail Naganov13501872023-10-18 16:15:46 -0700375int32_t Module::getNominalLatencyMs(const AudioPortConfig&) {
376 // Arbitrary value. Implementations must override this method to provide their actual latency.
377 static constexpr int32_t kLatencyMs = 5;
378 return kLatencyMs;
379}
380
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800381ndk::ScopedAStatus Module::createMmapBuffer(
382 const ::aidl::android::hardware::audio::core::StreamContext& context __unused,
383 ::aidl::android::hardware::audio::core::StreamDescriptor* desc __unused) {
384 LOG(ERROR) << __func__ << ": " << mType << ": is not implemented";
385 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
386}
387
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700388std::vector<AudioRoute*> Module::getAudioRoutesForAudioPortImpl(int32_t portId) {
389 std::vector<AudioRoute*> result;
390 auto& routes = getConfig().routes;
391 for (auto& r : routes) {
392 const auto& srcs = r.sourcePortIds;
393 if (r.sinkPortId == portId || std::find(srcs.begin(), srcs.end(), portId) != srcs.end()) {
394 result.push_back(&r);
395 }
396 }
397 return result;
398}
399
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000400Module::Configuration& Module::getConfig() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000401 if (!mConfig) {
Yi Konge62f97f2024-08-14 01:52:04 +0800402 mConfig = initializeConfig();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000403 }
404 return *mConfig;
405}
406
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700407std::set<int32_t> Module::getRoutableAudioPortIds(int32_t portId,
408 std::vector<AudioRoute*>* routes) {
409 std::vector<AudioRoute*> routesStorage;
410 if (routes == nullptr) {
411 routesStorage = getAudioRoutesForAudioPortImpl(portId);
412 routes = &routesStorage;
413 }
414 std::set<int32_t> result;
415 for (AudioRoute* r : *routes) {
416 if (r->sinkPortId == portId) {
417 result.insert(r->sourcePortIds.begin(), r->sourcePortIds.end());
418 } else {
419 result.insert(r->sinkPortId);
420 }
421 }
422 return result;
423}
424
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000425void Module::registerPatch(const AudioPatch& patch) {
426 auto& configs = getConfig().portConfigs;
427 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
428 for (auto portConfigId : portConfigIds) {
429 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
430 if (configIt != configs.end()) {
431 mPatches.insert(std::pair{portConfigId, patch.id});
432 if (configIt->portId != portConfigId) {
433 mPatches.insert(std::pair{configIt->portId, patch.id});
434 }
435 }
436 };
437 };
438 do_insert(patch.sourcePortConfigIds);
439 do_insert(patch.sinkPortConfigIds);
440}
441
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700442ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
443 const AudioPatch& newPatch) {
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700444 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700445 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700446 using Connections =
447 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
448 Connections oldConnections, newConnections;
449 auto fillConnectionsHelper = [&](Connections& connections,
450 const std::vector<int32_t>& mixPortCfgIds,
451 const std::vector<int32_t>& devicePortCfgIds) {
452 for (int32_t mixPortCfgId : mixPortCfgIds) {
453 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
454 }
455 };
456 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
457 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
458 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
459 patch.sourcePortConfigIds.end()) {
460 // Sources are mix ports.
461 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
462 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
463 [&](int32_t portConfigId) {
464 return mStreams.count(portConfigId) > 0;
465 }) != patch.sinkPortConfigIds.end()) {
466 // Sources are device ports.
467 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
468 } // Otherwise, there are no streams to notify.
469 };
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530470 auto restoreOldConnections = [&](const std::set<int32_t>& mixPortIds,
471 const bool continueWithEmptyDevices) {
472 for (const auto mixPort : mixPortIds) {
473 if (auto it = oldConnections.find(mixPort);
474 continueWithEmptyDevices || it != oldConnections.end()) {
475 const std::vector<AudioDevice> d =
476 it != oldConnections.end() ? getDevicesFromDevicePortConfigIds(it->second)
477 : std::vector<AudioDevice>();
478 if (auto status = mStreams.setStreamConnectedDevices(mixPort, d); status.isOk()) {
479 LOG(WARNING) << ":updateStreamsConnectedState: rollback: mix port config:"
480 << mixPort
481 << (d.empty() ? "; not connected"
482 : std::string("; connected to ") +
483 ::android::internal::ToString(d));
484 } else {
485 // can't do much about rollback failures
486 LOG(ERROR)
487 << ":updateStreamsConnectedState: rollback: failed for mix port config:"
488 << mixPort;
489 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700490 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000491 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530492 };
493 fillConnections(oldConnections, oldPatch);
494 fillConnections(newConnections, newPatch);
495 /**
496 * Illustration of oldConnections and newConnections
497 *
498 * oldConnections {
499 * a : {A,B,C},
500 * b : {D},
501 * d : {H,I,J},
502 * e : {N,O,P},
503 * f : {Q,R},
504 * g : {T,U,V},
505 * }
506 *
507 * newConnections {
508 * a : {A,B,C},
509 * c : {E,F,G},
510 * d : {K,L,M},
511 * e : {N,P},
512 * f : {Q,R,S},
513 * g : {U,V,W},
514 * }
515 *
516 * Expected routings:
517 * 'a': is ignored both in disconnect step and connect step,
518 * due to same devices both in oldConnections and newConnections.
519 * 'b': handled only in disconnect step with empty devices because 'b' is only present
520 * in oldConnections.
521 * 'c': handled only in connect step with {E,F,G} devices because 'c' is only present
522 * in newConnections.
523 * 'd': handled only in connect step with {K,L,M} devices because 'd' is also present
524 * in newConnections and it is ignored in disconnected step.
525 * 'e': handled only in connect step with {N,P} devices because 'e' is also present
526 * in newConnections and it is ignored in disconnect step. please note that there
527 * is no exclusive disconnection for device {O}.
528 * 'f': handled only in connect step with {Q,R,S} devices because 'f' is also present
529 * in newConnections and it is ignored in disconnect step. Even though stream is
530 * already connected with {Q,R} devices and connection happens with {Q,R,S}.
531 * 'g': handled only in connect step with {U,V,W} devices because 'g' is also present
532 * in newConnections and it is ignored in disconnect step. There is no exclusive
533 * disconnection with devices {T,U,V}.
534 *
535 * If, any failure, will lead to restoreOldConnections (rollback).
536 * The aim of the restoreOldConnections is to make connections back to oldConnections.
537 * Failures in restoreOldConnections aren't handled.
538 */
539
540 std::set<int32_t> idsToConnectBackOnFailure;
541 // disconnection step
542 for (const auto& [oldMixPortConfigId, oldDevicePortConfigIds] : oldConnections) {
543 if (auto it = newConnections.find(oldMixPortConfigId); it == newConnections.end()) {
544 idsToConnectBackOnFailure.insert(oldMixPortConfigId);
545 if (auto status = mStreams.setStreamConnectedDevices(oldMixPortConfigId, {});
546 status.isOk()) {
547 LOG(DEBUG) << __func__ << ": The stream on port config id " << oldMixPortConfigId
548 << " has been disconnected";
549 } else {
550 maybeFailure = std::move(status);
551 // proceed to rollback even on one failure
552 break;
553 }
554 }
555 }
556
557 if (!maybeFailure.isOk()) {
558 restoreOldConnections(idsToConnectBackOnFailure, false /*continueWithEmptyDevices*/);
559 LOG(WARNING) << __func__ << ": failed to disconnect from old patch. attempted rollback";
560 return maybeFailure;
561 }
562
563 std::set<int32_t> idsToRollbackOnFailure;
564 // connection step
565 for (const auto& [newMixPortConfigId, newDevicePortConfigIds] : newConnections) {
566 if (auto it = oldConnections.find(newMixPortConfigId);
567 it == oldConnections.end() || it->second != newDevicePortConfigIds) {
568 const auto connectedDevices = getDevicesFromDevicePortConfigIds(newDevicePortConfigIds);
569 idsToRollbackOnFailure.insert(newMixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700570 if (connectedDevices.empty()) {
571 // This is important as workers use the vector size to derive the connection status.
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530572 LOG(FATAL) << __func__ << ": No connected devices found for port config id "
573 << newMixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700574 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530575 if (auto status =
576 mStreams.setStreamConnectedDevices(newMixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700577 status.isOk()) {
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530578 LOG(DEBUG) << __func__ << ": The stream on port config id " << newMixPortConfigId
579 << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700580 << ::android::internal::ToString(connectedDevices);
581 } else {
582 maybeFailure = std::move(status);
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530583 // proceed to rollback even on one failure
584 break;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700585 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000586 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530587 }
588
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700589 if (!maybeFailure.isOk()) {
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530590 restoreOldConnections(idsToConnectBackOnFailure, false /*continueWithEmptyDevices*/);
591 restoreOldConnections(idsToRollbackOnFailure, true /*continueWithEmptyDevices*/);
592 LOG(WARNING) << __func__ << ": failed to connect for new patch. attempted rollback";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700593 return maybeFailure;
594 }
Ajender Reddy32e85fc2024-10-30 16:58:17 +0530595
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700596 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000597}
598
Mikhail Naganov00603d12022-05-02 22:52:13 +0000599ndk::ScopedAStatus Module::setModuleDebug(
600 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700601 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000602 << ", new flags: " << in_debug.toString();
603 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
604 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700605 LOG(ERROR) << __func__ << ": " << mType
Jaideep Sharma559a4912024-03-07 10:05:51 +0530606 << ": attempting to change device connections simulation while "
607 "having external "
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700608 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000609 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
610 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000611 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700612 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000613 << in_debug.streamTransientStateDelayMs;
614 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
615 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000616 mDebug = in_debug;
617 return ndk::ScopedAStatus::ok();
618}
619
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000620ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700621 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530622 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000623 return ndk::ScopedAStatus::ok();
624}
625
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000626ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700627 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530628 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000629 return ndk::ScopedAStatus::ok();
630}
631
Mikhail Naganov7499a002023-02-27 18:51:44 -0800632ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700633 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530634 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800635 return ndk::ScopedAStatus::ok();
636}
637
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800638ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700639 *_aidl_return = nullptr;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530640 LOG(DEBUG) << __func__ << ": " << mType << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800641 return ndk::ScopedAStatus::ok();
642}
643
Mikhail Naganov00603d12022-05-02 22:52:13 +0000644ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
645 AudioPort* _aidl_return) {
646 const int32_t templateId = in_templateIdAndAdditionalData.id;
647 auto& ports = getConfig().ports;
648 AudioPort connectedPort;
649 { // Scope the template port so that we don't accidentally modify it.
650 auto templateIt = findById<AudioPort>(ports, templateId);
651 if (templateIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530652 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000653 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
654 }
655 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530656 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
657 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000658 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
659 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000660 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
661 if (templateDevicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530662 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
663 << " is permanently attached";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000664 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
665 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700666 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530667 LOG(ERROR) << __func__ << ": " << mType << ": port id " << templateId
668 << " is a connected device port";
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700669 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
670 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000671 // Postpone id allocation until we ensure that there are no client errors.
672 connectedPort = *templateIt;
673 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
674 const auto& inputDevicePort =
675 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
676 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
677 connectedDevicePort.device.address = inputDevicePort.device.address;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530678 LOG(DEBUG) << __func__ << ": " << mType << ": device port " << connectedPort.id
679 << " device set to " << connectedDevicePort.device.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000680 // Check if there is already a connected port with for the same external device.
Jaideep Sharma559a4912024-03-07 10:05:51 +0530681
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700682 for (auto connectedPortPair : mConnectedDevicePorts) {
683 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000684 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
685 connectedDevicePort.device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530686 LOG(ERROR) << __func__ << ": " << mType << ": device "
687 << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700688 << " is already connected at the device port id "
689 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000690 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
691 }
692 }
693 }
694
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700695 // Two main cases are considered with regard to the profiles of the connected device port:
696 //
697 // 1. If the template device port has dynamic profiles, and at least one routable mix
698 // port also has dynamic profiles, it means that after connecting the device, the
699 // connected device port must have profiles populated with actual capabilities of
700 // the connected device, and dynamic of routable mix ports will be filled
701 // according to these capabilities. An example of this case is connection of an
702 // HDMI or USB device. For USB handled by ADSP, there can be mix ports with static
703 // profiles, and one dedicated mix port for "hi-fi" playback. The latter is left with
704 // dynamic profiles so that they can be populated with actual capabilities of
705 // the connected device.
706 //
707 // 2. If the template device port has dynamic profiles, while all routable mix ports
708 // have static profiles, it means that after connecting the device, the connected
709 // device port can be left with dynamic profiles, and profiles of mix ports are
710 // left untouched. An example of this case is connection of an analog wired
711 // headset, it should be treated in the same way as a speaker.
712 //
713 // Yet another possible case is when both the template device port and all routable
714 // mix ports have static profiles. This is allowed and handled correctly, however, it
715 // is not very practical, since these profiles are likely duplicates of each other.
716
717 std::vector<AudioRoute*> routesToMixPorts = getAudioRoutesForAudioPortImpl(templateId);
718 std::set<int32_t> routableMixPortIds = getRoutableAudioPortIds(templateId, &routesToMixPorts);
Mikhail Naganova92039a2023-12-20 14:27:22 -0800719 const int32_t nextPortId = getConfig().nextPortId++;
Mikhail Naganov55045b52023-10-24 17:03:50 -0700720 if (!mDebug.simulateDeviceConnections) {
721 // Even if the device port has static profiles, the HAL module might need to update
722 // them, or abort the connection process.
Mikhail Naganova92039a2023-12-20 14:27:22 -0800723 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort, nextPortId));
Mikhail Naganov55045b52023-10-24 17:03:50 -0700724 } else if (hasDynamicProfilesOnly(connectedPort.profiles)) {
725 auto& connectedProfiles = getConfig().connectedProfiles;
726 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
727 connectedProfilesIt != connectedProfiles.end()) {
728 connectedPort.profiles = connectedProfilesIt->second;
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700729 }
Mikhail Naganov55045b52023-10-24 17:03:50 -0700730 }
731 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
732 // Possible case 2. Check if all routable mix ports have static profiles.
733 if (auto dynamicMixPortIt = std::find_if(ports.begin(), ports.end(),
734 [&routableMixPortIds](const auto& p) {
735 return routableMixPortIds.count(p.id) > 0 &&
736 hasDynamicProfilesOnly(p.profiles);
737 });
738 dynamicMixPortIt != ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530739 LOG(ERROR) << __func__ << ": " << mType
740 << ": connected port only has dynamic profiles after connecting "
Mikhail Naganov55045b52023-10-24 17:03:50 -0700741 << "external device " << connectedPort.toString() << ", and there exist "
742 << "a routable mix port with dynamic profiles: "
743 << dynamicMixPortIt->toString();
744 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530745 }
746 }
747
Mikhail Naganova92039a2023-12-20 14:27:22 -0800748 connectedPort.id = nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700749 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700750 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Jaideep Sharma559a4912024-03-07 10:05:51 +0530751 LOG(DEBUG) << __func__ << ": " << mType << ": template port " << templateId
752 << " external device connected, "
Mikhail Naganov00603d12022-05-02 22:52:13 +0000753 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000754 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000755 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000756
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700757 // For routes where the template port is a source, add the connected port to sources,
758 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000759 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700760 for (AudioRoute* r : routesToMixPorts) {
761 if (r->sinkPortId == templateId) {
762 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
763 .sinkPortId = connectedPort.id,
764 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000765 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700766 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000767 }
768 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700769 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000770 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
771
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700772 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
773 // Note: this is a simplistic approach assuming that a mix port can only be populated
774 // from a single device port. Implementing support for stuffing dynamic profiles with
775 // a superset of all profiles from all routable dynamic device ports would be more involved.
776 for (auto& port : ports) {
777 if (routableMixPortIds.count(port.id) == 0) continue;
778 if (hasDynamicProfilesOnly(port.profiles)) {
779 port.profiles = connectedPort.profiles;
780 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700781 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700782 // Check if profiles are not all dynamic because they were populated by
783 // a previous connection. Otherwise, it means that they are actually static.
784 for (const auto& cp : mConnectedDevicePorts) {
785 if (cp.second.count(port.id) > 0) {
786 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700787 break;
788 }
789 }
790 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700791 }
792 }
793 *_aidl_return = std::move(connectedPort);
794
Mikhail Naganov00603d12022-05-02 22:52:13 +0000795 return ndk::ScopedAStatus::ok();
796}
797
798ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
799 auto& ports = getConfig().ports;
800 auto portIt = findById<AudioPort>(ports, in_portId);
801 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530802 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000803 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
804 }
805 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530806 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
807 << " is not a device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000808 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
809 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700810 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
811 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530812 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
813 << " is not a connected device port";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000814 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
815 }
816 auto& configs = getConfig().portConfigs;
817 auto& initials = getConfig().initialConfigs;
818 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
819 if (config.portId == in_portId) {
820 // Check if the configuration was provided by the client.
821 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
822 return initialIt == initials.end() || config != *initialIt;
823 }
824 return false;
825 });
826 if (configIt != configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530827 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
828 << " has a non-default config with id " << configIt->id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000829 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
830 }
jiabin783c48b2023-02-28 18:28:06 +0000831 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000832 ports.erase(portIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +0530833 LOG(DEBUG) << __func__ << ": " << mType << ": connected device port " << in_portId
834 << " released";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000835
836 auto& routes = getConfig().routes;
837 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
838 if (routesIt->sinkPortId == in_portId) {
839 routesIt = routes.erase(routesIt);
840 } else {
841 // Note: the list of sourcePortIds can't become empty because there must
842 // be the id of the template port in the route.
843 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
844 ++routesIt;
845 }
846 }
847
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700848 // Clear profiles for mix ports that are not connected to any other ports.
849 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
850 mConnectedDevicePorts.erase(connectedPortsIt);
851 for (const auto& connectedPort : mConnectedDevicePorts) {
852 for (int32_t mixPortId : connectedPort.second) {
853 mixPortsToClear.erase(mixPortId);
854 }
855 }
856 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700857 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
858 if (mixPortIt != ports.end()) {
859 mixPortIt->profiles = {};
860 }
861 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700862
Mikhail Naganov00603d12022-05-02 22:52:13 +0000863 return ndk::ScopedAStatus::ok();
864}
865
jiabindd23b0e2023-12-11 19:10:05 +0000866ndk::ScopedAStatus Module::prepareToDisconnectExternalDevice(int32_t in_portId) {
867 auto& ports = getConfig().ports;
868 auto portIt = findById<AudioPort>(ports, in_portId);
869 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530870 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
jiabindd23b0e2023-12-11 19:10:05 +0000871 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
872 }
873 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530874 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
875 << " is not a device port";
jiabindd23b0e2023-12-11 19:10:05 +0000876 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
877 }
878 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
879 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530880 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId
881 << " is not a connected device port";
jiabindd23b0e2023-12-11 19:10:05 +0000882 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
883 }
884
885 onPrepareToDisconnectExternalDevice(*portIt);
886
887 return ndk::ScopedAStatus::ok();
888}
889
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000890ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
891 *_aidl_return = getConfig().patches;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530892 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " patches";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000893 return ndk::ScopedAStatus::ok();
894}
895
896ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
897 auto& ports = getConfig().ports;
898 auto portIt = findById<AudioPort>(ports, in_portId);
899 if (portIt != ports.end()) {
900 *_aidl_return = *portIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530901 LOG(DEBUG) << __func__ << ": " << mType << ": returning port by id " << in_portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000902 return ndk::ScopedAStatus::ok();
903 }
Jaideep Sharma559a4912024-03-07 10:05:51 +0530904 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000905 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
906}
907
908ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
909 *_aidl_return = getConfig().portConfigs;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530910 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size()
911 << " port configs";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000912 return ndk::ScopedAStatus::ok();
913}
914
915ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
916 *_aidl_return = getConfig().ports;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530917 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " ports";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000918 return ndk::ScopedAStatus::ok();
919}
920
921ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
922 *_aidl_return = getConfig().routes;
Jaideep Sharma559a4912024-03-07 10:05:51 +0530923 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << _aidl_return->size() << " routes";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000924 return ndk::ScopedAStatus::ok();
925}
926
Mikhail Naganov00603d12022-05-02 22:52:13 +0000927ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
928 std::vector<AudioRoute>* _aidl_return) {
929 auto& ports = getConfig().ports;
930 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530931 LOG(ERROR) << __func__ << ": " << mType << ": port id " << in_portId << " not found";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000932 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
933 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700934 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
935 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
936 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000937 return ndk::ScopedAStatus::ok();
938}
939
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000940ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
941 OpenInputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530942 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
943 << ", buffer size " << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000944 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700945 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000946 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530947 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000948 << " does not correspond to an input mix port";
949 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
950 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000951 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700952 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
953 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000954 context.fillDescriptor(&_aidl_return->desc);
Kuowei Li53a8d4d2024-06-24 14:35:07 +0800955 if (hasMmapFlag(context.getFlags())) {
956 RETURN_STATUS_IF_ERROR(createMmapBuffer(context, &_aidl_return->desc));
957 }
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000958 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700959 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000960 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000961 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700962 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
963 RETURN_STATUS_IF_ERROR(
964 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
965 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +0000966 auto streamBinder = streamWrapper.getBinder();
967 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
968 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000969 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000970 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000971 return ndk::ScopedAStatus::ok();
972}
973
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000974ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
975 OpenOutputStreamReturn* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530976 LOG(DEBUG) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
977 << ", has offload info? " << (in_args.offloadInfo.has_value()) << ", buffer size "
978 << in_args.bufferSizeFrames << " frames";
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000979 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700980 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000981 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530982 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000983 << " does not correspond to an output mix port";
984 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
985 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000986 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
987 AudioOutputFlags::COMPRESS_OFFLOAD);
988 if (isOffload && !in_args.offloadInfo.has_value()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530989 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000990 << " has COMPRESS_OFFLOAD flag set, requires offload info";
991 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
992 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000993 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
994 AudioOutputFlags::NON_BLOCKING);
995 if (isNonBlocking && in_args.callback == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +0530996 LOG(ERROR) << __func__ << ": " << mType << ": port id " << port->id
Mikhail Naganov30301a42022-09-13 01:20:45 +0000997 << " has NON_BLOCKING flag set, requires async callback";
998 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
999 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001000 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001001 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
1002 isNonBlocking ? in_args.callback : nullptr,
1003 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001004 context.fillDescriptor(&_aidl_return->desc);
Kuowei Li53a8d4d2024-06-24 14:35:07 +08001005 if (hasMmapFlag(context.getFlags())) {
1006 RETURN_STATUS_IF_ERROR(createMmapBuffer(context, &_aidl_return->desc));
1007 }
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001008 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -07001009 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -07001010 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001011 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001012 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
1013 RETURN_STATUS_IF_ERROR(
1014 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
1015 }
Mikhail Naganovefb45bc2024-03-27 16:20:33 +00001016 auto streamBinder = streamWrapper.getBinder();
1017 AIBinder_setMinSchedulerPolicy(streamBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
1018 AIBinder_setInheritRt(streamBinder.get(), true);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001019 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001020 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001021 return ndk::ScopedAStatus::ok();
1022}
1023
Mikhail Naganov74927202022-12-19 16:37:14 +00001024ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
1025 SupportedPlaybackRateFactors* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301026 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganov74927202022-12-19 16:37:14 +00001027 (void)_aidl_return;
1028 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1029}
1030
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001031ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301032 LOG(DEBUG) << __func__ << ": " << mType << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001033 if (in_requested.sourcePortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301034 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001035 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1036 }
1037 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301038 LOG(ERROR) << __func__ << ": " << mType
1039 << ": requested patch has duplicate ids in the sources list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001040 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1041 }
1042 if (in_requested.sinkPortConfigIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301043 LOG(ERROR) << __func__ << ": " << mType << ": requested patch has empty sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001044 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1045 }
1046 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301047 LOG(ERROR) << __func__ << ": " << mType
1048 << ": requested patch has duplicate ids in the sinks list";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001049 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1050 }
1051
1052 auto& configs = getConfig().portConfigs;
1053 std::vector<int32_t> missingIds;
1054 auto sources =
1055 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
1056 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301057 LOG(ERROR) << __func__ << ": " << mType << ": following source port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001058 << ::android::internal::ToString(missingIds);
1059 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1060 }
1061 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
1062 if (!missingIds.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301063 LOG(ERROR) << __func__ << ": " << mType << ": following sink port config ids not found: "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001064 << ::android::internal::ToString(missingIds);
1065 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1066 }
1067 // bool indicates whether a non-exclusive route is available.
1068 // If only an exclusive route is available, that means the patch can not be
1069 // established if there is any other patch which currently uses the sink port.
1070 std::map<int32_t, bool> allowedSinkPorts;
1071 auto& routes = getConfig().routes;
1072 for (auto src : sources) {
1073 for (const auto& r : routes) {
1074 const auto& srcs = r.sourcePortIds;
1075 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
1076 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
1077 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
1078 }
1079 }
1080 }
1081 }
1082 for (auto sink : sinks) {
1083 if (allowedSinkPorts.count(sink->portId) == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301084 LOG(ERROR) << __func__ << ": " << mType << ": there is no route to the sink port id "
1085 << sink->portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001086 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1087 }
1088 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -07001089 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +00001090
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001091 auto& patches = getConfig().patches;
1092 auto existing = patches.end();
1093 std::optional<decltype(mPatches)> patchesBackup;
1094 if (in_requested.id != 0) {
1095 existing = findById<AudioPatch>(patches, in_requested.id);
1096 if (existing != patches.end()) {
1097 patchesBackup = mPatches;
1098 cleanUpPatch(existing->id);
1099 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301100 LOG(ERROR) << __func__ << ": " << mType << ": not found existing patch id "
1101 << in_requested.id;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001102 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1103 }
1104 }
1105 // Validate the requested patch.
1106 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
1107 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301108 LOG(ERROR) << __func__ << ": " << mType << ": sink port id " << sinkPortId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001109 << "is exclusive and is already used by some other patch";
1110 if (patchesBackup.has_value()) {
1111 mPatches = std::move(*patchesBackup);
1112 }
1113 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1114 }
1115 }
Mikhail Naganov13501872023-10-18 16:15:46 -07001116 // Find the highest sample rate among mix port configs.
1117 std::map<int32_t, AudioPortConfig*> sampleRates;
1118 std::vector<AudioPortConfig*>& mixPortConfigs =
1119 sources[0]->ext.getTag() == AudioPortExt::mix ? sources : sinks;
1120 for (auto mix : mixPortConfigs) {
1121 sampleRates.emplace(mix->sampleRate.value().value, mix);
1122 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001123 *_aidl_return = in_requested;
Mikhail Naganov13501872023-10-18 16:15:46 -07001124 auto maxSampleRateIt = std::max_element(sampleRates.begin(), sampleRates.end());
1125 const int32_t latencyMs = getNominalLatencyMs(*(maxSampleRateIt->second));
1126 _aidl_return->minimumStreamBufferSizeFrames =
1127 calculateBufferSizeFrames(latencyMs, maxSampleRateIt->first);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001128 _aidl_return->latenciesMs.clear();
1129 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
Mikhail Naganov13501872023-10-18 16:15:46 -07001130 _aidl_return->sinkPortConfigIds.size(), latencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001131 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001132 if (existing == patches.end()) {
1133 _aidl_return->id = getConfig().nextPatchId++;
1134 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001135 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001136 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -07001137 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001138 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001139 patchesBackup = mPatches;
1140 registerPatch(*_aidl_return);
1141 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
1142 mPatches = std::move(*patchesBackup);
1143 if (existing == patches.end()) {
1144 patches.pop_back();
1145 } else {
1146 *existing = oldPatch;
1147 }
1148 return status;
1149 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001150
Jaideep Sharma559a4912024-03-07 10:05:51 +05301151 LOG(DEBUG) << __func__ << ": " << mType << ": " << (oldPatch.id == 0 ? "created" : "updated")
1152 << " patch " << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001153 return ndk::ScopedAStatus::ok();
1154}
1155
1156ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
1157 AudioPortConfig* out_suggested, bool* _aidl_return) {
Mikhail Naganova92039a2023-12-20 14:27:22 -08001158 auto generate = [this](const AudioPort& port, AudioPortConfig* config) {
1159 return generateDefaultPortConfig(port, config);
1160 };
1161 return setAudioPortConfigImpl(in_requested, generate, out_suggested, _aidl_return);
1162}
1163
1164ndk::ScopedAStatus Module::setAudioPortConfigImpl(
1165 const AudioPortConfig& in_requested,
1166 const std::function<bool(const ::aidl::android::media::audio::common::AudioPort& port,
1167 ::aidl::android::media::audio::common::AudioPortConfig* config)>&
1168 fillPortConfig,
1169 AudioPortConfig* out_suggested, bool* applied) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301170 LOG(DEBUG) << __func__ << ": " << mType << ": requested " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001171 auto& configs = getConfig().portConfigs;
1172 auto existing = configs.end();
1173 if (in_requested.id != 0) {
1174 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1175 existing == configs.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301176 LOG(ERROR) << __func__ << ": " << mType << ": existing port config id "
1177 << in_requested.id << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001178 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1179 }
1180 }
1181
1182 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1183 if (portId == 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301184 LOG(ERROR) << __func__ << ": " << mType
1185 << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001186 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1187 }
1188 auto& ports = getConfig().ports;
1189 auto portIt = findById<AudioPort>(ports, portId);
1190 if (portIt == ports.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301191 LOG(ERROR) << __func__ << ": " << mType
1192 << ": requested port config points to non-existent portId " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001193 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1194 }
1195 if (existing != configs.end()) {
1196 *out_suggested = *existing;
1197 } else {
1198 AudioPortConfig newConfig;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001199 newConfig.portId = portIt->id;
1200 if (fillPortConfig(*portIt, &newConfig)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001201 *out_suggested = newConfig;
1202 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301203 LOG(ERROR) << __func__ << ": " << mType
1204 << ": unable generate a default config for port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001205 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1206 }
1207 }
1208 // From this moment, 'out_suggested' is either an existing port config,
1209 // or a new generated config. Now attempt to update it according to the specified
1210 // fields of 'in_requested'.
1211
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001212 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1213 // which takes care of their actual configuration automatically.
1214 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1215 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001216 bool requestedIsValid = true, requestedIsFullySpecified = true;
1217
1218 AudioIoFlags portFlags = portIt->flags;
1219 if (in_requested.flags.has_value()) {
1220 if (in_requested.flags.value() != portFlags) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301221 LOG(WARNING) << __func__ << ": " << mType << ": requested flags "
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001222 << in_requested.flags.value().toString() << " do not match port's "
1223 << portId << " flags " << portFlags.toString();
1224 requestedIsValid = false;
1225 }
1226 } else {
1227 requestedIsFullySpecified = false;
1228 }
1229
1230 AudioProfile portProfile;
1231 if (in_requested.format.has_value()) {
1232 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001233 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1234 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001235 out_suggested->format = format;
1236 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301237 LOG(WARNING) << __func__ << ": " << mType << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001238 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001239 requestedIsValid = false;
1240 }
1241 } else {
1242 requestedIsFullySpecified = false;
1243 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001244 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1245 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301246 LOG(ERROR) << __func__ << ": " << mType << ": port " << portId
1247 << " does not support format " << out_suggested->format.value().toString()
1248 << " anymore";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001249 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1250 }
1251
1252 if (in_requested.channelMask.has_value()) {
1253 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001254 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1255 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1256 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001257 out_suggested->channelMask = channelMask;
1258 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301259 LOG(WARNING) << __func__ << ": " << mType << ": requested channel mask "
1260 << channelMask.toString() << " is not supported for the format "
1261 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001262 requestedIsValid = false;
1263 }
1264 } else {
1265 requestedIsFullySpecified = false;
1266 }
1267
1268 if (in_requested.sampleRate.has_value()) {
1269 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001270 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1271 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001272 sampleRate.value) != portProfile.sampleRates.end()) {
1273 out_suggested->sampleRate = sampleRate;
1274 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301275 LOG(WARNING) << __func__ << ": " << mType << ": requested sample rate "
1276 << sampleRate.value << " is not supported for the format "
1277 << portProfile.format.toString() << " by the port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001278 requestedIsValid = false;
1279 }
1280 } else {
1281 requestedIsFullySpecified = false;
1282 }
1283
1284 if (in_requested.gain.has_value()) {
Weilin Xua33bb5e2024-10-02 17:16:42 +00001285 if (!setAudioPortConfigGain(*portIt, in_requested.gain.value())) {
1286 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1287 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001288 out_suggested->gain = in_requested.gain.value();
1289 }
1290
Mikhail Naganov248e9502023-02-21 16:32:40 -08001291 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1292 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1293 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
Mikhail Naganovb06a4922024-03-06 16:39:50 -08001294 // 'AudioMixPortExt.handle' and '.usecase' are set by the client,
1295 // copy from in_requested.
1296 const auto& src = in_requested.ext.get<AudioPortExt::Tag::mix>();
1297 auto& dst = out_suggested->ext.get<AudioPortExt::Tag::mix>();
1298 dst.handle = src.handle;
1299 dst.usecase = src.usecase;
Mikhail Naganov248e9502023-02-21 16:32:40 -08001300 }
1301 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301302 LOG(WARNING) << __func__ << ": " << mType << ": requested ext tag "
Mikhail Naganov248e9502023-02-21 16:32:40 -08001303 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1304 << toString(out_suggested->ext.getTag());
1305 requestedIsValid = false;
1306 }
1307 }
1308
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001309 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1310 out_suggested->id = getConfig().nextPortId++;
1311 configs.push_back(*out_suggested);
Mikhail Naganova92039a2023-12-20 14:27:22 -08001312 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301313 LOG(DEBUG) << __func__ << ": " << mType << ": created new port config "
1314 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001315 } else if (existing != configs.end() && requestedIsValid) {
1316 *existing = *out_suggested;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001317 *applied = true;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301318 LOG(DEBUG) << __func__ << ": " << mType << ": updated port config "
1319 << out_suggested->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001320 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301321 LOG(DEBUG) << __func__ << ": " << mType << ": not applied; existing config ? "
1322 << (existing != configs.end()) << "; requested is valid? " << requestedIsValid
1323 << ", fully specified? " << requestedIsFullySpecified;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001324 *applied = false;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001325 }
1326 return ndk::ScopedAStatus::ok();
1327}
1328
Weilin Xua33bb5e2024-10-02 17:16:42 +00001329bool Module::setAudioPortConfigGain(const AudioPort& port, const AudioGainConfig& gainRequested) {
1330 auto& ports = getConfig().ports;
1331 if (gainRequested.index < 0 || gainRequested.index >= (int)port.gains.size()) {
1332 LOG(ERROR) << __func__ << ": gains for port " << port.id << " is undefined";
1333 return false;
1334 }
1335 int stepValue = port.gains[gainRequested.index].stepValue;
1336 if (stepValue == 0) {
1337 LOG(ERROR) << __func__ << ": port gain step value is 0";
1338 return false;
1339 }
1340 int minValue = port.gains[gainRequested.index].minValue;
1341 int maxValue = port.gains[gainRequested.index].maxValue;
1342 if (gainRequested.values[0] > maxValue || gainRequested.values[0] < minValue) {
1343 LOG(ERROR) << __func__ << ": gain value " << gainRequested.values[0]
1344 << " out of range of min and max gain config";
1345 return false;
1346 }
1347 int gainIndex = (gainRequested.values[0] - minValue) / stepValue;
1348 int totalSteps = (maxValue - minValue) / stepValue;
1349 if (totalSteps == 0) {
1350 LOG(ERROR) << __func__ << ": difference between port gain min value " << minValue
1351 << " and max value " << maxValue << " is less than step value " << stepValue;
1352 return false;
1353 }
1354 // Root-power quantities are used in curve:
1355 // 10^((minMb / 100 + (maxMb / 100 - minMb / 100) * gainIndex / totalSteps) / (10 * 2))
1356 // where 100 is the conversion from mB to dB, 10 comes from the log 10 conversion from power
1357 // ratios, and 2 means are the square of amplitude.
1358 float gain =
1359 pow(10, (minValue + (maxValue - minValue) * (gainIndex / (float)totalSteps)) / 2000);
1360 if (gain < 0) {
1361 LOG(ERROR) << __func__ << ": gain " << gain << " is less than 0";
1362 return false;
1363 }
1364 for (const auto& route : getConfig().routes) {
1365 if (route.sinkPortId != port.id) {
1366 continue;
1367 }
1368 for (const auto sourcePortId : route.sourcePortIds) {
1369 mStreams.setGain(sourcePortId, gain);
1370 }
1371 }
1372 return true;
1373}
1374
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001375ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1376 auto& patches = getConfig().patches;
1377 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1378 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001379 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001380 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001381 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1382 mPatches = std::move(patchesBackup);
1383 return status;
1384 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001385 patches.erase(patchIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301386 LOG(DEBUG) << __func__ << ": " << mType << ": erased patch " << in_patchId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001387 return ndk::ScopedAStatus::ok();
1388 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301389 LOG(ERROR) << __func__ << ": " << mType << ": patch id " << in_patchId << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001390 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1391}
1392
1393ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1394 auto& configs = getConfig().portConfigs;
1395 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1396 if (configIt != configs.end()) {
1397 if (mStreams.count(in_portConfigId) != 0) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301398 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001399 << " has a stream opened on it";
1400 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1401 }
1402 auto patchIt = mPatches.find(in_portConfigId);
1403 if (patchIt != mPatches.end()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301404 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001405 << " is used by the patch with id " << patchIt->second;
1406 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1407 }
1408 auto& initials = getConfig().initialConfigs;
1409 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1410 if (initialIt == initials.end()) {
1411 configs.erase(configIt);
Jaideep Sharma559a4912024-03-07 10:05:51 +05301412 LOG(DEBUG) << __func__ << ": " << mType << ": erased port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001413 } else if (*configIt != *initialIt) {
1414 *configIt = *initialIt;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301415 LOG(DEBUG) << __func__ << ": " << mType << ": reset port config " << in_portConfigId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001416 }
1417 return ndk::ScopedAStatus::ok();
1418 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301419 LOG(ERROR) << __func__ << ": " << mType << ": port config id " << in_portConfigId
1420 << " not found";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001421 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1422}
1423
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001424ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1425 *_aidl_return = mMasterMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301426 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001427 return ndk::ScopedAStatus::ok();
1428}
1429
1430ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301431 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001432 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1433 : onMasterMuteChanged(in_mute);
1434 if (result.isOk()) {
1435 mMasterMute = in_mute;
1436 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301437 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterMuteChanged("
1438 << in_mute << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001439 // Reset master mute if it failed.
1440 onMasterMuteChanged(mMasterMute);
1441 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001442 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001443}
1444
1445ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1446 *_aidl_return = mMasterVolume;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301447 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001448 return ndk::ScopedAStatus::ok();
1449}
1450
1451ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301452 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001453 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001454 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1455 : onMasterVolumeChanged(in_volume);
1456 if (result.isOk()) {
1457 mMasterVolume = in_volume;
1458 } else {
1459 // Reset master volume if it failed.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301460 LOG(ERROR) << __func__ << ": " << mType << ": failed calling onMasterVolumeChanged("
1461 << in_volume << "), error=" << result;
jiabin783c48b2023-02-28 18:28:06 +00001462 onMasterVolumeChanged(mMasterVolume);
1463 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001464 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001465 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301466 LOG(ERROR) << __func__ << ": " << mType << ": invalid master volume value: " << in_volume;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001467 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1468}
1469
1470ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1471 *_aidl_return = mMicMute;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301472 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001473 return ndk::ScopedAStatus::ok();
1474}
1475
1476ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301477 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_mute;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001478 mMicMute = in_mute;
1479 return ndk::ScopedAStatus::ok();
1480}
1481
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001482ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001483 *_aidl_return = getMicrophoneInfos();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301484 LOG(DEBUG) << __func__ << ": " << mType << ": returning "
1485 << ::android::internal::ToString(*_aidl_return);
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001486 return ndk::ScopedAStatus::ok();
1487}
1488
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001489ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001490 if (!isValidAudioMode(in_mode)) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301491 LOG(ERROR) << __func__ << ": " << mType << ": invalid mode " << toString(in_mode);
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001492 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1493 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001494 // No checks for supported audio modes here, it's an informative notification.
Jaideep Sharma559a4912024-03-07 10:05:51 +05301495 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_mode);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001496 return ndk::ScopedAStatus::ok();
1497}
1498
1499ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301500 LOG(DEBUG) << __func__ << ": " << mType << ": " << toString(in_rotation);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001501 return ndk::ScopedAStatus::ok();
1502}
1503
1504ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301505 LOG(DEBUG) << __func__ << ": " << mType << ": " << in_isTurnedOn;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001506 return ndk::ScopedAStatus::ok();
1507}
1508
Vlad Popa83a6d822022-11-07 13:53:57 +01001509ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001510 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001511 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001512 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001513 *_aidl_return = mSoundDose.getInstance();
Jaideep Sharma559a4912024-03-07 10:05:51 +05301514 LOG(DEBUG) << __func__ << ": " << mType
1515 << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001516 return ndk::ScopedAStatus::ok();
1517}
1518
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001519ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301520 LOG(DEBUG) << __func__ << ": " << mType;
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001521 (void)_aidl_return;
1522 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1523}
1524
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001525const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001526const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001527
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001528ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1529 std::vector<VendorParameter>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301530 LOG(VERBOSE) << __func__ << ": " << mType << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001531 bool allParametersKnown = true;
1532 for (const auto& id : in_ids) {
1533 if (id == VendorDebug::kForceTransientBurstName) {
1534 VendorParameter forceTransientBurst{.id = id};
1535 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1536 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001537 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1538 VendorParameter forceSynchronousDrain{.id = id};
1539 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1540 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001541 } else {
1542 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301543 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << id << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001544 }
1545 }
1546 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1547 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001548}
1549
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001550namespace {
1551
1552template <typename W>
1553bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1554 std::optional<W> value;
1555 binder_status_t result = p.ext.getParcelable(&value);
1556 if (result == STATUS_OK && value.has_value()) {
1557 *v = value.value().value;
1558 return true;
1559 }
1560 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1561 << "\": " << result;
1562 return false;
1563}
1564
1565} // namespace
1566
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001567ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1568 bool in_async) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301569 LOG(VERBOSE) << __func__ << ": " << mType << ": parameter count " << in_parameters.size()
1570 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001571 bool allParametersKnown = true;
1572 for (const auto& p : in_parameters) {
1573 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001574 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1575 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1576 }
1577 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1578 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001579 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1580 }
1581 } else {
1582 allParametersKnown = false;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301583 LOG(VERBOSE) << __func__ << ": " << mType << ": unrecognized parameter \"" << p.id
1584 << "\"";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001585 }
1586 }
1587 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1588 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001589}
1590
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001591ndk::ScopedAStatus Module::addDeviceEffect(
1592 int32_t in_portConfigId,
1593 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1594 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301595 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1596 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001597 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301598 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1599 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001600 }
1601 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1602}
1603
1604ndk::ScopedAStatus Module::removeDeviceEffect(
1605 int32_t in_portConfigId,
1606 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1607 if (in_effect == nullptr) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301608 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1609 << ", null effect";
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001610 } else {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301611 LOG(DEBUG) << __func__ << ": " << mType << ": port id " << in_portConfigId
1612 << ", effect Binder " << in_effect->asBinder().get();
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001613 }
1614 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1615}
1616
jiabin9a8e6862023-01-12 23:06:37 +00001617ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1618 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301619 LOG(DEBUG) << __func__ << ": " << mType << ": mmap policy type " << toString(mmapPolicyType);
jiabin9a8e6862023-01-12 23:06:37 +00001620 std::set<int32_t> mmapSinks;
1621 std::set<int32_t> mmapSources;
1622 auto& ports = getConfig().ports;
1623 for (const auto& port : ports) {
1624 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1625 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1626 AudioInputFlags::MMAP_NOIRQ)) {
1627 mmapSinks.insert(port.id);
1628 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1629 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1630 AudioOutputFlags::MMAP_NOIRQ)) {
1631 mmapSources.insert(port.id);
1632 }
1633 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001634 if (mmapSources.empty() && mmapSinks.empty()) {
1635 AudioMMapPolicyInfo never;
1636 never.mmapPolicy = AudioMMapPolicy::NEVER;
1637 _aidl_return->push_back(never);
1638 return ndk::ScopedAStatus::ok();
1639 }
jiabin9a8e6862023-01-12 23:06:37 +00001640 for (const auto& route : getConfig().routes) {
1641 if (mmapSinks.count(route.sinkPortId) != 0) {
1642 // The sink is a mix port, add the sources if they are device ports.
1643 for (int sourcePortId : route.sourcePortIds) {
1644 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1645 if (sourcePortIt == ports.end()) {
1646 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301647 LOG(ERROR) << __func__ << ": " << mType << ": port id " << sourcePortId
1648 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001649 continue;
1650 }
1651 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1652 // The source is not a device port, skip
1653 continue;
1654 }
1655 AudioMMapPolicyInfo policyInfo;
1656 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1657 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1658 // default implementation.
1659 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1660 _aidl_return->push_back(policyInfo);
1661 }
1662 } else {
1663 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1664 if (sinkPortIt == ports.end()) {
1665 // This must not happen
Jaideep Sharma559a4912024-03-07 10:05:51 +05301666 LOG(ERROR) << __func__ << ": " << mType << ": port id " << route.sinkPortId
1667 << " cannot be found";
jiabin9a8e6862023-01-12 23:06:37 +00001668 continue;
1669 }
1670 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1671 // The sink is not a device port, skip
1672 continue;
1673 }
1674 if (count_any(mmapSources, route.sourcePortIds)) {
1675 AudioMMapPolicyInfo policyInfo;
1676 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1677 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1678 // default implementation.
1679 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1680 _aidl_return->push_back(policyInfo);
1681 }
1682 }
1683 }
1684 return ndk::ScopedAStatus::ok();
1685}
1686
Eric Laurente2432ea2023-01-12 17:47:31 +01001687ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301688 LOG(DEBUG) << __func__ << ": " << mType;
Eric Laurente2432ea2023-01-12 17:47:31 +01001689 *_aidl_return = false;
1690 return ndk::ScopedAStatus::ok();
1691}
1692
jiabinb76981e2023-01-18 00:58:30 +00001693ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1694 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301695 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001696 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1697 }
1698 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301699 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001700 return ndk::ScopedAStatus::ok();
1701}
1702
1703ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1704 if (!isMmapSupported()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301705 LOG(DEBUG) << __func__ << ": " << mType << ": mmap is not supported ";
jiabinb76981e2023-01-18 00:58:30 +00001706 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1707 }
1708 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
Jaideep Sharma559a4912024-03-07 10:05:51 +05301709 LOG(DEBUG) << __func__ << ": " << mType << ": returning " << *_aidl_return;
jiabinb76981e2023-01-18 00:58:30 +00001710 return ndk::ScopedAStatus::ok();
1711}
1712
1713bool Module::isMmapSupported() {
1714 if (mIsMmapSupported.has_value()) {
1715 return mIsMmapSupported.value();
1716 }
1717 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1718 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1719 mIsMmapSupported = false;
1720 } else {
1721 mIsMmapSupported =
1722 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1723 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1724 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1725 }) != mmapPolicyInfos.end();
1726 }
1727 return mIsMmapSupported.value();
1728}
1729
Mikhail Naganova92039a2023-12-20 14:27:22 -08001730ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001731 if (audioPort->ext.getTag() != AudioPortExt::device) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301732 LOG(ERROR) << __func__ << ": " << mType << ": not a device port: " << audioPort->toString();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001733 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1734 }
1735 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1736 if (!devicePort.device.type.connection.empty()) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301737 LOG(ERROR) << __func__ << ": " << mType << ": module implementation must override "
1738 "'populateConnectedDevicePort' "
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001739 << "to handle connection of external devices.";
1740 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1741 }
Jaideep Sharma559a4912024-03-07 10:05:51 +05301742 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001743 return ndk::ScopedAStatus::ok();
1744}
1745
1746ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1747 const std::vector<AudioPortConfig*>& sources __unused,
1748 const std::vector<AudioPortConfig*>& sinks __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301749 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001750 return ndk::ScopedAStatus::ok();
1751}
1752
jiabin783c48b2023-02-28 18:28:06 +00001753void Module::onExternalDeviceConnectionChanged(
1754 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1755 bool connected __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301756 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabin783c48b2023-02-28 18:28:06 +00001757}
1758
jiabindd23b0e2023-12-11 19:10:05 +00001759void Module::onPrepareToDisconnectExternalDevice(
1760 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301761 LOG(DEBUG) << __func__ << ": " << mType << ": do nothing and return";
jiabindd23b0e2023-12-11 19:10:05 +00001762}
1763
jiabin783c48b2023-02-28 18:28:06 +00001764ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301765 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001766 return ndk::ScopedAStatus::ok();
1767}
1768
1769ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
Jaideep Sharma559a4912024-03-07 10:05:51 +05301770 LOG(VERBOSE) << __func__ << ": " << mType << ": do nothing and return ok";
jiabin783c48b2023-02-28 18:28:06 +00001771 return ndk::ScopedAStatus::ok();
1772}
1773
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001774std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1775 std::vector<MicrophoneInfo> result;
1776 Configuration& config = getConfig();
1777 for (const AudioPort& port : config.ports) {
1778 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1779 const AudioDeviceType deviceType =
1780 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1781 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1782 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1783 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1784 // accordingly based on their physical microphone parameters.
1785 result.push_back(MicrophoneInfo{
1786 .id = port.name,
1787 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1788 .group = 0,
1789 .indexInTheGroup = 0,
1790 });
1791 }
1792 }
1793 }
1794 return result;
1795}
1796
Ram Mohan18f0d512023-07-01 00:47:09 +05301797ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1798 return mStreams.bluetoothParametersUpdated();
1799}
1800
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001801} // namespace aidl::android::hardware::audio::core