blob: 71dc4595e34bac6314808bdf48241d434c759d9c [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
Mikhail Naganov872d4a62023-03-09 18:19:01 -080037using aidl::android::hardware::audio::common::getFrameSizeInBytes;
38using aidl::android::hardware::audio::common::isBitPositionFlagSet;
39using aidl::android::hardware::audio::common::isValidAudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000040using aidl::android::hardware::audio::common::SinkMetadata;
41using aidl::android::hardware::audio::common::SourceMetadata;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010042using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000043using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000044using aidl::android::media::audio::common::AudioDevice;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000045using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000046using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000047using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000048using aidl::android::media::audio::common::AudioIoFlags;
jiabin9a8e6862023-01-12 23:06:37 +000049using aidl::android::media::audio::common::AudioMMapPolicy;
50using aidl::android::media::audio::common::AudioMMapPolicyInfo;
51using aidl::android::media::audio::common::AudioMMapPolicyType;
Mikhail Naganov04ae8222023-01-11 15:48:10 -080052using aidl::android::media::audio::common::AudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000053using aidl::android::media::audio::common::AudioOffloadInfo;
54using aidl::android::media::audio::common::AudioOutputFlags;
55using aidl::android::media::audio::common::AudioPort;
56using aidl::android::media::audio::common::AudioPortConfig;
57using aidl::android::media::audio::common::AudioPortExt;
58using aidl::android::media::audio::common::AudioProfile;
Mikhail Naganov20047bc2023-01-05 20:16:07 +000059using aidl::android::media::audio::common::Boolean;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000060using aidl::android::media::audio::common::Int;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080061using aidl::android::media::audio::common::MicrophoneInfo;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000062using aidl::android::media::audio::common::PcmType;
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 Naganovd5536d92023-03-24 18:27:58 -0700146std::ostream& operator<<(std::ostream& os, Module::Type t) {
147 switch (t) {
148 case Module::Type::DEFAULT:
149 os << "default";
150 break;
151 case Module::Type::R_SUBMIX:
152 os << "r_submix";
153 break;
154 case Module::Type::USB:
155 os << "usb";
156 break;
157 }
158 return os;
159}
160
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000161void Module::cleanUpPatch(int32_t patchId) {
162 erase_all_values(mPatches, std::set<int32_t>{patchId});
163}
164
Mikhail Naganov8651b362023-01-06 23:15:27 +0000165ndk::ScopedAStatus Module::createStreamContext(
166 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
167 std::shared_ptr<IStreamCallback> asyncCallback,
168 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000169 if (in_bufferSizeFrames <= 0) {
170 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
171 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
172 }
173 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
174 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
175 << ", must be at least " << kMinimumStreamBufferSizeFrames;
176 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
177 }
178 auto& configs = getConfig().portConfigs;
179 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000180 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000181 // validity of the portConfigId has already been checked.
182 const size_t frameSize =
183 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
184 if (frameSize == 0) {
185 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
186 << portConfigIt->toString();
187 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
188 }
189 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
190 if (frameSize > kMaximumStreamBufferSizeBytes / in_bufferSizeFrames) {
191 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
192 << " frames is too large, maximum size is "
193 << kMaximumStreamBufferSizeBytes / frameSize;
194 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
195 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000196 const auto& flags = portConfigIt->flags.value();
197 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000198 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
199 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000200 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000201 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
202 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000203 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
Mikhail Naganov194daaa2023-01-05 22:34:20 +0000204 mVendorDebug.forceTransientBurst,
205 mVendorDebug.forceSynchronousDrain};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000206 StreamContext temp(
207 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
208 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000209 portConfigIt->format.value(), portConfigIt->channelMask.value(),
jiabin253bd322023-01-25 23:57:31 +0000210 portConfigIt->sampleRate.value().value,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000211 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov8651b362023-01-06 23:15:27 +0000212 asyncCallback, outEventCallback, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000213 if (temp.isValid()) {
214 *out_context = std::move(temp);
215 } else {
216 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
217 }
218 } else {
219 // TODO: Implement simulation of MMAP buffer allocation
220 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000221 return ndk::ScopedAStatus::ok();
222}
223
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000224std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
225 std::vector<AudioDevice> result;
226 auto& ports = getConfig().ports;
227 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
228 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
229 auto portIt = findById<AudioPort>(ports, *it);
230 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
231 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
232 }
233 }
234 return result;
235}
236
237std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
238 std::set<int32_t> result;
239 auto patchIdsRange = mPatches.equal_range(portConfigId);
240 auto& patches = getConfig().patches;
241 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
242 auto patchIt = findById<AudioPatch>(patches, it->second);
243 if (patchIt == patches.end()) {
244 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
245 << "not found in the configuration";
246 }
247 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
248 portConfigId) != patchIt->sourcePortConfigIds.end()) {
249 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
250 } else {
251 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
252 }
253 }
254 return result;
255}
256
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000257ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
258 auto& configs = getConfig().portConfigs;
259 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
260 if (portConfigIt == configs.end()) {
261 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
262 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
263 }
264 const int32_t portId = portConfigIt->portId;
265 // In our implementation, configs of mix ports always have unique IDs.
266 CHECK(portId != in_portConfigId);
267 auto& ports = getConfig().ports;
268 auto portIt = findById<AudioPort>(ports, portId);
269 if (portIt == ports.end()) {
270 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
271 << in_portConfigId << " not found";
272 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
273 }
274 if (mStreams.count(in_portConfigId) != 0) {
275 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
276 << " already has a stream opened on it";
277 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
278 }
279 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
280 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
281 << " does not correspond to a mix port";
282 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
283 }
284 const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
285 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
286 LOG(ERROR) << __func__ << ": port id " << portId
287 << " has already reached maximum allowed opened stream count: "
288 << maxOpenStreamCount;
289 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
290 }
291 *port = &(*portIt);
292 return ndk::ScopedAStatus::ok();
293}
294
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000295template <typename C>
296std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
297 std::set<int32_t> result;
298 auto& portConfigs = getConfig().portConfigs;
299 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
300 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
301 if (portConfigIt != portConfigs.end()) {
302 result.insert(portConfigIt->portId);
303 }
304 }
305 return result;
306}
307
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000308internal::Configuration& Module::getConfig() {
309 if (!mConfig) {
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000310 switch (mType) {
311 case Type::DEFAULT:
312 mConfig = std::move(internal::getPrimaryConfiguration());
313 break;
314 case Type::R_SUBMIX:
315 mConfig = std::move(internal::getRSubmixConfiguration());
316 break;
jiabinb309d8d2023-01-20 19:07:15 +0000317 case Type::USB:
318 mConfig = std::move(internal::getUsbConfiguration());
jiabin253bd322023-01-25 23:57:31 +0000319 break;
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000320 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000321 }
322 return *mConfig;
323}
324
325void Module::registerPatch(const AudioPatch& patch) {
326 auto& configs = getConfig().portConfigs;
327 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
328 for (auto portConfigId : portConfigIds) {
329 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
330 if (configIt != configs.end()) {
331 mPatches.insert(std::pair{portConfigId, patch.id});
332 if (configIt->portId != portConfigId) {
333 mPatches.insert(std::pair{configIt->portId, patch.id});
334 }
335 }
336 };
337 };
338 do_insert(patch.sourcePortConfigIds);
339 do_insert(patch.sinkPortConfigIds);
340}
341
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000342void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) {
343 // Streams from the old patch need to be disconnected, streams from the new
344 // patch need to be connected. If the stream belongs to both patches, no need
345 // to update it.
346 std::set<int32_t> idsToDisconnect, idsToConnect;
347 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
348 oldPatch.sourcePortConfigIds.end());
349 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
350 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
351 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
352 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
353 if (idsToConnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000354 LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected";
355 mStreams.setStreamIsConnected(portConfigId, {});
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000356 }
357 });
358 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
359 if (idsToDisconnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000360 const auto connectedDevices = findConnectedDevices(portConfigId);
361 LOG(DEBUG) << "The stream on port config id " << portConfigId
362 << " is connected to: " << ::android::internal::ToString(connectedDevices);
363 mStreams.setStreamIsConnected(portConfigId, connectedDevices);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000364 }
365 });
366}
367
Mikhail Naganov00603d12022-05-02 22:52:13 +0000368ndk::ScopedAStatus Module::setModuleDebug(
369 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700370 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000371 << ", new flags: " << in_debug.toString();
372 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
373 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700374 LOG(ERROR) << __func__ << ": " << mType
375 << ": attempting to change device connections simulation while having external "
376 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000377 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
378 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000379 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700380 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000381 << in_debug.streamTransientStateDelayMs;
382 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
383 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000384 mDebug = in_debug;
385 return ndk::ScopedAStatus::ok();
386}
387
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000388ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800389 if (!mTelephony) {
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000390 mTelephony = ndk::SharedRefBase::make<Telephony>();
391 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800392 *_aidl_return = mTelephony.getPtr();
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000393 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
394 return ndk::ScopedAStatus::ok();
395}
396
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000397ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800398 if (!mBluetooth) {
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000399 mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000400 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800401 *_aidl_return = mBluetooth.getPtr();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000402 LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get();
403 return ndk::ScopedAStatus::ok();
404}
405
Mikhail Naganov7499a002023-02-27 18:51:44 -0800406ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
407 if (!mBluetoothA2dp) {
408 mBluetoothA2dp = ndk::SharedRefBase::make<BluetoothA2dp>();
409 }
410 *_aidl_return = mBluetoothA2dp.getPtr();
411 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothA2dp: " << _aidl_return->get();
412 return ndk::ScopedAStatus::ok();
413}
414
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800415ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
416 if (!mBluetoothLe) {
417 mBluetoothLe = ndk::SharedRefBase::make<BluetoothLe>();
418 }
419 *_aidl_return = mBluetoothLe.getPtr();
420 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothLe: " << _aidl_return->get();
421 return ndk::ScopedAStatus::ok();
422}
423
Mikhail Naganov00603d12022-05-02 22:52:13 +0000424ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
425 AudioPort* _aidl_return) {
426 const int32_t templateId = in_templateIdAndAdditionalData.id;
427 auto& ports = getConfig().ports;
428 AudioPort connectedPort;
429 { // Scope the template port so that we don't accidentally modify it.
430 auto templateIt = findById<AudioPort>(ports, templateId);
431 if (templateIt == ports.end()) {
432 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
433 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
434 }
435 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
436 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
437 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
438 }
439 if (!templateIt->profiles.empty()) {
440 LOG(ERROR) << __func__ << ": port id " << templateId
441 << " does not have dynamic profiles";
442 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
443 }
444 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
445 if (templateDevicePort.device.type.connection.empty()) {
446 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
447 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
448 }
449 // Postpone id allocation until we ensure that there are no client errors.
450 connectedPort = *templateIt;
451 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
452 const auto& inputDevicePort =
453 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
454 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
455 connectedDevicePort.device.address = inputDevicePort.device.address;
456 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
457 << connectedDevicePort.device.toString();
458 // Check if there is already a connected port with for the same external device.
459 for (auto connectedPortId : mConnectedDevicePorts) {
460 auto connectedPortIt = findById<AudioPort>(ports, connectedPortId);
461 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
462 connectedDevicePort.device) {
463 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
464 << " is already connected at the device port id " << connectedPortId;
465 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
466 }
467 }
468 }
469
470 if (!mDebug.simulateDeviceConnections) {
471 // In a real HAL here we would attempt querying the profiles from the device.
472 LOG(ERROR) << __func__ << ": failed to query supported device profiles";
jiabin253bd322023-01-25 23:57:31 +0000473 // TODO: Check the return value when it is ready for actual devices.
474 populateConnectedDevicePort(&connectedPort);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000475 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
476 }
477
478 connectedPort.id = ++getConfig().nextPortId;
479 mConnectedDevicePorts.insert(connectedPort.id);
480 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
481 << "connected port ID " << connectedPort.id;
482 auto& connectedProfiles = getConfig().connectedProfiles;
483 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
484 connectedProfilesIt != connectedProfiles.end()) {
485 connectedPort.profiles = connectedProfilesIt->second;
486 }
487 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000488 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000489 *_aidl_return = std::move(connectedPort);
490
491 std::vector<AudioRoute> newRoutes;
492 auto& routes = getConfig().routes;
493 for (auto& r : routes) {
494 if (r.sinkPortId == templateId) {
495 AudioRoute newRoute;
496 newRoute.sourcePortIds = r.sourcePortIds;
497 newRoute.sinkPortId = connectedPort.id;
498 newRoute.isExclusive = r.isExclusive;
499 newRoutes.push_back(std::move(newRoute));
500 } else {
501 auto& srcs = r.sourcePortIds;
502 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
503 srcs.push_back(connectedPort.id);
504 }
505 }
506 }
507 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
508
509 return ndk::ScopedAStatus::ok();
510}
511
512ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
513 auto& ports = getConfig().ports;
514 auto portIt = findById<AudioPort>(ports, in_portId);
515 if (portIt == ports.end()) {
516 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
517 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
518 }
519 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
520 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
521 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
522 }
523 if (mConnectedDevicePorts.count(in_portId) == 0) {
524 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
525 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
526 }
527 auto& configs = getConfig().portConfigs;
528 auto& initials = getConfig().initialConfigs;
529 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
530 if (config.portId == in_portId) {
531 // Check if the configuration was provided by the client.
532 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
533 return initialIt == initials.end() || config != *initialIt;
534 }
535 return false;
536 });
537 if (configIt != configs.end()) {
538 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
539 << configIt->id;
540 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
541 }
jiabin783c48b2023-02-28 18:28:06 +0000542 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000543 ports.erase(portIt);
544 mConnectedDevicePorts.erase(in_portId);
545 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
546
547 auto& routes = getConfig().routes;
548 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
549 if (routesIt->sinkPortId == in_portId) {
550 routesIt = routes.erase(routesIt);
551 } else {
552 // Note: the list of sourcePortIds can't become empty because there must
553 // be the id of the template port in the route.
554 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
555 ++routesIt;
556 }
557 }
558
559 return ndk::ScopedAStatus::ok();
560}
561
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000562ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
563 *_aidl_return = getConfig().patches;
564 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
565 return ndk::ScopedAStatus::ok();
566}
567
568ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
569 auto& ports = getConfig().ports;
570 auto portIt = findById<AudioPort>(ports, in_portId);
571 if (portIt != ports.end()) {
572 *_aidl_return = *portIt;
573 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
574 return ndk::ScopedAStatus::ok();
575 }
576 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
577 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
578}
579
580ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
581 *_aidl_return = getConfig().portConfigs;
582 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
583 return ndk::ScopedAStatus::ok();
584}
585
586ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
587 *_aidl_return = getConfig().ports;
588 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
589 return ndk::ScopedAStatus::ok();
590}
591
592ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
593 *_aidl_return = getConfig().routes;
594 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
595 return ndk::ScopedAStatus::ok();
596}
597
Mikhail Naganov00603d12022-05-02 22:52:13 +0000598ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
599 std::vector<AudioRoute>* _aidl_return) {
600 auto& ports = getConfig().ports;
601 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
602 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
603 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
604 }
605 auto& routes = getConfig().routes;
606 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
607 [&](const auto& r) {
608 const auto& srcs = r.sourcePortIds;
609 return r.sinkPortId == in_portId ||
610 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
611 });
612 return ndk::ScopedAStatus::ok();
613}
614
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000615ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
616 OpenInputStreamReturn* _aidl_return) {
617 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
618 << in_args.bufferSizeFrames << " frames";
619 AudioPort* port = nullptr;
620 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
621 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000622 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000623 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
624 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000625 << " does not correspond to an input mix port";
626 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
627 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000628 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000629 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000630 nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000631 !status.isOk()) {
632 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000633 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000634 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000635 std::shared_ptr<StreamIn> stream;
jiabin253bd322023-01-25 23:57:31 +0000636 ndk::ScopedAStatus status = getStreamInCreator(mType)(in_args.sinkMetadata, std::move(context),
637 mConfig->microphones, &stream);
638 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000639 return status;
640 }
641 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000642 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
643 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000644 auto patchIt = mPatches.find(in_args.portConfigId);
645 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000646 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000647 }
648 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000649 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000650 return ndk::ScopedAStatus::ok();
651}
652
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000653ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
654 OpenOutputStreamReturn* _aidl_return) {
655 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
656 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
657 << " frames";
658 AudioPort* port = nullptr;
659 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
660 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000661 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000662 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
663 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000664 << " does not correspond to an output mix port";
665 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
666 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000667 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
668 AudioOutputFlags::COMPRESS_OFFLOAD);
669 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000670 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000671 << " has COMPRESS_OFFLOAD flag set, requires offload info";
672 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
673 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000674 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
675 AudioOutputFlags::NON_BLOCKING);
676 if (isNonBlocking && in_args.callback == nullptr) {
677 LOG(ERROR) << __func__ << ": port id " << port->id
678 << " has NON_BLOCKING flag set, requires async callback";
679 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
680 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000681 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000682 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000683 isNonBlocking ? in_args.callback : nullptr,
684 in_args.eventCallback, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000685 !status.isOk()) {
686 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000687 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000688 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000689 std::shared_ptr<StreamOut> stream;
jiabin253bd322023-01-25 23:57:31 +0000690 ndk::ScopedAStatus status = getStreamOutCreator(mType)(
691 in_args.sourceMetadata, std::move(context), in_args.offloadInfo, &stream);
692 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000693 return status;
694 }
695 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000696 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
697 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000698 auto patchIt = mPatches.find(in_args.portConfigId);
699 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000700 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000701 }
702 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000703 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000704 return ndk::ScopedAStatus::ok();
705}
706
Mikhail Naganov74927202022-12-19 16:37:14 +0000707ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
708 SupportedPlaybackRateFactors* _aidl_return) {
709 LOG(DEBUG) << __func__;
710 (void)_aidl_return;
711 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
712}
713
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000714ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000715 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000716 if (in_requested.sourcePortConfigIds.empty()) {
717 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
718 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
719 }
720 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
721 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
722 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
723 }
724 if (in_requested.sinkPortConfigIds.empty()) {
725 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
726 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
727 }
728 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
729 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
730 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
731 }
732
733 auto& configs = getConfig().portConfigs;
734 std::vector<int32_t> missingIds;
735 auto sources =
736 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
737 if (!missingIds.empty()) {
738 LOG(ERROR) << __func__ << ": following source port config ids not found: "
739 << ::android::internal::ToString(missingIds);
740 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
741 }
742 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
743 if (!missingIds.empty()) {
744 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
745 << ::android::internal::ToString(missingIds);
746 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
747 }
748 // bool indicates whether a non-exclusive route is available.
749 // If only an exclusive route is available, that means the patch can not be
750 // established if there is any other patch which currently uses the sink port.
751 std::map<int32_t, bool> allowedSinkPorts;
752 auto& routes = getConfig().routes;
753 for (auto src : sources) {
754 for (const auto& r : routes) {
755 const auto& srcs = r.sourcePortIds;
756 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
757 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
758 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
759 }
760 }
761 }
762 }
763 for (auto sink : sinks) {
764 if (allowedSinkPorts.count(sink->portId) == 0) {
765 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
766 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
767 }
768 }
769
jiabin253bd322023-01-25 23:57:31 +0000770 if (auto status = checkAudioPatchEndpointsMatch(sources, sinks); !status.isOk()) {
771 return status;
772 }
773
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000774 auto& patches = getConfig().patches;
775 auto existing = patches.end();
776 std::optional<decltype(mPatches)> patchesBackup;
777 if (in_requested.id != 0) {
778 existing = findById<AudioPatch>(patches, in_requested.id);
779 if (existing != patches.end()) {
780 patchesBackup = mPatches;
781 cleanUpPatch(existing->id);
782 } else {
783 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
784 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
785 }
786 }
787 // Validate the requested patch.
788 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
789 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
790 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
791 << "is exclusive and is already used by some other patch";
792 if (patchesBackup.has_value()) {
793 mPatches = std::move(*patchesBackup);
794 }
795 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
796 }
797 }
798 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000799 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
800 _aidl_return->latenciesMs.clear();
801 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
802 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000803 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000804 if (existing == patches.end()) {
805 _aidl_return->id = getConfig().nextPatchId++;
806 patches.push_back(*_aidl_return);
807 existing = patches.begin() + (patches.size() - 1);
808 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000809 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000810 *existing = *_aidl_return;
811 }
812 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000813 updateStreamsConnectedState(oldPatch, *_aidl_return);
814
815 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
816 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000817 return ndk::ScopedAStatus::ok();
818}
819
820ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
821 AudioPortConfig* out_suggested, bool* _aidl_return) {
822 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
823 auto& configs = getConfig().portConfigs;
824 auto existing = configs.end();
825 if (in_requested.id != 0) {
826 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
827 existing == configs.end()) {
828 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
829 << " not found";
830 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
831 }
832 }
833
834 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
835 if (portId == 0) {
836 LOG(ERROR) << __func__ << ": input port config does not specify portId";
837 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
838 }
839 auto& ports = getConfig().ports;
840 auto portIt = findById<AudioPort>(ports, portId);
841 if (portIt == ports.end()) {
842 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
843 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
844 }
845 if (existing != configs.end()) {
846 *out_suggested = *existing;
847 } else {
848 AudioPortConfig newConfig;
849 if (generateDefaultPortConfig(*portIt, &newConfig)) {
850 *out_suggested = newConfig;
851 } else {
852 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
853 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
854 }
855 }
856 // From this moment, 'out_suggested' is either an existing port config,
857 // or a new generated config. Now attempt to update it according to the specified
858 // fields of 'in_requested'.
859
860 bool requestedIsValid = true, requestedIsFullySpecified = true;
861
862 AudioIoFlags portFlags = portIt->flags;
863 if (in_requested.flags.has_value()) {
864 if (in_requested.flags.value() != portFlags) {
865 LOG(WARNING) << __func__ << ": requested flags "
866 << in_requested.flags.value().toString() << " do not match port's "
867 << portId << " flags " << portFlags.toString();
868 requestedIsValid = false;
869 }
870 } else {
871 requestedIsFullySpecified = false;
872 }
873
874 AudioProfile portProfile;
875 if (in_requested.format.has_value()) {
876 const auto& format = in_requested.format.value();
877 if (findAudioProfile(*portIt, format, &portProfile)) {
878 out_suggested->format = format;
879 } else {
880 LOG(WARNING) << __func__ << ": requested format " << format.toString()
881 << " is not found in port's " << portId << " profiles";
882 requestedIsValid = false;
883 }
884 } else {
885 requestedIsFullySpecified = false;
886 }
887 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
888 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
889 << out_suggested->format.value().toString() << " anymore";
890 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
891 }
892
893 if (in_requested.channelMask.has_value()) {
894 const auto& channelMask = in_requested.channelMask.value();
895 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
896 portProfile.channelMasks.end()) {
897 out_suggested->channelMask = channelMask;
898 } else {
899 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
900 << " is not supported for the format " << portProfile.format.toString()
901 << " by the port " << portId;
902 requestedIsValid = false;
903 }
904 } else {
905 requestedIsFullySpecified = false;
906 }
907
908 if (in_requested.sampleRate.has_value()) {
909 const auto& sampleRate = in_requested.sampleRate.value();
910 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
911 sampleRate.value) != portProfile.sampleRates.end()) {
912 out_suggested->sampleRate = sampleRate;
913 } else {
914 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
915 << " is not supported for the format " << portProfile.format.toString()
916 << " by the port " << portId;
917 requestedIsValid = false;
918 }
919 } else {
920 requestedIsFullySpecified = false;
921 }
922
923 if (in_requested.gain.has_value()) {
924 // Let's pretend that gain can always be applied.
925 out_suggested->gain = in_requested.gain.value();
926 }
927
Mikhail Naganov248e9502023-02-21 16:32:40 -0800928 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
929 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
930 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
931 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
932 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
933 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
934 }
935 } else {
936 LOG(WARNING) << __func__ << ": requested ext tag "
937 << toString(in_requested.ext.getTag()) << " do not match port's tag "
938 << toString(out_suggested->ext.getTag());
939 requestedIsValid = false;
940 }
941 }
942
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000943 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
944 out_suggested->id = getConfig().nextPortId++;
945 configs.push_back(*out_suggested);
946 *_aidl_return = true;
947 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
948 } else if (existing != configs.end() && requestedIsValid) {
949 *existing = *out_suggested;
950 *_aidl_return = true;
951 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
952 } else {
953 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
954 << "; requested is valid? " << requestedIsValid << ", fully specified? "
955 << requestedIsFullySpecified;
956 *_aidl_return = false;
957 }
958 return ndk::ScopedAStatus::ok();
959}
960
961ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
962 auto& patches = getConfig().patches;
963 auto patchIt = findById<AudioPatch>(patches, in_patchId);
964 if (patchIt != patches.end()) {
965 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000966 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000967 patches.erase(patchIt);
968 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
969 return ndk::ScopedAStatus::ok();
970 }
971 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
972 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
973}
974
975ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
976 auto& configs = getConfig().portConfigs;
977 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
978 if (configIt != configs.end()) {
979 if (mStreams.count(in_portConfigId) != 0) {
980 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
981 << " has a stream opened on it";
982 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
983 }
984 auto patchIt = mPatches.find(in_portConfigId);
985 if (patchIt != mPatches.end()) {
986 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
987 << " is used by the patch with id " << patchIt->second;
988 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
989 }
990 auto& initials = getConfig().initialConfigs;
991 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
992 if (initialIt == initials.end()) {
993 configs.erase(configIt);
994 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
995 } else if (*configIt != *initialIt) {
996 *configIt = *initialIt;
997 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
998 }
999 return ndk::ScopedAStatus::ok();
1000 }
1001 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1002 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1003}
1004
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001005ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1006 *_aidl_return = mMasterMute;
1007 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1008 return ndk::ScopedAStatus::ok();
1009}
1010
1011ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1012 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001013 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1014 : onMasterMuteChanged(in_mute);
1015 if (result.isOk()) {
1016 mMasterMute = in_mute;
1017 } else {
1018 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1019 << "), error=" << result;
1020 // Reset master mute if it failed.
1021 onMasterMuteChanged(mMasterMute);
1022 }
1023 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001024}
1025
1026ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1027 *_aidl_return = mMasterVolume;
1028 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1029 return ndk::ScopedAStatus::ok();
1030}
1031
1032ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1033 LOG(DEBUG) << __func__ << ": " << in_volume;
1034 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001035 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1036 : onMasterVolumeChanged(in_volume);
1037 if (result.isOk()) {
1038 mMasterVolume = in_volume;
1039 } else {
1040 // Reset master volume if it failed.
1041 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1042 << "), error=" << result;
1043 onMasterVolumeChanged(mMasterVolume);
1044 }
1045 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001046 }
1047 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1048 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1049}
1050
1051ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1052 *_aidl_return = mMicMute;
1053 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1054 return ndk::ScopedAStatus::ok();
1055}
1056
1057ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1058 LOG(DEBUG) << __func__ << ": " << in_mute;
1059 mMicMute = in_mute;
1060 return ndk::ScopedAStatus::ok();
1061}
1062
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001063ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001064 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001065 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1066 return ndk::ScopedAStatus::ok();
1067}
1068
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001069ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001070 if (!isValidAudioMode(in_mode)) {
1071 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1072 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1073 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001074 // No checks for supported audio modes here, it's an informative notification.
1075 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1076 return ndk::ScopedAStatus::ok();
1077}
1078
1079ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1080 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1081 return ndk::ScopedAStatus::ok();
1082}
1083
1084ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1085 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1086 return ndk::ScopedAStatus::ok();
1087}
1088
Vlad Popa83a6d822022-11-07 13:53:57 +01001089ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001090 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001091 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001092 }
Mikhail Naganov7499a002023-02-27 18:51:44 -08001093 *_aidl_return = mSoundDose.getPtr();
Vlad Popa943b7e22022-12-08 14:24:12 +01001094 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001095 return ndk::ScopedAStatus::ok();
1096}
1097
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001098ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1099 LOG(DEBUG) << __func__;
1100 (void)_aidl_return;
1101 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1102}
1103
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001104const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001105const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001106
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001107ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1108 std::vector<VendorParameter>* _aidl_return) {
1109 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001110 bool allParametersKnown = true;
1111 for (const auto& id : in_ids) {
1112 if (id == VendorDebug::kForceTransientBurstName) {
1113 VendorParameter forceTransientBurst{.id = id};
1114 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1115 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001116 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1117 VendorParameter forceSynchronousDrain{.id = id};
1118 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1119 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001120 } else {
1121 allParametersKnown = false;
1122 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1123 }
1124 }
1125 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1126 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001127}
1128
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001129namespace {
1130
1131template <typename W>
1132bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1133 std::optional<W> value;
1134 binder_status_t result = p.ext.getParcelable(&value);
1135 if (result == STATUS_OK && value.has_value()) {
1136 *v = value.value().value;
1137 return true;
1138 }
1139 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1140 << "\": " << result;
1141 return false;
1142}
1143
1144} // namespace
1145
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001146ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1147 bool in_async) {
1148 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1149 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001150 bool allParametersKnown = true;
1151 for (const auto& p : in_parameters) {
1152 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001153 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1154 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1155 }
1156 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1157 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001158 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1159 }
1160 } else {
1161 allParametersKnown = false;
1162 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1163 }
1164 }
1165 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1166 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001167}
1168
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001169ndk::ScopedAStatus Module::addDeviceEffect(
1170 int32_t in_portConfigId,
1171 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1172 if (in_effect == nullptr) {
1173 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1174 } else {
1175 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1176 << in_effect->asBinder().get();
1177 }
1178 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1179}
1180
1181ndk::ScopedAStatus Module::removeDeviceEffect(
1182 int32_t in_portConfigId,
1183 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1184 if (in_effect == nullptr) {
1185 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1186 } else {
1187 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1188 << in_effect->asBinder().get();
1189 }
1190 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1191}
1192
jiabin9a8e6862023-01-12 23:06:37 +00001193ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1194 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1195 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1196 std::set<int32_t> mmapSinks;
1197 std::set<int32_t> mmapSources;
1198 auto& ports = getConfig().ports;
1199 for (const auto& port : ports) {
1200 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1201 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1202 AudioInputFlags::MMAP_NOIRQ)) {
1203 mmapSinks.insert(port.id);
1204 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1205 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1206 AudioOutputFlags::MMAP_NOIRQ)) {
1207 mmapSources.insert(port.id);
1208 }
1209 }
1210 for (const auto& route : getConfig().routes) {
1211 if (mmapSinks.count(route.sinkPortId) != 0) {
1212 // The sink is a mix port, add the sources if they are device ports.
1213 for (int sourcePortId : route.sourcePortIds) {
1214 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1215 if (sourcePortIt == ports.end()) {
1216 // This must not happen
1217 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1218 continue;
1219 }
1220 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1221 // The source is not a device port, skip
1222 continue;
1223 }
1224 AudioMMapPolicyInfo policyInfo;
1225 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1226 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1227 // default implementation.
1228 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1229 _aidl_return->push_back(policyInfo);
1230 }
1231 } else {
1232 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1233 if (sinkPortIt == ports.end()) {
1234 // This must not happen
1235 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1236 continue;
1237 }
1238 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1239 // The sink is not a device port, skip
1240 continue;
1241 }
1242 if (count_any(mmapSources, route.sourcePortIds)) {
1243 AudioMMapPolicyInfo policyInfo;
1244 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1245 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1246 // default implementation.
1247 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1248 _aidl_return->push_back(policyInfo);
1249 }
1250 }
1251 }
1252 return ndk::ScopedAStatus::ok();
1253}
1254
Eric Laurente2432ea2023-01-12 17:47:31 +01001255ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1256 LOG(DEBUG) << __func__;
1257 *_aidl_return = false;
1258 return ndk::ScopedAStatus::ok();
1259}
1260
jiabinb76981e2023-01-18 00:58:30 +00001261ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1262 if (!isMmapSupported()) {
1263 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1264 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1265 }
1266 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1267 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1268 return ndk::ScopedAStatus::ok();
1269}
1270
1271ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1272 if (!isMmapSupported()) {
1273 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1274 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1275 }
1276 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1277 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1278 return ndk::ScopedAStatus::ok();
1279}
1280
1281bool Module::isMmapSupported() {
1282 if (mIsMmapSupported.has_value()) {
1283 return mIsMmapSupported.value();
1284 }
1285 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1286 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1287 mIsMmapSupported = false;
1288 } else {
1289 mIsMmapSupported =
1290 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1291 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1292 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1293 }) != mmapPolicyInfos.end();
1294 }
1295 return mIsMmapSupported.value();
1296}
1297
jiabin253bd322023-01-25 23:57:31 +00001298ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001299 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001300 return ndk::ScopedAStatus::ok();
1301}
1302
1303ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1304 const std::vector<AudioPortConfig*>& sources __unused,
1305 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001306 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001307 return ndk::ScopedAStatus::ok();
1308}
1309
jiabin783c48b2023-02-28 18:28:06 +00001310void Module::onExternalDeviceConnectionChanged(
1311 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1312 bool connected __unused) {
1313 LOG(DEBUG) << __func__ << ": do nothing and return";
1314}
1315
1316ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1317 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1318 return ndk::ScopedAStatus::ok();
1319}
1320
1321ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1322 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1323 return ndk::ScopedAStatus::ok();
1324}
1325
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001326} // namespace aidl::android::hardware::audio::core