blob: 5625a4440079a9160779486a322f047291f827ee [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 Naganovb03b5c42023-07-26 13:13:35 -070029#include "core-impl/ModuleBluetooth.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070030#include "core-impl/ModulePrimary.h"
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053031#include "core-impl/ModuleRemoteSubmix.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070032#include "core-impl/ModuleStub.h"
jiabin253bd322023-01-25 23:57:31 +000033#include "core-impl/ModuleUsb.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010034#include "core-impl/SoundDose.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) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530113 case Type::DEFAULT:
Mikhail Naganov521fc492023-07-11 17:24:08 -0700114 return ndk::SharedRefBase::make<ModulePrimary>();
115 case Type::R_SUBMIX:
116 return ndk::SharedRefBase::make<ModuleRemoteSubmix>();
117 case Type::STUB:
118 return ndk::SharedRefBase::make<ModuleStub>();
119 case Type::USB:
120 return ndk::SharedRefBase::make<ModuleUsb>();
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700121 case Type::BLUETOOTH:
122 return ndk::SharedRefBase::make<ModuleBluetooth>();
jiabin253bd322023-01-25 23:57:31 +0000123 }
124}
125
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700126std::ostream& operator<<(std::ostream& os, Module::Type t) {
127 switch (t) {
128 case Module::Type::DEFAULT:
129 os << "default";
130 break;
131 case Module::Type::R_SUBMIX:
132 os << "r_submix";
133 break;
Mikhail Naganov521fc492023-07-11 17:24:08 -0700134 case Module::Type::STUB:
135 os << "stub";
136 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700137 case Module::Type::USB:
138 os << "usb";
139 break;
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700140 case Module::Type::BLUETOOTH:
141 os << "bluetooth";
142 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700143 }
144 return os;
145}
146
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000147void Module::cleanUpPatch(int32_t patchId) {
148 erase_all_values(mPatches, std::set<int32_t>{patchId});
149}
150
Mikhail Naganov8651b362023-01-06 23:15:27 +0000151ndk::ScopedAStatus Module::createStreamContext(
152 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
153 std::shared_ptr<IStreamCallback> asyncCallback,
154 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000155 if (in_bufferSizeFrames <= 0) {
156 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
157 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
158 }
159 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
160 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
161 << ", must be at least " << kMinimumStreamBufferSizeFrames;
162 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
163 }
164 auto& configs = getConfig().portConfigs;
165 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000166 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000167 // validity of the portConfigId has already been checked.
168 const size_t frameSize =
169 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
170 if (frameSize == 0) {
171 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
172 << portConfigIt->toString();
173 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
174 }
175 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700176 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000177 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
178 << " frames is too large, maximum size is "
179 << kMaximumStreamBufferSizeBytes / frameSize;
180 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
181 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000182 const auto& flags = portConfigIt->flags.value();
183 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000184 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
185 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000186 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000187 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
188 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000189 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
Mikhail Naganov194daaa2023-01-05 22:34:20 +0000190 mVendorDebug.forceTransientBurst,
191 mVendorDebug.forceSynchronousDrain};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000192 StreamContext temp(
193 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
194 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530195 portConfigIt->portId, portConfigIt->format.value(),
196 portConfigIt->channelMask.value(), portConfigIt->sampleRate.value().value, flags,
Mikhail Naganovb42a69e2023-06-16 12:38:25 -0700197 portConfigIt->ext.get<AudioPortExt::mix>().handle,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000198 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov8651b362023-01-06 23:15:27 +0000199 asyncCallback, outEventCallback, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000200 if (temp.isValid()) {
201 *out_context = std::move(temp);
202 } else {
203 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
204 }
205 } else {
206 // TODO: Implement simulation of MMAP buffer allocation
207 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000208 return ndk::ScopedAStatus::ok();
209}
210
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000211std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
212 std::vector<AudioDevice> result;
213 auto& ports = getConfig().ports;
214 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
215 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
216 auto portIt = findById<AudioPort>(ports, *it);
217 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
218 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
219 }
220 }
221 return result;
222}
223
224std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
225 std::set<int32_t> result;
226 auto patchIdsRange = mPatches.equal_range(portConfigId);
227 auto& patches = getConfig().patches;
228 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
229 auto patchIt = findById<AudioPatch>(patches, it->second);
230 if (patchIt == patches.end()) {
231 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
232 << "not found in the configuration";
233 }
234 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
235 portConfigId) != patchIt->sourcePortConfigIds.end()) {
236 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
237 } else {
238 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
239 }
240 }
241 return result;
242}
243
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000244ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
245 auto& configs = getConfig().portConfigs;
246 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
247 if (portConfigIt == configs.end()) {
248 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
249 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
250 }
251 const int32_t portId = portConfigIt->portId;
252 // In our implementation, configs of mix ports always have unique IDs.
253 CHECK(portId != in_portConfigId);
254 auto& ports = getConfig().ports;
255 auto portIt = findById<AudioPort>(ports, portId);
256 if (portIt == ports.end()) {
257 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
258 << in_portConfigId << " not found";
259 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
260 }
261 if (mStreams.count(in_portConfigId) != 0) {
262 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
263 << " already has a stream opened on it";
264 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
265 }
266 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
267 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
268 << " does not correspond to a mix port";
269 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
270 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700271 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000272 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
273 LOG(ERROR) << __func__ << ": port id " << portId
274 << " has already reached maximum allowed opened stream count: "
275 << maxOpenStreamCount;
276 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
277 }
278 *port = &(*portIt);
279 return ndk::ScopedAStatus::ok();
280}
281
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000282template <typename C>
283std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
284 std::set<int32_t> result;
285 auto& portConfigs = getConfig().portConfigs;
286 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
287 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
288 if (portConfigIt != portConfigs.end()) {
289 result.insert(portConfigIt->portId);
290 }
291 }
292 return result;
293}
294
Peter Yoon918a6a52023-07-13 17:04:37 +0900295std::unique_ptr<internal::Configuration> Module::initializeConfig() {
296 std::unique_ptr<internal::Configuration> config;
297 switch (getType()) {
298 case Type::DEFAULT:
299 config = std::move(internal::getPrimaryConfiguration());
300 break;
301 case Type::R_SUBMIX:
302 config = std::move(internal::getRSubmixConfiguration());
303 break;
304 case Type::STUB:
305 config = std::move(internal::getStubConfiguration());
306 break;
307 case Type::USB:
308 config = std::move(internal::getUsbConfiguration());
309 break;
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700310 case Type::BLUETOOTH:
311 config = std::move(internal::getBluetoothConfiguration());
312 break;
Peter Yoon918a6a52023-07-13 17:04:37 +0900313 }
314 return config;
315}
316
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000317internal::Configuration& Module::getConfig() {
318 if (!mConfig) {
Peter Yoon918a6a52023-07-13 17:04:37 +0900319 mConfig = std::move(initializeConfig());
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000320 }
321 return *mConfig;
322}
323
324void Module::registerPatch(const AudioPatch& patch) {
325 auto& configs = getConfig().portConfigs;
326 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
327 for (auto portConfigId : portConfigIds) {
328 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
329 if (configIt != configs.end()) {
330 mPatches.insert(std::pair{portConfigId, patch.id});
331 if (configIt->portId != portConfigId) {
332 mPatches.insert(std::pair{configIt->portId, patch.id});
333 }
334 }
335 };
336 };
337 do_insert(patch.sourcePortConfigIds);
338 do_insert(patch.sinkPortConfigIds);
339}
340
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700341ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
342 const AudioPatch& newPatch) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000343 // Streams from the old patch need to be disconnected, streams from the new
344 // patch need to be connected. If the stream belongs to both patches, no need
345 // to update it.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700346 auto maybeFailure = ndk::ScopedAStatus::ok();
347 std::set<int32_t> idsToDisconnect, idsToConnect, idsToDisconnectOnFailure;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000348 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
349 oldPatch.sourcePortConfigIds.end());
350 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
351 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
352 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
353 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700354 if (idsToConnect.count(portConfigId) == 0 && mStreams.count(portConfigId) != 0) {
355 if (auto status = mStreams.setStreamConnectedDevices(portConfigId, {}); status.isOk()) {
356 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
357 << portConfigId << " has been disconnected";
358 } else {
359 // Disconnection is tricky to roll back, just register a failure.
360 maybeFailure = std::move(status);
361 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000362 }
363 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700364 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000365 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700366 if (idsToDisconnect.count(portConfigId) == 0 && mStreams.count(portConfigId) != 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000367 const auto connectedDevices = findConnectedDevices(portConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700368 if (connectedDevices.empty()) {
369 // This is important as workers use the vector size to derive the connection status.
370 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
371 "config id "
372 << portConfigId;
373 }
374 if (auto status = mStreams.setStreamConnectedDevices(portConfigId, connectedDevices);
375 status.isOk()) {
376 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
377 << portConfigId << " has been connected to: "
378 << ::android::internal::ToString(connectedDevices);
379 } else {
380 maybeFailure = std::move(status);
381 idsToDisconnectOnFailure.insert(portConfigId);
382 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000383 }
384 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700385 if (!maybeFailure.isOk()) {
386 LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids "
387 << ::android::internal::ToString(idsToDisconnectOnFailure);
388 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
389 [&](const auto& portConfigId) {
390 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
391 (void)status.isOk(); // Can't do much about a failure here.
392 });
393 return maybeFailure;
394 }
395 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000396}
397
Mikhail Naganov00603d12022-05-02 22:52:13 +0000398ndk::ScopedAStatus Module::setModuleDebug(
399 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700400 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000401 << ", new flags: " << in_debug.toString();
402 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
403 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700404 LOG(ERROR) << __func__ << ": " << mType
405 << ": attempting to change device connections simulation while having external "
406 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000407 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
408 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000409 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700410 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000411 << in_debug.streamTransientStateDelayMs;
412 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
413 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000414 mDebug = in_debug;
415 return ndk::ScopedAStatus::ok();
416}
417
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000418ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700419 *_aidl_return = nullptr;
420 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000421 return ndk::ScopedAStatus::ok();
422}
423
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000424ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700425 *_aidl_return = nullptr;
426 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000427 return ndk::ScopedAStatus::ok();
428}
429
Mikhail Naganov7499a002023-02-27 18:51:44 -0800430ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700431 *_aidl_return = nullptr;
432 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800433 return ndk::ScopedAStatus::ok();
434}
435
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800436ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700437 *_aidl_return = nullptr;
438 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800439 return ndk::ScopedAStatus::ok();
440}
441
Mikhail Naganov00603d12022-05-02 22:52:13 +0000442ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
443 AudioPort* _aidl_return) {
444 const int32_t templateId = in_templateIdAndAdditionalData.id;
445 auto& ports = getConfig().ports;
446 AudioPort connectedPort;
447 { // Scope the template port so that we don't accidentally modify it.
448 auto templateIt = findById<AudioPort>(ports, templateId);
449 if (templateIt == ports.end()) {
450 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
451 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
452 }
453 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
454 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
455 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
456 }
457 if (!templateIt->profiles.empty()) {
458 LOG(ERROR) << __func__ << ": port id " << templateId
459 << " does not have dynamic profiles";
460 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
461 }
462 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
463 if (templateDevicePort.device.type.connection.empty()) {
464 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
465 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
466 }
467 // Postpone id allocation until we ensure that there are no client errors.
468 connectedPort = *templateIt;
469 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
470 const auto& inputDevicePort =
471 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
472 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
473 connectedDevicePort.device.address = inputDevicePort.device.address;
474 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
475 << connectedDevicePort.device.toString();
476 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700477 for (auto connectedPortPair : mConnectedDevicePorts) {
478 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000479 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
480 connectedDevicePort.device) {
481 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700482 << " is already connected at the device port id "
483 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000484 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
485 }
486 }
487 }
488
489 if (!mDebug.simulateDeviceConnections) {
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700490 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700491 } else {
492 auto& connectedProfiles = getConfig().connectedProfiles;
493 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
494 connectedProfilesIt != connectedProfiles.end()) {
495 connectedPort.profiles = connectedProfilesIt->second;
496 }
497 }
498 if (connectedPort.profiles.empty()) {
499 LOG(ERROR) << "Profiles of a connected port still empty after connecting external device "
500 << connectedPort.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000501 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
502 }
503
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530504 for (auto profile : connectedPort.profiles) {
505 if (profile.channelMasks.empty()) {
506 LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no channel masks";
507 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
508 }
509 if (profile.sampleRates.empty()) {
510 LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no sample rates";
511 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
512 }
513 }
514
Mikhail Naganov00603d12022-05-02 22:52:13 +0000515 connectedPort.id = ++getConfig().nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700516 auto [connectedPortsIt, _] =
517 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::vector<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000518 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
519 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000520 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000521 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000522
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700523 std::vector<int32_t> routablePortIds;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000524 std::vector<AudioRoute> newRoutes;
525 auto& routes = getConfig().routes;
526 for (auto& r : routes) {
527 if (r.sinkPortId == templateId) {
528 AudioRoute newRoute;
529 newRoute.sourcePortIds = r.sourcePortIds;
530 newRoute.sinkPortId = connectedPort.id;
531 newRoute.isExclusive = r.isExclusive;
532 newRoutes.push_back(std::move(newRoute));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700533 routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(),
534 r.sourcePortIds.end());
Mikhail Naganov00603d12022-05-02 22:52:13 +0000535 } else {
536 auto& srcs = r.sourcePortIds;
537 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
538 srcs.push_back(connectedPort.id);
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700539 routablePortIds.push_back(r.sinkPortId);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000540 }
541 }
542 }
543 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
544
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700545 // Note: this is a simplistic approach assuming that a mix port can only be populated
546 // from a single device port. Implementing support for stuffing dynamic profiles with a superset
547 // of all profiles from all routable dynamic device ports would be more involved.
548 for (const auto mixPortId : routablePortIds) {
549 auto portsIt = findById<AudioPort>(ports, mixPortId);
550 if (portsIt != ports.end() && portsIt->profiles.empty()) {
551 portsIt->profiles = connectedPort.profiles;
552 connectedPortsIt->second.push_back(portsIt->id);
553 }
554 }
555 *_aidl_return = std::move(connectedPort);
556
Mikhail Naganov00603d12022-05-02 22:52:13 +0000557 return ndk::ScopedAStatus::ok();
558}
559
560ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
561 auto& ports = getConfig().ports;
562 auto portIt = findById<AudioPort>(ports, in_portId);
563 if (portIt == ports.end()) {
564 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
565 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
566 }
567 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
568 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
569 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
570 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700571 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
572 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000573 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
574 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
575 }
576 auto& configs = getConfig().portConfigs;
577 auto& initials = getConfig().initialConfigs;
578 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
579 if (config.portId == in_portId) {
580 // Check if the configuration was provided by the client.
581 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
582 return initialIt == initials.end() || config != *initialIt;
583 }
584 return false;
585 });
586 if (configIt != configs.end()) {
587 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
588 << configIt->id;
589 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
590 }
jiabin783c48b2023-02-28 18:28:06 +0000591 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000592 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000593 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
594
595 auto& routes = getConfig().routes;
596 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
597 if (routesIt->sinkPortId == in_portId) {
598 routesIt = routes.erase(routesIt);
599 } else {
600 // Note: the list of sourcePortIds can't become empty because there must
601 // be the id of the template port in the route.
602 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
603 ++routesIt;
604 }
605 }
606
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700607 for (const auto mixPortId : connectedPortsIt->second) {
608 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
609 if (mixPortIt != ports.end()) {
610 mixPortIt->profiles = {};
611 }
612 }
613 mConnectedDevicePorts.erase(connectedPortsIt);
614
Mikhail Naganov00603d12022-05-02 22:52:13 +0000615 return ndk::ScopedAStatus::ok();
616}
617
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000618ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
619 *_aidl_return = getConfig().patches;
620 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
621 return ndk::ScopedAStatus::ok();
622}
623
624ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
625 auto& ports = getConfig().ports;
626 auto portIt = findById<AudioPort>(ports, in_portId);
627 if (portIt != ports.end()) {
628 *_aidl_return = *portIt;
629 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
630 return ndk::ScopedAStatus::ok();
631 }
632 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
633 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
634}
635
636ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
637 *_aidl_return = getConfig().portConfigs;
638 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
639 return ndk::ScopedAStatus::ok();
640}
641
642ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
643 *_aidl_return = getConfig().ports;
644 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
645 return ndk::ScopedAStatus::ok();
646}
647
648ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
649 *_aidl_return = getConfig().routes;
650 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
651 return ndk::ScopedAStatus::ok();
652}
653
Mikhail Naganov00603d12022-05-02 22:52:13 +0000654ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
655 std::vector<AudioRoute>* _aidl_return) {
656 auto& ports = getConfig().ports;
657 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
658 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
659 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
660 }
661 auto& routes = getConfig().routes;
662 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
663 [&](const auto& r) {
664 const auto& srcs = r.sourcePortIds;
665 return r.sinkPortId == in_portId ||
666 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
667 });
668 return ndk::ScopedAStatus::ok();
669}
670
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000671ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
672 OpenInputStreamReturn* _aidl_return) {
673 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
674 << in_args.bufferSizeFrames << " frames";
675 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700676 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000677 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
678 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000679 << " does not correspond to an input mix port";
680 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
681 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000682 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700683 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
684 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000685 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000686 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700687 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700688 mConfig->microphones, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000689 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700690 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
691 RETURN_STATUS_IF_ERROR(
692 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
693 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000694 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
695 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000696 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000697 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000698 return ndk::ScopedAStatus::ok();
699}
700
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000701ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
702 OpenOutputStreamReturn* _aidl_return) {
703 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
704 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
705 << " frames";
706 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700707 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000708 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
709 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000710 << " does not correspond to an output mix port";
711 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
712 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000713 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
714 AudioOutputFlags::COMPRESS_OFFLOAD);
715 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000716 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000717 << " has COMPRESS_OFFLOAD flag set, requires offload info";
718 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
719 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000720 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
721 AudioOutputFlags::NON_BLOCKING);
722 if (isNonBlocking && in_args.callback == nullptr) {
723 LOG(ERROR) << __func__ << ": port id " << port->id
724 << " has NON_BLOCKING flag set, requires async callback";
725 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
726 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000727 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700728 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
729 isNonBlocking ? in_args.callback : nullptr,
730 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000731 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000732 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700733 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700734 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000735 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700736 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
737 RETURN_STATUS_IF_ERROR(
738 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
739 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000740 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
741 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000742 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000743 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000744 return ndk::ScopedAStatus::ok();
745}
746
Mikhail Naganov74927202022-12-19 16:37:14 +0000747ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
748 SupportedPlaybackRateFactors* _aidl_return) {
749 LOG(DEBUG) << __func__;
750 (void)_aidl_return;
751 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
752}
753
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000754ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000755 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000756 if (in_requested.sourcePortConfigIds.empty()) {
757 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
758 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
759 }
760 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
761 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
762 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
763 }
764 if (in_requested.sinkPortConfigIds.empty()) {
765 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
766 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
767 }
768 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
769 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
770 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
771 }
772
773 auto& configs = getConfig().portConfigs;
774 std::vector<int32_t> missingIds;
775 auto sources =
776 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
777 if (!missingIds.empty()) {
778 LOG(ERROR) << __func__ << ": following source port config ids not found: "
779 << ::android::internal::ToString(missingIds);
780 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
781 }
782 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
783 if (!missingIds.empty()) {
784 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
785 << ::android::internal::ToString(missingIds);
786 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
787 }
788 // bool indicates whether a non-exclusive route is available.
789 // If only an exclusive route is available, that means the patch can not be
790 // established if there is any other patch which currently uses the sink port.
791 std::map<int32_t, bool> allowedSinkPorts;
792 auto& routes = getConfig().routes;
793 for (auto src : sources) {
794 for (const auto& r : routes) {
795 const auto& srcs = r.sourcePortIds;
796 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
797 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
798 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
799 }
800 }
801 }
802 }
803 for (auto sink : sinks) {
804 if (allowedSinkPorts.count(sink->portId) == 0) {
805 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
806 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
807 }
808 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700809 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000810
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000811 auto& patches = getConfig().patches;
812 auto existing = patches.end();
813 std::optional<decltype(mPatches)> patchesBackup;
814 if (in_requested.id != 0) {
815 existing = findById<AudioPatch>(patches, in_requested.id);
816 if (existing != patches.end()) {
817 patchesBackup = mPatches;
818 cleanUpPatch(existing->id);
819 } else {
820 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
821 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
822 }
823 }
824 // Validate the requested patch.
825 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
826 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
827 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
828 << "is exclusive and is already used by some other patch";
829 if (patchesBackup.has_value()) {
830 mPatches = std::move(*patchesBackup);
831 }
832 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
833 }
834 }
835 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000836 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
837 _aidl_return->latenciesMs.clear();
838 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
839 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000840 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000841 if (existing == patches.end()) {
842 _aidl_return->id = getConfig().nextPatchId++;
843 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000844 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000845 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000846 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700847 patchesBackup = mPatches;
848 registerPatch(*_aidl_return);
849 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
850 mPatches = std::move(*patchesBackup);
851 if (existing == patches.end()) {
852 patches.pop_back();
853 } else {
854 *existing = oldPatch;
855 }
856 return status;
857 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000858
859 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
860 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000861 return ndk::ScopedAStatus::ok();
862}
863
864ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
865 AudioPortConfig* out_suggested, bool* _aidl_return) {
866 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
867 auto& configs = getConfig().portConfigs;
868 auto existing = configs.end();
869 if (in_requested.id != 0) {
870 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
871 existing == configs.end()) {
872 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
873 << " not found";
874 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
875 }
876 }
877
878 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
879 if (portId == 0) {
880 LOG(ERROR) << __func__ << ": input port config does not specify portId";
881 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
882 }
883 auto& ports = getConfig().ports;
884 auto portIt = findById<AudioPort>(ports, portId);
885 if (portIt == ports.end()) {
886 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
887 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
888 }
889 if (existing != configs.end()) {
890 *out_suggested = *existing;
891 } else {
892 AudioPortConfig newConfig;
893 if (generateDefaultPortConfig(*portIt, &newConfig)) {
894 *out_suggested = newConfig;
895 } else {
896 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
897 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
898 }
899 }
900 // From this moment, 'out_suggested' is either an existing port config,
901 // or a new generated config. Now attempt to update it according to the specified
902 // fields of 'in_requested'.
903
904 bool requestedIsValid = true, requestedIsFullySpecified = true;
905
906 AudioIoFlags portFlags = portIt->flags;
907 if (in_requested.flags.has_value()) {
908 if (in_requested.flags.value() != portFlags) {
909 LOG(WARNING) << __func__ << ": requested flags "
910 << in_requested.flags.value().toString() << " do not match port's "
911 << portId << " flags " << portFlags.toString();
912 requestedIsValid = false;
913 }
914 } else {
915 requestedIsFullySpecified = false;
916 }
917
918 AudioProfile portProfile;
919 if (in_requested.format.has_value()) {
920 const auto& format = in_requested.format.value();
921 if (findAudioProfile(*portIt, format, &portProfile)) {
922 out_suggested->format = format;
923 } else {
924 LOG(WARNING) << __func__ << ": requested format " << format.toString()
925 << " is not found in port's " << portId << " profiles";
926 requestedIsValid = false;
927 }
928 } else {
929 requestedIsFullySpecified = false;
930 }
931 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
932 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
933 << out_suggested->format.value().toString() << " anymore";
934 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
935 }
936
937 if (in_requested.channelMask.has_value()) {
938 const auto& channelMask = in_requested.channelMask.value();
939 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
940 portProfile.channelMasks.end()) {
941 out_suggested->channelMask = channelMask;
942 } else {
943 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
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.sampleRate.has_value()) {
953 const auto& sampleRate = in_requested.sampleRate.value();
954 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
955 sampleRate.value) != portProfile.sampleRates.end()) {
956 out_suggested->sampleRate = sampleRate;
957 } else {
958 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
959 << " is not supported for the format " << portProfile.format.toString()
960 << " by the port " << portId;
961 requestedIsValid = false;
962 }
963 } else {
964 requestedIsFullySpecified = false;
965 }
966
967 if (in_requested.gain.has_value()) {
968 // Let's pretend that gain can always be applied.
969 out_suggested->gain = in_requested.gain.value();
970 }
971
Mikhail Naganov248e9502023-02-21 16:32:40 -0800972 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
973 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
974 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
975 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
976 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
977 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
978 }
979 } else {
980 LOG(WARNING) << __func__ << ": requested ext tag "
981 << toString(in_requested.ext.getTag()) << " do not match port's tag "
982 << toString(out_suggested->ext.getTag());
983 requestedIsValid = false;
984 }
985 }
986
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000987 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
988 out_suggested->id = getConfig().nextPortId++;
989 configs.push_back(*out_suggested);
990 *_aidl_return = true;
991 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
992 } else if (existing != configs.end() && requestedIsValid) {
993 *existing = *out_suggested;
994 *_aidl_return = true;
995 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
996 } else {
997 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
998 << "; requested is valid? " << requestedIsValid << ", fully specified? "
999 << requestedIsFullySpecified;
1000 *_aidl_return = false;
1001 }
1002 return ndk::ScopedAStatus::ok();
1003}
1004
1005ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1006 auto& patches = getConfig().patches;
1007 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1008 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001009 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001010 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001011 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1012 mPatches = std::move(patchesBackup);
1013 return status;
1014 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001015 patches.erase(patchIt);
1016 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1017 return ndk::ScopedAStatus::ok();
1018 }
1019 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1020 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1021}
1022
1023ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1024 auto& configs = getConfig().portConfigs;
1025 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1026 if (configIt != configs.end()) {
1027 if (mStreams.count(in_portConfigId) != 0) {
1028 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1029 << " has a stream opened on it";
1030 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1031 }
1032 auto patchIt = mPatches.find(in_portConfigId);
1033 if (patchIt != mPatches.end()) {
1034 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1035 << " is used by the patch with id " << patchIt->second;
1036 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1037 }
1038 auto& initials = getConfig().initialConfigs;
1039 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1040 if (initialIt == initials.end()) {
1041 configs.erase(configIt);
1042 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1043 } else if (*configIt != *initialIt) {
1044 *configIt = *initialIt;
1045 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1046 }
1047 return ndk::ScopedAStatus::ok();
1048 }
1049 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1050 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1051}
1052
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001053ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1054 *_aidl_return = mMasterMute;
1055 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1056 return ndk::ScopedAStatus::ok();
1057}
1058
1059ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1060 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001061 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1062 : onMasterMuteChanged(in_mute);
1063 if (result.isOk()) {
1064 mMasterMute = in_mute;
1065 } else {
1066 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1067 << "), error=" << result;
1068 // Reset master mute if it failed.
1069 onMasterMuteChanged(mMasterMute);
1070 }
1071 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001072}
1073
1074ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1075 *_aidl_return = mMasterVolume;
1076 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1077 return ndk::ScopedAStatus::ok();
1078}
1079
1080ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1081 LOG(DEBUG) << __func__ << ": " << in_volume;
1082 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001083 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1084 : onMasterVolumeChanged(in_volume);
1085 if (result.isOk()) {
1086 mMasterVolume = in_volume;
1087 } else {
1088 // Reset master volume if it failed.
1089 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1090 << "), error=" << result;
1091 onMasterVolumeChanged(mMasterVolume);
1092 }
1093 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001094 }
1095 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1096 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1097}
1098
1099ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1100 *_aidl_return = mMicMute;
1101 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1102 return ndk::ScopedAStatus::ok();
1103}
1104
1105ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1106 LOG(DEBUG) << __func__ << ": " << in_mute;
1107 mMicMute = in_mute;
1108 return ndk::ScopedAStatus::ok();
1109}
1110
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001111ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001112 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001113 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1114 return ndk::ScopedAStatus::ok();
1115}
1116
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001117ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001118 if (!isValidAudioMode(in_mode)) {
1119 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1120 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1121 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001122 // No checks for supported audio modes here, it's an informative notification.
1123 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1124 return ndk::ScopedAStatus::ok();
1125}
1126
1127ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1128 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1129 return ndk::ScopedAStatus::ok();
1130}
1131
1132ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1133 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1134 return ndk::ScopedAStatus::ok();
1135}
1136
Vlad Popa83a6d822022-11-07 13:53:57 +01001137ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001138 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001139 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001140 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001141 *_aidl_return = mSoundDose.getInstance();
Vlad Popa943b7e22022-12-08 14:24:12 +01001142 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001143 return ndk::ScopedAStatus::ok();
1144}
1145
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001146ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1147 LOG(DEBUG) << __func__;
1148 (void)_aidl_return;
1149 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1150}
1151
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001152const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001153const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001154
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001155ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1156 std::vector<VendorParameter>* _aidl_return) {
1157 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001158 bool allParametersKnown = true;
1159 for (const auto& id : in_ids) {
1160 if (id == VendorDebug::kForceTransientBurstName) {
1161 VendorParameter forceTransientBurst{.id = id};
1162 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1163 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001164 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1165 VendorParameter forceSynchronousDrain{.id = id};
1166 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1167 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001168 } else {
1169 allParametersKnown = false;
1170 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1171 }
1172 }
1173 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1174 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001175}
1176
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001177namespace {
1178
1179template <typename W>
1180bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1181 std::optional<W> value;
1182 binder_status_t result = p.ext.getParcelable(&value);
1183 if (result == STATUS_OK && value.has_value()) {
1184 *v = value.value().value;
1185 return true;
1186 }
1187 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1188 << "\": " << result;
1189 return false;
1190}
1191
1192} // namespace
1193
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001194ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1195 bool in_async) {
1196 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1197 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001198 bool allParametersKnown = true;
1199 for (const auto& p : in_parameters) {
1200 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001201 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1202 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1203 }
1204 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1205 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001206 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1207 }
1208 } else {
1209 allParametersKnown = false;
1210 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1211 }
1212 }
1213 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1214 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001215}
1216
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001217ndk::ScopedAStatus Module::addDeviceEffect(
1218 int32_t in_portConfigId,
1219 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1220 if (in_effect == nullptr) {
1221 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1222 } else {
1223 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1224 << in_effect->asBinder().get();
1225 }
1226 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1227}
1228
1229ndk::ScopedAStatus Module::removeDeviceEffect(
1230 int32_t in_portConfigId,
1231 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1232 if (in_effect == nullptr) {
1233 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1234 } else {
1235 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1236 << in_effect->asBinder().get();
1237 }
1238 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1239}
1240
jiabin9a8e6862023-01-12 23:06:37 +00001241ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1242 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1243 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1244 std::set<int32_t> mmapSinks;
1245 std::set<int32_t> mmapSources;
1246 auto& ports = getConfig().ports;
1247 for (const auto& port : ports) {
1248 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1249 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1250 AudioInputFlags::MMAP_NOIRQ)) {
1251 mmapSinks.insert(port.id);
1252 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1253 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1254 AudioOutputFlags::MMAP_NOIRQ)) {
1255 mmapSources.insert(port.id);
1256 }
1257 }
1258 for (const auto& route : getConfig().routes) {
1259 if (mmapSinks.count(route.sinkPortId) != 0) {
1260 // The sink is a mix port, add the sources if they are device ports.
1261 for (int sourcePortId : route.sourcePortIds) {
1262 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1263 if (sourcePortIt == ports.end()) {
1264 // This must not happen
1265 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1266 continue;
1267 }
1268 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1269 // The source is not a device port, skip
1270 continue;
1271 }
1272 AudioMMapPolicyInfo policyInfo;
1273 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1274 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1275 // default implementation.
1276 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1277 _aidl_return->push_back(policyInfo);
1278 }
1279 } else {
1280 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1281 if (sinkPortIt == ports.end()) {
1282 // This must not happen
1283 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1284 continue;
1285 }
1286 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1287 // The sink is not a device port, skip
1288 continue;
1289 }
1290 if (count_any(mmapSources, route.sourcePortIds)) {
1291 AudioMMapPolicyInfo policyInfo;
1292 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1293 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1294 // default implementation.
1295 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1296 _aidl_return->push_back(policyInfo);
1297 }
1298 }
1299 }
1300 return ndk::ScopedAStatus::ok();
1301}
1302
Eric Laurente2432ea2023-01-12 17:47:31 +01001303ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1304 LOG(DEBUG) << __func__;
1305 *_aidl_return = false;
1306 return ndk::ScopedAStatus::ok();
1307}
1308
jiabinb76981e2023-01-18 00:58:30 +00001309ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1310 if (!isMmapSupported()) {
1311 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1312 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1313 }
1314 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1315 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1316 return ndk::ScopedAStatus::ok();
1317}
1318
1319ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1320 if (!isMmapSupported()) {
1321 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1322 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1323 }
1324 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1325 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1326 return ndk::ScopedAStatus::ok();
1327}
1328
1329bool Module::isMmapSupported() {
1330 if (mIsMmapSupported.has_value()) {
1331 return mIsMmapSupported.value();
1332 }
1333 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1334 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1335 mIsMmapSupported = false;
1336 } else {
1337 mIsMmapSupported =
1338 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1339 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1340 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1341 }) != mmapPolicyInfos.end();
1342 }
1343 return mIsMmapSupported.value();
1344}
1345
jiabin253bd322023-01-25 23:57:31 +00001346ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001347 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001348 return ndk::ScopedAStatus::ok();
1349}
1350
1351ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1352 const std::vector<AudioPortConfig*>& sources __unused,
1353 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001354 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001355 return ndk::ScopedAStatus::ok();
1356}
1357
jiabin783c48b2023-02-28 18:28:06 +00001358void Module::onExternalDeviceConnectionChanged(
1359 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1360 bool connected __unused) {
1361 LOG(DEBUG) << __func__ << ": do nothing and return";
1362}
1363
1364ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1365 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1366 return ndk::ScopedAStatus::ok();
1367}
1368
1369ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1370 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1371 return ndk::ScopedAStatus::ok();
1372}
1373
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001374} // namespace aidl::android::hardware::audio::core