blob: f725670f97c32f5724e52061f98eff23114daf24 [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 }
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);
620 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700621 if (!mDebug.simulateDeviceConnections) {
622 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
623 } else {
624 auto& connectedProfiles = getConfig().connectedProfiles;
625 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
626 connectedProfilesIt != connectedProfiles.end()) {
627 connectedPort.profiles = connectedProfilesIt->second;
628 }
629 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700630 if (hasDynamicProfilesOnly(connectedPort.profiles)) {
631 // Possible case 2. Check if all routable mix ports have static profiles.
632 if (auto dynamicMixPortIt = std::find_if(ports.begin(), ports.end(),
633 [&routableMixPortIds](const auto& p) {
634 return routableMixPortIds.count(p.id) >
635 0 &&
636 hasDynamicProfilesOnly(p.profiles);
637 });
638 dynamicMixPortIt != ports.end()) {
639 LOG(ERROR) << __func__
640 << ": connected port only has dynamic profiles after connecting "
641 << "external device " << connectedPort.toString() << ", and there exist "
642 << "a routable mix port with dynamic profiles: "
643 << dynamicMixPortIt->toString();
644 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
645 }
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530646 }
647 }
648
Mikhail Naganovdc417732023-09-29 15:49:35 -0700649 connectedPort.id = getConfig().nextPortId++;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700650 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700651 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000652 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
653 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000654 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000655 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000656
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700657 // For routes where the template port is a source, add the connected port to sources,
658 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000659 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700660 for (AudioRoute* r : routesToMixPorts) {
661 if (r->sinkPortId == templateId) {
662 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
663 .sinkPortId = connectedPort.id,
664 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000665 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700666 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000667 }
668 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700669 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000670 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
671
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700672 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
673 // Note: this is a simplistic approach assuming that a mix port can only be populated
674 // from a single device port. Implementing support for stuffing dynamic profiles with
675 // a superset of all profiles from all routable dynamic device ports would be more involved.
676 for (auto& port : ports) {
677 if (routableMixPortIds.count(port.id) == 0) continue;
678 if (hasDynamicProfilesOnly(port.profiles)) {
679 port.profiles = connectedPort.profiles;
680 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700681 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700682 // Check if profiles are not all dynamic because they were populated by
683 // a previous connection. Otherwise, it means that they are actually static.
684 for (const auto& cp : mConnectedDevicePorts) {
685 if (cp.second.count(port.id) > 0) {
686 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700687 break;
688 }
689 }
690 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700691 }
692 }
693 *_aidl_return = std::move(connectedPort);
694
Mikhail Naganov00603d12022-05-02 22:52:13 +0000695 return ndk::ScopedAStatus::ok();
696}
697
698ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
699 auto& ports = getConfig().ports;
700 auto portIt = findById<AudioPort>(ports, in_portId);
701 if (portIt == ports.end()) {
702 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
703 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
704 }
705 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
706 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
707 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
708 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700709 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
710 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000711 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
712 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
713 }
714 auto& configs = getConfig().portConfigs;
715 auto& initials = getConfig().initialConfigs;
716 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
717 if (config.portId == in_portId) {
718 // Check if the configuration was provided by the client.
719 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
720 return initialIt == initials.end() || config != *initialIt;
721 }
722 return false;
723 });
724 if (configIt != configs.end()) {
725 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
726 << configIt->id;
727 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
728 }
jiabin783c48b2023-02-28 18:28:06 +0000729 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000730 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000731 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
732
733 auto& routes = getConfig().routes;
734 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
735 if (routesIt->sinkPortId == in_portId) {
736 routesIt = routes.erase(routesIt);
737 } else {
738 // Note: the list of sourcePortIds can't become empty because there must
739 // be the id of the template port in the route.
740 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
741 ++routesIt;
742 }
743 }
744
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700745 // Clear profiles for mix ports that are not connected to any other ports.
746 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
747 mConnectedDevicePorts.erase(connectedPortsIt);
748 for (const auto& connectedPort : mConnectedDevicePorts) {
749 for (int32_t mixPortId : connectedPort.second) {
750 mixPortsToClear.erase(mixPortId);
751 }
752 }
753 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700754 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
755 if (mixPortIt != ports.end()) {
756 mixPortIt->profiles = {};
757 }
758 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700759
Mikhail Naganov00603d12022-05-02 22:52:13 +0000760 return ndk::ScopedAStatus::ok();
761}
762
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000763ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
764 *_aidl_return = getConfig().patches;
765 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
766 return ndk::ScopedAStatus::ok();
767}
768
769ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
770 auto& ports = getConfig().ports;
771 auto portIt = findById<AudioPort>(ports, in_portId);
772 if (portIt != ports.end()) {
773 *_aidl_return = *portIt;
774 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
775 return ndk::ScopedAStatus::ok();
776 }
777 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
778 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
779}
780
781ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
782 *_aidl_return = getConfig().portConfigs;
783 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
784 return ndk::ScopedAStatus::ok();
785}
786
787ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
788 *_aidl_return = getConfig().ports;
789 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
790 return ndk::ScopedAStatus::ok();
791}
792
793ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
794 *_aidl_return = getConfig().routes;
795 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
796 return ndk::ScopedAStatus::ok();
797}
798
Mikhail Naganov00603d12022-05-02 22:52:13 +0000799ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
800 std::vector<AudioRoute>* _aidl_return) {
801 auto& ports = getConfig().ports;
802 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
803 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
804 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
805 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700806 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
807 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
808 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000809 return ndk::ScopedAStatus::ok();
810}
811
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000812ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
813 OpenInputStreamReturn* _aidl_return) {
814 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
815 << in_args.bufferSizeFrames << " frames";
816 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700817 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000818 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
819 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000820 << " does not correspond to an input mix port";
821 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
822 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000823 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700824 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
825 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000826 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000827 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700828 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000829 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000830 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700831 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
832 RETURN_STATUS_IF_ERROR(
833 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
834 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000835 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
836 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000837 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000838 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000839 return ndk::ScopedAStatus::ok();
840}
841
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000842ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
843 OpenOutputStreamReturn* _aidl_return) {
844 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
845 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
846 << " frames";
847 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700848 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000849 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
850 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000851 << " does not correspond to an output mix port";
852 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
853 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000854 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
855 AudioOutputFlags::COMPRESS_OFFLOAD);
856 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000857 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000858 << " has COMPRESS_OFFLOAD flag set, requires offload info";
859 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
860 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000861 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
862 AudioOutputFlags::NON_BLOCKING);
863 if (isNonBlocking && in_args.callback == nullptr) {
864 LOG(ERROR) << __func__ << ": port id " << port->id
865 << " has NON_BLOCKING flag set, requires async callback";
866 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
867 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000868 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700869 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
870 isNonBlocking ? in_args.callback : nullptr,
871 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000872 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000873 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700874 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700875 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000876 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700877 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
878 RETURN_STATUS_IF_ERROR(
879 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
880 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000881 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
882 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000883 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000884 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000885 return ndk::ScopedAStatus::ok();
886}
887
Mikhail Naganov74927202022-12-19 16:37:14 +0000888ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
889 SupportedPlaybackRateFactors* _aidl_return) {
890 LOG(DEBUG) << __func__;
891 (void)_aidl_return;
892 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
893}
894
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000895ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000896 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000897 if (in_requested.sourcePortConfigIds.empty()) {
898 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
899 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
900 }
901 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
902 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
903 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
904 }
905 if (in_requested.sinkPortConfigIds.empty()) {
906 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
907 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
908 }
909 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
910 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
911 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
912 }
913
914 auto& configs = getConfig().portConfigs;
915 std::vector<int32_t> missingIds;
916 auto sources =
917 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
918 if (!missingIds.empty()) {
919 LOG(ERROR) << __func__ << ": following source port config ids not found: "
920 << ::android::internal::ToString(missingIds);
921 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
922 }
923 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
924 if (!missingIds.empty()) {
925 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
926 << ::android::internal::ToString(missingIds);
927 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
928 }
929 // bool indicates whether a non-exclusive route is available.
930 // If only an exclusive route is available, that means the patch can not be
931 // established if there is any other patch which currently uses the sink port.
932 std::map<int32_t, bool> allowedSinkPorts;
933 auto& routes = getConfig().routes;
934 for (auto src : sources) {
935 for (const auto& r : routes) {
936 const auto& srcs = r.sourcePortIds;
937 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
938 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
939 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
940 }
941 }
942 }
943 }
944 for (auto sink : sinks) {
945 if (allowedSinkPorts.count(sink->portId) == 0) {
946 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
947 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
948 }
949 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700950 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000951
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000952 auto& patches = getConfig().patches;
953 auto existing = patches.end();
954 std::optional<decltype(mPatches)> patchesBackup;
955 if (in_requested.id != 0) {
956 existing = findById<AudioPatch>(patches, in_requested.id);
957 if (existing != patches.end()) {
958 patchesBackup = mPatches;
959 cleanUpPatch(existing->id);
960 } else {
961 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
962 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
963 }
964 }
965 // Validate the requested patch.
966 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
967 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
968 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
969 << "is exclusive and is already used by some other patch";
970 if (patchesBackup.has_value()) {
971 mPatches = std::move(*patchesBackup);
972 }
973 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
974 }
975 }
Mikhail Naganov13501872023-10-18 16:15:46 -0700976 // Find the highest sample rate among mix port configs.
977 std::map<int32_t, AudioPortConfig*> sampleRates;
978 std::vector<AudioPortConfig*>& mixPortConfigs =
979 sources[0]->ext.getTag() == AudioPortExt::mix ? sources : sinks;
980 for (auto mix : mixPortConfigs) {
981 sampleRates.emplace(mix->sampleRate.value().value, mix);
982 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000983 *_aidl_return = in_requested;
Mikhail Naganov13501872023-10-18 16:15:46 -0700984 auto maxSampleRateIt = std::max_element(sampleRates.begin(), sampleRates.end());
985 const int32_t latencyMs = getNominalLatencyMs(*(maxSampleRateIt->second));
986 _aidl_return->minimumStreamBufferSizeFrames =
987 calculateBufferSizeFrames(latencyMs, maxSampleRateIt->first);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000988 _aidl_return->latenciesMs.clear();
989 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
Mikhail Naganov13501872023-10-18 16:15:46 -0700990 _aidl_return->sinkPortConfigIds.size(), latencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000991 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000992 if (existing == patches.end()) {
993 _aidl_return->id = getConfig().nextPatchId++;
994 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000995 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000996 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -0700997 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000998 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700999 patchesBackup = mPatches;
1000 registerPatch(*_aidl_return);
1001 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
1002 mPatches = std::move(*patchesBackup);
1003 if (existing == patches.end()) {
1004 patches.pop_back();
1005 } else {
1006 *existing = oldPatch;
1007 }
1008 return status;
1009 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001010
1011 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
1012 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001013 return ndk::ScopedAStatus::ok();
1014}
1015
1016ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
1017 AudioPortConfig* out_suggested, bool* _aidl_return) {
1018 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
1019 auto& configs = getConfig().portConfigs;
1020 auto existing = configs.end();
1021 if (in_requested.id != 0) {
1022 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1023 existing == configs.end()) {
1024 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
1025 << " not found";
1026 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1027 }
1028 }
1029
1030 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1031 if (portId == 0) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001032 LOG(ERROR) << __func__ << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001033 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1034 }
1035 auto& ports = getConfig().ports;
1036 auto portIt = findById<AudioPort>(ports, portId);
1037 if (portIt == ports.end()) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001038 LOG(ERROR) << __func__ << ": requested port config points to non-existent portId "
1039 << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001040 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1041 }
1042 if (existing != configs.end()) {
1043 *out_suggested = *existing;
1044 } else {
1045 AudioPortConfig newConfig;
1046 if (generateDefaultPortConfig(*portIt, &newConfig)) {
1047 *out_suggested = newConfig;
1048 } else {
1049 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
1050 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1051 }
1052 }
1053 // From this moment, 'out_suggested' is either an existing port config,
1054 // or a new generated config. Now attempt to update it according to the specified
1055 // fields of 'in_requested'.
1056
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001057 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1058 // which takes care of their actual configuration automatically.
1059 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1060 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001061 bool requestedIsValid = true, requestedIsFullySpecified = true;
1062
1063 AudioIoFlags portFlags = portIt->flags;
1064 if (in_requested.flags.has_value()) {
1065 if (in_requested.flags.value() != portFlags) {
1066 LOG(WARNING) << __func__ << ": requested flags "
1067 << in_requested.flags.value().toString() << " do not match port's "
1068 << portId << " flags " << portFlags.toString();
1069 requestedIsValid = false;
1070 }
1071 } else {
1072 requestedIsFullySpecified = false;
1073 }
1074
1075 AudioProfile portProfile;
1076 if (in_requested.format.has_value()) {
1077 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001078 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1079 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001080 out_suggested->format = format;
1081 } else {
1082 LOG(WARNING) << __func__ << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001083 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001084 requestedIsValid = false;
1085 }
1086 } else {
1087 requestedIsFullySpecified = false;
1088 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001089 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1090 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001091 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
1092 << out_suggested->format.value().toString() << " anymore";
1093 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1094 }
1095
1096 if (in_requested.channelMask.has_value()) {
1097 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001098 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1099 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1100 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001101 out_suggested->channelMask = channelMask;
1102 } else {
1103 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
1104 << " is not supported for the format " << portProfile.format.toString()
1105 << " by the port " << portId;
1106 requestedIsValid = false;
1107 }
1108 } else {
1109 requestedIsFullySpecified = false;
1110 }
1111
1112 if (in_requested.sampleRate.has_value()) {
1113 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001114 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1115 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001116 sampleRate.value) != portProfile.sampleRates.end()) {
1117 out_suggested->sampleRate = sampleRate;
1118 } else {
1119 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
1120 << " is not supported for the format " << portProfile.format.toString()
1121 << " by the port " << portId;
1122 requestedIsValid = false;
1123 }
1124 } else {
1125 requestedIsFullySpecified = false;
1126 }
1127
1128 if (in_requested.gain.has_value()) {
1129 // Let's pretend that gain can always be applied.
1130 out_suggested->gain = in_requested.gain.value();
1131 }
1132
Mikhail Naganov248e9502023-02-21 16:32:40 -08001133 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1134 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1135 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
1136 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
1137 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
1138 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
1139 }
1140 } else {
1141 LOG(WARNING) << __func__ << ": requested ext tag "
1142 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1143 << toString(out_suggested->ext.getTag());
1144 requestedIsValid = false;
1145 }
1146 }
1147
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001148 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1149 out_suggested->id = getConfig().nextPortId++;
1150 configs.push_back(*out_suggested);
1151 *_aidl_return = true;
1152 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
1153 } else if (existing != configs.end() && requestedIsValid) {
1154 *existing = *out_suggested;
1155 *_aidl_return = true;
1156 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
1157 } else {
1158 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
1159 << "; requested is valid? " << requestedIsValid << ", fully specified? "
1160 << requestedIsFullySpecified;
1161 *_aidl_return = false;
1162 }
1163 return ndk::ScopedAStatus::ok();
1164}
1165
1166ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1167 auto& patches = getConfig().patches;
1168 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1169 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001170 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001171 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001172 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1173 mPatches = std::move(patchesBackup);
1174 return status;
1175 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001176 patches.erase(patchIt);
1177 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1178 return ndk::ScopedAStatus::ok();
1179 }
1180 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1181 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1182}
1183
1184ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1185 auto& configs = getConfig().portConfigs;
1186 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1187 if (configIt != configs.end()) {
1188 if (mStreams.count(in_portConfigId) != 0) {
1189 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1190 << " has a stream opened on it";
1191 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1192 }
1193 auto patchIt = mPatches.find(in_portConfigId);
1194 if (patchIt != mPatches.end()) {
1195 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1196 << " is used by the patch with id " << patchIt->second;
1197 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1198 }
1199 auto& initials = getConfig().initialConfigs;
1200 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1201 if (initialIt == initials.end()) {
1202 configs.erase(configIt);
1203 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1204 } else if (*configIt != *initialIt) {
1205 *configIt = *initialIt;
1206 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1207 }
1208 return ndk::ScopedAStatus::ok();
1209 }
1210 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1211 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1212}
1213
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001214ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1215 *_aidl_return = mMasterMute;
1216 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1217 return ndk::ScopedAStatus::ok();
1218}
1219
1220ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1221 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001222 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1223 : onMasterMuteChanged(in_mute);
1224 if (result.isOk()) {
1225 mMasterMute = in_mute;
1226 } else {
1227 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1228 << "), error=" << result;
1229 // Reset master mute if it failed.
1230 onMasterMuteChanged(mMasterMute);
1231 }
1232 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001233}
1234
1235ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1236 *_aidl_return = mMasterVolume;
1237 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1238 return ndk::ScopedAStatus::ok();
1239}
1240
1241ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1242 LOG(DEBUG) << __func__ << ": " << in_volume;
1243 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001244 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1245 : onMasterVolumeChanged(in_volume);
1246 if (result.isOk()) {
1247 mMasterVolume = in_volume;
1248 } else {
1249 // Reset master volume if it failed.
1250 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1251 << "), error=" << result;
1252 onMasterVolumeChanged(mMasterVolume);
1253 }
1254 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001255 }
1256 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1257 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1258}
1259
1260ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1261 *_aidl_return = mMicMute;
1262 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1263 return ndk::ScopedAStatus::ok();
1264}
1265
1266ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1267 LOG(DEBUG) << __func__ << ": " << in_mute;
1268 mMicMute = in_mute;
1269 return ndk::ScopedAStatus::ok();
1270}
1271
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001272ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001273 *_aidl_return = getMicrophoneInfos();
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001274 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1275 return ndk::ScopedAStatus::ok();
1276}
1277
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001278ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001279 if (!isValidAudioMode(in_mode)) {
1280 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1281 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1282 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001283 // No checks for supported audio modes here, it's an informative notification.
1284 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1285 return ndk::ScopedAStatus::ok();
1286}
1287
1288ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1289 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1290 return ndk::ScopedAStatus::ok();
1291}
1292
1293ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1294 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1295 return ndk::ScopedAStatus::ok();
1296}
1297
Vlad Popa83a6d822022-11-07 13:53:57 +01001298ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001299 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001300 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001301 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001302 *_aidl_return = mSoundDose.getInstance();
Vlad Popa943b7e22022-12-08 14:24:12 +01001303 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001304 return ndk::ScopedAStatus::ok();
1305}
1306
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001307ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1308 LOG(DEBUG) << __func__;
1309 (void)_aidl_return;
1310 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1311}
1312
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001313const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001314const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001315
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001316ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1317 std::vector<VendorParameter>* _aidl_return) {
1318 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001319 bool allParametersKnown = true;
1320 for (const auto& id : in_ids) {
1321 if (id == VendorDebug::kForceTransientBurstName) {
1322 VendorParameter forceTransientBurst{.id = id};
1323 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1324 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001325 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1326 VendorParameter forceSynchronousDrain{.id = id};
1327 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1328 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001329 } else {
1330 allParametersKnown = false;
1331 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1332 }
1333 }
1334 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1335 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001336}
1337
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001338namespace {
1339
1340template <typename W>
1341bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1342 std::optional<W> value;
1343 binder_status_t result = p.ext.getParcelable(&value);
1344 if (result == STATUS_OK && value.has_value()) {
1345 *v = value.value().value;
1346 return true;
1347 }
1348 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1349 << "\": " << result;
1350 return false;
1351}
1352
1353} // namespace
1354
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001355ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1356 bool in_async) {
1357 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1358 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001359 bool allParametersKnown = true;
1360 for (const auto& p : in_parameters) {
1361 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001362 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1363 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1364 }
1365 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1366 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001367 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1368 }
1369 } else {
1370 allParametersKnown = false;
1371 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1372 }
1373 }
1374 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1375 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001376}
1377
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001378ndk::ScopedAStatus Module::addDeviceEffect(
1379 int32_t in_portConfigId,
1380 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1381 if (in_effect == nullptr) {
1382 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1383 } else {
1384 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1385 << in_effect->asBinder().get();
1386 }
1387 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1388}
1389
1390ndk::ScopedAStatus Module::removeDeviceEffect(
1391 int32_t in_portConfigId,
1392 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1393 if (in_effect == nullptr) {
1394 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1395 } else {
1396 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1397 << in_effect->asBinder().get();
1398 }
1399 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1400}
1401
jiabin9a8e6862023-01-12 23:06:37 +00001402ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1403 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1404 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1405 std::set<int32_t> mmapSinks;
1406 std::set<int32_t> mmapSources;
1407 auto& ports = getConfig().ports;
1408 for (const auto& port : ports) {
1409 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1410 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1411 AudioInputFlags::MMAP_NOIRQ)) {
1412 mmapSinks.insert(port.id);
1413 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1414 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1415 AudioOutputFlags::MMAP_NOIRQ)) {
1416 mmapSources.insert(port.id);
1417 }
1418 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001419 if (mmapSources.empty() && mmapSinks.empty()) {
1420 AudioMMapPolicyInfo never;
1421 never.mmapPolicy = AudioMMapPolicy::NEVER;
1422 _aidl_return->push_back(never);
1423 return ndk::ScopedAStatus::ok();
1424 }
jiabin9a8e6862023-01-12 23:06:37 +00001425 for (const auto& route : getConfig().routes) {
1426 if (mmapSinks.count(route.sinkPortId) != 0) {
1427 // The sink is a mix port, add the sources if they are device ports.
1428 for (int sourcePortId : route.sourcePortIds) {
1429 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1430 if (sourcePortIt == ports.end()) {
1431 // This must not happen
1432 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1433 continue;
1434 }
1435 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1436 // The source is not a device port, skip
1437 continue;
1438 }
1439 AudioMMapPolicyInfo policyInfo;
1440 policyInfo.device = sourcePortIt->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 } else {
1447 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1448 if (sinkPortIt == ports.end()) {
1449 // This must not happen
1450 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1451 continue;
1452 }
1453 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1454 // The sink is not a device port, skip
1455 continue;
1456 }
1457 if (count_any(mmapSources, route.sourcePortIds)) {
1458 AudioMMapPolicyInfo policyInfo;
1459 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1460 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1461 // default implementation.
1462 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1463 _aidl_return->push_back(policyInfo);
1464 }
1465 }
1466 }
1467 return ndk::ScopedAStatus::ok();
1468}
1469
Eric Laurente2432ea2023-01-12 17:47:31 +01001470ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1471 LOG(DEBUG) << __func__;
1472 *_aidl_return = false;
1473 return ndk::ScopedAStatus::ok();
1474}
1475
jiabinb76981e2023-01-18 00:58:30 +00001476ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1477 if (!isMmapSupported()) {
1478 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1479 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1480 }
1481 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1482 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1483 return ndk::ScopedAStatus::ok();
1484}
1485
1486ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1487 if (!isMmapSupported()) {
1488 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1489 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1490 }
1491 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1492 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1493 return ndk::ScopedAStatus::ok();
1494}
1495
1496bool Module::isMmapSupported() {
1497 if (mIsMmapSupported.has_value()) {
1498 return mIsMmapSupported.value();
1499 }
1500 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1501 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1502 mIsMmapSupported = false;
1503 } else {
1504 mIsMmapSupported =
1505 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1506 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1507 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1508 }) != mmapPolicyInfos.end();
1509 }
1510 return mIsMmapSupported.value();
1511}
1512
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001513ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort) {
1514 if (audioPort->ext.getTag() != AudioPortExt::device) {
1515 LOG(ERROR) << __func__ << ": not a device port: " << audioPort->toString();
1516 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1517 }
1518 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1519 if (!devicePort.device.type.connection.empty()) {
1520 LOG(ERROR) << __func__
1521 << ": module implementation must override 'populateConnectedDevicePort' "
1522 << "to handle connection of external devices.";
1523 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1524 }
jiabin116d8392023-03-01 22:52:57 +00001525 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001526 return ndk::ScopedAStatus::ok();
1527}
1528
1529ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1530 const std::vector<AudioPortConfig*>& sources __unused,
1531 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001532 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001533 return ndk::ScopedAStatus::ok();
1534}
1535
jiabin783c48b2023-02-28 18:28:06 +00001536void Module::onExternalDeviceConnectionChanged(
1537 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1538 bool connected __unused) {
1539 LOG(DEBUG) << __func__ << ": do nothing and return";
1540}
1541
1542ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1543 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1544 return ndk::ScopedAStatus::ok();
1545}
1546
1547ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1548 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1549 return ndk::ScopedAStatus::ok();
1550}
1551
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001552std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1553 std::vector<MicrophoneInfo> result;
1554 Configuration& config = getConfig();
1555 for (const AudioPort& port : config.ports) {
1556 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1557 const AudioDeviceType deviceType =
1558 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1559 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1560 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1561 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1562 // accordingly based on their physical microphone parameters.
1563 result.push_back(MicrophoneInfo{
1564 .id = port.name,
1565 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1566 .group = 0,
1567 .indexInTheGroup = 0,
1568 });
1569 }
1570 }
1571 }
1572 return result;
1573}
1574
Ram Mohan18f0d512023-07-01 00:47:09 +05301575Module::BtProfileHandles Module::getBtProfileManagerHandles() {
1576 return std::make_tuple(std::weak_ptr<IBluetooth>(), std::weak_ptr<IBluetoothA2dp>(),
1577 std::weak_ptr<IBluetoothLe>());
1578}
1579
1580ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1581 return mStreams.bluetoothParametersUpdated();
1582}
1583
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001584} // namespace aidl::android::hardware::audio::core