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