blob: c0c6c4889d34cea0b64af5d1dfe9e45cc5f9dfe4 [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 Naganov75b59df2023-06-23 13:39:40 -0700343ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
344 const AudioPatch& newPatch) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000345 // Streams from the old patch need to be disconnected, streams from the new
346 // patch need to be connected. If the stream belongs to both patches, no need
347 // to update it.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700348 auto maybeFailure = ndk::ScopedAStatus::ok();
349 std::set<int32_t> idsToDisconnect, idsToConnect, idsToDisconnectOnFailure;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000350 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
351 oldPatch.sourcePortConfigIds.end());
352 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
353 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
354 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
355 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700356 if (idsToConnect.count(portConfigId) == 0 && mStreams.count(portConfigId) != 0) {
357 if (auto status = mStreams.setStreamConnectedDevices(portConfigId, {}); status.isOk()) {
358 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
359 << portConfigId << " has been disconnected";
360 } else {
361 // Disconnection is tricky to roll back, just register a failure.
362 maybeFailure = std::move(status);
363 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000364 }
365 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700366 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000367 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700368 if (idsToDisconnect.count(portConfigId) == 0 && mStreams.count(portConfigId) != 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000369 const auto connectedDevices = findConnectedDevices(portConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700370 if (connectedDevices.empty()) {
371 // This is important as workers use the vector size to derive the connection status.
372 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
373 "config id "
374 << portConfigId;
375 }
376 if (auto status = mStreams.setStreamConnectedDevices(portConfigId, connectedDevices);
377 status.isOk()) {
378 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
379 << portConfigId << " has been connected to: "
380 << ::android::internal::ToString(connectedDevices);
381 } else {
382 maybeFailure = std::move(status);
383 idsToDisconnectOnFailure.insert(portConfigId);
384 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000385 }
386 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700387 if (!maybeFailure.isOk()) {
388 LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids "
389 << ::android::internal::ToString(idsToDisconnectOnFailure);
390 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
391 [&](const auto& portConfigId) {
392 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
393 (void)status.isOk(); // Can't do much about a failure here.
394 });
395 return maybeFailure;
396 }
397 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000398}
399
Mikhail Naganov00603d12022-05-02 22:52:13 +0000400ndk::ScopedAStatus Module::setModuleDebug(
401 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700402 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000403 << ", new flags: " << in_debug.toString();
404 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
405 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700406 LOG(ERROR) << __func__ << ": " << mType
407 << ": attempting to change device connections simulation while having external "
408 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000409 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
410 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000411 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700412 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000413 << in_debug.streamTransientStateDelayMs;
414 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
415 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000416 mDebug = in_debug;
417 return ndk::ScopedAStatus::ok();
418}
419
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000420ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800421 if (!mTelephony) {
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000422 mTelephony = ndk::SharedRefBase::make<Telephony>();
423 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800424 *_aidl_return = mTelephony.getPtr();
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000425 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
426 return ndk::ScopedAStatus::ok();
427}
428
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000429ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800430 if (!mBluetooth) {
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000431 mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000432 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800433 *_aidl_return = mBluetooth.getPtr();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000434 LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get();
435 return ndk::ScopedAStatus::ok();
436}
437
Mikhail Naganov7499a002023-02-27 18:51:44 -0800438ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
439 if (!mBluetoothA2dp) {
440 mBluetoothA2dp = ndk::SharedRefBase::make<BluetoothA2dp>();
441 }
442 *_aidl_return = mBluetoothA2dp.getPtr();
443 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothA2dp: " << _aidl_return->get();
444 return ndk::ScopedAStatus::ok();
445}
446
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800447ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
448 if (!mBluetoothLe) {
449 mBluetoothLe = ndk::SharedRefBase::make<BluetoothLe>();
450 }
451 *_aidl_return = mBluetoothLe.getPtr();
452 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothLe: " << _aidl_return->get();
453 return ndk::ScopedAStatus::ok();
454}
455
Mikhail Naganov00603d12022-05-02 22:52:13 +0000456ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
457 AudioPort* _aidl_return) {
458 const int32_t templateId = in_templateIdAndAdditionalData.id;
459 auto& ports = getConfig().ports;
460 AudioPort connectedPort;
461 { // Scope the template port so that we don't accidentally modify it.
462 auto templateIt = findById<AudioPort>(ports, templateId);
463 if (templateIt == ports.end()) {
464 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
465 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
466 }
467 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
468 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
469 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
470 }
471 if (!templateIt->profiles.empty()) {
472 LOG(ERROR) << __func__ << ": port id " << templateId
473 << " does not have dynamic profiles";
474 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
475 }
476 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
477 if (templateDevicePort.device.type.connection.empty()) {
478 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
479 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
480 }
481 // Postpone id allocation until we ensure that there are no client errors.
482 connectedPort = *templateIt;
483 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
484 const auto& inputDevicePort =
485 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
486 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
487 connectedDevicePort.device.address = inputDevicePort.device.address;
488 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
489 << connectedDevicePort.device.toString();
490 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700491 for (auto connectedPortPair : mConnectedDevicePorts) {
492 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000493 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
494 connectedDevicePort.device) {
495 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700496 << " is already connected at the device port id "
497 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000498 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
499 }
500 }
501 }
502
503 if (!mDebug.simulateDeviceConnections) {
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700504 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700505 } else {
506 auto& connectedProfiles = getConfig().connectedProfiles;
507 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
508 connectedProfilesIt != connectedProfiles.end()) {
509 connectedPort.profiles = connectedProfilesIt->second;
510 }
511 }
512 if (connectedPort.profiles.empty()) {
513 LOG(ERROR) << "Profiles of a connected port still empty after connecting external device "
514 << connectedPort.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000515 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
516 }
517
518 connectedPort.id = ++getConfig().nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700519 auto [connectedPortsIt, _] =
520 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::vector<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000521 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
522 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000523 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000524 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000525
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700526 std::vector<int32_t> routablePortIds;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000527 std::vector<AudioRoute> newRoutes;
528 auto& routes = getConfig().routes;
529 for (auto& r : routes) {
530 if (r.sinkPortId == templateId) {
531 AudioRoute newRoute;
532 newRoute.sourcePortIds = r.sourcePortIds;
533 newRoute.sinkPortId = connectedPort.id;
534 newRoute.isExclusive = r.isExclusive;
535 newRoutes.push_back(std::move(newRoute));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700536 routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(),
537 r.sourcePortIds.end());
Mikhail Naganov00603d12022-05-02 22:52:13 +0000538 } else {
539 auto& srcs = r.sourcePortIds;
540 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
541 srcs.push_back(connectedPort.id);
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700542 routablePortIds.push_back(r.sinkPortId);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000543 }
544 }
545 }
546 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
547
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700548 // Note: this is a simplistic approach assuming that a mix port can only be populated
549 // from a single device port. Implementing support for stuffing dynamic profiles with a superset
550 // of all profiles from all routable dynamic device ports would be more involved.
551 for (const auto mixPortId : routablePortIds) {
552 auto portsIt = findById<AudioPort>(ports, mixPortId);
553 if (portsIt != ports.end() && portsIt->profiles.empty()) {
554 portsIt->profiles = connectedPort.profiles;
555 connectedPortsIt->second.push_back(portsIt->id);
556 }
557 }
558 *_aidl_return = std::move(connectedPort);
559
Mikhail Naganov00603d12022-05-02 22:52:13 +0000560 return ndk::ScopedAStatus::ok();
561}
562
563ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
564 auto& ports = getConfig().ports;
565 auto portIt = findById<AudioPort>(ports, in_portId);
566 if (portIt == ports.end()) {
567 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
568 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
569 }
570 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
571 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
572 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
573 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700574 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
575 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000576 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
577 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
578 }
579 auto& configs = getConfig().portConfigs;
580 auto& initials = getConfig().initialConfigs;
581 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
582 if (config.portId == in_portId) {
583 // Check if the configuration was provided by the client.
584 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
585 return initialIt == initials.end() || config != *initialIt;
586 }
587 return false;
588 });
589 if (configIt != configs.end()) {
590 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
591 << configIt->id;
592 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
593 }
jiabin783c48b2023-02-28 18:28:06 +0000594 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000595 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000596 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
597
598 auto& routes = getConfig().routes;
599 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
600 if (routesIt->sinkPortId == in_portId) {
601 routesIt = routes.erase(routesIt);
602 } else {
603 // Note: the list of sourcePortIds can't become empty because there must
604 // be the id of the template port in the route.
605 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
606 ++routesIt;
607 }
608 }
609
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700610 for (const auto mixPortId : connectedPortsIt->second) {
611 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
612 if (mixPortIt != ports.end()) {
613 mixPortIt->profiles = {};
614 }
615 }
616 mConnectedDevicePorts.erase(connectedPortsIt);
617
Mikhail Naganov00603d12022-05-02 22:52:13 +0000618 return ndk::ScopedAStatus::ok();
619}
620
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000621ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
622 *_aidl_return = getConfig().patches;
623 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
624 return ndk::ScopedAStatus::ok();
625}
626
627ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
628 auto& ports = getConfig().ports;
629 auto portIt = findById<AudioPort>(ports, in_portId);
630 if (portIt != ports.end()) {
631 *_aidl_return = *portIt;
632 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
633 return ndk::ScopedAStatus::ok();
634 }
635 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
636 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
637}
638
639ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
640 *_aidl_return = getConfig().portConfigs;
641 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
642 return ndk::ScopedAStatus::ok();
643}
644
645ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
646 *_aidl_return = getConfig().ports;
647 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
648 return ndk::ScopedAStatus::ok();
649}
650
651ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
652 *_aidl_return = getConfig().routes;
653 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
654 return ndk::ScopedAStatus::ok();
655}
656
Mikhail Naganov00603d12022-05-02 22:52:13 +0000657ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
658 std::vector<AudioRoute>* _aidl_return) {
659 auto& ports = getConfig().ports;
660 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
661 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
662 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
663 }
664 auto& routes = getConfig().routes;
665 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
666 [&](const auto& r) {
667 const auto& srcs = r.sourcePortIds;
668 return r.sinkPortId == in_portId ||
669 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
670 });
671 return ndk::ScopedAStatus::ok();
672}
673
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000674ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
675 OpenInputStreamReturn* _aidl_return) {
676 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
677 << in_args.bufferSizeFrames << " 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::input) {
681 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000682 << " does not correspond to an input mix port";
683 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
684 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000685 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700686 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
687 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000688 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000689 std::shared_ptr<StreamIn> stream;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700690 RETURN_STATUS_IF_ERROR(getStreamInCreator(mType)(in_args.sinkMetadata, std::move(context),
691 mConfig->microphones, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000692 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700693 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
694 RETURN_STATUS_IF_ERROR(
695 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
696 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000697 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
698 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000699 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000700 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000701 return ndk::ScopedAStatus::ok();
702}
703
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000704ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
705 OpenOutputStreamReturn* _aidl_return) {
706 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
707 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
708 << " frames";
709 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700710 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000711 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
712 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000713 << " does not correspond to an output mix port";
714 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
715 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000716 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
717 AudioOutputFlags::COMPRESS_OFFLOAD);
718 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000719 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000720 << " has COMPRESS_OFFLOAD flag set, requires offload info";
721 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
722 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000723 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
724 AudioOutputFlags::NON_BLOCKING);
725 if (isNonBlocking && in_args.callback == nullptr) {
726 LOG(ERROR) << __func__ << ": port id " << port->id
727 << " has NON_BLOCKING flag set, requires async callback";
728 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
729 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000730 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700731 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
732 isNonBlocking ? in_args.callback : nullptr,
733 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000734 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000735 std::shared_ptr<StreamOut> stream;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700736 RETURN_STATUS_IF_ERROR(getStreamOutCreator(mType)(in_args.sourceMetadata, std::move(context),
737 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000738 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700739 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
740 RETURN_STATUS_IF_ERROR(
741 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
742 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000743 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
744 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000745 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000746 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000747 return ndk::ScopedAStatus::ok();
748}
749
Mikhail Naganov74927202022-12-19 16:37:14 +0000750ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
751 SupportedPlaybackRateFactors* _aidl_return) {
752 LOG(DEBUG) << __func__;
753 (void)_aidl_return;
754 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
755}
756
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000757ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000758 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000759 if (in_requested.sourcePortConfigIds.empty()) {
760 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
761 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
762 }
763 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
764 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
765 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
766 }
767 if (in_requested.sinkPortConfigIds.empty()) {
768 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
769 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
770 }
771 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
772 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
773 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
774 }
775
776 auto& configs = getConfig().portConfigs;
777 std::vector<int32_t> missingIds;
778 auto sources =
779 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
780 if (!missingIds.empty()) {
781 LOG(ERROR) << __func__ << ": following source port config ids not found: "
782 << ::android::internal::ToString(missingIds);
783 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
784 }
785 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
786 if (!missingIds.empty()) {
787 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
788 << ::android::internal::ToString(missingIds);
789 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
790 }
791 // bool indicates whether a non-exclusive route is available.
792 // If only an exclusive route is available, that means the patch can not be
793 // established if there is any other patch which currently uses the sink port.
794 std::map<int32_t, bool> allowedSinkPorts;
795 auto& routes = getConfig().routes;
796 for (auto src : sources) {
797 for (const auto& r : routes) {
798 const auto& srcs = r.sourcePortIds;
799 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
800 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
801 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
802 }
803 }
804 }
805 }
806 for (auto sink : sinks) {
807 if (allowedSinkPorts.count(sink->portId) == 0) {
808 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
809 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
810 }
811 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700812 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000813
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000814 auto& patches = getConfig().patches;
815 auto existing = patches.end();
816 std::optional<decltype(mPatches)> patchesBackup;
817 if (in_requested.id != 0) {
818 existing = findById<AudioPatch>(patches, in_requested.id);
819 if (existing != patches.end()) {
820 patchesBackup = mPatches;
821 cleanUpPatch(existing->id);
822 } else {
823 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
824 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
825 }
826 }
827 // Validate the requested patch.
828 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
829 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
830 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
831 << "is exclusive and is already used by some other patch";
832 if (patchesBackup.has_value()) {
833 mPatches = std::move(*patchesBackup);
834 }
835 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
836 }
837 }
838 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000839 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
840 _aidl_return->latenciesMs.clear();
841 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
842 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000843 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000844 if (existing == patches.end()) {
845 _aidl_return->id = getConfig().nextPatchId++;
846 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000847 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000848 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000849 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700850 patchesBackup = mPatches;
851 registerPatch(*_aidl_return);
852 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
853 mPatches = std::move(*patchesBackup);
854 if (existing == patches.end()) {
855 patches.pop_back();
856 } else {
857 *existing = oldPatch;
858 }
859 return status;
860 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000861
862 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
863 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000864 return ndk::ScopedAStatus::ok();
865}
866
867ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
868 AudioPortConfig* out_suggested, bool* _aidl_return) {
869 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
870 auto& configs = getConfig().portConfigs;
871 auto existing = configs.end();
872 if (in_requested.id != 0) {
873 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
874 existing == configs.end()) {
875 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
876 << " not found";
877 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
878 }
879 }
880
881 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
882 if (portId == 0) {
883 LOG(ERROR) << __func__ << ": input port config does not specify portId";
884 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
885 }
886 auto& ports = getConfig().ports;
887 auto portIt = findById<AudioPort>(ports, portId);
888 if (portIt == ports.end()) {
889 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
890 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
891 }
892 if (existing != configs.end()) {
893 *out_suggested = *existing;
894 } else {
895 AudioPortConfig newConfig;
896 if (generateDefaultPortConfig(*portIt, &newConfig)) {
897 *out_suggested = newConfig;
898 } else {
899 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
900 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
901 }
902 }
903 // From this moment, 'out_suggested' is either an existing port config,
904 // or a new generated config. Now attempt to update it according to the specified
905 // fields of 'in_requested'.
906
907 bool requestedIsValid = true, requestedIsFullySpecified = true;
908
909 AudioIoFlags portFlags = portIt->flags;
910 if (in_requested.flags.has_value()) {
911 if (in_requested.flags.value() != portFlags) {
912 LOG(WARNING) << __func__ << ": requested flags "
913 << in_requested.flags.value().toString() << " do not match port's "
914 << portId << " flags " << portFlags.toString();
915 requestedIsValid = false;
916 }
917 } else {
918 requestedIsFullySpecified = false;
919 }
920
921 AudioProfile portProfile;
922 if (in_requested.format.has_value()) {
923 const auto& format = in_requested.format.value();
924 if (findAudioProfile(*portIt, format, &portProfile)) {
925 out_suggested->format = format;
926 } else {
927 LOG(WARNING) << __func__ << ": requested format " << format.toString()
928 << " is not found in port's " << portId << " profiles";
929 requestedIsValid = false;
930 }
931 } else {
932 requestedIsFullySpecified = false;
933 }
934 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
935 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
936 << out_suggested->format.value().toString() << " anymore";
937 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
938 }
939
940 if (in_requested.channelMask.has_value()) {
941 const auto& channelMask = in_requested.channelMask.value();
942 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
943 portProfile.channelMasks.end()) {
944 out_suggested->channelMask = channelMask;
945 } else {
946 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
947 << " is not supported for the format " << portProfile.format.toString()
948 << " by the port " << portId;
949 requestedIsValid = false;
950 }
951 } else {
952 requestedIsFullySpecified = false;
953 }
954
955 if (in_requested.sampleRate.has_value()) {
956 const auto& sampleRate = in_requested.sampleRate.value();
957 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
958 sampleRate.value) != portProfile.sampleRates.end()) {
959 out_suggested->sampleRate = sampleRate;
960 } else {
961 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
962 << " is not supported for the format " << portProfile.format.toString()
963 << " by the port " << portId;
964 requestedIsValid = false;
965 }
966 } else {
967 requestedIsFullySpecified = false;
968 }
969
970 if (in_requested.gain.has_value()) {
971 // Let's pretend that gain can always be applied.
972 out_suggested->gain = in_requested.gain.value();
973 }
974
Mikhail Naganov248e9502023-02-21 16:32:40 -0800975 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
976 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
977 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
978 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
979 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
980 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
981 }
982 } else {
983 LOG(WARNING) << __func__ << ": requested ext tag "
984 << toString(in_requested.ext.getTag()) << " do not match port's tag "
985 << toString(out_suggested->ext.getTag());
986 requestedIsValid = false;
987 }
988 }
989
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000990 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
991 out_suggested->id = getConfig().nextPortId++;
992 configs.push_back(*out_suggested);
993 *_aidl_return = true;
994 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
995 } else if (existing != configs.end() && requestedIsValid) {
996 *existing = *out_suggested;
997 *_aidl_return = true;
998 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
999 } else {
1000 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
1001 << "; requested is valid? " << requestedIsValid << ", fully specified? "
1002 << requestedIsFullySpecified;
1003 *_aidl_return = false;
1004 }
1005 return ndk::ScopedAStatus::ok();
1006}
1007
1008ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1009 auto& patches = getConfig().patches;
1010 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1011 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001012 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001013 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001014 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1015 mPatches = std::move(patchesBackup);
1016 return status;
1017 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001018 patches.erase(patchIt);
1019 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1020 return ndk::ScopedAStatus::ok();
1021 }
1022 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1023 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1024}
1025
1026ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1027 auto& configs = getConfig().portConfigs;
1028 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1029 if (configIt != configs.end()) {
1030 if (mStreams.count(in_portConfigId) != 0) {
1031 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1032 << " has a stream opened on it";
1033 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1034 }
1035 auto patchIt = mPatches.find(in_portConfigId);
1036 if (patchIt != mPatches.end()) {
1037 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1038 << " is used by the patch with id " << patchIt->second;
1039 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1040 }
1041 auto& initials = getConfig().initialConfigs;
1042 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1043 if (initialIt == initials.end()) {
1044 configs.erase(configIt);
1045 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1046 } else if (*configIt != *initialIt) {
1047 *configIt = *initialIt;
1048 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1049 }
1050 return ndk::ScopedAStatus::ok();
1051 }
1052 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1053 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1054}
1055
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001056ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1057 *_aidl_return = mMasterMute;
1058 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1059 return ndk::ScopedAStatus::ok();
1060}
1061
1062ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1063 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001064 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1065 : onMasterMuteChanged(in_mute);
1066 if (result.isOk()) {
1067 mMasterMute = in_mute;
1068 } else {
1069 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1070 << "), error=" << result;
1071 // Reset master mute if it failed.
1072 onMasterMuteChanged(mMasterMute);
1073 }
1074 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001075}
1076
1077ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1078 *_aidl_return = mMasterVolume;
1079 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1080 return ndk::ScopedAStatus::ok();
1081}
1082
1083ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1084 LOG(DEBUG) << __func__ << ": " << in_volume;
1085 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001086 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1087 : onMasterVolumeChanged(in_volume);
1088 if (result.isOk()) {
1089 mMasterVolume = in_volume;
1090 } else {
1091 // Reset master volume if it failed.
1092 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1093 << "), error=" << result;
1094 onMasterVolumeChanged(mMasterVolume);
1095 }
1096 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001097 }
1098 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1099 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1100}
1101
1102ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1103 *_aidl_return = mMicMute;
1104 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1105 return ndk::ScopedAStatus::ok();
1106}
1107
1108ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1109 LOG(DEBUG) << __func__ << ": " << in_mute;
1110 mMicMute = in_mute;
1111 return ndk::ScopedAStatus::ok();
1112}
1113
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001114ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001115 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001116 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1117 return ndk::ScopedAStatus::ok();
1118}
1119
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001120ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001121 if (!isValidAudioMode(in_mode)) {
1122 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1123 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1124 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001125 // No checks for supported audio modes here, it's an informative notification.
1126 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1127 return ndk::ScopedAStatus::ok();
1128}
1129
1130ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1131 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1132 return ndk::ScopedAStatus::ok();
1133}
1134
1135ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1136 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1137 return ndk::ScopedAStatus::ok();
1138}
1139
Vlad Popa83a6d822022-11-07 13:53:57 +01001140ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001141 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001142 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001143 }
Mikhail Naganov7499a002023-02-27 18:51:44 -08001144 *_aidl_return = mSoundDose.getPtr();
Vlad Popa943b7e22022-12-08 14:24:12 +01001145 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001146 return ndk::ScopedAStatus::ok();
1147}
1148
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001149ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1150 LOG(DEBUG) << __func__;
1151 (void)_aidl_return;
1152 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1153}
1154
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001155const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001156const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001157
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001158ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1159 std::vector<VendorParameter>* _aidl_return) {
1160 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001161 bool allParametersKnown = true;
1162 for (const auto& id : in_ids) {
1163 if (id == VendorDebug::kForceTransientBurstName) {
1164 VendorParameter forceTransientBurst{.id = id};
1165 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1166 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001167 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1168 VendorParameter forceSynchronousDrain{.id = id};
1169 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1170 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001171 } else {
1172 allParametersKnown = false;
1173 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1174 }
1175 }
1176 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1177 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001178}
1179
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001180namespace {
1181
1182template <typename W>
1183bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1184 std::optional<W> value;
1185 binder_status_t result = p.ext.getParcelable(&value);
1186 if (result == STATUS_OK && value.has_value()) {
1187 *v = value.value().value;
1188 return true;
1189 }
1190 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1191 << "\": " << result;
1192 return false;
1193}
1194
1195} // namespace
1196
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001197ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1198 bool in_async) {
1199 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1200 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001201 bool allParametersKnown = true;
1202 for (const auto& p : in_parameters) {
1203 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001204 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1205 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1206 }
1207 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1208 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001209 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1210 }
1211 } else {
1212 allParametersKnown = false;
1213 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1214 }
1215 }
1216 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1217 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001218}
1219
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001220ndk::ScopedAStatus Module::addDeviceEffect(
1221 int32_t in_portConfigId,
1222 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1223 if (in_effect == nullptr) {
1224 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1225 } else {
1226 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1227 << in_effect->asBinder().get();
1228 }
1229 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1230}
1231
1232ndk::ScopedAStatus Module::removeDeviceEffect(
1233 int32_t in_portConfigId,
1234 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1235 if (in_effect == nullptr) {
1236 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1237 } else {
1238 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1239 << in_effect->asBinder().get();
1240 }
1241 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1242}
1243
jiabin9a8e6862023-01-12 23:06:37 +00001244ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1245 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1246 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1247 std::set<int32_t> mmapSinks;
1248 std::set<int32_t> mmapSources;
1249 auto& ports = getConfig().ports;
1250 for (const auto& port : ports) {
1251 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1252 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1253 AudioInputFlags::MMAP_NOIRQ)) {
1254 mmapSinks.insert(port.id);
1255 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1256 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1257 AudioOutputFlags::MMAP_NOIRQ)) {
1258 mmapSources.insert(port.id);
1259 }
1260 }
1261 for (const auto& route : getConfig().routes) {
1262 if (mmapSinks.count(route.sinkPortId) != 0) {
1263 // The sink is a mix port, add the sources if they are device ports.
1264 for (int sourcePortId : route.sourcePortIds) {
1265 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1266 if (sourcePortIt == ports.end()) {
1267 // This must not happen
1268 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1269 continue;
1270 }
1271 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1272 // The source is not a device port, skip
1273 continue;
1274 }
1275 AudioMMapPolicyInfo policyInfo;
1276 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1277 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1278 // default implementation.
1279 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1280 _aidl_return->push_back(policyInfo);
1281 }
1282 } else {
1283 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1284 if (sinkPortIt == ports.end()) {
1285 // This must not happen
1286 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1287 continue;
1288 }
1289 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1290 // The sink is not a device port, skip
1291 continue;
1292 }
1293 if (count_any(mmapSources, route.sourcePortIds)) {
1294 AudioMMapPolicyInfo policyInfo;
1295 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1296 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1297 // default implementation.
1298 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1299 _aidl_return->push_back(policyInfo);
1300 }
1301 }
1302 }
1303 return ndk::ScopedAStatus::ok();
1304}
1305
Eric Laurente2432ea2023-01-12 17:47:31 +01001306ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1307 LOG(DEBUG) << __func__;
1308 *_aidl_return = false;
1309 return ndk::ScopedAStatus::ok();
1310}
1311
jiabinb76981e2023-01-18 00:58:30 +00001312ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1313 if (!isMmapSupported()) {
1314 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1315 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1316 }
1317 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1318 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1319 return ndk::ScopedAStatus::ok();
1320}
1321
1322ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1323 if (!isMmapSupported()) {
1324 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1325 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1326 }
1327 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1328 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1329 return ndk::ScopedAStatus::ok();
1330}
1331
1332bool Module::isMmapSupported() {
1333 if (mIsMmapSupported.has_value()) {
1334 return mIsMmapSupported.value();
1335 }
1336 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1337 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1338 mIsMmapSupported = false;
1339 } else {
1340 mIsMmapSupported =
1341 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1342 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1343 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1344 }) != mmapPolicyInfos.end();
1345 }
1346 return mIsMmapSupported.value();
1347}
1348
jiabin253bd322023-01-25 23:57:31 +00001349ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001350 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001351 return ndk::ScopedAStatus::ok();
1352}
1353
1354ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1355 const std::vector<AudioPortConfig*>& sources __unused,
1356 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001357 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001358 return ndk::ScopedAStatus::ok();
1359}
1360
jiabin783c48b2023-02-28 18:28:06 +00001361void Module::onExternalDeviceConnectionChanged(
1362 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1363 bool connected __unused) {
1364 LOG(DEBUG) << __func__ << ": do nothing and return";
1365}
1366
1367ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1368 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1369 return ndk::ScopedAStatus::ok();
1370}
1371
1372ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1373 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1374 return ndk::ScopedAStatus::ok();
1375}
1376
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001377} // namespace aidl::android::hardware::audio::core