blob: a8f03af4fbc5d05016ff8528c5a2e36bfc4d09df [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 Naganovdf5adfd2021-11-11 22:09:22 +000021#include <android-base/logging.h>
Shunkai Yao39bf2c32022-12-06 03:25:59 +000022#include <android/binder_ibinder_platform.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000023
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000024#include <Utils.h>
25#include <aidl/android/media/audio/common/AudioInputFlags.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000026#include <aidl/android/media/audio/common/AudioOutputFlags.h>
27
Mikhail Naganov10c6fe22022-09-30 23:49:17 +000028#include "core-impl/Bluetooth.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000029#include "core-impl/Module.h"
jiabin253bd322023-01-25 23:57:31 +000030#include "core-impl/ModuleUsb.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010031#include "core-impl/SoundDose.h"
Mikhail Naganovf429c032023-01-07 00:24:50 +000032#include "core-impl/StreamStub.h"
jiabin253bd322023-01-25 23:57:31 +000033#include "core-impl/StreamUsb.h"
Mikhail Naganov3b125b72022-10-05 02:12:39 +000034#include "core-impl/Telephony.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000035#include "core-impl/utils.h"
36
37using aidl::android::hardware::audio::common::SinkMetadata;
38using aidl::android::hardware::audio::common::SourceMetadata;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010039using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000040using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000041using aidl::android::media::audio::common::AudioDevice;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000042using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000043using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000044using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000045using aidl::android::media::audio::common::AudioIoFlags;
jiabin9a8e6862023-01-12 23:06:37 +000046using aidl::android::media::audio::common::AudioMMapPolicy;
47using aidl::android::media::audio::common::AudioMMapPolicyInfo;
48using aidl::android::media::audio::common::AudioMMapPolicyType;
Mikhail Naganov04ae8222023-01-11 15:48:10 -080049using aidl::android::media::audio::common::AudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000050using aidl::android::media::audio::common::AudioOffloadInfo;
51using aidl::android::media::audio::common::AudioOutputFlags;
52using aidl::android::media::audio::common::AudioPort;
53using aidl::android::media::audio::common::AudioPortConfig;
54using aidl::android::media::audio::common::AudioPortExt;
55using aidl::android::media::audio::common::AudioProfile;
Mikhail Naganov20047bc2023-01-05 20:16:07 +000056using aidl::android::media::audio::common::Boolean;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000057using aidl::android::media::audio::common::Int;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080058using aidl::android::media::audio::common::MicrophoneInfo;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000059using aidl::android::media::audio::common::PcmType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000060using android::hardware::audio::common::getFrameSizeInBytes;
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +000061using android::hardware::audio::common::isBitPositionFlagSet;
Mikhail Naganov04ae8222023-01-11 15:48:10 -080062using android::hardware::audio::common::isValidAudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000063
64namespace aidl::android::hardware::audio::core {
65
66namespace {
67
68bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
69 *config = {};
70 config->portId = port.id;
71 if (port.profiles.empty()) {
72 LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles";
73 return false;
74 }
75 const auto& profile = port.profiles.begin();
76 config->format = profile->format;
77 if (profile->channelMasks.empty()) {
78 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
79 << " has no channel masks";
80 return false;
81 }
82 config->channelMask = *profile->channelMasks.begin();
83 if (profile->sampleRates.empty()) {
84 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
85 << " has no sample rates";
86 return false;
87 }
88 Int sampleRate;
89 sampleRate.value = *profile->sampleRates.begin();
90 config->sampleRate = sampleRate;
91 config->flags = port.flags;
92 config->ext = port.ext;
93 return true;
94}
95
96bool 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
111std::shared_ptr<Module> Module::createInstance(Type type) {
112 switch (type) {
113 case Module::Type::USB:
114 return ndk::SharedRefBase::make<ModuleUsb>(type);
115 case Type::DEFAULT:
116 case Type::R_SUBMIX:
117 default:
118 return ndk::SharedRefBase::make<Module>(type);
119 }
120}
121
122// static
123StreamIn::CreateInstance Module::getStreamInCreator(Type type) {
124 switch (type) {
125 case Type::USB:
126 return StreamInUsb::createInstance;
127 case Type::DEFAULT:
128 case Type::R_SUBMIX:
129 default:
130 return StreamInStub::createInstance;
131 }
132}
133
134// static
135StreamOut::CreateInstance Module::getStreamOutCreator(Type type) {
136 switch (type) {
137 case Type::USB:
138 return StreamOutUsb::createInstance;
139 case Type::DEFAULT:
140 case Type::R_SUBMIX:
141 default:
142 return StreamOutStub::createInstance;
143 }
144}
145
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000146void Module::cleanUpPatch(int32_t patchId) {
147 erase_all_values(mPatches, std::set<int32_t>{patchId});
148}
149
Mikhail Naganov8651b362023-01-06 23:15:27 +0000150ndk::ScopedAStatus Module::createStreamContext(
151 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
152 std::shared_ptr<IStreamCallback> asyncCallback,
153 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000154 if (in_bufferSizeFrames <= 0) {
155 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
156 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
157 }
158 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
159 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
160 << ", must be at least " << kMinimumStreamBufferSizeFrames;
161 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
162 }
163 auto& configs = getConfig().portConfigs;
164 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000165 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000166 // validity of the portConfigId has already been checked.
167 const size_t frameSize =
168 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
169 if (frameSize == 0) {
170 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
171 << portConfigIt->toString();
172 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
173 }
174 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
175 if (frameSize > kMaximumStreamBufferSizeBytes / in_bufferSizeFrames) {
176 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
177 << " frames is too large, maximum size is "
178 << kMaximumStreamBufferSizeBytes / frameSize;
179 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
180 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000181 const auto& flags = portConfigIt->flags.value();
182 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000183 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
184 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000185 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000186 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
187 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000188 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
Mikhail Naganov194daaa2023-01-05 22:34:20 +0000189 mVendorDebug.forceTransientBurst,
190 mVendorDebug.forceSynchronousDrain};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000191 StreamContext temp(
192 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
193 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000194 portConfigIt->format.value(), portConfigIt->channelMask.value(),
jiabin253bd322023-01-25 23:57:31 +0000195 portConfigIt->sampleRate.value().value,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000196 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov8651b362023-01-06 23:15:27 +0000197 asyncCallback, outEventCallback, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000198 if (temp.isValid()) {
199 *out_context = std::move(temp);
200 } else {
201 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
202 }
203 } else {
204 // TODO: Implement simulation of MMAP buffer allocation
205 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000206 return ndk::ScopedAStatus::ok();
207}
208
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000209std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
210 std::vector<AudioDevice> result;
211 auto& ports = getConfig().ports;
212 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
213 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
214 auto portIt = findById<AudioPort>(ports, *it);
215 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
216 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
217 }
218 }
219 return result;
220}
221
222std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
223 std::set<int32_t> result;
224 auto patchIdsRange = mPatches.equal_range(portConfigId);
225 auto& patches = getConfig().patches;
226 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
227 auto patchIt = findById<AudioPatch>(patches, it->second);
228 if (patchIt == patches.end()) {
229 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
230 << "not found in the configuration";
231 }
232 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
233 portConfigId) != patchIt->sourcePortConfigIds.end()) {
234 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
235 } else {
236 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
237 }
238 }
239 return result;
240}
241
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000242ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
243 auto& configs = getConfig().portConfigs;
244 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
245 if (portConfigIt == configs.end()) {
246 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
247 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
248 }
249 const int32_t portId = portConfigIt->portId;
250 // In our implementation, configs of mix ports always have unique IDs.
251 CHECK(portId != in_portConfigId);
252 auto& ports = getConfig().ports;
253 auto portIt = findById<AudioPort>(ports, portId);
254 if (portIt == ports.end()) {
255 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
256 << in_portConfigId << " not found";
257 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
258 }
259 if (mStreams.count(in_portConfigId) != 0) {
260 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
261 << " already has a stream opened on it";
262 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
263 }
264 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
265 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
266 << " does not correspond to a mix port";
267 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
268 }
269 const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
270 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
271 LOG(ERROR) << __func__ << ": port id " << portId
272 << " has already reached maximum allowed opened stream count: "
273 << maxOpenStreamCount;
274 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
275 }
276 *port = &(*portIt);
277 return ndk::ScopedAStatus::ok();
278}
279
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000280template <typename C>
281std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
282 std::set<int32_t> result;
283 auto& portConfigs = getConfig().portConfigs;
284 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
285 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
286 if (portConfigIt != portConfigs.end()) {
287 result.insert(portConfigIt->portId);
288 }
289 }
290 return result;
291}
292
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000293internal::Configuration& Module::getConfig() {
294 if (!mConfig) {
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000295 switch (mType) {
296 case Type::DEFAULT:
297 mConfig = std::move(internal::getPrimaryConfiguration());
298 break;
299 case Type::R_SUBMIX:
300 mConfig = std::move(internal::getRSubmixConfiguration());
301 break;
jiabinb309d8d2023-01-20 19:07:15 +0000302 case Type::USB:
303 mConfig = std::move(internal::getUsbConfiguration());
jiabin253bd322023-01-25 23:57:31 +0000304 break;
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000305 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000306 }
307 return *mConfig;
308}
309
310void Module::registerPatch(const AudioPatch& patch) {
311 auto& configs = getConfig().portConfigs;
312 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
313 for (auto portConfigId : portConfigIds) {
314 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
315 if (configIt != configs.end()) {
316 mPatches.insert(std::pair{portConfigId, patch.id});
317 if (configIt->portId != portConfigId) {
318 mPatches.insert(std::pair{configIt->portId, patch.id});
319 }
320 }
321 };
322 };
323 do_insert(patch.sourcePortConfigIds);
324 do_insert(patch.sinkPortConfigIds);
325}
326
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000327void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) {
328 // Streams from the old patch need to be disconnected, streams from the new
329 // patch need to be connected. If the stream belongs to both patches, no need
330 // to update it.
331 std::set<int32_t> idsToDisconnect, idsToConnect;
332 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
333 oldPatch.sourcePortConfigIds.end());
334 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
335 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
336 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
337 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
338 if (idsToConnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000339 LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected";
340 mStreams.setStreamIsConnected(portConfigId, {});
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000341 }
342 });
343 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
344 if (idsToDisconnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000345 const auto connectedDevices = findConnectedDevices(portConfigId);
346 LOG(DEBUG) << "The stream on port config id " << portConfigId
347 << " is connected to: " << ::android::internal::ToString(connectedDevices);
348 mStreams.setStreamIsConnected(portConfigId, connectedDevices);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000349 }
350 });
351}
352
Mikhail Naganov00603d12022-05-02 22:52:13 +0000353ndk::ScopedAStatus Module::setModuleDebug(
354 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
355 LOG(DEBUG) << __func__ << ": old flags:" << mDebug.toString()
356 << ", new flags: " << in_debug.toString();
357 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
358 !mConnectedDevicePorts.empty()) {
359 LOG(ERROR) << __func__ << ": attempting to change device connections simulation "
360 << "while having external devices connected";
361 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
362 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000363 if (in_debug.streamTransientStateDelayMs < 0) {
364 LOG(ERROR) << __func__ << ": streamTransientStateDelayMs is negative: "
365 << in_debug.streamTransientStateDelayMs;
366 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
367 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000368 mDebug = in_debug;
369 return ndk::ScopedAStatus::ok();
370}
371
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000372ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800373 if (!mTelephony) {
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000374 mTelephony = ndk::SharedRefBase::make<Telephony>();
375 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800376 *_aidl_return = mTelephony.getPtr();
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000377 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
378 return ndk::ScopedAStatus::ok();
379}
380
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000381ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800382 if (!mBluetooth) {
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000383 mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000384 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800385 *_aidl_return = mBluetooth.getPtr();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000386 LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get();
387 return ndk::ScopedAStatus::ok();
388}
389
Mikhail Naganov7499a002023-02-27 18:51:44 -0800390ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
391 if (!mBluetoothA2dp) {
392 mBluetoothA2dp = ndk::SharedRefBase::make<BluetoothA2dp>();
393 }
394 *_aidl_return = mBluetoothA2dp.getPtr();
395 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothA2dp: " << _aidl_return->get();
396 return ndk::ScopedAStatus::ok();
397}
398
Mikhail Naganov00603d12022-05-02 22:52:13 +0000399ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
400 AudioPort* _aidl_return) {
401 const int32_t templateId = in_templateIdAndAdditionalData.id;
402 auto& ports = getConfig().ports;
403 AudioPort connectedPort;
404 { // Scope the template port so that we don't accidentally modify it.
405 auto templateIt = findById<AudioPort>(ports, templateId);
406 if (templateIt == ports.end()) {
407 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
408 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
409 }
410 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
411 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
412 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
413 }
414 if (!templateIt->profiles.empty()) {
415 LOG(ERROR) << __func__ << ": port id " << templateId
416 << " does not have dynamic profiles";
417 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
418 }
419 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
420 if (templateDevicePort.device.type.connection.empty()) {
421 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
422 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
423 }
424 // Postpone id allocation until we ensure that there are no client errors.
425 connectedPort = *templateIt;
426 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
427 const auto& inputDevicePort =
428 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
429 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
430 connectedDevicePort.device.address = inputDevicePort.device.address;
431 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
432 << connectedDevicePort.device.toString();
433 // Check if there is already a connected port with for the same external device.
434 for (auto connectedPortId : mConnectedDevicePorts) {
435 auto connectedPortIt = findById<AudioPort>(ports, connectedPortId);
436 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
437 connectedDevicePort.device) {
438 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
439 << " is already connected at the device port id " << connectedPortId;
440 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
441 }
442 }
443 }
444
445 if (!mDebug.simulateDeviceConnections) {
446 // In a real HAL here we would attempt querying the profiles from the device.
447 LOG(ERROR) << __func__ << ": failed to query supported device profiles";
jiabin253bd322023-01-25 23:57:31 +0000448 // TODO: Check the return value when it is ready for actual devices.
449 populateConnectedDevicePort(&connectedPort);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000450 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
451 }
452
453 connectedPort.id = ++getConfig().nextPortId;
454 mConnectedDevicePorts.insert(connectedPort.id);
455 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
456 << "connected port ID " << connectedPort.id;
457 auto& connectedProfiles = getConfig().connectedProfiles;
458 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
459 connectedProfilesIt != connectedProfiles.end()) {
460 connectedPort.profiles = connectedProfilesIt->second;
461 }
462 ports.push_back(connectedPort);
463 *_aidl_return = std::move(connectedPort);
464
465 std::vector<AudioRoute> newRoutes;
466 auto& routes = getConfig().routes;
467 for (auto& r : routes) {
468 if (r.sinkPortId == templateId) {
469 AudioRoute newRoute;
470 newRoute.sourcePortIds = r.sourcePortIds;
471 newRoute.sinkPortId = connectedPort.id;
472 newRoute.isExclusive = r.isExclusive;
473 newRoutes.push_back(std::move(newRoute));
474 } else {
475 auto& srcs = r.sourcePortIds;
476 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
477 srcs.push_back(connectedPort.id);
478 }
479 }
480 }
481 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
482
483 return ndk::ScopedAStatus::ok();
484}
485
486ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
487 auto& ports = getConfig().ports;
488 auto portIt = findById<AudioPort>(ports, in_portId);
489 if (portIt == ports.end()) {
490 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
491 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
492 }
493 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
494 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
495 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
496 }
497 if (mConnectedDevicePorts.count(in_portId) == 0) {
498 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
499 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
500 }
501 auto& configs = getConfig().portConfigs;
502 auto& initials = getConfig().initialConfigs;
503 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
504 if (config.portId == in_portId) {
505 // Check if the configuration was provided by the client.
506 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
507 return initialIt == initials.end() || config != *initialIt;
508 }
509 return false;
510 });
511 if (configIt != configs.end()) {
512 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
513 << configIt->id;
514 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
515 }
516 ports.erase(portIt);
517 mConnectedDevicePorts.erase(in_portId);
518 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
519
520 auto& routes = getConfig().routes;
521 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
522 if (routesIt->sinkPortId == in_portId) {
523 routesIt = routes.erase(routesIt);
524 } else {
525 // Note: the list of sourcePortIds can't become empty because there must
526 // be the id of the template port in the route.
527 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
528 ++routesIt;
529 }
530 }
531
532 return ndk::ScopedAStatus::ok();
533}
534
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000535ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
536 *_aidl_return = getConfig().patches;
537 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
538 return ndk::ScopedAStatus::ok();
539}
540
541ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
542 auto& ports = getConfig().ports;
543 auto portIt = findById<AudioPort>(ports, in_portId);
544 if (portIt != ports.end()) {
545 *_aidl_return = *portIt;
546 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
547 return ndk::ScopedAStatus::ok();
548 }
549 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
550 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
551}
552
553ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
554 *_aidl_return = getConfig().portConfigs;
555 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
556 return ndk::ScopedAStatus::ok();
557}
558
559ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
560 *_aidl_return = getConfig().ports;
561 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
562 return ndk::ScopedAStatus::ok();
563}
564
565ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
566 *_aidl_return = getConfig().routes;
567 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
568 return ndk::ScopedAStatus::ok();
569}
570
Mikhail Naganov00603d12022-05-02 22:52:13 +0000571ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
572 std::vector<AudioRoute>* _aidl_return) {
573 auto& ports = getConfig().ports;
574 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
575 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
576 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
577 }
578 auto& routes = getConfig().routes;
579 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
580 [&](const auto& r) {
581 const auto& srcs = r.sourcePortIds;
582 return r.sinkPortId == in_portId ||
583 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
584 });
585 return ndk::ScopedAStatus::ok();
586}
587
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000588ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
589 OpenInputStreamReturn* _aidl_return) {
590 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
591 << in_args.bufferSizeFrames << " frames";
592 AudioPort* port = nullptr;
593 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
594 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000595 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000596 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
597 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000598 << " does not correspond to an input mix port";
599 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
600 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000601 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000602 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000603 nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000604 !status.isOk()) {
605 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000606 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000607 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000608 std::shared_ptr<StreamIn> stream;
jiabin253bd322023-01-25 23:57:31 +0000609 ndk::ScopedAStatus status = getStreamInCreator(mType)(in_args.sinkMetadata, std::move(context),
610 mConfig->microphones, &stream);
611 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000612 return status;
613 }
614 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000615 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
616 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000617 auto patchIt = mPatches.find(in_args.portConfigId);
618 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000619 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000620 }
621 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000622 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000623 return ndk::ScopedAStatus::ok();
624}
625
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000626ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
627 OpenOutputStreamReturn* _aidl_return) {
628 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
629 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
630 << " frames";
631 AudioPort* port = nullptr;
632 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
633 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000634 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000635 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
636 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000637 << " does not correspond to an output mix port";
638 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
639 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000640 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
641 AudioOutputFlags::COMPRESS_OFFLOAD);
642 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000643 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000644 << " has COMPRESS_OFFLOAD flag set, requires offload info";
645 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
646 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000647 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
648 AudioOutputFlags::NON_BLOCKING);
649 if (isNonBlocking && in_args.callback == nullptr) {
650 LOG(ERROR) << __func__ << ": port id " << port->id
651 << " has NON_BLOCKING flag set, requires async callback";
652 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
653 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000654 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000655 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000656 isNonBlocking ? in_args.callback : nullptr,
657 in_args.eventCallback, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000658 !status.isOk()) {
659 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000660 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000661 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000662 std::shared_ptr<StreamOut> stream;
jiabin253bd322023-01-25 23:57:31 +0000663 ndk::ScopedAStatus status = getStreamOutCreator(mType)(
664 in_args.sourceMetadata, std::move(context), in_args.offloadInfo, &stream);
665 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000666 return status;
667 }
668 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000669 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
670 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000671 auto patchIt = mPatches.find(in_args.portConfigId);
672 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000673 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000674 }
675 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000676 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000677 return ndk::ScopedAStatus::ok();
678}
679
Mikhail Naganov74927202022-12-19 16:37:14 +0000680ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
681 SupportedPlaybackRateFactors* _aidl_return) {
682 LOG(DEBUG) << __func__;
683 (void)_aidl_return;
684 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
685}
686
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000687ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000688 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000689 if (in_requested.sourcePortConfigIds.empty()) {
690 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
691 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
692 }
693 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
694 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
695 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
696 }
697 if (in_requested.sinkPortConfigIds.empty()) {
698 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
699 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
700 }
701 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
702 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
703 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
704 }
705
706 auto& configs = getConfig().portConfigs;
707 std::vector<int32_t> missingIds;
708 auto sources =
709 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
710 if (!missingIds.empty()) {
711 LOG(ERROR) << __func__ << ": following source port config ids not found: "
712 << ::android::internal::ToString(missingIds);
713 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
714 }
715 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
716 if (!missingIds.empty()) {
717 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
718 << ::android::internal::ToString(missingIds);
719 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
720 }
721 // bool indicates whether a non-exclusive route is available.
722 // If only an exclusive route is available, that means the patch can not be
723 // established if there is any other patch which currently uses the sink port.
724 std::map<int32_t, bool> allowedSinkPorts;
725 auto& routes = getConfig().routes;
726 for (auto src : sources) {
727 for (const auto& r : routes) {
728 const auto& srcs = r.sourcePortIds;
729 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
730 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
731 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
732 }
733 }
734 }
735 }
736 for (auto sink : sinks) {
737 if (allowedSinkPorts.count(sink->portId) == 0) {
738 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
739 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
740 }
741 }
742
jiabin253bd322023-01-25 23:57:31 +0000743 if (auto status = checkAudioPatchEndpointsMatch(sources, sinks); !status.isOk()) {
744 return status;
745 }
746
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000747 auto& patches = getConfig().patches;
748 auto existing = patches.end();
749 std::optional<decltype(mPatches)> patchesBackup;
750 if (in_requested.id != 0) {
751 existing = findById<AudioPatch>(patches, in_requested.id);
752 if (existing != patches.end()) {
753 patchesBackup = mPatches;
754 cleanUpPatch(existing->id);
755 } else {
756 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
757 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
758 }
759 }
760 // Validate the requested patch.
761 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
762 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
763 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
764 << "is exclusive and is already used by some other patch";
765 if (patchesBackup.has_value()) {
766 mPatches = std::move(*patchesBackup);
767 }
768 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
769 }
770 }
771 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000772 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
773 _aidl_return->latenciesMs.clear();
774 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
775 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000776 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000777 if (existing == patches.end()) {
778 _aidl_return->id = getConfig().nextPatchId++;
779 patches.push_back(*_aidl_return);
780 existing = patches.begin() + (patches.size() - 1);
781 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000782 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000783 *existing = *_aidl_return;
784 }
785 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000786 updateStreamsConnectedState(oldPatch, *_aidl_return);
787
788 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
789 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000790 return ndk::ScopedAStatus::ok();
791}
792
793ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
794 AudioPortConfig* out_suggested, bool* _aidl_return) {
795 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
796 auto& configs = getConfig().portConfigs;
797 auto existing = configs.end();
798 if (in_requested.id != 0) {
799 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
800 existing == configs.end()) {
801 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
802 << " not found";
803 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
804 }
805 }
806
807 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
808 if (portId == 0) {
809 LOG(ERROR) << __func__ << ": input port config does not specify portId";
810 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
811 }
812 auto& ports = getConfig().ports;
813 auto portIt = findById<AudioPort>(ports, portId);
814 if (portIt == ports.end()) {
815 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
816 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
817 }
818 if (existing != configs.end()) {
819 *out_suggested = *existing;
820 } else {
821 AudioPortConfig newConfig;
822 if (generateDefaultPortConfig(*portIt, &newConfig)) {
823 *out_suggested = newConfig;
824 } else {
825 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
826 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
827 }
828 }
829 // From this moment, 'out_suggested' is either an existing port config,
830 // or a new generated config. Now attempt to update it according to the specified
831 // fields of 'in_requested'.
832
833 bool requestedIsValid = true, requestedIsFullySpecified = true;
834
835 AudioIoFlags portFlags = portIt->flags;
836 if (in_requested.flags.has_value()) {
837 if (in_requested.flags.value() != portFlags) {
838 LOG(WARNING) << __func__ << ": requested flags "
839 << in_requested.flags.value().toString() << " do not match port's "
840 << portId << " flags " << portFlags.toString();
841 requestedIsValid = false;
842 }
843 } else {
844 requestedIsFullySpecified = false;
845 }
846
847 AudioProfile portProfile;
848 if (in_requested.format.has_value()) {
849 const auto& format = in_requested.format.value();
850 if (findAudioProfile(*portIt, format, &portProfile)) {
851 out_suggested->format = format;
852 } else {
853 LOG(WARNING) << __func__ << ": requested format " << format.toString()
854 << " is not found in port's " << portId << " profiles";
855 requestedIsValid = false;
856 }
857 } else {
858 requestedIsFullySpecified = false;
859 }
860 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
861 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
862 << out_suggested->format.value().toString() << " anymore";
863 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
864 }
865
866 if (in_requested.channelMask.has_value()) {
867 const auto& channelMask = in_requested.channelMask.value();
868 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
869 portProfile.channelMasks.end()) {
870 out_suggested->channelMask = channelMask;
871 } else {
872 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
873 << " is not supported for the format " << portProfile.format.toString()
874 << " by the port " << portId;
875 requestedIsValid = false;
876 }
877 } else {
878 requestedIsFullySpecified = false;
879 }
880
881 if (in_requested.sampleRate.has_value()) {
882 const auto& sampleRate = in_requested.sampleRate.value();
883 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
884 sampleRate.value) != portProfile.sampleRates.end()) {
885 out_suggested->sampleRate = sampleRate;
886 } else {
887 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
888 << " is not supported for the format " << portProfile.format.toString()
889 << " by the port " << portId;
890 requestedIsValid = false;
891 }
892 } else {
893 requestedIsFullySpecified = false;
894 }
895
896 if (in_requested.gain.has_value()) {
897 // Let's pretend that gain can always be applied.
898 out_suggested->gain = in_requested.gain.value();
899 }
900
Mikhail Naganov248e9502023-02-21 16:32:40 -0800901 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
902 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
903 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
904 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
905 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
906 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
907 }
908 } else {
909 LOG(WARNING) << __func__ << ": requested ext tag "
910 << toString(in_requested.ext.getTag()) << " do not match port's tag "
911 << toString(out_suggested->ext.getTag());
912 requestedIsValid = false;
913 }
914 }
915
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000916 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
917 out_suggested->id = getConfig().nextPortId++;
918 configs.push_back(*out_suggested);
919 *_aidl_return = true;
920 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
921 } else if (existing != configs.end() && requestedIsValid) {
922 *existing = *out_suggested;
923 *_aidl_return = true;
924 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
925 } else {
926 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
927 << "; requested is valid? " << requestedIsValid << ", fully specified? "
928 << requestedIsFullySpecified;
929 *_aidl_return = false;
930 }
931 return ndk::ScopedAStatus::ok();
932}
933
934ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
935 auto& patches = getConfig().patches;
936 auto patchIt = findById<AudioPatch>(patches, in_patchId);
937 if (patchIt != patches.end()) {
938 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000939 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000940 patches.erase(patchIt);
941 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
942 return ndk::ScopedAStatus::ok();
943 }
944 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
945 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
946}
947
948ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
949 auto& configs = getConfig().portConfigs;
950 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
951 if (configIt != configs.end()) {
952 if (mStreams.count(in_portConfigId) != 0) {
953 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
954 << " has a stream opened on it";
955 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
956 }
957 auto patchIt = mPatches.find(in_portConfigId);
958 if (patchIt != mPatches.end()) {
959 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
960 << " is used by the patch with id " << patchIt->second;
961 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
962 }
963 auto& initials = getConfig().initialConfigs;
964 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
965 if (initialIt == initials.end()) {
966 configs.erase(configIt);
967 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
968 } else if (*configIt != *initialIt) {
969 *configIt = *initialIt;
970 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
971 }
972 return ndk::ScopedAStatus::ok();
973 }
974 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
975 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
976}
977
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000978ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
979 *_aidl_return = mMasterMute;
980 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
981 return ndk::ScopedAStatus::ok();
982}
983
984ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
985 LOG(DEBUG) << __func__ << ": " << in_mute;
986 mMasterMute = in_mute;
987 return ndk::ScopedAStatus::ok();
988}
989
990ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
991 *_aidl_return = mMasterVolume;
992 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
993 return ndk::ScopedAStatus::ok();
994}
995
996ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
997 LOG(DEBUG) << __func__ << ": " << in_volume;
998 if (in_volume >= 0.0f && in_volume <= 1.0f) {
999 mMasterVolume = in_volume;
1000 return ndk::ScopedAStatus::ok();
1001 }
1002 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1003 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1004}
1005
1006ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1007 *_aidl_return = mMicMute;
1008 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1009 return ndk::ScopedAStatus::ok();
1010}
1011
1012ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1013 LOG(DEBUG) << __func__ << ": " << in_mute;
1014 mMicMute = in_mute;
1015 return ndk::ScopedAStatus::ok();
1016}
1017
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001018ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001019 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001020 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1021 return ndk::ScopedAStatus::ok();
1022}
1023
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001024ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001025 if (!isValidAudioMode(in_mode)) {
1026 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1027 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1028 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001029 // No checks for supported audio modes here, it's an informative notification.
1030 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1031 return ndk::ScopedAStatus::ok();
1032}
1033
1034ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1035 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1036 return ndk::ScopedAStatus::ok();
1037}
1038
1039ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1040 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1041 return ndk::ScopedAStatus::ok();
1042}
1043
Vlad Popa83a6d822022-11-07 13:53:57 +01001044ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001045 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001046 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001047 }
Mikhail Naganov7499a002023-02-27 18:51:44 -08001048 *_aidl_return = mSoundDose.getPtr();
Vlad Popa943b7e22022-12-08 14:24:12 +01001049 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001050 return ndk::ScopedAStatus::ok();
1051}
1052
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001053ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1054 LOG(DEBUG) << __func__;
1055 (void)_aidl_return;
1056 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1057}
1058
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001059const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001060const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001061
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001062ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1063 std::vector<VendorParameter>* _aidl_return) {
1064 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001065 bool allParametersKnown = true;
1066 for (const auto& id : in_ids) {
1067 if (id == VendorDebug::kForceTransientBurstName) {
1068 VendorParameter forceTransientBurst{.id = id};
1069 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1070 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001071 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1072 VendorParameter forceSynchronousDrain{.id = id};
1073 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1074 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001075 } else {
1076 allParametersKnown = false;
1077 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1078 }
1079 }
1080 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1081 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001082}
1083
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001084namespace {
1085
1086template <typename W>
1087bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1088 std::optional<W> value;
1089 binder_status_t result = p.ext.getParcelable(&value);
1090 if (result == STATUS_OK && value.has_value()) {
1091 *v = value.value().value;
1092 return true;
1093 }
1094 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1095 << "\": " << result;
1096 return false;
1097}
1098
1099} // namespace
1100
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001101ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1102 bool in_async) {
1103 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1104 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001105 bool allParametersKnown = true;
1106 for (const auto& p : in_parameters) {
1107 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001108 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1109 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1110 }
1111 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1112 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001113 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1114 }
1115 } else {
1116 allParametersKnown = false;
1117 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1118 }
1119 }
1120 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1121 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001122}
1123
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001124ndk::ScopedAStatus Module::addDeviceEffect(
1125 int32_t in_portConfigId,
1126 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1127 if (in_effect == nullptr) {
1128 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1129 } else {
1130 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1131 << in_effect->asBinder().get();
1132 }
1133 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1134}
1135
1136ndk::ScopedAStatus Module::removeDeviceEffect(
1137 int32_t in_portConfigId,
1138 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1139 if (in_effect == nullptr) {
1140 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1141 } else {
1142 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1143 << in_effect->asBinder().get();
1144 }
1145 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1146}
1147
jiabin9a8e6862023-01-12 23:06:37 +00001148ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1149 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1150 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1151 std::set<int32_t> mmapSinks;
1152 std::set<int32_t> mmapSources;
1153 auto& ports = getConfig().ports;
1154 for (const auto& port : ports) {
1155 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1156 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1157 AudioInputFlags::MMAP_NOIRQ)) {
1158 mmapSinks.insert(port.id);
1159 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1160 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1161 AudioOutputFlags::MMAP_NOIRQ)) {
1162 mmapSources.insert(port.id);
1163 }
1164 }
1165 for (const auto& route : getConfig().routes) {
1166 if (mmapSinks.count(route.sinkPortId) != 0) {
1167 // The sink is a mix port, add the sources if they are device ports.
1168 for (int sourcePortId : route.sourcePortIds) {
1169 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1170 if (sourcePortIt == ports.end()) {
1171 // This must not happen
1172 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1173 continue;
1174 }
1175 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1176 // The source is not a device port, skip
1177 continue;
1178 }
1179 AudioMMapPolicyInfo policyInfo;
1180 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1181 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1182 // default implementation.
1183 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1184 _aidl_return->push_back(policyInfo);
1185 }
1186 } else {
1187 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1188 if (sinkPortIt == ports.end()) {
1189 // This must not happen
1190 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1191 continue;
1192 }
1193 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1194 // The sink is not a device port, skip
1195 continue;
1196 }
1197 if (count_any(mmapSources, route.sourcePortIds)) {
1198 AudioMMapPolicyInfo policyInfo;
1199 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1200 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1201 // default implementation.
1202 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1203 _aidl_return->push_back(policyInfo);
1204 }
1205 }
1206 }
1207 return ndk::ScopedAStatus::ok();
1208}
1209
Eric Laurente2432ea2023-01-12 17:47:31 +01001210ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1211 LOG(DEBUG) << __func__;
1212 *_aidl_return = false;
1213 return ndk::ScopedAStatus::ok();
1214}
1215
jiabinb76981e2023-01-18 00:58:30 +00001216ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1217 if (!isMmapSupported()) {
1218 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1219 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1220 }
1221 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1222 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1223 return ndk::ScopedAStatus::ok();
1224}
1225
1226ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1227 if (!isMmapSupported()) {
1228 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1229 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1230 }
1231 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1232 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1233 return ndk::ScopedAStatus::ok();
1234}
1235
1236bool Module::isMmapSupported() {
1237 if (mIsMmapSupported.has_value()) {
1238 return mIsMmapSupported.value();
1239 }
1240 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1241 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1242 mIsMmapSupported = false;
1243 } else {
1244 mIsMmapSupported =
1245 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1246 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1247 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1248 }) != mmapPolicyInfos.end();
1249 }
1250 return mIsMmapSupported.value();
1251}
1252
jiabin253bd322023-01-25 23:57:31 +00001253ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
1254 LOG(DEBUG) << __func__ << ": do nothing and return ok";
1255 return ndk::ScopedAStatus::ok();
1256}
1257
1258ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1259 const std::vector<AudioPortConfig*>& sources __unused,
1260 const std::vector<AudioPortConfig*>& sinks __unused) {
1261 LOG(DEBUG) << __func__ << ": do nothing and return ok";
1262 return ndk::ScopedAStatus::ok();
1263}
1264
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001265} // namespace aidl::android::hardware::audio::core