blob: ab71ef4ed2f9bedaf80f1536bc3b87f94fe27a99 [file] [log] [blame]
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <algorithm>
18#include <set>
19
20#define LOG_TAG "AHAL_Module"
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000021#include <Utils.h>
22#include <aidl/android/media/audio/common/AudioInputFlags.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000023#include <aidl/android/media/audio/common/AudioOutputFlags.h>
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -070024#include <android-base/logging.h>
25#include <android/binder_ibinder_platform.h>
26#include <error/expected_utils.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000027
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(),
Mikhail Naganovb42a69e2023-06-16 12:38:25 -0700210 portConfigIt->sampleRate.value().value, flags,
211 portConfigIt->ext.get<AudioPortExt::mix>().handle,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000212 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov8651b362023-01-06 23:15:27 +0000213 asyncCallback, outEventCallback, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000214 if (temp.isValid()) {
215 *out_context = std::move(temp);
216 } else {
217 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
218 }
219 } else {
220 // TODO: Implement simulation of MMAP buffer allocation
221 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000222 return ndk::ScopedAStatus::ok();
223}
224
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000225std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
226 std::vector<AudioDevice> result;
227 auto& ports = getConfig().ports;
228 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
229 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
230 auto portIt = findById<AudioPort>(ports, *it);
231 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
232 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
233 }
234 }
235 return result;
236}
237
238std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
239 std::set<int32_t> result;
240 auto patchIdsRange = mPatches.equal_range(portConfigId);
241 auto& patches = getConfig().patches;
242 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
243 auto patchIt = findById<AudioPatch>(patches, it->second);
244 if (patchIt == patches.end()) {
245 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
246 << "not found in the configuration";
247 }
248 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
249 portConfigId) != patchIt->sourcePortConfigIds.end()) {
250 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
251 } else {
252 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
253 }
254 }
255 return result;
256}
257
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000258ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
259 auto& configs = getConfig().portConfigs;
260 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
261 if (portConfigIt == configs.end()) {
262 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
263 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
264 }
265 const int32_t portId = portConfigIt->portId;
266 // In our implementation, configs of mix ports always have unique IDs.
267 CHECK(portId != in_portConfigId);
268 auto& ports = getConfig().ports;
269 auto portIt = findById<AudioPort>(ports, portId);
270 if (portIt == ports.end()) {
271 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
272 << in_portConfigId << " not found";
273 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
274 }
275 if (mStreams.count(in_portConfigId) != 0) {
276 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
277 << " already has a stream opened on it";
278 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
279 }
280 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
281 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
282 << " does not correspond to a mix port";
283 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
284 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700285 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000286 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
287 LOG(ERROR) << __func__ << ": port id " << portId
288 << " has already reached maximum allowed opened stream count: "
289 << maxOpenStreamCount;
290 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
291 }
292 *port = &(*portIt);
293 return ndk::ScopedAStatus::ok();
294}
295
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000296template <typename C>
297std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
298 std::set<int32_t> result;
299 auto& portConfigs = getConfig().portConfigs;
300 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
301 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
302 if (portConfigIt != portConfigs.end()) {
303 result.insert(portConfigIt->portId);
304 }
305 }
306 return result;
307}
308
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000309internal::Configuration& Module::getConfig() {
310 if (!mConfig) {
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000311 switch (mType) {
312 case Type::DEFAULT:
313 mConfig = std::move(internal::getPrimaryConfiguration());
314 break;
315 case Type::R_SUBMIX:
316 mConfig = std::move(internal::getRSubmixConfiguration());
317 break;
jiabinb309d8d2023-01-20 19:07:15 +0000318 case Type::USB:
319 mConfig = std::move(internal::getUsbConfiguration());
jiabin253bd322023-01-25 23:57:31 +0000320 break;
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000321 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000322 }
323 return *mConfig;
324}
325
326void Module::registerPatch(const AudioPatch& patch) {
327 auto& configs = getConfig().portConfigs;
328 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
329 for (auto portConfigId : portConfigIds) {
330 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
331 if (configIt != configs.end()) {
332 mPatches.insert(std::pair{portConfigId, patch.id});
333 if (configIt->portId != portConfigId) {
334 mPatches.insert(std::pair{configIt->portId, patch.id});
335 }
336 }
337 };
338 };
339 do_insert(patch.sourcePortConfigIds);
340 do_insert(patch.sinkPortConfigIds);
341}
342
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000343void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) {
344 // Streams from the old patch need to be disconnected, streams from the new
345 // patch need to be connected. If the stream belongs to both patches, no need
346 // to update it.
347 std::set<int32_t> idsToDisconnect, idsToConnect;
348 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
349 oldPatch.sourcePortConfigIds.end());
350 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
351 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
352 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
353 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
354 if (idsToConnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000355 LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected";
356 mStreams.setStreamIsConnected(portConfigId, {});
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000357 }
358 });
359 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
360 if (idsToDisconnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000361 const auto connectedDevices = findConnectedDevices(portConfigId);
362 LOG(DEBUG) << "The stream on port config id " << portConfigId
363 << " is connected to: " << ::android::internal::ToString(connectedDevices);
364 mStreams.setStreamIsConnected(portConfigId, connectedDevices);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000365 }
366 });
367}
368
Mikhail Naganov00603d12022-05-02 22:52:13 +0000369ndk::ScopedAStatus Module::setModuleDebug(
370 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700371 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000372 << ", new flags: " << in_debug.toString();
373 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
374 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700375 LOG(ERROR) << __func__ << ": " << mType
376 << ": attempting to change device connections simulation while having external "
377 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000378 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
379 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000380 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700381 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000382 << in_debug.streamTransientStateDelayMs;
383 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
384 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000385 mDebug = in_debug;
386 return ndk::ScopedAStatus::ok();
387}
388
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000389ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800390 if (!mTelephony) {
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000391 mTelephony = ndk::SharedRefBase::make<Telephony>();
392 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800393 *_aidl_return = mTelephony.getPtr();
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000394 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
395 return ndk::ScopedAStatus::ok();
396}
397
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000398ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800399 if (!mBluetooth) {
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000400 mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000401 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800402 *_aidl_return = mBluetooth.getPtr();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000403 LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get();
404 return ndk::ScopedAStatus::ok();
405}
406
Mikhail Naganov7499a002023-02-27 18:51:44 -0800407ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
408 if (!mBluetoothA2dp) {
409 mBluetoothA2dp = ndk::SharedRefBase::make<BluetoothA2dp>();
410 }
411 *_aidl_return = mBluetoothA2dp.getPtr();
412 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothA2dp: " << _aidl_return->get();
413 return ndk::ScopedAStatus::ok();
414}
415
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800416ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
417 if (!mBluetoothLe) {
418 mBluetoothLe = ndk::SharedRefBase::make<BluetoothLe>();
419 }
420 *_aidl_return = mBluetoothLe.getPtr();
421 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothLe: " << _aidl_return->get();
422 return ndk::ScopedAStatus::ok();
423}
424
Mikhail Naganov00603d12022-05-02 22:52:13 +0000425ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
426 AudioPort* _aidl_return) {
427 const int32_t templateId = in_templateIdAndAdditionalData.id;
428 auto& ports = getConfig().ports;
429 AudioPort connectedPort;
430 { // Scope the template port so that we don't accidentally modify it.
431 auto templateIt = findById<AudioPort>(ports, templateId);
432 if (templateIt == ports.end()) {
433 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
434 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
435 }
436 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
437 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
438 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
439 }
440 if (!templateIt->profiles.empty()) {
441 LOG(ERROR) << __func__ << ": port id " << templateId
442 << " does not have dynamic profiles";
443 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
444 }
445 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
446 if (templateDevicePort.device.type.connection.empty()) {
447 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
448 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
449 }
450 // Postpone id allocation until we ensure that there are no client errors.
451 connectedPort = *templateIt;
452 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
453 const auto& inputDevicePort =
454 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
455 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
456 connectedDevicePort.device.address = inputDevicePort.device.address;
457 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
458 << connectedDevicePort.device.toString();
459 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700460 for (auto connectedPortPair : mConnectedDevicePorts) {
461 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000462 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
463 connectedDevicePort.device) {
464 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700465 << " is already connected at the device port id "
466 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000467 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
468 }
469 }
470 }
471
472 if (!mDebug.simulateDeviceConnections) {
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700473 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700474 } else {
475 auto& connectedProfiles = getConfig().connectedProfiles;
476 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
477 connectedProfilesIt != connectedProfiles.end()) {
478 connectedPort.profiles = connectedProfilesIt->second;
479 }
480 }
481 if (connectedPort.profiles.empty()) {
482 LOG(ERROR) << "Profiles of a connected port still empty after connecting external device "
483 << connectedPort.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000484 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
485 }
486
487 connectedPort.id = ++getConfig().nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700488 auto [connectedPortsIt, _] =
489 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::vector<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000490 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
491 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000492 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000493 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000494
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700495 std::vector<int32_t> routablePortIds;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000496 std::vector<AudioRoute> newRoutes;
497 auto& routes = getConfig().routes;
498 for (auto& r : routes) {
499 if (r.sinkPortId == templateId) {
500 AudioRoute newRoute;
501 newRoute.sourcePortIds = r.sourcePortIds;
502 newRoute.sinkPortId = connectedPort.id;
503 newRoute.isExclusive = r.isExclusive;
504 newRoutes.push_back(std::move(newRoute));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700505 routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(),
506 r.sourcePortIds.end());
Mikhail Naganov00603d12022-05-02 22:52:13 +0000507 } else {
508 auto& srcs = r.sourcePortIds;
509 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
510 srcs.push_back(connectedPort.id);
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700511 routablePortIds.push_back(r.sinkPortId);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000512 }
513 }
514 }
515 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
516
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700517 // Note: this is a simplistic approach assuming that a mix port can only be populated
518 // from a single device port. Implementing support for stuffing dynamic profiles with a superset
519 // of all profiles from all routable dynamic device ports would be more involved.
520 for (const auto mixPortId : routablePortIds) {
521 auto portsIt = findById<AudioPort>(ports, mixPortId);
522 if (portsIt != ports.end() && portsIt->profiles.empty()) {
523 portsIt->profiles = connectedPort.profiles;
524 connectedPortsIt->second.push_back(portsIt->id);
525 }
526 }
527 *_aidl_return = std::move(connectedPort);
528
Mikhail Naganov00603d12022-05-02 22:52:13 +0000529 return ndk::ScopedAStatus::ok();
530}
531
532ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
533 auto& ports = getConfig().ports;
534 auto portIt = findById<AudioPort>(ports, in_portId);
535 if (portIt == ports.end()) {
536 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
537 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
538 }
539 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
540 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
541 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
542 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700543 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
544 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000545 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
546 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
547 }
548 auto& configs = getConfig().portConfigs;
549 auto& initials = getConfig().initialConfigs;
550 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
551 if (config.portId == in_portId) {
552 // Check if the configuration was provided by the client.
553 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
554 return initialIt == initials.end() || config != *initialIt;
555 }
556 return false;
557 });
558 if (configIt != configs.end()) {
559 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
560 << configIt->id;
561 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
562 }
jiabin783c48b2023-02-28 18:28:06 +0000563 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000564 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000565 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
566
567 auto& routes = getConfig().routes;
568 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
569 if (routesIt->sinkPortId == in_portId) {
570 routesIt = routes.erase(routesIt);
571 } else {
572 // Note: the list of sourcePortIds can't become empty because there must
573 // be the id of the template port in the route.
574 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
575 ++routesIt;
576 }
577 }
578
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700579 for (const auto mixPortId : connectedPortsIt->second) {
580 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
581 if (mixPortIt != ports.end()) {
582 mixPortIt->profiles = {};
583 }
584 }
585 mConnectedDevicePorts.erase(connectedPortsIt);
586
Mikhail Naganov00603d12022-05-02 22:52:13 +0000587 return ndk::ScopedAStatus::ok();
588}
589
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000590ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
591 *_aidl_return = getConfig().patches;
592 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
593 return ndk::ScopedAStatus::ok();
594}
595
596ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
597 auto& ports = getConfig().ports;
598 auto portIt = findById<AudioPort>(ports, in_portId);
599 if (portIt != ports.end()) {
600 *_aidl_return = *portIt;
601 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
602 return ndk::ScopedAStatus::ok();
603 }
604 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
605 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
606}
607
608ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
609 *_aidl_return = getConfig().portConfigs;
610 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
611 return ndk::ScopedAStatus::ok();
612}
613
614ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
615 *_aidl_return = getConfig().ports;
616 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
617 return ndk::ScopedAStatus::ok();
618}
619
620ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
621 *_aidl_return = getConfig().routes;
622 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
623 return ndk::ScopedAStatus::ok();
624}
625
Mikhail Naganov00603d12022-05-02 22:52:13 +0000626ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
627 std::vector<AudioRoute>* _aidl_return) {
628 auto& ports = getConfig().ports;
629 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
630 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
631 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
632 }
633 auto& routes = getConfig().routes;
634 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
635 [&](const auto& r) {
636 const auto& srcs = r.sourcePortIds;
637 return r.sinkPortId == in_portId ||
638 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
639 });
640 return ndk::ScopedAStatus::ok();
641}
642
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000643ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
644 OpenInputStreamReturn* _aidl_return) {
645 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
646 << in_args.bufferSizeFrames << " frames";
647 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700648 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000649 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
650 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000651 << " does not correspond to an input mix port";
652 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
653 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000654 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700655 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
656 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000657 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000658 std::shared_ptr<StreamIn> stream;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700659 RETURN_STATUS_IF_ERROR(getStreamInCreator(mType)(in_args.sinkMetadata, std::move(context),
660 mConfig->microphones, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000661 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000662 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
663 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000664 auto patchIt = mPatches.find(in_args.portConfigId);
665 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000666 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000667 }
668 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000669 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000670 return ndk::ScopedAStatus::ok();
671}
672
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000673ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
674 OpenOutputStreamReturn* _aidl_return) {
675 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
676 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
677 << " frames";
678 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700679 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000680 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
681 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000682 << " does not correspond to an output mix port";
683 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
684 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000685 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
686 AudioOutputFlags::COMPRESS_OFFLOAD);
687 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000688 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000689 << " has COMPRESS_OFFLOAD flag set, requires offload info";
690 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
691 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000692 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
693 AudioOutputFlags::NON_BLOCKING);
694 if (isNonBlocking && in_args.callback == nullptr) {
695 LOG(ERROR) << __func__ << ": port id " << port->id
696 << " has NON_BLOCKING flag set, requires async callback";
697 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
698 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000699 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700700 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
701 isNonBlocking ? in_args.callback : nullptr,
702 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000703 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000704 std::shared_ptr<StreamOut> stream;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700705 RETURN_STATUS_IF_ERROR(getStreamOutCreator(mType)(in_args.sourceMetadata, std::move(context),
706 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000707 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000708 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
709 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000710 auto patchIt = mPatches.find(in_args.portConfigId);
711 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000712 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000713 }
714 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000715 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000716 return ndk::ScopedAStatus::ok();
717}
718
Mikhail Naganov74927202022-12-19 16:37:14 +0000719ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
720 SupportedPlaybackRateFactors* _aidl_return) {
721 LOG(DEBUG) << __func__;
722 (void)_aidl_return;
723 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
724}
725
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000726ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000727 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000728 if (in_requested.sourcePortConfigIds.empty()) {
729 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
730 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
731 }
732 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
733 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
734 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
735 }
736 if (in_requested.sinkPortConfigIds.empty()) {
737 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
738 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
739 }
740 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
741 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
742 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
743 }
744
745 auto& configs = getConfig().portConfigs;
746 std::vector<int32_t> missingIds;
747 auto sources =
748 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
749 if (!missingIds.empty()) {
750 LOG(ERROR) << __func__ << ": following source port config ids not found: "
751 << ::android::internal::ToString(missingIds);
752 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
753 }
754 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
755 if (!missingIds.empty()) {
756 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
757 << ::android::internal::ToString(missingIds);
758 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
759 }
760 // bool indicates whether a non-exclusive route is available.
761 // If only an exclusive route is available, that means the patch can not be
762 // established if there is any other patch which currently uses the sink port.
763 std::map<int32_t, bool> allowedSinkPorts;
764 auto& routes = getConfig().routes;
765 for (auto src : sources) {
766 for (const auto& r : routes) {
767 const auto& srcs = r.sourcePortIds;
768 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
769 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
770 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
771 }
772 }
773 }
774 }
775 for (auto sink : sinks) {
776 if (allowedSinkPorts.count(sink->portId) == 0) {
777 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
778 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
779 }
780 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700781 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000782
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000783 auto& patches = getConfig().patches;
784 auto existing = patches.end();
785 std::optional<decltype(mPatches)> patchesBackup;
786 if (in_requested.id != 0) {
787 existing = findById<AudioPatch>(patches, in_requested.id);
788 if (existing != patches.end()) {
789 patchesBackup = mPatches;
790 cleanUpPatch(existing->id);
791 } else {
792 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
793 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
794 }
795 }
796 // Validate the requested patch.
797 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
798 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
799 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
800 << "is exclusive and is already used by some other patch";
801 if (patchesBackup.has_value()) {
802 mPatches = std::move(*patchesBackup);
803 }
804 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
805 }
806 }
807 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000808 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
809 _aidl_return->latenciesMs.clear();
810 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
811 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000812 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000813 if (existing == patches.end()) {
814 _aidl_return->id = getConfig().nextPatchId++;
815 patches.push_back(*_aidl_return);
816 existing = patches.begin() + (patches.size() - 1);
817 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000818 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000819 *existing = *_aidl_return;
820 }
821 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000822 updateStreamsConnectedState(oldPatch, *_aidl_return);
823
824 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
825 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000826 return ndk::ScopedAStatus::ok();
827}
828
829ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
830 AudioPortConfig* out_suggested, bool* _aidl_return) {
831 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
832 auto& configs = getConfig().portConfigs;
833 auto existing = configs.end();
834 if (in_requested.id != 0) {
835 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
836 existing == configs.end()) {
837 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
838 << " not found";
839 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
840 }
841 }
842
843 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
844 if (portId == 0) {
845 LOG(ERROR) << __func__ << ": input port config does not specify portId";
846 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
847 }
848 auto& ports = getConfig().ports;
849 auto portIt = findById<AudioPort>(ports, portId);
850 if (portIt == ports.end()) {
851 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
852 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
853 }
854 if (existing != configs.end()) {
855 *out_suggested = *existing;
856 } else {
857 AudioPortConfig newConfig;
858 if (generateDefaultPortConfig(*portIt, &newConfig)) {
859 *out_suggested = newConfig;
860 } else {
861 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
862 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
863 }
864 }
865 // From this moment, 'out_suggested' is either an existing port config,
866 // or a new generated config. Now attempt to update it according to the specified
867 // fields of 'in_requested'.
868
869 bool requestedIsValid = true, requestedIsFullySpecified = true;
870
871 AudioIoFlags portFlags = portIt->flags;
872 if (in_requested.flags.has_value()) {
873 if (in_requested.flags.value() != portFlags) {
874 LOG(WARNING) << __func__ << ": requested flags "
875 << in_requested.flags.value().toString() << " do not match port's "
876 << portId << " flags " << portFlags.toString();
877 requestedIsValid = false;
878 }
879 } else {
880 requestedIsFullySpecified = false;
881 }
882
883 AudioProfile portProfile;
884 if (in_requested.format.has_value()) {
885 const auto& format = in_requested.format.value();
886 if (findAudioProfile(*portIt, format, &portProfile)) {
887 out_suggested->format = format;
888 } else {
889 LOG(WARNING) << __func__ << ": requested format " << format.toString()
890 << " is not found in port's " << portId << " profiles";
891 requestedIsValid = false;
892 }
893 } else {
894 requestedIsFullySpecified = false;
895 }
896 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
897 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
898 << out_suggested->format.value().toString() << " anymore";
899 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
900 }
901
902 if (in_requested.channelMask.has_value()) {
903 const auto& channelMask = in_requested.channelMask.value();
904 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
905 portProfile.channelMasks.end()) {
906 out_suggested->channelMask = channelMask;
907 } else {
908 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
909 << " is not supported for the format " << portProfile.format.toString()
910 << " by the port " << portId;
911 requestedIsValid = false;
912 }
913 } else {
914 requestedIsFullySpecified = false;
915 }
916
917 if (in_requested.sampleRate.has_value()) {
918 const auto& sampleRate = in_requested.sampleRate.value();
919 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
920 sampleRate.value) != portProfile.sampleRates.end()) {
921 out_suggested->sampleRate = sampleRate;
922 } else {
923 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
924 << " is not supported for the format " << portProfile.format.toString()
925 << " by the port " << portId;
926 requestedIsValid = false;
927 }
928 } else {
929 requestedIsFullySpecified = false;
930 }
931
932 if (in_requested.gain.has_value()) {
933 // Let's pretend that gain can always be applied.
934 out_suggested->gain = in_requested.gain.value();
935 }
936
Mikhail Naganov248e9502023-02-21 16:32:40 -0800937 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
938 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
939 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
940 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
941 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
942 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
943 }
944 } else {
945 LOG(WARNING) << __func__ << ": requested ext tag "
946 << toString(in_requested.ext.getTag()) << " do not match port's tag "
947 << toString(out_suggested->ext.getTag());
948 requestedIsValid = false;
949 }
950 }
951
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000952 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
953 out_suggested->id = getConfig().nextPortId++;
954 configs.push_back(*out_suggested);
955 *_aidl_return = true;
956 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
957 } else if (existing != configs.end() && requestedIsValid) {
958 *existing = *out_suggested;
959 *_aidl_return = true;
960 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
961 } else {
962 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
963 << "; requested is valid? " << requestedIsValid << ", fully specified? "
964 << requestedIsFullySpecified;
965 *_aidl_return = false;
966 }
967 return ndk::ScopedAStatus::ok();
968}
969
970ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
971 auto& patches = getConfig().patches;
972 auto patchIt = findById<AudioPatch>(patches, in_patchId);
973 if (patchIt != patches.end()) {
974 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000975 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000976 patches.erase(patchIt);
977 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
978 return ndk::ScopedAStatus::ok();
979 }
980 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
981 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
982}
983
984ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
985 auto& configs = getConfig().portConfigs;
986 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
987 if (configIt != configs.end()) {
988 if (mStreams.count(in_portConfigId) != 0) {
989 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
990 << " has a stream opened on it";
991 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
992 }
993 auto patchIt = mPatches.find(in_portConfigId);
994 if (patchIt != mPatches.end()) {
995 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
996 << " is used by the patch with id " << patchIt->second;
997 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
998 }
999 auto& initials = getConfig().initialConfigs;
1000 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1001 if (initialIt == initials.end()) {
1002 configs.erase(configIt);
1003 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1004 } else if (*configIt != *initialIt) {
1005 *configIt = *initialIt;
1006 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1007 }
1008 return ndk::ScopedAStatus::ok();
1009 }
1010 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1011 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1012}
1013
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001014ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1015 *_aidl_return = mMasterMute;
1016 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1017 return ndk::ScopedAStatus::ok();
1018}
1019
1020ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1021 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001022 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1023 : onMasterMuteChanged(in_mute);
1024 if (result.isOk()) {
1025 mMasterMute = in_mute;
1026 } else {
1027 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1028 << "), error=" << result;
1029 // Reset master mute if it failed.
1030 onMasterMuteChanged(mMasterMute);
1031 }
1032 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001033}
1034
1035ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1036 *_aidl_return = mMasterVolume;
1037 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1038 return ndk::ScopedAStatus::ok();
1039}
1040
1041ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1042 LOG(DEBUG) << __func__ << ": " << in_volume;
1043 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001044 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1045 : onMasterVolumeChanged(in_volume);
1046 if (result.isOk()) {
1047 mMasterVolume = in_volume;
1048 } else {
1049 // Reset master volume if it failed.
1050 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1051 << "), error=" << result;
1052 onMasterVolumeChanged(mMasterVolume);
1053 }
1054 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001055 }
1056 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1057 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1058}
1059
1060ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1061 *_aidl_return = mMicMute;
1062 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1063 return ndk::ScopedAStatus::ok();
1064}
1065
1066ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1067 LOG(DEBUG) << __func__ << ": " << in_mute;
1068 mMicMute = in_mute;
1069 return ndk::ScopedAStatus::ok();
1070}
1071
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001072ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001073 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001074 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1075 return ndk::ScopedAStatus::ok();
1076}
1077
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001078ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001079 if (!isValidAudioMode(in_mode)) {
1080 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1081 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1082 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001083 // No checks for supported audio modes here, it's an informative notification.
1084 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1085 return ndk::ScopedAStatus::ok();
1086}
1087
1088ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1089 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1090 return ndk::ScopedAStatus::ok();
1091}
1092
1093ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1094 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1095 return ndk::ScopedAStatus::ok();
1096}
1097
Vlad Popa83a6d822022-11-07 13:53:57 +01001098ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001099 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001100 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001101 }
Mikhail Naganov7499a002023-02-27 18:51:44 -08001102 *_aidl_return = mSoundDose.getPtr();
Vlad Popa943b7e22022-12-08 14:24:12 +01001103 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001104 return ndk::ScopedAStatus::ok();
1105}
1106
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001107ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1108 LOG(DEBUG) << __func__;
1109 (void)_aidl_return;
1110 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1111}
1112
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001113const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001114const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001115
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001116ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1117 std::vector<VendorParameter>* _aidl_return) {
1118 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001119 bool allParametersKnown = true;
1120 for (const auto& id : in_ids) {
1121 if (id == VendorDebug::kForceTransientBurstName) {
1122 VendorParameter forceTransientBurst{.id = id};
1123 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1124 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001125 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1126 VendorParameter forceSynchronousDrain{.id = id};
1127 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1128 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001129 } else {
1130 allParametersKnown = false;
1131 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1132 }
1133 }
1134 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1135 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001136}
1137
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001138namespace {
1139
1140template <typename W>
1141bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1142 std::optional<W> value;
1143 binder_status_t result = p.ext.getParcelable(&value);
1144 if (result == STATUS_OK && value.has_value()) {
1145 *v = value.value().value;
1146 return true;
1147 }
1148 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1149 << "\": " << result;
1150 return false;
1151}
1152
1153} // namespace
1154
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001155ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1156 bool in_async) {
1157 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1158 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001159 bool allParametersKnown = true;
1160 for (const auto& p : in_parameters) {
1161 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001162 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1163 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1164 }
1165 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1166 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001167 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1168 }
1169 } else {
1170 allParametersKnown = false;
1171 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1172 }
1173 }
1174 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1175 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001176}
1177
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001178ndk::ScopedAStatus Module::addDeviceEffect(
1179 int32_t in_portConfigId,
1180 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1181 if (in_effect == nullptr) {
1182 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1183 } else {
1184 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1185 << in_effect->asBinder().get();
1186 }
1187 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1188}
1189
1190ndk::ScopedAStatus Module::removeDeviceEffect(
1191 int32_t in_portConfigId,
1192 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1193 if (in_effect == nullptr) {
1194 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1195 } else {
1196 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1197 << in_effect->asBinder().get();
1198 }
1199 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1200}
1201
jiabin9a8e6862023-01-12 23:06:37 +00001202ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1203 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1204 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1205 std::set<int32_t> mmapSinks;
1206 std::set<int32_t> mmapSources;
1207 auto& ports = getConfig().ports;
1208 for (const auto& port : ports) {
1209 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1210 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1211 AudioInputFlags::MMAP_NOIRQ)) {
1212 mmapSinks.insert(port.id);
1213 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1214 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1215 AudioOutputFlags::MMAP_NOIRQ)) {
1216 mmapSources.insert(port.id);
1217 }
1218 }
1219 for (const auto& route : getConfig().routes) {
1220 if (mmapSinks.count(route.sinkPortId) != 0) {
1221 // The sink is a mix port, add the sources if they are device ports.
1222 for (int sourcePortId : route.sourcePortIds) {
1223 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1224 if (sourcePortIt == ports.end()) {
1225 // This must not happen
1226 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1227 continue;
1228 }
1229 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1230 // The source is not a device port, skip
1231 continue;
1232 }
1233 AudioMMapPolicyInfo policyInfo;
1234 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1235 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1236 // default implementation.
1237 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1238 _aidl_return->push_back(policyInfo);
1239 }
1240 } else {
1241 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1242 if (sinkPortIt == ports.end()) {
1243 // This must not happen
1244 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1245 continue;
1246 }
1247 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1248 // The sink is not a device port, skip
1249 continue;
1250 }
1251 if (count_any(mmapSources, route.sourcePortIds)) {
1252 AudioMMapPolicyInfo policyInfo;
1253 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1254 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1255 // default implementation.
1256 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1257 _aidl_return->push_back(policyInfo);
1258 }
1259 }
1260 }
1261 return ndk::ScopedAStatus::ok();
1262}
1263
Eric Laurente2432ea2023-01-12 17:47:31 +01001264ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1265 LOG(DEBUG) << __func__;
1266 *_aidl_return = false;
1267 return ndk::ScopedAStatus::ok();
1268}
1269
jiabinb76981e2023-01-18 00:58:30 +00001270ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1271 if (!isMmapSupported()) {
1272 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1273 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1274 }
1275 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1276 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1277 return ndk::ScopedAStatus::ok();
1278}
1279
1280ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1281 if (!isMmapSupported()) {
1282 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1283 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1284 }
1285 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1286 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1287 return ndk::ScopedAStatus::ok();
1288}
1289
1290bool Module::isMmapSupported() {
1291 if (mIsMmapSupported.has_value()) {
1292 return mIsMmapSupported.value();
1293 }
1294 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1295 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1296 mIsMmapSupported = false;
1297 } else {
1298 mIsMmapSupported =
1299 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1300 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1301 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1302 }) != mmapPolicyInfos.end();
1303 }
1304 return mIsMmapSupported.value();
1305}
1306
jiabin253bd322023-01-25 23:57:31 +00001307ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001308 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001309 return ndk::ScopedAStatus::ok();
1310}
1311
1312ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1313 const std::vector<AudioPortConfig*>& sources __unused,
1314 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001315 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001316 return ndk::ScopedAStatus::ok();
1317}
1318
jiabin783c48b2023-02-28 18:28:06 +00001319void Module::onExternalDeviceConnectionChanged(
1320 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1321 bool connected __unused) {
1322 LOG(DEBUG) << __func__ << ": do nothing and return";
1323}
1324
1325ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1326 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1327 return ndk::ScopedAStatus::ok();
1328}
1329
1330ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1331 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1332 return ndk::ScopedAStatus::ok();
1333}
1334
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001335} // namespace aidl::android::hardware::audio::core