blob: 4a424bdfd808e69502639016ca89d44a71943dc3 [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 Naganove9f10fc2022-10-14 23:31:52 +0000535 std::shared_ptr<StreamIn> stream;
536 if (auto status = StreamIn::createInstance(in_args.sinkMetadata, std::move(context),
537 mConfig->microphones, &stream);
538 !status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000539 return status;
540 }
541 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000542 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
543 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000544 auto patchIt = mPatches.find(in_args.portConfigId);
545 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000546 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000547 }
548 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000549 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000550 return ndk::ScopedAStatus::ok();
551}
552
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000553ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
554 OpenOutputStreamReturn* _aidl_return) {
555 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
556 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
557 << " frames";
558 AudioPort* port = nullptr;
559 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
560 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000561 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000562 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
563 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000564 << " does not correspond to an output mix port";
565 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
566 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000567 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
568 AudioOutputFlags::COMPRESS_OFFLOAD);
569 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000570 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000571 << " has COMPRESS_OFFLOAD flag set, requires offload info";
572 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
573 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000574 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
575 AudioOutputFlags::NON_BLOCKING);
576 if (isNonBlocking && in_args.callback == nullptr) {
577 LOG(ERROR) << __func__ << ": port id " << port->id
578 << " has NON_BLOCKING flag set, requires async callback";
579 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
580 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000581 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000582 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
583 isNonBlocking ? in_args.callback : nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000584 !status.isOk()) {
585 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000586 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000587 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000588 std::shared_ptr<StreamOut> stream;
589 if (auto status = StreamOut::createInstance(in_args.sourceMetadata, std::move(context),
590 in_args.offloadInfo, &stream);
591 !status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000592 return status;
593 }
594 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000595 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
596 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000597 auto patchIt = mPatches.find(in_args.portConfigId);
598 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000599 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000600 }
601 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000602 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000603 return ndk::ScopedAStatus::ok();
604}
605
606ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000607 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000608 if (in_requested.sourcePortConfigIds.empty()) {
609 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
610 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
611 }
612 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
613 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
614 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
615 }
616 if (in_requested.sinkPortConfigIds.empty()) {
617 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
618 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
619 }
620 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
621 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
622 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
623 }
624
625 auto& configs = getConfig().portConfigs;
626 std::vector<int32_t> missingIds;
627 auto sources =
628 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
629 if (!missingIds.empty()) {
630 LOG(ERROR) << __func__ << ": following source port config ids not found: "
631 << ::android::internal::ToString(missingIds);
632 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
633 }
634 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
635 if (!missingIds.empty()) {
636 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
637 << ::android::internal::ToString(missingIds);
638 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
639 }
640 // bool indicates whether a non-exclusive route is available.
641 // If only an exclusive route is available, that means the patch can not be
642 // established if there is any other patch which currently uses the sink port.
643 std::map<int32_t, bool> allowedSinkPorts;
644 auto& routes = getConfig().routes;
645 for (auto src : sources) {
646 for (const auto& r : routes) {
647 const auto& srcs = r.sourcePortIds;
648 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
649 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
650 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
651 }
652 }
653 }
654 }
655 for (auto sink : sinks) {
656 if (allowedSinkPorts.count(sink->portId) == 0) {
657 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
658 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
659 }
660 }
661
662 auto& patches = getConfig().patches;
663 auto existing = patches.end();
664 std::optional<decltype(mPatches)> patchesBackup;
665 if (in_requested.id != 0) {
666 existing = findById<AudioPatch>(patches, in_requested.id);
667 if (existing != patches.end()) {
668 patchesBackup = mPatches;
669 cleanUpPatch(existing->id);
670 } else {
671 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
672 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
673 }
674 }
675 // Validate the requested patch.
676 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
677 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
678 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
679 << "is exclusive and is already used by some other patch";
680 if (patchesBackup.has_value()) {
681 mPatches = std::move(*patchesBackup);
682 }
683 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
684 }
685 }
686 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000687 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
688 _aidl_return->latenciesMs.clear();
689 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
690 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000691 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000692 if (existing == patches.end()) {
693 _aidl_return->id = getConfig().nextPatchId++;
694 patches.push_back(*_aidl_return);
695 existing = patches.begin() + (patches.size() - 1);
696 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000697 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000698 *existing = *_aidl_return;
699 }
700 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000701 updateStreamsConnectedState(oldPatch, *_aidl_return);
702
703 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
704 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000705 return ndk::ScopedAStatus::ok();
706}
707
708ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
709 AudioPortConfig* out_suggested, bool* _aidl_return) {
710 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
711 auto& configs = getConfig().portConfigs;
712 auto existing = configs.end();
713 if (in_requested.id != 0) {
714 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
715 existing == configs.end()) {
716 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
717 << " not found";
718 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
719 }
720 }
721
722 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
723 if (portId == 0) {
724 LOG(ERROR) << __func__ << ": input port config does not specify portId";
725 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
726 }
727 auto& ports = getConfig().ports;
728 auto portIt = findById<AudioPort>(ports, portId);
729 if (portIt == ports.end()) {
730 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
731 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
732 }
733 if (existing != configs.end()) {
734 *out_suggested = *existing;
735 } else {
736 AudioPortConfig newConfig;
737 if (generateDefaultPortConfig(*portIt, &newConfig)) {
738 *out_suggested = newConfig;
739 } else {
740 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
741 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
742 }
743 }
744 // From this moment, 'out_suggested' is either an existing port config,
745 // or a new generated config. Now attempt to update it according to the specified
746 // fields of 'in_requested'.
747
748 bool requestedIsValid = true, requestedIsFullySpecified = true;
749
750 AudioIoFlags portFlags = portIt->flags;
751 if (in_requested.flags.has_value()) {
752 if (in_requested.flags.value() != portFlags) {
753 LOG(WARNING) << __func__ << ": requested flags "
754 << in_requested.flags.value().toString() << " do not match port's "
755 << portId << " flags " << portFlags.toString();
756 requestedIsValid = false;
757 }
758 } else {
759 requestedIsFullySpecified = false;
760 }
761
762 AudioProfile portProfile;
763 if (in_requested.format.has_value()) {
764 const auto& format = in_requested.format.value();
765 if (findAudioProfile(*portIt, format, &portProfile)) {
766 out_suggested->format = format;
767 } else {
768 LOG(WARNING) << __func__ << ": requested format " << format.toString()
769 << " is not found in port's " << portId << " profiles";
770 requestedIsValid = false;
771 }
772 } else {
773 requestedIsFullySpecified = false;
774 }
775 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
776 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
777 << out_suggested->format.value().toString() << " anymore";
778 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
779 }
780
781 if (in_requested.channelMask.has_value()) {
782 const auto& channelMask = in_requested.channelMask.value();
783 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
784 portProfile.channelMasks.end()) {
785 out_suggested->channelMask = channelMask;
786 } else {
787 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
788 << " is not supported for the format " << portProfile.format.toString()
789 << " by the port " << portId;
790 requestedIsValid = false;
791 }
792 } else {
793 requestedIsFullySpecified = false;
794 }
795
796 if (in_requested.sampleRate.has_value()) {
797 const auto& sampleRate = in_requested.sampleRate.value();
798 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
799 sampleRate.value) != portProfile.sampleRates.end()) {
800 out_suggested->sampleRate = sampleRate;
801 } else {
802 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
803 << " is not supported for the format " << portProfile.format.toString()
804 << " by the port " << portId;
805 requestedIsValid = false;
806 }
807 } else {
808 requestedIsFullySpecified = false;
809 }
810
811 if (in_requested.gain.has_value()) {
812 // Let's pretend that gain can always be applied.
813 out_suggested->gain = in_requested.gain.value();
814 }
815
816 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
817 out_suggested->id = getConfig().nextPortId++;
818 configs.push_back(*out_suggested);
819 *_aidl_return = true;
820 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
821 } else if (existing != configs.end() && requestedIsValid) {
822 *existing = *out_suggested;
823 *_aidl_return = true;
824 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
825 } else {
826 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
827 << "; requested is valid? " << requestedIsValid << ", fully specified? "
828 << requestedIsFullySpecified;
829 *_aidl_return = false;
830 }
831 return ndk::ScopedAStatus::ok();
832}
833
834ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
835 auto& patches = getConfig().patches;
836 auto patchIt = findById<AudioPatch>(patches, in_patchId);
837 if (patchIt != patches.end()) {
838 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000839 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000840 patches.erase(patchIt);
841 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
842 return ndk::ScopedAStatus::ok();
843 }
844 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
845 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
846}
847
848ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
849 auto& configs = getConfig().portConfigs;
850 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
851 if (configIt != configs.end()) {
852 if (mStreams.count(in_portConfigId) != 0) {
853 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
854 << " has a stream opened on it";
855 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
856 }
857 auto patchIt = mPatches.find(in_portConfigId);
858 if (patchIt != mPatches.end()) {
859 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
860 << " is used by the patch with id " << patchIt->second;
861 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
862 }
863 auto& initials = getConfig().initialConfigs;
864 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
865 if (initialIt == initials.end()) {
866 configs.erase(configIt);
867 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
868 } else if (*configIt != *initialIt) {
869 *configIt = *initialIt;
870 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
871 }
872 return ndk::ScopedAStatus::ok();
873 }
874 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
875 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
876}
877
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000878ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
879 *_aidl_return = mMasterMute;
880 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
881 return ndk::ScopedAStatus::ok();
882}
883
884ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
885 LOG(DEBUG) << __func__ << ": " << in_mute;
886 mMasterMute = in_mute;
887 return ndk::ScopedAStatus::ok();
888}
889
890ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
891 *_aidl_return = mMasterVolume;
892 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
893 return ndk::ScopedAStatus::ok();
894}
895
896ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
897 LOG(DEBUG) << __func__ << ": " << in_volume;
898 if (in_volume >= 0.0f && in_volume <= 1.0f) {
899 mMasterVolume = in_volume;
900 return ndk::ScopedAStatus::ok();
901 }
902 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
903 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
904}
905
906ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
907 *_aidl_return = mMicMute;
908 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
909 return ndk::ScopedAStatus::ok();
910}
911
912ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
913 LOG(DEBUG) << __func__ << ": " << in_mute;
914 mMicMute = in_mute;
915 return ndk::ScopedAStatus::ok();
916}
917
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000918ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
919 *_aidl_return = mConfig->microphones;
920 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
921 return ndk::ScopedAStatus::ok();
922}
923
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000924ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
925 // No checks for supported audio modes here, it's an informative notification.
926 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
927 return ndk::ScopedAStatus::ok();
928}
929
930ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
931 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
932 return ndk::ScopedAStatus::ok();
933}
934
935ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
936 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
937 return ndk::ScopedAStatus::ok();
938}
939
Vlad Popa83a6d822022-11-07 13:53:57 +0100940ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Vlad Popa943b7e22022-12-08 14:24:12 +0100941 if (mSoundDose == nullptr) {
942 mSoundDose = ndk::SharedRefBase::make<SoundDose>();
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000943 mSoundDoseBinder = mSoundDose->asBinder();
944 AIBinder_setMinSchedulerPolicy(mSoundDoseBinder.get(), SCHED_NORMAL,
945 ANDROID_PRIORITY_AUDIO);
Vlad Popa943b7e22022-12-08 14:24:12 +0100946 }
947 *_aidl_return = mSoundDose;
948 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +0100949 return ndk::ScopedAStatus::ok();
950}
951
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000952ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
953 LOG(DEBUG) << __func__;
954 (void)_aidl_return;
955 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
956}
957
958ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
959 std::vector<VendorParameter>* _aidl_return) {
960 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
961 (void)_aidl_return;
962 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
963}
964
965ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
966 bool in_async) {
967 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
968 << ", async: " << in_async;
969 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
970}
971
Mikhail Naganovfb1acde2022-12-12 18:57:36 +0000972ndk::ScopedAStatus Module::addDeviceEffect(
973 int32_t in_portConfigId,
974 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
975 if (in_effect == nullptr) {
976 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
977 } else {
978 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
979 << in_effect->asBinder().get();
980 }
981 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
982}
983
984ndk::ScopedAStatus Module::removeDeviceEffect(
985 int32_t in_portConfigId,
986 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
987 if (in_effect == nullptr) {
988 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
989 } else {
990 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
991 << in_effect->asBinder().get();
992 }
993 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
994}
995
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000996} // namespace aidl::android::hardware::audio::core