blob: 6885a49d6b9bf9481a6a6419e374b8b04b9af141 [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";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700190 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000191 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 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700284 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000285 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.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700459 for (auto connectedPortPair : mConnectedDevicePorts) {
460 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000461 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
462 connectedDevicePort.device) {
463 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700464 << " is already connected at the device port id "
465 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000466 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
467 }
468 }
469 }
470
471 if (!mDebug.simulateDeviceConnections) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700472 if (ndk::ScopedAStatus status = populateConnectedDevicePort(&connectedPort);
473 !status.isOk()) {
474 return status;
475 }
476 } else {
477 auto& connectedProfiles = getConfig().connectedProfiles;
478 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
479 connectedProfilesIt != connectedProfiles.end()) {
480 connectedPort.profiles = connectedProfilesIt->second;
481 }
482 }
483 if (connectedPort.profiles.empty()) {
484 LOG(ERROR) << "Profiles of a connected port still empty after connecting external device "
485 << connectedPort.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000486 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
487 }
488
489 connectedPort.id = ++getConfig().nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700490 auto [connectedPortsIt, _] =
491 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::vector<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000492 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
493 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000494 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000495 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000496
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700497 std::vector<int32_t> routablePortIds;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000498 std::vector<AudioRoute> newRoutes;
499 auto& routes = getConfig().routes;
500 for (auto& r : routes) {
501 if (r.sinkPortId == templateId) {
502 AudioRoute newRoute;
503 newRoute.sourcePortIds = r.sourcePortIds;
504 newRoute.sinkPortId = connectedPort.id;
505 newRoute.isExclusive = r.isExclusive;
506 newRoutes.push_back(std::move(newRoute));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700507 routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(),
508 r.sourcePortIds.end());
Mikhail Naganov00603d12022-05-02 22:52:13 +0000509 } else {
510 auto& srcs = r.sourcePortIds;
511 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
512 srcs.push_back(connectedPort.id);
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700513 routablePortIds.push_back(r.sinkPortId);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000514 }
515 }
516 }
517 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
518
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700519 // Note: this is a simplistic approach assuming that a mix port can only be populated
520 // from a single device port. Implementing support for stuffing dynamic profiles with a superset
521 // of all profiles from all routable dynamic device ports would be more involved.
522 for (const auto mixPortId : routablePortIds) {
523 auto portsIt = findById<AudioPort>(ports, mixPortId);
524 if (portsIt != ports.end() && portsIt->profiles.empty()) {
525 portsIt->profiles = connectedPort.profiles;
526 connectedPortsIt->second.push_back(portsIt->id);
527 }
528 }
529 *_aidl_return = std::move(connectedPort);
530
Mikhail Naganov00603d12022-05-02 22:52:13 +0000531 return ndk::ScopedAStatus::ok();
532}
533
534ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
535 auto& ports = getConfig().ports;
536 auto portIt = findById<AudioPort>(ports, in_portId);
537 if (portIt == ports.end()) {
538 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
539 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
540 }
541 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
542 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
543 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
544 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700545 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
546 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000547 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
548 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
549 }
550 auto& configs = getConfig().portConfigs;
551 auto& initials = getConfig().initialConfigs;
552 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
553 if (config.portId == in_portId) {
554 // Check if the configuration was provided by the client.
555 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
556 return initialIt == initials.end() || config != *initialIt;
557 }
558 return false;
559 });
560 if (configIt != configs.end()) {
561 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
562 << configIt->id;
563 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
564 }
jiabin783c48b2023-02-28 18:28:06 +0000565 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000566 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000567 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
568
569 auto& routes = getConfig().routes;
570 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
571 if (routesIt->sinkPortId == in_portId) {
572 routesIt = routes.erase(routesIt);
573 } else {
574 // Note: the list of sourcePortIds can't become empty because there must
575 // be the id of the template port in the route.
576 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
577 ++routesIt;
578 }
579 }
580
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700581 for (const auto mixPortId : connectedPortsIt->second) {
582 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
583 if (mixPortIt != ports.end()) {
584 mixPortIt->profiles = {};
585 }
586 }
587 mConnectedDevicePorts.erase(connectedPortsIt);
588
Mikhail Naganov00603d12022-05-02 22:52:13 +0000589 return ndk::ScopedAStatus::ok();
590}
591
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000592ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
593 *_aidl_return = getConfig().patches;
594 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
595 return ndk::ScopedAStatus::ok();
596}
597
598ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
599 auto& ports = getConfig().ports;
600 auto portIt = findById<AudioPort>(ports, in_portId);
601 if (portIt != ports.end()) {
602 *_aidl_return = *portIt;
603 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
604 return ndk::ScopedAStatus::ok();
605 }
606 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
607 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
608}
609
610ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
611 *_aidl_return = getConfig().portConfigs;
612 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
613 return ndk::ScopedAStatus::ok();
614}
615
616ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
617 *_aidl_return = getConfig().ports;
618 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
619 return ndk::ScopedAStatus::ok();
620}
621
622ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
623 *_aidl_return = getConfig().routes;
624 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
625 return ndk::ScopedAStatus::ok();
626}
627
Mikhail Naganov00603d12022-05-02 22:52:13 +0000628ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
629 std::vector<AudioRoute>* _aidl_return) {
630 auto& ports = getConfig().ports;
631 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
632 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
633 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
634 }
635 auto& routes = getConfig().routes;
636 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
637 [&](const auto& r) {
638 const auto& srcs = r.sourcePortIds;
639 return r.sinkPortId == in_portId ||
640 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
641 });
642 return ndk::ScopedAStatus::ok();
643}
644
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000645ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
646 OpenInputStreamReturn* _aidl_return) {
647 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
648 << in_args.bufferSizeFrames << " frames";
649 AudioPort* port = nullptr;
650 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
651 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000652 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000653 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
654 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000655 << " does not correspond to an input mix port";
656 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
657 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000658 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000659 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000660 nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000661 !status.isOk()) {
662 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000663 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000664 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000665 std::shared_ptr<StreamIn> stream;
jiabin253bd322023-01-25 23:57:31 +0000666 ndk::ScopedAStatus status = getStreamInCreator(mType)(in_args.sinkMetadata, std::move(context),
667 mConfig->microphones, &stream);
668 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000669 return status;
670 }
671 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000672 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
673 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000674 auto patchIt = mPatches.find(in_args.portConfigId);
675 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000676 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000677 }
678 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000679 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000680 return ndk::ScopedAStatus::ok();
681}
682
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000683ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
684 OpenOutputStreamReturn* _aidl_return) {
685 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
686 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
687 << " frames";
688 AudioPort* port = nullptr;
689 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
690 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000691 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000692 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
693 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000694 << " does not correspond to an output mix port";
695 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
696 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000697 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
698 AudioOutputFlags::COMPRESS_OFFLOAD);
699 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000700 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000701 << " has COMPRESS_OFFLOAD flag set, requires offload info";
702 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
703 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000704 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
705 AudioOutputFlags::NON_BLOCKING);
706 if (isNonBlocking && in_args.callback == nullptr) {
707 LOG(ERROR) << __func__ << ": port id " << port->id
708 << " has NON_BLOCKING flag set, requires async callback";
709 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
710 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000711 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000712 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000713 isNonBlocking ? in_args.callback : nullptr,
714 in_args.eventCallback, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000715 !status.isOk()) {
716 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000717 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000718 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000719 std::shared_ptr<StreamOut> stream;
jiabin253bd322023-01-25 23:57:31 +0000720 ndk::ScopedAStatus status = getStreamOutCreator(mType)(
721 in_args.sourceMetadata, std::move(context), in_args.offloadInfo, &stream);
722 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000723 return status;
724 }
725 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000726 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
727 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000728 auto patchIt = mPatches.find(in_args.portConfigId);
729 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000730 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000731 }
732 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000733 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000734 return ndk::ScopedAStatus::ok();
735}
736
Mikhail Naganov74927202022-12-19 16:37:14 +0000737ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
738 SupportedPlaybackRateFactors* _aidl_return) {
739 LOG(DEBUG) << __func__;
740 (void)_aidl_return;
741 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
742}
743
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000744ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000745 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000746 if (in_requested.sourcePortConfigIds.empty()) {
747 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
748 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
749 }
750 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
751 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
752 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
753 }
754 if (in_requested.sinkPortConfigIds.empty()) {
755 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
756 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
757 }
758 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
759 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
760 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
761 }
762
763 auto& configs = getConfig().portConfigs;
764 std::vector<int32_t> missingIds;
765 auto sources =
766 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
767 if (!missingIds.empty()) {
768 LOG(ERROR) << __func__ << ": following source port config ids not found: "
769 << ::android::internal::ToString(missingIds);
770 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
771 }
772 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
773 if (!missingIds.empty()) {
774 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
775 << ::android::internal::ToString(missingIds);
776 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
777 }
778 // bool indicates whether a non-exclusive route is available.
779 // If only an exclusive route is available, that means the patch can not be
780 // established if there is any other patch which currently uses the sink port.
781 std::map<int32_t, bool> allowedSinkPorts;
782 auto& routes = getConfig().routes;
783 for (auto src : sources) {
784 for (const auto& r : routes) {
785 const auto& srcs = r.sourcePortIds;
786 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
787 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
788 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
789 }
790 }
791 }
792 }
793 for (auto sink : sinks) {
794 if (allowedSinkPorts.count(sink->portId) == 0) {
795 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
796 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
797 }
798 }
799
jiabin253bd322023-01-25 23:57:31 +0000800 if (auto status = checkAudioPatchEndpointsMatch(sources, sinks); !status.isOk()) {
801 return status;
802 }
803
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000804 auto& patches = getConfig().patches;
805 auto existing = patches.end();
806 std::optional<decltype(mPatches)> patchesBackup;
807 if (in_requested.id != 0) {
808 existing = findById<AudioPatch>(patches, in_requested.id);
809 if (existing != patches.end()) {
810 patchesBackup = mPatches;
811 cleanUpPatch(existing->id);
812 } else {
813 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
814 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
815 }
816 }
817 // Validate the requested patch.
818 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
819 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
820 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
821 << "is exclusive and is already used by some other patch";
822 if (patchesBackup.has_value()) {
823 mPatches = std::move(*patchesBackup);
824 }
825 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
826 }
827 }
828 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000829 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
830 _aidl_return->latenciesMs.clear();
831 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
832 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000833 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000834 if (existing == patches.end()) {
835 _aidl_return->id = getConfig().nextPatchId++;
836 patches.push_back(*_aidl_return);
837 existing = patches.begin() + (patches.size() - 1);
838 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000839 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000840 *existing = *_aidl_return;
841 }
842 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000843 updateStreamsConnectedState(oldPatch, *_aidl_return);
844
845 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
846 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000847 return ndk::ScopedAStatus::ok();
848}
849
850ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
851 AudioPortConfig* out_suggested, bool* _aidl_return) {
852 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
853 auto& configs = getConfig().portConfigs;
854 auto existing = configs.end();
855 if (in_requested.id != 0) {
856 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
857 existing == configs.end()) {
858 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
859 << " not found";
860 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
861 }
862 }
863
864 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
865 if (portId == 0) {
866 LOG(ERROR) << __func__ << ": input port config does not specify portId";
867 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
868 }
869 auto& ports = getConfig().ports;
870 auto portIt = findById<AudioPort>(ports, portId);
871 if (portIt == ports.end()) {
872 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
873 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
874 }
875 if (existing != configs.end()) {
876 *out_suggested = *existing;
877 } else {
878 AudioPortConfig newConfig;
879 if (generateDefaultPortConfig(*portIt, &newConfig)) {
880 *out_suggested = newConfig;
881 } else {
882 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
883 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
884 }
885 }
886 // From this moment, 'out_suggested' is either an existing port config,
887 // or a new generated config. Now attempt to update it according to the specified
888 // fields of 'in_requested'.
889
890 bool requestedIsValid = true, requestedIsFullySpecified = true;
891
892 AudioIoFlags portFlags = portIt->flags;
893 if (in_requested.flags.has_value()) {
894 if (in_requested.flags.value() != portFlags) {
895 LOG(WARNING) << __func__ << ": requested flags "
896 << in_requested.flags.value().toString() << " do not match port's "
897 << portId << " flags " << portFlags.toString();
898 requestedIsValid = false;
899 }
900 } else {
901 requestedIsFullySpecified = false;
902 }
903
904 AudioProfile portProfile;
905 if (in_requested.format.has_value()) {
906 const auto& format = in_requested.format.value();
907 if (findAudioProfile(*portIt, format, &portProfile)) {
908 out_suggested->format = format;
909 } else {
910 LOG(WARNING) << __func__ << ": requested format " << format.toString()
911 << " is not found in port's " << portId << " profiles";
912 requestedIsValid = false;
913 }
914 } else {
915 requestedIsFullySpecified = false;
916 }
917 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
918 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
919 << out_suggested->format.value().toString() << " anymore";
920 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
921 }
922
923 if (in_requested.channelMask.has_value()) {
924 const auto& channelMask = in_requested.channelMask.value();
925 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
926 portProfile.channelMasks.end()) {
927 out_suggested->channelMask = channelMask;
928 } else {
929 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
930 << " is not supported for the format " << portProfile.format.toString()
931 << " by the port " << portId;
932 requestedIsValid = false;
933 }
934 } else {
935 requestedIsFullySpecified = false;
936 }
937
938 if (in_requested.sampleRate.has_value()) {
939 const auto& sampleRate = in_requested.sampleRate.value();
940 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
941 sampleRate.value) != portProfile.sampleRates.end()) {
942 out_suggested->sampleRate = sampleRate;
943 } else {
944 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
945 << " is not supported for the format " << portProfile.format.toString()
946 << " by the port " << portId;
947 requestedIsValid = false;
948 }
949 } else {
950 requestedIsFullySpecified = false;
951 }
952
953 if (in_requested.gain.has_value()) {
954 // Let's pretend that gain can always be applied.
955 out_suggested->gain = in_requested.gain.value();
956 }
957
Mikhail Naganov248e9502023-02-21 16:32:40 -0800958 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
959 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
960 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
961 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
962 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
963 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
964 }
965 } else {
966 LOG(WARNING) << __func__ << ": requested ext tag "
967 << toString(in_requested.ext.getTag()) << " do not match port's tag "
968 << toString(out_suggested->ext.getTag());
969 requestedIsValid = false;
970 }
971 }
972
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000973 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
974 out_suggested->id = getConfig().nextPortId++;
975 configs.push_back(*out_suggested);
976 *_aidl_return = true;
977 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
978 } else if (existing != configs.end() && requestedIsValid) {
979 *existing = *out_suggested;
980 *_aidl_return = true;
981 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
982 } else {
983 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
984 << "; requested is valid? " << requestedIsValid << ", fully specified? "
985 << requestedIsFullySpecified;
986 *_aidl_return = false;
987 }
988 return ndk::ScopedAStatus::ok();
989}
990
991ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
992 auto& patches = getConfig().patches;
993 auto patchIt = findById<AudioPatch>(patches, in_patchId);
994 if (patchIt != patches.end()) {
995 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000996 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000997 patches.erase(patchIt);
998 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
999 return ndk::ScopedAStatus::ok();
1000 }
1001 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1002 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1003}
1004
1005ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1006 auto& configs = getConfig().portConfigs;
1007 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1008 if (configIt != configs.end()) {
1009 if (mStreams.count(in_portConfigId) != 0) {
1010 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1011 << " has a stream opened on it";
1012 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1013 }
1014 auto patchIt = mPatches.find(in_portConfigId);
1015 if (patchIt != mPatches.end()) {
1016 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1017 << " is used by the patch with id " << patchIt->second;
1018 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1019 }
1020 auto& initials = getConfig().initialConfigs;
1021 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1022 if (initialIt == initials.end()) {
1023 configs.erase(configIt);
1024 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1025 } else if (*configIt != *initialIt) {
1026 *configIt = *initialIt;
1027 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1028 }
1029 return ndk::ScopedAStatus::ok();
1030 }
1031 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1032 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1033}
1034
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001035ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1036 *_aidl_return = mMasterMute;
1037 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1038 return ndk::ScopedAStatus::ok();
1039}
1040
1041ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1042 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001043 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1044 : onMasterMuteChanged(in_mute);
1045 if (result.isOk()) {
1046 mMasterMute = in_mute;
1047 } else {
1048 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1049 << "), error=" << result;
1050 // Reset master mute if it failed.
1051 onMasterMuteChanged(mMasterMute);
1052 }
1053 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001054}
1055
1056ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1057 *_aidl_return = mMasterVolume;
1058 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1059 return ndk::ScopedAStatus::ok();
1060}
1061
1062ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1063 LOG(DEBUG) << __func__ << ": " << in_volume;
1064 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001065 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1066 : onMasterVolumeChanged(in_volume);
1067 if (result.isOk()) {
1068 mMasterVolume = in_volume;
1069 } else {
1070 // Reset master volume if it failed.
1071 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1072 << "), error=" << result;
1073 onMasterVolumeChanged(mMasterVolume);
1074 }
1075 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001076 }
1077 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1078 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1079}
1080
1081ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1082 *_aidl_return = mMicMute;
1083 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1084 return ndk::ScopedAStatus::ok();
1085}
1086
1087ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1088 LOG(DEBUG) << __func__ << ": " << in_mute;
1089 mMicMute = in_mute;
1090 return ndk::ScopedAStatus::ok();
1091}
1092
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001093ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001094 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001095 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1096 return ndk::ScopedAStatus::ok();
1097}
1098
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001099ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001100 if (!isValidAudioMode(in_mode)) {
1101 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1102 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1103 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001104 // No checks for supported audio modes here, it's an informative notification.
1105 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1106 return ndk::ScopedAStatus::ok();
1107}
1108
1109ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1110 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1111 return ndk::ScopedAStatus::ok();
1112}
1113
1114ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1115 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1116 return ndk::ScopedAStatus::ok();
1117}
1118
Vlad Popa83a6d822022-11-07 13:53:57 +01001119ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001120 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001121 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001122 }
Mikhail Naganov7499a002023-02-27 18:51:44 -08001123 *_aidl_return = mSoundDose.getPtr();
Vlad Popa943b7e22022-12-08 14:24:12 +01001124 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001125 return ndk::ScopedAStatus::ok();
1126}
1127
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001128ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1129 LOG(DEBUG) << __func__;
1130 (void)_aidl_return;
1131 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1132}
1133
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001134const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001135const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001136
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001137ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1138 std::vector<VendorParameter>* _aidl_return) {
1139 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001140 bool allParametersKnown = true;
1141 for (const auto& id : in_ids) {
1142 if (id == VendorDebug::kForceTransientBurstName) {
1143 VendorParameter forceTransientBurst{.id = id};
1144 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1145 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001146 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1147 VendorParameter forceSynchronousDrain{.id = id};
1148 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1149 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001150 } else {
1151 allParametersKnown = false;
1152 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1153 }
1154 }
1155 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1156 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001157}
1158
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001159namespace {
1160
1161template <typename W>
1162bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1163 std::optional<W> value;
1164 binder_status_t result = p.ext.getParcelable(&value);
1165 if (result == STATUS_OK && value.has_value()) {
1166 *v = value.value().value;
1167 return true;
1168 }
1169 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1170 << "\": " << result;
1171 return false;
1172}
1173
1174} // namespace
1175
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001176ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1177 bool in_async) {
1178 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1179 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001180 bool allParametersKnown = true;
1181 for (const auto& p : in_parameters) {
1182 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001183 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1184 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1185 }
1186 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1187 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001188 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1189 }
1190 } else {
1191 allParametersKnown = false;
1192 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1193 }
1194 }
1195 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1196 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001197}
1198
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001199ndk::ScopedAStatus Module::addDeviceEffect(
1200 int32_t in_portConfigId,
1201 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1202 if (in_effect == nullptr) {
1203 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1204 } else {
1205 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1206 << in_effect->asBinder().get();
1207 }
1208 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1209}
1210
1211ndk::ScopedAStatus Module::removeDeviceEffect(
1212 int32_t in_portConfigId,
1213 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1214 if (in_effect == nullptr) {
1215 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1216 } else {
1217 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1218 << in_effect->asBinder().get();
1219 }
1220 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1221}
1222
jiabin9a8e6862023-01-12 23:06:37 +00001223ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1224 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1225 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1226 std::set<int32_t> mmapSinks;
1227 std::set<int32_t> mmapSources;
1228 auto& ports = getConfig().ports;
1229 for (const auto& port : ports) {
1230 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1231 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1232 AudioInputFlags::MMAP_NOIRQ)) {
1233 mmapSinks.insert(port.id);
1234 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1235 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1236 AudioOutputFlags::MMAP_NOIRQ)) {
1237 mmapSources.insert(port.id);
1238 }
1239 }
1240 for (const auto& route : getConfig().routes) {
1241 if (mmapSinks.count(route.sinkPortId) != 0) {
1242 // The sink is a mix port, add the sources if they are device ports.
1243 for (int sourcePortId : route.sourcePortIds) {
1244 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1245 if (sourcePortIt == ports.end()) {
1246 // This must not happen
1247 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1248 continue;
1249 }
1250 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1251 // The source is not a device port, skip
1252 continue;
1253 }
1254 AudioMMapPolicyInfo policyInfo;
1255 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1256 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1257 // default implementation.
1258 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1259 _aidl_return->push_back(policyInfo);
1260 }
1261 } else {
1262 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1263 if (sinkPortIt == ports.end()) {
1264 // This must not happen
1265 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1266 continue;
1267 }
1268 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1269 // The sink is not a device port, skip
1270 continue;
1271 }
1272 if (count_any(mmapSources, route.sourcePortIds)) {
1273 AudioMMapPolicyInfo policyInfo;
1274 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1275 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1276 // default implementation.
1277 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1278 _aidl_return->push_back(policyInfo);
1279 }
1280 }
1281 }
1282 return ndk::ScopedAStatus::ok();
1283}
1284
Eric Laurente2432ea2023-01-12 17:47:31 +01001285ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1286 LOG(DEBUG) << __func__;
1287 *_aidl_return = false;
1288 return ndk::ScopedAStatus::ok();
1289}
1290
jiabinb76981e2023-01-18 00:58:30 +00001291ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1292 if (!isMmapSupported()) {
1293 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1294 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1295 }
1296 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1297 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1298 return ndk::ScopedAStatus::ok();
1299}
1300
1301ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1302 if (!isMmapSupported()) {
1303 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1304 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1305 }
1306 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1307 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1308 return ndk::ScopedAStatus::ok();
1309}
1310
1311bool Module::isMmapSupported() {
1312 if (mIsMmapSupported.has_value()) {
1313 return mIsMmapSupported.value();
1314 }
1315 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1316 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1317 mIsMmapSupported = false;
1318 } else {
1319 mIsMmapSupported =
1320 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1321 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1322 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1323 }) != mmapPolicyInfos.end();
1324 }
1325 return mIsMmapSupported.value();
1326}
1327
jiabin253bd322023-01-25 23:57:31 +00001328ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001329 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001330 return ndk::ScopedAStatus::ok();
1331}
1332
1333ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1334 const std::vector<AudioPortConfig*>& sources __unused,
1335 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001336 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001337 return ndk::ScopedAStatus::ok();
1338}
1339
jiabin783c48b2023-02-28 18:28:06 +00001340void Module::onExternalDeviceConnectionChanged(
1341 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1342 bool connected __unused) {
1343 LOG(DEBUG) << __func__ << ": do nothing and return";
1344}
1345
1346ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1347 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1348 return ndk::ScopedAStatus::ok();
1349}
1350
1351ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1352 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1353 return ndk::ScopedAStatus::ok();
1354}
1355
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001356} // namespace aidl::android::hardware::audio::core