blob: 89e3d9915dbc44a15633469bd5b9cd5f502f2a81 [file] [log] [blame]
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <algorithm>
18#include <set>
19
20#define LOG_TAG "AHAL_Module"
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000021#include <aidl/android/media/audio/common/AudioInputFlags.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000022#include <aidl/android/media/audio/common/AudioOutputFlags.h>
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -070023#include <android-base/logging.h>
24#include <android/binder_ibinder_platform.h>
25#include <error/expected_utils.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000026
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000027#include "core-impl/Configuration.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000028#include "core-impl/Module.h"
Mikhail Naganovb03b5c42023-07-26 13:13:35 -070029#include "core-impl/ModuleBluetooth.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070030#include "core-impl/ModulePrimary.h"
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053031#include "core-impl/ModuleRemoteSubmix.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070032#include "core-impl/ModuleStub.h"
jiabin253bd322023-01-25 23:57:31 +000033#include "core-impl/ModuleUsb.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010034#include "core-impl/SoundDose.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000035#include "core-impl/utils.h"
36
Mikhail Naganov55045b52023-10-24 17:03:50 -070037using aidl::android::hardware::audio::common::frameCountFromDurationMs;
Mikhail Naganov872d4a62023-03-09 18:19:01 -080038using aidl::android::hardware::audio::common::getFrameSizeInBytes;
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 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000205 auto& configs = getConfig().portConfigs;
206 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000207 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000208 // validity of the portConfigId has already been checked.
Mikhail Naganov13501872023-10-18 16:15:46 -0700209 const int32_t minimumStreamBufferSizeFrames = calculateBufferSizeFrames(
210 getNominalLatencyMs(*portConfigIt), portConfigIt->sampleRate.value().value);
211 if (in_bufferSizeFrames < minimumStreamBufferSizeFrames) {
212 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
213 << ", must be at least " << minimumStreamBufferSizeFrames;
214 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
215 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000216 const size_t frameSize =
217 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
218 if (frameSize == 0) {
219 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
220 << portConfigIt->toString();
221 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
222 }
223 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700224 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000225 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
226 << " frames is too large, maximum size is "
227 << kMaximumStreamBufferSizeBytes / frameSize;
228 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
229 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000230 const auto& flags = portConfigIt->flags.value();
231 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000232 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
233 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000234 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000235 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
236 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000237 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
Mikhail Naganov194daaa2023-01-05 22:34:20 +0000238 mVendorDebug.forceTransientBurst,
239 mVendorDebug.forceSynchronousDrain};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000240 StreamContext temp(
241 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
242 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganov13501872023-10-18 16:15:46 -0700243 portConfigIt->format.value(), portConfigIt->channelMask.value(),
244 portConfigIt->sampleRate.value().value, flags, getNominalLatencyMs(*portConfigIt),
Mikhail Naganovb42a69e2023-06-16 12:38:25 -0700245 portConfigIt->ext.get<AudioPortExt::mix>().handle,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000246 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov3c8b6ce2023-10-31 11:20:30 -0700247 asyncCallback, outEventCallback,
248 std::weak_ptr<sounddose::StreamDataProcessorInterface>{}, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000249 if (temp.isValid()) {
250 *out_context = std::move(temp);
251 } else {
252 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
253 }
254 } else {
255 // TODO: Implement simulation of MMAP buffer allocation
256 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000257 return ndk::ScopedAStatus::ok();
258}
259
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000260std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
261 std::vector<AudioDevice> result;
262 auto& ports = getConfig().ports;
263 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
264 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
265 auto portIt = findById<AudioPort>(ports, *it);
266 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
267 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
268 }
269 }
270 return result;
271}
272
273std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
274 std::set<int32_t> result;
275 auto patchIdsRange = mPatches.equal_range(portConfigId);
276 auto& patches = getConfig().patches;
277 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
278 auto patchIt = findById<AudioPatch>(patches, it->second);
279 if (patchIt == patches.end()) {
280 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
281 << "not found in the configuration";
282 }
283 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
284 portConfigId) != patchIt->sourcePortConfigIds.end()) {
285 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
286 } else {
287 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
288 }
289 }
290 return result;
291}
292
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000293ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
294 auto& configs = getConfig().portConfigs;
295 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
296 if (portConfigIt == configs.end()) {
297 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
298 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
299 }
300 const int32_t portId = portConfigIt->portId;
301 // In our implementation, configs of mix ports always have unique IDs.
302 CHECK(portId != in_portConfigId);
303 auto& ports = getConfig().ports;
304 auto portIt = findById<AudioPort>(ports, portId);
305 if (portIt == ports.end()) {
306 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
307 << in_portConfigId << " not found";
308 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
309 }
310 if (mStreams.count(in_portConfigId) != 0) {
311 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
312 << " already has a stream opened on it";
313 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
314 }
315 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
316 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
317 << " does not correspond to a mix port";
318 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
319 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700320 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000321 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
322 LOG(ERROR) << __func__ << ": port id " << portId
323 << " has already reached maximum allowed opened stream count: "
324 << maxOpenStreamCount;
325 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
326 }
327 *port = &(*portIt);
328 return ndk::ScopedAStatus::ok();
329}
330
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000331void Module::populateConnectedProfiles() {
332 Configuration& config = getConfig();
333 for (const AudioPort& port : config.ports) {
334 if (port.ext.getTag() == AudioPortExt::device) {
335 if (auto devicePort = port.ext.get<AudioPortExt::device>();
336 !devicePort.device.type.connection.empty() && port.profiles.empty()) {
337 if (auto connIt = config.connectedProfiles.find(port.id);
338 connIt == config.connectedProfiles.end()) {
339 config.connectedProfiles.emplace(
340 port.id, internal::getStandard16And24BitPcmAudioProfiles());
341 }
342 }
343 }
344 }
345}
346
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000347template <typename C>
348std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
349 std::set<int32_t> result;
350 auto& portConfigs = getConfig().portConfigs;
351 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
352 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
353 if (portConfigIt != portConfigs.end()) {
354 result.insert(portConfigIt->portId);
355 }
356 }
357 return result;
358}
359
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000360std::unique_ptr<Module::Configuration> Module::initializeConfig() {
361 return internal::getConfiguration(getType());
Peter Yoon918a6a52023-07-13 17:04:37 +0900362}
363
Mikhail Naganov13501872023-10-18 16:15:46 -0700364int32_t Module::getNominalLatencyMs(const AudioPortConfig&) {
365 // Arbitrary value. Implementations must override this method to provide their actual latency.
366 static constexpr int32_t kLatencyMs = 5;
367 return kLatencyMs;
368}
369
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700370std::vector<AudioRoute*> Module::getAudioRoutesForAudioPortImpl(int32_t portId) {
371 std::vector<AudioRoute*> result;
372 auto& routes = getConfig().routes;
373 for (auto& r : routes) {
374 const auto& srcs = r.sourcePortIds;
375 if (r.sinkPortId == portId || std::find(srcs.begin(), srcs.end(), portId) != srcs.end()) {
376 result.push_back(&r);
377 }
378 }
379 return result;
380}
381
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000382Module::Configuration& Module::getConfig() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000383 if (!mConfig) {
Peter Yoon918a6a52023-07-13 17:04:37 +0900384 mConfig = std::move(initializeConfig());
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000385 }
386 return *mConfig;
387}
388
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700389std::set<int32_t> Module::getRoutableAudioPortIds(int32_t portId,
390 std::vector<AudioRoute*>* routes) {
391 std::vector<AudioRoute*> routesStorage;
392 if (routes == nullptr) {
393 routesStorage = getAudioRoutesForAudioPortImpl(portId);
394 routes = &routesStorage;
395 }
396 std::set<int32_t> result;
397 for (AudioRoute* r : *routes) {
398 if (r->sinkPortId == portId) {
399 result.insert(r->sourcePortIds.begin(), r->sourcePortIds.end());
400 } else {
401 result.insert(r->sinkPortId);
402 }
403 }
404 return result;
405}
406
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000407void Module::registerPatch(const AudioPatch& patch) {
408 auto& configs = getConfig().portConfigs;
409 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
410 for (auto portConfigId : portConfigIds) {
411 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
412 if (configIt != configs.end()) {
413 mPatches.insert(std::pair{portConfigId, patch.id});
414 if (configIt->portId != portConfigId) {
415 mPatches.insert(std::pair{configIt->portId, patch.id});
416 }
417 }
418 };
419 };
420 do_insert(patch.sourcePortConfigIds);
421 do_insert(patch.sinkPortConfigIds);
422}
423
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700424ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
425 const AudioPatch& newPatch) {
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700426 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700427 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700428 using Connections =
429 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
430 Connections oldConnections, newConnections;
431 auto fillConnectionsHelper = [&](Connections& connections,
432 const std::vector<int32_t>& mixPortCfgIds,
433 const std::vector<int32_t>& devicePortCfgIds) {
434 for (int32_t mixPortCfgId : mixPortCfgIds) {
435 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
436 }
437 };
438 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
439 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
440 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
441 patch.sourcePortConfigIds.end()) {
442 // Sources are mix ports.
443 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
444 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
445 [&](int32_t portConfigId) {
446 return mStreams.count(portConfigId) > 0;
447 }) != patch.sinkPortConfigIds.end()) {
448 // Sources are device ports.
449 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
450 } // Otherwise, there are no streams to notify.
451 };
452 fillConnections(oldConnections, oldPatch);
453 fillConnections(newConnections, newPatch);
454
455 std::for_each(oldConnections.begin(), oldConnections.end(), [&](const auto& connectionPair) {
456 const int32_t mixPortConfigId = connectionPair.first;
457 if (auto it = newConnections.find(mixPortConfigId);
458 it == newConnections.end() || it->second != connectionPair.second) {
459 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, {});
460 status.isOk()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700461 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700462 << mixPortConfigId << " has been disconnected";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700463 } else {
464 // Disconnection is tricky to roll back, just register a failure.
465 maybeFailure = std::move(status);
466 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000467 }
468 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700469 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700470 std::set<int32_t> idsToDisconnectOnFailure;
471 std::for_each(newConnections.begin(), newConnections.end(), [&](const auto& connectionPair) {
472 const int32_t mixPortConfigId = connectionPair.first;
473 if (auto it = oldConnections.find(mixPortConfigId);
474 it == oldConnections.end() || it->second != connectionPair.second) {
475 const auto connectedDevices = findConnectedDevices(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700476 if (connectedDevices.empty()) {
477 // This is important as workers use the vector size to derive the connection status.
478 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
479 "config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700480 << mixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700481 }
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700482 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700483 status.isOk()) {
484 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700485 << mixPortConfigId << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700486 << ::android::internal::ToString(connectedDevices);
487 } else {
488 maybeFailure = std::move(status);
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700489 idsToDisconnectOnFailure.insert(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700490 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000491 }
492 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700493 if (!maybeFailure.isOk()) {
494 LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids "
495 << ::android::internal::ToString(idsToDisconnectOnFailure);
496 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
497 [&](const auto& portConfigId) {
498 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
499 (void)status.isOk(); // Can't do much about a failure here.
500 });
501 return maybeFailure;
502 }
503 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000504}
505
Mikhail Naganov00603d12022-05-02 22:52:13 +0000506ndk::ScopedAStatus Module::setModuleDebug(
507 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700508 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000509 << ", new flags: " << in_debug.toString();
510 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
511 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700512 LOG(ERROR) << __func__ << ": " << mType
513 << ": attempting to change device connections simulation while having external "
514 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000515 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
516 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000517 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700518 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000519 << in_debug.streamTransientStateDelayMs;
520 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
521 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000522 mDebug = in_debug;
523 return ndk::ScopedAStatus::ok();
524}
525
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000526ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700527 *_aidl_return = nullptr;
528 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000529 return ndk::ScopedAStatus::ok();
530}
531
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000532ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700533 *_aidl_return = nullptr;
534 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000535 return ndk::ScopedAStatus::ok();
536}
537
Mikhail Naganov7499a002023-02-27 18:51:44 -0800538ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700539 *_aidl_return = nullptr;
540 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800541 return ndk::ScopedAStatus::ok();
542}
543
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800544ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700545 *_aidl_return = nullptr;
546 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800547 return ndk::ScopedAStatus::ok();
548}
549
Mikhail Naganov00603d12022-05-02 22:52:13 +0000550ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
551 AudioPort* _aidl_return) {
552 const int32_t templateId = in_templateIdAndAdditionalData.id;
553 auto& ports = getConfig().ports;
554 AudioPort connectedPort;
555 { // Scope the template port so that we don't accidentally modify it.
556 auto templateIt = findById<AudioPort>(ports, templateId);
557 if (templateIt == ports.end()) {
558 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
559 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
560 }
561 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
562 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
563 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
564 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000565 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
566 if (templateDevicePort.device.type.connection.empty()) {
567 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
568 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
569 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700570 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
571 LOG(ERROR) << __func__ << ": port id " << templateId << " is a connected device port";
572 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
573 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000574 // Postpone id allocation until we ensure that there are no client errors.
575 connectedPort = *templateIt;
576 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
577 const auto& inputDevicePort =
578 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
579 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
580 connectedDevicePort.device.address = inputDevicePort.device.address;
581 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
582 << connectedDevicePort.device.toString();
583 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700584 for (auto connectedPortPair : mConnectedDevicePorts) {
585 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000586 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
587 connectedDevicePort.device) {
588 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700589 << " is already connected at the device port id "
590 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000591 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
592 }
593 }
594 }
595
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700596 // Two main cases are considered with regard to the profiles of the connected device port:
597 //
598 // 1. If the template device port has dynamic profiles, and at least one routable mix
599 // port also has dynamic profiles, it means that after connecting the device, the
600 // connected device port must have profiles populated with actual capabilities of
601 // the connected device, and dynamic of routable mix ports will be filled
602 // according to these capabilities. An example of this case is connection of an
603 // HDMI or USB device. For USB handled by ADSP, there can be mix ports with static
604 // profiles, and one dedicated mix port for "hi-fi" playback. The latter is left with
605 // dynamic profiles so that they can be populated with actual capabilities of
606 // the connected device.
607 //
608 // 2. If the template device port has dynamic profiles, while all routable mix ports
609 // have static profiles, it means that after connecting the device, the connected
610 // device port can be left with dynamic profiles, and profiles of mix ports are
611 // left untouched. An example of this case is connection of an analog wired
612 // headset, it should be treated in the same way as a speaker.
613 //
614 // Yet another possible case is when both the template device port and all routable
615 // mix ports have static profiles. This is allowed and handled correctly, however, it
616 // is not very practical, since these profiles are likely duplicates of each other.
617
618 std::vector<AudioRoute*> routesToMixPorts = getAudioRoutesForAudioPortImpl(templateId);
619 std::set<int32_t> routableMixPortIds = getRoutableAudioPortIds(templateId, &routesToMixPorts);
Mikhail Naganov55045b52023-10-24 17:03:50 -0700620 if (!mDebug.simulateDeviceConnections) {
621 // Even if the device port has static profiles, the HAL module might need to update
622 // them, or abort the connection process.
623 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
624 } else if (hasDynamicProfilesOnly(connectedPort.profiles)) {
625 auto& connectedProfiles = getConfig().connectedProfiles;
626 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
627 connectedProfilesIt != connectedProfiles.end()) {
628 connectedPort.profiles = connectedProfilesIt->second;
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700629 }
Mikhail Naganov55045b52023-10-24 17:03:50 -0700630 }
631 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
632 // Possible case 2. Check if all routable mix ports have static profiles.
633 if (auto dynamicMixPortIt = std::find_if(ports.begin(), ports.end(),
634 [&routableMixPortIds](const auto& p) {
635 return routableMixPortIds.count(p.id) > 0 &&
636 hasDynamicProfilesOnly(p.profiles);
637 });
638 dynamicMixPortIt != ports.end()) {
639 LOG(ERROR) << __func__ << ": connected port only has dynamic profiles after connecting "
640 << "external device " << connectedPort.toString() << ", and there exist "
641 << "a routable mix port with dynamic profiles: "
642 << dynamicMixPortIt->toString();
643 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530644 }
645 }
646
Mikhail Naganovdc417732023-09-29 15:49:35 -0700647 connectedPort.id = getConfig().nextPortId++;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700648 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700649 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000650 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
651 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000652 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000653 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000654
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700655 // For routes where the template port is a source, add the connected port to sources,
656 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000657 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700658 for (AudioRoute* r : routesToMixPorts) {
659 if (r->sinkPortId == templateId) {
660 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
661 .sinkPortId = connectedPort.id,
662 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000663 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700664 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000665 }
666 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700667 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000668 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
669
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700670 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
671 // Note: this is a simplistic approach assuming that a mix port can only be populated
672 // from a single device port. Implementing support for stuffing dynamic profiles with
673 // a superset of all profiles from all routable dynamic device ports would be more involved.
674 for (auto& port : ports) {
675 if (routableMixPortIds.count(port.id) == 0) continue;
676 if (hasDynamicProfilesOnly(port.profiles)) {
677 port.profiles = connectedPort.profiles;
678 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700679 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700680 // Check if profiles are not all dynamic because they were populated by
681 // a previous connection. Otherwise, it means that they are actually static.
682 for (const auto& cp : mConnectedDevicePorts) {
683 if (cp.second.count(port.id) > 0) {
684 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700685 break;
686 }
687 }
688 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700689 }
690 }
691 *_aidl_return = std::move(connectedPort);
692
Mikhail Naganov00603d12022-05-02 22:52:13 +0000693 return ndk::ScopedAStatus::ok();
694}
695
696ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
697 auto& ports = getConfig().ports;
698 auto portIt = findById<AudioPort>(ports, in_portId);
699 if (portIt == ports.end()) {
700 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
701 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
702 }
703 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
704 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
705 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
706 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700707 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
708 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000709 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
710 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
711 }
712 auto& configs = getConfig().portConfigs;
713 auto& initials = getConfig().initialConfigs;
714 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
715 if (config.portId == in_portId) {
716 // Check if the configuration was provided by the client.
717 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
718 return initialIt == initials.end() || config != *initialIt;
719 }
720 return false;
721 });
722 if (configIt != configs.end()) {
723 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
724 << configIt->id;
725 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
726 }
jiabin783c48b2023-02-28 18:28:06 +0000727 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000728 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000729 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
730
731 auto& routes = getConfig().routes;
732 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
733 if (routesIt->sinkPortId == in_portId) {
734 routesIt = routes.erase(routesIt);
735 } else {
736 // Note: the list of sourcePortIds can't become empty because there must
737 // be the id of the template port in the route.
738 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
739 ++routesIt;
740 }
741 }
742
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700743 // Clear profiles for mix ports that are not connected to any other ports.
744 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
745 mConnectedDevicePorts.erase(connectedPortsIt);
746 for (const auto& connectedPort : mConnectedDevicePorts) {
747 for (int32_t mixPortId : connectedPort.second) {
748 mixPortsToClear.erase(mixPortId);
749 }
750 }
751 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700752 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
753 if (mixPortIt != ports.end()) {
754 mixPortIt->profiles = {};
755 }
756 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700757
Mikhail Naganov00603d12022-05-02 22:52:13 +0000758 return ndk::ScopedAStatus::ok();
759}
760
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000761ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
762 *_aidl_return = getConfig().patches;
763 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
764 return ndk::ScopedAStatus::ok();
765}
766
767ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
768 auto& ports = getConfig().ports;
769 auto portIt = findById<AudioPort>(ports, in_portId);
770 if (portIt != ports.end()) {
771 *_aidl_return = *portIt;
772 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
773 return ndk::ScopedAStatus::ok();
774 }
775 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
776 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
777}
778
779ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
780 *_aidl_return = getConfig().portConfigs;
781 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
782 return ndk::ScopedAStatus::ok();
783}
784
785ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
786 *_aidl_return = getConfig().ports;
787 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
788 return ndk::ScopedAStatus::ok();
789}
790
791ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
792 *_aidl_return = getConfig().routes;
793 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
794 return ndk::ScopedAStatus::ok();
795}
796
Mikhail Naganov00603d12022-05-02 22:52:13 +0000797ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
798 std::vector<AudioRoute>* _aidl_return) {
799 auto& ports = getConfig().ports;
800 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
801 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
802 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
803 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700804 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
805 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
806 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000807 return ndk::ScopedAStatus::ok();
808}
809
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000810ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
811 OpenInputStreamReturn* _aidl_return) {
812 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
813 << in_args.bufferSizeFrames << " frames";
814 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700815 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000816 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
817 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000818 << " does not correspond to an input mix port";
819 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
820 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000821 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700822 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
823 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000824 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000825 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700826 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000827 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000828 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700829 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
830 RETURN_STATUS_IF_ERROR(
831 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
832 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000833 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
834 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000835 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000836 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000837 return ndk::ScopedAStatus::ok();
838}
839
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000840ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
841 OpenOutputStreamReturn* _aidl_return) {
842 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
843 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
844 << " frames";
845 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700846 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000847 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
848 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000849 << " does not correspond to an output mix port";
850 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
851 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000852 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
853 AudioOutputFlags::COMPRESS_OFFLOAD);
854 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000855 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000856 << " has COMPRESS_OFFLOAD flag set, requires offload info";
857 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
858 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000859 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
860 AudioOutputFlags::NON_BLOCKING);
861 if (isNonBlocking && in_args.callback == nullptr) {
862 LOG(ERROR) << __func__ << ": port id " << port->id
863 << " has NON_BLOCKING flag set, requires async callback";
864 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
865 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000866 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700867 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
868 isNonBlocking ? in_args.callback : nullptr,
869 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000870 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000871 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700872 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700873 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000874 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700875 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
876 RETURN_STATUS_IF_ERROR(
877 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
878 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000879 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
880 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000881 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000882 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000883 return ndk::ScopedAStatus::ok();
884}
885
Mikhail Naganov74927202022-12-19 16:37:14 +0000886ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
887 SupportedPlaybackRateFactors* _aidl_return) {
888 LOG(DEBUG) << __func__;
889 (void)_aidl_return;
890 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
891}
892
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000893ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000894 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000895 if (in_requested.sourcePortConfigIds.empty()) {
896 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
897 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
898 }
899 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
900 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
901 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
902 }
903 if (in_requested.sinkPortConfigIds.empty()) {
904 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
905 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
906 }
907 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
908 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
909 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
910 }
911
912 auto& configs = getConfig().portConfigs;
913 std::vector<int32_t> missingIds;
914 auto sources =
915 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
916 if (!missingIds.empty()) {
917 LOG(ERROR) << __func__ << ": following source port config ids not found: "
918 << ::android::internal::ToString(missingIds);
919 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
920 }
921 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
922 if (!missingIds.empty()) {
923 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
924 << ::android::internal::ToString(missingIds);
925 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
926 }
927 // bool indicates whether a non-exclusive route is available.
928 // If only an exclusive route is available, that means the patch can not be
929 // established if there is any other patch which currently uses the sink port.
930 std::map<int32_t, bool> allowedSinkPorts;
931 auto& routes = getConfig().routes;
932 for (auto src : sources) {
933 for (const auto& r : routes) {
934 const auto& srcs = r.sourcePortIds;
935 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
936 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
937 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
938 }
939 }
940 }
941 }
942 for (auto sink : sinks) {
943 if (allowedSinkPorts.count(sink->portId) == 0) {
944 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
945 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
946 }
947 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700948 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000949
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000950 auto& patches = getConfig().patches;
951 auto existing = patches.end();
952 std::optional<decltype(mPatches)> patchesBackup;
953 if (in_requested.id != 0) {
954 existing = findById<AudioPatch>(patches, in_requested.id);
955 if (existing != patches.end()) {
956 patchesBackup = mPatches;
957 cleanUpPatch(existing->id);
958 } else {
959 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
960 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
961 }
962 }
963 // Validate the requested patch.
964 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
965 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
966 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
967 << "is exclusive and is already used by some other patch";
968 if (patchesBackup.has_value()) {
969 mPatches = std::move(*patchesBackup);
970 }
971 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
972 }
973 }
Mikhail Naganov13501872023-10-18 16:15:46 -0700974 // Find the highest sample rate among mix port configs.
975 std::map<int32_t, AudioPortConfig*> sampleRates;
976 std::vector<AudioPortConfig*>& mixPortConfigs =
977 sources[0]->ext.getTag() == AudioPortExt::mix ? sources : sinks;
978 for (auto mix : mixPortConfigs) {
979 sampleRates.emplace(mix->sampleRate.value().value, mix);
980 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000981 *_aidl_return = in_requested;
Mikhail Naganov13501872023-10-18 16:15:46 -0700982 auto maxSampleRateIt = std::max_element(sampleRates.begin(), sampleRates.end());
983 const int32_t latencyMs = getNominalLatencyMs(*(maxSampleRateIt->second));
984 _aidl_return->minimumStreamBufferSizeFrames =
985 calculateBufferSizeFrames(latencyMs, maxSampleRateIt->first);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000986 _aidl_return->latenciesMs.clear();
987 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
Mikhail Naganov13501872023-10-18 16:15:46 -0700988 _aidl_return->sinkPortConfigIds.size(), latencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000989 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000990 if (existing == patches.end()) {
991 _aidl_return->id = getConfig().nextPatchId++;
992 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000993 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000994 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -0700995 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000996 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700997 patchesBackup = mPatches;
998 registerPatch(*_aidl_return);
999 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
1000 mPatches = std::move(*patchesBackup);
1001 if (existing == patches.end()) {
1002 patches.pop_back();
1003 } else {
1004 *existing = oldPatch;
1005 }
1006 return status;
1007 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001008
1009 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
1010 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001011 return ndk::ScopedAStatus::ok();
1012}
1013
1014ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
1015 AudioPortConfig* out_suggested, bool* _aidl_return) {
1016 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
1017 auto& configs = getConfig().portConfigs;
1018 auto existing = configs.end();
1019 if (in_requested.id != 0) {
1020 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1021 existing == configs.end()) {
1022 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
1023 << " not found";
1024 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1025 }
1026 }
1027
1028 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1029 if (portId == 0) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001030 LOG(ERROR) << __func__ << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001031 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1032 }
1033 auto& ports = getConfig().ports;
1034 auto portIt = findById<AudioPort>(ports, portId);
1035 if (portIt == ports.end()) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001036 LOG(ERROR) << __func__ << ": requested port config points to non-existent portId "
1037 << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001038 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1039 }
1040 if (existing != configs.end()) {
1041 *out_suggested = *existing;
1042 } else {
1043 AudioPortConfig newConfig;
1044 if (generateDefaultPortConfig(*portIt, &newConfig)) {
1045 *out_suggested = newConfig;
1046 } else {
1047 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
1048 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1049 }
1050 }
1051 // From this moment, 'out_suggested' is either an existing port config,
1052 // or a new generated config. Now attempt to update it according to the specified
1053 // fields of 'in_requested'.
1054
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001055 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1056 // which takes care of their actual configuration automatically.
1057 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1058 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001059 bool requestedIsValid = true, requestedIsFullySpecified = true;
1060
1061 AudioIoFlags portFlags = portIt->flags;
1062 if (in_requested.flags.has_value()) {
1063 if (in_requested.flags.value() != portFlags) {
1064 LOG(WARNING) << __func__ << ": requested flags "
1065 << in_requested.flags.value().toString() << " do not match port's "
1066 << portId << " flags " << portFlags.toString();
1067 requestedIsValid = false;
1068 }
1069 } else {
1070 requestedIsFullySpecified = false;
1071 }
1072
1073 AudioProfile portProfile;
1074 if (in_requested.format.has_value()) {
1075 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001076 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1077 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001078 out_suggested->format = format;
1079 } else {
1080 LOG(WARNING) << __func__ << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001081 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001082 requestedIsValid = false;
1083 }
1084 } else {
1085 requestedIsFullySpecified = false;
1086 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001087 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1088 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001089 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
1090 << out_suggested->format.value().toString() << " anymore";
1091 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1092 }
1093
1094 if (in_requested.channelMask.has_value()) {
1095 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001096 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1097 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1098 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001099 out_suggested->channelMask = channelMask;
1100 } else {
1101 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
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.sampleRate.has_value()) {
1111 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001112 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1113 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001114 sampleRate.value) != portProfile.sampleRates.end()) {
1115 out_suggested->sampleRate = sampleRate;
1116 } else {
1117 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
1118 << " is not supported for the format " << portProfile.format.toString()
1119 << " by the port " << portId;
1120 requestedIsValid = false;
1121 }
1122 } else {
1123 requestedIsFullySpecified = false;
1124 }
1125
1126 if (in_requested.gain.has_value()) {
1127 // Let's pretend that gain can always be applied.
1128 out_suggested->gain = in_requested.gain.value();
1129 }
1130
Mikhail Naganov248e9502023-02-21 16:32:40 -08001131 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1132 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1133 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
1134 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
1135 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
1136 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
1137 }
1138 } else {
1139 LOG(WARNING) << __func__ << ": requested ext tag "
1140 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1141 << toString(out_suggested->ext.getTag());
1142 requestedIsValid = false;
1143 }
1144 }
1145
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001146 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1147 out_suggested->id = getConfig().nextPortId++;
1148 configs.push_back(*out_suggested);
1149 *_aidl_return = true;
1150 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
1151 } else if (existing != configs.end() && requestedIsValid) {
1152 *existing = *out_suggested;
1153 *_aidl_return = true;
1154 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
1155 } else {
1156 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
1157 << "; requested is valid? " << requestedIsValid << ", fully specified? "
1158 << requestedIsFullySpecified;
1159 *_aidl_return = false;
1160 }
1161 return ndk::ScopedAStatus::ok();
1162}
1163
1164ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1165 auto& patches = getConfig().patches;
1166 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1167 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001168 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001169 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001170 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1171 mPatches = std::move(patchesBackup);
1172 return status;
1173 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001174 patches.erase(patchIt);
1175 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1176 return ndk::ScopedAStatus::ok();
1177 }
1178 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1179 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1180}
1181
1182ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1183 auto& configs = getConfig().portConfigs;
1184 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1185 if (configIt != configs.end()) {
1186 if (mStreams.count(in_portConfigId) != 0) {
1187 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1188 << " has a stream opened on it";
1189 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1190 }
1191 auto patchIt = mPatches.find(in_portConfigId);
1192 if (patchIt != mPatches.end()) {
1193 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1194 << " is used by the patch with id " << patchIt->second;
1195 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1196 }
1197 auto& initials = getConfig().initialConfigs;
1198 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1199 if (initialIt == initials.end()) {
1200 configs.erase(configIt);
1201 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1202 } else if (*configIt != *initialIt) {
1203 *configIt = *initialIt;
1204 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1205 }
1206 return ndk::ScopedAStatus::ok();
1207 }
1208 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1209 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1210}
1211
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001212ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1213 *_aidl_return = mMasterMute;
1214 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1215 return ndk::ScopedAStatus::ok();
1216}
1217
1218ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1219 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001220 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1221 : onMasterMuteChanged(in_mute);
1222 if (result.isOk()) {
1223 mMasterMute = in_mute;
1224 } else {
1225 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1226 << "), error=" << result;
1227 // Reset master mute if it failed.
1228 onMasterMuteChanged(mMasterMute);
1229 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001230 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001231}
1232
1233ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1234 *_aidl_return = mMasterVolume;
1235 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1236 return ndk::ScopedAStatus::ok();
1237}
1238
1239ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1240 LOG(DEBUG) << __func__ << ": " << in_volume;
1241 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001242 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1243 : onMasterVolumeChanged(in_volume);
1244 if (result.isOk()) {
1245 mMasterVolume = in_volume;
1246 } else {
1247 // Reset master volume if it failed.
1248 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1249 << "), error=" << result;
1250 onMasterVolumeChanged(mMasterVolume);
1251 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001252 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001253 }
1254 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1255 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1256}
1257
1258ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1259 *_aidl_return = mMicMute;
1260 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1261 return ndk::ScopedAStatus::ok();
1262}
1263
1264ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1265 LOG(DEBUG) << __func__ << ": " << in_mute;
1266 mMicMute = in_mute;
1267 return ndk::ScopedAStatus::ok();
1268}
1269
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001270ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001271 *_aidl_return = getMicrophoneInfos();
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001272 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1273 return ndk::ScopedAStatus::ok();
1274}
1275
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001276ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001277 if (!isValidAudioMode(in_mode)) {
1278 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1279 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1280 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001281 // No checks for supported audio modes here, it's an informative notification.
1282 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1283 return ndk::ScopedAStatus::ok();
1284}
1285
1286ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1287 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1288 return ndk::ScopedAStatus::ok();
1289}
1290
1291ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1292 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1293 return ndk::ScopedAStatus::ok();
1294}
1295
Vlad Popa83a6d822022-11-07 13:53:57 +01001296ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001297 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001298 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001299 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001300 *_aidl_return = mSoundDose.getInstance();
Vlad Popa943b7e22022-12-08 14:24:12 +01001301 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001302 return ndk::ScopedAStatus::ok();
1303}
1304
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001305ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1306 LOG(DEBUG) << __func__;
1307 (void)_aidl_return;
1308 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1309}
1310
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001311const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001312const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001313
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001314ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1315 std::vector<VendorParameter>* _aidl_return) {
1316 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001317 bool allParametersKnown = true;
1318 for (const auto& id : in_ids) {
1319 if (id == VendorDebug::kForceTransientBurstName) {
1320 VendorParameter forceTransientBurst{.id = id};
1321 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1322 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001323 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1324 VendorParameter forceSynchronousDrain{.id = id};
1325 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1326 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001327 } else {
1328 allParametersKnown = false;
1329 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1330 }
1331 }
1332 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1333 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001334}
1335
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001336namespace {
1337
1338template <typename W>
1339bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1340 std::optional<W> value;
1341 binder_status_t result = p.ext.getParcelable(&value);
1342 if (result == STATUS_OK && value.has_value()) {
1343 *v = value.value().value;
1344 return true;
1345 }
1346 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1347 << "\": " << result;
1348 return false;
1349}
1350
1351} // namespace
1352
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001353ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1354 bool in_async) {
1355 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1356 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001357 bool allParametersKnown = true;
1358 for (const auto& p : in_parameters) {
1359 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001360 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1361 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1362 }
1363 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1364 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001365 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1366 }
1367 } else {
1368 allParametersKnown = false;
1369 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1370 }
1371 }
1372 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1373 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001374}
1375
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001376ndk::ScopedAStatus Module::addDeviceEffect(
1377 int32_t in_portConfigId,
1378 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1379 if (in_effect == nullptr) {
1380 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1381 } else {
1382 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1383 << in_effect->asBinder().get();
1384 }
1385 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1386}
1387
1388ndk::ScopedAStatus Module::removeDeviceEffect(
1389 int32_t in_portConfigId,
1390 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1391 if (in_effect == nullptr) {
1392 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1393 } else {
1394 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1395 << in_effect->asBinder().get();
1396 }
1397 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1398}
1399
jiabin9a8e6862023-01-12 23:06:37 +00001400ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1401 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1402 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1403 std::set<int32_t> mmapSinks;
1404 std::set<int32_t> mmapSources;
1405 auto& ports = getConfig().ports;
1406 for (const auto& port : ports) {
1407 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1408 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1409 AudioInputFlags::MMAP_NOIRQ)) {
1410 mmapSinks.insert(port.id);
1411 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1412 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1413 AudioOutputFlags::MMAP_NOIRQ)) {
1414 mmapSources.insert(port.id);
1415 }
1416 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001417 if (mmapSources.empty() && mmapSinks.empty()) {
1418 AudioMMapPolicyInfo never;
1419 never.mmapPolicy = AudioMMapPolicy::NEVER;
1420 _aidl_return->push_back(never);
1421 return ndk::ScopedAStatus::ok();
1422 }
jiabin9a8e6862023-01-12 23:06:37 +00001423 for (const auto& route : getConfig().routes) {
1424 if (mmapSinks.count(route.sinkPortId) != 0) {
1425 // The sink is a mix port, add the sources if they are device ports.
1426 for (int sourcePortId : route.sourcePortIds) {
1427 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1428 if (sourcePortIt == ports.end()) {
1429 // This must not happen
1430 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1431 continue;
1432 }
1433 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1434 // The source is not a device port, skip
1435 continue;
1436 }
1437 AudioMMapPolicyInfo policyInfo;
1438 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1439 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1440 // default implementation.
1441 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1442 _aidl_return->push_back(policyInfo);
1443 }
1444 } else {
1445 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1446 if (sinkPortIt == ports.end()) {
1447 // This must not happen
1448 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1449 continue;
1450 }
1451 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1452 // The sink is not a device port, skip
1453 continue;
1454 }
1455 if (count_any(mmapSources, route.sourcePortIds)) {
1456 AudioMMapPolicyInfo policyInfo;
1457 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1458 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1459 // default implementation.
1460 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1461 _aidl_return->push_back(policyInfo);
1462 }
1463 }
1464 }
1465 return ndk::ScopedAStatus::ok();
1466}
1467
Eric Laurente2432ea2023-01-12 17:47:31 +01001468ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1469 LOG(DEBUG) << __func__;
1470 *_aidl_return = false;
1471 return ndk::ScopedAStatus::ok();
1472}
1473
jiabinb76981e2023-01-18 00:58:30 +00001474ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1475 if (!isMmapSupported()) {
1476 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1477 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1478 }
1479 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1480 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1481 return ndk::ScopedAStatus::ok();
1482}
1483
1484ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1485 if (!isMmapSupported()) {
1486 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1487 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1488 }
1489 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1490 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1491 return ndk::ScopedAStatus::ok();
1492}
1493
1494bool Module::isMmapSupported() {
1495 if (mIsMmapSupported.has_value()) {
1496 return mIsMmapSupported.value();
1497 }
1498 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1499 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1500 mIsMmapSupported = false;
1501 } else {
1502 mIsMmapSupported =
1503 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1504 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1505 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1506 }) != mmapPolicyInfos.end();
1507 }
1508 return mIsMmapSupported.value();
1509}
1510
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001511ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort) {
1512 if (audioPort->ext.getTag() != AudioPortExt::device) {
1513 LOG(ERROR) << __func__ << ": not a device port: " << audioPort->toString();
1514 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1515 }
1516 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1517 if (!devicePort.device.type.connection.empty()) {
1518 LOG(ERROR) << __func__
1519 << ": module implementation must override 'populateConnectedDevicePort' "
1520 << "to handle connection of external devices.";
1521 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1522 }
jiabin116d8392023-03-01 22:52:57 +00001523 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001524 return ndk::ScopedAStatus::ok();
1525}
1526
1527ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1528 const std::vector<AudioPortConfig*>& sources __unused,
1529 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001530 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001531 return ndk::ScopedAStatus::ok();
1532}
1533
jiabin783c48b2023-02-28 18:28:06 +00001534void Module::onExternalDeviceConnectionChanged(
1535 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1536 bool connected __unused) {
1537 LOG(DEBUG) << __func__ << ": do nothing and return";
1538}
1539
1540ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1541 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1542 return ndk::ScopedAStatus::ok();
1543}
1544
1545ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1546 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1547 return ndk::ScopedAStatus::ok();
1548}
1549
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001550std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1551 std::vector<MicrophoneInfo> result;
1552 Configuration& config = getConfig();
1553 for (const AudioPort& port : config.ports) {
1554 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1555 const AudioDeviceType deviceType =
1556 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1557 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1558 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1559 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1560 // accordingly based on their physical microphone parameters.
1561 result.push_back(MicrophoneInfo{
1562 .id = port.name,
1563 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1564 .group = 0,
1565 .indexInTheGroup = 0,
1566 });
1567 }
1568 }
1569 }
1570 return result;
1571}
1572
Ram Mohan18f0d512023-07-01 00:47:09 +05301573ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1574 return mStreams.bluetoothParametersUpdated();
1575}
1576
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001577} // namespace aidl::android::hardware::audio::core