blob: b8e1df809af064ec55489cc4ef59a5cccf643aaa [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
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000096bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
97 AudioProfile* profile) {
98 if (auto profilesIt =
99 find_if(port.profiles.begin(), port.profiles.end(),
100 [&format](const auto& profile) { return profile.format == format; });
101 profilesIt != port.profiles.end()) {
102 *profile = *profilesIt;
103 return true;
104 }
105 return false;
106}
Mikhail Naganov00603d12022-05-02 22:52:13 +0000107
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000108} // namespace
109
jiabin253bd322023-01-25 23:57:31 +0000110// static
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000111std::shared_ptr<Module> Module::createInstance(Type type, std::unique_ptr<Configuration>&& config) {
jiabin253bd322023-01-25 23:57:31 +0000112 switch (type) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530113 case Type::DEFAULT:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000114 return ndk::SharedRefBase::make<ModulePrimary>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700115 case Type::R_SUBMIX:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000116 return ndk::SharedRefBase::make<ModuleRemoteSubmix>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700117 case Type::STUB:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000118 return ndk::SharedRefBase::make<ModuleStub>(std::move(config));
Mikhail Naganov521fc492023-07-11 17:24:08 -0700119 case Type::USB:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000120 return ndk::SharedRefBase::make<ModuleUsb>(std::move(config));
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700121 case Type::BLUETOOTH:
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000122 return ndk::SharedRefBase::make<ModuleBluetooth>(std::move(config));
jiabin253bd322023-01-25 23:57:31 +0000123 }
124}
125
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000126// static
127std::optional<Module::Type> Module::typeFromString(const std::string& type) {
128 if (type == "default")
129 return Module::Type::DEFAULT;
130 else if (type == "r_submix")
131 return Module::Type::R_SUBMIX;
132 else if (type == "stub")
133 return Module::Type::STUB;
134 else if (type == "usb")
135 return Module::Type::USB;
136 else if (type == "bluetooth")
137 return Module::Type::BLUETOOTH;
138 return {};
139}
140
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700141std::ostream& operator<<(std::ostream& os, Module::Type t) {
142 switch (t) {
143 case Module::Type::DEFAULT:
144 os << "default";
145 break;
146 case Module::Type::R_SUBMIX:
147 os << "r_submix";
148 break;
Mikhail Naganov521fc492023-07-11 17:24:08 -0700149 case Module::Type::STUB:
150 os << "stub";
151 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700152 case Module::Type::USB:
153 os << "usb";
154 break;
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700155 case Module::Type::BLUETOOTH:
156 os << "bluetooth";
157 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700158 }
159 return os;
160}
161
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000162Module::Module(Type type, std::unique_ptr<Configuration>&& config)
163 : mType(type), mConfig(std::move(config)) {
164 populateConnectedProfiles();
165}
166
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000167void Module::cleanUpPatch(int32_t patchId) {
168 erase_all_values(mPatches, std::set<int32_t>{patchId});
169}
170
Mikhail Naganov8651b362023-01-06 23:15:27 +0000171ndk::ScopedAStatus Module::createStreamContext(
172 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
173 std::shared_ptr<IStreamCallback> asyncCallback,
174 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000175 if (in_bufferSizeFrames <= 0) {
176 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
177 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
178 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000179 auto& configs = getConfig().portConfigs;
180 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganova92039a2023-12-20 14:27:22 -0800181 const int32_t nominalLatencyMs = getNominalLatencyMs(*portConfigIt);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000182 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000183 // validity of the portConfigId has already been checked.
Mikhail Naganova92039a2023-12-20 14:27:22 -0800184 const int32_t minimumStreamBufferSizeFrames =
185 calculateBufferSizeFrames(nominalLatencyMs, portConfigIt->sampleRate.value().value);
Mikhail Naganov13501872023-10-18 16:15:46 -0700186 if (in_bufferSizeFrames < minimumStreamBufferSizeFrames) {
187 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
188 << ", must be at least " << minimumStreamBufferSizeFrames;
189 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
190 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000191 const size_t frameSize =
192 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
193 if (frameSize == 0) {
194 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
195 << portConfigIt->toString();
196 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
197 }
198 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700199 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000200 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
201 << " frames is too large, maximum size is "
202 << kMaximumStreamBufferSizeBytes / frameSize;
203 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
204 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000205 const auto& flags = portConfigIt->flags.value();
206 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000207 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
208 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000209 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000210 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
211 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000212 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
Mikhail Naganov194daaa2023-01-05 22:34:20 +0000213 mVendorDebug.forceTransientBurst,
214 mVendorDebug.forceSynchronousDrain};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000215 StreamContext temp(
216 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
217 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganov13501872023-10-18 16:15:46 -0700218 portConfigIt->format.value(), portConfigIt->channelMask.value(),
Mikhail Naganova92039a2023-12-20 14:27:22 -0800219 portConfigIt->sampleRate.value().value, flags, nominalLatencyMs,
Mikhail Naganovb42a69e2023-06-16 12:38:25 -0700220 portConfigIt->ext.get<AudioPortExt::mix>().handle,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000221 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov3c8b6ce2023-10-31 11:20:30 -0700222 asyncCallback, outEventCallback,
223 std::weak_ptr<sounddose::StreamDataProcessorInterface>{}, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000224 if (temp.isValid()) {
225 *out_context = std::move(temp);
226 } else {
227 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
228 }
229 } else {
230 // TODO: Implement simulation of MMAP buffer allocation
231 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000232 return ndk::ScopedAStatus::ok();
233}
234
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000235std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
236 std::vector<AudioDevice> result;
237 auto& ports = getConfig().ports;
238 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
239 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
240 auto portIt = findById<AudioPort>(ports, *it);
241 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
242 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
243 }
244 }
245 return result;
246}
247
248std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
249 std::set<int32_t> result;
250 auto patchIdsRange = mPatches.equal_range(portConfigId);
251 auto& patches = getConfig().patches;
252 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
253 auto patchIt = findById<AudioPatch>(patches, it->second);
254 if (patchIt == patches.end()) {
255 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
256 << "not found in the configuration";
257 }
258 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
259 portConfigId) != patchIt->sourcePortConfigIds.end()) {
260 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
261 } else {
262 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
263 }
264 }
265 return result;
266}
267
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000268ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
269 auto& configs = getConfig().portConfigs;
270 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
271 if (portConfigIt == configs.end()) {
272 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
273 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
274 }
275 const int32_t portId = portConfigIt->portId;
276 // In our implementation, configs of mix ports always have unique IDs.
277 CHECK(portId != in_portConfigId);
278 auto& ports = getConfig().ports;
279 auto portIt = findById<AudioPort>(ports, portId);
280 if (portIt == ports.end()) {
281 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
282 << in_portConfigId << " not found";
283 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
284 }
285 if (mStreams.count(in_portConfigId) != 0) {
286 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
287 << " already has a stream opened on it";
288 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
289 }
290 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
291 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
292 << " does not correspond to a mix port";
293 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
294 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700295 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000296 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
297 LOG(ERROR) << __func__ << ": port id " << portId
298 << " has already reached maximum allowed opened stream count: "
299 << maxOpenStreamCount;
300 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
301 }
302 *port = &(*portIt);
303 return ndk::ScopedAStatus::ok();
304}
305
Mikhail Naganova92039a2023-12-20 14:27:22 -0800306bool Module::generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
307 const bool allowDynamicConfig = port.ext.getTag() == AudioPortExt::device;
308 for (const auto& profile : port.profiles) {
309 if (isDynamicProfile(profile)) continue;
310 config->format = profile.format;
311 config->channelMask = *profile.channelMasks.begin();
312 config->sampleRate = Int{.value = *profile.sampleRates.begin()};
313 config->flags = port.flags;
314 config->ext = port.ext;
315 return true;
316 }
317 if (allowDynamicConfig) {
318 config->format = AudioFormatDescription{};
319 config->channelMask = AudioChannelLayout{};
320 config->sampleRate = Int{.value = 0};
321 config->flags = port.flags;
322 config->ext = port.ext;
323 return true;
324 }
325 LOG(ERROR) << __func__ << ": port " << port.id << " only has dynamic profiles";
326 return false;
327}
328
Lorena Torres-Huertaf7492512023-01-14 02:49:41 +0000329void Module::populateConnectedProfiles() {
330 Configuration& config = getConfig();
331 for (const AudioPort& port : config.ports) {
332 if (port.ext.getTag() == AudioPortExt::device) {
333 if (auto devicePort = port.ext.get<AudioPortExt::device>();
334 !devicePort.device.type.connection.empty() && port.profiles.empty()) {
335 if (auto connIt = config.connectedProfiles.find(port.id);
336 connIt == config.connectedProfiles.end()) {
337 config.connectedProfiles.emplace(
338 port.id, internal::getStandard16And24BitPcmAudioProfiles());
339 }
340 }
341 }
342 }
343}
344
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000345template <typename C>
346std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
347 std::set<int32_t> result;
348 auto& portConfigs = getConfig().portConfigs;
349 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
350 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
351 if (portConfigIt != portConfigs.end()) {
352 result.insert(portConfigIt->portId);
353 }
354 }
355 return result;
356}
357
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000358std::unique_ptr<Module::Configuration> Module::initializeConfig() {
359 return internal::getConfiguration(getType());
Peter Yoon918a6a52023-07-13 17:04:37 +0900360}
361
Mikhail Naganov13501872023-10-18 16:15:46 -0700362int32_t Module::getNominalLatencyMs(const AudioPortConfig&) {
363 // Arbitrary value. Implementations must override this method to provide their actual latency.
364 static constexpr int32_t kLatencyMs = 5;
365 return kLatencyMs;
366}
367
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700368std::vector<AudioRoute*> Module::getAudioRoutesForAudioPortImpl(int32_t portId) {
369 std::vector<AudioRoute*> result;
370 auto& routes = getConfig().routes;
371 for (auto& r : routes) {
372 const auto& srcs = r.sourcePortIds;
373 if (r.sinkPortId == portId || std::find(srcs.begin(), srcs.end(), portId) != srcs.end()) {
374 result.push_back(&r);
375 }
376 }
377 return result;
378}
379
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +0000380Module::Configuration& Module::getConfig() {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000381 if (!mConfig) {
Peter Yoon918a6a52023-07-13 17:04:37 +0900382 mConfig = std::move(initializeConfig());
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000383 }
384 return *mConfig;
385}
386
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700387std::set<int32_t> Module::getRoutableAudioPortIds(int32_t portId,
388 std::vector<AudioRoute*>* routes) {
389 std::vector<AudioRoute*> routesStorage;
390 if (routes == nullptr) {
391 routesStorage = getAudioRoutesForAudioPortImpl(portId);
392 routes = &routesStorage;
393 }
394 std::set<int32_t> result;
395 for (AudioRoute* r : *routes) {
396 if (r->sinkPortId == portId) {
397 result.insert(r->sourcePortIds.begin(), r->sourcePortIds.end());
398 } else {
399 result.insert(r->sinkPortId);
400 }
401 }
402 return result;
403}
404
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000405void Module::registerPatch(const AudioPatch& patch) {
406 auto& configs = getConfig().portConfigs;
407 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
408 for (auto portConfigId : portConfigIds) {
409 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
410 if (configIt != configs.end()) {
411 mPatches.insert(std::pair{portConfigId, patch.id});
412 if (configIt->portId != portConfigId) {
413 mPatches.insert(std::pair{configIt->portId, patch.id});
414 }
415 }
416 };
417 };
418 do_insert(patch.sourcePortConfigIds);
419 do_insert(patch.sinkPortConfigIds);
420}
421
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700422ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
423 const AudioPatch& newPatch) {
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700424 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700425 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700426 using Connections =
427 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
428 Connections oldConnections, newConnections;
429 auto fillConnectionsHelper = [&](Connections& connections,
430 const std::vector<int32_t>& mixPortCfgIds,
431 const std::vector<int32_t>& devicePortCfgIds) {
432 for (int32_t mixPortCfgId : mixPortCfgIds) {
433 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
434 }
435 };
436 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
437 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
438 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
439 patch.sourcePortConfigIds.end()) {
440 // Sources are mix ports.
441 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
442 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
443 [&](int32_t portConfigId) {
444 return mStreams.count(portConfigId) > 0;
445 }) != patch.sinkPortConfigIds.end()) {
446 // Sources are device ports.
447 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
448 } // Otherwise, there are no streams to notify.
449 };
450 fillConnections(oldConnections, oldPatch);
451 fillConnections(newConnections, newPatch);
452
453 std::for_each(oldConnections.begin(), oldConnections.end(), [&](const auto& connectionPair) {
454 const int32_t mixPortConfigId = connectionPair.first;
455 if (auto it = newConnections.find(mixPortConfigId);
456 it == newConnections.end() || it->second != connectionPair.second) {
457 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, {});
458 status.isOk()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700459 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700460 << mixPortConfigId << " has been disconnected";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700461 } else {
462 // Disconnection is tricky to roll back, just register a failure.
463 maybeFailure = std::move(status);
464 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000465 }
466 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700467 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700468 std::set<int32_t> idsToDisconnectOnFailure;
469 std::for_each(newConnections.begin(), newConnections.end(), [&](const auto& connectionPair) {
470 const int32_t mixPortConfigId = connectionPair.first;
471 if (auto it = oldConnections.find(mixPortConfigId);
472 it == oldConnections.end() || it->second != connectionPair.second) {
473 const auto connectedDevices = findConnectedDevices(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700474 if (connectedDevices.empty()) {
475 // This is important as workers use the vector size to derive the connection status.
476 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
477 "config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700478 << mixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700479 }
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700480 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700481 status.isOk()) {
482 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700483 << mixPortConfigId << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700484 << ::android::internal::ToString(connectedDevices);
485 } else {
486 maybeFailure = std::move(status);
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700487 idsToDisconnectOnFailure.insert(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700488 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000489 }
490 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700491 if (!maybeFailure.isOk()) {
492 LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids "
493 << ::android::internal::ToString(idsToDisconnectOnFailure);
494 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
495 [&](const auto& portConfigId) {
496 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
497 (void)status.isOk(); // Can't do much about a failure here.
498 });
499 return maybeFailure;
500 }
501 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000502}
503
Mikhail Naganov00603d12022-05-02 22:52:13 +0000504ndk::ScopedAStatus Module::setModuleDebug(
505 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700506 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000507 << ", new flags: " << in_debug.toString();
508 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
509 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700510 LOG(ERROR) << __func__ << ": " << mType
511 << ": attempting to change device connections simulation while having external "
512 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000513 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
514 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000515 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700516 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000517 << in_debug.streamTransientStateDelayMs;
518 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
519 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000520 mDebug = in_debug;
521 return ndk::ScopedAStatus::ok();
522}
523
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000524ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700525 *_aidl_return = nullptr;
526 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000527 return ndk::ScopedAStatus::ok();
528}
529
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000530ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700531 *_aidl_return = nullptr;
532 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000533 return ndk::ScopedAStatus::ok();
534}
535
Mikhail Naganov7499a002023-02-27 18:51:44 -0800536ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700537 *_aidl_return = nullptr;
538 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800539 return ndk::ScopedAStatus::ok();
540}
541
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800542ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700543 *_aidl_return = nullptr;
544 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800545 return ndk::ScopedAStatus::ok();
546}
547
Mikhail Naganov00603d12022-05-02 22:52:13 +0000548ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
549 AudioPort* _aidl_return) {
550 const int32_t templateId = in_templateIdAndAdditionalData.id;
551 auto& ports = getConfig().ports;
552 AudioPort connectedPort;
553 { // Scope the template port so that we don't accidentally modify it.
554 auto templateIt = findById<AudioPort>(ports, templateId);
555 if (templateIt == ports.end()) {
556 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
557 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
558 }
559 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
560 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
561 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
562 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000563 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
564 if (templateDevicePort.device.type.connection.empty()) {
565 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
566 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
567 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700568 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
569 LOG(ERROR) << __func__ << ": port id " << templateId << " is a connected device port";
570 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
571 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000572 // Postpone id allocation until we ensure that there are no client errors.
573 connectedPort = *templateIt;
574 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
575 const auto& inputDevicePort =
576 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
577 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
578 connectedDevicePort.device.address = inputDevicePort.device.address;
579 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
580 << connectedDevicePort.device.toString();
581 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700582 for (auto connectedPortPair : mConnectedDevicePorts) {
583 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000584 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
585 connectedDevicePort.device) {
586 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700587 << " is already connected at the device port id "
588 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000589 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
590 }
591 }
592 }
593
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700594 // Two main cases are considered with regard to the profiles of the connected device port:
595 //
596 // 1. If the template device port has dynamic profiles, and at least one routable mix
597 // port also has dynamic profiles, it means that after connecting the device, the
598 // connected device port must have profiles populated with actual capabilities of
599 // the connected device, and dynamic of routable mix ports will be filled
600 // according to these capabilities. An example of this case is connection of an
601 // HDMI or USB device. For USB handled by ADSP, there can be mix ports with static
602 // profiles, and one dedicated mix port for "hi-fi" playback. The latter is left with
603 // dynamic profiles so that they can be populated with actual capabilities of
604 // the connected device.
605 //
606 // 2. If the template device port has dynamic profiles, while all routable mix ports
607 // have static profiles, it means that after connecting the device, the connected
608 // device port can be left with dynamic profiles, and profiles of mix ports are
609 // left untouched. An example of this case is connection of an analog wired
610 // headset, it should be treated in the same way as a speaker.
611 //
612 // Yet another possible case is when both the template device port and all routable
613 // mix ports have static profiles. This is allowed and handled correctly, however, it
614 // is not very practical, since these profiles are likely duplicates of each other.
615
616 std::vector<AudioRoute*> routesToMixPorts = getAudioRoutesForAudioPortImpl(templateId);
617 std::set<int32_t> routableMixPortIds = getRoutableAudioPortIds(templateId, &routesToMixPorts);
Mikhail Naganova92039a2023-12-20 14:27:22 -0800618 const int32_t nextPortId = getConfig().nextPortId++;
Mikhail Naganov55045b52023-10-24 17:03:50 -0700619 if (!mDebug.simulateDeviceConnections) {
620 // Even if the device port has static profiles, the HAL module might need to update
621 // them, or abort the connection process.
Mikhail Naganova92039a2023-12-20 14:27:22 -0800622 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort, nextPortId));
Mikhail Naganov55045b52023-10-24 17:03:50 -0700623 } else if (hasDynamicProfilesOnly(connectedPort.profiles)) {
624 auto& connectedProfiles = getConfig().connectedProfiles;
625 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
626 connectedProfilesIt != connectedProfiles.end()) {
627 connectedPort.profiles = connectedProfilesIt->second;
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700628 }
Mikhail Naganov55045b52023-10-24 17:03:50 -0700629 }
630 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) > 0 &&
635 hasDynamicProfilesOnly(p.profiles);
636 });
637 dynamicMixPortIt != ports.end()) {
638 LOG(ERROR) << __func__ << ": connected port only has dynamic profiles after connecting "
639 << "external device " << connectedPort.toString() << ", and there exist "
640 << "a routable mix port with dynamic profiles: "
641 << dynamicMixPortIt->toString();
642 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530643 }
644 }
645
Mikhail Naganova92039a2023-12-20 14:27:22 -0800646 connectedPort.id = nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700647 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700648 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000649 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
650 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000651 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000652 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000653
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700654 // For routes where the template port is a source, add the connected port to sources,
655 // otherwise, create a new route by copying from the route for the template port.
Mikhail Naganov00603d12022-05-02 22:52:13 +0000656 std::vector<AudioRoute> newRoutes;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700657 for (AudioRoute* r : routesToMixPorts) {
658 if (r->sinkPortId == templateId) {
659 newRoutes.push_back(AudioRoute{.sourcePortIds = r->sourcePortIds,
660 .sinkPortId = connectedPort.id,
661 .isExclusive = r->isExclusive});
Mikhail Naganov00603d12022-05-02 22:52:13 +0000662 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700663 r->sourcePortIds.push_back(connectedPort.id);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000664 }
665 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700666 auto& routes = getConfig().routes;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000667 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
668
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700669 if (!hasDynamicProfilesOnly(connectedPort.profiles) && !routableMixPortIds.empty()) {
670 // Note: this is a simplistic approach assuming that a mix port can only be populated
671 // from a single device port. Implementing support for stuffing dynamic profiles with
672 // a superset of all profiles from all routable dynamic device ports would be more involved.
673 for (auto& port : ports) {
674 if (routableMixPortIds.count(port.id) == 0) continue;
675 if (hasDynamicProfilesOnly(port.profiles)) {
676 port.profiles = connectedPort.profiles;
677 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700678 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700679 // Check if profiles are not all dynamic because they were populated by
680 // a previous connection. Otherwise, it means that they are actually static.
681 for (const auto& cp : mConnectedDevicePorts) {
682 if (cp.second.count(port.id) > 0) {
683 connectedPortsIt->second.insert(port.id);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700684 break;
685 }
686 }
687 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700688 }
689 }
690 *_aidl_return = std::move(connectedPort);
691
Mikhail Naganov00603d12022-05-02 22:52:13 +0000692 return ndk::ScopedAStatus::ok();
693}
694
695ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
696 auto& ports = getConfig().ports;
697 auto portIt = findById<AudioPort>(ports, in_portId);
698 if (portIt == ports.end()) {
699 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
700 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
701 }
702 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
703 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
704 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
705 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700706 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
707 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000708 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
709 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
710 }
711 auto& configs = getConfig().portConfigs;
712 auto& initials = getConfig().initialConfigs;
713 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
714 if (config.portId == in_portId) {
715 // Check if the configuration was provided by the client.
716 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
717 return initialIt == initials.end() || config != *initialIt;
718 }
719 return false;
720 });
721 if (configIt != configs.end()) {
722 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
723 << configIt->id;
724 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
725 }
jiabin783c48b2023-02-28 18:28:06 +0000726 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000727 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000728 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
729
730 auto& routes = getConfig().routes;
731 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
732 if (routesIt->sinkPortId == in_portId) {
733 routesIt = routes.erase(routesIt);
734 } else {
735 // Note: the list of sourcePortIds can't become empty because there must
736 // be the id of the template port in the route.
737 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
738 ++routesIt;
739 }
740 }
741
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700742 // Clear profiles for mix ports that are not connected to any other ports.
743 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
744 mConnectedDevicePorts.erase(connectedPortsIt);
745 for (const auto& connectedPort : mConnectedDevicePorts) {
746 for (int32_t mixPortId : connectedPort.second) {
747 mixPortsToClear.erase(mixPortId);
748 }
749 }
750 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700751 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
752 if (mixPortIt != ports.end()) {
753 mixPortIt->profiles = {};
754 }
755 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700756
Mikhail Naganov00603d12022-05-02 22:52:13 +0000757 return ndk::ScopedAStatus::ok();
758}
759
jiabindd23b0e2023-12-11 19:10:05 +0000760ndk::ScopedAStatus Module::prepareToDisconnectExternalDevice(int32_t in_portId) {
761 auto& ports = getConfig().ports;
762 auto portIt = findById<AudioPort>(ports, in_portId);
763 if (portIt == ports.end()) {
764 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
765 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
766 }
767 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
768 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
769 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
770 }
771 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
772 if (connectedPortsIt == mConnectedDevicePorts.end()) {
773 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
774 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
775 }
776
777 onPrepareToDisconnectExternalDevice(*portIt);
778
779 return ndk::ScopedAStatus::ok();
780}
781
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000782ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
783 *_aidl_return = getConfig().patches;
784 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
785 return ndk::ScopedAStatus::ok();
786}
787
788ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
789 auto& ports = getConfig().ports;
790 auto portIt = findById<AudioPort>(ports, in_portId);
791 if (portIt != ports.end()) {
792 *_aidl_return = *portIt;
793 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
794 return ndk::ScopedAStatus::ok();
795 }
796 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
797 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
798}
799
800ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
801 *_aidl_return = getConfig().portConfigs;
802 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
803 return ndk::ScopedAStatus::ok();
804}
805
806ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
807 *_aidl_return = getConfig().ports;
808 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
809 return ndk::ScopedAStatus::ok();
810}
811
812ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
813 *_aidl_return = getConfig().routes;
814 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
815 return ndk::ScopedAStatus::ok();
816}
817
Mikhail Naganov00603d12022-05-02 22:52:13 +0000818ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
819 std::vector<AudioRoute>* _aidl_return) {
820 auto& ports = getConfig().ports;
821 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
822 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
823 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
824 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700825 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(in_portId);
826 std::transform(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
827 [](auto rptr) { return *rptr; });
Mikhail Naganov00603d12022-05-02 22:52:13 +0000828 return ndk::ScopedAStatus::ok();
829}
830
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000831ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
832 OpenInputStreamReturn* _aidl_return) {
833 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
834 << in_args.bufferSizeFrames << " frames";
835 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700836 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000837 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
838 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000839 << " does not correspond to an input mix port";
840 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
841 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000842 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700843 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
844 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000845 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000846 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700847 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +0000848 getMicrophoneInfos(), &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000849 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700850 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
851 RETURN_STATUS_IF_ERROR(
852 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
853 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000854 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
855 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000856 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000857 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000858 return ndk::ScopedAStatus::ok();
859}
860
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000861ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
862 OpenOutputStreamReturn* _aidl_return) {
863 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
864 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
865 << " frames";
866 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700867 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000868 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
869 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000870 << " does not correspond to an output mix port";
871 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
872 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000873 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
874 AudioOutputFlags::COMPRESS_OFFLOAD);
875 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000876 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000877 << " has COMPRESS_OFFLOAD flag set, requires offload info";
878 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
879 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000880 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
881 AudioOutputFlags::NON_BLOCKING);
882 if (isNonBlocking && in_args.callback == nullptr) {
883 LOG(ERROR) << __func__ << ": port id " << port->id
884 << " has NON_BLOCKING flag set, requires async callback";
885 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
886 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000887 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700888 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
889 isNonBlocking ? in_args.callback : nullptr,
890 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000891 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000892 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700893 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700894 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000895 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700896 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
897 RETURN_STATUS_IF_ERROR(
898 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
899 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000900 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
901 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000902 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000903 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000904 return ndk::ScopedAStatus::ok();
905}
906
Mikhail Naganov74927202022-12-19 16:37:14 +0000907ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
908 SupportedPlaybackRateFactors* _aidl_return) {
909 LOG(DEBUG) << __func__;
910 (void)_aidl_return;
911 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
912}
913
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000914ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000915 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000916 if (in_requested.sourcePortConfigIds.empty()) {
917 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
918 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
919 }
920 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
921 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
922 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
923 }
924 if (in_requested.sinkPortConfigIds.empty()) {
925 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
926 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
927 }
928 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
929 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
930 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
931 }
932
933 auto& configs = getConfig().portConfigs;
934 std::vector<int32_t> missingIds;
935 auto sources =
936 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
937 if (!missingIds.empty()) {
938 LOG(ERROR) << __func__ << ": following source port config ids not found: "
939 << ::android::internal::ToString(missingIds);
940 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
941 }
942 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
943 if (!missingIds.empty()) {
944 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
945 << ::android::internal::ToString(missingIds);
946 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
947 }
948 // bool indicates whether a non-exclusive route is available.
949 // If only an exclusive route is available, that means the patch can not be
950 // established if there is any other patch which currently uses the sink port.
951 std::map<int32_t, bool> allowedSinkPorts;
952 auto& routes = getConfig().routes;
953 for (auto src : sources) {
954 for (const auto& r : routes) {
955 const auto& srcs = r.sourcePortIds;
956 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
957 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
958 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
959 }
960 }
961 }
962 }
963 for (auto sink : sinks) {
964 if (allowedSinkPorts.count(sink->portId) == 0) {
965 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
966 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
967 }
968 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700969 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000970
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000971 auto& patches = getConfig().patches;
972 auto existing = patches.end();
973 std::optional<decltype(mPatches)> patchesBackup;
974 if (in_requested.id != 0) {
975 existing = findById<AudioPatch>(patches, in_requested.id);
976 if (existing != patches.end()) {
977 patchesBackup = mPatches;
978 cleanUpPatch(existing->id);
979 } else {
980 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
981 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
982 }
983 }
984 // Validate the requested patch.
985 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
986 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
987 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
988 << "is exclusive and is already used by some other patch";
989 if (patchesBackup.has_value()) {
990 mPatches = std::move(*patchesBackup);
991 }
992 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
993 }
994 }
Mikhail Naganov13501872023-10-18 16:15:46 -0700995 // Find the highest sample rate among mix port configs.
996 std::map<int32_t, AudioPortConfig*> sampleRates;
997 std::vector<AudioPortConfig*>& mixPortConfigs =
998 sources[0]->ext.getTag() == AudioPortExt::mix ? sources : sinks;
999 for (auto mix : mixPortConfigs) {
1000 sampleRates.emplace(mix->sampleRate.value().value, mix);
1001 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001002 *_aidl_return = in_requested;
Mikhail Naganov13501872023-10-18 16:15:46 -07001003 auto maxSampleRateIt = std::max_element(sampleRates.begin(), sampleRates.end());
1004 const int32_t latencyMs = getNominalLatencyMs(*(maxSampleRateIt->second));
1005 _aidl_return->minimumStreamBufferSizeFrames =
1006 calculateBufferSizeFrames(latencyMs, maxSampleRateIt->first);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +00001007 _aidl_return->latenciesMs.clear();
1008 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
Mikhail Naganov13501872023-10-18 16:15:46 -07001009 _aidl_return->sinkPortConfigIds.size(), latencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001010 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001011 if (existing == patches.end()) {
1012 _aidl_return->id = getConfig().nextPatchId++;
1013 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001014 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001015 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -07001016 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001017 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001018 patchesBackup = mPatches;
1019 registerPatch(*_aidl_return);
1020 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
1021 mPatches = std::move(*patchesBackup);
1022 if (existing == patches.end()) {
1023 patches.pop_back();
1024 } else {
1025 *existing = oldPatch;
1026 }
1027 return status;
1028 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +00001029
1030 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
1031 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001032 return ndk::ScopedAStatus::ok();
1033}
1034
1035ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
1036 AudioPortConfig* out_suggested, bool* _aidl_return) {
Mikhail Naganova92039a2023-12-20 14:27:22 -08001037 auto generate = [this](const AudioPort& port, AudioPortConfig* config) {
1038 return generateDefaultPortConfig(port, config);
1039 };
1040 return setAudioPortConfigImpl(in_requested, generate, out_suggested, _aidl_return);
1041}
1042
1043ndk::ScopedAStatus Module::setAudioPortConfigImpl(
1044 const AudioPortConfig& in_requested,
1045 const std::function<bool(const ::aidl::android::media::audio::common::AudioPort& port,
1046 ::aidl::android::media::audio::common::AudioPortConfig* config)>&
1047 fillPortConfig,
1048 AudioPortConfig* out_suggested, bool* applied) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001049 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
1050 auto& configs = getConfig().portConfigs;
1051 auto existing = configs.end();
1052 if (in_requested.id != 0) {
1053 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
1054 existing == configs.end()) {
1055 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
1056 << " not found";
1057 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1058 }
1059 }
1060
1061 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
1062 if (portId == 0) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001063 LOG(ERROR) << __func__ << ": requested port config does not specify portId";
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001064 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1065 }
1066 auto& ports = getConfig().ports;
1067 auto portIt = findById<AudioPort>(ports, portId);
1068 if (portIt == ports.end()) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001069 LOG(ERROR) << __func__ << ": requested port config points to non-existent portId "
1070 << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001071 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1072 }
1073 if (existing != configs.end()) {
1074 *out_suggested = *existing;
1075 } else {
1076 AudioPortConfig newConfig;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001077 newConfig.portId = portIt->id;
1078 if (fillPortConfig(*portIt, &newConfig)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001079 *out_suggested = newConfig;
1080 } else {
1081 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
1082 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1083 }
1084 }
1085 // From this moment, 'out_suggested' is either an existing port config,
1086 // or a new generated config. Now attempt to update it according to the specified
1087 // fields of 'in_requested'.
1088
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001089 // Device ports with only dynamic profiles are used for devices that are connected via ADSP,
1090 // which takes care of their actual configuration automatically.
1091 const bool allowDynamicConfig = portIt->ext.getTag() == AudioPortExt::device &&
1092 hasDynamicProfilesOnly(portIt->profiles);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001093 bool requestedIsValid = true, requestedIsFullySpecified = true;
1094
1095 AudioIoFlags portFlags = portIt->flags;
1096 if (in_requested.flags.has_value()) {
1097 if (in_requested.flags.value() != portFlags) {
1098 LOG(WARNING) << __func__ << ": requested flags "
1099 << in_requested.flags.value().toString() << " do not match port's "
1100 << portId << " flags " << portFlags.toString();
1101 requestedIsValid = false;
1102 }
1103 } else {
1104 requestedIsFullySpecified = false;
1105 }
1106
1107 AudioProfile portProfile;
1108 if (in_requested.format.has_value()) {
1109 const auto& format = in_requested.format.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001110 if ((format == AudioFormatDescription{} && allowDynamicConfig) ||
1111 findAudioProfile(*portIt, format, &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001112 out_suggested->format = format;
1113 } else {
1114 LOG(WARNING) << __func__ << ": requested format " << format.toString()
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001115 << " is not found in the profiles of port " << portId;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001116 requestedIsValid = false;
1117 }
1118 } else {
1119 requestedIsFullySpecified = false;
1120 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001121 if (!(out_suggested->format.value() == AudioFormatDescription{} && allowDynamicConfig) &&
1122 !findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001123 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
1124 << out_suggested->format.value().toString() << " anymore";
1125 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1126 }
1127
1128 if (in_requested.channelMask.has_value()) {
1129 const auto& channelMask = in_requested.channelMask.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001130 if ((channelMask == AudioChannelLayout{} && allowDynamicConfig) ||
1131 find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
1132 portProfile.channelMasks.end()) {
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001133 out_suggested->channelMask = channelMask;
1134 } else {
1135 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
1136 << " is not supported for the format " << portProfile.format.toString()
1137 << " by the port " << portId;
1138 requestedIsValid = false;
1139 }
1140 } else {
1141 requestedIsFullySpecified = false;
1142 }
1143
1144 if (in_requested.sampleRate.has_value()) {
1145 const auto& sampleRate = in_requested.sampleRate.value();
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001146 if ((sampleRate.value == 0 && allowDynamicConfig) ||
1147 find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001148 sampleRate.value) != portProfile.sampleRates.end()) {
1149 out_suggested->sampleRate = sampleRate;
1150 } else {
1151 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
1152 << " is not supported for the format " << portProfile.format.toString()
1153 << " by the port " << portId;
1154 requestedIsValid = false;
1155 }
1156 } else {
1157 requestedIsFullySpecified = false;
1158 }
1159
1160 if (in_requested.gain.has_value()) {
1161 // Let's pretend that gain can always be applied.
1162 out_suggested->gain = in_requested.gain.value();
1163 }
1164
Mikhail Naganov248e9502023-02-21 16:32:40 -08001165 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1166 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1167 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
1168 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
1169 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
1170 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
1171 }
1172 } else {
1173 LOG(WARNING) << __func__ << ": requested ext tag "
1174 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1175 << toString(out_suggested->ext.getTag());
1176 requestedIsValid = false;
1177 }
1178 }
1179
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001180 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1181 out_suggested->id = getConfig().nextPortId++;
1182 configs.push_back(*out_suggested);
Mikhail Naganova92039a2023-12-20 14:27:22 -08001183 *applied = true;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001184 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
1185 } else if (existing != configs.end() && requestedIsValid) {
1186 *existing = *out_suggested;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001187 *applied = true;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001188 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
1189 } else {
1190 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
1191 << "; requested is valid? " << requestedIsValid << ", fully specified? "
1192 << requestedIsFullySpecified;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001193 *applied = false;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001194 }
1195 return ndk::ScopedAStatus::ok();
1196}
1197
1198ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1199 auto& patches = getConfig().patches;
1200 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1201 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001202 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001203 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001204 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1205 mPatches = std::move(patchesBackup);
1206 return status;
1207 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001208 patches.erase(patchIt);
1209 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1210 return ndk::ScopedAStatus::ok();
1211 }
1212 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1213 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1214}
1215
1216ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1217 auto& configs = getConfig().portConfigs;
1218 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1219 if (configIt != configs.end()) {
1220 if (mStreams.count(in_portConfigId) != 0) {
1221 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1222 << " has a stream opened on it";
1223 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1224 }
1225 auto patchIt = mPatches.find(in_portConfigId);
1226 if (patchIt != mPatches.end()) {
1227 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1228 << " is used by the patch with id " << patchIt->second;
1229 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1230 }
1231 auto& initials = getConfig().initialConfigs;
1232 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1233 if (initialIt == initials.end()) {
1234 configs.erase(configIt);
1235 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1236 } else if (*configIt != *initialIt) {
1237 *configIt = *initialIt;
1238 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1239 }
1240 return ndk::ScopedAStatus::ok();
1241 }
1242 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1243 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1244}
1245
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001246ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1247 *_aidl_return = mMasterMute;
1248 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1249 return ndk::ScopedAStatus::ok();
1250}
1251
1252ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1253 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001254 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1255 : onMasterMuteChanged(in_mute);
1256 if (result.isOk()) {
1257 mMasterMute = in_mute;
1258 } else {
1259 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1260 << "), error=" << result;
1261 // Reset master mute if it failed.
1262 onMasterMuteChanged(mMasterMute);
1263 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001264 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001265}
1266
1267ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1268 *_aidl_return = mMasterVolume;
1269 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1270 return ndk::ScopedAStatus::ok();
1271}
1272
1273ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1274 LOG(DEBUG) << __func__ << ": " << in_volume;
1275 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001276 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1277 : onMasterVolumeChanged(in_volume);
1278 if (result.isOk()) {
1279 mMasterVolume = in_volume;
1280 } else {
1281 // Reset master volume if it failed.
1282 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1283 << "), error=" << result;
1284 onMasterVolumeChanged(mMasterVolume);
1285 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001286 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001287 }
1288 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1289 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1290}
1291
1292ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1293 *_aidl_return = mMicMute;
1294 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1295 return ndk::ScopedAStatus::ok();
1296}
1297
1298ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1299 LOG(DEBUG) << __func__ << ": " << in_mute;
1300 mMicMute = in_mute;
1301 return ndk::ScopedAStatus::ok();
1302}
1303
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001304ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001305 *_aidl_return = getMicrophoneInfos();
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001306 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1307 return ndk::ScopedAStatus::ok();
1308}
1309
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001310ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001311 if (!isValidAudioMode(in_mode)) {
1312 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1313 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1314 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001315 // No checks for supported audio modes here, it's an informative notification.
1316 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1317 return ndk::ScopedAStatus::ok();
1318}
1319
1320ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1321 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1322 return ndk::ScopedAStatus::ok();
1323}
1324
1325ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1326 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1327 return ndk::ScopedAStatus::ok();
1328}
1329
Vlad Popa83a6d822022-11-07 13:53:57 +01001330ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001331 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001332 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001333 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001334 *_aidl_return = mSoundDose.getInstance();
Vlad Popa943b7e22022-12-08 14:24:12 +01001335 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001336 return ndk::ScopedAStatus::ok();
1337}
1338
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001339ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1340 LOG(DEBUG) << __func__;
1341 (void)_aidl_return;
1342 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1343}
1344
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001345const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001346const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001347
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001348ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1349 std::vector<VendorParameter>* _aidl_return) {
1350 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001351 bool allParametersKnown = true;
1352 for (const auto& id : in_ids) {
1353 if (id == VendorDebug::kForceTransientBurstName) {
1354 VendorParameter forceTransientBurst{.id = id};
1355 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1356 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001357 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1358 VendorParameter forceSynchronousDrain{.id = id};
1359 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1360 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001361 } else {
1362 allParametersKnown = false;
1363 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1364 }
1365 }
1366 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1367 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001368}
1369
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001370namespace {
1371
1372template <typename W>
1373bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1374 std::optional<W> value;
1375 binder_status_t result = p.ext.getParcelable(&value);
1376 if (result == STATUS_OK && value.has_value()) {
1377 *v = value.value().value;
1378 return true;
1379 }
1380 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1381 << "\": " << result;
1382 return false;
1383}
1384
1385} // namespace
1386
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001387ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1388 bool in_async) {
1389 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1390 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001391 bool allParametersKnown = true;
1392 for (const auto& p : in_parameters) {
1393 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001394 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1395 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1396 }
1397 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1398 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001399 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1400 }
1401 } else {
1402 allParametersKnown = false;
1403 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1404 }
1405 }
1406 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1407 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001408}
1409
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001410ndk::ScopedAStatus Module::addDeviceEffect(
1411 int32_t in_portConfigId,
1412 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1413 if (in_effect == nullptr) {
1414 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1415 } else {
1416 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1417 << in_effect->asBinder().get();
1418 }
1419 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1420}
1421
1422ndk::ScopedAStatus Module::removeDeviceEffect(
1423 int32_t in_portConfigId,
1424 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1425 if (in_effect == nullptr) {
1426 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1427 } else {
1428 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1429 << in_effect->asBinder().get();
1430 }
1431 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1432}
1433
jiabin9a8e6862023-01-12 23:06:37 +00001434ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1435 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1436 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1437 std::set<int32_t> mmapSinks;
1438 std::set<int32_t> mmapSources;
1439 auto& ports = getConfig().ports;
1440 for (const auto& port : ports) {
1441 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1442 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1443 AudioInputFlags::MMAP_NOIRQ)) {
1444 mmapSinks.insert(port.id);
1445 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1446 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1447 AudioOutputFlags::MMAP_NOIRQ)) {
1448 mmapSources.insert(port.id);
1449 }
1450 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001451 if (mmapSources.empty() && mmapSinks.empty()) {
1452 AudioMMapPolicyInfo never;
1453 never.mmapPolicy = AudioMMapPolicy::NEVER;
1454 _aidl_return->push_back(never);
1455 return ndk::ScopedAStatus::ok();
1456 }
jiabin9a8e6862023-01-12 23:06:37 +00001457 for (const auto& route : getConfig().routes) {
1458 if (mmapSinks.count(route.sinkPortId) != 0) {
1459 // The sink is a mix port, add the sources if they are device ports.
1460 for (int sourcePortId : route.sourcePortIds) {
1461 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1462 if (sourcePortIt == ports.end()) {
1463 // This must not happen
1464 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1465 continue;
1466 }
1467 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1468 // The source is not a device port, skip
1469 continue;
1470 }
1471 AudioMMapPolicyInfo policyInfo;
1472 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1473 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1474 // default implementation.
1475 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1476 _aidl_return->push_back(policyInfo);
1477 }
1478 } else {
1479 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1480 if (sinkPortIt == ports.end()) {
1481 // This must not happen
1482 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1483 continue;
1484 }
1485 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1486 // The sink is not a device port, skip
1487 continue;
1488 }
1489 if (count_any(mmapSources, route.sourcePortIds)) {
1490 AudioMMapPolicyInfo policyInfo;
1491 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1492 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1493 // default implementation.
1494 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1495 _aidl_return->push_back(policyInfo);
1496 }
1497 }
1498 }
1499 return ndk::ScopedAStatus::ok();
1500}
1501
Eric Laurente2432ea2023-01-12 17:47:31 +01001502ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1503 LOG(DEBUG) << __func__;
1504 *_aidl_return = false;
1505 return ndk::ScopedAStatus::ok();
1506}
1507
jiabinb76981e2023-01-18 00:58:30 +00001508ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1509 if (!isMmapSupported()) {
1510 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1511 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1512 }
1513 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1514 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1515 return ndk::ScopedAStatus::ok();
1516}
1517
1518ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1519 if (!isMmapSupported()) {
1520 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1521 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1522 }
1523 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1524 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1525 return ndk::ScopedAStatus::ok();
1526}
1527
1528bool Module::isMmapSupported() {
1529 if (mIsMmapSupported.has_value()) {
1530 return mIsMmapSupported.value();
1531 }
1532 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1533 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1534 mIsMmapSupported = false;
1535 } else {
1536 mIsMmapSupported =
1537 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1538 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1539 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1540 }) != mmapPolicyInfos.end();
1541 }
1542 return mIsMmapSupported.value();
1543}
1544
Mikhail Naganova92039a2023-12-20 14:27:22 -08001545ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001546 if (audioPort->ext.getTag() != AudioPortExt::device) {
1547 LOG(ERROR) << __func__ << ": not a device port: " << audioPort->toString();
1548 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1549 }
1550 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1551 if (!devicePort.device.type.connection.empty()) {
1552 LOG(ERROR) << __func__
1553 << ": module implementation must override 'populateConnectedDevicePort' "
1554 << "to handle connection of external devices.";
1555 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1556 }
jiabin116d8392023-03-01 22:52:57 +00001557 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001558 return ndk::ScopedAStatus::ok();
1559}
1560
1561ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1562 const std::vector<AudioPortConfig*>& sources __unused,
1563 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001564 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001565 return ndk::ScopedAStatus::ok();
1566}
1567
jiabin783c48b2023-02-28 18:28:06 +00001568void Module::onExternalDeviceConnectionChanged(
1569 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1570 bool connected __unused) {
1571 LOG(DEBUG) << __func__ << ": do nothing and return";
1572}
1573
jiabindd23b0e2023-12-11 19:10:05 +00001574void Module::onPrepareToDisconnectExternalDevice(
1575 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused) {
1576 LOG(DEBUG) << __func__ << ": do nothing and return";
1577}
1578
jiabin783c48b2023-02-28 18:28:06 +00001579ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1580 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1581 return ndk::ScopedAStatus::ok();
1582}
1583
1584ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1585 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1586 return ndk::ScopedAStatus::ok();
1587}
1588
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001589std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1590 std::vector<MicrophoneInfo> result;
1591 Configuration& config = getConfig();
1592 for (const AudioPort& port : config.ports) {
1593 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1594 const AudioDeviceType deviceType =
1595 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1596 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1597 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1598 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1599 // accordingly based on their physical microphone parameters.
1600 result.push_back(MicrophoneInfo{
1601 .id = port.name,
1602 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1603 .group = 0,
1604 .indexInTheGroup = 0,
1605 });
1606 }
1607 }
1608 }
1609 return result;
1610}
1611
Ram Mohan18f0d512023-07-01 00:47:09 +05301612ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1613 return mStreams.bluetoothParametersUpdated();
1614}
1615
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001616} // namespace aidl::android::hardware::audio::core