blob: 10450099e1f42d2d29a76f3a4956f6928bf094ce [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 Naganov8651b362023-01-06 23:15:27 +0000245 asyncCallback, outEventCallback, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000246 if (temp.isValid()) {
247 *out_context = std::move(temp);
248 } else {
249 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
250 }
251 } else {
252 // TODO: Implement simulation of MMAP buffer allocation
253 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000254 return ndk::ScopedAStatus::ok();
255}
256
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000257std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
258 std::vector<AudioDevice> result;
259 auto& ports = getConfig().ports;
260 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
261 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
262 auto portIt = findById<AudioPort>(ports, *it);
263 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
264 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
265 }
266 }
267 return result;
268}
269
270std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
271 std::set<int32_t> result;
272 auto patchIdsRange = mPatches.equal_range(portConfigId);
273 auto& patches = getConfig().patches;
274 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
275 auto patchIt = findById<AudioPatch>(patches, it->second);
276 if (patchIt == patches.end()) {
277 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
278 << "not found in the configuration";
279 }
280 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
281 portConfigId) != patchIt->sourcePortConfigIds.end()) {
282 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
283 } else {
284 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
285 }
286 }
287 return result;
288}
289
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000290ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
291 auto& configs = getConfig().portConfigs;
292 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
293 if (portConfigIt == configs.end()) {
294 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
295 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
296 }
297 const int32_t portId = portConfigIt->portId;
298 // In our implementation, configs of mix ports always have unique IDs.
299 CHECK(portId != in_portConfigId);
300 auto& ports = getConfig().ports;
301 auto portIt = findById<AudioPort>(ports, portId);
302 if (portIt == ports.end()) {
303 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
304 << in_portConfigId << " not found";
305 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
306 }
307 if (mStreams.count(in_portConfigId) != 0) {
308 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
309 << " already has a stream opened on it";
310 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
311 }
312 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
313 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
314 << " does not correspond to a mix port";
315 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
316 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700317 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000318 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
319 LOG(ERROR) << __func__ << ": port id " << portId
320 << " has already reached maximum allowed opened stream count: "
321 << maxOpenStreamCount;
322 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
323 }
324 *port = &(*portIt);
325 return ndk::ScopedAStatus::ok();
326}
327
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000328void Module::populateConnectedProfiles() {
329 Configuration& config = getConfig();
330 for (const AudioPort& port : config.ports) {
331 if (port.ext.getTag() == AudioPortExt::device) {
332 if (auto devicePort = port.ext.get<AudioPortExt::device>();
333 !devicePort.device.type.connection.empty() && port.profiles.empty()) {
334 if (auto connIt = config.connectedProfiles.find(port.id);
335 connIt == config.connectedProfiles.end()) {
336 config.connectedProfiles.emplace(
337 port.id, internal::getStandard16And24BitPcmAudioProfiles());
338 }
339 }
340 }
341 }
342}
343
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000344template <typename C>
345std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
346 std::set<int32_t> result;
347 auto& portConfigs = getConfig().portConfigs;
348 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
349 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
350 if (portConfigIt != portConfigs.end()) {
351 result.insert(portConfigIt->portId);
352 }
353 }
354 return result;
355}
356
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000357std::unique_ptr<Module::Configuration> Module::initializeConfig() {
358 return internal::getConfiguration(getType());
Peter Yoon918a6a52023-07-13 17:04:37 +0900359}
360
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700361std::vector<AudioRoute*> Module::getAudioRoutesForAudioPortImpl(int32_t portId) {
362 std::vector<AudioRoute*> result;
363 auto& routes = getConfig().routes;
364 for (auto& r : routes) {
365 const auto& srcs = r.sourcePortIds;
366 if (r.sinkPortId == portId || std::find(srcs.begin(), srcs.end(), portId) != srcs.end()) {
367 result.push_back(&r);
368 }
369 }
370 return result;
371}
372
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000373Module::Configuration& Module::getConfig() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000374 if (!mConfig) {
Peter Yoon918a6a52023-07-13 17:04:37 +0900375 mConfig = std::move(initializeConfig());
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000376 }
377 return *mConfig;
378}
379
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700380std::set<int32_t> Module::getRoutableAudioPortIds(int32_t portId,
381 std::vector<AudioRoute*>* routes) {
382 std::vector<AudioRoute*> routesStorage;
383 if (routes == nullptr) {
384 routesStorage = getAudioRoutesForAudioPortImpl(portId);
385 routes = &routesStorage;
386 }
387 std::set<int32_t> result;
388 for (AudioRoute* r : *routes) {
389 if (r->sinkPortId == portId) {
390 result.insert(r->sourcePortIds.begin(), r->sourcePortIds.end());
391 } else {
392 result.insert(r->sinkPortId);
393 }
394 }
395 return result;
396}
397
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000398void Module::registerPatch(const AudioPatch& patch) {
399 auto& configs = getConfig().portConfigs;
400 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
401 for (auto portConfigId : portConfigIds) {
402 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
403 if (configIt != configs.end()) {
404 mPatches.insert(std::pair{portConfigId, patch.id});
405 if (configIt->portId != portConfigId) {
406 mPatches.insert(std::pair{configIt->portId, patch.id});
407 }
408 }
409 };
410 };
411 do_insert(patch.sourcePortConfigIds);
412 do_insert(patch.sinkPortConfigIds);
413}
414
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700415ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
416 const AudioPatch& newPatch) {
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700417 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700418 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700419 using Connections =
420 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
421 Connections oldConnections, newConnections;
422 auto fillConnectionsHelper = [&](Connections& connections,
423 const std::vector<int32_t>& mixPortCfgIds,
424 const std::vector<int32_t>& devicePortCfgIds) {
425 for (int32_t mixPortCfgId : mixPortCfgIds) {
426 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
427 }
428 };
429 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
430 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
431 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
432 patch.sourcePortConfigIds.end()) {
433 // Sources are mix ports.
434 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
435 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
436 [&](int32_t portConfigId) {
437 return mStreams.count(portConfigId) > 0;
438 }) != patch.sinkPortConfigIds.end()) {
439 // Sources are device ports.
440 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
441 } // Otherwise, there are no streams to notify.
442 };
443 fillConnections(oldConnections, oldPatch);
444 fillConnections(newConnections, newPatch);
445
446 std::for_each(oldConnections.begin(), oldConnections.end(), [&](const auto& connectionPair) {
447 const int32_t mixPortConfigId = connectionPair.first;
448 if (auto it = newConnections.find(mixPortConfigId);
449 it == newConnections.end() || it->second != connectionPair.second) {
450 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, {});
451 status.isOk()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700452 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700453 << mixPortConfigId << " has been disconnected";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700454 } else {
455 // Disconnection is tricky to roll back, just register a failure.
456 maybeFailure = std::move(status);
457 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000458 }
459 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700460 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700461 std::set<int32_t> idsToDisconnectOnFailure;
462 std::for_each(newConnections.begin(), newConnections.end(), [&](const auto& connectionPair) {
463 const int32_t mixPortConfigId = connectionPair.first;
464 if (auto it = oldConnections.find(mixPortConfigId);
465 it == oldConnections.end() || it->second != connectionPair.second) {
466 const auto connectedDevices = findConnectedDevices(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700467 if (connectedDevices.empty()) {
468 // This is important as workers use the vector size to derive the connection status.
469 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
470 "config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700471 << mixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700472 }
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700473 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700474 status.isOk()) {
475 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700476 << mixPortConfigId << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700477 << ::android::internal::ToString(connectedDevices);
478 } else {
479 maybeFailure = std::move(status);
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700480 idsToDisconnectOnFailure.insert(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700481 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000482 }
483 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700484 if (!maybeFailure.isOk()) {
485 LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids "
486 << ::android::internal::ToString(idsToDisconnectOnFailure);
487 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
488 [&](const auto& portConfigId) {
489 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
490 (void)status.isOk(); // Can't do much about a failure here.
491 });
492 return maybeFailure;
493 }
494 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000495}
496
Mikhail Naganov00603d12022-05-02 22:52:13 +0000497ndk::ScopedAStatus Module::setModuleDebug(
498 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700499 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000500 << ", new flags: " << in_debug.toString();
501 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
502 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700503 LOG(ERROR) << __func__ << ": " << mType
504 << ": attempting to change device connections simulation while having external "
505 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000506 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
507 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000508 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700509 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000510 << in_debug.streamTransientStateDelayMs;
511 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
512 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000513 mDebug = in_debug;
514 return ndk::ScopedAStatus::ok();
515}
516
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000517ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700518 *_aidl_return = nullptr;
519 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000520 return ndk::ScopedAStatus::ok();
521}
522
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000523ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700524 *_aidl_return = nullptr;
525 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000526 return ndk::ScopedAStatus::ok();
527}
528
Mikhail Naganov7499a002023-02-27 18:51:44 -0800529ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700530 *_aidl_return = nullptr;
531 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800532 return ndk::ScopedAStatus::ok();
533}
534
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800535ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700536 *_aidl_return = nullptr;
537 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800538 return ndk::ScopedAStatus::ok();
539}
540
Mikhail Naganov00603d12022-05-02 22:52:13 +0000541ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
542 AudioPort* _aidl_return) {
543 const int32_t templateId = in_templateIdAndAdditionalData.id;
544 auto& ports = getConfig().ports;
545 AudioPort connectedPort;
546 { // Scope the template port so that we don't accidentally modify it.
547 auto templateIt = findById<AudioPort>(ports, templateId);
548 if (templateIt == ports.end()) {
549 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
550 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
551 }
552 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
553 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
554 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
555 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000556 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
557 if (templateDevicePort.device.type.connection.empty()) {
558 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
559 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
560 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700561 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
562 LOG(ERROR) << __func__ << ": port id " << templateId << " is a connected device port";
563 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
564 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000565 // Postpone id allocation until we ensure that there are no client errors.
566 connectedPort = *templateIt;
567 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
568 const auto& inputDevicePort =
569 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
570 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
571 connectedDevicePort.device.address = inputDevicePort.device.address;
572 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
573 << connectedDevicePort.device.toString();
574 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700575 for (auto connectedPortPair : mConnectedDevicePorts) {
576 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000577 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
578 connectedDevicePort.device) {
579 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700580 << " is already connected at the device port id "
581 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000582 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
583 }
584 }
585 }
586
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700587 // Two main cases are considered with regard to the profiles of the connected device port:
588 //
589 // 1. If the template device port has dynamic profiles, and at least one routable mix
590 // port also has dynamic profiles, it means that after connecting the device, the
591 // connected device port must have profiles populated with actual capabilities of
592 // the connected device, and dynamic of routable mix ports will be filled
593 // according to these capabilities. An example of this case is connection of an
594 // HDMI or USB device. For USB handled by ADSP, there can be mix ports with static
595 // profiles, and one dedicated mix port for "hi-fi" playback. The latter is left with
596 // dynamic profiles so that they can be populated with actual capabilities of
597 // the connected device.
598 //
599 // 2. If the template device port has dynamic profiles, while all routable mix ports
600 // have static profiles, it means that after connecting the device, the connected
601 // device port can be left with dynamic profiles, and profiles of mix ports are
602 // left untouched. An example of this case is connection of an analog wired
603 // headset, it should be treated in the same way as a speaker.
604 //
605 // Yet another possible case is when both the template device port and all routable
606 // mix ports have static profiles. This is allowed and handled correctly, however, it
607 // is not very practical, since these profiles are likely duplicates of each other.
608
609 std::vector<AudioRoute*> routesToMixPorts = getAudioRoutesForAudioPortImpl(templateId);
610 std::set<int32_t> routableMixPortIds = getRoutableAudioPortIds(templateId, &routesToMixPorts);
611 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700612 if (!mDebug.simulateDeviceConnections) {
613 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
614 } else {
615 auto& connectedProfiles = getConfig().connectedProfiles;
616 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
617 connectedProfilesIt != connectedProfiles.end()) {
618 connectedPort.profiles = connectedProfilesIt->second;
619 }
620 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700621 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
622 // Possible case 2. Check if all routable mix ports have static profiles.
623 if (auto dynamicMixPortIt = std::find_if(ports.begin(), ports.end(),
624 [&routableMixPortIds](const auto& p) {
625 return routableMixPortIds.count(p.id) >
626 0 &&
627 hasDynamicProfilesOnly(p.profiles);
628 });
629 dynamicMixPortIt != ports.end()) {
630 LOG(ERROR) << __func__
631 << ": connected port only has dynamic profiles after connecting "
632 << "external device " << connectedPort.toString() << ", and there exist "
633 << "a routable mix port with dynamic profiles: "
634 << dynamicMixPortIt->toString();
635 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
636 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530637 }
638 }
639
Mikhail Naganovdc417732023-09-29 15:49:35 -0700640 connectedPort.id = getConfig().nextPortId++;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700641 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700642 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000643 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
644 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000645 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000646 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000647
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700648 // For routes where the template port is a source, add the connected port to sources,
649 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000650 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700651 for (AudioRoute* r : routesToMixPorts) {
652 if (r->sinkPortId == templateId) {
653 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
654 .sinkPortId = connectedPort.id,
655 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000656 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700657 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000658 }
659 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700660 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000661 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
662
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700663 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
664 // Note: this is a simplistic approach assuming that a mix port can only be populated
665 // from a single device port. Implementing support for stuffing dynamic profiles with
666 // a superset of all profiles from all routable dynamic device ports would be more involved.
667 for (auto& port : ports) {
668 if (routableMixPortIds.count(port.id) == 0) continue;
669 if (hasDynamicProfilesOnly(port.profiles)) {
670 port.profiles = connectedPort.profiles;
671 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700672 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700673 // Check if profiles are not all dynamic because they were populated by
674 // a previous connection. Otherwise, it means that they are actually static.
675 for (const auto& cp : mConnectedDevicePorts) {
676 if (cp.second.count(port.id) > 0) {
677 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700678 break;
679 }
680 }
681 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700682 }
683 }
684 *_aidl_return = std::move(connectedPort);
685
Mikhail Naganov00603d12022-05-02 22:52:13 +0000686 return ndk::ScopedAStatus::ok();
687}
688
689ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
690 auto& ports = getConfig().ports;
691 auto portIt = findById<AudioPort>(ports, in_portId);
692 if (portIt == ports.end()) {
693 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
694 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
695 }
696 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
697 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
698 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
699 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700700 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
701 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000702 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
703 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
704 }
705 auto& configs = getConfig().portConfigs;
706 auto& initials = getConfig().initialConfigs;
707 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
708 if (config.portId == in_portId) {
709 // Check if the configuration was provided by the client.
710 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
711 return initialIt == initials.end() || config != *initialIt;
712 }
713 return false;
714 });
715 if (configIt != configs.end()) {
716 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
717 << configIt->id;
718 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
719 }
jiabin783c48b2023-02-28 18:28:06 +0000720 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000721 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000722 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
723
724 auto& routes = getConfig().routes;
725 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
726 if (routesIt->sinkPortId == in_portId) {
727 routesIt = routes.erase(routesIt);
728 } else {
729 // Note: the list of sourcePortIds can't become empty because there must
730 // be the id of the template port in the route.
731 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
732 ++routesIt;
733 }
734 }
735
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700736 // Clear profiles for mix ports that are not connected to any other ports.
737 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
738 mConnectedDevicePorts.erase(connectedPortsIt);
739 for (const auto& connectedPort : mConnectedDevicePorts) {
740 for (int32_t mixPortId : connectedPort.second) {
741 mixPortsToClear.erase(mixPortId);
742 }
743 }
744 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700745 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
746 if (mixPortIt != ports.end()) {
747 mixPortIt->profiles = {};
748 }
749 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700750
Mikhail Naganov00603d12022-05-02 22:52:13 +0000751 return ndk::ScopedAStatus::ok();
752}
753
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000754ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
755 *_aidl_return = getConfig().patches;
756 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
757 return ndk::ScopedAStatus::ok();
758}
759
760ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
761 auto& ports = getConfig().ports;
762 auto portIt = findById<AudioPort>(ports, in_portId);
763 if (portIt != ports.end()) {
764 *_aidl_return = *portIt;
765 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
766 return ndk::ScopedAStatus::ok();
767 }
768 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
769 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
770}
771
772ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
773 *_aidl_return = getConfig().portConfigs;
774 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
775 return ndk::ScopedAStatus::ok();
776}
777
778ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
779 *_aidl_return = getConfig().ports;
780 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
781 return ndk::ScopedAStatus::ok();
782}
783
784ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
785 *_aidl_return = getConfig().routes;
786 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
787 return ndk::ScopedAStatus::ok();
788}
789
Mikhail Naganov00603d12022-05-02 22:52:13 +0000790ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
791 std::vector<AudioRoute>* _aidl_return) {
792 auto& ports = getConfig().ports;
793 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
794 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
795 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
796 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700797 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
798 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
799 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000800 return ndk::ScopedAStatus::ok();
801}
802
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000803ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
804 OpenInputStreamReturn* _aidl_return) {
805 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
806 << in_args.bufferSizeFrames << " frames";
807 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700808 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000809 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
810 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000811 << " does not correspond to an input mix port";
812 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
813 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000814 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700815 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
816 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000817 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000818 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700819 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000820 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000821 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700822 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
823 RETURN_STATUS_IF_ERROR(
824 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
825 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000826 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
827 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000828 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000829 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000830 return ndk::ScopedAStatus::ok();
831}
832
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000833ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
834 OpenOutputStreamReturn* _aidl_return) {
835 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
836 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
837 << " frames";
838 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700839 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000840 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
841 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000842 << " does not correspond to an output mix port";
843 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
844 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000845 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
846 AudioOutputFlags::COMPRESS_OFFLOAD);
847 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000848 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000849 << " has COMPRESS_OFFLOAD flag set, requires offload info";
850 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
851 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000852 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
853 AudioOutputFlags::NON_BLOCKING);
854 if (isNonBlocking && in_args.callback == nullptr) {
855 LOG(ERROR) << __func__ << ": port id " << port->id
856 << " has NON_BLOCKING flag set, requires async callback";
857 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
858 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000859 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700860 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
861 isNonBlocking ? in_args.callback : nullptr,
862 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000863 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000864 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700865 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700866 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000867 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700868 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
869 RETURN_STATUS_IF_ERROR(
870 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
871 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000872 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
873 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000874 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000875 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000876 return ndk::ScopedAStatus::ok();
877}
878
Mikhail Naganov74927202022-12-19 16:37:14 +0000879ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
880 SupportedPlaybackRateFactors* _aidl_return) {
881 LOG(DEBUG) << __func__;
882 (void)_aidl_return;
883 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
884}
885
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000886ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000887 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000888 if (in_requested.sourcePortConfigIds.empty()) {
889 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
890 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
891 }
892 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
893 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
894 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
895 }
896 if (in_requested.sinkPortConfigIds.empty()) {
897 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
898 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
899 }
900 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
901 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
902 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
903 }
904
905 auto& configs = getConfig().portConfigs;
906 std::vector<int32_t> missingIds;
907 auto sources =
908 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
909 if (!missingIds.empty()) {
910 LOG(ERROR) << __func__ << ": following source port config ids not found: "
911 << ::android::internal::ToString(missingIds);
912 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
913 }
914 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
915 if (!missingIds.empty()) {
916 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
917 << ::android::internal::ToString(missingIds);
918 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
919 }
920 // bool indicates whether a non-exclusive route is available.
921 // If only an exclusive route is available, that means the patch can not be
922 // established if there is any other patch which currently uses the sink port.
923 std::map<int32_t, bool> allowedSinkPorts;
924 auto& routes = getConfig().routes;
925 for (auto src : sources) {
926 for (const auto& r : routes) {
927 const auto& srcs = r.sourcePortIds;
928 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
929 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
930 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
931 }
932 }
933 }
934 }
935 for (auto sink : sinks) {
936 if (allowedSinkPorts.count(sink->portId) == 0) {
937 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
938 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
939 }
940 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700941 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000942
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000943 auto& patches = getConfig().patches;
944 auto existing = patches.end();
945 std::optional<decltype(mPatches)> patchesBackup;
946 if (in_requested.id != 0) {
947 existing = findById<AudioPatch>(patches, in_requested.id);
948 if (existing != patches.end()) {
949 patchesBackup = mPatches;
950 cleanUpPatch(existing->id);
951 } else {
952 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
953 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
954 }
955 }
956 // Validate the requested patch.
957 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
958 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
959 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
960 << "is exclusive and is already used by some other patch";
961 if (patchesBackup.has_value()) {
962 mPatches = std::move(*patchesBackup);
963 }
964 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
965 }
966 }
967 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000968 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
969 _aidl_return->latenciesMs.clear();
970 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
971 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000972 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000973 if (existing == patches.end()) {
974 _aidl_return->id = getConfig().nextPatchId++;
975 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000976 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000977 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -0700978 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000979 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700980 patchesBackup = mPatches;
981 registerPatch(*_aidl_return);
982 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
983 mPatches = std::move(*patchesBackup);
984 if (existing == patches.end()) {
985 patches.pop_back();
986 } else {
987 *existing = oldPatch;
988 }
989 return status;
990 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000991
992 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
993 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000994 return ndk::ScopedAStatus::ok();
995}
996
997ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
998 AudioPortConfig* out_suggested, bool* _aidl_return) {
999 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
1000 auto& configs = getConfig().portConfigs;
1001 auto existing = configs.end();
1002 if (in_requested.id != 0) {
1003 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1004 existing == configs.end()) {
1005 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
1006 << " not found";
1007 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1008 }
1009 }
1010
1011 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1012 if (portId == 0) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001013 LOG(ERROR) << __func__ << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001014 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1015 }
1016 auto& ports = getConfig().ports;
1017 auto portIt = findById<AudioPort>(ports, portId);
1018 if (portIt == ports.end()) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001019 LOG(ERROR) << __func__ << ": requested port config points to non-existent portId "
1020 << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001021 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1022 }
1023 if (existing != configs.end()) {
1024 *out_suggested = *existing;
1025 } else {
1026 AudioPortConfig newConfig;
1027 if (generateDefaultPortConfig(*portIt, &newConfig)) {
1028 *out_suggested = newConfig;
1029 } else {
1030 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
1031 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1032 }
1033 }
1034 // From this moment, 'out_suggested' is either an existing port config,
1035 // or a new generated config. Now attempt to update it according to the specified
1036 // fields of 'in_requested'.
1037
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001038 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1039 // which takes care of their actual configuration automatically.
1040 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1041 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001042 bool requestedIsValid = true, requestedIsFullySpecified = true;
1043
1044 AudioIoFlags portFlags = portIt->flags;
1045 if (in_requested.flags.has_value()) {
1046 if (in_requested.flags.value() != portFlags) {
1047 LOG(WARNING) << __func__ << ": requested flags "
1048 << in_requested.flags.value().toString() << " do not match port's "
1049 << portId << " flags " << portFlags.toString();
1050 requestedIsValid = false;
1051 }
1052 } else {
1053 requestedIsFullySpecified = false;
1054 }
1055
1056 AudioProfile portProfile;
1057 if (in_requested.format.has_value()) {
1058 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001059 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1060 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001061 out_suggested->format = format;
1062 } else {
1063 LOG(WARNING) << __func__ << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001064 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001065 requestedIsValid = false;
1066 }
1067 } else {
1068 requestedIsFullySpecified = false;
1069 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001070 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1071 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001072 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
1073 << out_suggested->format.value().toString() << " anymore";
1074 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1075 }
1076
1077 if (in_requested.channelMask.has_value()) {
1078 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001079 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1080 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1081 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001082 out_suggested->channelMask = channelMask;
1083 } else {
1084 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
1085 << " is not supported for the format " << portProfile.format.toString()
1086 << " by the port " << portId;
1087 requestedIsValid = false;
1088 }
1089 } else {
1090 requestedIsFullySpecified = false;
1091 }
1092
1093 if (in_requested.sampleRate.has_value()) {
1094 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001095 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1096 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001097 sampleRate.value) != portProfile.sampleRates.end()) {
1098 out_suggested->sampleRate = sampleRate;
1099 } else {
1100 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
1101 << " is not supported for the format " << portProfile.format.toString()
1102 << " by the port " << portId;
1103 requestedIsValid = false;
1104 }
1105 } else {
1106 requestedIsFullySpecified = false;
1107 }
1108
1109 if (in_requested.gain.has_value()) {
1110 // Let's pretend that gain can always be applied.
1111 out_suggested->gain = in_requested.gain.value();
1112 }
1113
Mikhail Naganov248e9502023-02-21 16:32:40 -08001114 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1115 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1116 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
1117 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
1118 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
1119 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
1120 }
1121 } else {
1122 LOG(WARNING) << __func__ << ": requested ext tag "
1123 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1124 << toString(out_suggested->ext.getTag());
1125 requestedIsValid = false;
1126 }
1127 }
1128
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001129 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1130 out_suggested->id = getConfig().nextPortId++;
1131 configs.push_back(*out_suggested);
1132 *_aidl_return = true;
1133 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
1134 } else if (existing != configs.end() && requestedIsValid) {
1135 *existing = *out_suggested;
1136 *_aidl_return = true;
1137 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
1138 } else {
1139 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
1140 << "; requested is valid? " << requestedIsValid << ", fully specified? "
1141 << requestedIsFullySpecified;
1142 *_aidl_return = false;
1143 }
1144 return ndk::ScopedAStatus::ok();
1145}
1146
1147ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1148 auto& patches = getConfig().patches;
1149 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1150 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001151 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001152 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001153 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1154 mPatches = std::move(patchesBackup);
1155 return status;
1156 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001157 patches.erase(patchIt);
1158 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1159 return ndk::ScopedAStatus::ok();
1160 }
1161 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1162 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1163}
1164
1165ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1166 auto& configs = getConfig().portConfigs;
1167 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1168 if (configIt != configs.end()) {
1169 if (mStreams.count(in_portConfigId) != 0) {
1170 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1171 << " has a stream opened on it";
1172 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1173 }
1174 auto patchIt = mPatches.find(in_portConfigId);
1175 if (patchIt != mPatches.end()) {
1176 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1177 << " is used by the patch with id " << patchIt->second;
1178 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1179 }
1180 auto& initials = getConfig().initialConfigs;
1181 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1182 if (initialIt == initials.end()) {
1183 configs.erase(configIt);
1184 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1185 } else if (*configIt != *initialIt) {
1186 *configIt = *initialIt;
1187 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1188 }
1189 return ndk::ScopedAStatus::ok();
1190 }
1191 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1192 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1193}
1194
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001195ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1196 *_aidl_return = mMasterMute;
1197 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1198 return ndk::ScopedAStatus::ok();
1199}
1200
1201ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1202 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001203 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1204 : onMasterMuteChanged(in_mute);
1205 if (result.isOk()) {
1206 mMasterMute = in_mute;
1207 } else {
1208 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1209 << "), error=" << result;
1210 // Reset master mute if it failed.
1211 onMasterMuteChanged(mMasterMute);
1212 }
1213 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001214}
1215
1216ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1217 *_aidl_return = mMasterVolume;
1218 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1219 return ndk::ScopedAStatus::ok();
1220}
1221
1222ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1223 LOG(DEBUG) << __func__ << ": " << in_volume;
1224 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001225 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1226 : onMasterVolumeChanged(in_volume);
1227 if (result.isOk()) {
1228 mMasterVolume = in_volume;
1229 } else {
1230 // Reset master volume if it failed.
1231 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1232 << "), error=" << result;
1233 onMasterVolumeChanged(mMasterVolume);
1234 }
1235 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001236 }
1237 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1238 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1239}
1240
1241ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1242 *_aidl_return = mMicMute;
1243 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1244 return ndk::ScopedAStatus::ok();
1245}
1246
1247ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1248 LOG(DEBUG) << __func__ << ": " << in_mute;
1249 mMicMute = in_mute;
1250 return ndk::ScopedAStatus::ok();
1251}
1252
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001253ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001254 *_aidl_return = getMicrophoneInfos();
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001255 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1256 return ndk::ScopedAStatus::ok();
1257}
1258
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001259ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001260 if (!isValidAudioMode(in_mode)) {
1261 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1262 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1263 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001264 // No checks for supported audio modes here, it's an informative notification.
1265 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1266 return ndk::ScopedAStatus::ok();
1267}
1268
1269ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1270 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1271 return ndk::ScopedAStatus::ok();
1272}
1273
1274ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1275 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1276 return ndk::ScopedAStatus::ok();
1277}
1278
Vlad Popa83a6d822022-11-07 13:53:57 +01001279ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001280 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001281 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001282 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001283 *_aidl_return = mSoundDose.getInstance();
Vlad Popa943b7e22022-12-08 14:24:12 +01001284 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001285 return ndk::ScopedAStatus::ok();
1286}
1287
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001288ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1289 LOG(DEBUG) << __func__;
1290 (void)_aidl_return;
1291 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1292}
1293
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001294const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001295const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001296
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001297ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1298 std::vector<VendorParameter>* _aidl_return) {
1299 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001300 bool allParametersKnown = true;
1301 for (const auto& id : in_ids) {
1302 if (id == VendorDebug::kForceTransientBurstName) {
1303 VendorParameter forceTransientBurst{.id = id};
1304 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1305 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001306 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1307 VendorParameter forceSynchronousDrain{.id = id};
1308 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1309 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001310 } else {
1311 allParametersKnown = false;
1312 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1313 }
1314 }
1315 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1316 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001317}
1318
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001319namespace {
1320
1321template <typename W>
1322bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1323 std::optional<W> value;
1324 binder_status_t result = p.ext.getParcelable(&value);
1325 if (result == STATUS_OK && value.has_value()) {
1326 *v = value.value().value;
1327 return true;
1328 }
1329 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1330 << "\": " << result;
1331 return false;
1332}
1333
1334} // namespace
1335
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001336ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1337 bool in_async) {
1338 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1339 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001340 bool allParametersKnown = true;
1341 for (const auto& p : in_parameters) {
1342 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001343 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1344 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1345 }
1346 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1347 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001348 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1349 }
1350 } else {
1351 allParametersKnown = false;
1352 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1353 }
1354 }
1355 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1356 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001357}
1358
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001359ndk::ScopedAStatus Module::addDeviceEffect(
1360 int32_t in_portConfigId,
1361 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1362 if (in_effect == nullptr) {
1363 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1364 } else {
1365 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1366 << in_effect->asBinder().get();
1367 }
1368 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1369}
1370
1371ndk::ScopedAStatus Module::removeDeviceEffect(
1372 int32_t in_portConfigId,
1373 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1374 if (in_effect == nullptr) {
1375 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1376 } else {
1377 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1378 << in_effect->asBinder().get();
1379 }
1380 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1381}
1382
jiabin9a8e6862023-01-12 23:06:37 +00001383ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1384 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1385 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1386 std::set<int32_t> mmapSinks;
1387 std::set<int32_t> mmapSources;
1388 auto& ports = getConfig().ports;
1389 for (const auto& port : ports) {
1390 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1391 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1392 AudioInputFlags::MMAP_NOIRQ)) {
1393 mmapSinks.insert(port.id);
1394 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1395 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1396 AudioOutputFlags::MMAP_NOIRQ)) {
1397 mmapSources.insert(port.id);
1398 }
1399 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001400 if (mmapSources.empty() && mmapSinks.empty()) {
1401 AudioMMapPolicyInfo never;
1402 never.mmapPolicy = AudioMMapPolicy::NEVER;
1403 _aidl_return->push_back(never);
1404 return ndk::ScopedAStatus::ok();
1405 }
jiabin9a8e6862023-01-12 23:06:37 +00001406 for (const auto& route : getConfig().routes) {
1407 if (mmapSinks.count(route.sinkPortId) != 0) {
1408 // The sink is a mix port, add the sources if they are device ports.
1409 for (int sourcePortId : route.sourcePortIds) {
1410 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1411 if (sourcePortIt == ports.end()) {
1412 // This must not happen
1413 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1414 continue;
1415 }
1416 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1417 // The source is not a device port, skip
1418 continue;
1419 }
1420 AudioMMapPolicyInfo policyInfo;
1421 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1422 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1423 // default implementation.
1424 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1425 _aidl_return->push_back(policyInfo);
1426 }
1427 } else {
1428 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1429 if (sinkPortIt == ports.end()) {
1430 // This must not happen
1431 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1432 continue;
1433 }
1434 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1435 // The sink is not a device port, skip
1436 continue;
1437 }
1438 if (count_any(mmapSources, route.sourcePortIds)) {
1439 AudioMMapPolicyInfo policyInfo;
1440 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1441 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1442 // default implementation.
1443 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1444 _aidl_return->push_back(policyInfo);
1445 }
1446 }
1447 }
1448 return ndk::ScopedAStatus::ok();
1449}
1450
Eric Laurente2432ea2023-01-12 17:47:31 +01001451ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1452 LOG(DEBUG) << __func__;
1453 *_aidl_return = false;
1454 return ndk::ScopedAStatus::ok();
1455}
1456
jiabinb76981e2023-01-18 00:58:30 +00001457ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1458 if (!isMmapSupported()) {
1459 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1460 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1461 }
1462 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1463 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1464 return ndk::ScopedAStatus::ok();
1465}
1466
1467ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1468 if (!isMmapSupported()) {
1469 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1470 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1471 }
1472 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1473 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1474 return ndk::ScopedAStatus::ok();
1475}
1476
1477bool Module::isMmapSupported() {
1478 if (mIsMmapSupported.has_value()) {
1479 return mIsMmapSupported.value();
1480 }
1481 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1482 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1483 mIsMmapSupported = false;
1484 } else {
1485 mIsMmapSupported =
1486 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1487 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1488 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1489 }) != mmapPolicyInfos.end();
1490 }
1491 return mIsMmapSupported.value();
1492}
1493
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001494ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort) {
1495 if (audioPort->ext.getTag() != AudioPortExt::device) {
1496 LOG(ERROR) << __func__ << ": not a device port: " << audioPort->toString();
1497 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1498 }
1499 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1500 if (!devicePort.device.type.connection.empty()) {
1501 LOG(ERROR) << __func__
1502 << ": module implementation must override 'populateConnectedDevicePort' "
1503 << "to handle connection of external devices.";
1504 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1505 }
jiabin116d8392023-03-01 22:52:57 +00001506 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001507 return ndk::ScopedAStatus::ok();
1508}
1509
1510ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1511 const std::vector<AudioPortConfig*>& sources __unused,
1512 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001513 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001514 return ndk::ScopedAStatus::ok();
1515}
1516
jiabin783c48b2023-02-28 18:28:06 +00001517void Module::onExternalDeviceConnectionChanged(
1518 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1519 bool connected __unused) {
1520 LOG(DEBUG) << __func__ << ": do nothing and return";
1521}
1522
1523ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1524 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1525 return ndk::ScopedAStatus::ok();
1526}
1527
1528ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1529 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1530 return ndk::ScopedAStatus::ok();
1531}
1532
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001533std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1534 std::vector<MicrophoneInfo> result;
1535 Configuration& config = getConfig();
1536 for (const AudioPort& port : config.ports) {
1537 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1538 const AudioDeviceType deviceType =
1539 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1540 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1541 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1542 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1543 // accordingly based on their physical microphone parameters.
1544 result.push_back(MicrophoneInfo{
1545 .id = port.name,
1546 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1547 .group = 0,
1548 .indexInTheGroup = 0,
1549 });
1550 }
1551 }
1552 }
1553 return result;
1554}
1555
Ram Mohan18f0d512023-07-01 00:47:09 +05301556Module::BtProfileHandles Module::getBtProfileManagerHandles() {
1557 return std::make_tuple(std::weak_ptr<IBluetooth>(), std::weak_ptr<IBluetoothA2dp>(),
1558 std::weak_ptr<IBluetoothLe>());
1559}
1560
1561ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1562 return mStreams.bluetoothParametersUpdated();
1563}
1564
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001565} // namespace aidl::android::hardware::audio::core