blob: 780d6a9c1ddf6fdc750dedbc5b493a7015b45901 [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
Mikhail Naganov10c6fe22022-09-30 23:49:17 +000028#include "core-impl/Bluetooth.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000029#include "core-impl/Module.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010030#include "core-impl/SoundDose.h"
Mikhail Naganov3b125b72022-10-05 02:12:39 +000031#include "core-impl/Telephony.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000032#include "core-impl/utils.h"
33
34using aidl::android::hardware::audio::common::SinkMetadata;
35using aidl::android::hardware::audio::common::SourceMetadata;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010036using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000037using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000038using aidl::android::media::audio::common::AudioDevice;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000039using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000040using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000041using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000042using aidl::android::media::audio::common::AudioIoFlags;
43using aidl::android::media::audio::common::AudioOffloadInfo;
44using aidl::android::media::audio::common::AudioOutputFlags;
45using aidl::android::media::audio::common::AudioPort;
46using aidl::android::media::audio::common::AudioPortConfig;
47using aidl::android::media::audio::common::AudioPortExt;
48using aidl::android::media::audio::common::AudioProfile;
Mikhail Naganov20047bc2023-01-05 20:16:07 +000049using aidl::android::media::audio::common::Boolean;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000050using aidl::android::media::audio::common::Int;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000051using aidl::android::media::audio::common::PcmType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000052using android::hardware::audio::common::getFrameSizeInBytes;
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +000053using android::hardware::audio::common::isBitPositionFlagSet;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000054
55namespace aidl::android::hardware::audio::core {
56
57namespace {
58
59bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
60 *config = {};
61 config->portId = port.id;
62 if (port.profiles.empty()) {
63 LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles";
64 return false;
65 }
66 const auto& profile = port.profiles.begin();
67 config->format = profile->format;
68 if (profile->channelMasks.empty()) {
69 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
70 << " has no channel masks";
71 return false;
72 }
73 config->channelMask = *profile->channelMasks.begin();
74 if (profile->sampleRates.empty()) {
75 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
76 << " has no sample rates";
77 return false;
78 }
79 Int sampleRate;
80 sampleRate.value = *profile->sampleRates.begin();
81 config->sampleRate = sampleRate;
82 config->flags = port.flags;
83 config->ext = port.ext;
84 return true;
85}
86
87bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
88 AudioProfile* profile) {
89 if (auto profilesIt =
90 find_if(port.profiles.begin(), port.profiles.end(),
91 [&format](const auto& profile) { return profile.format == format; });
92 profilesIt != port.profiles.end()) {
93 *profile = *profilesIt;
94 return true;
95 }
96 return false;
97}
Mikhail Naganov00603d12022-05-02 22:52:13 +000098
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000099} // namespace
100
101void Module::cleanUpPatch(int32_t patchId) {
102 erase_all_values(mPatches, std::set<int32_t>{patchId});
103}
104
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000105ndk::ScopedAStatus Module::createStreamContext(int32_t in_portConfigId, int64_t in_bufferSizeFrames,
Mikhail Naganov30301a42022-09-13 01:20:45 +0000106 std::shared_ptr<IStreamCallback> asyncCallback,
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000107 StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000108 if (in_bufferSizeFrames <= 0) {
109 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
110 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
111 }
112 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
113 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
114 << ", must be at least " << kMinimumStreamBufferSizeFrames;
115 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
116 }
117 auto& configs = getConfig().portConfigs;
118 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000119 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000120 // validity of the portConfigId has already been checked.
121 const size_t frameSize =
122 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
123 if (frameSize == 0) {
124 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
125 << portConfigIt->toString();
126 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
127 }
128 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
129 if (frameSize > kMaximumStreamBufferSizeBytes / in_bufferSizeFrames) {
130 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
131 << " frames is too large, maximum size is "
132 << kMaximumStreamBufferSizeBytes / frameSize;
133 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
134 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000135 const auto& flags = portConfigIt->flags.value();
136 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000137 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
138 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000139 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000140 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
141 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000142 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
143 mVendorDebug.forceTransientBurst};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000144 StreamContext temp(
145 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
146 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000147 portConfigIt->format.value(), portConfigIt->channelMask.value(),
148 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000149 asyncCallback, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000150 if (temp.isValid()) {
151 *out_context = std::move(temp);
152 } else {
153 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
154 }
155 } else {
156 // TODO: Implement simulation of MMAP buffer allocation
157 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000158 return ndk::ScopedAStatus::ok();
159}
160
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000161std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
162 std::vector<AudioDevice> result;
163 auto& ports = getConfig().ports;
164 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
165 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
166 auto portIt = findById<AudioPort>(ports, *it);
167 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
168 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
169 }
170 }
171 return result;
172}
173
174std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
175 std::set<int32_t> result;
176 auto patchIdsRange = mPatches.equal_range(portConfigId);
177 auto& patches = getConfig().patches;
178 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
179 auto patchIt = findById<AudioPatch>(patches, it->second);
180 if (patchIt == patches.end()) {
181 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
182 << "not found in the configuration";
183 }
184 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
185 portConfigId) != patchIt->sourcePortConfigIds.end()) {
186 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
187 } else {
188 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
189 }
190 }
191 return result;
192}
193
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000194ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
195 auto& configs = getConfig().portConfigs;
196 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
197 if (portConfigIt == configs.end()) {
198 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
199 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
200 }
201 const int32_t portId = portConfigIt->portId;
202 // In our implementation, configs of mix ports always have unique IDs.
203 CHECK(portId != in_portConfigId);
204 auto& ports = getConfig().ports;
205 auto portIt = findById<AudioPort>(ports, portId);
206 if (portIt == ports.end()) {
207 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
208 << in_portConfigId << " not found";
209 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
210 }
211 if (mStreams.count(in_portConfigId) != 0) {
212 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
213 << " already has a stream opened on it";
214 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
215 }
216 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
217 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
218 << " does not correspond to a mix port";
219 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
220 }
221 const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
222 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
223 LOG(ERROR) << __func__ << ": port id " << portId
224 << " has already reached maximum allowed opened stream count: "
225 << maxOpenStreamCount;
226 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
227 }
228 *port = &(*portIt);
229 return ndk::ScopedAStatus::ok();
230}
231
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000232template <typename C>
233std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
234 std::set<int32_t> result;
235 auto& portConfigs = getConfig().portConfigs;
236 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
237 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
238 if (portConfigIt != portConfigs.end()) {
239 result.insert(portConfigIt->portId);
240 }
241 }
242 return result;
243}
244
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000245internal::Configuration& Module::getConfig() {
246 if (!mConfig) {
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000247 switch (mType) {
248 case Type::DEFAULT:
249 mConfig = std::move(internal::getPrimaryConfiguration());
250 break;
251 case Type::R_SUBMIX:
252 mConfig = std::move(internal::getRSubmixConfiguration());
253 break;
254 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000255 }
256 return *mConfig;
257}
258
259void Module::registerPatch(const AudioPatch& patch) {
260 auto& configs = getConfig().portConfigs;
261 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
262 for (auto portConfigId : portConfigIds) {
263 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
264 if (configIt != configs.end()) {
265 mPatches.insert(std::pair{portConfigId, patch.id});
266 if (configIt->portId != portConfigId) {
267 mPatches.insert(std::pair{configIt->portId, patch.id});
268 }
269 }
270 };
271 };
272 do_insert(patch.sourcePortConfigIds);
273 do_insert(patch.sinkPortConfigIds);
274}
275
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000276void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) {
277 // Streams from the old patch need to be disconnected, streams from the new
278 // patch need to be connected. If the stream belongs to both patches, no need
279 // to update it.
280 std::set<int32_t> idsToDisconnect, idsToConnect;
281 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
282 oldPatch.sourcePortConfigIds.end());
283 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
284 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
285 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
286 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
287 if (idsToConnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000288 LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected";
289 mStreams.setStreamIsConnected(portConfigId, {});
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000290 }
291 });
292 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
293 if (idsToDisconnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000294 const auto connectedDevices = findConnectedDevices(portConfigId);
295 LOG(DEBUG) << "The stream on port config id " << portConfigId
296 << " is connected to: " << ::android::internal::ToString(connectedDevices);
297 mStreams.setStreamIsConnected(portConfigId, connectedDevices);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000298 }
299 });
300}
301
Mikhail Naganov00603d12022-05-02 22:52:13 +0000302ndk::ScopedAStatus Module::setModuleDebug(
303 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
304 LOG(DEBUG) << __func__ << ": old flags:" << mDebug.toString()
305 << ", new flags: " << in_debug.toString();
306 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
307 !mConnectedDevicePorts.empty()) {
308 LOG(ERROR) << __func__ << ": attempting to change device connections simulation "
309 << "while having external devices connected";
310 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
311 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000312 if (in_debug.streamTransientStateDelayMs < 0) {
313 LOG(ERROR) << __func__ << ": streamTransientStateDelayMs is negative: "
314 << in_debug.streamTransientStateDelayMs;
315 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
316 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000317 mDebug = in_debug;
318 return ndk::ScopedAStatus::ok();
319}
320
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000321ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
322 if (mTelephony == nullptr) {
323 mTelephony = ndk::SharedRefBase::make<Telephony>();
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000324 mTelephonyBinder = mTelephony->asBinder();
325 AIBinder_setMinSchedulerPolicy(mTelephonyBinder.get(), SCHED_NORMAL,
Shunkai Yao39bf2c32022-12-06 03:25:59 +0000326 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000327 }
328 *_aidl_return = mTelephony;
329 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
330 return ndk::ScopedAStatus::ok();
331}
332
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000333ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
334 if (mBluetooth == nullptr) {
335 mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
336 mBluetoothBinder = mBluetooth->asBinder();
337 AIBinder_setMinSchedulerPolicy(mBluetoothBinder.get(), SCHED_NORMAL,
338 ANDROID_PRIORITY_AUDIO);
339 }
340 *_aidl_return = mBluetooth;
341 LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get();
342 return ndk::ScopedAStatus::ok();
343}
344
Mikhail Naganov00603d12022-05-02 22:52:13 +0000345ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
346 AudioPort* _aidl_return) {
347 const int32_t templateId = in_templateIdAndAdditionalData.id;
348 auto& ports = getConfig().ports;
349 AudioPort connectedPort;
350 { // Scope the template port so that we don't accidentally modify it.
351 auto templateIt = findById<AudioPort>(ports, templateId);
352 if (templateIt == ports.end()) {
353 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
354 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
355 }
356 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
357 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
358 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
359 }
360 if (!templateIt->profiles.empty()) {
361 LOG(ERROR) << __func__ << ": port id " << templateId
362 << " does not have dynamic profiles";
363 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
364 }
365 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
366 if (templateDevicePort.device.type.connection.empty()) {
367 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
368 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
369 }
370 // Postpone id allocation until we ensure that there are no client errors.
371 connectedPort = *templateIt;
372 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
373 const auto& inputDevicePort =
374 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
375 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
376 connectedDevicePort.device.address = inputDevicePort.device.address;
377 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
378 << connectedDevicePort.device.toString();
379 // Check if there is already a connected port with for the same external device.
380 for (auto connectedPortId : mConnectedDevicePorts) {
381 auto connectedPortIt = findById<AudioPort>(ports, connectedPortId);
382 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
383 connectedDevicePort.device) {
384 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
385 << " is already connected at the device port id " << connectedPortId;
386 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
387 }
388 }
389 }
390
391 if (!mDebug.simulateDeviceConnections) {
392 // In a real HAL here we would attempt querying the profiles from the device.
393 LOG(ERROR) << __func__ << ": failed to query supported device profiles";
394 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
395 }
396
397 connectedPort.id = ++getConfig().nextPortId;
398 mConnectedDevicePorts.insert(connectedPort.id);
399 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
400 << "connected port ID " << connectedPort.id;
401 auto& connectedProfiles = getConfig().connectedProfiles;
402 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
403 connectedProfilesIt != connectedProfiles.end()) {
404 connectedPort.profiles = connectedProfilesIt->second;
405 }
406 ports.push_back(connectedPort);
407 *_aidl_return = std::move(connectedPort);
408
409 std::vector<AudioRoute> newRoutes;
410 auto& routes = getConfig().routes;
411 for (auto& r : routes) {
412 if (r.sinkPortId == templateId) {
413 AudioRoute newRoute;
414 newRoute.sourcePortIds = r.sourcePortIds;
415 newRoute.sinkPortId = connectedPort.id;
416 newRoute.isExclusive = r.isExclusive;
417 newRoutes.push_back(std::move(newRoute));
418 } else {
419 auto& srcs = r.sourcePortIds;
420 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
421 srcs.push_back(connectedPort.id);
422 }
423 }
424 }
425 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
426
427 return ndk::ScopedAStatus::ok();
428}
429
430ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
431 auto& ports = getConfig().ports;
432 auto portIt = findById<AudioPort>(ports, in_portId);
433 if (portIt == ports.end()) {
434 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
435 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
436 }
437 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
438 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
439 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
440 }
441 if (mConnectedDevicePorts.count(in_portId) == 0) {
442 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
443 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
444 }
445 auto& configs = getConfig().portConfigs;
446 auto& initials = getConfig().initialConfigs;
447 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
448 if (config.portId == in_portId) {
449 // Check if the configuration was provided by the client.
450 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
451 return initialIt == initials.end() || config != *initialIt;
452 }
453 return false;
454 });
455 if (configIt != configs.end()) {
456 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
457 << configIt->id;
458 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
459 }
460 ports.erase(portIt);
461 mConnectedDevicePorts.erase(in_portId);
462 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
463
464 auto& routes = getConfig().routes;
465 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
466 if (routesIt->sinkPortId == in_portId) {
467 routesIt = routes.erase(routesIt);
468 } else {
469 // Note: the list of sourcePortIds can't become empty because there must
470 // be the id of the template port in the route.
471 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
472 ++routesIt;
473 }
474 }
475
476 return ndk::ScopedAStatus::ok();
477}
478
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000479ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
480 *_aidl_return = getConfig().patches;
481 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
482 return ndk::ScopedAStatus::ok();
483}
484
485ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
486 auto& ports = getConfig().ports;
487 auto portIt = findById<AudioPort>(ports, in_portId);
488 if (portIt != ports.end()) {
489 *_aidl_return = *portIt;
490 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
491 return ndk::ScopedAStatus::ok();
492 }
493 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
494 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
495}
496
497ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
498 *_aidl_return = getConfig().portConfigs;
499 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
500 return ndk::ScopedAStatus::ok();
501}
502
503ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
504 *_aidl_return = getConfig().ports;
505 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
506 return ndk::ScopedAStatus::ok();
507}
508
509ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
510 *_aidl_return = getConfig().routes;
511 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
512 return ndk::ScopedAStatus::ok();
513}
514
Mikhail Naganov00603d12022-05-02 22:52:13 +0000515ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
516 std::vector<AudioRoute>* _aidl_return) {
517 auto& ports = getConfig().ports;
518 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
519 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
520 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
521 }
522 auto& routes = getConfig().routes;
523 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
524 [&](const auto& r) {
525 const auto& srcs = r.sourcePortIds;
526 return r.sinkPortId == in_portId ||
527 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
528 });
529 return ndk::ScopedAStatus::ok();
530}
531
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000532ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
533 OpenInputStreamReturn* _aidl_return) {
534 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
535 << in_args.bufferSizeFrames << " frames";
536 AudioPort* port = nullptr;
537 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
538 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000539 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000540 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
541 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000542 << " does not correspond to an input mix port";
543 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
544 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000545 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000546 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
547 &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000548 !status.isOk()) {
549 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000550 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000551 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000552 std::shared_ptr<StreamIn> stream;
553 if (auto status = StreamIn::createInstance(in_args.sinkMetadata, std::move(context),
554 mConfig->microphones, &stream);
555 !status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000556 return status;
557 }
558 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000559 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
560 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000561 auto patchIt = mPatches.find(in_args.portConfigId);
562 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000563 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000564 }
565 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000566 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000567 return ndk::ScopedAStatus::ok();
568}
569
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000570ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
571 OpenOutputStreamReturn* _aidl_return) {
572 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
573 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
574 << " frames";
575 AudioPort* port = nullptr;
576 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
577 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000578 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000579 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
580 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000581 << " does not correspond to an output mix port";
582 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
583 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000584 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
585 AudioOutputFlags::COMPRESS_OFFLOAD);
586 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000587 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000588 << " has COMPRESS_OFFLOAD flag set, requires offload info";
589 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
590 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000591 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
592 AudioOutputFlags::NON_BLOCKING);
593 if (isNonBlocking && in_args.callback == nullptr) {
594 LOG(ERROR) << __func__ << ": port id " << port->id
595 << " has NON_BLOCKING flag set, requires async callback";
596 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
597 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000598 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000599 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
600 isNonBlocking ? in_args.callback : nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000601 !status.isOk()) {
602 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000603 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000604 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000605 std::shared_ptr<StreamOut> stream;
606 if (auto status = StreamOut::createInstance(in_args.sourceMetadata, std::move(context),
607 in_args.offloadInfo, &stream);
608 !status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000609 return status;
610 }
611 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000612 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
613 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000614 auto patchIt = mPatches.find(in_args.portConfigId);
615 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000616 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000617 }
618 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000619 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000620 return ndk::ScopedAStatus::ok();
621}
622
Mikhail Naganov74927202022-12-19 16:37:14 +0000623ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
624 SupportedPlaybackRateFactors* _aidl_return) {
625 LOG(DEBUG) << __func__;
626 (void)_aidl_return;
627 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
628}
629
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000630ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000631 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000632 if (in_requested.sourcePortConfigIds.empty()) {
633 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
634 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
635 }
636 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
637 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
638 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
639 }
640 if (in_requested.sinkPortConfigIds.empty()) {
641 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
642 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
643 }
644 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
645 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
646 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
647 }
648
649 auto& configs = getConfig().portConfigs;
650 std::vector<int32_t> missingIds;
651 auto sources =
652 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
653 if (!missingIds.empty()) {
654 LOG(ERROR) << __func__ << ": following source port config ids not found: "
655 << ::android::internal::ToString(missingIds);
656 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
657 }
658 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
659 if (!missingIds.empty()) {
660 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
661 << ::android::internal::ToString(missingIds);
662 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
663 }
664 // bool indicates whether a non-exclusive route is available.
665 // If only an exclusive route is available, that means the patch can not be
666 // established if there is any other patch which currently uses the sink port.
667 std::map<int32_t, bool> allowedSinkPorts;
668 auto& routes = getConfig().routes;
669 for (auto src : sources) {
670 for (const auto& r : routes) {
671 const auto& srcs = r.sourcePortIds;
672 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
673 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
674 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
675 }
676 }
677 }
678 }
679 for (auto sink : sinks) {
680 if (allowedSinkPorts.count(sink->portId) == 0) {
681 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
682 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
683 }
684 }
685
686 auto& patches = getConfig().patches;
687 auto existing = patches.end();
688 std::optional<decltype(mPatches)> patchesBackup;
689 if (in_requested.id != 0) {
690 existing = findById<AudioPatch>(patches, in_requested.id);
691 if (existing != patches.end()) {
692 patchesBackup = mPatches;
693 cleanUpPatch(existing->id);
694 } else {
695 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
696 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
697 }
698 }
699 // Validate the requested patch.
700 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
701 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
702 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
703 << "is exclusive and is already used by some other patch";
704 if (patchesBackup.has_value()) {
705 mPatches = std::move(*patchesBackup);
706 }
707 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
708 }
709 }
710 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000711 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
712 _aidl_return->latenciesMs.clear();
713 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
714 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000715 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000716 if (existing == patches.end()) {
717 _aidl_return->id = getConfig().nextPatchId++;
718 patches.push_back(*_aidl_return);
719 existing = patches.begin() + (patches.size() - 1);
720 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000721 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000722 *existing = *_aidl_return;
723 }
724 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000725 updateStreamsConnectedState(oldPatch, *_aidl_return);
726
727 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
728 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000729 return ndk::ScopedAStatus::ok();
730}
731
732ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
733 AudioPortConfig* out_suggested, bool* _aidl_return) {
734 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
735 auto& configs = getConfig().portConfigs;
736 auto existing = configs.end();
737 if (in_requested.id != 0) {
738 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
739 existing == configs.end()) {
740 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
741 << " not found";
742 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
743 }
744 }
745
746 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
747 if (portId == 0) {
748 LOG(ERROR) << __func__ << ": input port config does not specify portId";
749 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
750 }
751 auto& ports = getConfig().ports;
752 auto portIt = findById<AudioPort>(ports, portId);
753 if (portIt == ports.end()) {
754 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
755 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
756 }
757 if (existing != configs.end()) {
758 *out_suggested = *existing;
759 } else {
760 AudioPortConfig newConfig;
761 if (generateDefaultPortConfig(*portIt, &newConfig)) {
762 *out_suggested = newConfig;
763 } else {
764 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
765 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
766 }
767 }
768 // From this moment, 'out_suggested' is either an existing port config,
769 // or a new generated config. Now attempt to update it according to the specified
770 // fields of 'in_requested'.
771
772 bool requestedIsValid = true, requestedIsFullySpecified = true;
773
774 AudioIoFlags portFlags = portIt->flags;
775 if (in_requested.flags.has_value()) {
776 if (in_requested.flags.value() != portFlags) {
777 LOG(WARNING) << __func__ << ": requested flags "
778 << in_requested.flags.value().toString() << " do not match port's "
779 << portId << " flags " << portFlags.toString();
780 requestedIsValid = false;
781 }
782 } else {
783 requestedIsFullySpecified = false;
784 }
785
786 AudioProfile portProfile;
787 if (in_requested.format.has_value()) {
788 const auto& format = in_requested.format.value();
789 if (findAudioProfile(*portIt, format, &portProfile)) {
790 out_suggested->format = format;
791 } else {
792 LOG(WARNING) << __func__ << ": requested format " << format.toString()
793 << " is not found in port's " << portId << " profiles";
794 requestedIsValid = false;
795 }
796 } else {
797 requestedIsFullySpecified = false;
798 }
799 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
800 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
801 << out_suggested->format.value().toString() << " anymore";
802 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
803 }
804
805 if (in_requested.channelMask.has_value()) {
806 const auto& channelMask = in_requested.channelMask.value();
807 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
808 portProfile.channelMasks.end()) {
809 out_suggested->channelMask = channelMask;
810 } else {
811 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
812 << " is not supported for the format " << portProfile.format.toString()
813 << " by the port " << portId;
814 requestedIsValid = false;
815 }
816 } else {
817 requestedIsFullySpecified = false;
818 }
819
820 if (in_requested.sampleRate.has_value()) {
821 const auto& sampleRate = in_requested.sampleRate.value();
822 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
823 sampleRate.value) != portProfile.sampleRates.end()) {
824 out_suggested->sampleRate = sampleRate;
825 } else {
826 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
827 << " is not supported for the format " << portProfile.format.toString()
828 << " by the port " << portId;
829 requestedIsValid = false;
830 }
831 } else {
832 requestedIsFullySpecified = false;
833 }
834
835 if (in_requested.gain.has_value()) {
836 // Let's pretend that gain can always be applied.
837 out_suggested->gain = in_requested.gain.value();
838 }
839
840 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
841 out_suggested->id = getConfig().nextPortId++;
842 configs.push_back(*out_suggested);
843 *_aidl_return = true;
844 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
845 } else if (existing != configs.end() && requestedIsValid) {
846 *existing = *out_suggested;
847 *_aidl_return = true;
848 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
849 } else {
850 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
851 << "; requested is valid? " << requestedIsValid << ", fully specified? "
852 << requestedIsFullySpecified;
853 *_aidl_return = false;
854 }
855 return ndk::ScopedAStatus::ok();
856}
857
858ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
859 auto& patches = getConfig().patches;
860 auto patchIt = findById<AudioPatch>(patches, in_patchId);
861 if (patchIt != patches.end()) {
862 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000863 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000864 patches.erase(patchIt);
865 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
866 return ndk::ScopedAStatus::ok();
867 }
868 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
869 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
870}
871
872ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
873 auto& configs = getConfig().portConfigs;
874 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
875 if (configIt != configs.end()) {
876 if (mStreams.count(in_portConfigId) != 0) {
877 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
878 << " has a stream opened on it";
879 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
880 }
881 auto patchIt = mPatches.find(in_portConfigId);
882 if (patchIt != mPatches.end()) {
883 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
884 << " is used by the patch with id " << patchIt->second;
885 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
886 }
887 auto& initials = getConfig().initialConfigs;
888 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
889 if (initialIt == initials.end()) {
890 configs.erase(configIt);
891 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
892 } else if (*configIt != *initialIt) {
893 *configIt = *initialIt;
894 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
895 }
896 return ndk::ScopedAStatus::ok();
897 }
898 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
899 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
900}
901
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000902ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
903 *_aidl_return = mMasterMute;
904 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
905 return ndk::ScopedAStatus::ok();
906}
907
908ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
909 LOG(DEBUG) << __func__ << ": " << in_mute;
910 mMasterMute = in_mute;
911 return ndk::ScopedAStatus::ok();
912}
913
914ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
915 *_aidl_return = mMasterVolume;
916 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
917 return ndk::ScopedAStatus::ok();
918}
919
920ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
921 LOG(DEBUG) << __func__ << ": " << in_volume;
922 if (in_volume >= 0.0f && in_volume <= 1.0f) {
923 mMasterVolume = in_volume;
924 return ndk::ScopedAStatus::ok();
925 }
926 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
927 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
928}
929
930ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
931 *_aidl_return = mMicMute;
932 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
933 return ndk::ScopedAStatus::ok();
934}
935
936ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
937 LOG(DEBUG) << __func__ << ": " << in_mute;
938 mMicMute = in_mute;
939 return ndk::ScopedAStatus::ok();
940}
941
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000942ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
943 *_aidl_return = mConfig->microphones;
944 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
945 return ndk::ScopedAStatus::ok();
946}
947
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000948ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
949 // No checks for supported audio modes here, it's an informative notification.
950 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
951 return ndk::ScopedAStatus::ok();
952}
953
954ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
955 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
956 return ndk::ScopedAStatus::ok();
957}
958
959ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
960 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
961 return ndk::ScopedAStatus::ok();
962}
963
Vlad Popa83a6d822022-11-07 13:53:57 +0100964ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Vlad Popa943b7e22022-12-08 14:24:12 +0100965 if (mSoundDose == nullptr) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +0100966 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000967 mSoundDoseBinder = mSoundDose->asBinder();
968 AIBinder_setMinSchedulerPolicy(mSoundDoseBinder.get(), SCHED_NORMAL,
969 ANDROID_PRIORITY_AUDIO);
Vlad Popa943b7e22022-12-08 14:24:12 +0100970 }
971 *_aidl_return = mSoundDose;
972 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +0100973 return ndk::ScopedAStatus::ok();
974}
975
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000976ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
977 LOG(DEBUG) << __func__;
978 (void)_aidl_return;
979 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
980}
981
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000982const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
983
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000984ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
985 std::vector<VendorParameter>* _aidl_return) {
986 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000987 bool allParametersKnown = true;
988 for (const auto& id : in_ids) {
989 if (id == VendorDebug::kForceTransientBurstName) {
990 VendorParameter forceTransientBurst{.id = id};
991 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
992 _aidl_return->push_back(std::move(forceTransientBurst));
993 } else {
994 allParametersKnown = false;
995 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
996 }
997 }
998 if (allParametersKnown) return ndk::ScopedAStatus::ok();
999 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001000}
1001
1002ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1003 bool in_async) {
1004 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1005 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001006 bool allParametersKnown = true;
1007 for (const auto& p : in_parameters) {
1008 if (p.id == VendorDebug::kForceTransientBurstName) {
1009 std::optional<Boolean> value;
1010 binder_status_t result = p.ext.getParcelable(&value);
1011 if (result == STATUS_OK) {
1012 mVendorDebug.forceTransientBurst = value.value().value;
1013 } else {
1014 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1015 << "\"";
1016 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1017 }
1018 } else {
1019 allParametersKnown = false;
1020 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1021 }
1022 }
1023 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1024 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001025}
1026
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001027ndk::ScopedAStatus Module::addDeviceEffect(
1028 int32_t in_portConfigId,
1029 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1030 if (in_effect == nullptr) {
1031 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1032 } else {
1033 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1034 << in_effect->asBinder().get();
1035 }
1036 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1037}
1038
1039ndk::ScopedAStatus Module::removeDeviceEffect(
1040 int32_t in_portConfigId,
1041 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1042 if (in_effect == nullptr) {
1043 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1044 } else {
1045 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1046 << in_effect->asBinder().get();
1047 }
1048 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1049}
1050
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001051} // namespace aidl::android::hardware::audio::core