blob: 6a833c48484a710119f5b66b4e0d8f9f76dd1c83 [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
Mikhail Naganov74927202022-12-19 16:37:14 +0000606ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
607 SupportedPlaybackRateFactors* _aidl_return) {
608 LOG(DEBUG) << __func__;
609 (void)_aidl_return;
610 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
611}
612
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000613ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000614 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000615 if (in_requested.sourcePortConfigIds.empty()) {
616 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
617 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
618 }
619 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
620 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
621 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
622 }
623 if (in_requested.sinkPortConfigIds.empty()) {
624 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
625 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
626 }
627 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
628 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
629 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
630 }
631
632 auto& configs = getConfig().portConfigs;
633 std::vector<int32_t> missingIds;
634 auto sources =
635 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
636 if (!missingIds.empty()) {
637 LOG(ERROR) << __func__ << ": following source port config ids not found: "
638 << ::android::internal::ToString(missingIds);
639 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
640 }
641 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
642 if (!missingIds.empty()) {
643 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
644 << ::android::internal::ToString(missingIds);
645 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
646 }
647 // bool indicates whether a non-exclusive route is available.
648 // If only an exclusive route is available, that means the patch can not be
649 // established if there is any other patch which currently uses the sink port.
650 std::map<int32_t, bool> allowedSinkPorts;
651 auto& routes = getConfig().routes;
652 for (auto src : sources) {
653 for (const auto& r : routes) {
654 const auto& srcs = r.sourcePortIds;
655 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
656 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
657 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
658 }
659 }
660 }
661 }
662 for (auto sink : sinks) {
663 if (allowedSinkPorts.count(sink->portId) == 0) {
664 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
665 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
666 }
667 }
668
669 auto& patches = getConfig().patches;
670 auto existing = patches.end();
671 std::optional<decltype(mPatches)> patchesBackup;
672 if (in_requested.id != 0) {
673 existing = findById<AudioPatch>(patches, in_requested.id);
674 if (existing != patches.end()) {
675 patchesBackup = mPatches;
676 cleanUpPatch(existing->id);
677 } else {
678 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
679 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
680 }
681 }
682 // Validate the requested patch.
683 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
684 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
685 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
686 << "is exclusive and is already used by some other patch";
687 if (patchesBackup.has_value()) {
688 mPatches = std::move(*patchesBackup);
689 }
690 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
691 }
692 }
693 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000694 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
695 _aidl_return->latenciesMs.clear();
696 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
697 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000698 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000699 if (existing == patches.end()) {
700 _aidl_return->id = getConfig().nextPatchId++;
701 patches.push_back(*_aidl_return);
702 existing = patches.begin() + (patches.size() - 1);
703 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000704 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000705 *existing = *_aidl_return;
706 }
707 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000708 updateStreamsConnectedState(oldPatch, *_aidl_return);
709
710 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
711 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000712 return ndk::ScopedAStatus::ok();
713}
714
715ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
716 AudioPortConfig* out_suggested, bool* _aidl_return) {
717 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
718 auto& configs = getConfig().portConfigs;
719 auto existing = configs.end();
720 if (in_requested.id != 0) {
721 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
722 existing == configs.end()) {
723 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
724 << " not found";
725 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
726 }
727 }
728
729 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
730 if (portId == 0) {
731 LOG(ERROR) << __func__ << ": input port config does not specify portId";
732 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
733 }
734 auto& ports = getConfig().ports;
735 auto portIt = findById<AudioPort>(ports, portId);
736 if (portIt == ports.end()) {
737 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
738 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
739 }
740 if (existing != configs.end()) {
741 *out_suggested = *existing;
742 } else {
743 AudioPortConfig newConfig;
744 if (generateDefaultPortConfig(*portIt, &newConfig)) {
745 *out_suggested = newConfig;
746 } else {
747 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
748 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
749 }
750 }
751 // From this moment, 'out_suggested' is either an existing port config,
752 // or a new generated config. Now attempt to update it according to the specified
753 // fields of 'in_requested'.
754
755 bool requestedIsValid = true, requestedIsFullySpecified = true;
756
757 AudioIoFlags portFlags = portIt->flags;
758 if (in_requested.flags.has_value()) {
759 if (in_requested.flags.value() != portFlags) {
760 LOG(WARNING) << __func__ << ": requested flags "
761 << in_requested.flags.value().toString() << " do not match port's "
762 << portId << " flags " << portFlags.toString();
763 requestedIsValid = false;
764 }
765 } else {
766 requestedIsFullySpecified = false;
767 }
768
769 AudioProfile portProfile;
770 if (in_requested.format.has_value()) {
771 const auto& format = in_requested.format.value();
772 if (findAudioProfile(*portIt, format, &portProfile)) {
773 out_suggested->format = format;
774 } else {
775 LOG(WARNING) << __func__ << ": requested format " << format.toString()
776 << " is not found in port's " << portId << " profiles";
777 requestedIsValid = false;
778 }
779 } else {
780 requestedIsFullySpecified = false;
781 }
782 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
783 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
784 << out_suggested->format.value().toString() << " anymore";
785 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
786 }
787
788 if (in_requested.channelMask.has_value()) {
789 const auto& channelMask = in_requested.channelMask.value();
790 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
791 portProfile.channelMasks.end()) {
792 out_suggested->channelMask = channelMask;
793 } else {
794 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
795 << " is not supported for the format " << portProfile.format.toString()
796 << " by the port " << portId;
797 requestedIsValid = false;
798 }
799 } else {
800 requestedIsFullySpecified = false;
801 }
802
803 if (in_requested.sampleRate.has_value()) {
804 const auto& sampleRate = in_requested.sampleRate.value();
805 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
806 sampleRate.value) != portProfile.sampleRates.end()) {
807 out_suggested->sampleRate = sampleRate;
808 } else {
809 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
810 << " is not supported for the format " << portProfile.format.toString()
811 << " by the port " << portId;
812 requestedIsValid = false;
813 }
814 } else {
815 requestedIsFullySpecified = false;
816 }
817
818 if (in_requested.gain.has_value()) {
819 // Let's pretend that gain can always be applied.
820 out_suggested->gain = in_requested.gain.value();
821 }
822
823 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
824 out_suggested->id = getConfig().nextPortId++;
825 configs.push_back(*out_suggested);
826 *_aidl_return = true;
827 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
828 } else if (existing != configs.end() && requestedIsValid) {
829 *existing = *out_suggested;
830 *_aidl_return = true;
831 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
832 } else {
833 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
834 << "; requested is valid? " << requestedIsValid << ", fully specified? "
835 << requestedIsFullySpecified;
836 *_aidl_return = false;
837 }
838 return ndk::ScopedAStatus::ok();
839}
840
841ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
842 auto& patches = getConfig().patches;
843 auto patchIt = findById<AudioPatch>(patches, in_patchId);
844 if (patchIt != patches.end()) {
845 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000846 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000847 patches.erase(patchIt);
848 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
849 return ndk::ScopedAStatus::ok();
850 }
851 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
852 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
853}
854
855ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
856 auto& configs = getConfig().portConfigs;
857 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
858 if (configIt != configs.end()) {
859 if (mStreams.count(in_portConfigId) != 0) {
860 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
861 << " has a stream opened on it";
862 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
863 }
864 auto patchIt = mPatches.find(in_portConfigId);
865 if (patchIt != mPatches.end()) {
866 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
867 << " is used by the patch with id " << patchIt->second;
868 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
869 }
870 auto& initials = getConfig().initialConfigs;
871 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
872 if (initialIt == initials.end()) {
873 configs.erase(configIt);
874 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
875 } else if (*configIt != *initialIt) {
876 *configIt = *initialIt;
877 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
878 }
879 return ndk::ScopedAStatus::ok();
880 }
881 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
882 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
883}
884
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000885ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
886 *_aidl_return = mMasterMute;
887 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
888 return ndk::ScopedAStatus::ok();
889}
890
891ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
892 LOG(DEBUG) << __func__ << ": " << in_mute;
893 mMasterMute = in_mute;
894 return ndk::ScopedAStatus::ok();
895}
896
897ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
898 *_aidl_return = mMasterVolume;
899 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
900 return ndk::ScopedAStatus::ok();
901}
902
903ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
904 LOG(DEBUG) << __func__ << ": " << in_volume;
905 if (in_volume >= 0.0f && in_volume <= 1.0f) {
906 mMasterVolume = in_volume;
907 return ndk::ScopedAStatus::ok();
908 }
909 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
910 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
911}
912
913ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
914 *_aidl_return = mMicMute;
915 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
916 return ndk::ScopedAStatus::ok();
917}
918
919ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
920 LOG(DEBUG) << __func__ << ": " << in_mute;
921 mMicMute = in_mute;
922 return ndk::ScopedAStatus::ok();
923}
924
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000925ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
926 *_aidl_return = mConfig->microphones;
927 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
928 return ndk::ScopedAStatus::ok();
929}
930
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000931ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
932 // No checks for supported audio modes here, it's an informative notification.
933 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
934 return ndk::ScopedAStatus::ok();
935}
936
937ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
938 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
939 return ndk::ScopedAStatus::ok();
940}
941
942ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
943 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
944 return ndk::ScopedAStatus::ok();
945}
946
Vlad Popa83a6d822022-11-07 13:53:57 +0100947ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Vlad Popa943b7e22022-12-08 14:24:12 +0100948 if (mSoundDose == nullptr) {
949 mSoundDose = ndk::SharedRefBase::make<SoundDose>();
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000950 mSoundDoseBinder = mSoundDose->asBinder();
951 AIBinder_setMinSchedulerPolicy(mSoundDoseBinder.get(), SCHED_NORMAL,
952 ANDROID_PRIORITY_AUDIO);
Vlad Popa943b7e22022-12-08 14:24:12 +0100953 }
954 *_aidl_return = mSoundDose;
955 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +0100956 return ndk::ScopedAStatus::ok();
957}
958
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000959ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
960 LOG(DEBUG) << __func__;
961 (void)_aidl_return;
962 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
963}
964
965ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
966 std::vector<VendorParameter>* _aidl_return) {
967 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
968 (void)_aidl_return;
969 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
970}
971
972ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
973 bool in_async) {
974 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
975 << ", async: " << in_async;
976 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
977}
978
Mikhail Naganovfb1acde2022-12-12 18:57:36 +0000979ndk::ScopedAStatus Module::addDeviceEffect(
980 int32_t in_portConfigId,
981 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
982 if (in_effect == nullptr) {
983 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
984 } else {
985 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
986 << in_effect->asBinder().get();
987 }
988 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
989}
990
991ndk::ScopedAStatus Module::removeDeviceEffect(
992 int32_t in_portConfigId,
993 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
994 if (in_effect == nullptr) {
995 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
996 } else {
997 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
998 << in_effect->asBinder().get();
999 }
1000 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1001}
1002
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001003} // namespace aidl::android::hardware::audio::core