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