blob: 984b9a101451d8c6a5602724d6c3d36fdeebd980 [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 Naganovdf5adfd2021-11-11 22:09:22 +0000146void Module::cleanUpPatch(int32_t patchId) {
147 erase_all_values(mPatches, std::set<int32_t>{patchId});
148}
149
Mikhail Naganov8651b362023-01-06 23:15:27 +0000150ndk::ScopedAStatus Module::createStreamContext(
151 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
152 std::shared_ptr<IStreamCallback> asyncCallback,
153 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000154 if (in_bufferSizeFrames <= 0) {
155 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
156 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
157 }
158 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
159 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
160 << ", must be at least " << kMinimumStreamBufferSizeFrames;
161 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
162 }
163 auto& configs = getConfig().portConfigs;
164 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000165 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000166 // validity of the portConfigId has already been checked.
167 const size_t frameSize =
168 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
169 if (frameSize == 0) {
170 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
171 << portConfigIt->toString();
172 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
173 }
174 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
175 if (frameSize > kMaximumStreamBufferSizeBytes / in_bufferSizeFrames) {
176 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
177 << " frames is too large, maximum size is "
178 << kMaximumStreamBufferSizeBytes / frameSize;
179 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
180 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000181 const auto& flags = portConfigIt->flags.value();
182 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000183 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
184 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000185 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000186 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
187 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000188 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
Mikhail Naganov194daaa2023-01-05 22:34:20 +0000189 mVendorDebug.forceTransientBurst,
190 mVendorDebug.forceSynchronousDrain};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000191 StreamContext temp(
192 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
193 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000194 portConfigIt->format.value(), portConfigIt->channelMask.value(),
jiabin253bd322023-01-25 23:57:31 +0000195 portConfigIt->sampleRate.value().value,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000196 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov8651b362023-01-06 23:15:27 +0000197 asyncCallback, outEventCallback, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000198 if (temp.isValid()) {
199 *out_context = std::move(temp);
200 } else {
201 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
202 }
203 } else {
204 // TODO: Implement simulation of MMAP buffer allocation
205 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000206 return ndk::ScopedAStatus::ok();
207}
208
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000209std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
210 std::vector<AudioDevice> result;
211 auto& ports = getConfig().ports;
212 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
213 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
214 auto portIt = findById<AudioPort>(ports, *it);
215 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
216 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
217 }
218 }
219 return result;
220}
221
222std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
223 std::set<int32_t> result;
224 auto patchIdsRange = mPatches.equal_range(portConfigId);
225 auto& patches = getConfig().patches;
226 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
227 auto patchIt = findById<AudioPatch>(patches, it->second);
228 if (patchIt == patches.end()) {
229 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
230 << "not found in the configuration";
231 }
232 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
233 portConfigId) != patchIt->sourcePortConfigIds.end()) {
234 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
235 } else {
236 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
237 }
238 }
239 return result;
240}
241
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000242ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
243 auto& configs = getConfig().portConfigs;
244 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
245 if (portConfigIt == configs.end()) {
246 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
247 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
248 }
249 const int32_t portId = portConfigIt->portId;
250 // In our implementation, configs of mix ports always have unique IDs.
251 CHECK(portId != in_portConfigId);
252 auto& ports = getConfig().ports;
253 auto portIt = findById<AudioPort>(ports, portId);
254 if (portIt == ports.end()) {
255 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
256 << in_portConfigId << " not found";
257 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
258 }
259 if (mStreams.count(in_portConfigId) != 0) {
260 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
261 << " already has a stream opened on it";
262 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
263 }
264 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
265 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
266 << " does not correspond to a mix port";
267 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
268 }
269 const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
270 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
271 LOG(ERROR) << __func__ << ": port id " << portId
272 << " has already reached maximum allowed opened stream count: "
273 << maxOpenStreamCount;
274 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
275 }
276 *port = &(*portIt);
277 return ndk::ScopedAStatus::ok();
278}
279
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000280template <typename C>
281std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
282 std::set<int32_t> result;
283 auto& portConfigs = getConfig().portConfigs;
284 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
285 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
286 if (portConfigIt != portConfigs.end()) {
287 result.insert(portConfigIt->portId);
288 }
289 }
290 return result;
291}
292
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000293internal::Configuration& Module::getConfig() {
294 if (!mConfig) {
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000295 switch (mType) {
296 case Type::DEFAULT:
297 mConfig = std::move(internal::getPrimaryConfiguration());
298 break;
299 case Type::R_SUBMIX:
300 mConfig = std::move(internal::getRSubmixConfiguration());
301 break;
jiabinb309d8d2023-01-20 19:07:15 +0000302 case Type::USB:
303 mConfig = std::move(internal::getUsbConfiguration());
jiabin253bd322023-01-25 23:57:31 +0000304 break;
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000305 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000306 }
307 return *mConfig;
308}
309
310void Module::registerPatch(const AudioPatch& patch) {
311 auto& configs = getConfig().portConfigs;
312 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
313 for (auto portConfigId : portConfigIds) {
314 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
315 if (configIt != configs.end()) {
316 mPatches.insert(std::pair{portConfigId, patch.id});
317 if (configIt->portId != portConfigId) {
318 mPatches.insert(std::pair{configIt->portId, patch.id});
319 }
320 }
321 };
322 };
323 do_insert(patch.sourcePortConfigIds);
324 do_insert(patch.sinkPortConfigIds);
325}
326
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000327void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) {
328 // Streams from the old patch need to be disconnected, streams from the new
329 // patch need to be connected. If the stream belongs to both patches, no need
330 // to update it.
331 std::set<int32_t> idsToDisconnect, idsToConnect;
332 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
333 oldPatch.sourcePortConfigIds.end());
334 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
335 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
336 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
337 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
338 if (idsToConnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000339 LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected";
340 mStreams.setStreamIsConnected(portConfigId, {});
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000341 }
342 });
343 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
344 if (idsToDisconnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000345 const auto connectedDevices = findConnectedDevices(portConfigId);
346 LOG(DEBUG) << "The stream on port config id " << portConfigId
347 << " is connected to: " << ::android::internal::ToString(connectedDevices);
348 mStreams.setStreamIsConnected(portConfigId, connectedDevices);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000349 }
350 });
351}
352
Mikhail Naganov00603d12022-05-02 22:52:13 +0000353ndk::ScopedAStatus Module::setModuleDebug(
354 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
355 LOG(DEBUG) << __func__ << ": old flags:" << mDebug.toString()
356 << ", new flags: " << in_debug.toString();
357 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
358 !mConnectedDevicePorts.empty()) {
359 LOG(ERROR) << __func__ << ": attempting to change device connections simulation "
360 << "while having external devices connected";
361 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
362 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000363 if (in_debug.streamTransientStateDelayMs < 0) {
364 LOG(ERROR) << __func__ << ": streamTransientStateDelayMs is negative: "
365 << in_debug.streamTransientStateDelayMs;
366 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
367 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000368 mDebug = in_debug;
369 return ndk::ScopedAStatus::ok();
370}
371
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000372ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800373 if (!mTelephony) {
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000374 mTelephony = ndk::SharedRefBase::make<Telephony>();
375 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800376 *_aidl_return = mTelephony.getPtr();
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000377 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
378 return ndk::ScopedAStatus::ok();
379}
380
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000381ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -0800382 if (!mBluetooth) {
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000383 mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000384 }
Mikhail Naganov7499a002023-02-27 18:51:44 -0800385 *_aidl_return = mBluetooth.getPtr();
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000386 LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get();
387 return ndk::ScopedAStatus::ok();
388}
389
Mikhail Naganov7499a002023-02-27 18:51:44 -0800390ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
391 if (!mBluetoothA2dp) {
392 mBluetoothA2dp = ndk::SharedRefBase::make<BluetoothA2dp>();
393 }
394 *_aidl_return = mBluetoothA2dp.getPtr();
395 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothA2dp: " << _aidl_return->get();
396 return ndk::ScopedAStatus::ok();
397}
398
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800399ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
400 if (!mBluetoothLe) {
401 mBluetoothLe = ndk::SharedRefBase::make<BluetoothLe>();
402 }
403 *_aidl_return = mBluetoothLe.getPtr();
404 LOG(DEBUG) << __func__ << ": returning instance of IBluetoothLe: " << _aidl_return->get();
405 return ndk::ScopedAStatus::ok();
406}
407
Mikhail Naganov00603d12022-05-02 22:52:13 +0000408ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
409 AudioPort* _aidl_return) {
410 const int32_t templateId = in_templateIdAndAdditionalData.id;
411 auto& ports = getConfig().ports;
412 AudioPort connectedPort;
413 { // Scope the template port so that we don't accidentally modify it.
414 auto templateIt = findById<AudioPort>(ports, templateId);
415 if (templateIt == ports.end()) {
416 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
417 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
418 }
419 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
420 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
421 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
422 }
423 if (!templateIt->profiles.empty()) {
424 LOG(ERROR) << __func__ << ": port id " << templateId
425 << " does not have dynamic profiles";
426 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
427 }
428 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
429 if (templateDevicePort.device.type.connection.empty()) {
430 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
431 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
432 }
433 // Postpone id allocation until we ensure that there are no client errors.
434 connectedPort = *templateIt;
435 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
436 const auto& inputDevicePort =
437 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
438 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
439 connectedDevicePort.device.address = inputDevicePort.device.address;
440 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
441 << connectedDevicePort.device.toString();
442 // Check if there is already a connected port with for the same external device.
443 for (auto connectedPortId : mConnectedDevicePorts) {
444 auto connectedPortIt = findById<AudioPort>(ports, connectedPortId);
445 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
446 connectedDevicePort.device) {
447 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
448 << " is already connected at the device port id " << connectedPortId;
449 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
450 }
451 }
452 }
453
454 if (!mDebug.simulateDeviceConnections) {
455 // In a real HAL here we would attempt querying the profiles from the device.
456 LOG(ERROR) << __func__ << ": failed to query supported device profiles";
jiabin253bd322023-01-25 23:57:31 +0000457 // TODO: Check the return value when it is ready for actual devices.
458 populateConnectedDevicePort(&connectedPort);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000459 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
460 }
461
462 connectedPort.id = ++getConfig().nextPortId;
463 mConnectedDevicePorts.insert(connectedPort.id);
464 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
465 << "connected port ID " << connectedPort.id;
466 auto& connectedProfiles = getConfig().connectedProfiles;
467 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
468 connectedProfilesIt != connectedProfiles.end()) {
469 connectedPort.profiles = connectedProfilesIt->second;
470 }
471 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000472 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000473 *_aidl_return = std::move(connectedPort);
474
475 std::vector<AudioRoute> newRoutes;
476 auto& routes = getConfig().routes;
477 for (auto& r : routes) {
478 if (r.sinkPortId == templateId) {
479 AudioRoute newRoute;
480 newRoute.sourcePortIds = r.sourcePortIds;
481 newRoute.sinkPortId = connectedPort.id;
482 newRoute.isExclusive = r.isExclusive;
483 newRoutes.push_back(std::move(newRoute));
484 } else {
485 auto& srcs = r.sourcePortIds;
486 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
487 srcs.push_back(connectedPort.id);
488 }
489 }
490 }
491 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
492
493 return ndk::ScopedAStatus::ok();
494}
495
496ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
497 auto& ports = getConfig().ports;
498 auto portIt = findById<AudioPort>(ports, in_portId);
499 if (portIt == ports.end()) {
500 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
501 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
502 }
503 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
504 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
505 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
506 }
507 if (mConnectedDevicePorts.count(in_portId) == 0) {
508 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
509 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
510 }
511 auto& configs = getConfig().portConfigs;
512 auto& initials = getConfig().initialConfigs;
513 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
514 if (config.portId == in_portId) {
515 // Check if the configuration was provided by the client.
516 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
517 return initialIt == initials.end() || config != *initialIt;
518 }
519 return false;
520 });
521 if (configIt != configs.end()) {
522 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
523 << configIt->id;
524 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
525 }
jiabin783c48b2023-02-28 18:28:06 +0000526 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000527 ports.erase(portIt);
528 mConnectedDevicePorts.erase(in_portId);
529 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
530
531 auto& routes = getConfig().routes;
532 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
533 if (routesIt->sinkPortId == in_portId) {
534 routesIt = routes.erase(routesIt);
535 } else {
536 // Note: the list of sourcePortIds can't become empty because there must
537 // be the id of the template port in the route.
538 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
539 ++routesIt;
540 }
541 }
542
543 return ndk::ScopedAStatus::ok();
544}
545
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000546ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
547 *_aidl_return = getConfig().patches;
548 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
549 return ndk::ScopedAStatus::ok();
550}
551
552ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
553 auto& ports = getConfig().ports;
554 auto portIt = findById<AudioPort>(ports, in_portId);
555 if (portIt != ports.end()) {
556 *_aidl_return = *portIt;
557 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
558 return ndk::ScopedAStatus::ok();
559 }
560 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
561 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
562}
563
564ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
565 *_aidl_return = getConfig().portConfigs;
566 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
567 return ndk::ScopedAStatus::ok();
568}
569
570ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
571 *_aidl_return = getConfig().ports;
572 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
573 return ndk::ScopedAStatus::ok();
574}
575
576ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
577 *_aidl_return = getConfig().routes;
578 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
579 return ndk::ScopedAStatus::ok();
580}
581
Mikhail Naganov00603d12022-05-02 22:52:13 +0000582ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
583 std::vector<AudioRoute>* _aidl_return) {
584 auto& ports = getConfig().ports;
585 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
586 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
587 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
588 }
589 auto& routes = getConfig().routes;
590 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
591 [&](const auto& r) {
592 const auto& srcs = r.sourcePortIds;
593 return r.sinkPortId == in_portId ||
594 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
595 });
596 return ndk::ScopedAStatus::ok();
597}
598
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000599ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
600 OpenInputStreamReturn* _aidl_return) {
601 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
602 << in_args.bufferSizeFrames << " frames";
603 AudioPort* port = nullptr;
604 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
605 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000606 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000607 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
608 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000609 << " does not correspond to an input mix port";
610 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
611 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000612 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000613 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000614 nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000615 !status.isOk()) {
616 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000617 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000618 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000619 std::shared_ptr<StreamIn> stream;
jiabin253bd322023-01-25 23:57:31 +0000620 ndk::ScopedAStatus status = getStreamInCreator(mType)(in_args.sinkMetadata, std::move(context),
621 mConfig->microphones, &stream);
622 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000623 return status;
624 }
625 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000626 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
627 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000628 auto patchIt = mPatches.find(in_args.portConfigId);
629 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000630 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000631 }
632 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000633 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000634 return ndk::ScopedAStatus::ok();
635}
636
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000637ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
638 OpenOutputStreamReturn* _aidl_return) {
639 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
640 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
641 << " frames";
642 AudioPort* port = nullptr;
643 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
644 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000645 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000646 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
647 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000648 << " does not correspond to an output mix port";
649 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
650 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000651 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
652 AudioOutputFlags::COMPRESS_OFFLOAD);
653 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000654 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000655 << " has COMPRESS_OFFLOAD flag set, requires offload info";
656 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
657 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000658 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
659 AudioOutputFlags::NON_BLOCKING);
660 if (isNonBlocking && in_args.callback == nullptr) {
661 LOG(ERROR) << __func__ << ": port id " << port->id
662 << " has NON_BLOCKING flag set, requires async callback";
663 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
664 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000665 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000666 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000667 isNonBlocking ? in_args.callback : nullptr,
668 in_args.eventCallback, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000669 !status.isOk()) {
670 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000671 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000672 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000673 std::shared_ptr<StreamOut> stream;
jiabin253bd322023-01-25 23:57:31 +0000674 ndk::ScopedAStatus status = getStreamOutCreator(mType)(
675 in_args.sourceMetadata, std::move(context), in_args.offloadInfo, &stream);
676 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000677 return status;
678 }
679 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000680 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
681 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000682 auto patchIt = mPatches.find(in_args.portConfigId);
683 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000684 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000685 }
686 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000687 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000688 return ndk::ScopedAStatus::ok();
689}
690
Mikhail Naganov74927202022-12-19 16:37:14 +0000691ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
692 SupportedPlaybackRateFactors* _aidl_return) {
693 LOG(DEBUG) << __func__;
694 (void)_aidl_return;
695 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
696}
697
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000698ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000699 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000700 if (in_requested.sourcePortConfigIds.empty()) {
701 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
702 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
703 }
704 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
705 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
706 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
707 }
708 if (in_requested.sinkPortConfigIds.empty()) {
709 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
710 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
711 }
712 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
713 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
714 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
715 }
716
717 auto& configs = getConfig().portConfigs;
718 std::vector<int32_t> missingIds;
719 auto sources =
720 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
721 if (!missingIds.empty()) {
722 LOG(ERROR) << __func__ << ": following source port config ids not found: "
723 << ::android::internal::ToString(missingIds);
724 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
725 }
726 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
727 if (!missingIds.empty()) {
728 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
729 << ::android::internal::ToString(missingIds);
730 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
731 }
732 // bool indicates whether a non-exclusive route is available.
733 // If only an exclusive route is available, that means the patch can not be
734 // established if there is any other patch which currently uses the sink port.
735 std::map<int32_t, bool> allowedSinkPorts;
736 auto& routes = getConfig().routes;
737 for (auto src : sources) {
738 for (const auto& r : routes) {
739 const auto& srcs = r.sourcePortIds;
740 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
741 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
742 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
743 }
744 }
745 }
746 }
747 for (auto sink : sinks) {
748 if (allowedSinkPorts.count(sink->portId) == 0) {
749 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
750 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
751 }
752 }
753
jiabin253bd322023-01-25 23:57:31 +0000754 if (auto status = checkAudioPatchEndpointsMatch(sources, sinks); !status.isOk()) {
755 return status;
756 }
757
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000758 auto& patches = getConfig().patches;
759 auto existing = patches.end();
760 std::optional<decltype(mPatches)> patchesBackup;
761 if (in_requested.id != 0) {
762 existing = findById<AudioPatch>(patches, in_requested.id);
763 if (existing != patches.end()) {
764 patchesBackup = mPatches;
765 cleanUpPatch(existing->id);
766 } else {
767 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
768 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
769 }
770 }
771 // Validate the requested patch.
772 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
773 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
774 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
775 << "is exclusive and is already used by some other patch";
776 if (patchesBackup.has_value()) {
777 mPatches = std::move(*patchesBackup);
778 }
779 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
780 }
781 }
782 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000783 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
784 _aidl_return->latenciesMs.clear();
785 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
786 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000787 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000788 if (existing == patches.end()) {
789 _aidl_return->id = getConfig().nextPatchId++;
790 patches.push_back(*_aidl_return);
791 existing = patches.begin() + (patches.size() - 1);
792 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000793 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000794 *existing = *_aidl_return;
795 }
796 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000797 updateStreamsConnectedState(oldPatch, *_aidl_return);
798
799 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
800 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000801 return ndk::ScopedAStatus::ok();
802}
803
804ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
805 AudioPortConfig* out_suggested, bool* _aidl_return) {
806 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
807 auto& configs = getConfig().portConfigs;
808 auto existing = configs.end();
809 if (in_requested.id != 0) {
810 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
811 existing == configs.end()) {
812 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
813 << " not found";
814 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
815 }
816 }
817
818 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
819 if (portId == 0) {
820 LOG(ERROR) << __func__ << ": input port config does not specify portId";
821 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
822 }
823 auto& ports = getConfig().ports;
824 auto portIt = findById<AudioPort>(ports, portId);
825 if (portIt == ports.end()) {
826 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
827 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
828 }
829 if (existing != configs.end()) {
830 *out_suggested = *existing;
831 } else {
832 AudioPortConfig newConfig;
833 if (generateDefaultPortConfig(*portIt, &newConfig)) {
834 *out_suggested = newConfig;
835 } else {
836 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
837 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
838 }
839 }
840 // From this moment, 'out_suggested' is either an existing port config,
841 // or a new generated config. Now attempt to update it according to the specified
842 // fields of 'in_requested'.
843
844 bool requestedIsValid = true, requestedIsFullySpecified = true;
845
846 AudioIoFlags portFlags = portIt->flags;
847 if (in_requested.flags.has_value()) {
848 if (in_requested.flags.value() != portFlags) {
849 LOG(WARNING) << __func__ << ": requested flags "
850 << in_requested.flags.value().toString() << " do not match port's "
851 << portId << " flags " << portFlags.toString();
852 requestedIsValid = false;
853 }
854 } else {
855 requestedIsFullySpecified = false;
856 }
857
858 AudioProfile portProfile;
859 if (in_requested.format.has_value()) {
860 const auto& format = in_requested.format.value();
861 if (findAudioProfile(*portIt, format, &portProfile)) {
862 out_suggested->format = format;
863 } else {
864 LOG(WARNING) << __func__ << ": requested format " << format.toString()
865 << " is not found in port's " << portId << " profiles";
866 requestedIsValid = false;
867 }
868 } else {
869 requestedIsFullySpecified = false;
870 }
871 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
872 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
873 << out_suggested->format.value().toString() << " anymore";
874 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
875 }
876
877 if (in_requested.channelMask.has_value()) {
878 const auto& channelMask = in_requested.channelMask.value();
879 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
880 portProfile.channelMasks.end()) {
881 out_suggested->channelMask = channelMask;
882 } else {
883 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
884 << " is not supported for the format " << portProfile.format.toString()
885 << " by the port " << portId;
886 requestedIsValid = false;
887 }
888 } else {
889 requestedIsFullySpecified = false;
890 }
891
892 if (in_requested.sampleRate.has_value()) {
893 const auto& sampleRate = in_requested.sampleRate.value();
894 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
895 sampleRate.value) != portProfile.sampleRates.end()) {
896 out_suggested->sampleRate = sampleRate;
897 } else {
898 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
899 << " is not supported for the format " << portProfile.format.toString()
900 << " by the port " << portId;
901 requestedIsValid = false;
902 }
903 } else {
904 requestedIsFullySpecified = false;
905 }
906
907 if (in_requested.gain.has_value()) {
908 // Let's pretend that gain can always be applied.
909 out_suggested->gain = in_requested.gain.value();
910 }
911
Mikhail Naganov248e9502023-02-21 16:32:40 -0800912 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
913 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
914 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
915 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
916 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
917 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
918 }
919 } else {
920 LOG(WARNING) << __func__ << ": requested ext tag "
921 << toString(in_requested.ext.getTag()) << " do not match port's tag "
922 << toString(out_suggested->ext.getTag());
923 requestedIsValid = false;
924 }
925 }
926
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000927 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
928 out_suggested->id = getConfig().nextPortId++;
929 configs.push_back(*out_suggested);
930 *_aidl_return = true;
931 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
932 } else if (existing != configs.end() && requestedIsValid) {
933 *existing = *out_suggested;
934 *_aidl_return = true;
935 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
936 } else {
937 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
938 << "; requested is valid? " << requestedIsValid << ", fully specified? "
939 << requestedIsFullySpecified;
940 *_aidl_return = false;
941 }
942 return ndk::ScopedAStatus::ok();
943}
944
945ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
946 auto& patches = getConfig().patches;
947 auto patchIt = findById<AudioPatch>(patches, in_patchId);
948 if (patchIt != patches.end()) {
949 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000950 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000951 patches.erase(patchIt);
952 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
953 return ndk::ScopedAStatus::ok();
954 }
955 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
956 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
957}
958
959ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
960 auto& configs = getConfig().portConfigs;
961 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
962 if (configIt != configs.end()) {
963 if (mStreams.count(in_portConfigId) != 0) {
964 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
965 << " has a stream opened on it";
966 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
967 }
968 auto patchIt = mPatches.find(in_portConfigId);
969 if (patchIt != mPatches.end()) {
970 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
971 << " is used by the patch with id " << patchIt->second;
972 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
973 }
974 auto& initials = getConfig().initialConfigs;
975 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
976 if (initialIt == initials.end()) {
977 configs.erase(configIt);
978 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
979 } else if (*configIt != *initialIt) {
980 *configIt = *initialIt;
981 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
982 }
983 return ndk::ScopedAStatus::ok();
984 }
985 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
986 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
987}
988
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000989ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
990 *_aidl_return = mMasterMute;
991 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
992 return ndk::ScopedAStatus::ok();
993}
994
995ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
996 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +0000997 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
998 : onMasterMuteChanged(in_mute);
999 if (result.isOk()) {
1000 mMasterMute = in_mute;
1001 } else {
1002 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1003 << "), error=" << result;
1004 // Reset master mute if it failed.
1005 onMasterMuteChanged(mMasterMute);
1006 }
1007 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001008}
1009
1010ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1011 *_aidl_return = mMasterVolume;
1012 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1013 return ndk::ScopedAStatus::ok();
1014}
1015
1016ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1017 LOG(DEBUG) << __func__ << ": " << in_volume;
1018 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001019 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1020 : onMasterVolumeChanged(in_volume);
1021 if (result.isOk()) {
1022 mMasterVolume = in_volume;
1023 } else {
1024 // Reset master volume if it failed.
1025 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1026 << "), error=" << result;
1027 onMasterVolumeChanged(mMasterVolume);
1028 }
1029 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001030 }
1031 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1032 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1033}
1034
1035ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1036 *_aidl_return = mMicMute;
1037 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1038 return ndk::ScopedAStatus::ok();
1039}
1040
1041ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1042 LOG(DEBUG) << __func__ << ": " << in_mute;
1043 mMicMute = in_mute;
1044 return ndk::ScopedAStatus::ok();
1045}
1046
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001047ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001048 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001049 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1050 return ndk::ScopedAStatus::ok();
1051}
1052
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001053ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001054 if (!isValidAudioMode(in_mode)) {
1055 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1056 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1057 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001058 // No checks for supported audio modes here, it's an informative notification.
1059 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1060 return ndk::ScopedAStatus::ok();
1061}
1062
1063ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1064 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1065 return ndk::ScopedAStatus::ok();
1066}
1067
1068ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1069 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1070 return ndk::ScopedAStatus::ok();
1071}
1072
Vlad Popa83a6d822022-11-07 13:53:57 +01001073ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001074 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001075 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001076 }
Mikhail Naganov7499a002023-02-27 18:51:44 -08001077 *_aidl_return = mSoundDose.getPtr();
Vlad Popa943b7e22022-12-08 14:24:12 +01001078 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001079 return ndk::ScopedAStatus::ok();
1080}
1081
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001082ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1083 LOG(DEBUG) << __func__;
1084 (void)_aidl_return;
1085 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1086}
1087
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001088const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001089const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001090
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001091ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1092 std::vector<VendorParameter>* _aidl_return) {
1093 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001094 bool allParametersKnown = true;
1095 for (const auto& id : in_ids) {
1096 if (id == VendorDebug::kForceTransientBurstName) {
1097 VendorParameter forceTransientBurst{.id = id};
1098 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1099 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001100 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1101 VendorParameter forceSynchronousDrain{.id = id};
1102 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1103 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001104 } else {
1105 allParametersKnown = false;
1106 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1107 }
1108 }
1109 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1110 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001111}
1112
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001113namespace {
1114
1115template <typename W>
1116bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1117 std::optional<W> value;
1118 binder_status_t result = p.ext.getParcelable(&value);
1119 if (result == STATUS_OK && value.has_value()) {
1120 *v = value.value().value;
1121 return true;
1122 }
1123 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1124 << "\": " << result;
1125 return false;
1126}
1127
1128} // namespace
1129
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001130ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1131 bool in_async) {
1132 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1133 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001134 bool allParametersKnown = true;
1135 for (const auto& p : in_parameters) {
1136 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001137 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1138 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1139 }
1140 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1141 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001142 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1143 }
1144 } else {
1145 allParametersKnown = false;
1146 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1147 }
1148 }
1149 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1150 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001151}
1152
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001153ndk::ScopedAStatus Module::addDeviceEffect(
1154 int32_t in_portConfigId,
1155 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1156 if (in_effect == nullptr) {
1157 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1158 } else {
1159 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1160 << in_effect->asBinder().get();
1161 }
1162 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1163}
1164
1165ndk::ScopedAStatus Module::removeDeviceEffect(
1166 int32_t in_portConfigId,
1167 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1168 if (in_effect == nullptr) {
1169 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1170 } else {
1171 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1172 << in_effect->asBinder().get();
1173 }
1174 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1175}
1176
jiabin9a8e6862023-01-12 23:06:37 +00001177ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1178 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1179 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1180 std::set<int32_t> mmapSinks;
1181 std::set<int32_t> mmapSources;
1182 auto& ports = getConfig().ports;
1183 for (const auto& port : ports) {
1184 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1185 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1186 AudioInputFlags::MMAP_NOIRQ)) {
1187 mmapSinks.insert(port.id);
1188 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1189 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1190 AudioOutputFlags::MMAP_NOIRQ)) {
1191 mmapSources.insert(port.id);
1192 }
1193 }
1194 for (const auto& route : getConfig().routes) {
1195 if (mmapSinks.count(route.sinkPortId) != 0) {
1196 // The sink is a mix port, add the sources if they are device ports.
1197 for (int sourcePortId : route.sourcePortIds) {
1198 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1199 if (sourcePortIt == ports.end()) {
1200 // This must not happen
1201 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1202 continue;
1203 }
1204 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1205 // The source is not a device port, skip
1206 continue;
1207 }
1208 AudioMMapPolicyInfo policyInfo;
1209 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1210 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1211 // default implementation.
1212 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1213 _aidl_return->push_back(policyInfo);
1214 }
1215 } else {
1216 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1217 if (sinkPortIt == ports.end()) {
1218 // This must not happen
1219 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1220 continue;
1221 }
1222 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1223 // The sink is not a device port, skip
1224 continue;
1225 }
1226 if (count_any(mmapSources, route.sourcePortIds)) {
1227 AudioMMapPolicyInfo policyInfo;
1228 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1229 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1230 // default implementation.
1231 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1232 _aidl_return->push_back(policyInfo);
1233 }
1234 }
1235 }
1236 return ndk::ScopedAStatus::ok();
1237}
1238
Eric Laurente2432ea2023-01-12 17:47:31 +01001239ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1240 LOG(DEBUG) << __func__;
1241 *_aidl_return = false;
1242 return ndk::ScopedAStatus::ok();
1243}
1244
jiabinb76981e2023-01-18 00:58:30 +00001245ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1246 if (!isMmapSupported()) {
1247 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1248 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1249 }
1250 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1251 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1252 return ndk::ScopedAStatus::ok();
1253}
1254
1255ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1256 if (!isMmapSupported()) {
1257 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1258 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1259 }
1260 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1261 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1262 return ndk::ScopedAStatus::ok();
1263}
1264
1265bool Module::isMmapSupported() {
1266 if (mIsMmapSupported.has_value()) {
1267 return mIsMmapSupported.value();
1268 }
1269 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1270 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1271 mIsMmapSupported = false;
1272 } else {
1273 mIsMmapSupported =
1274 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1275 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1276 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1277 }) != mmapPolicyInfos.end();
1278 }
1279 return mIsMmapSupported.value();
1280}
1281
jiabin253bd322023-01-25 23:57:31 +00001282ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001283 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001284 return ndk::ScopedAStatus::ok();
1285}
1286
1287ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1288 const std::vector<AudioPortConfig*>& sources __unused,
1289 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001290 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001291 return ndk::ScopedAStatus::ok();
1292}
1293
jiabin783c48b2023-02-28 18:28:06 +00001294void Module::onExternalDeviceConnectionChanged(
1295 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1296 bool connected __unused) {
1297 LOG(DEBUG) << __func__ << ": do nothing and return";
1298}
1299
1300ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1301 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1302 return ndk::ScopedAStatus::ok();
1303}
1304
1305ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1306 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1307 return ndk::ScopedAStatus::ok();
1308}
1309
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001310} // namespace aidl::android::hardware::audio::core