blob: 47d6fa44bea92e3da464090d51b52c9cd60cbf22 [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>
Shunkai Yao39bf2c32022-12-06 03:25:59 +000022#include <android/binder_ibinder_platform.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000023
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000024#include <Utils.h>
25#include <aidl/android/media/audio/common/AudioInputFlags.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000026#include <aidl/android/media/audio/common/AudioOutputFlags.h>
27
28#include "core-impl/Module.h"
Mikhail Naganov3b125b72022-10-05 02:12:39 +000029#include "core-impl/Telephony.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000030#include "core-impl/utils.h"
31
32using aidl::android::hardware::audio::common::SinkMetadata;
33using aidl::android::hardware::audio::common::SourceMetadata;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000034using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000035using aidl::android::media::audio::common::AudioDevice;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000036using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000037using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000038using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000039using aidl::android::media::audio::common::AudioIoFlags;
40using aidl::android::media::audio::common::AudioOffloadInfo;
41using aidl::android::media::audio::common::AudioOutputFlags;
42using aidl::android::media::audio::common::AudioPort;
43using aidl::android::media::audio::common::AudioPortConfig;
44using aidl::android::media::audio::common::AudioPortExt;
45using aidl::android::media::audio::common::AudioProfile;
46using aidl::android::media::audio::common::Int;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000047using aidl::android::media::audio::common::PcmType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000048using android::hardware::audio::common::getFrameSizeInBytes;
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +000049using android::hardware::audio::common::isBitPositionFlagSet;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000050
51namespace aidl::android::hardware::audio::core {
52
53namespace {
54
55bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
56 *config = {};
57 config->portId = port.id;
58 if (port.profiles.empty()) {
59 LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles";
60 return false;
61 }
62 const auto& profile = port.profiles.begin();
63 config->format = profile->format;
64 if (profile->channelMasks.empty()) {
65 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
66 << " has no channel masks";
67 return false;
68 }
69 config->channelMask = *profile->channelMasks.begin();
70 if (profile->sampleRates.empty()) {
71 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
72 << " has no sample rates";
73 return false;
74 }
75 Int sampleRate;
76 sampleRate.value = *profile->sampleRates.begin();
77 config->sampleRate = sampleRate;
78 config->flags = port.flags;
79 config->ext = port.ext;
80 return true;
81}
82
83bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
84 AudioProfile* profile) {
85 if (auto profilesIt =
86 find_if(port.profiles.begin(), port.profiles.end(),
87 [&format](const auto& profile) { return profile.format == format; });
88 profilesIt != port.profiles.end()) {
89 *profile = *profilesIt;
90 return true;
91 }
92 return false;
93}
Mikhail Naganov00603d12022-05-02 22:52:13 +000094
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000095} // namespace
96
97void Module::cleanUpPatch(int32_t patchId) {
98 erase_all_values(mPatches, std::set<int32_t>{patchId});
99}
100
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000101ndk::ScopedAStatus Module::createStreamContext(int32_t in_portConfigId, int64_t in_bufferSizeFrames,
Mikhail Naganov30301a42022-09-13 01:20:45 +0000102 std::shared_ptr<IStreamCallback> asyncCallback,
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000103 StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000104 if (in_bufferSizeFrames <= 0) {
105 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
106 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
107 }
108 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
109 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
110 << ", must be at least " << kMinimumStreamBufferSizeFrames;
111 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
112 }
113 auto& configs = getConfig().portConfigs;
114 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000115 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000116 // validity of the portConfigId has already been checked.
117 const size_t frameSize =
118 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
119 if (frameSize == 0) {
120 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
121 << portConfigIt->toString();
122 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
123 }
124 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
125 if (frameSize > kMaximumStreamBufferSizeBytes / in_bufferSizeFrames) {
126 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
127 << " frames is too large, maximum size is "
128 << kMaximumStreamBufferSizeBytes / frameSize;
129 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
130 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000131 const auto& flags = portConfigIt->flags.value();
132 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000133 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
134 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000135 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000136 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
137 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000138 StreamContext temp(
139 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
140 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000141 portConfigIt->format.value(), portConfigIt->channelMask.value(),
142 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov30301a42022-09-13 01:20:45 +0000143 asyncCallback, mDebug.streamTransientStateDelayMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000144 if (temp.isValid()) {
145 *out_context = std::move(temp);
146 } else {
147 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
148 }
149 } else {
150 // TODO: Implement simulation of MMAP buffer allocation
151 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000152 return ndk::ScopedAStatus::ok();
153}
154
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000155std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
156 std::vector<AudioDevice> result;
157 auto& ports = getConfig().ports;
158 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
159 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
160 auto portIt = findById<AudioPort>(ports, *it);
161 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
162 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
163 }
164 }
165 return result;
166}
167
168std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
169 std::set<int32_t> result;
170 auto patchIdsRange = mPatches.equal_range(portConfigId);
171 auto& patches = getConfig().patches;
172 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
173 auto patchIt = findById<AudioPatch>(patches, it->second);
174 if (patchIt == patches.end()) {
175 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
176 << "not found in the configuration";
177 }
178 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
179 portConfigId) != patchIt->sourcePortConfigIds.end()) {
180 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
181 } else {
182 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
183 }
184 }
185 return result;
186}
187
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000188ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
189 auto& configs = getConfig().portConfigs;
190 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
191 if (portConfigIt == configs.end()) {
192 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
193 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
194 }
195 const int32_t portId = portConfigIt->portId;
196 // In our implementation, configs of mix ports always have unique IDs.
197 CHECK(portId != in_portConfigId);
198 auto& ports = getConfig().ports;
199 auto portIt = findById<AudioPort>(ports, portId);
200 if (portIt == ports.end()) {
201 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
202 << in_portConfigId << " not found";
203 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
204 }
205 if (mStreams.count(in_portConfigId) != 0) {
206 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
207 << " already has a stream opened on it";
208 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
209 }
210 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
211 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
212 << " does not correspond to a mix port";
213 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
214 }
215 const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
216 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
217 LOG(ERROR) << __func__ << ": port id " << portId
218 << " has already reached maximum allowed opened stream count: "
219 << maxOpenStreamCount;
220 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
221 }
222 *port = &(*portIt);
223 return ndk::ScopedAStatus::ok();
224}
225
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000226template <typename C>
227std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
228 std::set<int32_t> result;
229 auto& portConfigs = getConfig().portConfigs;
230 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
231 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
232 if (portConfigIt != portConfigs.end()) {
233 result.insert(portConfigIt->portId);
234 }
235 }
236 return result;
237}
238
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000239internal::Configuration& Module::getConfig() {
240 if (!mConfig) {
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000241 switch (mType) {
242 case Type::DEFAULT:
243 mConfig = std::move(internal::getPrimaryConfiguration());
244 break;
245 case Type::R_SUBMIX:
246 mConfig = std::move(internal::getRSubmixConfiguration());
247 break;
248 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000249 }
250 return *mConfig;
251}
252
253void Module::registerPatch(const AudioPatch& patch) {
254 auto& configs = getConfig().portConfigs;
255 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
256 for (auto portConfigId : portConfigIds) {
257 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
258 if (configIt != configs.end()) {
259 mPatches.insert(std::pair{portConfigId, patch.id});
260 if (configIt->portId != portConfigId) {
261 mPatches.insert(std::pair{configIt->portId, patch.id});
262 }
263 }
264 };
265 };
266 do_insert(patch.sourcePortConfigIds);
267 do_insert(patch.sinkPortConfigIds);
268}
269
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000270void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) {
271 // Streams from the old patch need to be disconnected, streams from the new
272 // patch need to be connected. If the stream belongs to both patches, no need
273 // to update it.
274 std::set<int32_t> idsToDisconnect, idsToConnect;
275 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
276 oldPatch.sourcePortConfigIds.end());
277 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
278 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
279 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
280 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
281 if (idsToConnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000282 LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected";
283 mStreams.setStreamIsConnected(portConfigId, {});
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000284 }
285 });
286 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
287 if (idsToDisconnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000288 const auto connectedDevices = findConnectedDevices(portConfigId);
289 LOG(DEBUG) << "The stream on port config id " << portConfigId
290 << " is connected to: " << ::android::internal::ToString(connectedDevices);
291 mStreams.setStreamIsConnected(portConfigId, connectedDevices);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000292 }
293 });
294}
295
Mikhail Naganov00603d12022-05-02 22:52:13 +0000296ndk::ScopedAStatus Module::setModuleDebug(
297 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
298 LOG(DEBUG) << __func__ << ": old flags:" << mDebug.toString()
299 << ", new flags: " << in_debug.toString();
300 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
301 !mConnectedDevicePorts.empty()) {
302 LOG(ERROR) << __func__ << ": attempting to change device connections simulation "
303 << "while having external devices connected";
304 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
305 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000306 if (in_debug.streamTransientStateDelayMs < 0) {
307 LOG(ERROR) << __func__ << ": streamTransientStateDelayMs is negative: "
308 << in_debug.streamTransientStateDelayMs;
309 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
310 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000311 mDebug = in_debug;
312 return ndk::ScopedAStatus::ok();
313}
314
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000315ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
316 if (mTelephony == nullptr) {
317 mTelephony = ndk::SharedRefBase::make<Telephony>();
Shunkai Yao39bf2c32022-12-06 03:25:59 +0000318 AIBinder_setMinSchedulerPolicy(mTelephony->asBinder().get(), SCHED_NORMAL,
319 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000320 }
321 *_aidl_return = mTelephony;
322 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
323 return ndk::ScopedAStatus::ok();
324}
325
Mikhail Naganov00603d12022-05-02 22:52:13 +0000326ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
327 AudioPort* _aidl_return) {
328 const int32_t templateId = in_templateIdAndAdditionalData.id;
329 auto& ports = getConfig().ports;
330 AudioPort connectedPort;
331 { // Scope the template port so that we don't accidentally modify it.
332 auto templateIt = findById<AudioPort>(ports, templateId);
333 if (templateIt == ports.end()) {
334 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
335 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
336 }
337 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
338 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
339 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
340 }
341 if (!templateIt->profiles.empty()) {
342 LOG(ERROR) << __func__ << ": port id " << templateId
343 << " does not have dynamic profiles";
344 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
345 }
346 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
347 if (templateDevicePort.device.type.connection.empty()) {
348 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
349 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
350 }
351 // Postpone id allocation until we ensure that there are no client errors.
352 connectedPort = *templateIt;
353 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
354 const auto& inputDevicePort =
355 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
356 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
357 connectedDevicePort.device.address = inputDevicePort.device.address;
358 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
359 << connectedDevicePort.device.toString();
360 // Check if there is already a connected port with for the same external device.
361 for (auto connectedPortId : mConnectedDevicePorts) {
362 auto connectedPortIt = findById<AudioPort>(ports, connectedPortId);
363 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
364 connectedDevicePort.device) {
365 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
366 << " is already connected at the device port id " << connectedPortId;
367 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
368 }
369 }
370 }
371
372 if (!mDebug.simulateDeviceConnections) {
373 // In a real HAL here we would attempt querying the profiles from the device.
374 LOG(ERROR) << __func__ << ": failed to query supported device profiles";
375 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
376 }
377
378 connectedPort.id = ++getConfig().nextPortId;
379 mConnectedDevicePorts.insert(connectedPort.id);
380 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
381 << "connected port ID " << connectedPort.id;
382 auto& connectedProfiles = getConfig().connectedProfiles;
383 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
384 connectedProfilesIt != connectedProfiles.end()) {
385 connectedPort.profiles = connectedProfilesIt->second;
386 }
387 ports.push_back(connectedPort);
388 *_aidl_return = std::move(connectedPort);
389
390 std::vector<AudioRoute> newRoutes;
391 auto& routes = getConfig().routes;
392 for (auto& r : routes) {
393 if (r.sinkPortId == templateId) {
394 AudioRoute newRoute;
395 newRoute.sourcePortIds = r.sourcePortIds;
396 newRoute.sinkPortId = connectedPort.id;
397 newRoute.isExclusive = r.isExclusive;
398 newRoutes.push_back(std::move(newRoute));
399 } else {
400 auto& srcs = r.sourcePortIds;
401 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
402 srcs.push_back(connectedPort.id);
403 }
404 }
405 }
406 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
407
408 return ndk::ScopedAStatus::ok();
409}
410
411ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
412 auto& ports = getConfig().ports;
413 auto portIt = findById<AudioPort>(ports, in_portId);
414 if (portIt == ports.end()) {
415 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
416 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
417 }
418 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
419 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
420 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
421 }
422 if (mConnectedDevicePorts.count(in_portId) == 0) {
423 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
424 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
425 }
426 auto& configs = getConfig().portConfigs;
427 auto& initials = getConfig().initialConfigs;
428 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
429 if (config.portId == in_portId) {
430 // Check if the configuration was provided by the client.
431 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
432 return initialIt == initials.end() || config != *initialIt;
433 }
434 return false;
435 });
436 if (configIt != configs.end()) {
437 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
438 << configIt->id;
439 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
440 }
441 ports.erase(portIt);
442 mConnectedDevicePorts.erase(in_portId);
443 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
444
445 auto& routes = getConfig().routes;
446 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
447 if (routesIt->sinkPortId == in_portId) {
448 routesIt = routes.erase(routesIt);
449 } else {
450 // Note: the list of sourcePortIds can't become empty because there must
451 // be the id of the template port in the route.
452 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
453 ++routesIt;
454 }
455 }
456
457 return ndk::ScopedAStatus::ok();
458}
459
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000460ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
461 *_aidl_return = getConfig().patches;
462 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
463 return ndk::ScopedAStatus::ok();
464}
465
466ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
467 auto& ports = getConfig().ports;
468 auto portIt = findById<AudioPort>(ports, in_portId);
469 if (portIt != ports.end()) {
470 *_aidl_return = *portIt;
471 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
472 return ndk::ScopedAStatus::ok();
473 }
474 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
475 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
476}
477
478ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
479 *_aidl_return = getConfig().portConfigs;
480 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
481 return ndk::ScopedAStatus::ok();
482}
483
484ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
485 *_aidl_return = getConfig().ports;
486 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
487 return ndk::ScopedAStatus::ok();
488}
489
490ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
491 *_aidl_return = getConfig().routes;
492 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
493 return ndk::ScopedAStatus::ok();
494}
495
Mikhail Naganov00603d12022-05-02 22:52:13 +0000496ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
497 std::vector<AudioRoute>* _aidl_return) {
498 auto& ports = getConfig().ports;
499 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
500 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
501 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
502 }
503 auto& routes = getConfig().routes;
504 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
505 [&](const auto& r) {
506 const auto& srcs = r.sourcePortIds;
507 return r.sinkPortId == in_portId ||
508 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
509 });
510 return ndk::ScopedAStatus::ok();
511}
512
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000513ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
514 OpenInputStreamReturn* _aidl_return) {
515 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
516 << in_args.bufferSizeFrames << " frames";
517 AudioPort* port = nullptr;
518 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
519 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000520 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000521 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
522 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000523 << " does not correspond to an input mix port";
524 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
525 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000526 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000527 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
528 &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000529 !status.isOk()) {
530 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000531 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000532 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000533 auto stream = ndk::SharedRefBase::make<StreamIn>(in_args.sinkMetadata, std::move(context),
534 mConfig->microphones);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000535 if (auto status = stream->init(); !status.isOk()) {
536 return status;
537 }
Shunkai Yao39bf2c32022-12-06 03:25:59 +0000538 AIBinder_setMinSchedulerPolicy(stream->asBinder().get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000539 StreamWrapper streamWrapper(stream);
540 auto patchIt = mPatches.find(in_args.portConfigId);
541 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000542 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000543 }
544 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000545 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000546 return ndk::ScopedAStatus::ok();
547}
548
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000549ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
550 OpenOutputStreamReturn* _aidl_return) {
551 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
552 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
553 << " frames";
554 AudioPort* port = nullptr;
555 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
556 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000557 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000558 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
559 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000560 << " does not correspond to an output mix port";
561 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
562 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000563 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
564 AudioOutputFlags::COMPRESS_OFFLOAD);
565 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000566 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000567 << " has COMPRESS_OFFLOAD flag set, requires offload info";
568 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
569 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000570 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
571 AudioOutputFlags::NON_BLOCKING);
572 if (isNonBlocking && in_args.callback == nullptr) {
573 LOG(ERROR) << __func__ << ": port id " << port->id
574 << " has NON_BLOCKING flag set, requires async callback";
575 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
576 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000577 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000578 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
579 isNonBlocking ? in_args.callback : nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000580 !status.isOk()) {
581 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000582 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000583 context.fillDescriptor(&_aidl_return->desc);
584 auto stream = ndk::SharedRefBase::make<StreamOut>(in_args.sourceMetadata, std::move(context),
585 in_args.offloadInfo);
586 if (auto status = stream->init(); !status.isOk()) {
587 return status;
588 }
Shunkai Yao39bf2c32022-12-06 03:25:59 +0000589 AIBinder_setMinSchedulerPolicy(stream->asBinder().get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000590 StreamWrapper streamWrapper(stream);
591 auto patchIt = mPatches.find(in_args.portConfigId);
592 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000593 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000594 }
595 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000596 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000597 return ndk::ScopedAStatus::ok();
598}
599
600ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000601 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000602 if (in_requested.sourcePortConfigIds.empty()) {
603 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
604 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
605 }
606 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
607 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
608 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
609 }
610 if (in_requested.sinkPortConfigIds.empty()) {
611 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
612 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
613 }
614 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
615 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
616 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
617 }
618
619 auto& configs = getConfig().portConfigs;
620 std::vector<int32_t> missingIds;
621 auto sources =
622 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
623 if (!missingIds.empty()) {
624 LOG(ERROR) << __func__ << ": following source port config ids not found: "
625 << ::android::internal::ToString(missingIds);
626 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
627 }
628 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
629 if (!missingIds.empty()) {
630 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
631 << ::android::internal::ToString(missingIds);
632 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
633 }
634 // bool indicates whether a non-exclusive route is available.
635 // If only an exclusive route is available, that means the patch can not be
636 // established if there is any other patch which currently uses the sink port.
637 std::map<int32_t, bool> allowedSinkPorts;
638 auto& routes = getConfig().routes;
639 for (auto src : sources) {
640 for (const auto& r : routes) {
641 const auto& srcs = r.sourcePortIds;
642 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
643 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
644 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
645 }
646 }
647 }
648 }
649 for (auto sink : sinks) {
650 if (allowedSinkPorts.count(sink->portId) == 0) {
651 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
652 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
653 }
654 }
655
656 auto& patches = getConfig().patches;
657 auto existing = patches.end();
658 std::optional<decltype(mPatches)> patchesBackup;
659 if (in_requested.id != 0) {
660 existing = findById<AudioPatch>(patches, in_requested.id);
661 if (existing != patches.end()) {
662 patchesBackup = mPatches;
663 cleanUpPatch(existing->id);
664 } else {
665 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
666 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
667 }
668 }
669 // Validate the requested patch.
670 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
671 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
672 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
673 << "is exclusive and is already used by some other patch";
674 if (patchesBackup.has_value()) {
675 mPatches = std::move(*patchesBackup);
676 }
677 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
678 }
679 }
680 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000681 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
682 _aidl_return->latenciesMs.clear();
683 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
684 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000685 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000686 if (existing == patches.end()) {
687 _aidl_return->id = getConfig().nextPatchId++;
688 patches.push_back(*_aidl_return);
689 existing = patches.begin() + (patches.size() - 1);
690 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000691 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000692 *existing = *_aidl_return;
693 }
694 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000695 updateStreamsConnectedState(oldPatch, *_aidl_return);
696
697 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
698 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000699 return ndk::ScopedAStatus::ok();
700}
701
702ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
703 AudioPortConfig* out_suggested, bool* _aidl_return) {
704 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
705 auto& configs = getConfig().portConfigs;
706 auto existing = configs.end();
707 if (in_requested.id != 0) {
708 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
709 existing == configs.end()) {
710 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
711 << " not found";
712 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
713 }
714 }
715
716 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
717 if (portId == 0) {
718 LOG(ERROR) << __func__ << ": input port config does not specify portId";
719 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
720 }
721 auto& ports = getConfig().ports;
722 auto portIt = findById<AudioPort>(ports, portId);
723 if (portIt == ports.end()) {
724 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
725 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
726 }
727 if (existing != configs.end()) {
728 *out_suggested = *existing;
729 } else {
730 AudioPortConfig newConfig;
731 if (generateDefaultPortConfig(*portIt, &newConfig)) {
732 *out_suggested = newConfig;
733 } else {
734 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
735 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
736 }
737 }
738 // From this moment, 'out_suggested' is either an existing port config,
739 // or a new generated config. Now attempt to update it according to the specified
740 // fields of 'in_requested'.
741
742 bool requestedIsValid = true, requestedIsFullySpecified = true;
743
744 AudioIoFlags portFlags = portIt->flags;
745 if (in_requested.flags.has_value()) {
746 if (in_requested.flags.value() != portFlags) {
747 LOG(WARNING) << __func__ << ": requested flags "
748 << in_requested.flags.value().toString() << " do not match port's "
749 << portId << " flags " << portFlags.toString();
750 requestedIsValid = false;
751 }
752 } else {
753 requestedIsFullySpecified = false;
754 }
755
756 AudioProfile portProfile;
757 if (in_requested.format.has_value()) {
758 const auto& format = in_requested.format.value();
759 if (findAudioProfile(*portIt, format, &portProfile)) {
760 out_suggested->format = format;
761 } else {
762 LOG(WARNING) << __func__ << ": requested format " << format.toString()
763 << " is not found in port's " << portId << " profiles";
764 requestedIsValid = false;
765 }
766 } else {
767 requestedIsFullySpecified = false;
768 }
769 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
770 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
771 << out_suggested->format.value().toString() << " anymore";
772 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
773 }
774
775 if (in_requested.channelMask.has_value()) {
776 const auto& channelMask = in_requested.channelMask.value();
777 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
778 portProfile.channelMasks.end()) {
779 out_suggested->channelMask = channelMask;
780 } else {
781 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
782 << " is not supported for the format " << portProfile.format.toString()
783 << " by the port " << portId;
784 requestedIsValid = false;
785 }
786 } else {
787 requestedIsFullySpecified = false;
788 }
789
790 if (in_requested.sampleRate.has_value()) {
791 const auto& sampleRate = in_requested.sampleRate.value();
792 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
793 sampleRate.value) != portProfile.sampleRates.end()) {
794 out_suggested->sampleRate = sampleRate;
795 } else {
796 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
797 << " is not supported for the format " << portProfile.format.toString()
798 << " by the port " << portId;
799 requestedIsValid = false;
800 }
801 } else {
802 requestedIsFullySpecified = false;
803 }
804
805 if (in_requested.gain.has_value()) {
806 // Let's pretend that gain can always be applied.
807 out_suggested->gain = in_requested.gain.value();
808 }
809
810 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
811 out_suggested->id = getConfig().nextPortId++;
812 configs.push_back(*out_suggested);
813 *_aidl_return = true;
814 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
815 } else if (existing != configs.end() && requestedIsValid) {
816 *existing = *out_suggested;
817 *_aidl_return = true;
818 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
819 } else {
820 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
821 << "; requested is valid? " << requestedIsValid << ", fully specified? "
822 << requestedIsFullySpecified;
823 *_aidl_return = false;
824 }
825 return ndk::ScopedAStatus::ok();
826}
827
828ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
829 auto& patches = getConfig().patches;
830 auto patchIt = findById<AudioPatch>(patches, in_patchId);
831 if (patchIt != patches.end()) {
832 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000833 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000834 patches.erase(patchIt);
835 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
836 return ndk::ScopedAStatus::ok();
837 }
838 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
839 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
840}
841
842ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
843 auto& configs = getConfig().portConfigs;
844 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
845 if (configIt != configs.end()) {
846 if (mStreams.count(in_portConfigId) != 0) {
847 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
848 << " has a stream opened on it";
849 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
850 }
851 auto patchIt = mPatches.find(in_portConfigId);
852 if (patchIt != mPatches.end()) {
853 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
854 << " is used by the patch with id " << patchIt->second;
855 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
856 }
857 auto& initials = getConfig().initialConfigs;
858 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
859 if (initialIt == initials.end()) {
860 configs.erase(configIt);
861 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
862 } else if (*configIt != *initialIt) {
863 *configIt = *initialIt;
864 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
865 }
866 return ndk::ScopedAStatus::ok();
867 }
868 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
869 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
870}
871
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000872ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
873 *_aidl_return = mMasterMute;
874 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
875 return ndk::ScopedAStatus::ok();
876}
877
878ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
879 LOG(DEBUG) << __func__ << ": " << in_mute;
880 mMasterMute = in_mute;
881 return ndk::ScopedAStatus::ok();
882}
883
884ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
885 *_aidl_return = mMasterVolume;
886 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
887 return ndk::ScopedAStatus::ok();
888}
889
890ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
891 LOG(DEBUG) << __func__ << ": " << in_volume;
892 if (in_volume >= 0.0f && in_volume <= 1.0f) {
893 mMasterVolume = in_volume;
894 return ndk::ScopedAStatus::ok();
895 }
896 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
897 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
898}
899
900ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
901 *_aidl_return = mMicMute;
902 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
903 return ndk::ScopedAStatus::ok();
904}
905
906ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
907 LOG(DEBUG) << __func__ << ": " << in_mute;
908 mMicMute = in_mute;
909 return ndk::ScopedAStatus::ok();
910}
911
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000912ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
913 *_aidl_return = mConfig->microphones;
914 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
915 return ndk::ScopedAStatus::ok();
916}
917
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000918ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
919 // No checks for supported audio modes here, it's an informative notification.
920 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
921 return ndk::ScopedAStatus::ok();
922}
923
924ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
925 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
926 return ndk::ScopedAStatus::ok();
927}
928
929ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
930 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
931 return ndk::ScopedAStatus::ok();
932}
933
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000934} // namespace aidl::android::hardware::audio::core