blob: 8a7f9f0d8de0647cdaeb361374572367998b9b1e [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"
jiabin253bd322023-01-25 23:57:31 +000030#include "core-impl/ModuleUsb.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010031#include "core-impl/SoundDose.h"
Mikhail Naganovf429c032023-01-07 00:24:50 +000032#include "core-impl/StreamStub.h"
jiabin253bd322023-01-25 23:57:31 +000033#include "core-impl/StreamUsb.h"
Mikhail Naganov3b125b72022-10-05 02:12:39 +000034#include "core-impl/Telephony.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000035#include "core-impl/utils.h"
36
Mikhail Naganov872d4a62023-03-09 18:19:01 -080037using aidl::android::hardware::audio::common::getFrameSizeInBytes;
38using aidl::android::hardware::audio::common::isBitPositionFlagSet;
39using aidl::android::hardware::audio::common::isValidAudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000040using aidl::android::hardware::audio::common::SinkMetadata;
41using aidl::android::hardware::audio::common::SourceMetadata;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010042using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000043using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000044using aidl::android::media::audio::common::AudioDevice;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000045using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000046using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000047using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000048using aidl::android::media::audio::common::AudioIoFlags;
jiabin9a8e6862023-01-12 23:06:37 +000049using aidl::android::media::audio::common::AudioMMapPolicy;
50using aidl::android::media::audio::common::AudioMMapPolicyInfo;
51using aidl::android::media::audio::common::AudioMMapPolicyType;
Mikhail Naganov04ae8222023-01-11 15:48:10 -080052using aidl::android::media::audio::common::AudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000053using aidl::android::media::audio::common::AudioOffloadInfo;
54using aidl::android::media::audio::common::AudioOutputFlags;
55using aidl::android::media::audio::common::AudioPort;
56using aidl::android::media::audio::common::AudioPortConfig;
57using aidl::android::media::audio::common::AudioPortExt;
58using aidl::android::media::audio::common::AudioProfile;
Mikhail Naganov20047bc2023-01-05 20:16:07 +000059using aidl::android::media::audio::common::Boolean;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000060using aidl::android::media::audio::common::Int;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080061using aidl::android::media::audio::common::MicrophoneInfo;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000062using aidl::android::media::audio::common::PcmType;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000063
64namespace aidl::android::hardware::audio::core {
65
66namespace {
67
68bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
69 *config = {};
70 config->portId = port.id;
71 if (port.profiles.empty()) {
72 LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles";
73 return false;
74 }
75 const auto& profile = port.profiles.begin();
76 config->format = profile->format;
77 if (profile->channelMasks.empty()) {
78 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
79 << " has no channel masks";
80 return false;
81 }
82 config->channelMask = *profile->channelMasks.begin();
83 if (profile->sampleRates.empty()) {
84 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
85 << " has no sample rates";
86 return false;
87 }
88 Int sampleRate;
89 sampleRate.value = *profile->sampleRates.begin();
90 config->sampleRate = sampleRate;
91 config->flags = port.flags;
92 config->ext = port.ext;
93 return true;
94}
95
96bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
97 AudioProfile* profile) {
98 if (auto profilesIt =
99 find_if(port.profiles.begin(), port.profiles.end(),
100 [&format](const auto& profile) { return profile.format == format; });
101 profilesIt != port.profiles.end()) {
102 *profile = *profilesIt;
103 return true;
104 }
105 return false;
106}
Mikhail Naganov00603d12022-05-02 22:52:13 +0000107
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000108} // namespace
109
jiabin253bd322023-01-25 23:57:31 +0000110// static
111std::shared_ptr<Module> Module::createInstance(Type type) {
112 switch (type) {
113 case Module::Type::USB:
114 return ndk::SharedRefBase::make<ModuleUsb>(type);
115 case Type::DEFAULT:
116 case Type::R_SUBMIX:
117 default:
118 return ndk::SharedRefBase::make<Module>(type);
119 }
120}
121
122// static
123StreamIn::CreateInstance Module::getStreamInCreator(Type type) {
124 switch (type) {
125 case Type::USB:
126 return StreamInUsb::createInstance;
127 case Type::DEFAULT:
128 case Type::R_SUBMIX:
129 default:
130 return StreamInStub::createInstance;
131 }
132}
133
134// static
135StreamOut::CreateInstance Module::getStreamOutCreator(Type type) {
136 switch (type) {
137 case Type::USB:
138 return StreamOutUsb::createInstance;
139 case Type::DEFAULT:
140 case Type::R_SUBMIX:
141 default:
142 return StreamOutStub::createInstance;
143 }
144}
145
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700146std::ostream& operator<<(std::ostream& os, Module::Type t) {
147 switch (t) {
148 case Module::Type::DEFAULT:
149 os << "default";
150 break;
151 case Module::Type::R_SUBMIX:
152 os << "r_submix";
153 break;
154 case Module::Type::USB:
155 os << "usb";
156 break;
157 }
158 return os;
159}
160
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000161void Module::cleanUpPatch(int32_t patchId) {
162 erase_all_values(mPatches, std::set<int32_t>{patchId});
163}
164
Mikhail Naganov8651b362023-01-06 23:15:27 +0000165ndk::ScopedAStatus Module::createStreamContext(
166 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
167 std::shared_ptr<IStreamCallback> asyncCallback,
168 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000169 if (in_bufferSizeFrames <= 0) {
170 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
171 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
172 }
173 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
174 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
175 << ", must be at least " << kMinimumStreamBufferSizeFrames;
176 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
177 }
178 auto& configs = getConfig().portConfigs;
179 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000180 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000181 // validity of the portConfigId has already been checked.
182 const size_t frameSize =
183 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
184 if (frameSize == 0) {
185 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
186 << portConfigIt->toString();
187 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
188 }
189 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700190 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000191 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
192 << " frames is too large, maximum size is "
193 << kMaximumStreamBufferSizeBytes / frameSize;
194 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
195 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000196 const auto& flags = portConfigIt->flags.value();
197 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000198 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
199 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000200 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000201 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
202 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000203 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
Mikhail Naganov194daaa2023-01-05 22:34:20 +0000204 mVendorDebug.forceTransientBurst,
205 mVendorDebug.forceSynchronousDrain};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000206 StreamContext temp(
207 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
208 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000209 portConfigIt->format.value(), portConfigIt->channelMask.value(),
Mikhail Naganovb42a69e2023-06-16 12:38:25 -0700210 portConfigIt->sampleRate.value().value, flags,
211 portConfigIt->ext.get<AudioPortExt::mix>().handle,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000212 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov8651b362023-01-06 23:15:27 +0000213 asyncCallback, outEventCallback, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000214 if (temp.isValid()) {
215 *out_context = std::move(temp);
216 } else {
217 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
218 }
219 } else {
220 // TODO: Implement simulation of MMAP buffer allocation
221 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000222 return ndk::ScopedAStatus::ok();
223}
224
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000225std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
226 std::vector<AudioDevice> result;
227 auto& ports = getConfig().ports;
228 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
229 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
230 auto portIt = findById<AudioPort>(ports, *it);
231 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
232 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
233 }
234 }
235 return result;
236}
237
238std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
239 std::set<int32_t> result;
240 auto patchIdsRange = mPatches.equal_range(portConfigId);
241 auto& patches = getConfig().patches;
242 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
243 auto patchIt = findById<AudioPatch>(patches, it->second);
244 if (patchIt == patches.end()) {
245 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
246 << "not found in the configuration";
247 }
248 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
249 portConfigId) != patchIt->sourcePortConfigIds.end()) {
250 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
251 } else {
252 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
253 }
254 }
255 return result;
256}
257
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000258ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
259 auto& configs = getConfig().portConfigs;
260 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
261 if (portConfigIt == configs.end()) {
262 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
263 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
264 }
265 const int32_t portId = portConfigIt->portId;
266 // In our implementation, configs of mix ports always have unique IDs.
267 CHECK(portId != in_portConfigId);
268 auto& ports = getConfig().ports;
269 auto portIt = findById<AudioPort>(ports, portId);
270 if (portIt == ports.end()) {
271 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
272 << in_portConfigId << " not found";
273 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
274 }
275 if (mStreams.count(in_portConfigId) != 0) {
276 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
277 << " already has a stream opened on it";
278 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
279 }
280 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
281 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
282 << " does not correspond to a mix port";
283 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
284 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700285 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000286 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
287 LOG(ERROR) << __func__ << ": port id " << portId
288 << " has already reached maximum allowed opened stream count: "
289 << maxOpenStreamCount;
290 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
291 }
292 *port = &(*portIt);
293 return ndk::ScopedAStatus::ok();
294}
295
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000296template <typename C>
297std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
298 std::set<int32_t> result;
299 auto& portConfigs = getConfig().portConfigs;
300 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
301 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
302 if (portConfigIt != portConfigs.end()) {
303 result.insert(portConfigIt->portId);
304 }
305 }
306 return result;
307}
308
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000309internal::Configuration& Module::getConfig() {
310 if (!mConfig) {
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000311 switch (mType) {
312 case Type::DEFAULT:
313 mConfig = std::move(internal::getPrimaryConfiguration());
314 break;
315 case Type::R_SUBMIX:
316 mConfig = std::move(internal::getRSubmixConfiguration());
317 break;
jiabinb309d8d2023-01-20 19:07:15 +0000318 case Type::USB:
319 mConfig = std::move(internal::getUsbConfiguration());
jiabin253bd322023-01-25 23:57:31 +0000320 break;
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000321 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000322 }
323 return *mConfig;
324}
325
326void Module::registerPatch(const AudioPatch& patch) {
327 auto& configs = getConfig().portConfigs;
328 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
329 for (auto portConfigId : portConfigIds) {
330 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
331 if (configIt != configs.end()) {
332 mPatches.insert(std::pair{portConfigId, patch.id});
333 if (configIt->portId != portConfigId) {
334 mPatches.insert(std::pair{configIt->portId, patch.id});
335 }
336 }
337 };
338 };
339 do_insert(patch.sourcePortConfigIds);
340 do_insert(patch.sinkPortConfigIds);
341}
342
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000343void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) {
344 // Streams from the old patch need to be disconnected, streams from the new
345 // patch need to be connected. If the stream belongs to both patches, no need
346 // to update it.
347 std::set<int32_t> idsToDisconnect, idsToConnect;
348 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
349 oldPatch.sourcePortConfigIds.end());
350 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
351 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
352 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
353 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
354 if (idsToConnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000355 LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected";
356 mStreams.setStreamIsConnected(portConfigId, {});
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000357 }
358 });
359 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
360 if (idsToDisconnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000361 const auto connectedDevices = findConnectedDevices(portConfigId);
362 LOG(DEBUG) << "The stream on port config id " << portConfigId
363 << " is connected to: " << ::android::internal::ToString(connectedDevices);
364 mStreams.setStreamIsConnected(portConfigId, connectedDevices);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000365 }
366 });
367}
368
Mikhail Naganov00603d12022-05-02 22:52:13 +0000369ndk::ScopedAStatus Module::setModuleDebug(
370 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700371 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000372 << ", new flags: " << in_debug.toString();
373 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
374 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700375 LOG(ERROR) << __func__ << ": " << mType
376 << ": attempting to change device connections simulation while having external "
377 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000378 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
379 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000380 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700381 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000382 << in_debug.streamTransientStateDelayMs;
383 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
384 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000385 mDebug = in_debug;
386 return ndk::ScopedAStatus::ok();
387}
388
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000389ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800390 if (!mTelephony) {
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000391 mTelephony = ndk::SharedRefBase::make<Telephony>();
392 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800393 *_aidl_return = mTelephony.getPtr();
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000394 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
395 return ndk::ScopedAStatus::ok();
396}
397
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000398ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800399 if (!mBluetooth) {
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000400 mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000401 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800402 *_aidl_return = mBluetooth.getPtr();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000403 LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get();
404 return ndk::ScopedAStatus::ok();
405}
406
Mikhail Naganov7499a002023-02-27 18:51:44 -0800407ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
408 if (!mBluetoothA2dp) {
409 mBluetoothA2dp = ndk::SharedRefBase::make<BluetoothA2dp>();
410 }
411 *_aidl_return = mBluetoothA2dp.getPtr();
412 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothA2dp: " << _aidl_return->get();
413 return ndk::ScopedAStatus::ok();
414}
415
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800416ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
417 if (!mBluetoothLe) {
418 mBluetoothLe = ndk::SharedRefBase::make<BluetoothLe>();
419 }
420 *_aidl_return = mBluetoothLe.getPtr();
421 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothLe: " << _aidl_return->get();
422 return ndk::ScopedAStatus::ok();
423}
424
Mikhail Naganov00603d12022-05-02 22:52:13 +0000425ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
426 AudioPort* _aidl_return) {
427 const int32_t templateId = in_templateIdAndAdditionalData.id;
428 auto& ports = getConfig().ports;
429 AudioPort connectedPort;
430 { // Scope the template port so that we don't accidentally modify it.
431 auto templateIt = findById<AudioPort>(ports, templateId);
432 if (templateIt == ports.end()) {
433 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
434 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
435 }
436 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
437 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
438 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
439 }
440 if (!templateIt->profiles.empty()) {
441 LOG(ERROR) << __func__ << ": port id " << templateId
442 << " does not have dynamic profiles";
443 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
444 }
445 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
446 if (templateDevicePort.device.type.connection.empty()) {
447 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
448 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
449 }
450 // Postpone id allocation until we ensure that there are no client errors.
451 connectedPort = *templateIt;
452 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
453 const auto& inputDevicePort =
454 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
455 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
456 connectedDevicePort.device.address = inputDevicePort.device.address;
457 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
458 << connectedDevicePort.device.toString();
459 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700460 for (auto connectedPortPair : mConnectedDevicePorts) {
461 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000462 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
463 connectedDevicePort.device) {
464 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700465 << " is already connected at the device port id "
466 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000467 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
468 }
469 }
470 }
471
472 if (!mDebug.simulateDeviceConnections) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700473 if (ndk::ScopedAStatus status = populateConnectedDevicePort(&connectedPort);
474 !status.isOk()) {
475 return status;
476 }
477 } else {
478 auto& connectedProfiles = getConfig().connectedProfiles;
479 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
480 connectedProfilesIt != connectedProfiles.end()) {
481 connectedPort.profiles = connectedProfilesIt->second;
482 }
483 }
484 if (connectedPort.profiles.empty()) {
485 LOG(ERROR) << "Profiles of a connected port still empty after connecting external device "
486 << connectedPort.toString();
Mikhail Naganov00603d12022-05-02 22:52:13 +0000487 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
488 }
489
490 connectedPort.id = ++getConfig().nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700491 auto [connectedPortsIt, _] =
492 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::vector<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000493 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
494 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000495 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000496 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000497
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700498 std::vector<int32_t> routablePortIds;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000499 std::vector<AudioRoute> newRoutes;
500 auto& routes = getConfig().routes;
501 for (auto& r : routes) {
502 if (r.sinkPortId == templateId) {
503 AudioRoute newRoute;
504 newRoute.sourcePortIds = r.sourcePortIds;
505 newRoute.sinkPortId = connectedPort.id;
506 newRoute.isExclusive = r.isExclusive;
507 newRoutes.push_back(std::move(newRoute));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700508 routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(),
509 r.sourcePortIds.end());
Mikhail Naganov00603d12022-05-02 22:52:13 +0000510 } else {
511 auto& srcs = r.sourcePortIds;
512 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
513 srcs.push_back(connectedPort.id);
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700514 routablePortIds.push_back(r.sinkPortId);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000515 }
516 }
517 }
518 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
519
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700520 // Note: this is a simplistic approach assuming that a mix port can only be populated
521 // from a single device port. Implementing support for stuffing dynamic profiles with a superset
522 // of all profiles from all routable dynamic device ports would be more involved.
523 for (const auto mixPortId : routablePortIds) {
524 auto portsIt = findById<AudioPort>(ports, mixPortId);
525 if (portsIt != ports.end() && portsIt->profiles.empty()) {
526 portsIt->profiles = connectedPort.profiles;
527 connectedPortsIt->second.push_back(portsIt->id);
528 }
529 }
530 *_aidl_return = std::move(connectedPort);
531
Mikhail Naganov00603d12022-05-02 22:52:13 +0000532 return ndk::ScopedAStatus::ok();
533}
534
535ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
536 auto& ports = getConfig().ports;
537 auto portIt = findById<AudioPort>(ports, in_portId);
538 if (portIt == ports.end()) {
539 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
540 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
541 }
542 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
543 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
544 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
545 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700546 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
547 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000548 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
549 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
550 }
551 auto& configs = getConfig().portConfigs;
552 auto& initials = getConfig().initialConfigs;
553 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
554 if (config.portId == in_portId) {
555 // Check if the configuration was provided by the client.
556 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
557 return initialIt == initials.end() || config != *initialIt;
558 }
559 return false;
560 });
561 if (configIt != configs.end()) {
562 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
563 << configIt->id;
564 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
565 }
jiabin783c48b2023-02-28 18:28:06 +0000566 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000567 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000568 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
569
570 auto& routes = getConfig().routes;
571 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
572 if (routesIt->sinkPortId == in_portId) {
573 routesIt = routes.erase(routesIt);
574 } else {
575 // Note: the list of sourcePortIds can't become empty because there must
576 // be the id of the template port in the route.
577 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
578 ++routesIt;
579 }
580 }
581
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700582 for (const auto mixPortId : connectedPortsIt->second) {
583 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
584 if (mixPortIt != ports.end()) {
585 mixPortIt->profiles = {};
586 }
587 }
588 mConnectedDevicePorts.erase(connectedPortsIt);
589
Mikhail Naganov00603d12022-05-02 22:52:13 +0000590 return ndk::ScopedAStatus::ok();
591}
592
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000593ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
594 *_aidl_return = getConfig().patches;
595 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
596 return ndk::ScopedAStatus::ok();
597}
598
599ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
600 auto& ports = getConfig().ports;
601 auto portIt = findById<AudioPort>(ports, in_portId);
602 if (portIt != ports.end()) {
603 *_aidl_return = *portIt;
604 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
605 return ndk::ScopedAStatus::ok();
606 }
607 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
608 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
609}
610
611ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
612 *_aidl_return = getConfig().portConfigs;
613 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
614 return ndk::ScopedAStatus::ok();
615}
616
617ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
618 *_aidl_return = getConfig().ports;
619 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
620 return ndk::ScopedAStatus::ok();
621}
622
623ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
624 *_aidl_return = getConfig().routes;
625 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
626 return ndk::ScopedAStatus::ok();
627}
628
Mikhail Naganov00603d12022-05-02 22:52:13 +0000629ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
630 std::vector<AudioRoute>* _aidl_return) {
631 auto& ports = getConfig().ports;
632 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
633 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
634 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
635 }
636 auto& routes = getConfig().routes;
637 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
638 [&](const auto& r) {
639 const auto& srcs = r.sourcePortIds;
640 return r.sinkPortId == in_portId ||
641 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
642 });
643 return ndk::ScopedAStatus::ok();
644}
645
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000646ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
647 OpenInputStreamReturn* _aidl_return) {
648 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
649 << in_args.bufferSizeFrames << " frames";
650 AudioPort* port = nullptr;
651 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
652 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000653 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000654 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
655 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000656 << " does not correspond to an input mix port";
657 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
658 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000659 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000660 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000661 nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000662 !status.isOk()) {
663 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000664 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000665 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000666 std::shared_ptr<StreamIn> stream;
jiabin253bd322023-01-25 23:57:31 +0000667 ndk::ScopedAStatus status = getStreamInCreator(mType)(in_args.sinkMetadata, std::move(context),
668 mConfig->microphones, &stream);
669 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000670 return status;
671 }
672 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000673 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
674 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000675 auto patchIt = mPatches.find(in_args.portConfigId);
676 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000677 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000678 }
679 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000680 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000681 return ndk::ScopedAStatus::ok();
682}
683
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000684ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
685 OpenOutputStreamReturn* _aidl_return) {
686 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
687 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
688 << " frames";
689 AudioPort* port = nullptr;
690 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
691 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000692 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000693 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
694 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000695 << " does not correspond to an output mix port";
696 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
697 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000698 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
699 AudioOutputFlags::COMPRESS_OFFLOAD);
700 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000701 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000702 << " has COMPRESS_OFFLOAD flag set, requires offload info";
703 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
704 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000705 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
706 AudioOutputFlags::NON_BLOCKING);
707 if (isNonBlocking && in_args.callback == nullptr) {
708 LOG(ERROR) << __func__ << ": port id " << port->id
709 << " has NON_BLOCKING flag set, requires async callback";
710 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
711 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000712 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000713 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000714 isNonBlocking ? in_args.callback : nullptr,
715 in_args.eventCallback, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000716 !status.isOk()) {
717 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000718 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000719 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000720 std::shared_ptr<StreamOut> stream;
jiabin253bd322023-01-25 23:57:31 +0000721 ndk::ScopedAStatus status = getStreamOutCreator(mType)(
722 in_args.sourceMetadata, std::move(context), in_args.offloadInfo, &stream);
723 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000724 return status;
725 }
726 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000727 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
728 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000729 auto patchIt = mPatches.find(in_args.portConfigId);
730 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000731 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000732 }
733 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000734 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000735 return ndk::ScopedAStatus::ok();
736}
737
Mikhail Naganov74927202022-12-19 16:37:14 +0000738ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
739 SupportedPlaybackRateFactors* _aidl_return) {
740 LOG(DEBUG) << __func__;
741 (void)_aidl_return;
742 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
743}
744
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000745ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000746 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000747 if (in_requested.sourcePortConfigIds.empty()) {
748 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
749 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
750 }
751 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
752 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
753 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
754 }
755 if (in_requested.sinkPortConfigIds.empty()) {
756 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
757 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
758 }
759 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
760 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
761 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
762 }
763
764 auto& configs = getConfig().portConfigs;
765 std::vector<int32_t> missingIds;
766 auto sources =
767 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
768 if (!missingIds.empty()) {
769 LOG(ERROR) << __func__ << ": following source port config ids not found: "
770 << ::android::internal::ToString(missingIds);
771 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
772 }
773 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
774 if (!missingIds.empty()) {
775 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
776 << ::android::internal::ToString(missingIds);
777 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
778 }
779 // bool indicates whether a non-exclusive route is available.
780 // If only an exclusive route is available, that means the patch can not be
781 // established if there is any other patch which currently uses the sink port.
782 std::map<int32_t, bool> allowedSinkPorts;
783 auto& routes = getConfig().routes;
784 for (auto src : sources) {
785 for (const auto& r : routes) {
786 const auto& srcs = r.sourcePortIds;
787 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
788 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
789 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
790 }
791 }
792 }
793 }
794 for (auto sink : sinks) {
795 if (allowedSinkPorts.count(sink->portId) == 0) {
796 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
797 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
798 }
799 }
800
jiabin253bd322023-01-25 23:57:31 +0000801 if (auto status = checkAudioPatchEndpointsMatch(sources, sinks); !status.isOk()) {
802 return status;
803 }
804
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000805 auto& patches = getConfig().patches;
806 auto existing = patches.end();
807 std::optional<decltype(mPatches)> patchesBackup;
808 if (in_requested.id != 0) {
809 existing = findById<AudioPatch>(patches, in_requested.id);
810 if (existing != patches.end()) {
811 patchesBackup = mPatches;
812 cleanUpPatch(existing->id);
813 } else {
814 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
815 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
816 }
817 }
818 // Validate the requested patch.
819 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
820 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
821 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
822 << "is exclusive and is already used by some other patch";
823 if (patchesBackup.has_value()) {
824 mPatches = std::move(*patchesBackup);
825 }
826 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
827 }
828 }
829 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000830 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
831 _aidl_return->latenciesMs.clear();
832 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
833 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000834 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000835 if (existing == patches.end()) {
836 _aidl_return->id = getConfig().nextPatchId++;
837 patches.push_back(*_aidl_return);
838 existing = patches.begin() + (patches.size() - 1);
839 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000840 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000841 *existing = *_aidl_return;
842 }
843 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000844 updateStreamsConnectedState(oldPatch, *_aidl_return);
845
846 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
847 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000848 return ndk::ScopedAStatus::ok();
849}
850
851ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
852 AudioPortConfig* out_suggested, bool* _aidl_return) {
853 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
854 auto& configs = getConfig().portConfigs;
855 auto existing = configs.end();
856 if (in_requested.id != 0) {
857 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
858 existing == configs.end()) {
859 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
860 << " not found";
861 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
862 }
863 }
864
865 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
866 if (portId == 0) {
867 LOG(ERROR) << __func__ << ": input port config does not specify portId";
868 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
869 }
870 auto& ports = getConfig().ports;
871 auto portIt = findById<AudioPort>(ports, portId);
872 if (portIt == ports.end()) {
873 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
874 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
875 }
876 if (existing != configs.end()) {
877 *out_suggested = *existing;
878 } else {
879 AudioPortConfig newConfig;
880 if (generateDefaultPortConfig(*portIt, &newConfig)) {
881 *out_suggested = newConfig;
882 } else {
883 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
884 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
885 }
886 }
887 // From this moment, 'out_suggested' is either an existing port config,
888 // or a new generated config. Now attempt to update it according to the specified
889 // fields of 'in_requested'.
890
891 bool requestedIsValid = true, requestedIsFullySpecified = true;
892
893 AudioIoFlags portFlags = portIt->flags;
894 if (in_requested.flags.has_value()) {
895 if (in_requested.flags.value() != portFlags) {
896 LOG(WARNING) << __func__ << ": requested flags "
897 << in_requested.flags.value().toString() << " do not match port's "
898 << portId << " flags " << portFlags.toString();
899 requestedIsValid = false;
900 }
901 } else {
902 requestedIsFullySpecified = false;
903 }
904
905 AudioProfile portProfile;
906 if (in_requested.format.has_value()) {
907 const auto& format = in_requested.format.value();
908 if (findAudioProfile(*portIt, format, &portProfile)) {
909 out_suggested->format = format;
910 } else {
911 LOG(WARNING) << __func__ << ": requested format " << format.toString()
912 << " is not found in port's " << portId << " profiles";
913 requestedIsValid = false;
914 }
915 } else {
916 requestedIsFullySpecified = false;
917 }
918 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
919 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
920 << out_suggested->format.value().toString() << " anymore";
921 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
922 }
923
924 if (in_requested.channelMask.has_value()) {
925 const auto& channelMask = in_requested.channelMask.value();
926 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
927 portProfile.channelMasks.end()) {
928 out_suggested->channelMask = channelMask;
929 } else {
930 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
931 << " is not supported for the format " << portProfile.format.toString()
932 << " by the port " << portId;
933 requestedIsValid = false;
934 }
935 } else {
936 requestedIsFullySpecified = false;
937 }
938
939 if (in_requested.sampleRate.has_value()) {
940 const auto& sampleRate = in_requested.sampleRate.value();
941 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
942 sampleRate.value) != portProfile.sampleRates.end()) {
943 out_suggested->sampleRate = sampleRate;
944 } else {
945 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
946 << " is not supported for the format " << portProfile.format.toString()
947 << " by the port " << portId;
948 requestedIsValid = false;
949 }
950 } else {
951 requestedIsFullySpecified = false;
952 }
953
954 if (in_requested.gain.has_value()) {
955 // Let's pretend that gain can always be applied.
956 out_suggested->gain = in_requested.gain.value();
957 }
958
Mikhail Naganov248e9502023-02-21 16:32:40 -0800959 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
960 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
961 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
962 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
963 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
964 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
965 }
966 } else {
967 LOG(WARNING) << __func__ << ": requested ext tag "
968 << toString(in_requested.ext.getTag()) << " do not match port's tag "
969 << toString(out_suggested->ext.getTag());
970 requestedIsValid = false;
971 }
972 }
973
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000974 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
975 out_suggested->id = getConfig().nextPortId++;
976 configs.push_back(*out_suggested);
977 *_aidl_return = true;
978 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
979 } else if (existing != configs.end() && requestedIsValid) {
980 *existing = *out_suggested;
981 *_aidl_return = true;
982 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
983 } else {
984 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
985 << "; requested is valid? " << requestedIsValid << ", fully specified? "
986 << requestedIsFullySpecified;
987 *_aidl_return = false;
988 }
989 return ndk::ScopedAStatus::ok();
990}
991
992ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
993 auto& patches = getConfig().patches;
994 auto patchIt = findById<AudioPatch>(patches, in_patchId);
995 if (patchIt != patches.end()) {
996 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000997 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000998 patches.erase(patchIt);
999 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1000 return ndk::ScopedAStatus::ok();
1001 }
1002 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1003 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1004}
1005
1006ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1007 auto& configs = getConfig().portConfigs;
1008 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1009 if (configIt != configs.end()) {
1010 if (mStreams.count(in_portConfigId) != 0) {
1011 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1012 << " has a stream opened on it";
1013 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1014 }
1015 auto patchIt = mPatches.find(in_portConfigId);
1016 if (patchIt != mPatches.end()) {
1017 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1018 << " is used by the patch with id " << patchIt->second;
1019 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1020 }
1021 auto& initials = getConfig().initialConfigs;
1022 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1023 if (initialIt == initials.end()) {
1024 configs.erase(configIt);
1025 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1026 } else if (*configIt != *initialIt) {
1027 *configIt = *initialIt;
1028 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1029 }
1030 return ndk::ScopedAStatus::ok();
1031 }
1032 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1033 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1034}
1035
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001036ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1037 *_aidl_return = mMasterMute;
1038 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1039 return ndk::ScopedAStatus::ok();
1040}
1041
1042ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1043 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001044 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1045 : onMasterMuteChanged(in_mute);
1046 if (result.isOk()) {
1047 mMasterMute = in_mute;
1048 } else {
1049 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1050 << "), error=" << result;
1051 // Reset master mute if it failed.
1052 onMasterMuteChanged(mMasterMute);
1053 }
1054 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001055}
1056
1057ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1058 *_aidl_return = mMasterVolume;
1059 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1060 return ndk::ScopedAStatus::ok();
1061}
1062
1063ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1064 LOG(DEBUG) << __func__ << ": " << in_volume;
1065 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001066 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1067 : onMasterVolumeChanged(in_volume);
1068 if (result.isOk()) {
1069 mMasterVolume = in_volume;
1070 } else {
1071 // Reset master volume if it failed.
1072 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1073 << "), error=" << result;
1074 onMasterVolumeChanged(mMasterVolume);
1075 }
1076 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001077 }
1078 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1079 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1080}
1081
1082ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1083 *_aidl_return = mMicMute;
1084 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1085 return ndk::ScopedAStatus::ok();
1086}
1087
1088ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1089 LOG(DEBUG) << __func__ << ": " << in_mute;
1090 mMicMute = in_mute;
1091 return ndk::ScopedAStatus::ok();
1092}
1093
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001094ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001095 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001096 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1097 return ndk::ScopedAStatus::ok();
1098}
1099
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001100ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001101 if (!isValidAudioMode(in_mode)) {
1102 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1103 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1104 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001105 // No checks for supported audio modes here, it's an informative notification.
1106 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1107 return ndk::ScopedAStatus::ok();
1108}
1109
1110ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1111 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1112 return ndk::ScopedAStatus::ok();
1113}
1114
1115ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1116 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1117 return ndk::ScopedAStatus::ok();
1118}
1119
Vlad Popa83a6d822022-11-07 13:53:57 +01001120ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001121 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001122 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001123 }
Mikhail Naganov7499a002023-02-27 18:51:44 -08001124 *_aidl_return = mSoundDose.getPtr();
Vlad Popa943b7e22022-12-08 14:24:12 +01001125 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001126 return ndk::ScopedAStatus::ok();
1127}
1128
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001129ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1130 LOG(DEBUG) << __func__;
1131 (void)_aidl_return;
1132 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1133}
1134
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001135const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001136const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001137
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001138ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1139 std::vector<VendorParameter>* _aidl_return) {
1140 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001141 bool allParametersKnown = true;
1142 for (const auto& id : in_ids) {
1143 if (id == VendorDebug::kForceTransientBurstName) {
1144 VendorParameter forceTransientBurst{.id = id};
1145 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1146 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001147 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1148 VendorParameter forceSynchronousDrain{.id = id};
1149 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1150 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001151 } else {
1152 allParametersKnown = false;
1153 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1154 }
1155 }
1156 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1157 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001158}
1159
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001160namespace {
1161
1162template <typename W>
1163bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1164 std::optional<W> value;
1165 binder_status_t result = p.ext.getParcelable(&value);
1166 if (result == STATUS_OK && value.has_value()) {
1167 *v = value.value().value;
1168 return true;
1169 }
1170 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1171 << "\": " << result;
1172 return false;
1173}
1174
1175} // namespace
1176
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001177ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1178 bool in_async) {
1179 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1180 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001181 bool allParametersKnown = true;
1182 for (const auto& p : in_parameters) {
1183 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001184 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1185 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1186 }
1187 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1188 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001189 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1190 }
1191 } else {
1192 allParametersKnown = false;
1193 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1194 }
1195 }
1196 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1197 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001198}
1199
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001200ndk::ScopedAStatus Module::addDeviceEffect(
1201 int32_t in_portConfigId,
1202 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1203 if (in_effect == nullptr) {
1204 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1205 } else {
1206 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1207 << in_effect->asBinder().get();
1208 }
1209 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1210}
1211
1212ndk::ScopedAStatus Module::removeDeviceEffect(
1213 int32_t in_portConfigId,
1214 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1215 if (in_effect == nullptr) {
1216 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1217 } else {
1218 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1219 << in_effect->asBinder().get();
1220 }
1221 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1222}
1223
jiabin9a8e6862023-01-12 23:06:37 +00001224ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1225 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1226 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1227 std::set<int32_t> mmapSinks;
1228 std::set<int32_t> mmapSources;
1229 auto& ports = getConfig().ports;
1230 for (const auto& port : ports) {
1231 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1232 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1233 AudioInputFlags::MMAP_NOIRQ)) {
1234 mmapSinks.insert(port.id);
1235 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1236 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1237 AudioOutputFlags::MMAP_NOIRQ)) {
1238 mmapSources.insert(port.id);
1239 }
1240 }
1241 for (const auto& route : getConfig().routes) {
1242 if (mmapSinks.count(route.sinkPortId) != 0) {
1243 // The sink is a mix port, add the sources if they are device ports.
1244 for (int sourcePortId : route.sourcePortIds) {
1245 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1246 if (sourcePortIt == ports.end()) {
1247 // This must not happen
1248 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1249 continue;
1250 }
1251 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1252 // The source is not a device port, skip
1253 continue;
1254 }
1255 AudioMMapPolicyInfo policyInfo;
1256 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1257 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1258 // default implementation.
1259 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1260 _aidl_return->push_back(policyInfo);
1261 }
1262 } else {
1263 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1264 if (sinkPortIt == ports.end()) {
1265 // This must not happen
1266 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1267 continue;
1268 }
1269 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1270 // The sink is not a device port, skip
1271 continue;
1272 }
1273 if (count_any(mmapSources, route.sourcePortIds)) {
1274 AudioMMapPolicyInfo policyInfo;
1275 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1276 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1277 // default implementation.
1278 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1279 _aidl_return->push_back(policyInfo);
1280 }
1281 }
1282 }
1283 return ndk::ScopedAStatus::ok();
1284}
1285
Eric Laurente2432ea2023-01-12 17:47:31 +01001286ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1287 LOG(DEBUG) << __func__;
1288 *_aidl_return = false;
1289 return ndk::ScopedAStatus::ok();
1290}
1291
jiabinb76981e2023-01-18 00:58:30 +00001292ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1293 if (!isMmapSupported()) {
1294 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1295 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1296 }
1297 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1298 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1299 return ndk::ScopedAStatus::ok();
1300}
1301
1302ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1303 if (!isMmapSupported()) {
1304 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1305 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1306 }
1307 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1308 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1309 return ndk::ScopedAStatus::ok();
1310}
1311
1312bool Module::isMmapSupported() {
1313 if (mIsMmapSupported.has_value()) {
1314 return mIsMmapSupported.value();
1315 }
1316 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1317 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1318 mIsMmapSupported = false;
1319 } else {
1320 mIsMmapSupported =
1321 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1322 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1323 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1324 }) != mmapPolicyInfos.end();
1325 }
1326 return mIsMmapSupported.value();
1327}
1328
jiabin253bd322023-01-25 23:57:31 +00001329ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001330 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001331 return ndk::ScopedAStatus::ok();
1332}
1333
1334ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1335 const std::vector<AudioPortConfig*>& sources __unused,
1336 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001337 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001338 return ndk::ScopedAStatus::ok();
1339}
1340
jiabin783c48b2023-02-28 18:28:06 +00001341void Module::onExternalDeviceConnectionChanged(
1342 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1343 bool connected __unused) {
1344 LOG(DEBUG) << __func__ << ": do nothing and return";
1345}
1346
1347ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1348 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1349 return ndk::ScopedAStatus::ok();
1350}
1351
1352ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1353 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1354 return ndk::ScopedAStatus::ok();
1355}
1356
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001357} // namespace aidl::android::hardware::audio::core