blob: 5481eb25d1546e0db51a54023e2dc0b531fe2081 [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) {
Mikhail Naganovb06a4922024-03-06 16:39:50 -08001168 // 'AudioMixPortExt.handle' and '.usecase' are set by the client,
1169 // copy from in_requested.
1170 const auto& src = in_requested.ext.get<AudioPortExt::Tag::mix>();
1171 auto& dst = out_suggested->ext.get<AudioPortExt::Tag::mix>();
1172 dst.handle = src.handle;
1173 dst.usecase = src.usecase;
Mikhail Naganov248e9502023-02-21 16:32:40 -08001174 }
1175 } else {
1176 LOG(WARNING) << __func__ << ": requested ext tag "
1177 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1178 << toString(out_suggested->ext.getTag());
1179 requestedIsValid = false;
1180 }
1181 }
1182
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001183 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1184 out_suggested->id = getConfig().nextPortId++;
1185 configs.push_back(*out_suggested);
Mikhail Naganova92039a2023-12-20 14:27:22 -08001186 *applied = true;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001187 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
1188 } else if (existing != configs.end() && requestedIsValid) {
1189 *existing = *out_suggested;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001190 *applied = true;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001191 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
1192 } else {
1193 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
1194 << "; requested is valid? " << requestedIsValid << ", fully specified? "
1195 << requestedIsFullySpecified;
Mikhail Naganova92039a2023-12-20 14:27:22 -08001196 *applied = false;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001197 }
1198 return ndk::ScopedAStatus::ok();
1199}
1200
1201ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1202 auto& patches = getConfig().patches;
1203 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1204 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001205 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001206 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001207 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1208 mPatches = std::move(patchesBackup);
1209 return status;
1210 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001211 patches.erase(patchIt);
1212 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1213 return ndk::ScopedAStatus::ok();
1214 }
1215 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1216 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1217}
1218
1219ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1220 auto& configs = getConfig().portConfigs;
1221 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1222 if (configIt != configs.end()) {
1223 if (mStreams.count(in_portConfigId) != 0) {
1224 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1225 << " has a stream opened on it";
1226 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1227 }
1228 auto patchIt = mPatches.find(in_portConfigId);
1229 if (patchIt != mPatches.end()) {
1230 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1231 << " is used by the patch with id " << patchIt->second;
1232 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1233 }
1234 auto& initials = getConfig().initialConfigs;
1235 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1236 if (initialIt == initials.end()) {
1237 configs.erase(configIt);
1238 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1239 } else if (*configIt != *initialIt) {
1240 *configIt = *initialIt;
1241 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1242 }
1243 return ndk::ScopedAStatus::ok();
1244 }
1245 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1246 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1247}
1248
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001249ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1250 *_aidl_return = mMasterMute;
1251 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1252 return ndk::ScopedAStatus::ok();
1253}
1254
1255ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1256 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001257 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1258 : onMasterMuteChanged(in_mute);
1259 if (result.isOk()) {
1260 mMasterMute = in_mute;
1261 } else {
1262 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1263 << "), error=" << result;
1264 // Reset master mute if it failed.
1265 onMasterMuteChanged(mMasterMute);
1266 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001267 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001268}
1269
1270ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1271 *_aidl_return = mMasterVolume;
1272 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1273 return ndk::ScopedAStatus::ok();
1274}
1275
1276ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1277 LOG(DEBUG) << __func__ << ": " << in_volume;
1278 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001279 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1280 : onMasterVolumeChanged(in_volume);
1281 if (result.isOk()) {
1282 mMasterVolume = in_volume;
1283 } else {
1284 // Reset master volume if it failed.
1285 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1286 << "), error=" << result;
1287 onMasterVolumeChanged(mMasterVolume);
1288 }
Mikhail Naganov55045b52023-10-24 17:03:50 -07001289 return result;
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001290 }
1291 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1292 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1293}
1294
1295ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1296 *_aidl_return = mMicMute;
1297 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1298 return ndk::ScopedAStatus::ok();
1299}
1300
1301ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1302 LOG(DEBUG) << __func__ << ": " << in_mute;
1303 mMicMute = in_mute;
1304 return ndk::ScopedAStatus::ok();
1305}
1306
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001307ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001308 *_aidl_return = getMicrophoneInfos();
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001309 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1310 return ndk::ScopedAStatus::ok();
1311}
1312
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001313ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001314 if (!isValidAudioMode(in_mode)) {
1315 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1316 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1317 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001318 // No checks for supported audio modes here, it's an informative notification.
1319 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1320 return ndk::ScopedAStatus::ok();
1321}
1322
1323ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1324 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1325 return ndk::ScopedAStatus::ok();
1326}
1327
1328ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1329 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1330 return ndk::ScopedAStatus::ok();
1331}
1332
Vlad Popa83a6d822022-11-07 13:53:57 +01001333ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001334 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001335 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001336 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001337 *_aidl_return = mSoundDose.getInstance();
Vlad Popa943b7e22022-12-08 14:24:12 +01001338 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001339 return ndk::ScopedAStatus::ok();
1340}
1341
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001342ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1343 LOG(DEBUG) << __func__;
1344 (void)_aidl_return;
1345 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1346}
1347
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001348const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001349const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001350
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001351ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1352 std::vector<VendorParameter>* _aidl_return) {
1353 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001354 bool allParametersKnown = true;
1355 for (const auto& id : in_ids) {
1356 if (id == VendorDebug::kForceTransientBurstName) {
1357 VendorParameter forceTransientBurst{.id = id};
1358 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1359 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001360 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1361 VendorParameter forceSynchronousDrain{.id = id};
1362 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1363 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001364 } else {
1365 allParametersKnown = false;
1366 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1367 }
1368 }
1369 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1370 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001371}
1372
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001373namespace {
1374
1375template <typename W>
1376bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1377 std::optional<W> value;
1378 binder_status_t result = p.ext.getParcelable(&value);
1379 if (result == STATUS_OK && value.has_value()) {
1380 *v = value.value().value;
1381 return true;
1382 }
1383 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1384 << "\": " << result;
1385 return false;
1386}
1387
1388} // namespace
1389
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001390ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1391 bool in_async) {
1392 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1393 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001394 bool allParametersKnown = true;
1395 for (const auto& p : in_parameters) {
1396 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001397 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1398 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1399 }
1400 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1401 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001402 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1403 }
1404 } else {
1405 allParametersKnown = false;
1406 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1407 }
1408 }
1409 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1410 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001411}
1412
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001413ndk::ScopedAStatus Module::addDeviceEffect(
1414 int32_t in_portConfigId,
1415 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1416 if (in_effect == nullptr) {
1417 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1418 } else {
1419 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1420 << in_effect->asBinder().get();
1421 }
1422 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1423}
1424
1425ndk::ScopedAStatus Module::removeDeviceEffect(
1426 int32_t in_portConfigId,
1427 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1428 if (in_effect == nullptr) {
1429 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1430 } else {
1431 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1432 << in_effect->asBinder().get();
1433 }
1434 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1435}
1436
jiabin9a8e6862023-01-12 23:06:37 +00001437ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1438 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1439 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1440 std::set<int32_t> mmapSinks;
1441 std::set<int32_t> mmapSources;
1442 auto& ports = getConfig().ports;
1443 for (const auto& port : ports) {
1444 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1445 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1446 AudioInputFlags::MMAP_NOIRQ)) {
1447 mmapSinks.insert(port.id);
1448 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1449 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1450 AudioOutputFlags::MMAP_NOIRQ)) {
1451 mmapSources.insert(port.id);
1452 }
1453 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001454 if (mmapSources.empty() && mmapSinks.empty()) {
1455 AudioMMapPolicyInfo never;
1456 never.mmapPolicy = AudioMMapPolicy::NEVER;
1457 _aidl_return->push_back(never);
1458 return ndk::ScopedAStatus::ok();
1459 }
jiabin9a8e6862023-01-12 23:06:37 +00001460 for (const auto& route : getConfig().routes) {
1461 if (mmapSinks.count(route.sinkPortId) != 0) {
1462 // The sink is a mix port, add the sources if they are device ports.
1463 for (int sourcePortId : route.sourcePortIds) {
1464 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1465 if (sourcePortIt == ports.end()) {
1466 // This must not happen
1467 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1468 continue;
1469 }
1470 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1471 // The source is not a device port, skip
1472 continue;
1473 }
1474 AudioMMapPolicyInfo policyInfo;
1475 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1476 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1477 // default implementation.
1478 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1479 _aidl_return->push_back(policyInfo);
1480 }
1481 } else {
1482 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1483 if (sinkPortIt == ports.end()) {
1484 // This must not happen
1485 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1486 continue;
1487 }
1488 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1489 // The sink is not a device port, skip
1490 continue;
1491 }
1492 if (count_any(mmapSources, route.sourcePortIds)) {
1493 AudioMMapPolicyInfo policyInfo;
1494 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1495 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1496 // default implementation.
1497 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1498 _aidl_return->push_back(policyInfo);
1499 }
1500 }
1501 }
1502 return ndk::ScopedAStatus::ok();
1503}
1504
Eric Laurente2432ea2023-01-12 17:47:31 +01001505ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1506 LOG(DEBUG) << __func__;
1507 *_aidl_return = false;
1508 return ndk::ScopedAStatus::ok();
1509}
1510
jiabinb76981e2023-01-18 00:58:30 +00001511ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1512 if (!isMmapSupported()) {
1513 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1514 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1515 }
1516 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1517 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1518 return ndk::ScopedAStatus::ok();
1519}
1520
1521ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1522 if (!isMmapSupported()) {
1523 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1524 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1525 }
1526 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1527 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1528 return ndk::ScopedAStatus::ok();
1529}
1530
1531bool Module::isMmapSupported() {
1532 if (mIsMmapSupported.has_value()) {
1533 return mIsMmapSupported.value();
1534 }
1535 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1536 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1537 mIsMmapSupported = false;
1538 } else {
1539 mIsMmapSupported =
1540 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1541 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1542 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1543 }) != mmapPolicyInfos.end();
1544 }
1545 return mIsMmapSupported.value();
1546}
1547
Mikhail Naganova92039a2023-12-20 14:27:22 -08001548ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
Mikhail Naganov84bcc042023-10-05 17:36:57 -07001549 if (audioPort->ext.getTag() != AudioPortExt::device) {
1550 LOG(ERROR) << __func__ << ": not a device port: " << audioPort->toString();
1551 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1552 }
1553 const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
1554 if (!devicePort.device.type.connection.empty()) {
1555 LOG(ERROR) << __func__
1556 << ": module implementation must override 'populateConnectedDevicePort' "
1557 << "to handle connection of external devices.";
1558 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1559 }
jiabin116d8392023-03-01 22:52:57 +00001560 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001561 return ndk::ScopedAStatus::ok();
1562}
1563
1564ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1565 const std::vector<AudioPortConfig*>& sources __unused,
1566 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001567 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001568 return ndk::ScopedAStatus::ok();
1569}
1570
jiabin783c48b2023-02-28 18:28:06 +00001571void Module::onExternalDeviceConnectionChanged(
1572 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1573 bool connected __unused) {
1574 LOG(DEBUG) << __func__ << ": do nothing and return";
1575}
1576
jiabindd23b0e2023-12-11 19:10:05 +00001577void Module::onPrepareToDisconnectExternalDevice(
1578 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused) {
1579 LOG(DEBUG) << __func__ << ": do nothing and return";
1580}
1581
jiabin783c48b2023-02-28 18:28:06 +00001582ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1583 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1584 return ndk::ScopedAStatus::ok();
1585}
1586
1587ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1588 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1589 return ndk::ScopedAStatus::ok();
1590}
1591
Lorena Torres-Huerta533cc782023-01-18 00:11:48 +00001592std::vector<MicrophoneInfo> Module::getMicrophoneInfos() {
1593 std::vector<MicrophoneInfo> result;
1594 Configuration& config = getConfig();
1595 for (const AudioPort& port : config.ports) {
1596 if (port.ext.getTag() == AudioPortExt::Tag::device) {
1597 const AudioDeviceType deviceType =
1598 port.ext.get<AudioPortExt::Tag::device>().device.type.type;
1599 if (deviceType == AudioDeviceType::IN_MICROPHONE ||
1600 deviceType == AudioDeviceType::IN_MICROPHONE_BACK) {
1601 // Placeholder values. Vendor implementations must populate MicrophoneInfo
1602 // accordingly based on their physical microphone parameters.
1603 result.push_back(MicrophoneInfo{
1604 .id = port.name,
1605 .device = port.ext.get<AudioPortExt::Tag::device>().device,
1606 .group = 0,
1607 .indexInTheGroup = 0,
1608 });
1609 }
1610 }
1611 }
1612 return result;
1613}
1614
Ram Mohan18f0d512023-07-01 00:47:09 +05301615ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1616 return mStreams.bluetoothParametersUpdated();
1617}
1618
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001619} // namespace aidl::android::hardware::audio::core