blob: 1e561d4ec79955a11c1a408ddee640d8982ee3c2 [file] [log] [blame]
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <algorithm>
18#include <set>
19
20#define LOG_TAG "AHAL_Module"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000021#include <android-base/logging.h>
22
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000023#include <Utils.h>
24#include <aidl/android/media/audio/common/AudioInputFlags.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000025#include <aidl/android/media/audio/common/AudioOutputFlags.h>
26
27#include "core-impl/Module.h"
Mikhail Naganov3b125b72022-10-05 02:12:39 +000028#include "core-impl/Telephony.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000029#include "core-impl/utils.h"
30
31using aidl::android::hardware::audio::common::SinkMetadata;
32using aidl::android::hardware::audio::common::SourceMetadata;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000033using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000034using aidl::android::media::audio::common::AudioDevice;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000035using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000036using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000037using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000038using aidl::android::media::audio::common::AudioIoFlags;
39using aidl::android::media::audio::common::AudioOffloadInfo;
40using aidl::android::media::audio::common::AudioOutputFlags;
41using aidl::android::media::audio::common::AudioPort;
42using aidl::android::media::audio::common::AudioPortConfig;
43using aidl::android::media::audio::common::AudioPortExt;
44using aidl::android::media::audio::common::AudioProfile;
45using aidl::android::media::audio::common::Int;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000046using aidl::android::media::audio::common::PcmType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000047using android::hardware::audio::common::getFrameSizeInBytes;
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +000048using android::hardware::audio::common::isBitPositionFlagSet;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000049
50namespace aidl::android::hardware::audio::core {
51
52namespace {
53
54bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
55 *config = {};
56 config->portId = port.id;
57 if (port.profiles.empty()) {
58 LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles";
59 return false;
60 }
61 const auto& profile = port.profiles.begin();
62 config->format = profile->format;
63 if (profile->channelMasks.empty()) {
64 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
65 << " has no channel masks";
66 return false;
67 }
68 config->channelMask = *profile->channelMasks.begin();
69 if (profile->sampleRates.empty()) {
70 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
71 << " has no sample rates";
72 return false;
73 }
74 Int sampleRate;
75 sampleRate.value = *profile->sampleRates.begin();
76 config->sampleRate = sampleRate;
77 config->flags = port.flags;
78 config->ext = port.ext;
79 return true;
80}
81
82bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
83 AudioProfile* profile) {
84 if (auto profilesIt =
85 find_if(port.profiles.begin(), port.profiles.end(),
86 [&format](const auto& profile) { return profile.format == format; });
87 profilesIt != port.profiles.end()) {
88 *profile = *profilesIt;
89 return true;
90 }
91 return false;
92}
Mikhail Naganov00603d12022-05-02 22:52:13 +000093
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000094} // namespace
95
96void Module::cleanUpPatch(int32_t patchId) {
97 erase_all_values(mPatches, std::set<int32_t>{patchId});
98}
99
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000100ndk::ScopedAStatus Module::createStreamContext(int32_t in_portConfigId, int64_t in_bufferSizeFrames,
Mikhail Naganov30301a42022-09-13 01:20:45 +0000101 std::shared_ptr<IStreamCallback> asyncCallback,
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000102 StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000103 if (in_bufferSizeFrames <= 0) {
104 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
105 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
106 }
107 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
108 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
109 << ", must be at least " << kMinimumStreamBufferSizeFrames;
110 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
111 }
112 auto& configs = getConfig().portConfigs;
113 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000114 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000115 // validity of the portConfigId has already been checked.
116 const size_t frameSize =
117 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
118 if (frameSize == 0) {
119 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
120 << portConfigIt->toString();
121 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
122 }
123 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
124 if (frameSize > kMaximumStreamBufferSizeBytes / in_bufferSizeFrames) {
125 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
126 << " frames is too large, maximum size is "
127 << kMaximumStreamBufferSizeBytes / frameSize;
128 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
129 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000130 const auto& flags = portConfigIt->flags.value();
131 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000132 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
133 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000134 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000135 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
136 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000137 StreamContext temp(
138 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
139 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000140 portConfigIt->format.value(), portConfigIt->channelMask.value(),
141 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov30301a42022-09-13 01:20:45 +0000142 asyncCallback, mDebug.streamTransientStateDelayMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000143 if (temp.isValid()) {
144 *out_context = std::move(temp);
145 } else {
146 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
147 }
148 } else {
149 // TODO: Implement simulation of MMAP buffer allocation
150 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000151 return ndk::ScopedAStatus::ok();
152}
153
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000154std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
155 std::vector<AudioDevice> result;
156 auto& ports = getConfig().ports;
157 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
158 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
159 auto portIt = findById<AudioPort>(ports, *it);
160 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
161 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
162 }
163 }
164 return result;
165}
166
167std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
168 std::set<int32_t> result;
169 auto patchIdsRange = mPatches.equal_range(portConfigId);
170 auto& patches = getConfig().patches;
171 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
172 auto patchIt = findById<AudioPatch>(patches, it->second);
173 if (patchIt == patches.end()) {
174 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
175 << "not found in the configuration";
176 }
177 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
178 portConfigId) != patchIt->sourcePortConfigIds.end()) {
179 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
180 } else {
181 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
182 }
183 }
184 return result;
185}
186
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000187ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
188 auto& configs = getConfig().portConfigs;
189 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
190 if (portConfigIt == configs.end()) {
191 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
192 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
193 }
194 const int32_t portId = portConfigIt->portId;
195 // In our implementation, configs of mix ports always have unique IDs.
196 CHECK(portId != in_portConfigId);
197 auto& ports = getConfig().ports;
198 auto portIt = findById<AudioPort>(ports, portId);
199 if (portIt == ports.end()) {
200 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
201 << in_portConfigId << " not found";
202 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
203 }
204 if (mStreams.count(in_portConfigId) != 0) {
205 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
206 << " already has a stream opened on it";
207 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
208 }
209 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
210 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
211 << " does not correspond to a mix port";
212 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
213 }
214 const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
215 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
216 LOG(ERROR) << __func__ << ": port id " << portId
217 << " has already reached maximum allowed opened stream count: "
218 << maxOpenStreamCount;
219 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
220 }
221 *port = &(*portIt);
222 return ndk::ScopedAStatus::ok();
223}
224
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000225template <typename C>
226std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
227 std::set<int32_t> result;
228 auto& portConfigs = getConfig().portConfigs;
229 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
230 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
231 if (portConfigIt != portConfigs.end()) {
232 result.insert(portConfigIt->portId);
233 }
234 }
235 return result;
236}
237
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000238internal::Configuration& Module::getConfig() {
239 if (!mConfig) {
240 mConfig.reset(new internal::Configuration(internal::getNullPrimaryConfiguration()));
241 }
242 return *mConfig;
243}
244
245void Module::registerPatch(const AudioPatch& patch) {
246 auto& configs = getConfig().portConfigs;
247 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
248 for (auto portConfigId : portConfigIds) {
249 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
250 if (configIt != configs.end()) {
251 mPatches.insert(std::pair{portConfigId, patch.id});
252 if (configIt->portId != portConfigId) {
253 mPatches.insert(std::pair{configIt->portId, patch.id});
254 }
255 }
256 };
257 };
258 do_insert(patch.sourcePortConfigIds);
259 do_insert(patch.sinkPortConfigIds);
260}
261
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000262void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) {
263 // Streams from the old patch need to be disconnected, streams from the new
264 // patch need to be connected. If the stream belongs to both patches, no need
265 // to update it.
266 std::set<int32_t> idsToDisconnect, idsToConnect;
267 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
268 oldPatch.sourcePortConfigIds.end());
269 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
270 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
271 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
272 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
273 if (idsToConnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000274 LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected";
275 mStreams.setStreamIsConnected(portConfigId, {});
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000276 }
277 });
278 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
279 if (idsToDisconnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000280 const auto connectedDevices = findConnectedDevices(portConfigId);
281 LOG(DEBUG) << "The stream on port config id " << portConfigId
282 << " is connected to: " << ::android::internal::ToString(connectedDevices);
283 mStreams.setStreamIsConnected(portConfigId, connectedDevices);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000284 }
285 });
286}
287
Mikhail Naganov00603d12022-05-02 22:52:13 +0000288ndk::ScopedAStatus Module::setModuleDebug(
289 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
290 LOG(DEBUG) << __func__ << ": old flags:" << mDebug.toString()
291 << ", new flags: " << in_debug.toString();
292 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
293 !mConnectedDevicePorts.empty()) {
294 LOG(ERROR) << __func__ << ": attempting to change device connections simulation "
295 << "while having external devices connected";
296 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
297 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000298 if (in_debug.streamTransientStateDelayMs < 0) {
299 LOG(ERROR) << __func__ << ": streamTransientStateDelayMs is negative: "
300 << in_debug.streamTransientStateDelayMs;
301 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
302 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000303 mDebug = in_debug;
304 return ndk::ScopedAStatus::ok();
305}
306
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000307ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
308 if (mTelephony == nullptr) {
309 mTelephony = ndk::SharedRefBase::make<Telephony>();
310 }
311 *_aidl_return = mTelephony;
312 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
313 return ndk::ScopedAStatus::ok();
314}
315
Mikhail Naganov00603d12022-05-02 22:52:13 +0000316ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
317 AudioPort* _aidl_return) {
318 const int32_t templateId = in_templateIdAndAdditionalData.id;
319 auto& ports = getConfig().ports;
320 AudioPort connectedPort;
321 { // Scope the template port so that we don't accidentally modify it.
322 auto templateIt = findById<AudioPort>(ports, templateId);
323 if (templateIt == ports.end()) {
324 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
325 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
326 }
327 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
328 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
329 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
330 }
331 if (!templateIt->profiles.empty()) {
332 LOG(ERROR) << __func__ << ": port id " << templateId
333 << " does not have dynamic profiles";
334 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
335 }
336 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
337 if (templateDevicePort.device.type.connection.empty()) {
338 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
339 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
340 }
341 // Postpone id allocation until we ensure that there are no client errors.
342 connectedPort = *templateIt;
343 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
344 const auto& inputDevicePort =
345 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
346 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
347 connectedDevicePort.device.address = inputDevicePort.device.address;
348 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
349 << connectedDevicePort.device.toString();
350 // Check if there is already a connected port with for the same external device.
351 for (auto connectedPortId : mConnectedDevicePorts) {
352 auto connectedPortIt = findById<AudioPort>(ports, connectedPortId);
353 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
354 connectedDevicePort.device) {
355 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
356 << " is already connected at the device port id " << connectedPortId;
357 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
358 }
359 }
360 }
361
362 if (!mDebug.simulateDeviceConnections) {
363 // In a real HAL here we would attempt querying the profiles from the device.
364 LOG(ERROR) << __func__ << ": failed to query supported device profiles";
365 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
366 }
367
368 connectedPort.id = ++getConfig().nextPortId;
369 mConnectedDevicePorts.insert(connectedPort.id);
370 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
371 << "connected port ID " << connectedPort.id;
372 auto& connectedProfiles = getConfig().connectedProfiles;
373 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
374 connectedProfilesIt != connectedProfiles.end()) {
375 connectedPort.profiles = connectedProfilesIt->second;
376 }
377 ports.push_back(connectedPort);
378 *_aidl_return = std::move(connectedPort);
379
380 std::vector<AudioRoute> newRoutes;
381 auto& routes = getConfig().routes;
382 for (auto& r : routes) {
383 if (r.sinkPortId == templateId) {
384 AudioRoute newRoute;
385 newRoute.sourcePortIds = r.sourcePortIds;
386 newRoute.sinkPortId = connectedPort.id;
387 newRoute.isExclusive = r.isExclusive;
388 newRoutes.push_back(std::move(newRoute));
389 } else {
390 auto& srcs = r.sourcePortIds;
391 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
392 srcs.push_back(connectedPort.id);
393 }
394 }
395 }
396 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
397
398 return ndk::ScopedAStatus::ok();
399}
400
401ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
402 auto& ports = getConfig().ports;
403 auto portIt = findById<AudioPort>(ports, in_portId);
404 if (portIt == ports.end()) {
405 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
406 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
407 }
408 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
409 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
410 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
411 }
412 if (mConnectedDevicePorts.count(in_portId) == 0) {
413 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
414 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
415 }
416 auto& configs = getConfig().portConfigs;
417 auto& initials = getConfig().initialConfigs;
418 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
419 if (config.portId == in_portId) {
420 // Check if the configuration was provided by the client.
421 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
422 return initialIt == initials.end() || config != *initialIt;
423 }
424 return false;
425 });
426 if (configIt != configs.end()) {
427 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
428 << configIt->id;
429 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
430 }
431 ports.erase(portIt);
432 mConnectedDevicePorts.erase(in_portId);
433 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
434
435 auto& routes = getConfig().routes;
436 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
437 if (routesIt->sinkPortId == in_portId) {
438 routesIt = routes.erase(routesIt);
439 } else {
440 // Note: the list of sourcePortIds can't become empty because there must
441 // be the id of the template port in the route.
442 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
443 ++routesIt;
444 }
445 }
446
447 return ndk::ScopedAStatus::ok();
448}
449
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000450ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
451 *_aidl_return = getConfig().patches;
452 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
453 return ndk::ScopedAStatus::ok();
454}
455
456ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
457 auto& ports = getConfig().ports;
458 auto portIt = findById<AudioPort>(ports, in_portId);
459 if (portIt != ports.end()) {
460 *_aidl_return = *portIt;
461 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
462 return ndk::ScopedAStatus::ok();
463 }
464 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
465 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
466}
467
468ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
469 *_aidl_return = getConfig().portConfigs;
470 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
471 return ndk::ScopedAStatus::ok();
472}
473
474ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
475 *_aidl_return = getConfig().ports;
476 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
477 return ndk::ScopedAStatus::ok();
478}
479
480ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
481 *_aidl_return = getConfig().routes;
482 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
483 return ndk::ScopedAStatus::ok();
484}
485
Mikhail Naganov00603d12022-05-02 22:52:13 +0000486ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
487 std::vector<AudioRoute>* _aidl_return) {
488 auto& ports = getConfig().ports;
489 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
490 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
491 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
492 }
493 auto& routes = getConfig().routes;
494 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
495 [&](const auto& r) {
496 const auto& srcs = r.sourcePortIds;
497 return r.sinkPortId == in_portId ||
498 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
499 });
500 return ndk::ScopedAStatus::ok();
501}
502
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000503ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
504 OpenInputStreamReturn* _aidl_return) {
505 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
506 << in_args.bufferSizeFrames << " frames";
507 AudioPort* port = nullptr;
508 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
509 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000510 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000511 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
512 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000513 << " does not correspond to an input mix port";
514 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
515 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000516 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000517 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
518 &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000519 !status.isOk()) {
520 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000521 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000522 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000523 auto stream = ndk::SharedRefBase::make<StreamIn>(in_args.sinkMetadata, std::move(context),
524 mConfig->microphones);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000525 if (auto status = stream->init(); !status.isOk()) {
526 return status;
527 }
528 StreamWrapper streamWrapper(stream);
529 auto patchIt = mPatches.find(in_args.portConfigId);
530 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000531 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000532 }
533 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000534 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000535 return ndk::ScopedAStatus::ok();
536}
537
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000538ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
539 OpenOutputStreamReturn* _aidl_return) {
540 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
541 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
542 << " frames";
543 AudioPort* port = nullptr;
544 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
545 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000546 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000547 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
548 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000549 << " does not correspond to an output mix port";
550 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
551 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000552 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
553 AudioOutputFlags::COMPRESS_OFFLOAD);
554 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000555 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000556 << " has COMPRESS_OFFLOAD flag set, requires offload info";
557 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
558 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000559 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
560 AudioOutputFlags::NON_BLOCKING);
561 if (isNonBlocking && in_args.callback == nullptr) {
562 LOG(ERROR) << __func__ << ": port id " << port->id
563 << " has NON_BLOCKING flag set, requires async callback";
564 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
565 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000566 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000567 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
568 isNonBlocking ? in_args.callback : nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000569 !status.isOk()) {
570 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000571 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000572 context.fillDescriptor(&_aidl_return->desc);
573 auto stream = ndk::SharedRefBase::make<StreamOut>(in_args.sourceMetadata, std::move(context),
574 in_args.offloadInfo);
575 if (auto status = stream->init(); !status.isOk()) {
576 return status;
577 }
578 StreamWrapper streamWrapper(stream);
579 auto patchIt = mPatches.find(in_args.portConfigId);
580 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000581 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000582 }
583 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000584 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000585 return ndk::ScopedAStatus::ok();
586}
587
588ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000589 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000590 if (in_requested.sourcePortConfigIds.empty()) {
591 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
592 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
593 }
594 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
595 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
596 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
597 }
598 if (in_requested.sinkPortConfigIds.empty()) {
599 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
600 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
601 }
602 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
603 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
604 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
605 }
606
607 auto& configs = getConfig().portConfigs;
608 std::vector<int32_t> missingIds;
609 auto sources =
610 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
611 if (!missingIds.empty()) {
612 LOG(ERROR) << __func__ << ": following source port config ids not found: "
613 << ::android::internal::ToString(missingIds);
614 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
615 }
616 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
617 if (!missingIds.empty()) {
618 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
619 << ::android::internal::ToString(missingIds);
620 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
621 }
622 // bool indicates whether a non-exclusive route is available.
623 // If only an exclusive route is available, that means the patch can not be
624 // established if there is any other patch which currently uses the sink port.
625 std::map<int32_t, bool> allowedSinkPorts;
626 auto& routes = getConfig().routes;
627 for (auto src : sources) {
628 for (const auto& r : routes) {
629 const auto& srcs = r.sourcePortIds;
630 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
631 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
632 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
633 }
634 }
635 }
636 }
637 for (auto sink : sinks) {
638 if (allowedSinkPorts.count(sink->portId) == 0) {
639 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
640 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
641 }
642 }
643
644 auto& patches = getConfig().patches;
645 auto existing = patches.end();
646 std::optional<decltype(mPatches)> patchesBackup;
647 if (in_requested.id != 0) {
648 existing = findById<AudioPatch>(patches, in_requested.id);
649 if (existing != patches.end()) {
650 patchesBackup = mPatches;
651 cleanUpPatch(existing->id);
652 } else {
653 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
654 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
655 }
656 }
657 // Validate the requested patch.
658 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
659 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
660 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
661 << "is exclusive and is already used by some other patch";
662 if (patchesBackup.has_value()) {
663 mPatches = std::move(*patchesBackup);
664 }
665 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
666 }
667 }
668 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000669 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
670 _aidl_return->latenciesMs.clear();
671 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
672 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000673 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000674 if (existing == patches.end()) {
675 _aidl_return->id = getConfig().nextPatchId++;
676 patches.push_back(*_aidl_return);
677 existing = patches.begin() + (patches.size() - 1);
678 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000679 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000680 *existing = *_aidl_return;
681 }
682 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000683 updateStreamsConnectedState(oldPatch, *_aidl_return);
684
685 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
686 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000687 return ndk::ScopedAStatus::ok();
688}
689
690ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
691 AudioPortConfig* out_suggested, bool* _aidl_return) {
692 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
693 auto& configs = getConfig().portConfigs;
694 auto existing = configs.end();
695 if (in_requested.id != 0) {
696 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
697 existing == configs.end()) {
698 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
699 << " not found";
700 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
701 }
702 }
703
704 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
705 if (portId == 0) {
706 LOG(ERROR) << __func__ << ": input port config does not specify portId";
707 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
708 }
709 auto& ports = getConfig().ports;
710 auto portIt = findById<AudioPort>(ports, portId);
711 if (portIt == ports.end()) {
712 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
713 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
714 }
715 if (existing != configs.end()) {
716 *out_suggested = *existing;
717 } else {
718 AudioPortConfig newConfig;
719 if (generateDefaultPortConfig(*portIt, &newConfig)) {
720 *out_suggested = newConfig;
721 } else {
722 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
723 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
724 }
725 }
726 // From this moment, 'out_suggested' is either an existing port config,
727 // or a new generated config. Now attempt to update it according to the specified
728 // fields of 'in_requested'.
729
730 bool requestedIsValid = true, requestedIsFullySpecified = true;
731
732 AudioIoFlags portFlags = portIt->flags;
733 if (in_requested.flags.has_value()) {
734 if (in_requested.flags.value() != portFlags) {
735 LOG(WARNING) << __func__ << ": requested flags "
736 << in_requested.flags.value().toString() << " do not match port's "
737 << portId << " flags " << portFlags.toString();
738 requestedIsValid = false;
739 }
740 } else {
741 requestedIsFullySpecified = false;
742 }
743
744 AudioProfile portProfile;
745 if (in_requested.format.has_value()) {
746 const auto& format = in_requested.format.value();
747 if (findAudioProfile(*portIt, format, &portProfile)) {
748 out_suggested->format = format;
749 } else {
750 LOG(WARNING) << __func__ << ": requested format " << format.toString()
751 << " is not found in port's " << portId << " profiles";
752 requestedIsValid = false;
753 }
754 } else {
755 requestedIsFullySpecified = false;
756 }
757 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
758 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
759 << out_suggested->format.value().toString() << " anymore";
760 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
761 }
762
763 if (in_requested.channelMask.has_value()) {
764 const auto& channelMask = in_requested.channelMask.value();
765 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
766 portProfile.channelMasks.end()) {
767 out_suggested->channelMask = channelMask;
768 } else {
769 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
770 << " is not supported for the format " << portProfile.format.toString()
771 << " by the port " << portId;
772 requestedIsValid = false;
773 }
774 } else {
775 requestedIsFullySpecified = false;
776 }
777
778 if (in_requested.sampleRate.has_value()) {
779 const auto& sampleRate = in_requested.sampleRate.value();
780 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
781 sampleRate.value) != portProfile.sampleRates.end()) {
782 out_suggested->sampleRate = sampleRate;
783 } else {
784 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
785 << " is not supported for the format " << portProfile.format.toString()
786 << " by the port " << portId;
787 requestedIsValid = false;
788 }
789 } else {
790 requestedIsFullySpecified = false;
791 }
792
793 if (in_requested.gain.has_value()) {
794 // Let's pretend that gain can always be applied.
795 out_suggested->gain = in_requested.gain.value();
796 }
797
798 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
799 out_suggested->id = getConfig().nextPortId++;
800 configs.push_back(*out_suggested);
801 *_aidl_return = true;
802 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
803 } else if (existing != configs.end() && requestedIsValid) {
804 *existing = *out_suggested;
805 *_aidl_return = true;
806 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
807 } else {
808 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
809 << "; requested is valid? " << requestedIsValid << ", fully specified? "
810 << requestedIsFullySpecified;
811 *_aidl_return = false;
812 }
813 return ndk::ScopedAStatus::ok();
814}
815
816ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
817 auto& patches = getConfig().patches;
818 auto patchIt = findById<AudioPatch>(patches, in_patchId);
819 if (patchIt != patches.end()) {
820 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000821 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000822 patches.erase(patchIt);
823 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
824 return ndk::ScopedAStatus::ok();
825 }
826 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
827 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
828}
829
830ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
831 auto& configs = getConfig().portConfigs;
832 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
833 if (configIt != configs.end()) {
834 if (mStreams.count(in_portConfigId) != 0) {
835 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
836 << " has a stream opened on it";
837 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
838 }
839 auto patchIt = mPatches.find(in_portConfigId);
840 if (patchIt != mPatches.end()) {
841 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
842 << " is used by the patch with id " << patchIt->second;
843 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
844 }
845 auto& initials = getConfig().initialConfigs;
846 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
847 if (initialIt == initials.end()) {
848 configs.erase(configIt);
849 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
850 } else if (*configIt != *initialIt) {
851 *configIt = *initialIt;
852 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
853 }
854 return ndk::ScopedAStatus::ok();
855 }
856 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
857 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
858}
859
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000860ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
861 *_aidl_return = mMasterMute;
862 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
863 return ndk::ScopedAStatus::ok();
864}
865
866ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
867 LOG(DEBUG) << __func__ << ": " << in_mute;
868 mMasterMute = in_mute;
869 return ndk::ScopedAStatus::ok();
870}
871
872ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
873 *_aidl_return = mMasterVolume;
874 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
875 return ndk::ScopedAStatus::ok();
876}
877
878ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
879 LOG(DEBUG) << __func__ << ": " << in_volume;
880 if (in_volume >= 0.0f && in_volume <= 1.0f) {
881 mMasterVolume = in_volume;
882 return ndk::ScopedAStatus::ok();
883 }
884 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
885 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
886}
887
888ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
889 *_aidl_return = mMicMute;
890 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
891 return ndk::ScopedAStatus::ok();
892}
893
894ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
895 LOG(DEBUG) << __func__ << ": " << in_mute;
896 mMicMute = in_mute;
897 return ndk::ScopedAStatus::ok();
898}
899
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000900ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
901 *_aidl_return = mConfig->microphones;
902 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
903 return ndk::ScopedAStatus::ok();
904}
905
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000906ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
907 // No checks for supported audio modes here, it's an informative notification.
908 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
909 return ndk::ScopedAStatus::ok();
910}
911
912ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
913 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
914 return ndk::ScopedAStatus::ok();
915}
916
917ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
918 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
919 return ndk::ScopedAStatus::ok();
920}
921
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000922} // namespace aidl::android::hardware::audio::core