blob: a77397d5b67b4b08e3bf3c13f97c2cac3879e2c9 [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 <Utils.h>
22#include <aidl/android/media/audio/common/AudioInputFlags.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000023#include <aidl/android/media/audio/common/AudioOutputFlags.h>
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -070024#include <android-base/logging.h>
25#include <android/binder_ibinder_platform.h>
26#include <error/expected_utils.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000027
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000028#include "core-impl/Configuration.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000029#include "core-impl/Module.h"
Mikhail Naganovb03b5c42023-07-26 13:13:35 -070030#include "core-impl/ModuleBluetooth.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070031#include "core-impl/ModulePrimary.h"
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053032#include "core-impl/ModuleRemoteSubmix.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070033#include "core-impl/ModuleStub.h"
jiabin253bd322023-01-25 23:57:31 +000034#include "core-impl/ModuleUsb.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010035#include "core-impl/SoundDose.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000036#include "core-impl/utils.h"
37
Mikhail Naganov872d4a62023-03-09 18:19:01 -080038using aidl::android::hardware::audio::common::getFrameSizeInBytes;
39using aidl::android::hardware::audio::common::isBitPositionFlagSet;
40using aidl::android::hardware::audio::common::isValidAudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000041using aidl::android::hardware::audio::common::SinkMetadata;
42using aidl::android::hardware::audio::common::SourceMetadata;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010043using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000044using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000045using aidl::android::media::audio::common::AudioDevice;
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +000046using aidl::android::media::audio::common::AudioDeviceType;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000047using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000048using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000049using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000050using aidl::android::media::audio::common::AudioIoFlags;
jiabin9a8e6862023-01-12 23:06:37 +000051using aidl::android::media::audio::common::AudioMMapPolicy;
52using aidl::android::media::audio::common::AudioMMapPolicyInfo;
53using aidl::android::media::audio::common::AudioMMapPolicyType;
Mikhail Naganov04ae8222023-01-11 15:48:10 -080054using aidl::android::media::audio::common::AudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000055using aidl::android::media::audio::common::AudioOffloadInfo;
56using aidl::android::media::audio::common::AudioOutputFlags;
57using aidl::android::media::audio::common::AudioPort;
58using aidl::android::media::audio::common::AudioPortConfig;
59using aidl::android::media::audio::common::AudioPortExt;
60using aidl::android::media::audio::common::AudioProfile;
Mikhail Naganov20047bc2023-01-05 20:16:07 +000061using aidl::android::media::audio::common::Boolean;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000062using aidl::android::media::audio::common::Int;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080063using aidl::android::media::audio::common::MicrophoneInfo;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000064using aidl::android::media::audio::common::PcmType;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000065
66namespace aidl::android::hardware::audio::core {
67
68namespace {
69
Mikhail Naganov84bcc042023-10-05 17:36:57 -070070inline bool hasDynamicChannelMasks(const std::vector<AudioChannelLayout>& channelMasks) {
71 return channelMasks.empty() ||
72 std::all_of(channelMasks.begin(), channelMasks.end(),
73 [](const auto& channelMask) { return channelMask == AudioChannelLayout{}; });
74}
75
76inline bool hasDynamicFormat(const AudioFormatDescription& format) {
77 return format == AudioFormatDescription{};
78}
79
80inline bool hasDynamicSampleRates(const std::vector<int32_t>& sampleRates) {
81 return sampleRates.empty() ||
82 std::all_of(sampleRates.begin(), sampleRates.end(),
83 [](const auto& sampleRate) { return sampleRate == 0; });
84}
85
86inline bool isDynamicProfile(const AudioProfile& profile) {
87 return hasDynamicFormat(profile.format) || hasDynamicChannelMasks(profile.channelMasks) ||
88 hasDynamicSampleRates(profile.sampleRates);
89}
90
91bool hasDynamicProfilesOnly(const std::vector<AudioProfile>& profiles) {
92 if (profiles.empty()) return true;
93 return std::all_of(profiles.begin(), profiles.end(), isDynamicProfile);
94}
95
96// Note: does not assign an ID to the config.
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000097bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -070098 const bool allowDynamicConfig = port.ext.getTag() == AudioPortExt::device;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000099 *config = {};
100 config->portId = port.id;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700101 for (const auto& profile : port.profiles) {
102 if (isDynamicProfile(profile)) continue;
103 config->format = profile.format;
104 config->channelMask = *profile.channelMasks.begin();
105 config->sampleRate = Int{.value = *profile.sampleRates.begin()};
106 config->flags = port.flags;
107 config->ext = port.ext;
108 return true;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000109 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700110 if (allowDynamicConfig) {
111 config->format = AudioFormatDescription{};
112 config->channelMask = AudioChannelLayout{};
113 config->sampleRate = Int{.value = 0};
114 config->flags = port.flags;
115 config->ext = port.ext;
116 return true;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000117 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700118 LOG(ERROR) << __func__ << ": port " << port.id << " only has dynamic profiles";
119 return false;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000120}
121
122bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
123 AudioProfile* profile) {
124 if (auto profilesIt =
125 find_if(port.profiles.begin(), port.profiles.end(),
126 [&format](const auto& profile) { return profile.format == format; });
127 profilesIt != port.profiles.end()) {
128 *profile = *profilesIt;
129 return true;
130 }
131 return false;
132}
Mikhail Naganov00603d12022-05-02 22:52:13 +0000133
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000134} // namespace
135
jiabin253bd322023-01-25 23:57:31 +0000136// static
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000137std::shared_ptr<Module> Module::createInstance(Type type, std::unique_ptr<Configuration>&& config) {
jiabin253bd322023-01-25 23:57:31 +0000138 switch (type) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530139 case Type::DEFAULT:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000140 return ndk::SharedRefBase::make<ModulePrimary>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700141 case Type::R_SUBMIX:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000142 return ndk::SharedRefBase::make<ModuleRemoteSubmix>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700143 case Type::STUB:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000144 return ndk::SharedRefBase::make<ModuleStub>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700145 case Type::USB:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000146 return ndk::SharedRefBase::make<ModuleUsb>(std::move(config));
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700147 case Type::BLUETOOTH:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000148 return ndk::SharedRefBase::make<ModuleBluetooth>(std::move(config));
jiabin253bd322023-01-25 23:57:31 +0000149 }
150}
151
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000152// static
153std::optional<Module::Type> Module::typeFromString(const std::string& type) {
154 if (type == "default")
155 return Module::Type::DEFAULT;
156 else if (type == "r_submix")
157 return Module::Type::R_SUBMIX;
158 else if (type == "stub")
159 return Module::Type::STUB;
160 else if (type == "usb")
161 return Module::Type::USB;
162 else if (type == "bluetooth")
163 return Module::Type::BLUETOOTH;
164 return {};
165}
166
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700167std::ostream& operator<<(std::ostream& os, Module::Type t) {
168 switch (t) {
169 case Module::Type::DEFAULT:
170 os << "default";
171 break;
172 case Module::Type::R_SUBMIX:
173 os << "r_submix";
174 break;
Mikhail Naganov521fc492023-07-11 17:24:08 -0700175 case Module::Type::STUB:
176 os << "stub";
177 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700178 case Module::Type::USB:
179 os << "usb";
180 break;
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700181 case Module::Type::BLUETOOTH:
182 os << "bluetooth";
183 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700184 }
185 return os;
186}
187
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000188Module::Module(Type type, std::unique_ptr<Configuration>&& config)
189 : mType(type), mConfig(std::move(config)) {
190 populateConnectedProfiles();
191}
192
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000193void Module::cleanUpPatch(int32_t patchId) {
194 erase_all_values(mPatches, std::set<int32_t>{patchId});
195}
196
Mikhail Naganov8651b362023-01-06 23:15:27 +0000197ndk::ScopedAStatus Module::createStreamContext(
198 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
199 std::shared_ptr<IStreamCallback> asyncCallback,
200 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000201 if (in_bufferSizeFrames <= 0) {
202 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
203 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
204 }
205 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
206 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
207 << ", must be at least " << kMinimumStreamBufferSizeFrames;
208 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
209 }
210 auto& configs = getConfig().portConfigs;
211 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000212 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000213 // validity of the portConfigId has already been checked.
214 const size_t frameSize =
215 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
216 if (frameSize == 0) {
217 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
218 << portConfigIt->toString();
219 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
220 }
221 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700222 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000223 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
224 << " frames is too large, maximum size is "
225 << kMaximumStreamBufferSizeBytes / frameSize;
226 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
227 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000228 const auto& flags = portConfigIt->flags.value();
229 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000230 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
231 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000232 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000233 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
234 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000235 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
Mikhail Naganov194daaa2023-01-05 22:34:20 +0000236 mVendorDebug.forceTransientBurst,
237 mVendorDebug.forceSynchronousDrain};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000238 StreamContext temp(
239 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
240 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530241 portConfigIt->portId, portConfigIt->format.value(),
242 portConfigIt->channelMask.value(), portConfigIt->sampleRate.value().value, flags,
Mikhail Naganovb42a69e2023-06-16 12:38:25 -0700243 portConfigIt->ext.get<AudioPortExt::mix>().handle,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000244 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov3c8b6ce2023-10-31 11:20:30 -0700245 asyncCallback, outEventCallback,
246 std::weak_ptr<sounddose::StreamDataProcessorInterface>{}, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000247 if (temp.isValid()) {
248 *out_context = std::move(temp);
249 } else {
250 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
251 }
252 } else {
253 // TODO: Implement simulation of MMAP buffer allocation
254 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000255 return ndk::ScopedAStatus::ok();
256}
257
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000258std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
259 std::vector<AudioDevice> result;
260 auto& ports = getConfig().ports;
261 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
262 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
263 auto portIt = findById<AudioPort>(ports, *it);
264 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
265 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
266 }
267 }
268 return result;
269}
270
271std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
272 std::set<int32_t> result;
273 auto patchIdsRange = mPatches.equal_range(portConfigId);
274 auto& patches = getConfig().patches;
275 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
276 auto patchIt = findById<AudioPatch>(patches, it->second);
277 if (patchIt == patches.end()) {
278 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
279 << "not found in the configuration";
280 }
281 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
282 portConfigId) != patchIt->sourcePortConfigIds.end()) {
283 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
284 } else {
285 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
286 }
287 }
288 return result;
289}
290
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000291ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
292 auto& configs = getConfig().portConfigs;
293 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
294 if (portConfigIt == configs.end()) {
295 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
296 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
297 }
298 const int32_t portId = portConfigIt->portId;
299 // In our implementation, configs of mix ports always have unique IDs.
300 CHECK(portId != in_portConfigId);
301 auto& ports = getConfig().ports;
302 auto portIt = findById<AudioPort>(ports, portId);
303 if (portIt == ports.end()) {
304 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
305 << in_portConfigId << " not found";
306 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
307 }
308 if (mStreams.count(in_portConfigId) != 0) {
309 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
310 << " already has a stream opened on it";
311 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
312 }
313 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
314 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
315 << " does not correspond to a mix port";
316 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
317 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700318 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000319 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
320 LOG(ERROR) << __func__ << ": port id " << portId
321 << " has already reached maximum allowed opened stream count: "
322 << maxOpenStreamCount;
323 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
324 }
325 *port = &(*portIt);
326 return ndk::ScopedAStatus::ok();
327}
328
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000329void Module::populateConnectedProfiles() {
330 Configuration& config = getConfig();
331 for (const AudioPort& port : config.ports) {
332 if (port.ext.getTag() == AudioPortExt::device) {
333 if (auto devicePort = port.ext.get<AudioPortExt::device>();
334 !devicePort.device.type.connection.empty() && port.profiles.empty()) {
335 if (auto connIt = config.connectedProfiles.find(port.id);
336 connIt == config.connectedProfiles.end()) {
337 config.connectedProfiles.emplace(
338 port.id, internal::getStandard16And24BitPcmAudioProfiles());
339 }
340 }
341 }
342 }
343}
344
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000345template <typename C>
346std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
347 std::set<int32_t> result;
348 auto& portConfigs = getConfig().portConfigs;
349 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
350 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
351 if (portConfigIt != portConfigs.end()) {
352 result.insert(portConfigIt->portId);
353 }
354 }
355 return result;
356}
357
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000358std::unique_ptr<Module::Configuration> Module::initializeConfig() {
359 return internal::getConfiguration(getType());
Peter Yoon918a6a52023-07-13 17:04:37 +0900360}
361
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700362std::vector<AudioRoute*> Module::getAudioRoutesForAudioPortImpl(int32_t portId) {
363 std::vector<AudioRoute*> result;
364 auto& routes = getConfig().routes;
365 for (auto& r : routes) {
366 const auto& srcs = r.sourcePortIds;
367 if (r.sinkPortId == portId || std::find(srcs.begin(), srcs.end(), portId) != srcs.end()) {
368 result.push_back(&r);
369 }
370 }
371 return result;
372}
373
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000374Module::Configuration& Module::getConfig() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000375 if (!mConfig) {
Peter Yoon918a6a52023-07-13 17:04:37 +0900376 mConfig = std::move(initializeConfig());
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000377 }
378 return *mConfig;
379}
380
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700381std::set<int32_t> Module::getRoutableAudioPortIds(int32_t portId,
382 std::vector<AudioRoute*>* routes) {
383 std::vector<AudioRoute*> routesStorage;
384 if (routes == nullptr) {
385 routesStorage = getAudioRoutesForAudioPortImpl(portId);
386 routes = &routesStorage;
387 }
388 std::set<int32_t> result;
389 for (AudioRoute* r : *routes) {
390 if (r->sinkPortId == portId) {
391 result.insert(r->sourcePortIds.begin(), r->sourcePortIds.end());
392 } else {
393 result.insert(r->sinkPortId);
394 }
395 }
396 return result;
397}
398
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000399void Module::registerPatch(const AudioPatch& patch) {
400 auto& configs = getConfig().portConfigs;
401 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
402 for (auto portConfigId : portConfigIds) {
403 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
404 if (configIt != configs.end()) {
405 mPatches.insert(std::pair{portConfigId, patch.id});
406 if (configIt->portId != portConfigId) {
407 mPatches.insert(std::pair{configIt->portId, patch.id});
408 }
409 }
410 };
411 };
412 do_insert(patch.sourcePortConfigIds);
413 do_insert(patch.sinkPortConfigIds);
414}
415
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700416ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
417 const AudioPatch& newPatch) {
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700418 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700419 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700420 using Connections =
421 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
422 Connections oldConnections, newConnections;
423 auto fillConnectionsHelper = [&](Connections& connections,
424 const std::vector<int32_t>& mixPortCfgIds,
425 const std::vector<int32_t>& devicePortCfgIds) {
426 for (int32_t mixPortCfgId : mixPortCfgIds) {
427 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
428 }
429 };
430 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
431 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
432 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
433 patch.sourcePortConfigIds.end()) {
434 // Sources are mix ports.
435 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
436 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
437 [&](int32_t portConfigId) {
438 return mStreams.count(portConfigId) > 0;
439 }) != patch.sinkPortConfigIds.end()) {
440 // Sources are device ports.
441 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
442 } // Otherwise, there are no streams to notify.
443 };
444 fillConnections(oldConnections, oldPatch);
445 fillConnections(newConnections, newPatch);
446
447 std::for_each(oldConnections.begin(), oldConnections.end(), [&](const auto& connectionPair) {
448 const int32_t mixPortConfigId = connectionPair.first;
449 if (auto it = newConnections.find(mixPortConfigId);
450 it == newConnections.end() || it->second != connectionPair.second) {
451 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, {});
452 status.isOk()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700453 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700454 << mixPortConfigId << " has been disconnected";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700455 } else {
456 // Disconnection is tricky to roll back, just register a failure.
457 maybeFailure = std::move(status);
458 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000459 }
460 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700461 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700462 std::set<int32_t> idsToDisconnectOnFailure;
463 std::for_each(newConnections.begin(), newConnections.end(), [&](const auto& connectionPair) {
464 const int32_t mixPortConfigId = connectionPair.first;
465 if (auto it = oldConnections.find(mixPortConfigId);
466 it == oldConnections.end() || it->second != connectionPair.second) {
467 const auto connectedDevices = findConnectedDevices(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700468 if (connectedDevices.empty()) {
469 // This is important as workers use the vector size to derive the connection status.
470 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
471 "config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700472 << mixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700473 }
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700474 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700475 status.isOk()) {
476 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700477 << mixPortConfigId << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700478 << ::android::internal::ToString(connectedDevices);
479 } else {
480 maybeFailure = std::move(status);
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700481 idsToDisconnectOnFailure.insert(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700482 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000483 }
484 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700485 if (!maybeFailure.isOk()) {
486 LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids "
487 << ::android::internal::ToString(idsToDisconnectOnFailure);
488 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
489 [&](const auto& portConfigId) {
490 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
491 (void)status.isOk(); // Can't do much about a failure here.
492 });
493 return maybeFailure;
494 }
495 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000496}
497
Mikhail Naganov00603d12022-05-02 22:52:13 +0000498ndk::ScopedAStatus Module::setModuleDebug(
499 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700500 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000501 << ", new flags: " << in_debug.toString();
502 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
503 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700504 LOG(ERROR) << __func__ << ": " << mType
505 << ": attempting to change device connections simulation while having external "
506 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000507 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
508 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000509 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700510 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000511 << in_debug.streamTransientStateDelayMs;
512 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
513 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000514 mDebug = in_debug;
515 return ndk::ScopedAStatus::ok();
516}
517
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000518ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700519 *_aidl_return = nullptr;
520 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000521 return ndk::ScopedAStatus::ok();
522}
523
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000524ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700525 *_aidl_return = nullptr;
526 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000527 return ndk::ScopedAStatus::ok();
528}
529
Mikhail Naganov7499a002023-02-27 18:51:44 -0800530ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700531 *_aidl_return = nullptr;
532 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800533 return ndk::ScopedAStatus::ok();
534}
535
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800536ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700537 *_aidl_return = nullptr;
538 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800539 return ndk::ScopedAStatus::ok();
540}
541
Mikhail Naganov00603d12022-05-02 22:52:13 +0000542ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
543 AudioPort* _aidl_return) {
544 const int32_t templateId = in_templateIdAndAdditionalData.id;
545 auto& ports = getConfig().ports;
546 AudioPort connectedPort;
547 { // Scope the template port so that we don't accidentally modify it.
548 auto templateIt = findById<AudioPort>(ports, templateId);
549 if (templateIt == ports.end()) {
550 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
551 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
552 }
553 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
554 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
555 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
556 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000557 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
558 if (templateDevicePort.device.type.connection.empty()) {
559 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
560 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
561 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700562 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
563 LOG(ERROR) << __func__ << ": port id " << templateId << " is a connected device port";
564 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
565 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000566 // Postpone id allocation until we ensure that there are no client errors.
567 connectedPort = *templateIt;
568 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
569 const auto& inputDevicePort =
570 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
571 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
572 connectedDevicePort.device.address = inputDevicePort.device.address;
573 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
574 << connectedDevicePort.device.toString();
575 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700576 for (auto connectedPortPair : mConnectedDevicePorts) {
577 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000578 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
579 connectedDevicePort.device) {
580 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700581 << " is already connected at the device port id "
582 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000583 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
584 }
585 }
586 }
587
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700588 // Two main cases are considered with regard to the profiles of the connected device port:
589 //
590 // 1. If the template device port has dynamic profiles, and at least one routable mix
591 // port also has dynamic profiles, it means that after connecting the device, the
592 // connected device port must have profiles populated with actual capabilities of
593 // the connected device, and dynamic of routable mix ports will be filled
594 // according to these capabilities. An example of this case is connection of an
595 // HDMI or USB device. For USB handled by ADSP, there can be mix ports with static
596 // profiles, and one dedicated mix port for "hi-fi" playback. The latter is left with
597 // dynamic profiles so that they can be populated with actual capabilities of
598 // the connected device.
599 //
600 // 2. If the template device port has dynamic profiles, while all routable mix ports
601 // have static profiles, it means that after connecting the device, the connected
602 // device port can be left with dynamic profiles, and profiles of mix ports are
603 // left untouched. An example of this case is connection of an analog wired
604 // headset, it should be treated in the same way as a speaker.
605 //
606 // Yet another possible case is when both the template device port and all routable
607 // mix ports have static profiles. This is allowed and handled correctly, however, it
608 // is not very practical, since these profiles are likely duplicates of each other.
609
610 std::vector<AudioRoute*> routesToMixPorts = getAudioRoutesForAudioPortImpl(templateId);
611 std::set<int32_t> routableMixPortIds = getRoutableAudioPortIds(templateId, &routesToMixPorts);
612 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700613 if (!mDebug.simulateDeviceConnections) {
614 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
615 } else {
616 auto& connectedProfiles = getConfig().connectedProfiles;
617 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
618 connectedProfilesIt != connectedProfiles.end()) {
619 connectedPort.profiles = connectedProfilesIt->second;
620 }
621 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700622 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
623 // Possible case 2. Check if all routable mix ports have static profiles.
624 if (auto dynamicMixPortIt = std::find_if(ports.begin(), ports.end(),
625 [&routableMixPortIds](const auto& p) {
626 return routableMixPortIds.count(p.id) >
627 0 &&
628 hasDynamicProfilesOnly(p.profiles);
629 });
630 dynamicMixPortIt != ports.end()) {
631 LOG(ERROR) << __func__
632 << ": connected port only has dynamic profiles after connecting "
633 << "external device " << connectedPort.toString() << ", and there exist "
634 << "a routable mix port with dynamic profiles: "
635 << dynamicMixPortIt->toString();
636 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
637 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530638 }
639 }
640
Mikhail Naganovdc417732023-09-29 15:49:35 -0700641 connectedPort.id = getConfig().nextPortId++;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700642 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700643 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000644 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
645 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000646 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000647 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000648
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700649 // For routes where the template port is a source, add the connected port to sources,
650 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000651 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700652 for (AudioRoute* r : routesToMixPorts) {
653 if (r->sinkPortId == templateId) {
654 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
655 .sinkPortId = connectedPort.id,
656 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000657 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700658 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000659 }
660 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700661 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000662 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
663
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700664 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
665 // Note: this is a simplistic approach assuming that a mix port can only be populated
666 // from a single device port. Implementing support for stuffing dynamic profiles with
667 // a superset of all profiles from all routable dynamic device ports would be more involved.
668 for (auto& port : ports) {
669 if (routableMixPortIds.count(port.id) == 0) continue;
670 if (hasDynamicProfilesOnly(port.profiles)) {
671 port.profiles = connectedPort.profiles;
672 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700673 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700674 // Check if profiles are not all dynamic because they were populated by
675 // a previous connection. Otherwise, it means that they are actually static.
676 for (const auto& cp : mConnectedDevicePorts) {
677 if (cp.second.count(port.id) > 0) {
678 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700679 break;
680 }
681 }
682 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700683 }
684 }
685 *_aidl_return = std::move(connectedPort);
686
Mikhail Naganov00603d12022-05-02 22:52:13 +0000687 return ndk::ScopedAStatus::ok();
688}
689
690ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
691 auto& ports = getConfig().ports;
692 auto portIt = findById<AudioPort>(ports, in_portId);
693 if (portIt == ports.end()) {
694 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
695 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
696 }
697 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
698 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
699 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
700 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700701 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
702 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000703 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
704 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
705 }
706 auto& configs = getConfig().portConfigs;
707 auto& initials = getConfig().initialConfigs;
708 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
709 if (config.portId == in_portId) {
710 // Check if the configuration was provided by the client.
711 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
712 return initialIt == initials.end() || config != *initialIt;
713 }
714 return false;
715 });
716 if (configIt != configs.end()) {
717 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
718 << configIt->id;
719 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
720 }
jiabin783c48b2023-02-28 18:28:06 +0000721 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000722 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000723 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
724
725 auto& routes = getConfig().routes;
726 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
727 if (routesIt->sinkPortId == in_portId) {
728 routesIt = routes.erase(routesIt);
729 } else {
730 // Note: the list of sourcePortIds can't become empty because there must
731 // be the id of the template port in the route.
732 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
733 ++routesIt;
734 }
735 }
736
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700737 // Clear profiles for mix ports that are not connected to any other ports.
738 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
739 mConnectedDevicePorts.erase(connectedPortsIt);
740 for (const auto& connectedPort : mConnectedDevicePorts) {
741 for (int32_t mixPortId : connectedPort.second) {
742 mixPortsToClear.erase(mixPortId);
743 }
744 }
745 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700746 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
747 if (mixPortIt != ports.end()) {
748 mixPortIt->profiles = {};
749 }
750 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700751
Mikhail Naganov00603d12022-05-02 22:52:13 +0000752 return ndk::ScopedAStatus::ok();
753}
754
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000755ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
756 *_aidl_return = getConfig().patches;
757 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
758 return ndk::ScopedAStatus::ok();
759}
760
761ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
762 auto& ports = getConfig().ports;
763 auto portIt = findById<AudioPort>(ports, in_portId);
764 if (portIt != ports.end()) {
765 *_aidl_return = *portIt;
766 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
767 return ndk::ScopedAStatus::ok();
768 }
769 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
770 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
771}
772
773ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
774 *_aidl_return = getConfig().portConfigs;
775 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
776 return ndk::ScopedAStatus::ok();
777}
778
779ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
780 *_aidl_return = getConfig().ports;
781 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
782 return ndk::ScopedAStatus::ok();
783}
784
785ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
786 *_aidl_return = getConfig().routes;
787 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
788 return ndk::ScopedAStatus::ok();
789}
790
Mikhail Naganov00603d12022-05-02 22:52:13 +0000791ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
792 std::vector<AudioRoute>* _aidl_return) {
793 auto& ports = getConfig().ports;
794 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
795 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
796 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
797 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700798 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
799 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
800 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000801 return ndk::ScopedAStatus::ok();
802}
803
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000804ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
805 OpenInputStreamReturn* _aidl_return) {
806 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
807 << in_args.bufferSizeFrames << " frames";
808 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700809 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000810 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
811 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000812 << " does not correspond to an input mix port";
813 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
814 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000815 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700816 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
817 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000818 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000819 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700820 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000821 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000822 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700823 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
824 RETURN_STATUS_IF_ERROR(
825 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
826 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000827 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
828 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000829 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000830 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000831 return ndk::ScopedAStatus::ok();
832}
833
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000834ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
835 OpenOutputStreamReturn* _aidl_return) {
836 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
837 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
838 << " frames";
839 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700840 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000841 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
842 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000843 << " does not correspond to an output mix port";
844 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
845 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000846 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
847 AudioOutputFlags::COMPRESS_OFFLOAD);
848 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000849 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000850 << " has COMPRESS_OFFLOAD flag set, requires offload info";
851 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
852 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000853 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
854 AudioOutputFlags::NON_BLOCKING);
855 if (isNonBlocking && in_args.callback == nullptr) {
856 LOG(ERROR) << __func__ << ": port id " << port->id
857 << " has NON_BLOCKING flag set, requires async callback";
858 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
859 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000860 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700861 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
862 isNonBlocking ? in_args.callback : nullptr,
863 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000864 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000865 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700866 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700867 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000868 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700869 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
870 RETURN_STATUS_IF_ERROR(
871 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
872 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000873 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
874 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000875 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000876 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000877 return ndk::ScopedAStatus::ok();
878}
879
Mikhail Naganov74927202022-12-19 16:37:14 +0000880ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
881 SupportedPlaybackRateFactors* _aidl_return) {
882 LOG(DEBUG) << __func__;
883 (void)_aidl_return;
884 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
885}
886
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000887ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000888 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000889 if (in_requested.sourcePortConfigIds.empty()) {
890 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
891 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
892 }
893 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
894 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
895 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
896 }
897 if (in_requested.sinkPortConfigIds.empty()) {
898 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
899 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
900 }
901 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
902 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
903 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
904 }
905
906 auto& configs = getConfig().portConfigs;
907 std::vector<int32_t> missingIds;
908 auto sources =
909 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
910 if (!missingIds.empty()) {
911 LOG(ERROR) << __func__ << ": following source port config ids not found: "
912 << ::android::internal::ToString(missingIds);
913 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
914 }
915 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
916 if (!missingIds.empty()) {
917 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
918 << ::android::internal::ToString(missingIds);
919 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
920 }
921 // bool indicates whether a non-exclusive route is available.
922 // If only an exclusive route is available, that means the patch can not be
923 // established if there is any other patch which currently uses the sink port.
924 std::map<int32_t, bool> allowedSinkPorts;
925 auto& routes = getConfig().routes;
926 for (auto src : sources) {
927 for (const auto& r : routes) {
928 const auto& srcs = r.sourcePortIds;
929 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
930 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
931 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
932 }
933 }
934 }
935 }
936 for (auto sink : sinks) {
937 if (allowedSinkPorts.count(sink->portId) == 0) {
938 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
939 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
940 }
941 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700942 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000943
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000944 auto& patches = getConfig().patches;
945 auto existing = patches.end();
946 std::optional<decltype(mPatches)> patchesBackup;
947 if (in_requested.id != 0) {
948 existing = findById<AudioPatch>(patches, in_requested.id);
949 if (existing != patches.end()) {
950 patchesBackup = mPatches;
951 cleanUpPatch(existing->id);
952 } else {
953 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
954 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
955 }
956 }
957 // Validate the requested patch.
958 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
959 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
960 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
961 << "is exclusive and is already used by some other patch";
962 if (patchesBackup.has_value()) {
963 mPatches = std::move(*patchesBackup);
964 }
965 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
966 }
967 }
968 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000969 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
970 _aidl_return->latenciesMs.clear();
971 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
972 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000973 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000974 if (existing == patches.end()) {
975 _aidl_return->id = getConfig().nextPatchId++;
976 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000977 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000978 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -0700979 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000980 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700981 patchesBackup = mPatches;
982 registerPatch(*_aidl_return);
983 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
984 mPatches = std::move(*patchesBackup);
985 if (existing == patches.end()) {
986 patches.pop_back();
987 } else {
988 *existing = oldPatch;
989 }
990 return status;
991 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000992
993 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
994 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000995 return ndk::ScopedAStatus::ok();
996}
997
998ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
999 AudioPortConfig* out_suggested, bool* _aidl_return) {
1000 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
1001 auto& configs = getConfig().portConfigs;
1002 auto existing = configs.end();
1003 if (in_requested.id != 0) {
1004 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1005 existing == configs.end()) {
1006 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
1007 << " not found";
1008 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1009 }
1010 }
1011
1012 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1013 if (portId == 0) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001014 LOG(ERROR) << __func__ << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001015 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1016 }
1017 auto& ports = getConfig().ports;
1018 auto portIt = findById<AudioPort>(ports, portId);
1019 if (portIt == ports.end()) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001020 LOG(ERROR) << __func__ << ": requested port config points to non-existent portId "
1021 << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001022 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1023 }
1024 if (existing != configs.end()) {
1025 *out_suggested = *existing;
1026 } else {
1027 AudioPortConfig newConfig;
1028 if (generateDefaultPortConfig(*portIt, &newConfig)) {
1029 *out_suggested = newConfig;
1030 } else {
1031 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
1032 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1033 }
1034 }
1035 // From this moment, 'out_suggested' is either an existing port config,
1036 // or a new generated config. Now attempt to update it according to the specified
1037 // fields of 'in_requested'.
1038
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001039 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1040 // which takes care of their actual configuration automatically.
1041 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1042 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001043 bool requestedIsValid = true, requestedIsFullySpecified = true;
1044
1045 AudioIoFlags portFlags = portIt->flags;
1046 if (in_requested.flags.has_value()) {
1047 if (in_requested.flags.value() != portFlags) {
1048 LOG(WARNING) << __func__ << ": requested flags "
1049 << in_requested.flags.value().toString() << " do not match port's "
1050 << portId << " flags " << portFlags.toString();
1051 requestedIsValid = false;
1052 }
1053 } else {
1054 requestedIsFullySpecified = false;
1055 }
1056
1057 AudioProfile portProfile;
1058 if (in_requested.format.has_value()) {
1059 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001060 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1061 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001062 out_suggested->format = format;
1063 } else {
1064 LOG(WARNING) << __func__ << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001065 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001066 requestedIsValid = false;
1067 }
1068 } else {
1069 requestedIsFullySpecified = false;
1070 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001071 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1072 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001073 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
1074 << out_suggested->format.value().toString() << " anymore";
1075 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1076 }
1077
1078 if (in_requested.channelMask.has_value()) {
1079 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001080 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1081 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1082 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001083 out_suggested->channelMask = channelMask;
1084 } else {
1085 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
1086 << " is not supported for the format " << portProfile.format.toString()
1087 << " by the port " << portId;
1088 requestedIsValid = false;
1089 }
1090 } else {
1091 requestedIsFullySpecified = false;
1092 }
1093
1094 if (in_requested.sampleRate.has_value()) {
1095 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001096 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1097 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001098 sampleRate.value) != portProfile.sampleRates.end()) {
1099 out_suggested->sampleRate = sampleRate;
1100 } else {
1101 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
1102 << " is not supported for the format " << portProfile.format.toString()
1103 << " by the port " << portId;
1104 requestedIsValid = false;
1105 }
1106 } else {
1107 requestedIsFullySpecified = false;
1108 }
1109
1110 if (in_requested.gain.has_value()) {
1111 // Let's pretend that gain can always be applied.
1112 out_suggested->gain = in_requested.gain.value();
1113 }
1114
Mikhail Naganov248e9502023-02-21 16:32:40 -08001115 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1116 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1117 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
1118 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
1119 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
1120 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
1121 }
1122 } else {
1123 LOG(WARNING) << __func__ << ": requested ext tag "
1124 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1125 << toString(out_suggested->ext.getTag());
1126 requestedIsValid = false;
1127 }
1128 }
1129
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001130 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1131 out_suggested->id = getConfig().nextPortId++;
1132 configs.push_back(*out_suggested);
1133 *_aidl_return = true;
1134 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
1135 } else if (existing != configs.end() && requestedIsValid) {
1136 *existing = *out_suggested;
1137 *_aidl_return = true;
1138 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
1139 } else {
1140 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
1141 << "; requested is valid? " << requestedIsValid << ", fully specified? "
1142 << requestedIsFullySpecified;
1143 *_aidl_return = false;
1144 }
1145 return ndk::ScopedAStatus::ok();
1146}
1147
1148ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1149 auto& patches = getConfig().patches;
1150 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1151 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001152 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001153 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001154 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1155 mPatches = std::move(patchesBackup);
1156 return status;
1157 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001158 patches.erase(patchIt);
1159 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1160 return ndk::ScopedAStatus::ok();
1161 }
1162 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1163 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1164}
1165
1166ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1167 auto& configs = getConfig().portConfigs;
1168 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1169 if (configIt != configs.end()) {
1170 if (mStreams.count(in_portConfigId) != 0) {
1171 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1172 << " has a stream opened on it";
1173 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1174 }
1175 auto patchIt = mPatches.find(in_portConfigId);
1176 if (patchIt != mPatches.end()) {
1177 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1178 << " is used by the patch with id " << patchIt->second;
1179 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1180 }
1181 auto& initials = getConfig().initialConfigs;
1182 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1183 if (initialIt == initials.end()) {
1184 configs.erase(configIt);
1185 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1186 } else if (*configIt != *initialIt) {
1187 *configIt = *initialIt;
1188 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1189 }
1190 return ndk::ScopedAStatus::ok();
1191 }
1192 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1193 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1194}
1195
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001196ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1197 *_aidl_return = mMasterMute;
1198 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1199 return ndk::ScopedAStatus::ok();
1200}
1201
1202ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1203 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001204 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1205 : onMasterMuteChanged(in_mute);
1206 if (result.isOk()) {
1207 mMasterMute = in_mute;
1208 } else {
1209 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1210 << "), error=" << result;
1211 // Reset master mute if it failed.
1212 onMasterMuteChanged(mMasterMute);
1213 }
1214 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001215}
1216
1217ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1218 *_aidl_return = mMasterVolume;
1219 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1220 return ndk::ScopedAStatus::ok();
1221}
1222
1223ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1224 LOG(DEBUG) << __func__ << ": " << in_volume;
1225 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001226 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1227 : onMasterVolumeChanged(in_volume);
1228 if (result.isOk()) {
1229 mMasterVolume = in_volume;
1230 } else {
1231 // Reset master volume if it failed.
1232 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1233 << "), error=" << result;
1234 onMasterVolumeChanged(mMasterVolume);
1235 }
1236 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001237 }
1238 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1239 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1240}
1241
1242ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1243 *_aidl_return = mMicMute;
1244 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1245 return ndk::ScopedAStatus::ok();
1246}
1247
1248ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1249 LOG(DEBUG) << __func__ << ": " << in_mute;
1250 mMicMute = in_mute;
1251 return ndk::ScopedAStatus::ok();
1252}
1253
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001254ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001255 *_aidl_return = getMicrophoneInfos();
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001256 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1257 return ndk::ScopedAStatus::ok();
1258}
1259
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001260ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001261 if (!isValidAudioMode(in_mode)) {
1262 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1263 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1264 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001265 // No checks for supported audio modes here, it's an informative notification.
1266 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1267 return ndk::ScopedAStatus::ok();
1268}
1269
1270ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1271 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1272 return ndk::ScopedAStatus::ok();
1273}
1274
1275ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1276 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1277 return ndk::ScopedAStatus::ok();
1278}
1279
Vlad Popa83a6d822022-11-07 13:53:57 +01001280ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001281 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001282 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001283 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001284 *_aidl_return = mSoundDose.getInstance();
Vlad Popa943b7e22022-12-08 14:24:12 +01001285 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001286 return ndk::ScopedAStatus::ok();
1287}
1288
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001289ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1290 LOG(DEBUG) << __func__;
1291 (void)_aidl_return;
1292 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1293}
1294
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001295const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001296const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001297
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001298ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1299 std::vector<VendorParameter>* _aidl_return) {
1300 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001301 bool allParametersKnown = true;
1302 for (const auto& id : in_ids) {
1303 if (id == VendorDebug::kForceTransientBurstName) {
1304 VendorParameter forceTransientBurst{.id = id};
1305 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1306 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001307 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1308 VendorParameter forceSynchronousDrain{.id = id};
1309 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1310 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001311 } else {
1312 allParametersKnown = false;
1313 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1314 }
1315 }
1316 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1317 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001318}
1319
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001320namespace {
1321
1322template <typename W>
1323bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1324 std::optional<W> value;
1325 binder_status_t result = p.ext.getParcelable(&value);
1326 if (result == STATUS_OK && value.has_value()) {
1327 *v = value.value().value;
1328 return true;
1329 }
1330 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1331 << "\": " << result;
1332 return false;
1333}
1334
1335} // namespace
1336
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001337ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1338 bool in_async) {
1339 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1340 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001341 bool allParametersKnown = true;
1342 for (const auto& p : in_parameters) {
1343 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001344 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1345 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1346 }
1347 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1348 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001349 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1350 }
1351 } else {
1352 allParametersKnown = false;
1353 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1354 }
1355 }
1356 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1357 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001358}
1359
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001360ndk::ScopedAStatus Module::addDeviceEffect(
1361 int32_t in_portConfigId,
1362 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1363 if (in_effect == nullptr) {
1364 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1365 } else {
1366 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1367 << in_effect->asBinder().get();
1368 }
1369 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1370}
1371
1372ndk::ScopedAStatus Module::removeDeviceEffect(
1373 int32_t in_portConfigId,
1374 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1375 if (in_effect == nullptr) {
1376 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1377 } else {
1378 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1379 << in_effect->asBinder().get();
1380 }
1381 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1382}
1383
jiabin9a8e6862023-01-12 23:06:37 +00001384ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1385 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1386 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1387 std::set<int32_t> mmapSinks;
1388 std::set<int32_t> mmapSources;
1389 auto& ports = getConfig().ports;
1390 for (const auto& port : ports) {
1391 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1392 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1393 AudioInputFlags::MMAP_NOIRQ)) {
1394 mmapSinks.insert(port.id);
1395 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1396 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1397 AudioOutputFlags::MMAP_NOIRQ)) {
1398 mmapSources.insert(port.id);
1399 }
1400 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001401 if (mmapSources.empty() && mmapSinks.empty()) {
1402 AudioMMapPolicyInfo never;
1403 never.mmapPolicy = AudioMMapPolicy::NEVER;
1404 _aidl_return->push_back(never);
1405 return ndk::ScopedAStatus::ok();
1406 }
jiabin9a8e6862023-01-12 23:06:37 +00001407 for (const auto& route : getConfig().routes) {
1408 if (mmapSinks.count(route.sinkPortId) != 0) {
1409 // The sink is a mix port, add the sources if they are device ports.
1410 for (int sourcePortId : route.sourcePortIds) {
1411 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1412 if (sourcePortIt == ports.end()) {
1413 // This must not happen
1414 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1415 continue;
1416 }
1417 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1418 // The source is not a device port, skip
1419 continue;
1420 }
1421 AudioMMapPolicyInfo policyInfo;
1422 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1423 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1424 // default implementation.
1425 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1426 _aidl_return->push_back(policyInfo);
1427 }
1428 } else {
1429 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1430 if (sinkPortIt == ports.end()) {
1431 // This must not happen
1432 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1433 continue;
1434 }
1435 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1436 // The sink is not a device port, skip
1437 continue;
1438 }
1439 if (count_any(mmapSources, route.sourcePortIds)) {
1440 AudioMMapPolicyInfo policyInfo;
1441 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1442 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1443 // default implementation.
1444 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1445 _aidl_return->push_back(policyInfo);
1446 }
1447 }
1448 }
1449 return ndk::ScopedAStatus::ok();
1450}
1451
Eric Laurente2432ea2023-01-12 17:47:31 +01001452ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1453 LOG(DEBUG) << __func__;
1454 *_aidl_return = false;
1455 return ndk::ScopedAStatus::ok();
1456}
1457
jiabinb76981e2023-01-18 00:58:30 +00001458ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1459 if (!isMmapSupported()) {
1460 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1461 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1462 }
1463 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1464 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1465 return ndk::ScopedAStatus::ok();
1466}
1467
1468ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1469 if (!isMmapSupported()) {
1470 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1471 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1472 }
1473 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1474 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1475 return ndk::ScopedAStatus::ok();
1476}
1477
1478bool Module::isMmapSupported() {
1479 if (mIsMmapSupported.has_value()) {
1480 return mIsMmapSupported.value();
1481 }
1482 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1483 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1484 mIsMmapSupported = false;
1485 } else {
1486 mIsMmapSupported =
1487 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1488 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1489 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1490 }) != mmapPolicyInfos.end();
1491 }
1492 return mIsMmapSupported.value();
1493}
1494
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001495ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort) {
1496 if (audioPort->ext.getTag() != AudioPortExt::device) {
1497 LOG(ERROR) << __func__ << ": not a device port: " << audioPort->toString();
1498 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1499 }
1500 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1501 if (!devicePort.device.type.connection.empty()) {
1502 LOG(ERROR) << __func__
1503 << ": module implementation must override 'populateConnectedDevicePort' "
1504 << "to handle connection of external devices.";
1505 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1506 }
jiabin116d8392023-03-01 22:52:57 +00001507 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001508 return ndk::ScopedAStatus::ok();
1509}
1510
1511ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1512 const std::vector<AudioPortConfig*>& sources __unused,
1513 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001514 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001515 return ndk::ScopedAStatus::ok();
1516}
1517
jiabin783c48b2023-02-28 18:28:06 +00001518void Module::onExternalDeviceConnectionChanged(
1519 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1520 bool connected __unused) {
1521 LOG(DEBUG) << __func__ << ": do nothing and return";
1522}
1523
1524ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1525 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1526 return ndk::ScopedAStatus::ok();
1527}
1528
1529ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1530 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1531 return ndk::ScopedAStatus::ok();
1532}
1533
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001534std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1535 std::vector<MicrophoneInfo> result;
1536 Configuration& config = getConfig();
1537 for (const AudioPort& port : config.ports) {
1538 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1539 const AudioDeviceType deviceType =
1540 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1541 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1542 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1543 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1544 // accordingly based on their physical microphone parameters.
1545 result.push_back(MicrophoneInfo{
1546 .id = port.name,
1547 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1548 .group = 0,
1549 .indexInTheGroup = 0,
1550 });
1551 }
1552 }
1553 }
1554 return result;
1555}
1556
Ram Mohan18f0d512023-07-01 00:47:09 +05301557Module::BtProfileHandles Module::getBtProfileManagerHandles() {
1558 return std::make_tuple(std::weak_ptr<IBluetooth>(), std::weak_ptr<IBluetoothA2dp>(),
1559 std::weak_ptr<IBluetoothLe>());
1560}
1561
1562ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1563 return mStreams.bluetoothParametersUpdated();
1564}
1565
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001566} // namespace aidl::android::hardware::audio::core