blob: b546597adaefd59a8c887281137cedd04e6e9085 [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
37using aidl::android::hardware::audio::common::SinkMetadata;
38using aidl::android::hardware::audio::common::SourceMetadata;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010039using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000040using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000041using aidl::android::media::audio::common::AudioDevice;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000042using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000043using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000044using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000045using aidl::android::media::audio::common::AudioIoFlags;
jiabin9a8e6862023-01-12 23:06:37 +000046using aidl::android::media::audio::common::AudioMMapPolicy;
47using aidl::android::media::audio::common::AudioMMapPolicyInfo;
48using aidl::android::media::audio::common::AudioMMapPolicyType;
Mikhail Naganov04ae8222023-01-11 15:48:10 -080049using aidl::android::media::audio::common::AudioMode;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000050using aidl::android::media::audio::common::AudioOffloadInfo;
51using aidl::android::media::audio::common::AudioOutputFlags;
52using aidl::android::media::audio::common::AudioPort;
53using aidl::android::media::audio::common::AudioPortConfig;
54using aidl::android::media::audio::common::AudioPortExt;
55using aidl::android::media::audio::common::AudioProfile;
Mikhail Naganov20047bc2023-01-05 20:16:07 +000056using aidl::android::media::audio::common::Boolean;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000057using aidl::android::media::audio::common::Int;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080058using aidl::android::media::audio::common::MicrophoneInfo;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000059using aidl::android::media::audio::common::PcmType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000060using android::hardware::audio::common::getFrameSizeInBytes;
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +000061using android::hardware::audio::common::isBitPositionFlagSet;
Mikhail Naganov04ae8222023-01-11 15:48:10 -080062using android::hardware::audio::common::isValidAudioMode;
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) {
373 if (mTelephony == nullptr) {
374 mTelephony = ndk::SharedRefBase::make<Telephony>();
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000375 mTelephonyBinder = mTelephony->asBinder();
376 AIBinder_setMinSchedulerPolicy(mTelephonyBinder.get(), SCHED_NORMAL,
Shunkai Yao39bf2c32022-12-06 03:25:59 +0000377 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000378 }
379 *_aidl_return = mTelephony;
380 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
381 return ndk::ScopedAStatus::ok();
382}
383
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000384ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
385 if (mBluetooth == nullptr) {
386 mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
387 mBluetoothBinder = mBluetooth->asBinder();
388 AIBinder_setMinSchedulerPolicy(mBluetoothBinder.get(), SCHED_NORMAL,
389 ANDROID_PRIORITY_AUDIO);
390 }
391 *_aidl_return = mBluetooth;
392 LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get();
393 return ndk::ScopedAStatus::ok();
394}
395
Mikhail Naganov00603d12022-05-02 22:52:13 +0000396ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
397 AudioPort* _aidl_return) {
398 const int32_t templateId = in_templateIdAndAdditionalData.id;
399 auto& ports = getConfig().ports;
400 AudioPort connectedPort;
401 { // Scope the template port so that we don't accidentally modify it.
402 auto templateIt = findById<AudioPort>(ports, templateId);
403 if (templateIt == ports.end()) {
404 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
405 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
406 }
407 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
408 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
409 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
410 }
411 if (!templateIt->profiles.empty()) {
412 LOG(ERROR) << __func__ << ": port id " << templateId
413 << " does not have dynamic profiles";
414 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
415 }
416 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
417 if (templateDevicePort.device.type.connection.empty()) {
418 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
419 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
420 }
421 // Postpone id allocation until we ensure that there are no client errors.
422 connectedPort = *templateIt;
423 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
424 const auto& inputDevicePort =
425 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
426 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
427 connectedDevicePort.device.address = inputDevicePort.device.address;
428 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
429 << connectedDevicePort.device.toString();
430 // Check if there is already a connected port with for the same external device.
431 for (auto connectedPortId : mConnectedDevicePorts) {
432 auto connectedPortIt = findById<AudioPort>(ports, connectedPortId);
433 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
434 connectedDevicePort.device) {
435 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
436 << " is already connected at the device port id " << connectedPortId;
437 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
438 }
439 }
440 }
441
442 if (!mDebug.simulateDeviceConnections) {
443 // In a real HAL here we would attempt querying the profiles from the device.
444 LOG(ERROR) << __func__ << ": failed to query supported device profiles";
jiabin253bd322023-01-25 23:57:31 +0000445 // TODO: Check the return value when it is ready for actual devices.
446 populateConnectedDevicePort(&connectedPort);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000447 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
448 }
449
450 connectedPort.id = ++getConfig().nextPortId;
451 mConnectedDevicePorts.insert(connectedPort.id);
452 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
453 << "connected port ID " << connectedPort.id;
454 auto& connectedProfiles = getConfig().connectedProfiles;
455 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
456 connectedProfilesIt != connectedProfiles.end()) {
457 connectedPort.profiles = connectedProfilesIt->second;
458 }
459 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000460 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000461 *_aidl_return = std::move(connectedPort);
462
463 std::vector<AudioRoute> newRoutes;
464 auto& routes = getConfig().routes;
465 for (auto& r : routes) {
466 if (r.sinkPortId == templateId) {
467 AudioRoute newRoute;
468 newRoute.sourcePortIds = r.sourcePortIds;
469 newRoute.sinkPortId = connectedPort.id;
470 newRoute.isExclusive = r.isExclusive;
471 newRoutes.push_back(std::move(newRoute));
472 } else {
473 auto& srcs = r.sourcePortIds;
474 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
475 srcs.push_back(connectedPort.id);
476 }
477 }
478 }
479 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
480
481 return ndk::ScopedAStatus::ok();
482}
483
484ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
485 auto& ports = getConfig().ports;
486 auto portIt = findById<AudioPort>(ports, in_portId);
487 if (portIt == ports.end()) {
488 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
489 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
490 }
491 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
492 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
493 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
494 }
495 if (mConnectedDevicePorts.count(in_portId) == 0) {
496 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
497 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
498 }
499 auto& configs = getConfig().portConfigs;
500 auto& initials = getConfig().initialConfigs;
501 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
502 if (config.portId == in_portId) {
503 // Check if the configuration was provided by the client.
504 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
505 return initialIt == initials.end() || config != *initialIt;
506 }
507 return false;
508 });
509 if (configIt != configs.end()) {
510 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
511 << configIt->id;
512 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
513 }
jiabin783c48b2023-02-28 18:28:06 +0000514 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000515 ports.erase(portIt);
516 mConnectedDevicePorts.erase(in_portId);
517 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
518
519 auto& routes = getConfig().routes;
520 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
521 if (routesIt->sinkPortId == in_portId) {
522 routesIt = routes.erase(routesIt);
523 } else {
524 // Note: the list of sourcePortIds can't become empty because there must
525 // be the id of the template port in the route.
526 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
527 ++routesIt;
528 }
529 }
530
531 return ndk::ScopedAStatus::ok();
532}
533
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000534ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
535 *_aidl_return = getConfig().patches;
536 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
537 return ndk::ScopedAStatus::ok();
538}
539
540ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
541 auto& ports = getConfig().ports;
542 auto portIt = findById<AudioPort>(ports, in_portId);
543 if (portIt != ports.end()) {
544 *_aidl_return = *portIt;
545 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
546 return ndk::ScopedAStatus::ok();
547 }
548 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
549 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
550}
551
552ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
553 *_aidl_return = getConfig().portConfigs;
554 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
555 return ndk::ScopedAStatus::ok();
556}
557
558ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
559 *_aidl_return = getConfig().ports;
560 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
561 return ndk::ScopedAStatus::ok();
562}
563
564ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
565 *_aidl_return = getConfig().routes;
566 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
567 return ndk::ScopedAStatus::ok();
568}
569
Mikhail Naganov00603d12022-05-02 22:52:13 +0000570ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
571 std::vector<AudioRoute>* _aidl_return) {
572 auto& ports = getConfig().ports;
573 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
574 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
575 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
576 }
577 auto& routes = getConfig().routes;
578 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
579 [&](const auto& r) {
580 const auto& srcs = r.sourcePortIds;
581 return r.sinkPortId == in_portId ||
582 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
583 });
584 return ndk::ScopedAStatus::ok();
585}
586
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000587ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
588 OpenInputStreamReturn* _aidl_return) {
589 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
590 << in_args.bufferSizeFrames << " frames";
591 AudioPort* port = nullptr;
592 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
593 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000594 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000595 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
596 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000597 << " does not correspond to an input mix port";
598 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
599 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000600 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000601 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000602 nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000603 !status.isOk()) {
604 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000605 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000606 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000607 std::shared_ptr<StreamIn> stream;
jiabin253bd322023-01-25 23:57:31 +0000608 ndk::ScopedAStatus status = getStreamInCreator(mType)(in_args.sinkMetadata, std::move(context),
609 mConfig->microphones, &stream);
610 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000611 return status;
612 }
613 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000614 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
615 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000616 auto patchIt = mPatches.find(in_args.portConfigId);
617 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000618 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000619 }
620 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000621 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000622 return ndk::ScopedAStatus::ok();
623}
624
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000625ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
626 OpenOutputStreamReturn* _aidl_return) {
627 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
628 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
629 << " frames";
630 AudioPort* port = nullptr;
631 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
632 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000633 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000634 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
635 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000636 << " does not correspond to an output mix port";
637 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
638 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000639 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
640 AudioOutputFlags::COMPRESS_OFFLOAD);
641 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000642 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000643 << " has COMPRESS_OFFLOAD flag set, requires offload info";
644 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
645 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000646 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
647 AudioOutputFlags::NON_BLOCKING);
648 if (isNonBlocking && in_args.callback == nullptr) {
649 LOG(ERROR) << __func__ << ": port id " << port->id
650 << " has NON_BLOCKING flag set, requires async callback";
651 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
652 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000653 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000654 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
Mikhail Naganov8651b362023-01-06 23:15:27 +0000655 isNonBlocking ? in_args.callback : nullptr,
656 in_args.eventCallback, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000657 !status.isOk()) {
658 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000659 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000660 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000661 std::shared_ptr<StreamOut> stream;
jiabin253bd322023-01-25 23:57:31 +0000662 ndk::ScopedAStatus status = getStreamOutCreator(mType)(
663 in_args.sourceMetadata, std::move(context), in_args.offloadInfo, &stream);
664 if (!status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000665 return status;
666 }
667 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000668 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
669 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000670 auto patchIt = mPatches.find(in_args.portConfigId);
671 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000672 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000673 }
674 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000675 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000676 return ndk::ScopedAStatus::ok();
677}
678
Mikhail Naganov74927202022-12-19 16:37:14 +0000679ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
680 SupportedPlaybackRateFactors* _aidl_return) {
681 LOG(DEBUG) << __func__;
682 (void)_aidl_return;
683 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
684}
685
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000686ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000687 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000688 if (in_requested.sourcePortConfigIds.empty()) {
689 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
690 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
691 }
692 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
693 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
694 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
695 }
696 if (in_requested.sinkPortConfigIds.empty()) {
697 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
698 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
699 }
700 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
701 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
702 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
703 }
704
705 auto& configs = getConfig().portConfigs;
706 std::vector<int32_t> missingIds;
707 auto sources =
708 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
709 if (!missingIds.empty()) {
710 LOG(ERROR) << __func__ << ": following source port config ids not found: "
711 << ::android::internal::ToString(missingIds);
712 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
713 }
714 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
715 if (!missingIds.empty()) {
716 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
717 << ::android::internal::ToString(missingIds);
718 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
719 }
720 // bool indicates whether a non-exclusive route is available.
721 // If only an exclusive route is available, that means the patch can not be
722 // established if there is any other patch which currently uses the sink port.
723 std::map<int32_t, bool> allowedSinkPorts;
724 auto& routes = getConfig().routes;
725 for (auto src : sources) {
726 for (const auto& r : routes) {
727 const auto& srcs = r.sourcePortIds;
728 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
729 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
730 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
731 }
732 }
733 }
734 }
735 for (auto sink : sinks) {
736 if (allowedSinkPorts.count(sink->portId) == 0) {
737 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
738 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
739 }
740 }
741
jiabin253bd322023-01-25 23:57:31 +0000742 if (auto status = checkAudioPatchEndpointsMatch(sources, sinks); !status.isOk()) {
743 return status;
744 }
745
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000746 auto& patches = getConfig().patches;
747 auto existing = patches.end();
748 std::optional<decltype(mPatches)> patchesBackup;
749 if (in_requested.id != 0) {
750 existing = findById<AudioPatch>(patches, in_requested.id);
751 if (existing != patches.end()) {
752 patchesBackup = mPatches;
753 cleanUpPatch(existing->id);
754 } else {
755 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
756 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
757 }
758 }
759 // Validate the requested patch.
760 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
761 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
762 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
763 << "is exclusive and is already used by some other patch";
764 if (patchesBackup.has_value()) {
765 mPatches = std::move(*patchesBackup);
766 }
767 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
768 }
769 }
770 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000771 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
772 _aidl_return->latenciesMs.clear();
773 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
774 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000775 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000776 if (existing == patches.end()) {
777 _aidl_return->id = getConfig().nextPatchId++;
778 patches.push_back(*_aidl_return);
779 existing = patches.begin() + (patches.size() - 1);
780 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000781 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000782 *existing = *_aidl_return;
783 }
784 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000785 updateStreamsConnectedState(oldPatch, *_aidl_return);
786
787 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
788 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000789 return ndk::ScopedAStatus::ok();
790}
791
792ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
793 AudioPortConfig* out_suggested, bool* _aidl_return) {
794 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
795 auto& configs = getConfig().portConfigs;
796 auto existing = configs.end();
797 if (in_requested.id != 0) {
798 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
799 existing == configs.end()) {
800 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
801 << " not found";
802 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
803 }
804 }
805
806 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
807 if (portId == 0) {
808 LOG(ERROR) << __func__ << ": input port config does not specify portId";
809 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
810 }
811 auto& ports = getConfig().ports;
812 auto portIt = findById<AudioPort>(ports, portId);
813 if (portIt == ports.end()) {
814 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
815 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
816 }
817 if (existing != configs.end()) {
818 *out_suggested = *existing;
819 } else {
820 AudioPortConfig newConfig;
821 if (generateDefaultPortConfig(*portIt, &newConfig)) {
822 *out_suggested = newConfig;
823 } else {
824 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
825 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
826 }
827 }
828 // From this moment, 'out_suggested' is either an existing port config,
829 // or a new generated config. Now attempt to update it according to the specified
830 // fields of 'in_requested'.
831
832 bool requestedIsValid = true, requestedIsFullySpecified = true;
833
834 AudioIoFlags portFlags = portIt->flags;
835 if (in_requested.flags.has_value()) {
836 if (in_requested.flags.value() != portFlags) {
837 LOG(WARNING) << __func__ << ": requested flags "
838 << in_requested.flags.value().toString() << " do not match port's "
839 << portId << " flags " << portFlags.toString();
840 requestedIsValid = false;
841 }
842 } else {
843 requestedIsFullySpecified = false;
844 }
845
846 AudioProfile portProfile;
847 if (in_requested.format.has_value()) {
848 const auto& format = in_requested.format.value();
849 if (findAudioProfile(*portIt, format, &portProfile)) {
850 out_suggested->format = format;
851 } else {
852 LOG(WARNING) << __func__ << ": requested format " << format.toString()
853 << " is not found in port's " << portId << " profiles";
854 requestedIsValid = false;
855 }
856 } else {
857 requestedIsFullySpecified = false;
858 }
859 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
860 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
861 << out_suggested->format.value().toString() << " anymore";
862 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
863 }
864
865 if (in_requested.channelMask.has_value()) {
866 const auto& channelMask = in_requested.channelMask.value();
867 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
868 portProfile.channelMasks.end()) {
869 out_suggested->channelMask = channelMask;
870 } else {
871 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
872 << " is not supported for the format " << portProfile.format.toString()
873 << " by the port " << portId;
874 requestedIsValid = false;
875 }
876 } else {
877 requestedIsFullySpecified = false;
878 }
879
880 if (in_requested.sampleRate.has_value()) {
881 const auto& sampleRate = in_requested.sampleRate.value();
882 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
883 sampleRate.value) != portProfile.sampleRates.end()) {
884 out_suggested->sampleRate = sampleRate;
885 } else {
886 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
887 << " is not supported for the format " << portProfile.format.toString()
888 << " by the port " << portId;
889 requestedIsValid = false;
890 }
891 } else {
892 requestedIsFullySpecified = false;
893 }
894
895 if (in_requested.gain.has_value()) {
896 // Let's pretend that gain can always be applied.
897 out_suggested->gain = in_requested.gain.value();
898 }
899
Mikhail Naganov248e9502023-02-21 16:32:40 -0800900 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
901 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
902 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
903 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
904 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
905 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
906 }
907 } else {
908 LOG(WARNING) << __func__ << ": requested ext tag "
909 << toString(in_requested.ext.getTag()) << " do not match port's tag "
910 << toString(out_suggested->ext.getTag());
911 requestedIsValid = false;
912 }
913 }
914
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000915 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
916 out_suggested->id = getConfig().nextPortId++;
917 configs.push_back(*out_suggested);
918 *_aidl_return = true;
919 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
920 } else if (existing != configs.end() && requestedIsValid) {
921 *existing = *out_suggested;
922 *_aidl_return = true;
923 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
924 } else {
925 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
926 << "; requested is valid? " << requestedIsValid << ", fully specified? "
927 << requestedIsFullySpecified;
928 *_aidl_return = false;
929 }
930 return ndk::ScopedAStatus::ok();
931}
932
933ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
934 auto& patches = getConfig().patches;
935 auto patchIt = findById<AudioPatch>(patches, in_patchId);
936 if (patchIt != patches.end()) {
937 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000938 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000939 patches.erase(patchIt);
940 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
941 return ndk::ScopedAStatus::ok();
942 }
943 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
944 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
945}
946
947ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
948 auto& configs = getConfig().portConfigs;
949 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
950 if (configIt != configs.end()) {
951 if (mStreams.count(in_portConfigId) != 0) {
952 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
953 << " has a stream opened on it";
954 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
955 }
956 auto patchIt = mPatches.find(in_portConfigId);
957 if (patchIt != mPatches.end()) {
958 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
959 << " is used by the patch with id " << patchIt->second;
960 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
961 }
962 auto& initials = getConfig().initialConfigs;
963 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
964 if (initialIt == initials.end()) {
965 configs.erase(configIt);
966 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
967 } else if (*configIt != *initialIt) {
968 *configIt = *initialIt;
969 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
970 }
971 return ndk::ScopedAStatus::ok();
972 }
973 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
974 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
975}
976
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000977ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
978 *_aidl_return = mMasterMute;
979 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
980 return ndk::ScopedAStatus::ok();
981}
982
983ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
984 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +0000985 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
986 : onMasterMuteChanged(in_mute);
987 if (result.isOk()) {
988 mMasterMute = in_mute;
989 } else {
990 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
991 << "), error=" << result;
992 // Reset master mute if it failed.
993 onMasterMuteChanged(mMasterMute);
994 }
995 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000996}
997
998ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
999 *_aidl_return = mMasterVolume;
1000 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1001 return ndk::ScopedAStatus::ok();
1002}
1003
1004ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1005 LOG(DEBUG) << __func__ << ": " << in_volume;
1006 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001007 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1008 : onMasterVolumeChanged(in_volume);
1009 if (result.isOk()) {
1010 mMasterVolume = in_volume;
1011 } else {
1012 // Reset master volume if it failed.
1013 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1014 << "), error=" << result;
1015 onMasterVolumeChanged(mMasterVolume);
1016 }
1017 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001018 }
1019 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1020 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1021}
1022
1023ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1024 *_aidl_return = mMicMute;
1025 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1026 return ndk::ScopedAStatus::ok();
1027}
1028
1029ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1030 LOG(DEBUG) << __func__ << ": " << in_mute;
1031 mMicMute = in_mute;
1032 return ndk::ScopedAStatus::ok();
1033}
1034
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001035ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001036 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001037 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1038 return ndk::ScopedAStatus::ok();
1039}
1040
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001041ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001042 if (!isValidAudioMode(in_mode)) {
1043 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1044 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1045 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001046 // No checks for supported audio modes here, it's an informative notification.
1047 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1048 return ndk::ScopedAStatus::ok();
1049}
1050
1051ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1052 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1053 return ndk::ScopedAStatus::ok();
1054}
1055
1056ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1057 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1058 return ndk::ScopedAStatus::ok();
1059}
1060
Vlad Popa83a6d822022-11-07 13:53:57 +01001061ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Vlad Popa943b7e22022-12-08 14:24:12 +01001062 if (mSoundDose == nullptr) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001063 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Mikhail Naganovdf5feba2022-12-15 00:11:14 +00001064 mSoundDoseBinder = mSoundDose->asBinder();
1065 AIBinder_setMinSchedulerPolicy(mSoundDoseBinder.get(), SCHED_NORMAL,
1066 ANDROID_PRIORITY_AUDIO);
Vlad Popa943b7e22022-12-08 14:24:12 +01001067 }
1068 *_aidl_return = mSoundDose;
1069 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001070 return ndk::ScopedAStatus::ok();
1071}
1072
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001073ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1074 LOG(DEBUG) << __func__;
1075 (void)_aidl_return;
1076 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1077}
1078
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001079const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001080const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001081
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001082ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1083 std::vector<VendorParameter>* _aidl_return) {
1084 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001085 bool allParametersKnown = true;
1086 for (const auto& id : in_ids) {
1087 if (id == VendorDebug::kForceTransientBurstName) {
1088 VendorParameter forceTransientBurst{.id = id};
1089 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1090 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001091 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1092 VendorParameter forceSynchronousDrain{.id = id};
1093 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1094 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001095 } else {
1096 allParametersKnown = false;
1097 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1098 }
1099 }
1100 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1101 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001102}
1103
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001104namespace {
1105
1106template <typename W>
1107bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1108 std::optional<W> value;
1109 binder_status_t result = p.ext.getParcelable(&value);
1110 if (result == STATUS_OK && value.has_value()) {
1111 *v = value.value().value;
1112 return true;
1113 }
1114 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1115 << "\": " << result;
1116 return false;
1117}
1118
1119} // namespace
1120
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001121ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1122 bool in_async) {
1123 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1124 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001125 bool allParametersKnown = true;
1126 for (const auto& p : in_parameters) {
1127 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001128 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1129 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1130 }
1131 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1132 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001133 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1134 }
1135 } else {
1136 allParametersKnown = false;
1137 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1138 }
1139 }
1140 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1141 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001142}
1143
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001144ndk::ScopedAStatus Module::addDeviceEffect(
1145 int32_t in_portConfigId,
1146 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1147 if (in_effect == nullptr) {
1148 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1149 } else {
1150 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1151 << in_effect->asBinder().get();
1152 }
1153 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1154}
1155
1156ndk::ScopedAStatus Module::removeDeviceEffect(
1157 int32_t in_portConfigId,
1158 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1159 if (in_effect == nullptr) {
1160 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1161 } else {
1162 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1163 << in_effect->asBinder().get();
1164 }
1165 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1166}
1167
jiabin9a8e6862023-01-12 23:06:37 +00001168ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1169 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1170 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1171 std::set<int32_t> mmapSinks;
1172 std::set<int32_t> mmapSources;
1173 auto& ports = getConfig().ports;
1174 for (const auto& port : ports) {
1175 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1176 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1177 AudioInputFlags::MMAP_NOIRQ)) {
1178 mmapSinks.insert(port.id);
1179 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1180 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1181 AudioOutputFlags::MMAP_NOIRQ)) {
1182 mmapSources.insert(port.id);
1183 }
1184 }
1185 for (const auto& route : getConfig().routes) {
1186 if (mmapSinks.count(route.sinkPortId) != 0) {
1187 // The sink is a mix port, add the sources if they are device ports.
1188 for (int sourcePortId : route.sourcePortIds) {
1189 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1190 if (sourcePortIt == ports.end()) {
1191 // This must not happen
1192 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1193 continue;
1194 }
1195 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1196 // The source is not a device port, skip
1197 continue;
1198 }
1199 AudioMMapPolicyInfo policyInfo;
1200 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1201 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1202 // default implementation.
1203 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1204 _aidl_return->push_back(policyInfo);
1205 }
1206 } else {
1207 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1208 if (sinkPortIt == ports.end()) {
1209 // This must not happen
1210 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1211 continue;
1212 }
1213 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1214 // The sink is not a device port, skip
1215 continue;
1216 }
1217 if (count_any(mmapSources, route.sourcePortIds)) {
1218 AudioMMapPolicyInfo policyInfo;
1219 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1220 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1221 // default implementation.
1222 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1223 _aidl_return->push_back(policyInfo);
1224 }
1225 }
1226 }
1227 return ndk::ScopedAStatus::ok();
1228}
1229
Eric Laurente2432ea2023-01-12 17:47:31 +01001230ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1231 LOG(DEBUG) << __func__;
1232 *_aidl_return = false;
1233 return ndk::ScopedAStatus::ok();
1234}
1235
jiabinb76981e2023-01-18 00:58:30 +00001236ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1237 if (!isMmapSupported()) {
1238 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1239 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1240 }
1241 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1242 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1243 return ndk::ScopedAStatus::ok();
1244}
1245
1246ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1247 if (!isMmapSupported()) {
1248 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1249 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1250 }
1251 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1252 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1253 return ndk::ScopedAStatus::ok();
1254}
1255
1256bool Module::isMmapSupported() {
1257 if (mIsMmapSupported.has_value()) {
1258 return mIsMmapSupported.value();
1259 }
1260 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1261 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1262 mIsMmapSupported = false;
1263 } else {
1264 mIsMmapSupported =
1265 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1266 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1267 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1268 }) != mmapPolicyInfos.end();
1269 }
1270 return mIsMmapSupported.value();
1271}
1272
jiabin253bd322023-01-25 23:57:31 +00001273ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
1274 LOG(DEBUG) << __func__ << ": do nothing and return ok";
1275 return ndk::ScopedAStatus::ok();
1276}
1277
1278ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1279 const std::vector<AudioPortConfig*>& sources __unused,
1280 const std::vector<AudioPortConfig*>& sinks __unused) {
1281 LOG(DEBUG) << __func__ << ": do nothing and return ok";
1282 return ndk::ScopedAStatus::ok();
1283}
1284
jiabin783c48b2023-02-28 18:28:06 +00001285void Module::onExternalDeviceConnectionChanged(
1286 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1287 bool connected __unused) {
1288 LOG(DEBUG) << __func__ << ": do nothing and return";
1289}
1290
1291ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1292 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1293 return ndk::ScopedAStatus::ok();
1294}
1295
1296ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1297 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1298 return ndk::ScopedAStatus::ok();
1299}
1300
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001301} // namespace aidl::android::hardware::audio::core