blob: 12bbbb0cba3f8fecc0240ce6439c4b0e4f3481f8 [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
28#include "core-impl/Module.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070029#include "core-impl/ModulePrimary.h"
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053030#include "core-impl/ModuleRemoteSubmix.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070031#include "core-impl/ModuleStub.h"
jiabin253bd322023-01-25 23:57:31 +000032#include "core-impl/ModuleUsb.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010033#include "core-impl/SoundDose.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000034#include "core-impl/utils.h"
35
Mikhail Naganov872d4a62023-03-09 18:19:01 -080036using aidl::android::hardware::audio::common::getFrameSizeInBytes;
37using aidl::android::hardware::audio::common::isBitPositionFlagSet;
38using aidl::android::hardware::audio::common::isValidAudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000039using aidl::android::hardware::audio::common::SinkMetadata;
40using aidl::android::hardware::audio::common::SourceMetadata;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010041using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000042using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000043using aidl::android::media::audio::common::AudioDevice;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000044using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000045using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000046using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000047using aidl::android::media::audio::common::AudioIoFlags;
jiabin9a8e6862023-01-12 23:06:37 +000048using aidl::android::media::audio::common::AudioMMapPolicy;
49using aidl::android::media::audio::common::AudioMMapPolicyInfo;
50using aidl::android::media::audio::common::AudioMMapPolicyType;
Mikhail Naganov04ae8222023-01-11 15:48:10 -080051using aidl::android::media::audio::common::AudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000052using aidl::android::media::audio::common::AudioOffloadInfo;
53using aidl::android::media::audio::common::AudioOutputFlags;
54using aidl::android::media::audio::common::AudioPort;
55using aidl::android::media::audio::common::AudioPortConfig;
56using aidl::android::media::audio::common::AudioPortExt;
57using aidl::android::media::audio::common::AudioProfile;
Mikhail Naganov20047bc2023-01-05 20:16:07 +000058using aidl::android::media::audio::common::Boolean;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000059using aidl::android::media::audio::common::Int;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080060using aidl::android::media::audio::common::MicrophoneInfo;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000061using aidl::android::media::audio::common::PcmType;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000062
63namespace aidl::android::hardware::audio::core {
64
65namespace {
66
67bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
68 *config = {};
69 config->portId = port.id;
70 if (port.profiles.empty()) {
71 LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles";
72 return false;
73 }
74 const auto& profile = port.profiles.begin();
75 config->format = profile->format;
76 if (profile->channelMasks.empty()) {
77 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
78 << " has no channel masks";
79 return false;
80 }
81 config->channelMask = *profile->channelMasks.begin();
82 if (profile->sampleRates.empty()) {
83 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
84 << " has no sample rates";
85 return false;
86 }
87 Int sampleRate;
88 sampleRate.value = *profile->sampleRates.begin();
89 config->sampleRate = sampleRate;
90 config->flags = port.flags;
91 config->ext = port.ext;
92 return true;
93}
94
95bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
96 AudioProfile* profile) {
97 if (auto profilesIt =
98 find_if(port.profiles.begin(), port.profiles.end(),
99 [&format](const auto& profile) { return profile.format == format; });
100 profilesIt != port.profiles.end()) {
101 *profile = *profilesIt;
102 return true;
103 }
104 return false;
105}
Mikhail Naganov00603d12022-05-02 22:52:13 +0000106
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000107} // namespace
108
jiabin253bd322023-01-25 23:57:31 +0000109// static
110std::shared_ptr<Module> Module::createInstance(Type type) {
111 switch (type) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530112 case Type::DEFAULT:
Mikhail Naganov521fc492023-07-11 17:24:08 -0700113 return ndk::SharedRefBase::make<ModulePrimary>();
114 case Type::R_SUBMIX:
115 return ndk::SharedRefBase::make<ModuleRemoteSubmix>();
116 case Type::STUB:
117 return ndk::SharedRefBase::make<ModuleStub>();
118 case Type::USB:
119 return ndk::SharedRefBase::make<ModuleUsb>();
jiabin253bd322023-01-25 23:57:31 +0000120 }
121}
122
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700123std::ostream& operator<<(std::ostream& os, Module::Type t) {
124 switch (t) {
125 case Module::Type::DEFAULT:
126 os << "default";
127 break;
128 case Module::Type::R_SUBMIX:
129 os << "r_submix";
130 break;
Mikhail Naganov521fc492023-07-11 17:24:08 -0700131 case Module::Type::STUB:
132 os << "stub";
133 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700134 case Module::Type::USB:
135 os << "usb";
136 break;
137 }
138 return os;
139}
140
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000141void Module::cleanUpPatch(int32_t patchId) {
142 erase_all_values(mPatches, std::set<int32_t>{patchId});
143}
144
Mikhail Naganov8651b362023-01-06 23:15:27 +0000145ndk::ScopedAStatus Module::createStreamContext(
146 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
147 std::shared_ptr<IStreamCallback> asyncCallback,
148 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000149 if (in_bufferSizeFrames <= 0) {
150 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
151 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
152 }
153 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
154 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
155 << ", must be at least " << kMinimumStreamBufferSizeFrames;
156 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
157 }
158 auto& configs = getConfig().portConfigs;
159 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000160 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000161 // validity of the portConfigId has already been checked.
162 const size_t frameSize =
163 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
164 if (frameSize == 0) {
165 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
166 << portConfigIt->toString();
167 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
168 }
169 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700170 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000171 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
172 << " frames is too large, maximum size is "
173 << kMaximumStreamBufferSizeBytes / frameSize;
174 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
175 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000176 const auto& flags = portConfigIt->flags.value();
177 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000178 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
179 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000180 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000181 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
182 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000183 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
Mikhail Naganov194daaa2023-01-05 22:34:20 +0000184 mVendorDebug.forceTransientBurst,
185 mVendorDebug.forceSynchronousDrain};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000186 StreamContext temp(
187 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
188 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530189 portConfigIt->portId, portConfigIt->format.value(),
190 portConfigIt->channelMask.value(), portConfigIt->sampleRate.value().value, flags,
Mikhail Naganovb42a69e2023-06-16 12:38:25 -0700191 portConfigIt->ext.get<AudioPortExt::mix>().handle,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000192 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov8651b362023-01-06 23:15:27 +0000193 asyncCallback, outEventCallback, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000194 if (temp.isValid()) {
195 *out_context = std::move(temp);
196 } else {
197 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
198 }
199 } else {
200 // TODO: Implement simulation of MMAP buffer allocation
201 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000202 return ndk::ScopedAStatus::ok();
203}
204
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000205std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
206 std::vector<AudioDevice> result;
207 auto& ports = getConfig().ports;
208 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
209 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
210 auto portIt = findById<AudioPort>(ports, *it);
211 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
212 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
213 }
214 }
215 return result;
216}
217
218std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
219 std::set<int32_t> result;
220 auto patchIdsRange = mPatches.equal_range(portConfigId);
221 auto& patches = getConfig().patches;
222 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
223 auto patchIt = findById<AudioPatch>(patches, it->second);
224 if (patchIt == patches.end()) {
225 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
226 << "not found in the configuration";
227 }
228 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
229 portConfigId) != patchIt->sourcePortConfigIds.end()) {
230 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
231 } else {
232 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
233 }
234 }
235 return result;
236}
237
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000238ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
239 auto& configs = getConfig().portConfigs;
240 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
241 if (portConfigIt == configs.end()) {
242 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
243 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
244 }
245 const int32_t portId = portConfigIt->portId;
246 // In our implementation, configs of mix ports always have unique IDs.
247 CHECK(portId != in_portConfigId);
248 auto& ports = getConfig().ports;
249 auto portIt = findById<AudioPort>(ports, portId);
250 if (portIt == ports.end()) {
251 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
252 << in_portConfigId << " not found";
253 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
254 }
255 if (mStreams.count(in_portConfigId) != 0) {
256 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
257 << " already has a stream opened on it";
258 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
259 }
260 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
261 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
262 << " does not correspond to a mix port";
263 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
264 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700265 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000266 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
267 LOG(ERROR) << __func__ << ": port id " << portId
268 << " has already reached maximum allowed opened stream count: "
269 << maxOpenStreamCount;
270 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
271 }
272 *port = &(*portIt);
273 return ndk::ScopedAStatus::ok();
274}
275
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000276template <typename C>
277std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
278 std::set<int32_t> result;
279 auto& portConfigs = getConfig().portConfigs;
280 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
281 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
282 if (portConfigIt != portConfigs.end()) {
283 result.insert(portConfigIt->portId);
284 }
285 }
286 return result;
287}
288
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000289internal::Configuration& Module::getConfig() {
290 if (!mConfig) {
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000291 switch (mType) {
292 case Type::DEFAULT:
293 mConfig = std::move(internal::getPrimaryConfiguration());
294 break;
295 case Type::R_SUBMIX:
296 mConfig = std::move(internal::getRSubmixConfiguration());
297 break;
Mikhail Naganov521fc492023-07-11 17:24:08 -0700298 case Type::STUB:
299 mConfig = std::move(internal::getStubConfiguration());
300 break;
jiabinb309d8d2023-01-20 19:07:15 +0000301 case Type::USB:
302 mConfig = std::move(internal::getUsbConfiguration());
jiabin253bd322023-01-25 23:57:31 +0000303 break;
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000304 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000305 }
306 return *mConfig;
307}
308
309void Module::registerPatch(const AudioPatch& patch) {
310 auto& configs = getConfig().portConfigs;
311 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
312 for (auto portConfigId : portConfigIds) {
313 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
314 if (configIt != configs.end()) {
315 mPatches.insert(std::pair{portConfigId, patch.id});
316 if (configIt->portId != portConfigId) {
317 mPatches.insert(std::pair{configIt->portId, patch.id});
318 }
319 }
320 };
321 };
322 do_insert(patch.sourcePortConfigIds);
323 do_insert(patch.sinkPortConfigIds);
324}
325
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700326ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
327 const AudioPatch& newPatch) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000328 // Streams from the old patch need to be disconnected, streams from the new
329 // patch need to be connected. If the stream belongs to both patches, no need
330 // to update it.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700331 auto maybeFailure = ndk::ScopedAStatus::ok();
332 std::set<int32_t> idsToDisconnect, idsToConnect, idsToDisconnectOnFailure;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000333 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
334 oldPatch.sourcePortConfigIds.end());
335 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
336 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
337 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
338 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700339 if (idsToConnect.count(portConfigId) == 0 && mStreams.count(portConfigId) != 0) {
340 if (auto status = mStreams.setStreamConnectedDevices(portConfigId, {}); status.isOk()) {
341 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
342 << portConfigId << " has been disconnected";
343 } else {
344 // Disconnection is tricky to roll back, just register a failure.
345 maybeFailure = std::move(status);
346 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000347 }
348 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700349 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000350 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700351 if (idsToDisconnect.count(portConfigId) == 0 && mStreams.count(portConfigId) != 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000352 const auto connectedDevices = findConnectedDevices(portConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700353 if (connectedDevices.empty()) {
354 // This is important as workers use the vector size to derive the connection status.
355 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
356 "config id "
357 << portConfigId;
358 }
359 if (auto status = mStreams.setStreamConnectedDevices(portConfigId, connectedDevices);
360 status.isOk()) {
361 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
362 << portConfigId << " has been connected to: "
363 << ::android::internal::ToString(connectedDevices);
364 } else {
365 maybeFailure = std::move(status);
366 idsToDisconnectOnFailure.insert(portConfigId);
367 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000368 }
369 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700370 if (!maybeFailure.isOk()) {
371 LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids "
372 << ::android::internal::ToString(idsToDisconnectOnFailure);
373 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
374 [&](const auto& portConfigId) {
375 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
376 (void)status.isOk(); // Can't do much about a failure here.
377 });
378 return maybeFailure;
379 }
380 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000381}
382
Mikhail Naganov00603d12022-05-02 22:52:13 +0000383ndk::ScopedAStatus Module::setModuleDebug(
384 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700385 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000386 << ", new flags: " << in_debug.toString();
387 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
388 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700389 LOG(ERROR) << __func__ << ": " << mType
390 << ": attempting to change device connections simulation while having external "
391 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000392 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
393 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000394 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700395 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000396 << in_debug.streamTransientStateDelayMs;
397 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
398 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000399 mDebug = in_debug;
400 return ndk::ScopedAStatus::ok();
401}
402
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000403ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700404 *_aidl_return = nullptr;
405 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000406 return ndk::ScopedAStatus::ok();
407}
408
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000409ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700410 *_aidl_return = nullptr;
411 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000412 return ndk::ScopedAStatus::ok();
413}
414
Mikhail Naganov7499a002023-02-27 18:51:44 -0800415ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700416 *_aidl_return = nullptr;
417 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800418 return ndk::ScopedAStatus::ok();
419}
420
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800421ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700422 *_aidl_return = nullptr;
423 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800424 return ndk::ScopedAStatus::ok();
425}
426
Mikhail Naganov00603d12022-05-02 22:52:13 +0000427ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
428 AudioPort* _aidl_return) {
429 const int32_t templateId = in_templateIdAndAdditionalData.id;
430 auto& ports = getConfig().ports;
431 AudioPort connectedPort;
432 { // Scope the template port so that we don't accidentally modify it.
433 auto templateIt = findById<AudioPort>(ports, templateId);
434 if (templateIt == ports.end()) {
435 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
436 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
437 }
438 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
439 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
440 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
441 }
442 if (!templateIt->profiles.empty()) {
443 LOG(ERROR) << __func__ << ": port id " << templateId
444 << " does not have dynamic profiles";
445 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
446 }
447 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
448 if (templateDevicePort.device.type.connection.empty()) {
449 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
450 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
451 }
452 // Postpone id allocation until we ensure that there are no client errors.
453 connectedPort = *templateIt;
454 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
455 const auto& inputDevicePort =
456 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
457 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
458 connectedDevicePort.device.address = inputDevicePort.device.address;
459 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
460 << connectedDevicePort.device.toString();
461 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700462 for (auto connectedPortPair : mConnectedDevicePorts) {
463 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000464 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
465 connectedDevicePort.device) {
466 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700467 << " is already connected at the device port id "
468 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000469 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
470 }
471 }
472 }
473
474 if (!mDebug.simulateDeviceConnections) {
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700475 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700476 } 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
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530489 for (auto profile : connectedPort.profiles) {
490 if (profile.channelMasks.empty()) {
491 LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no channel masks";
492 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
493 }
494 if (profile.sampleRates.empty()) {
495 LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no sample rates";
496 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
497 }
498 }
499
Mikhail Naganov00603d12022-05-02 22:52:13 +0000500 connectedPort.id = ++getConfig().nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700501 auto [connectedPortsIt, _] =
502 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::vector<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000503 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
504 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000505 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000506 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000507
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700508 std::vector<int32_t> routablePortIds;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000509 std::vector<AudioRoute> newRoutes;
510 auto& routes = getConfig().routes;
511 for (auto& r : routes) {
512 if (r.sinkPortId == templateId) {
513 AudioRoute newRoute;
514 newRoute.sourcePortIds = r.sourcePortIds;
515 newRoute.sinkPortId = connectedPort.id;
516 newRoute.isExclusive = r.isExclusive;
517 newRoutes.push_back(std::move(newRoute));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700518 routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(),
519 r.sourcePortIds.end());
Mikhail Naganov00603d12022-05-02 22:52:13 +0000520 } else {
521 auto& srcs = r.sourcePortIds;
522 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
523 srcs.push_back(connectedPort.id);
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700524 routablePortIds.push_back(r.sinkPortId);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000525 }
526 }
527 }
528 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
529
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700530 // Note: this is a simplistic approach assuming that a mix port can only be populated
531 // from a single device port. Implementing support for stuffing dynamic profiles with a superset
532 // of all profiles from all routable dynamic device ports would be more involved.
533 for (const auto mixPortId : routablePortIds) {
534 auto portsIt = findById<AudioPort>(ports, mixPortId);
535 if (portsIt != ports.end() && portsIt->profiles.empty()) {
536 portsIt->profiles = connectedPort.profiles;
537 connectedPortsIt->second.push_back(portsIt->id);
538 }
539 }
540 *_aidl_return = std::move(connectedPort);
541
Mikhail Naganov00603d12022-05-02 22:52:13 +0000542 return ndk::ScopedAStatus::ok();
543}
544
545ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
546 auto& ports = getConfig().ports;
547 auto portIt = findById<AudioPort>(ports, in_portId);
548 if (portIt == ports.end()) {
549 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
550 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
551 }
552 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
553 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
554 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
555 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700556 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
557 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000558 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
559 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
560 }
561 auto& configs = getConfig().portConfigs;
562 auto& initials = getConfig().initialConfigs;
563 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
564 if (config.portId == in_portId) {
565 // Check if the configuration was provided by the client.
566 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
567 return initialIt == initials.end() || config != *initialIt;
568 }
569 return false;
570 });
571 if (configIt != configs.end()) {
572 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
573 << configIt->id;
574 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
575 }
jiabin783c48b2023-02-28 18:28:06 +0000576 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000577 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000578 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
579
580 auto& routes = getConfig().routes;
581 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
582 if (routesIt->sinkPortId == in_portId) {
583 routesIt = routes.erase(routesIt);
584 } else {
585 // Note: the list of sourcePortIds can't become empty because there must
586 // be the id of the template port in the route.
587 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
588 ++routesIt;
589 }
590 }
591
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700592 for (const auto mixPortId : connectedPortsIt->second) {
593 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
594 if (mixPortIt != ports.end()) {
595 mixPortIt->profiles = {};
596 }
597 }
598 mConnectedDevicePorts.erase(connectedPortsIt);
599
Mikhail Naganov00603d12022-05-02 22:52:13 +0000600 return ndk::ScopedAStatus::ok();
601}
602
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000603ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
604 *_aidl_return = getConfig().patches;
605 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
606 return ndk::ScopedAStatus::ok();
607}
608
609ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
610 auto& ports = getConfig().ports;
611 auto portIt = findById<AudioPort>(ports, in_portId);
612 if (portIt != ports.end()) {
613 *_aidl_return = *portIt;
614 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
615 return ndk::ScopedAStatus::ok();
616 }
617 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
618 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
619}
620
621ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
622 *_aidl_return = getConfig().portConfigs;
623 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
624 return ndk::ScopedAStatus::ok();
625}
626
627ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
628 *_aidl_return = getConfig().ports;
629 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
630 return ndk::ScopedAStatus::ok();
631}
632
633ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
634 *_aidl_return = getConfig().routes;
635 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
636 return ndk::ScopedAStatus::ok();
637}
638
Mikhail Naganov00603d12022-05-02 22:52:13 +0000639ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
640 std::vector<AudioRoute>* _aidl_return) {
641 auto& ports = getConfig().ports;
642 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
643 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
644 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
645 }
646 auto& routes = getConfig().routes;
647 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
648 [&](const auto& r) {
649 const auto& srcs = r.sourcePortIds;
650 return r.sinkPortId == in_portId ||
651 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
652 });
653 return ndk::ScopedAStatus::ok();
654}
655
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000656ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
657 OpenInputStreamReturn* _aidl_return) {
658 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
659 << in_args.bufferSizeFrames << " frames";
660 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700661 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000662 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
663 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000664 << " does not correspond to an input mix port";
665 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
666 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000667 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700668 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
669 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000670 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000671 std::shared_ptr<StreamIn> stream;
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700672 RETURN_STATUS_IF_ERROR(createInputStream(in_args.sinkMetadata, std::move(context),
673 mConfig->microphones, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000674 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700675 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
676 RETURN_STATUS_IF_ERROR(
677 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
678 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000679 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
680 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000681 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000682 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000683 return ndk::ScopedAStatus::ok();
684}
685
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000686ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
687 OpenOutputStreamReturn* _aidl_return) {
688 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
689 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
690 << " frames";
691 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700692 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000693 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
694 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000695 << " does not correspond to an output mix port";
696 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
697 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000698 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
699 AudioOutputFlags::COMPRESS_OFFLOAD);
700 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000701 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000702 << " has COMPRESS_OFFLOAD flag set, requires offload info";
703 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
704 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000705 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
706 AudioOutputFlags::NON_BLOCKING);
707 if (isNonBlocking && in_args.callback == nullptr) {
708 LOG(ERROR) << __func__ << ": port id " << port->id
709 << " has NON_BLOCKING flag set, requires async callback";
710 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
711 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000712 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700713 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
714 isNonBlocking ? in_args.callback : nullptr,
715 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000716 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000717 std::shared_ptr<StreamOut> stream;
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700718 RETURN_STATUS_IF_ERROR(createOutputStream(in_args.sourceMetadata, std::move(context),
719 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000720 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700721 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
722 RETURN_STATUS_IF_ERROR(
723 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
724 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000725 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
726 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000727 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000728 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000729 return ndk::ScopedAStatus::ok();
730}
731
Mikhail Naganov74927202022-12-19 16:37:14 +0000732ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
733 SupportedPlaybackRateFactors* _aidl_return) {
734 LOG(DEBUG) << __func__;
735 (void)_aidl_return;
736 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
737}
738
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000739ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000740 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000741 if (in_requested.sourcePortConfigIds.empty()) {
742 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
743 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
744 }
745 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
746 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
747 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
748 }
749 if (in_requested.sinkPortConfigIds.empty()) {
750 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
751 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
752 }
753 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
754 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
755 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
756 }
757
758 auto& configs = getConfig().portConfigs;
759 std::vector<int32_t> missingIds;
760 auto sources =
761 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
762 if (!missingIds.empty()) {
763 LOG(ERROR) << __func__ << ": following source port config ids not found: "
764 << ::android::internal::ToString(missingIds);
765 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
766 }
767 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
768 if (!missingIds.empty()) {
769 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
770 << ::android::internal::ToString(missingIds);
771 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
772 }
773 // bool indicates whether a non-exclusive route is available.
774 // If only an exclusive route is available, that means the patch can not be
775 // established if there is any other patch which currently uses the sink port.
776 std::map<int32_t, bool> allowedSinkPorts;
777 auto& routes = getConfig().routes;
778 for (auto src : sources) {
779 for (const auto& r : routes) {
780 const auto& srcs = r.sourcePortIds;
781 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
782 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
783 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
784 }
785 }
786 }
787 }
788 for (auto sink : sinks) {
789 if (allowedSinkPorts.count(sink->portId) == 0) {
790 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
791 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
792 }
793 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700794 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000795
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000796 auto& patches = getConfig().patches;
797 auto existing = patches.end();
798 std::optional<decltype(mPatches)> patchesBackup;
799 if (in_requested.id != 0) {
800 existing = findById<AudioPatch>(patches, in_requested.id);
801 if (existing != patches.end()) {
802 patchesBackup = mPatches;
803 cleanUpPatch(existing->id);
804 } else {
805 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
806 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
807 }
808 }
809 // Validate the requested patch.
810 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
811 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
812 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
813 << "is exclusive and is already used by some other patch";
814 if (patchesBackup.has_value()) {
815 mPatches = std::move(*patchesBackup);
816 }
817 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
818 }
819 }
820 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000821 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
822 _aidl_return->latenciesMs.clear();
823 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
824 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000825 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000826 if (existing == patches.end()) {
827 _aidl_return->id = getConfig().nextPatchId++;
828 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000829 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000830 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000831 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700832 patchesBackup = mPatches;
833 registerPatch(*_aidl_return);
834 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
835 mPatches = std::move(*patchesBackup);
836 if (existing == patches.end()) {
837 patches.pop_back();
838 } else {
839 *existing = oldPatch;
840 }
841 return status;
842 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000843
844 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
845 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000846 return ndk::ScopedAStatus::ok();
847}
848
849ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
850 AudioPortConfig* out_suggested, bool* _aidl_return) {
851 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
852 auto& configs = getConfig().portConfigs;
853 auto existing = configs.end();
854 if (in_requested.id != 0) {
855 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
856 existing == configs.end()) {
857 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
858 << " not found";
859 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
860 }
861 }
862
863 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
864 if (portId == 0) {
865 LOG(ERROR) << __func__ << ": input port config does not specify portId";
866 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
867 }
868 auto& ports = getConfig().ports;
869 auto portIt = findById<AudioPort>(ports, portId);
870 if (portIt == ports.end()) {
871 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
872 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
873 }
874 if (existing != configs.end()) {
875 *out_suggested = *existing;
876 } else {
877 AudioPortConfig newConfig;
878 if (generateDefaultPortConfig(*portIt, &newConfig)) {
879 *out_suggested = newConfig;
880 } else {
881 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
882 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
883 }
884 }
885 // From this moment, 'out_suggested' is either an existing port config,
886 // or a new generated config. Now attempt to update it according to the specified
887 // fields of 'in_requested'.
888
889 bool requestedIsValid = true, requestedIsFullySpecified = true;
890
891 AudioIoFlags portFlags = portIt->flags;
892 if (in_requested.flags.has_value()) {
893 if (in_requested.flags.value() != portFlags) {
894 LOG(WARNING) << __func__ << ": requested flags "
895 << in_requested.flags.value().toString() << " do not match port's "
896 << portId << " flags " << portFlags.toString();
897 requestedIsValid = false;
898 }
899 } else {
900 requestedIsFullySpecified = false;
901 }
902
903 AudioProfile portProfile;
904 if (in_requested.format.has_value()) {
905 const auto& format = in_requested.format.value();
906 if (findAudioProfile(*portIt, format, &portProfile)) {
907 out_suggested->format = format;
908 } else {
909 LOG(WARNING) << __func__ << ": requested format " << format.toString()
910 << " is not found in port's " << portId << " profiles";
911 requestedIsValid = false;
912 }
913 } else {
914 requestedIsFullySpecified = false;
915 }
916 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
917 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
918 << out_suggested->format.value().toString() << " anymore";
919 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
920 }
921
922 if (in_requested.channelMask.has_value()) {
923 const auto& channelMask = in_requested.channelMask.value();
924 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
925 portProfile.channelMasks.end()) {
926 out_suggested->channelMask = channelMask;
927 } else {
928 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
929 << " is not supported for the format " << portProfile.format.toString()
930 << " by the port " << portId;
931 requestedIsValid = false;
932 }
933 } else {
934 requestedIsFullySpecified = false;
935 }
936
937 if (in_requested.sampleRate.has_value()) {
938 const auto& sampleRate = in_requested.sampleRate.value();
939 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
940 sampleRate.value) != portProfile.sampleRates.end()) {
941 out_suggested->sampleRate = sampleRate;
942 } else {
943 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
944 << " is not supported for the format " << portProfile.format.toString()
945 << " by the port " << portId;
946 requestedIsValid = false;
947 }
948 } else {
949 requestedIsFullySpecified = false;
950 }
951
952 if (in_requested.gain.has_value()) {
953 // Let's pretend that gain can always be applied.
954 out_suggested->gain = in_requested.gain.value();
955 }
956
Mikhail Naganov248e9502023-02-21 16:32:40 -0800957 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
958 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
959 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
960 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
961 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
962 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
963 }
964 } else {
965 LOG(WARNING) << __func__ << ": requested ext tag "
966 << toString(in_requested.ext.getTag()) << " do not match port's tag "
967 << toString(out_suggested->ext.getTag());
968 requestedIsValid = false;
969 }
970 }
971
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000972 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
973 out_suggested->id = getConfig().nextPortId++;
974 configs.push_back(*out_suggested);
975 *_aidl_return = true;
976 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
977 } else if (existing != configs.end() && requestedIsValid) {
978 *existing = *out_suggested;
979 *_aidl_return = true;
980 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
981 } else {
982 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
983 << "; requested is valid? " << requestedIsValid << ", fully specified? "
984 << requestedIsFullySpecified;
985 *_aidl_return = false;
986 }
987 return ndk::ScopedAStatus::ok();
988}
989
990ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
991 auto& patches = getConfig().patches;
992 auto patchIt = findById<AudioPatch>(patches, in_patchId);
993 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700994 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000995 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700996 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
997 mPatches = std::move(patchesBackup);
998 return status;
999 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001000 patches.erase(patchIt);
1001 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1002 return ndk::ScopedAStatus::ok();
1003 }
1004 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1005 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1006}
1007
1008ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1009 auto& configs = getConfig().portConfigs;
1010 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1011 if (configIt != configs.end()) {
1012 if (mStreams.count(in_portConfigId) != 0) {
1013 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1014 << " has a stream opened on it";
1015 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1016 }
1017 auto patchIt = mPatches.find(in_portConfigId);
1018 if (patchIt != mPatches.end()) {
1019 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1020 << " is used by the patch with id " << patchIt->second;
1021 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1022 }
1023 auto& initials = getConfig().initialConfigs;
1024 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1025 if (initialIt == initials.end()) {
1026 configs.erase(configIt);
1027 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1028 } else if (*configIt != *initialIt) {
1029 *configIt = *initialIt;
1030 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1031 }
1032 return ndk::ScopedAStatus::ok();
1033 }
1034 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1035 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1036}
1037
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001038ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1039 *_aidl_return = mMasterMute;
1040 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1041 return ndk::ScopedAStatus::ok();
1042}
1043
1044ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1045 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001046 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1047 : onMasterMuteChanged(in_mute);
1048 if (result.isOk()) {
1049 mMasterMute = in_mute;
1050 } else {
1051 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1052 << "), error=" << result;
1053 // Reset master mute if it failed.
1054 onMasterMuteChanged(mMasterMute);
1055 }
1056 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001057}
1058
1059ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1060 *_aidl_return = mMasterVolume;
1061 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1062 return ndk::ScopedAStatus::ok();
1063}
1064
1065ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1066 LOG(DEBUG) << __func__ << ": " << in_volume;
1067 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001068 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1069 : onMasterVolumeChanged(in_volume);
1070 if (result.isOk()) {
1071 mMasterVolume = in_volume;
1072 } else {
1073 // Reset master volume if it failed.
1074 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1075 << "), error=" << result;
1076 onMasterVolumeChanged(mMasterVolume);
1077 }
1078 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001079 }
1080 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1081 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1082}
1083
1084ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1085 *_aidl_return = mMicMute;
1086 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1087 return ndk::ScopedAStatus::ok();
1088}
1089
1090ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1091 LOG(DEBUG) << __func__ << ": " << in_mute;
1092 mMicMute = in_mute;
1093 return ndk::ScopedAStatus::ok();
1094}
1095
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001096ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001097 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001098 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1099 return ndk::ScopedAStatus::ok();
1100}
1101
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001102ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001103 if (!isValidAudioMode(in_mode)) {
1104 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1105 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1106 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001107 // No checks for supported audio modes here, it's an informative notification.
1108 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1109 return ndk::ScopedAStatus::ok();
1110}
1111
1112ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1113 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1114 return ndk::ScopedAStatus::ok();
1115}
1116
1117ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1118 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1119 return ndk::ScopedAStatus::ok();
1120}
1121
Vlad Popa83a6d822022-11-07 13:53:57 +01001122ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001123 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001124 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001125 }
Mikhail Naganov7499a002023-02-27 18:51:44 -08001126 *_aidl_return = mSoundDose.getPtr();
Vlad Popa943b7e22022-12-08 14:24:12 +01001127 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001128 return ndk::ScopedAStatus::ok();
1129}
1130
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001131ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1132 LOG(DEBUG) << __func__;
1133 (void)_aidl_return;
1134 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1135}
1136
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001137const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001138const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001139
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001140ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1141 std::vector<VendorParameter>* _aidl_return) {
1142 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001143 bool allParametersKnown = true;
1144 for (const auto& id : in_ids) {
1145 if (id == VendorDebug::kForceTransientBurstName) {
1146 VendorParameter forceTransientBurst{.id = id};
1147 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1148 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001149 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1150 VendorParameter forceSynchronousDrain{.id = id};
1151 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1152 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001153 } else {
1154 allParametersKnown = false;
1155 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1156 }
1157 }
1158 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1159 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001160}
1161
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001162namespace {
1163
1164template <typename W>
1165bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1166 std::optional<W> value;
1167 binder_status_t result = p.ext.getParcelable(&value);
1168 if (result == STATUS_OK && value.has_value()) {
1169 *v = value.value().value;
1170 return true;
1171 }
1172 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1173 << "\": " << result;
1174 return false;
1175}
1176
1177} // namespace
1178
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001179ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1180 bool in_async) {
1181 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1182 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001183 bool allParametersKnown = true;
1184 for (const auto& p : in_parameters) {
1185 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001186 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1187 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1188 }
1189 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1190 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001191 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1192 }
1193 } else {
1194 allParametersKnown = false;
1195 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1196 }
1197 }
1198 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1199 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001200}
1201
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001202ndk::ScopedAStatus Module::addDeviceEffect(
1203 int32_t in_portConfigId,
1204 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1205 if (in_effect == nullptr) {
1206 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1207 } else {
1208 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1209 << in_effect->asBinder().get();
1210 }
1211 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1212}
1213
1214ndk::ScopedAStatus Module::removeDeviceEffect(
1215 int32_t in_portConfigId,
1216 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1217 if (in_effect == nullptr) {
1218 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1219 } else {
1220 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1221 << in_effect->asBinder().get();
1222 }
1223 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1224}
1225
jiabin9a8e6862023-01-12 23:06:37 +00001226ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1227 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1228 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1229 std::set<int32_t> mmapSinks;
1230 std::set<int32_t> mmapSources;
1231 auto& ports = getConfig().ports;
1232 for (const auto& port : ports) {
1233 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1234 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1235 AudioInputFlags::MMAP_NOIRQ)) {
1236 mmapSinks.insert(port.id);
1237 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1238 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1239 AudioOutputFlags::MMAP_NOIRQ)) {
1240 mmapSources.insert(port.id);
1241 }
1242 }
1243 for (const auto& route : getConfig().routes) {
1244 if (mmapSinks.count(route.sinkPortId) != 0) {
1245 // The sink is a mix port, add the sources if they are device ports.
1246 for (int sourcePortId : route.sourcePortIds) {
1247 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1248 if (sourcePortIt == ports.end()) {
1249 // This must not happen
1250 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1251 continue;
1252 }
1253 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1254 // The source is not a device port, skip
1255 continue;
1256 }
1257 AudioMMapPolicyInfo policyInfo;
1258 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1259 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1260 // default implementation.
1261 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1262 _aidl_return->push_back(policyInfo);
1263 }
1264 } else {
1265 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1266 if (sinkPortIt == ports.end()) {
1267 // This must not happen
1268 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1269 continue;
1270 }
1271 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1272 // The sink is not a device port, skip
1273 continue;
1274 }
1275 if (count_any(mmapSources, route.sourcePortIds)) {
1276 AudioMMapPolicyInfo policyInfo;
1277 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1278 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1279 // default implementation.
1280 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1281 _aidl_return->push_back(policyInfo);
1282 }
1283 }
1284 }
1285 return ndk::ScopedAStatus::ok();
1286}
1287
Eric Laurente2432ea2023-01-12 17:47:31 +01001288ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1289 LOG(DEBUG) << __func__;
1290 *_aidl_return = false;
1291 return ndk::ScopedAStatus::ok();
1292}
1293
jiabinb76981e2023-01-18 00:58:30 +00001294ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1295 if (!isMmapSupported()) {
1296 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1297 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1298 }
1299 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1300 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1301 return ndk::ScopedAStatus::ok();
1302}
1303
1304ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1305 if (!isMmapSupported()) {
1306 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1307 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1308 }
1309 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1310 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1311 return ndk::ScopedAStatus::ok();
1312}
1313
1314bool Module::isMmapSupported() {
1315 if (mIsMmapSupported.has_value()) {
1316 return mIsMmapSupported.value();
1317 }
1318 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1319 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1320 mIsMmapSupported = false;
1321 } else {
1322 mIsMmapSupported =
1323 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1324 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1325 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1326 }) != mmapPolicyInfos.end();
1327 }
1328 return mIsMmapSupported.value();
1329}
1330
jiabin253bd322023-01-25 23:57:31 +00001331ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001332 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001333 return ndk::ScopedAStatus::ok();
1334}
1335
1336ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1337 const std::vector<AudioPortConfig*>& sources __unused,
1338 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001339 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001340 return ndk::ScopedAStatus::ok();
1341}
1342
jiabin783c48b2023-02-28 18:28:06 +00001343void Module::onExternalDeviceConnectionChanged(
1344 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1345 bool connected __unused) {
1346 LOG(DEBUG) << __func__ << ": do nothing and return";
1347}
1348
1349ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1350 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1351 return ndk::ScopedAStatus::ok();
1352}
1353
1354ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1355 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1356 return ndk::ScopedAStatus::ok();
1357}
1358
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001359} // namespace aidl::android::hardware::audio::core