blob: 3117134f4de1fabae642fed6a0c97c898b7ffe3b [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 Naganov4f5d3f12022-07-22 23:23:25 +000021#include <Utils.h>
22#include <aidl/android/media/audio/common/AudioInputFlags.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000023#include <aidl/android/media/audio/common/AudioOutputFlags.h>
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -070024#include <android-base/logging.h>
25#include <android/binder_ibinder_platform.h>
26#include <error/expected_utils.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000027
28#include "core-impl/Module.h"
Mikhail Naganovb03b5c42023-07-26 13:13:35 -070029#include "core-impl/ModuleBluetooth.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070030#include "core-impl/ModulePrimary.h"
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053031#include "core-impl/ModuleRemoteSubmix.h"
Mikhail Naganov521fc492023-07-11 17:24:08 -070032#include "core-impl/ModuleStub.h"
jiabin253bd322023-01-25 23:57:31 +000033#include "core-impl/ModuleUsb.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010034#include "core-impl/SoundDose.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) {
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530113 case Type::DEFAULT:
Mikhail Naganov521fc492023-07-11 17:24:08 -0700114 return ndk::SharedRefBase::make<ModulePrimary>();
115 case Type::R_SUBMIX:
116 return ndk::SharedRefBase::make<ModuleRemoteSubmix>();
117 case Type::STUB:
118 return ndk::SharedRefBase::make<ModuleStub>();
119 case Type::USB:
120 return ndk::SharedRefBase::make<ModuleUsb>();
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700121 case Type::BLUETOOTH:
122 return ndk::SharedRefBase::make<ModuleBluetooth>();
jiabin253bd322023-01-25 23:57:31 +0000123 }
124}
125
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700126std::ostream& operator<<(std::ostream& os, Module::Type t) {
127 switch (t) {
128 case Module::Type::DEFAULT:
129 os << "default";
130 break;
131 case Module::Type::R_SUBMIX:
132 os << "r_submix";
133 break;
Mikhail Naganov521fc492023-07-11 17:24:08 -0700134 case Module::Type::STUB:
135 os << "stub";
136 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700137 case Module::Type::USB:
138 os << "usb";
139 break;
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700140 case Module::Type::BLUETOOTH:
141 os << "bluetooth";
142 break;
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700143 }
144 return os;
145}
146
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000147void Module::cleanUpPatch(int32_t patchId) {
148 erase_all_values(mPatches, std::set<int32_t>{patchId});
149}
150
Mikhail Naganov8651b362023-01-06 23:15:27 +0000151ndk::ScopedAStatus Module::createStreamContext(
152 int32_t in_portConfigId, int64_t in_bufferSizeFrames,
153 std::shared_ptr<IStreamCallback> asyncCallback,
154 std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000155 if (in_bufferSizeFrames <= 0) {
156 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
157 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
158 }
159 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
160 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
161 << ", must be at least " << kMinimumStreamBufferSizeFrames;
162 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
163 }
164 auto& configs = getConfig().portConfigs;
165 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000166 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000167 // validity of the portConfigId has already been checked.
168 const size_t frameSize =
169 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
170 if (frameSize == 0) {
171 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
172 << portConfigIt->toString();
173 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
174 }
175 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700176 if (frameSize > static_cast<size_t>(kMaximumStreamBufferSizeBytes / in_bufferSizeFrames)) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000177 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
178 << " frames is too large, maximum size is "
179 << kMaximumStreamBufferSizeBytes / frameSize;
180 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
181 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000182 const auto& flags = portConfigIt->flags.value();
183 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000184 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
185 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000186 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000187 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
188 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +0000189 StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs,
Mikhail Naganov194daaa2023-01-05 22:34:20 +0000190 mVendorDebug.forceTransientBurst,
191 mVendorDebug.forceSynchronousDrain};
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000192 StreamContext temp(
193 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
194 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530195 portConfigIt->portId, portConfigIt->format.value(),
196 portConfigIt->channelMask.value(), portConfigIt->sampleRate.value().value, flags,
Mikhail Naganovb42a69e2023-06-16 12:38:25 -0700197 portConfigIt->ext.get<AudioPortExt::mix>().handle,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000198 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov8651b362023-01-06 23:15:27 +0000199 asyncCallback, outEventCallback, params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000200 if (temp.isValid()) {
201 *out_context = std::move(temp);
202 } else {
203 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
204 }
205 } else {
206 // TODO: Implement simulation of MMAP buffer allocation
207 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000208 return ndk::ScopedAStatus::ok();
209}
210
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000211std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
212 std::vector<AudioDevice> result;
213 auto& ports = getConfig().ports;
214 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
215 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
216 auto portIt = findById<AudioPort>(ports, *it);
217 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
218 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
219 }
220 }
221 return result;
222}
223
224std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
225 std::set<int32_t> result;
226 auto patchIdsRange = mPatches.equal_range(portConfigId);
227 auto& patches = getConfig().patches;
228 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
229 auto patchIt = findById<AudioPatch>(patches, it->second);
230 if (patchIt == patches.end()) {
231 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
232 << "not found in the configuration";
233 }
234 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
235 portConfigId) != patchIt->sourcePortConfigIds.end()) {
236 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
237 } else {
238 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
239 }
240 }
241 return result;
242}
243
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000244ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
245 auto& configs = getConfig().portConfigs;
246 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
247 if (portConfigIt == configs.end()) {
248 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
249 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
250 }
251 const int32_t portId = portConfigIt->portId;
252 // In our implementation, configs of mix ports always have unique IDs.
253 CHECK(portId != in_portConfigId);
254 auto& ports = getConfig().ports;
255 auto portIt = findById<AudioPort>(ports, portId);
256 if (portIt == ports.end()) {
257 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
258 << in_portConfigId << " not found";
259 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
260 }
261 if (mStreams.count(in_portConfigId) != 0) {
262 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
263 << " already has a stream opened on it";
264 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
265 }
266 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
267 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
268 << " does not correspond to a mix port";
269 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
270 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700271 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000272 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
273 LOG(ERROR) << __func__ << ": port id " << portId
274 << " has already reached maximum allowed opened stream count: "
275 << maxOpenStreamCount;
276 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
277 }
278 *port = &(*portIt);
279 return ndk::ScopedAStatus::ok();
280}
281
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000282template <typename C>
283std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
284 std::set<int32_t> result;
285 auto& portConfigs = getConfig().portConfigs;
286 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
287 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
288 if (portConfigIt != portConfigs.end()) {
289 result.insert(portConfigIt->portId);
290 }
291 }
292 return result;
293}
294
Peter Yoon918a6a52023-07-13 17:04:37 +0900295std::unique_ptr<internal::Configuration> Module::initializeConfig() {
296 std::unique_ptr<internal::Configuration> config;
297 switch (getType()) {
298 case Type::DEFAULT:
299 config = std::move(internal::getPrimaryConfiguration());
300 break;
301 case Type::R_SUBMIX:
302 config = std::move(internal::getRSubmixConfiguration());
303 break;
304 case Type::STUB:
305 config = std::move(internal::getStubConfiguration());
306 break;
307 case Type::USB:
308 config = std::move(internal::getUsbConfiguration());
309 break;
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700310 case Type::BLUETOOTH:
311 config = std::move(internal::getBluetoothConfiguration());
312 break;
Peter Yoon918a6a52023-07-13 17:04:37 +0900313 }
314 return config;
315}
316
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000317internal::Configuration& Module::getConfig() {
318 if (!mConfig) {
Peter Yoon918a6a52023-07-13 17:04:37 +0900319 mConfig = std::move(initializeConfig());
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000320 }
321 return *mConfig;
322}
323
324void Module::registerPatch(const AudioPatch& patch) {
325 auto& configs = getConfig().portConfigs;
326 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
327 for (auto portConfigId : portConfigIds) {
328 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
329 if (configIt != configs.end()) {
330 mPatches.insert(std::pair{portConfigId, patch.id});
331 if (configIt->portId != portConfigId) {
332 mPatches.insert(std::pair{configIt->portId, patch.id});
333 }
334 }
335 };
336 };
337 do_insert(patch.sourcePortConfigIds);
338 do_insert(patch.sinkPortConfigIds);
339}
340
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700341ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
342 const AudioPatch& newPatch) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000343 // Streams from the old patch need to be disconnected, streams from the new
344 // patch need to be connected. If the stream belongs to both patches, no need
345 // to update it.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700346 auto maybeFailure = ndk::ScopedAStatus::ok();
347 std::set<int32_t> idsToDisconnect, idsToConnect, idsToDisconnectOnFailure;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000348 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
349 oldPatch.sourcePortConfigIds.end());
350 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
351 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
352 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
353 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700354 if (idsToConnect.count(portConfigId) == 0 && mStreams.count(portConfigId) != 0) {
355 if (auto status = mStreams.setStreamConnectedDevices(portConfigId, {}); status.isOk()) {
356 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
357 << portConfigId << " has been disconnected";
358 } else {
359 // Disconnection is tricky to roll back, just register a failure.
360 maybeFailure = std::move(status);
361 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000362 }
363 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700364 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000365 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700366 if (idsToDisconnect.count(portConfigId) == 0 && mStreams.count(portConfigId) != 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000367 const auto connectedDevices = findConnectedDevices(portConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700368 if (connectedDevices.empty()) {
369 // This is important as workers use the vector size to derive the connection status.
370 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
371 "config id "
372 << portConfigId;
373 }
374 if (auto status = mStreams.setStreamConnectedDevices(portConfigId, connectedDevices);
375 status.isOk()) {
376 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
377 << portConfigId << " has been connected to: "
378 << ::android::internal::ToString(connectedDevices);
379 } else {
380 maybeFailure = std::move(status);
381 idsToDisconnectOnFailure.insert(portConfigId);
382 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000383 }
384 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700385 if (!maybeFailure.isOk()) {
386 LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids "
387 << ::android::internal::ToString(idsToDisconnectOnFailure);
388 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
389 [&](const auto& portConfigId) {
390 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
391 (void)status.isOk(); // Can't do much about a failure here.
392 });
393 return maybeFailure;
394 }
395 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000396}
397
Mikhail Naganov00603d12022-05-02 22:52:13 +0000398ndk::ScopedAStatus Module::setModuleDebug(
399 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700400 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000401 << ", new flags: " << in_debug.toString();
402 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
403 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700404 LOG(ERROR) << __func__ << ": " << mType
405 << ": attempting to change device connections simulation while having external "
406 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000407 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
408 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000409 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700410 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000411 << in_debug.streamTransientStateDelayMs;
412 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
413 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000414 mDebug = in_debug;
415 return ndk::ScopedAStatus::ok();
416}
417
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000418ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700419 *_aidl_return = nullptr;
420 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000421 return ndk::ScopedAStatus::ok();
422}
423
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000424ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700425 *_aidl_return = nullptr;
426 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000427 return ndk::ScopedAStatus::ok();
428}
429
Mikhail Naganov7499a002023-02-27 18:51:44 -0800430ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700431 *_aidl_return = nullptr;
432 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800433 return ndk::ScopedAStatus::ok();
434}
435
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800436ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700437 *_aidl_return = nullptr;
438 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800439 return ndk::ScopedAStatus::ok();
440}
441
Mikhail Naganov00603d12022-05-02 22:52:13 +0000442ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
443 AudioPort* _aidl_return) {
444 const int32_t templateId = in_templateIdAndAdditionalData.id;
445 auto& ports = getConfig().ports;
446 AudioPort connectedPort;
447 { // Scope the template port so that we don't accidentally modify it.
448 auto templateIt = findById<AudioPort>(ports, templateId);
449 if (templateIt == ports.end()) {
450 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
451 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
452 }
453 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
454 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
455 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
456 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000457 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
458 if (templateDevicePort.device.type.connection.empty()) {
459 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
460 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
461 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700462 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
463 LOG(ERROR) << __func__ << ": port id " << templateId << " is a connected device port";
464 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
465 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000466 // Postpone id allocation until we ensure that there are no client errors.
467 connectedPort = *templateIt;
468 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
469 const auto& inputDevicePort =
470 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
471 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
472 connectedDevicePort.device.address = inputDevicePort.device.address;
473 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
474 << connectedDevicePort.device.toString();
475 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700476 for (auto connectedPortPair : mConnectedDevicePorts) {
477 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000478 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
479 connectedDevicePort.device) {
480 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700481 << " is already connected at the device port id "
482 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000483 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
484 }
485 }
486 }
487
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700488 if (connectedPort.profiles.empty()) {
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700489 if (!mDebug.simulateDeviceConnections) {
490 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
491 } else {
492 auto& connectedProfiles = getConfig().connectedProfiles;
493 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
494 connectedProfilesIt != connectedProfiles.end()) {
495 connectedPort.profiles = connectedProfilesIt->second;
496 }
497 }
498 if (connectedPort.profiles.empty()) {
499 LOG(ERROR) << __func__
500 << ": profiles of a connected port still empty after connecting external "
501 "device "
502 << connectedPort.toString();
503 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
504 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000505 }
506
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530507 for (auto profile : connectedPort.profiles) {
508 if (profile.channelMasks.empty()) {
509 LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no channel masks";
510 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
511 }
512 if (profile.sampleRates.empty()) {
513 LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no sample rates";
514 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
515 }
516 }
517
Mikhail Naganov00603d12022-05-02 22:52:13 +0000518 connectedPort.id = ++getConfig().nextPortId;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700519 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700520 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000521 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
522 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000523 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000524 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000525
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700526 std::vector<int32_t> routablePortIds;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000527 std::vector<AudioRoute> newRoutes;
528 auto& routes = getConfig().routes;
529 for (auto& r : routes) {
530 if (r.sinkPortId == templateId) {
531 AudioRoute newRoute;
532 newRoute.sourcePortIds = r.sourcePortIds;
533 newRoute.sinkPortId = connectedPort.id;
534 newRoute.isExclusive = r.isExclusive;
535 newRoutes.push_back(std::move(newRoute));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700536 routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(),
537 r.sourcePortIds.end());
Mikhail Naganov00603d12022-05-02 22:52:13 +0000538 } else {
539 auto& srcs = r.sourcePortIds;
540 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
541 srcs.push_back(connectedPort.id);
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700542 routablePortIds.push_back(r.sinkPortId);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000543 }
544 }
545 }
546 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
547
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700548 // Note: this is a simplistic approach assuming that a mix port can only be populated
549 // from a single device port. Implementing support for stuffing dynamic profiles with a superset
550 // of all profiles from all routable dynamic device ports would be more involved.
551 for (const auto mixPortId : routablePortIds) {
552 auto portsIt = findById<AudioPort>(ports, mixPortId);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700553 if (portsIt != ports.end()) {
554 if (portsIt->profiles.empty()) {
555 portsIt->profiles = connectedPort.profiles;
556 connectedPortsIt->second.insert(portsIt->id);
557 } else {
558 // Check if profiles are non empty because they were populated by
559 // a previous connection. Otherwise, it means that they are not empty because
560 // the mix port has static profiles.
561 for (const auto cp : mConnectedDevicePorts) {
562 if (cp.second.count(portsIt->id) > 0) {
563 connectedPortsIt->second.insert(portsIt->id);
564 break;
565 }
566 }
567 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700568 }
569 }
570 *_aidl_return = std::move(connectedPort);
571
Mikhail Naganov00603d12022-05-02 22:52:13 +0000572 return ndk::ScopedAStatus::ok();
573}
574
575ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
576 auto& ports = getConfig().ports;
577 auto portIt = findById<AudioPort>(ports, in_portId);
578 if (portIt == ports.end()) {
579 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
580 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
581 }
582 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
583 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
584 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
585 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700586 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
587 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000588 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
589 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
590 }
591 auto& configs = getConfig().portConfigs;
592 auto& initials = getConfig().initialConfigs;
593 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
594 if (config.portId == in_portId) {
595 // Check if the configuration was provided by the client.
596 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
597 return initialIt == initials.end() || config != *initialIt;
598 }
599 return false;
600 });
601 if (configIt != configs.end()) {
602 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
603 << configIt->id;
604 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
605 }
jiabin783c48b2023-02-28 18:28:06 +0000606 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000607 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000608 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
609
610 auto& routes = getConfig().routes;
611 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
612 if (routesIt->sinkPortId == in_portId) {
613 routesIt = routes.erase(routesIt);
614 } else {
615 // Note: the list of sourcePortIds can't become empty because there must
616 // be the id of the template port in the route.
617 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
618 ++routesIt;
619 }
620 }
621
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700622 // Clear profiles for mix ports that are not connected to any other ports.
623 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
624 mConnectedDevicePorts.erase(connectedPortsIt);
625 for (const auto& connectedPort : mConnectedDevicePorts) {
626 for (int32_t mixPortId : connectedPort.second) {
627 mixPortsToClear.erase(mixPortId);
628 }
629 }
630 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700631 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
632 if (mixPortIt != ports.end()) {
633 mixPortIt->profiles = {};
634 }
635 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700636
Mikhail Naganov00603d12022-05-02 22:52:13 +0000637 return ndk::ScopedAStatus::ok();
638}
639
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000640ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
641 *_aidl_return = getConfig().patches;
642 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
643 return ndk::ScopedAStatus::ok();
644}
645
646ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
647 auto& ports = getConfig().ports;
648 auto portIt = findById<AudioPort>(ports, in_portId);
649 if (portIt != ports.end()) {
650 *_aidl_return = *portIt;
651 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
652 return ndk::ScopedAStatus::ok();
653 }
654 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
655 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
656}
657
658ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
659 *_aidl_return = getConfig().portConfigs;
660 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
661 return ndk::ScopedAStatus::ok();
662}
663
664ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
665 *_aidl_return = getConfig().ports;
666 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
667 return ndk::ScopedAStatus::ok();
668}
669
670ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
671 *_aidl_return = getConfig().routes;
672 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
673 return ndk::ScopedAStatus::ok();
674}
675
Mikhail Naganov00603d12022-05-02 22:52:13 +0000676ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
677 std::vector<AudioRoute>* _aidl_return) {
678 auto& ports = getConfig().ports;
679 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
680 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
681 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
682 }
683 auto& routes = getConfig().routes;
684 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
685 [&](const auto& r) {
686 const auto& srcs = r.sourcePortIds;
687 return r.sinkPortId == in_portId ||
688 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
689 });
690 return ndk::ScopedAStatus::ok();
691}
692
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000693ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
694 OpenInputStreamReturn* _aidl_return) {
695 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
696 << in_args.bufferSizeFrames << " frames";
697 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700698 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000699 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
700 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000701 << " does not correspond to an input mix port";
702 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
703 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000704 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700705 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
706 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000707 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000708 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700709 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700710 mConfig->microphones, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000711 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700712 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
713 RETURN_STATUS_IF_ERROR(
714 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
715 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000716 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
717 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000718 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000719 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000720 return ndk::ScopedAStatus::ok();
721}
722
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000723ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
724 OpenOutputStreamReturn* _aidl_return) {
725 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
726 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
727 << " frames";
728 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700729 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000730 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
731 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000732 << " does not correspond to an output mix port";
733 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
734 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000735 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
736 AudioOutputFlags::COMPRESS_OFFLOAD);
737 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000738 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000739 << " has COMPRESS_OFFLOAD flag set, requires offload info";
740 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
741 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000742 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
743 AudioOutputFlags::NON_BLOCKING);
744 if (isNonBlocking && in_args.callback == nullptr) {
745 LOG(ERROR) << __func__ << ": port id " << port->id
746 << " has NON_BLOCKING flag set, requires async callback";
747 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
748 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000749 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700750 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
751 isNonBlocking ? in_args.callback : nullptr,
752 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000753 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000754 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700755 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700756 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000757 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700758 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
759 RETURN_STATUS_IF_ERROR(
760 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
761 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000762 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
763 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000764 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000765 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000766 return ndk::ScopedAStatus::ok();
767}
768
Mikhail Naganov74927202022-12-19 16:37:14 +0000769ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
770 SupportedPlaybackRateFactors* _aidl_return) {
771 LOG(DEBUG) << __func__;
772 (void)_aidl_return;
773 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
774}
775
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000776ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000777 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000778 if (in_requested.sourcePortConfigIds.empty()) {
779 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
780 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
781 }
782 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
783 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
784 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
785 }
786 if (in_requested.sinkPortConfigIds.empty()) {
787 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
788 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
789 }
790 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
791 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
792 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
793 }
794
795 auto& configs = getConfig().portConfigs;
796 std::vector<int32_t> missingIds;
797 auto sources =
798 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
799 if (!missingIds.empty()) {
800 LOG(ERROR) << __func__ << ": following source port config ids not found: "
801 << ::android::internal::ToString(missingIds);
802 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
803 }
804 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
805 if (!missingIds.empty()) {
806 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
807 << ::android::internal::ToString(missingIds);
808 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
809 }
810 // bool indicates whether a non-exclusive route is available.
811 // If only an exclusive route is available, that means the patch can not be
812 // established if there is any other patch which currently uses the sink port.
813 std::map<int32_t, bool> allowedSinkPorts;
814 auto& routes = getConfig().routes;
815 for (auto src : sources) {
816 for (const auto& r : routes) {
817 const auto& srcs = r.sourcePortIds;
818 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
819 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
820 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
821 }
822 }
823 }
824 }
825 for (auto sink : sinks) {
826 if (allowedSinkPorts.count(sink->portId) == 0) {
827 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
828 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
829 }
830 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700831 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000832
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000833 auto& patches = getConfig().patches;
834 auto existing = patches.end();
835 std::optional<decltype(mPatches)> patchesBackup;
836 if (in_requested.id != 0) {
837 existing = findById<AudioPatch>(patches, in_requested.id);
838 if (existing != patches.end()) {
839 patchesBackup = mPatches;
840 cleanUpPatch(existing->id);
841 } else {
842 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
843 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
844 }
845 }
846 // Validate the requested patch.
847 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
848 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
849 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
850 << "is exclusive and is already used by some other patch";
851 if (patchesBackup.has_value()) {
852 mPatches = std::move(*patchesBackup);
853 }
854 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
855 }
856 }
857 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000858 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
859 _aidl_return->latenciesMs.clear();
860 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
861 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000862 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000863 if (existing == patches.end()) {
864 _aidl_return->id = getConfig().nextPatchId++;
865 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000866 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000867 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000868 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700869 patchesBackup = mPatches;
870 registerPatch(*_aidl_return);
871 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
872 mPatches = std::move(*patchesBackup);
873 if (existing == patches.end()) {
874 patches.pop_back();
875 } else {
876 *existing = oldPatch;
877 }
878 return status;
879 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000880
881 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
882 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000883 return ndk::ScopedAStatus::ok();
884}
885
886ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
887 AudioPortConfig* out_suggested, bool* _aidl_return) {
888 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
889 auto& configs = getConfig().portConfigs;
890 auto existing = configs.end();
891 if (in_requested.id != 0) {
892 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
893 existing == configs.end()) {
894 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
895 << " not found";
896 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
897 }
898 }
899
900 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
901 if (portId == 0) {
902 LOG(ERROR) << __func__ << ": input port config does not specify portId";
903 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
904 }
905 auto& ports = getConfig().ports;
906 auto portIt = findById<AudioPort>(ports, portId);
907 if (portIt == ports.end()) {
908 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
909 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
910 }
911 if (existing != configs.end()) {
912 *out_suggested = *existing;
913 } else {
914 AudioPortConfig newConfig;
915 if (generateDefaultPortConfig(*portIt, &newConfig)) {
916 *out_suggested = newConfig;
917 } else {
918 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
919 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
920 }
921 }
922 // From this moment, 'out_suggested' is either an existing port config,
923 // or a new generated config. Now attempt to update it according to the specified
924 // fields of 'in_requested'.
925
926 bool requestedIsValid = true, requestedIsFullySpecified = true;
927
928 AudioIoFlags portFlags = portIt->flags;
929 if (in_requested.flags.has_value()) {
930 if (in_requested.flags.value() != portFlags) {
931 LOG(WARNING) << __func__ << ": requested flags "
932 << in_requested.flags.value().toString() << " do not match port's "
933 << portId << " flags " << portFlags.toString();
934 requestedIsValid = false;
935 }
936 } else {
937 requestedIsFullySpecified = false;
938 }
939
940 AudioProfile portProfile;
941 if (in_requested.format.has_value()) {
942 const auto& format = in_requested.format.value();
943 if (findAudioProfile(*portIt, format, &portProfile)) {
944 out_suggested->format = format;
945 } else {
946 LOG(WARNING) << __func__ << ": requested format " << format.toString()
947 << " is not found in port's " << portId << " profiles";
948 requestedIsValid = false;
949 }
950 } else {
951 requestedIsFullySpecified = false;
952 }
953 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
954 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
955 << out_suggested->format.value().toString() << " anymore";
956 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
957 }
958
959 if (in_requested.channelMask.has_value()) {
960 const auto& channelMask = in_requested.channelMask.value();
961 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
962 portProfile.channelMasks.end()) {
963 out_suggested->channelMask = channelMask;
964 } else {
965 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
966 << " is not supported for the format " << portProfile.format.toString()
967 << " by the port " << portId;
968 requestedIsValid = false;
969 }
970 } else {
971 requestedIsFullySpecified = false;
972 }
973
974 if (in_requested.sampleRate.has_value()) {
975 const auto& sampleRate = in_requested.sampleRate.value();
976 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
977 sampleRate.value) != portProfile.sampleRates.end()) {
978 out_suggested->sampleRate = sampleRate;
979 } else {
980 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
981 << " is not supported for the format " << portProfile.format.toString()
982 << " by the port " << portId;
983 requestedIsValid = false;
984 }
985 } else {
986 requestedIsFullySpecified = false;
987 }
988
989 if (in_requested.gain.has_value()) {
990 // Let's pretend that gain can always be applied.
991 out_suggested->gain = in_requested.gain.value();
992 }
993
Mikhail Naganov248e9502023-02-21 16:32:40 -0800994 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
995 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
996 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
997 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
998 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
999 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
1000 }
1001 } else {
1002 LOG(WARNING) << __func__ << ": requested ext tag "
1003 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1004 << toString(out_suggested->ext.getTag());
1005 requestedIsValid = false;
1006 }
1007 }
1008
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001009 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1010 out_suggested->id = getConfig().nextPortId++;
1011 configs.push_back(*out_suggested);
1012 *_aidl_return = true;
1013 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
1014 } else if (existing != configs.end() && requestedIsValid) {
1015 *existing = *out_suggested;
1016 *_aidl_return = true;
1017 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
1018 } else {
1019 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
1020 << "; requested is valid? " << requestedIsValid << ", fully specified? "
1021 << requestedIsFullySpecified;
1022 *_aidl_return = false;
1023 }
1024 return ndk::ScopedAStatus::ok();
1025}
1026
1027ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1028 auto& patches = getConfig().patches;
1029 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1030 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001031 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001032 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001033 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1034 mPatches = std::move(patchesBackup);
1035 return status;
1036 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001037 patches.erase(patchIt);
1038 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1039 return ndk::ScopedAStatus::ok();
1040 }
1041 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1042 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1043}
1044
1045ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1046 auto& configs = getConfig().portConfigs;
1047 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1048 if (configIt != configs.end()) {
1049 if (mStreams.count(in_portConfigId) != 0) {
1050 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1051 << " has a stream opened on it";
1052 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1053 }
1054 auto patchIt = mPatches.find(in_portConfigId);
1055 if (patchIt != mPatches.end()) {
1056 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1057 << " is used by the patch with id " << patchIt->second;
1058 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1059 }
1060 auto& initials = getConfig().initialConfigs;
1061 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1062 if (initialIt == initials.end()) {
1063 configs.erase(configIt);
1064 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1065 } else if (*configIt != *initialIt) {
1066 *configIt = *initialIt;
1067 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1068 }
1069 return ndk::ScopedAStatus::ok();
1070 }
1071 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1072 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1073}
1074
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001075ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1076 *_aidl_return = mMasterMute;
1077 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1078 return ndk::ScopedAStatus::ok();
1079}
1080
1081ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1082 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001083 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1084 : onMasterMuteChanged(in_mute);
1085 if (result.isOk()) {
1086 mMasterMute = in_mute;
1087 } else {
1088 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1089 << "), error=" << result;
1090 // Reset master mute if it failed.
1091 onMasterMuteChanged(mMasterMute);
1092 }
1093 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001094}
1095
1096ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1097 *_aidl_return = mMasterVolume;
1098 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1099 return ndk::ScopedAStatus::ok();
1100}
1101
1102ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1103 LOG(DEBUG) << __func__ << ": " << in_volume;
1104 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001105 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1106 : onMasterVolumeChanged(in_volume);
1107 if (result.isOk()) {
1108 mMasterVolume = in_volume;
1109 } else {
1110 // Reset master volume if it failed.
1111 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1112 << "), error=" << result;
1113 onMasterVolumeChanged(mMasterVolume);
1114 }
1115 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001116 }
1117 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1118 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1119}
1120
1121ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1122 *_aidl_return = mMicMute;
1123 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1124 return ndk::ScopedAStatus::ok();
1125}
1126
1127ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1128 LOG(DEBUG) << __func__ << ": " << in_mute;
1129 mMicMute = in_mute;
1130 return ndk::ScopedAStatus::ok();
1131}
1132
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001133ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001134 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001135 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1136 return ndk::ScopedAStatus::ok();
1137}
1138
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001139ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001140 if (!isValidAudioMode(in_mode)) {
1141 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1142 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1143 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001144 // No checks for supported audio modes here, it's an informative notification.
1145 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1146 return ndk::ScopedAStatus::ok();
1147}
1148
1149ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1150 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1151 return ndk::ScopedAStatus::ok();
1152}
1153
1154ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1155 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1156 return ndk::ScopedAStatus::ok();
1157}
1158
Vlad Popa83a6d822022-11-07 13:53:57 +01001159ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001160 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001161 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001162 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001163 *_aidl_return = mSoundDose.getInstance();
Vlad Popa943b7e22022-12-08 14:24:12 +01001164 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001165 return ndk::ScopedAStatus::ok();
1166}
1167
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001168ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1169 LOG(DEBUG) << __func__;
1170 (void)_aidl_return;
1171 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1172}
1173
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001174const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001175const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001176
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001177ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1178 std::vector<VendorParameter>* _aidl_return) {
1179 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001180 bool allParametersKnown = true;
1181 for (const auto& id : in_ids) {
1182 if (id == VendorDebug::kForceTransientBurstName) {
1183 VendorParameter forceTransientBurst{.id = id};
1184 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1185 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001186 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1187 VendorParameter forceSynchronousDrain{.id = id};
1188 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1189 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001190 } else {
1191 allParametersKnown = false;
1192 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1193 }
1194 }
1195 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1196 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001197}
1198
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001199namespace {
1200
1201template <typename W>
1202bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1203 std::optional<W> value;
1204 binder_status_t result = p.ext.getParcelable(&value);
1205 if (result == STATUS_OK && value.has_value()) {
1206 *v = value.value().value;
1207 return true;
1208 }
1209 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1210 << "\": " << result;
1211 return false;
1212}
1213
1214} // namespace
1215
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001216ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1217 bool in_async) {
1218 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1219 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001220 bool allParametersKnown = true;
1221 for (const auto& p : in_parameters) {
1222 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001223 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1224 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1225 }
1226 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1227 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001228 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1229 }
1230 } else {
1231 allParametersKnown = false;
1232 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1233 }
1234 }
1235 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1236 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001237}
1238
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001239ndk::ScopedAStatus Module::addDeviceEffect(
1240 int32_t in_portConfigId,
1241 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1242 if (in_effect == nullptr) {
1243 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1244 } else {
1245 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1246 << in_effect->asBinder().get();
1247 }
1248 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1249}
1250
1251ndk::ScopedAStatus Module::removeDeviceEffect(
1252 int32_t in_portConfigId,
1253 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1254 if (in_effect == nullptr) {
1255 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1256 } else {
1257 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1258 << in_effect->asBinder().get();
1259 }
1260 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1261}
1262
jiabin9a8e6862023-01-12 23:06:37 +00001263ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1264 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1265 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1266 std::set<int32_t> mmapSinks;
1267 std::set<int32_t> mmapSources;
1268 auto& ports = getConfig().ports;
1269 for (const auto& port : ports) {
1270 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1271 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1272 AudioInputFlags::MMAP_NOIRQ)) {
1273 mmapSinks.insert(port.id);
1274 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1275 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1276 AudioOutputFlags::MMAP_NOIRQ)) {
1277 mmapSources.insert(port.id);
1278 }
1279 }
1280 for (const auto& route : getConfig().routes) {
1281 if (mmapSinks.count(route.sinkPortId) != 0) {
1282 // The sink is a mix port, add the sources if they are device ports.
1283 for (int sourcePortId : route.sourcePortIds) {
1284 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1285 if (sourcePortIt == ports.end()) {
1286 // This must not happen
1287 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1288 continue;
1289 }
1290 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1291 // The source is not a device port, skip
1292 continue;
1293 }
1294 AudioMMapPolicyInfo policyInfo;
1295 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1296 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1297 // default implementation.
1298 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1299 _aidl_return->push_back(policyInfo);
1300 }
1301 } else {
1302 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1303 if (sinkPortIt == ports.end()) {
1304 // This must not happen
1305 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1306 continue;
1307 }
1308 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1309 // The sink is not a device port, skip
1310 continue;
1311 }
1312 if (count_any(mmapSources, route.sourcePortIds)) {
1313 AudioMMapPolicyInfo policyInfo;
1314 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1315 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1316 // default implementation.
1317 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1318 _aidl_return->push_back(policyInfo);
1319 }
1320 }
1321 }
1322 return ndk::ScopedAStatus::ok();
1323}
1324
Eric Laurente2432ea2023-01-12 17:47:31 +01001325ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1326 LOG(DEBUG) << __func__;
1327 *_aidl_return = false;
1328 return ndk::ScopedAStatus::ok();
1329}
1330
jiabinb76981e2023-01-18 00:58:30 +00001331ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1332 if (!isMmapSupported()) {
1333 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1334 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1335 }
1336 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1337 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1338 return ndk::ScopedAStatus::ok();
1339}
1340
1341ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1342 if (!isMmapSupported()) {
1343 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1344 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1345 }
1346 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1347 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1348 return ndk::ScopedAStatus::ok();
1349}
1350
1351bool Module::isMmapSupported() {
1352 if (mIsMmapSupported.has_value()) {
1353 return mIsMmapSupported.value();
1354 }
1355 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1356 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1357 mIsMmapSupported = false;
1358 } else {
1359 mIsMmapSupported =
1360 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1361 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1362 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1363 }) != mmapPolicyInfos.end();
1364 }
1365 return mIsMmapSupported.value();
1366}
1367
jiabin253bd322023-01-25 23:57:31 +00001368ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001369 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001370 return ndk::ScopedAStatus::ok();
1371}
1372
1373ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1374 const std::vector<AudioPortConfig*>& sources __unused,
1375 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001376 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001377 return ndk::ScopedAStatus::ok();
1378}
1379
jiabin783c48b2023-02-28 18:28:06 +00001380void Module::onExternalDeviceConnectionChanged(
1381 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1382 bool connected __unused) {
1383 LOG(DEBUG) << __func__ << ": do nothing and return";
1384}
1385
1386ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1387 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1388 return ndk::ScopedAStatus::ok();
1389}
1390
1391ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1392 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1393 return ndk::ScopedAStatus::ok();
1394}
1395
Ram Mohan18f0d512023-07-01 00:47:09 +05301396Module::BtProfileHandles Module::getBtProfileManagerHandles() {
1397 return std::make_tuple(std::weak_ptr<IBluetooth>(), std::weak_ptr<IBluetoothA2dp>(),
1398 std::weak_ptr<IBluetoothLe>());
1399}
1400
1401ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1402 return mStreams.bluetoothParametersUpdated();
1403}
1404
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001405} // namespace aidl::android::hardware::audio::core