blob: b58c5621c4e937b851d329c97f4d4489dff31523 [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;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010035using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000036using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000037using aidl::android::media::audio::common::AudioDevice;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000038using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000039using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000040using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000041using aidl::android::media::audio::common::AudioIoFlags;
42using aidl::android::media::audio::common::AudioOffloadInfo;
43using aidl::android::media::audio::common::AudioOutputFlags;
44using aidl::android::media::audio::common::AudioPort;
45using aidl::android::media::audio::common::AudioPortConfig;
46using aidl::android::media::audio::common::AudioPortExt;
47using aidl::android::media::audio::common::AudioProfile;
48using aidl::android::media::audio::common::Int;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000049using aidl::android::media::audio::common::PcmType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000050using android::hardware::audio::common::getFrameSizeInBytes;
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +000051using android::hardware::audio::common::isBitPositionFlagSet;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000052
53namespace aidl::android::hardware::audio::core {
54
55namespace {
56
57bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
58 *config = {};
59 config->portId = port.id;
60 if (port.profiles.empty()) {
61 LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles";
62 return false;
63 }
64 const auto& profile = port.profiles.begin();
65 config->format = profile->format;
66 if (profile->channelMasks.empty()) {
67 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
68 << " has no channel masks";
69 return false;
70 }
71 config->channelMask = *profile->channelMasks.begin();
72 if (profile->sampleRates.empty()) {
73 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
74 << " has no sample rates";
75 return false;
76 }
77 Int sampleRate;
78 sampleRate.value = *profile->sampleRates.begin();
79 config->sampleRate = sampleRate;
80 config->flags = port.flags;
81 config->ext = port.ext;
82 return true;
83}
84
85bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
86 AudioProfile* profile) {
87 if (auto profilesIt =
88 find_if(port.profiles.begin(), port.profiles.end(),
89 [&format](const auto& profile) { return profile.format == format; });
90 profilesIt != port.profiles.end()) {
91 *profile = *profilesIt;
92 return true;
93 }
94 return false;
95}
Mikhail Naganov00603d12022-05-02 22:52:13 +000096
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000097} // namespace
98
99void Module::cleanUpPatch(int32_t patchId) {
100 erase_all_values(mPatches, std::set<int32_t>{patchId});
101}
102
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000103ndk::ScopedAStatus Module::createStreamContext(int32_t in_portConfigId, int64_t in_bufferSizeFrames,
Mikhail Naganov30301a42022-09-13 01:20:45 +0000104 std::shared_ptr<IStreamCallback> asyncCallback,
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000105 StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000106 if (in_bufferSizeFrames <= 0) {
107 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
108 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
109 }
110 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
111 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
112 << ", must be at least " << kMinimumStreamBufferSizeFrames;
113 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
114 }
115 auto& configs = getConfig().portConfigs;
116 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000117 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000118 // validity of the portConfigId has already been checked.
119 const size_t frameSize =
120 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
121 if (frameSize == 0) {
122 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
123 << portConfigIt->toString();
124 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
125 }
126 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
127 if (frameSize > kMaximumStreamBufferSizeBytes / in_bufferSizeFrames) {
128 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
129 << " frames is too large, maximum size is "
130 << kMaximumStreamBufferSizeBytes / frameSize;
131 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
132 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000133 const auto& flags = portConfigIt->flags.value();
134 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000135 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
136 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000137 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000138 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
139 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000140 StreamContext temp(
141 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
142 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000143 portConfigIt->format.value(), portConfigIt->channelMask.value(),
144 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov30301a42022-09-13 01:20:45 +0000145 asyncCallback, mDebug.streamTransientStateDelayMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000146 if (temp.isValid()) {
147 *out_context = std::move(temp);
148 } else {
149 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
150 }
151 } else {
152 // TODO: Implement simulation of MMAP buffer allocation
153 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000154 return ndk::ScopedAStatus::ok();
155}
156
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000157std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
158 std::vector<AudioDevice> result;
159 auto& ports = getConfig().ports;
160 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
161 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
162 auto portIt = findById<AudioPort>(ports, *it);
163 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
164 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
165 }
166 }
167 return result;
168}
169
170std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
171 std::set<int32_t> result;
172 auto patchIdsRange = mPatches.equal_range(portConfigId);
173 auto& patches = getConfig().patches;
174 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
175 auto patchIt = findById<AudioPatch>(patches, it->second);
176 if (patchIt == patches.end()) {
177 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
178 << "not found in the configuration";
179 }
180 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
181 portConfigId) != patchIt->sourcePortConfigIds.end()) {
182 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
183 } else {
184 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
185 }
186 }
187 return result;
188}
189
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000190ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
191 auto& configs = getConfig().portConfigs;
192 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
193 if (portConfigIt == configs.end()) {
194 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
195 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
196 }
197 const int32_t portId = portConfigIt->portId;
198 // In our implementation, configs of mix ports always have unique IDs.
199 CHECK(portId != in_portConfigId);
200 auto& ports = getConfig().ports;
201 auto portIt = findById<AudioPort>(ports, portId);
202 if (portIt == ports.end()) {
203 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
204 << in_portConfigId << " not found";
205 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
206 }
207 if (mStreams.count(in_portConfigId) != 0) {
208 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
209 << " already has a stream opened on it";
210 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
211 }
212 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
213 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
214 << " does not correspond to a mix port";
215 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
216 }
217 const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
218 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
219 LOG(ERROR) << __func__ << ": port id " << portId
220 << " has already reached maximum allowed opened stream count: "
221 << maxOpenStreamCount;
222 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
223 }
224 *port = &(*portIt);
225 return ndk::ScopedAStatus::ok();
226}
227
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000228template <typename C>
229std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
230 std::set<int32_t> result;
231 auto& portConfigs = getConfig().portConfigs;
232 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
233 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
234 if (portConfigIt != portConfigs.end()) {
235 result.insert(portConfigIt->portId);
236 }
237 }
238 return result;
239}
240
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000241internal::Configuration& Module::getConfig() {
242 if (!mConfig) {
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000243 switch (mType) {
244 case Type::DEFAULT:
245 mConfig = std::move(internal::getPrimaryConfiguration());
246 break;
247 case Type::R_SUBMIX:
248 mConfig = std::move(internal::getRSubmixConfiguration());
249 break;
250 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000251 }
252 return *mConfig;
253}
254
255void Module::registerPatch(const AudioPatch& patch) {
256 auto& configs = getConfig().portConfigs;
257 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
258 for (auto portConfigId : portConfigIds) {
259 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
260 if (configIt != configs.end()) {
261 mPatches.insert(std::pair{portConfigId, patch.id});
262 if (configIt->portId != portConfigId) {
263 mPatches.insert(std::pair{configIt->portId, patch.id});
264 }
265 }
266 };
267 };
268 do_insert(patch.sourcePortConfigIds);
269 do_insert(patch.sinkPortConfigIds);
270}
271
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000272void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) {
273 // Streams from the old patch need to be disconnected, streams from the new
274 // patch need to be connected. If the stream belongs to both patches, no need
275 // to update it.
276 std::set<int32_t> idsToDisconnect, idsToConnect;
277 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
278 oldPatch.sourcePortConfigIds.end());
279 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
280 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
281 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
282 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
283 if (idsToConnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000284 LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected";
285 mStreams.setStreamIsConnected(portConfigId, {});
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000286 }
287 });
288 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
289 if (idsToDisconnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000290 const auto connectedDevices = findConnectedDevices(portConfigId);
291 LOG(DEBUG) << "The stream on port config id " << portConfigId
292 << " is connected to: " << ::android::internal::ToString(connectedDevices);
293 mStreams.setStreamIsConnected(portConfigId, connectedDevices);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000294 }
295 });
296}
297
Mikhail Naganov00603d12022-05-02 22:52:13 +0000298ndk::ScopedAStatus Module::setModuleDebug(
299 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
300 LOG(DEBUG) << __func__ << ": old flags:" << mDebug.toString()
301 << ", new flags: " << in_debug.toString();
302 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
303 !mConnectedDevicePorts.empty()) {
304 LOG(ERROR) << __func__ << ": attempting to change device connections simulation "
305 << "while having external devices connected";
306 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
307 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000308 if (in_debug.streamTransientStateDelayMs < 0) {
309 LOG(ERROR) << __func__ << ": streamTransientStateDelayMs is negative: "
310 << in_debug.streamTransientStateDelayMs;
311 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
312 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000313 mDebug = in_debug;
314 return ndk::ScopedAStatus::ok();
315}
316
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000317ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
318 if (mTelephony == nullptr) {
319 mTelephony = ndk::SharedRefBase::make<Telephony>();
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000320 mTelephonyBinder = mTelephony->asBinder();
321 AIBinder_setMinSchedulerPolicy(mTelephonyBinder.get(), SCHED_NORMAL,
Shunkai Yao39bf2c32022-12-06 03:25:59 +0000322 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000323 }
324 *_aidl_return = mTelephony;
325 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
326 return ndk::ScopedAStatus::ok();
327}
328
Mikhail Naganov00603d12022-05-02 22:52:13 +0000329ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
330 AudioPort* _aidl_return) {
331 const int32_t templateId = in_templateIdAndAdditionalData.id;
332 auto& ports = getConfig().ports;
333 AudioPort connectedPort;
334 { // Scope the template port so that we don't accidentally modify it.
335 auto templateIt = findById<AudioPort>(ports, templateId);
336 if (templateIt == ports.end()) {
337 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
338 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
339 }
340 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
341 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
342 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
343 }
344 if (!templateIt->profiles.empty()) {
345 LOG(ERROR) << __func__ << ": port id " << templateId
346 << " does not have dynamic profiles";
347 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
348 }
349 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
350 if (templateDevicePort.device.type.connection.empty()) {
351 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
352 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
353 }
354 // Postpone id allocation until we ensure that there are no client errors.
355 connectedPort = *templateIt;
356 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
357 const auto& inputDevicePort =
358 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
359 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
360 connectedDevicePort.device.address = inputDevicePort.device.address;
361 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
362 << connectedDevicePort.device.toString();
363 // Check if there is already a connected port with for the same external device.
364 for (auto connectedPortId : mConnectedDevicePorts) {
365 auto connectedPortIt = findById<AudioPort>(ports, connectedPortId);
366 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
367 connectedDevicePort.device) {
368 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
369 << " is already connected at the device port id " << connectedPortId;
370 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
371 }
372 }
373 }
374
375 if (!mDebug.simulateDeviceConnections) {
376 // In a real HAL here we would attempt querying the profiles from the device.
377 LOG(ERROR) << __func__ << ": failed to query supported device profiles";
378 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
379 }
380
381 connectedPort.id = ++getConfig().nextPortId;
382 mConnectedDevicePorts.insert(connectedPort.id);
383 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
384 << "connected port ID " << connectedPort.id;
385 auto& connectedProfiles = getConfig().connectedProfiles;
386 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
387 connectedProfilesIt != connectedProfiles.end()) {
388 connectedPort.profiles = connectedProfilesIt->second;
389 }
390 ports.push_back(connectedPort);
391 *_aidl_return = std::move(connectedPort);
392
393 std::vector<AudioRoute> newRoutes;
394 auto& routes = getConfig().routes;
395 for (auto& r : routes) {
396 if (r.sinkPortId == templateId) {
397 AudioRoute newRoute;
398 newRoute.sourcePortIds = r.sourcePortIds;
399 newRoute.sinkPortId = connectedPort.id;
400 newRoute.isExclusive = r.isExclusive;
401 newRoutes.push_back(std::move(newRoute));
402 } else {
403 auto& srcs = r.sourcePortIds;
404 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
405 srcs.push_back(connectedPort.id);
406 }
407 }
408 }
409 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
410
411 return ndk::ScopedAStatus::ok();
412}
413
414ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
415 auto& ports = getConfig().ports;
416 auto portIt = findById<AudioPort>(ports, in_portId);
417 if (portIt == ports.end()) {
418 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
419 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
420 }
421 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
422 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
423 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
424 }
425 if (mConnectedDevicePorts.count(in_portId) == 0) {
426 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
427 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
428 }
429 auto& configs = getConfig().portConfigs;
430 auto& initials = getConfig().initialConfigs;
431 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
432 if (config.portId == in_portId) {
433 // Check if the configuration was provided by the client.
434 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
435 return initialIt == initials.end() || config != *initialIt;
436 }
437 return false;
438 });
439 if (configIt != configs.end()) {
440 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
441 << configIt->id;
442 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
443 }
444 ports.erase(portIt);
445 mConnectedDevicePorts.erase(in_portId);
446 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
447
448 auto& routes = getConfig().routes;
449 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
450 if (routesIt->sinkPortId == in_portId) {
451 routesIt = routes.erase(routesIt);
452 } else {
453 // Note: the list of sourcePortIds can't become empty because there must
454 // be the id of the template port in the route.
455 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
456 ++routesIt;
457 }
458 }
459
460 return ndk::ScopedAStatus::ok();
461}
462
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000463ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
464 *_aidl_return = getConfig().patches;
465 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
466 return ndk::ScopedAStatus::ok();
467}
468
469ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
470 auto& ports = getConfig().ports;
471 auto portIt = findById<AudioPort>(ports, in_portId);
472 if (portIt != ports.end()) {
473 *_aidl_return = *portIt;
474 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
475 return ndk::ScopedAStatus::ok();
476 }
477 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
478 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
479}
480
481ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
482 *_aidl_return = getConfig().portConfigs;
483 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
484 return ndk::ScopedAStatus::ok();
485}
486
487ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
488 *_aidl_return = getConfig().ports;
489 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
490 return ndk::ScopedAStatus::ok();
491}
492
493ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
494 *_aidl_return = getConfig().routes;
495 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
496 return ndk::ScopedAStatus::ok();
497}
498
Mikhail Naganov00603d12022-05-02 22:52:13 +0000499ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
500 std::vector<AudioRoute>* _aidl_return) {
501 auto& ports = getConfig().ports;
502 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
503 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
504 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
505 }
506 auto& routes = getConfig().routes;
507 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
508 [&](const auto& r) {
509 const auto& srcs = r.sourcePortIds;
510 return r.sinkPortId == in_portId ||
511 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
512 });
513 return ndk::ScopedAStatus::ok();
514}
515
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000516ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
517 OpenInputStreamReturn* _aidl_return) {
518 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
519 << in_args.bufferSizeFrames << " frames";
520 AudioPort* port = nullptr;
521 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
522 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000523 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000524 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
525 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000526 << " does not correspond to an input mix port";
527 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
528 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000529 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000530 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
531 &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000532 !status.isOk()) {
533 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000534 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000535 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000536 std::shared_ptr<StreamIn> stream;
537 if (auto status = StreamIn::createInstance(in_args.sinkMetadata, std::move(context),
538 mConfig->microphones, &stream);
539 !status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000540 return status;
541 }
542 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000543 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
544 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000545 auto patchIt = mPatches.find(in_args.portConfigId);
546 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000547 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000548 }
549 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000550 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000551 return ndk::ScopedAStatus::ok();
552}
553
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000554ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
555 OpenOutputStreamReturn* _aidl_return) {
556 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
557 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
558 << " frames";
559 AudioPort* port = nullptr;
560 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
561 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000562 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000563 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
564 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000565 << " does not correspond to an output mix port";
566 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
567 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000568 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
569 AudioOutputFlags::COMPRESS_OFFLOAD);
570 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000571 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000572 << " has COMPRESS_OFFLOAD flag set, requires offload info";
573 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
574 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000575 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
576 AudioOutputFlags::NON_BLOCKING);
577 if (isNonBlocking && in_args.callback == nullptr) {
578 LOG(ERROR) << __func__ << ": port id " << port->id
579 << " has NON_BLOCKING flag set, requires async callback";
580 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
581 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000582 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000583 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
584 isNonBlocking ? in_args.callback : nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000585 !status.isOk()) {
586 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000587 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000588 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000589 std::shared_ptr<StreamOut> stream;
590 if (auto status = StreamOut::createInstance(in_args.sourceMetadata, std::move(context),
591 in_args.offloadInfo, &stream);
592 !status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000593 return status;
594 }
595 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000596 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
597 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000598 auto patchIt = mPatches.find(in_args.portConfigId);
599 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000600 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000601 }
602 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000603 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000604 return ndk::ScopedAStatus::ok();
605}
606
Mikhail Naganov74927202022-12-19 16:37:14 +0000607ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
608 SupportedPlaybackRateFactors* _aidl_return) {
609 LOG(DEBUG) << __func__;
610 (void)_aidl_return;
611 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
612}
613
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000614ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000615 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000616 if (in_requested.sourcePortConfigIds.empty()) {
617 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
618 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
619 }
620 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
621 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
622 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
623 }
624 if (in_requested.sinkPortConfigIds.empty()) {
625 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
626 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
627 }
628 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
629 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
630 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
631 }
632
633 auto& configs = getConfig().portConfigs;
634 std::vector<int32_t> missingIds;
635 auto sources =
636 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
637 if (!missingIds.empty()) {
638 LOG(ERROR) << __func__ << ": following source port config ids not found: "
639 << ::android::internal::ToString(missingIds);
640 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
641 }
642 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
643 if (!missingIds.empty()) {
644 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
645 << ::android::internal::ToString(missingIds);
646 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
647 }
648 // bool indicates whether a non-exclusive route is available.
649 // If only an exclusive route is available, that means the patch can not be
650 // established if there is any other patch which currently uses the sink port.
651 std::map<int32_t, bool> allowedSinkPorts;
652 auto& routes = getConfig().routes;
653 for (auto src : sources) {
654 for (const auto& r : routes) {
655 const auto& srcs = r.sourcePortIds;
656 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
657 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
658 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
659 }
660 }
661 }
662 }
663 for (auto sink : sinks) {
664 if (allowedSinkPorts.count(sink->portId) == 0) {
665 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
666 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
667 }
668 }
669
670 auto& patches = getConfig().patches;
671 auto existing = patches.end();
672 std::optional<decltype(mPatches)> patchesBackup;
673 if (in_requested.id != 0) {
674 existing = findById<AudioPatch>(patches, in_requested.id);
675 if (existing != patches.end()) {
676 patchesBackup = mPatches;
677 cleanUpPatch(existing->id);
678 } else {
679 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
680 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
681 }
682 }
683 // Validate the requested patch.
684 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
685 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
686 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
687 << "is exclusive and is already used by some other patch";
688 if (patchesBackup.has_value()) {
689 mPatches = std::move(*patchesBackup);
690 }
691 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
692 }
693 }
694 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000695 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
696 _aidl_return->latenciesMs.clear();
697 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
698 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000699 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000700 if (existing == patches.end()) {
701 _aidl_return->id = getConfig().nextPatchId++;
702 patches.push_back(*_aidl_return);
703 existing = patches.begin() + (patches.size() - 1);
704 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000705 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000706 *existing = *_aidl_return;
707 }
708 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000709 updateStreamsConnectedState(oldPatch, *_aidl_return);
710
711 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
712 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000713 return ndk::ScopedAStatus::ok();
714}
715
716ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
717 AudioPortConfig* out_suggested, bool* _aidl_return) {
718 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
719 auto& configs = getConfig().portConfigs;
720 auto existing = configs.end();
721 if (in_requested.id != 0) {
722 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
723 existing == configs.end()) {
724 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
725 << " not found";
726 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
727 }
728 }
729
730 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
731 if (portId == 0) {
732 LOG(ERROR) << __func__ << ": input port config does not specify portId";
733 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
734 }
735 auto& ports = getConfig().ports;
736 auto portIt = findById<AudioPort>(ports, portId);
737 if (portIt == ports.end()) {
738 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
739 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
740 }
741 if (existing != configs.end()) {
742 *out_suggested = *existing;
743 } else {
744 AudioPortConfig newConfig;
745 if (generateDefaultPortConfig(*portIt, &newConfig)) {
746 *out_suggested = newConfig;
747 } else {
748 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
749 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
750 }
751 }
752 // From this moment, 'out_suggested' is either an existing port config,
753 // or a new generated config. Now attempt to update it according to the specified
754 // fields of 'in_requested'.
755
756 bool requestedIsValid = true, requestedIsFullySpecified = true;
757
758 AudioIoFlags portFlags = portIt->flags;
759 if (in_requested.flags.has_value()) {
760 if (in_requested.flags.value() != portFlags) {
761 LOG(WARNING) << __func__ << ": requested flags "
762 << in_requested.flags.value().toString() << " do not match port's "
763 << portId << " flags " << portFlags.toString();
764 requestedIsValid = false;
765 }
766 } else {
767 requestedIsFullySpecified = false;
768 }
769
770 AudioProfile portProfile;
771 if (in_requested.format.has_value()) {
772 const auto& format = in_requested.format.value();
773 if (findAudioProfile(*portIt, format, &portProfile)) {
774 out_suggested->format = format;
775 } else {
776 LOG(WARNING) << __func__ << ": requested format " << format.toString()
777 << " is not found in port's " << portId << " profiles";
778 requestedIsValid = false;
779 }
780 } else {
781 requestedIsFullySpecified = false;
782 }
783 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
784 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
785 << out_suggested->format.value().toString() << " anymore";
786 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
787 }
788
789 if (in_requested.channelMask.has_value()) {
790 const auto& channelMask = in_requested.channelMask.value();
791 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
792 portProfile.channelMasks.end()) {
793 out_suggested->channelMask = channelMask;
794 } else {
795 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
796 << " is not supported for the format " << portProfile.format.toString()
797 << " by the port " << portId;
798 requestedIsValid = false;
799 }
800 } else {
801 requestedIsFullySpecified = false;
802 }
803
804 if (in_requested.sampleRate.has_value()) {
805 const auto& sampleRate = in_requested.sampleRate.value();
806 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
807 sampleRate.value) != portProfile.sampleRates.end()) {
808 out_suggested->sampleRate = sampleRate;
809 } else {
810 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
811 << " is not supported for the format " << portProfile.format.toString()
812 << " by the port " << portId;
813 requestedIsValid = false;
814 }
815 } else {
816 requestedIsFullySpecified = false;
817 }
818
819 if (in_requested.gain.has_value()) {
820 // Let's pretend that gain can always be applied.
821 out_suggested->gain = in_requested.gain.value();
822 }
823
824 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
825 out_suggested->id = getConfig().nextPortId++;
826 configs.push_back(*out_suggested);
827 *_aidl_return = true;
828 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
829 } else if (existing != configs.end() && requestedIsValid) {
830 *existing = *out_suggested;
831 *_aidl_return = true;
832 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
833 } else {
834 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
835 << "; requested is valid? " << requestedIsValid << ", fully specified? "
836 << requestedIsFullySpecified;
837 *_aidl_return = false;
838 }
839 return ndk::ScopedAStatus::ok();
840}
841
842ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
843 auto& patches = getConfig().patches;
844 auto patchIt = findById<AudioPatch>(patches, in_patchId);
845 if (patchIt != patches.end()) {
846 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000847 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000848 patches.erase(patchIt);
849 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
850 return ndk::ScopedAStatus::ok();
851 }
852 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
853 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
854}
855
856ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
857 auto& configs = getConfig().portConfigs;
858 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
859 if (configIt != configs.end()) {
860 if (mStreams.count(in_portConfigId) != 0) {
861 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
862 << " has a stream opened on it";
863 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
864 }
865 auto patchIt = mPatches.find(in_portConfigId);
866 if (patchIt != mPatches.end()) {
867 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
868 << " is used by the patch with id " << patchIt->second;
869 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
870 }
871 auto& initials = getConfig().initialConfigs;
872 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
873 if (initialIt == initials.end()) {
874 configs.erase(configIt);
875 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
876 } else if (*configIt != *initialIt) {
877 *configIt = *initialIt;
878 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
879 }
880 return ndk::ScopedAStatus::ok();
881 }
882 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
883 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
884}
885
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000886ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
887 *_aidl_return = mMasterMute;
888 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
889 return ndk::ScopedAStatus::ok();
890}
891
892ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
893 LOG(DEBUG) << __func__ << ": " << in_mute;
894 mMasterMute = in_mute;
895 return ndk::ScopedAStatus::ok();
896}
897
898ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
899 *_aidl_return = mMasterVolume;
900 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
901 return ndk::ScopedAStatus::ok();
902}
903
904ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
905 LOG(DEBUG) << __func__ << ": " << in_volume;
906 if (in_volume >= 0.0f && in_volume <= 1.0f) {
907 mMasterVolume = in_volume;
908 return ndk::ScopedAStatus::ok();
909 }
910 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
911 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
912}
913
914ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
915 *_aidl_return = mMicMute;
916 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
917 return ndk::ScopedAStatus::ok();
918}
919
920ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
921 LOG(DEBUG) << __func__ << ": " << in_mute;
922 mMicMute = in_mute;
923 return ndk::ScopedAStatus::ok();
924}
925
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000926ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
927 *_aidl_return = mConfig->microphones;
928 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
929 return ndk::ScopedAStatus::ok();
930}
931
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000932ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
933 // No checks for supported audio modes here, it's an informative notification.
934 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
935 return ndk::ScopedAStatus::ok();
936}
937
938ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
939 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
940 return ndk::ScopedAStatus::ok();
941}
942
943ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
944 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
945 return ndk::ScopedAStatus::ok();
946}
947
Vlad Popa83a6d822022-11-07 13:53:57 +0100948ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Vlad Popa943b7e22022-12-08 14:24:12 +0100949 if (mSoundDose == nullptr) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +0100950 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000951 mSoundDoseBinder = mSoundDose->asBinder();
952 AIBinder_setMinSchedulerPolicy(mSoundDoseBinder.get(), SCHED_NORMAL,
953 ANDROID_PRIORITY_AUDIO);
Vlad Popa943b7e22022-12-08 14:24:12 +0100954 }
955 *_aidl_return = mSoundDose;
956 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +0100957 return ndk::ScopedAStatus::ok();
958}
959
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000960ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
961 LOG(DEBUG) << __func__;
962 (void)_aidl_return;
963 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
964}
965
966ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
967 std::vector<VendorParameter>* _aidl_return) {
968 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
969 (void)_aidl_return;
970 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
971}
972
973ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
974 bool in_async) {
975 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
976 << ", async: " << in_async;
977 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
978}
979
Mikhail Naganovfb1acde2022-12-12 18:57:36 +0000980ndk::ScopedAStatus Module::addDeviceEffect(
981 int32_t in_portConfigId,
982 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
983 if (in_effect == nullptr) {
984 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
985 } else {
986 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
987 << in_effect->asBinder().get();
988 }
989 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
990}
991
992ndk::ScopedAStatus Module::removeDeviceEffect(
993 int32_t in_portConfigId,
994 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
995 if (in_effect == nullptr) {
996 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
997 } else {
998 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
999 << in_effect->asBinder().get();
1000 }
1001 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1002}
1003
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001004} // namespace aidl::android::hardware::audio::core