blob: 76132b38fe21f40648bdaa4b6e325ab065c66e97 [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 Naganov89a8ea92023-09-29 17:02:12 -0700343 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700344 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700345 using Connections =
346 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
347 Connections oldConnections, newConnections;
348 auto fillConnectionsHelper = [&](Connections& connections,
349 const std::vector<int32_t>& mixPortCfgIds,
350 const std::vector<int32_t>& devicePortCfgIds) {
351 for (int32_t mixPortCfgId : mixPortCfgIds) {
352 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
353 }
354 };
355 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
356 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
357 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
358 patch.sourcePortConfigIds.end()) {
359 // Sources are mix ports.
360 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
361 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
362 [&](int32_t portConfigId) {
363 return mStreams.count(portConfigId) > 0;
364 }) != patch.sinkPortConfigIds.end()) {
365 // Sources are device ports.
366 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
367 } // Otherwise, there are no streams to notify.
368 };
369 fillConnections(oldConnections, oldPatch);
370 fillConnections(newConnections, newPatch);
371
372 std::for_each(oldConnections.begin(), oldConnections.end(), [&](const auto& connectionPair) {
373 const int32_t mixPortConfigId = connectionPair.first;
374 if (auto it = newConnections.find(mixPortConfigId);
375 it == newConnections.end() || it->second != connectionPair.second) {
376 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, {});
377 status.isOk()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700378 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700379 << mixPortConfigId << " has been disconnected";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700380 } else {
381 // Disconnection is tricky to roll back, just register a failure.
382 maybeFailure = std::move(status);
383 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000384 }
385 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700386 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700387 std::set<int32_t> idsToDisconnectOnFailure;
388 std::for_each(newConnections.begin(), newConnections.end(), [&](const auto& connectionPair) {
389 const int32_t mixPortConfigId = connectionPair.first;
390 if (auto it = oldConnections.find(mixPortConfigId);
391 it == oldConnections.end() || it->second != connectionPair.second) {
392 const auto connectedDevices = findConnectedDevices(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700393 if (connectedDevices.empty()) {
394 // This is important as workers use the vector size to derive the connection status.
395 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
396 "config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700397 << mixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700398 }
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700399 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700400 status.isOk()) {
401 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700402 << mixPortConfigId << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700403 << ::android::internal::ToString(connectedDevices);
404 } else {
405 maybeFailure = std::move(status);
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700406 idsToDisconnectOnFailure.insert(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700407 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000408 }
409 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700410 if (!maybeFailure.isOk()) {
411 LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids "
412 << ::android::internal::ToString(idsToDisconnectOnFailure);
413 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
414 [&](const auto& portConfigId) {
415 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
416 (void)status.isOk(); // Can't do much about a failure here.
417 });
418 return maybeFailure;
419 }
420 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000421}
422
Mikhail Naganov00603d12022-05-02 22:52:13 +0000423ndk::ScopedAStatus Module::setModuleDebug(
424 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700425 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000426 << ", new flags: " << in_debug.toString();
427 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
428 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700429 LOG(ERROR) << __func__ << ": " << mType
430 << ": attempting to change device connections simulation while having external "
431 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000432 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
433 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000434 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700435 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000436 << in_debug.streamTransientStateDelayMs;
437 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
438 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000439 mDebug = in_debug;
440 return ndk::ScopedAStatus::ok();
441}
442
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000443ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700444 *_aidl_return = nullptr;
445 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000446 return ndk::ScopedAStatus::ok();
447}
448
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000449ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700450 *_aidl_return = nullptr;
451 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000452 return ndk::ScopedAStatus::ok();
453}
454
Mikhail Naganov7499a002023-02-27 18:51:44 -0800455ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700456 *_aidl_return = nullptr;
457 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800458 return ndk::ScopedAStatus::ok();
459}
460
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800461ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700462 *_aidl_return = nullptr;
463 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800464 return ndk::ScopedAStatus::ok();
465}
466
Mikhail Naganov00603d12022-05-02 22:52:13 +0000467ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
468 AudioPort* _aidl_return) {
469 const int32_t templateId = in_templateIdAndAdditionalData.id;
470 auto& ports = getConfig().ports;
471 AudioPort connectedPort;
472 { // Scope the template port so that we don't accidentally modify it.
473 auto templateIt = findById<AudioPort>(ports, templateId);
474 if (templateIt == ports.end()) {
475 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
476 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
477 }
478 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
479 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
480 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
481 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000482 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
483 if (templateDevicePort.device.type.connection.empty()) {
484 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
485 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
486 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700487 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
488 LOG(ERROR) << __func__ << ": port id " << templateId << " is a connected device port";
489 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
490 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000491 // Postpone id allocation until we ensure that there are no client errors.
492 connectedPort = *templateIt;
493 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
494 const auto& inputDevicePort =
495 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
496 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
497 connectedDevicePort.device.address = inputDevicePort.device.address;
498 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
499 << connectedDevicePort.device.toString();
500 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700501 for (auto connectedPortPair : mConnectedDevicePorts) {
502 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000503 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
504 connectedDevicePort.device) {
505 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700506 << " is already connected at the device port id "
507 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000508 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
509 }
510 }
511 }
512
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700513 if (connectedPort.profiles.empty()) {
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700514 if (!mDebug.simulateDeviceConnections) {
515 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
516 } else {
517 auto& connectedProfiles = getConfig().connectedProfiles;
518 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
519 connectedProfilesIt != connectedProfiles.end()) {
520 connectedPort.profiles = connectedProfilesIt->second;
521 }
522 }
523 if (connectedPort.profiles.empty()) {
524 LOG(ERROR) << __func__
525 << ": profiles of a connected port still empty after connecting external "
526 "device "
527 << connectedPort.toString();
528 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
529 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000530 }
531
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530532 for (auto profile : connectedPort.profiles) {
533 if (profile.channelMasks.empty()) {
534 LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no channel masks";
535 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
536 }
537 if (profile.sampleRates.empty()) {
538 LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no sample rates";
539 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
540 }
541 }
542
Mikhail Naganovdc417732023-09-29 15:49:35 -0700543 connectedPort.id = getConfig().nextPortId++;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700544 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700545 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000546 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
547 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000548 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000549 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000550
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700551 std::vector<int32_t> routablePortIds;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000552 std::vector<AudioRoute> newRoutes;
553 auto& routes = getConfig().routes;
554 for (auto& r : routes) {
555 if (r.sinkPortId == templateId) {
556 AudioRoute newRoute;
557 newRoute.sourcePortIds = r.sourcePortIds;
558 newRoute.sinkPortId = connectedPort.id;
559 newRoute.isExclusive = r.isExclusive;
560 newRoutes.push_back(std::move(newRoute));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700561 routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(),
562 r.sourcePortIds.end());
Mikhail Naganov00603d12022-05-02 22:52:13 +0000563 } else {
564 auto& srcs = r.sourcePortIds;
565 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
566 srcs.push_back(connectedPort.id);
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700567 routablePortIds.push_back(r.sinkPortId);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000568 }
569 }
570 }
571 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
572
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700573 // Note: this is a simplistic approach assuming that a mix port can only be populated
574 // from a single device port. Implementing support for stuffing dynamic profiles with a superset
575 // of all profiles from all routable dynamic device ports would be more involved.
576 for (const auto mixPortId : routablePortIds) {
577 auto portsIt = findById<AudioPort>(ports, mixPortId);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700578 if (portsIt != ports.end()) {
579 if (portsIt->profiles.empty()) {
580 portsIt->profiles = connectedPort.profiles;
581 connectedPortsIt->second.insert(portsIt->id);
582 } else {
583 // Check if profiles are non empty because they were populated by
584 // a previous connection. Otherwise, it means that they are not empty because
585 // the mix port has static profiles.
586 for (const auto cp : mConnectedDevicePorts) {
587 if (cp.second.count(portsIt->id) > 0) {
588 connectedPortsIt->second.insert(portsIt->id);
589 break;
590 }
591 }
592 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700593 }
594 }
595 *_aidl_return = std::move(connectedPort);
596
Mikhail Naganov00603d12022-05-02 22:52:13 +0000597 return ndk::ScopedAStatus::ok();
598}
599
600ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
601 auto& ports = getConfig().ports;
602 auto portIt = findById<AudioPort>(ports, in_portId);
603 if (portIt == ports.end()) {
604 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
605 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
606 }
607 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
608 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
609 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
610 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700611 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
612 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000613 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
614 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
615 }
616 auto& configs = getConfig().portConfigs;
617 auto& initials = getConfig().initialConfigs;
618 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
619 if (config.portId == in_portId) {
620 // Check if the configuration was provided by the client.
621 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
622 return initialIt == initials.end() || config != *initialIt;
623 }
624 return false;
625 });
626 if (configIt != configs.end()) {
627 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
628 << configIt->id;
629 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
630 }
jiabin783c48b2023-02-28 18:28:06 +0000631 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000632 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000633 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
634
635 auto& routes = getConfig().routes;
636 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
637 if (routesIt->sinkPortId == in_portId) {
638 routesIt = routes.erase(routesIt);
639 } else {
640 // Note: the list of sourcePortIds can't become empty because there must
641 // be the id of the template port in the route.
642 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
643 ++routesIt;
644 }
645 }
646
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700647 // Clear profiles for mix ports that are not connected to any other ports.
648 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
649 mConnectedDevicePorts.erase(connectedPortsIt);
650 for (const auto& connectedPort : mConnectedDevicePorts) {
651 for (int32_t mixPortId : connectedPort.second) {
652 mixPortsToClear.erase(mixPortId);
653 }
654 }
655 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700656 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
657 if (mixPortIt != ports.end()) {
658 mixPortIt->profiles = {};
659 }
660 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700661
Mikhail Naganov00603d12022-05-02 22:52:13 +0000662 return ndk::ScopedAStatus::ok();
663}
664
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000665ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
666 *_aidl_return = getConfig().patches;
667 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
668 return ndk::ScopedAStatus::ok();
669}
670
671ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
672 auto& ports = getConfig().ports;
673 auto portIt = findById<AudioPort>(ports, in_portId);
674 if (portIt != ports.end()) {
675 *_aidl_return = *portIt;
676 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
677 return ndk::ScopedAStatus::ok();
678 }
679 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
680 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
681}
682
683ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
684 *_aidl_return = getConfig().portConfigs;
685 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
686 return ndk::ScopedAStatus::ok();
687}
688
689ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
690 *_aidl_return = getConfig().ports;
691 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
692 return ndk::ScopedAStatus::ok();
693}
694
695ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
696 *_aidl_return = getConfig().routes;
697 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
698 return ndk::ScopedAStatus::ok();
699}
700
Mikhail Naganov00603d12022-05-02 22:52:13 +0000701ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
702 std::vector<AudioRoute>* _aidl_return) {
703 auto& ports = getConfig().ports;
704 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
705 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
706 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
707 }
708 auto& routes = getConfig().routes;
709 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
710 [&](const auto& r) {
711 const auto& srcs = r.sourcePortIds;
712 return r.sinkPortId == in_portId ||
713 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
714 });
715 return ndk::ScopedAStatus::ok();
716}
717
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000718ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
719 OpenInputStreamReturn* _aidl_return) {
720 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
721 << in_args.bufferSizeFrames << " frames";
722 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700723 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000724 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
725 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000726 << " does not correspond to an input mix port";
727 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
728 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000729 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700730 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
731 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000732 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000733 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700734 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700735 mConfig->microphones, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000736 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700737 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
738 RETURN_STATUS_IF_ERROR(
739 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
740 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000741 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
742 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000743 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000744 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000745 return ndk::ScopedAStatus::ok();
746}
747
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000748ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
749 OpenOutputStreamReturn* _aidl_return) {
750 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
751 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
752 << " frames";
753 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700754 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000755 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
756 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000757 << " does not correspond to an output mix port";
758 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
759 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000760 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
761 AudioOutputFlags::COMPRESS_OFFLOAD);
762 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000763 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000764 << " has COMPRESS_OFFLOAD flag set, requires offload info";
765 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
766 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000767 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
768 AudioOutputFlags::NON_BLOCKING);
769 if (isNonBlocking && in_args.callback == nullptr) {
770 LOG(ERROR) << __func__ << ": port id " << port->id
771 << " has NON_BLOCKING flag set, requires async callback";
772 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
773 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000774 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700775 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
776 isNonBlocking ? in_args.callback : nullptr,
777 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000778 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000779 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700780 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700781 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000782 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700783 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
784 RETURN_STATUS_IF_ERROR(
785 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
786 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000787 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
788 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000789 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000790 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000791 return ndk::ScopedAStatus::ok();
792}
793
Mikhail Naganov74927202022-12-19 16:37:14 +0000794ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
795 SupportedPlaybackRateFactors* _aidl_return) {
796 LOG(DEBUG) << __func__;
797 (void)_aidl_return;
798 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
799}
800
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000801ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000802 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000803 if (in_requested.sourcePortConfigIds.empty()) {
804 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
805 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
806 }
807 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
808 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
809 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
810 }
811 if (in_requested.sinkPortConfigIds.empty()) {
812 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
813 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
814 }
815 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
816 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
817 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
818 }
819
820 auto& configs = getConfig().portConfigs;
821 std::vector<int32_t> missingIds;
822 auto sources =
823 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
824 if (!missingIds.empty()) {
825 LOG(ERROR) << __func__ << ": following source port config ids not found: "
826 << ::android::internal::ToString(missingIds);
827 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
828 }
829 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
830 if (!missingIds.empty()) {
831 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
832 << ::android::internal::ToString(missingIds);
833 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
834 }
835 // bool indicates whether a non-exclusive route is available.
836 // If only an exclusive route is available, that means the patch can not be
837 // established if there is any other patch which currently uses the sink port.
838 std::map<int32_t, bool> allowedSinkPorts;
839 auto& routes = getConfig().routes;
840 for (auto src : sources) {
841 for (const auto& r : routes) {
842 const auto& srcs = r.sourcePortIds;
843 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
844 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
845 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
846 }
847 }
848 }
849 }
850 for (auto sink : sinks) {
851 if (allowedSinkPorts.count(sink->portId) == 0) {
852 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
853 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
854 }
855 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700856 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000857
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000858 auto& patches = getConfig().patches;
859 auto existing = patches.end();
860 std::optional<decltype(mPatches)> patchesBackup;
861 if (in_requested.id != 0) {
862 existing = findById<AudioPatch>(patches, in_requested.id);
863 if (existing != patches.end()) {
864 patchesBackup = mPatches;
865 cleanUpPatch(existing->id);
866 } else {
867 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
868 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
869 }
870 }
871 // Validate the requested patch.
872 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
873 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
874 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
875 << "is exclusive and is already used by some other patch";
876 if (patchesBackup.has_value()) {
877 mPatches = std::move(*patchesBackup);
878 }
879 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
880 }
881 }
882 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000883 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
884 _aidl_return->latenciesMs.clear();
885 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
886 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000887 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000888 if (existing == patches.end()) {
889 _aidl_return->id = getConfig().nextPatchId++;
890 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000891 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000892 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -0700893 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000894 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700895 patchesBackup = mPatches;
896 registerPatch(*_aidl_return);
897 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
898 mPatches = std::move(*patchesBackup);
899 if (existing == patches.end()) {
900 patches.pop_back();
901 } else {
902 *existing = oldPatch;
903 }
904 return status;
905 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000906
907 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
908 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000909 return ndk::ScopedAStatus::ok();
910}
911
912ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
913 AudioPortConfig* out_suggested, bool* _aidl_return) {
914 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
915 auto& configs = getConfig().portConfigs;
916 auto existing = configs.end();
917 if (in_requested.id != 0) {
918 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
919 existing == configs.end()) {
920 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
921 << " not found";
922 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
923 }
924 }
925
926 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
927 if (portId == 0) {
928 LOG(ERROR) << __func__ << ": input port config does not specify portId";
929 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
930 }
931 auto& ports = getConfig().ports;
932 auto portIt = findById<AudioPort>(ports, portId);
933 if (portIt == ports.end()) {
934 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
935 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
936 }
937 if (existing != configs.end()) {
938 *out_suggested = *existing;
939 } else {
940 AudioPortConfig newConfig;
941 if (generateDefaultPortConfig(*portIt, &newConfig)) {
942 *out_suggested = newConfig;
943 } else {
944 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
945 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
946 }
947 }
948 // From this moment, 'out_suggested' is either an existing port config,
949 // or a new generated config. Now attempt to update it according to the specified
950 // fields of 'in_requested'.
951
952 bool requestedIsValid = true, requestedIsFullySpecified = true;
953
954 AudioIoFlags portFlags = portIt->flags;
955 if (in_requested.flags.has_value()) {
956 if (in_requested.flags.value() != portFlags) {
957 LOG(WARNING) << __func__ << ": requested flags "
958 << in_requested.flags.value().toString() << " do not match port's "
959 << portId << " flags " << portFlags.toString();
960 requestedIsValid = false;
961 }
962 } else {
963 requestedIsFullySpecified = false;
964 }
965
966 AudioProfile portProfile;
967 if (in_requested.format.has_value()) {
968 const auto& format = in_requested.format.value();
969 if (findAudioProfile(*portIt, format, &portProfile)) {
970 out_suggested->format = format;
971 } else {
972 LOG(WARNING) << __func__ << ": requested format " << format.toString()
973 << " is not found in port's " << portId << " profiles";
974 requestedIsValid = false;
975 }
976 } else {
977 requestedIsFullySpecified = false;
978 }
979 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
980 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
981 << out_suggested->format.value().toString() << " anymore";
982 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
983 }
984
985 if (in_requested.channelMask.has_value()) {
986 const auto& channelMask = in_requested.channelMask.value();
987 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
988 portProfile.channelMasks.end()) {
989 out_suggested->channelMask = channelMask;
990 } else {
991 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
992 << " is not supported for the format " << portProfile.format.toString()
993 << " by the port " << portId;
994 requestedIsValid = false;
995 }
996 } else {
997 requestedIsFullySpecified = false;
998 }
999
1000 if (in_requested.sampleRate.has_value()) {
1001 const auto& sampleRate = in_requested.sampleRate.value();
1002 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
1003 sampleRate.value) != portProfile.sampleRates.end()) {
1004 out_suggested->sampleRate = sampleRate;
1005 } else {
1006 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
1007 << " is not supported for the format " << portProfile.format.toString()
1008 << " by the port " << portId;
1009 requestedIsValid = false;
1010 }
1011 } else {
1012 requestedIsFullySpecified = false;
1013 }
1014
1015 if (in_requested.gain.has_value()) {
1016 // Let's pretend that gain can always be applied.
1017 out_suggested->gain = in_requested.gain.value();
1018 }
1019
Mikhail Naganov248e9502023-02-21 16:32:40 -08001020 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1021 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1022 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
1023 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
1024 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
1025 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
1026 }
1027 } else {
1028 LOG(WARNING) << __func__ << ": requested ext tag "
1029 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1030 << toString(out_suggested->ext.getTag());
1031 requestedIsValid = false;
1032 }
1033 }
1034
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001035 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1036 out_suggested->id = getConfig().nextPortId++;
1037 configs.push_back(*out_suggested);
1038 *_aidl_return = true;
1039 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
1040 } else if (existing != configs.end() && requestedIsValid) {
1041 *existing = *out_suggested;
1042 *_aidl_return = true;
1043 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
1044 } else {
1045 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
1046 << "; requested is valid? " << requestedIsValid << ", fully specified? "
1047 << requestedIsFullySpecified;
1048 *_aidl_return = false;
1049 }
1050 return ndk::ScopedAStatus::ok();
1051}
1052
1053ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1054 auto& patches = getConfig().patches;
1055 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1056 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001057 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001058 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001059 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1060 mPatches = std::move(patchesBackup);
1061 return status;
1062 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001063 patches.erase(patchIt);
1064 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1065 return ndk::ScopedAStatus::ok();
1066 }
1067 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1068 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1069}
1070
1071ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1072 auto& configs = getConfig().portConfigs;
1073 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1074 if (configIt != configs.end()) {
1075 if (mStreams.count(in_portConfigId) != 0) {
1076 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1077 << " has a stream opened on it";
1078 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1079 }
1080 auto patchIt = mPatches.find(in_portConfigId);
1081 if (patchIt != mPatches.end()) {
1082 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1083 << " is used by the patch with id " << patchIt->second;
1084 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1085 }
1086 auto& initials = getConfig().initialConfigs;
1087 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1088 if (initialIt == initials.end()) {
1089 configs.erase(configIt);
1090 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1091 } else if (*configIt != *initialIt) {
1092 *configIt = *initialIt;
1093 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1094 }
1095 return ndk::ScopedAStatus::ok();
1096 }
1097 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1098 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1099}
1100
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001101ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1102 *_aidl_return = mMasterMute;
1103 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1104 return ndk::ScopedAStatus::ok();
1105}
1106
1107ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1108 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001109 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1110 : onMasterMuteChanged(in_mute);
1111 if (result.isOk()) {
1112 mMasterMute = in_mute;
1113 } else {
1114 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1115 << "), error=" << result;
1116 // Reset master mute if it failed.
1117 onMasterMuteChanged(mMasterMute);
1118 }
1119 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001120}
1121
1122ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1123 *_aidl_return = mMasterVolume;
1124 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1125 return ndk::ScopedAStatus::ok();
1126}
1127
1128ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1129 LOG(DEBUG) << __func__ << ": " << in_volume;
1130 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001131 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1132 : onMasterVolumeChanged(in_volume);
1133 if (result.isOk()) {
1134 mMasterVolume = in_volume;
1135 } else {
1136 // Reset master volume if it failed.
1137 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1138 << "), error=" << result;
1139 onMasterVolumeChanged(mMasterVolume);
1140 }
1141 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001142 }
1143 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1144 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1145}
1146
1147ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1148 *_aidl_return = mMicMute;
1149 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1150 return ndk::ScopedAStatus::ok();
1151}
1152
1153ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1154 LOG(DEBUG) << __func__ << ": " << in_mute;
1155 mMicMute = in_mute;
1156 return ndk::ScopedAStatus::ok();
1157}
1158
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001159ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001160 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001161 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1162 return ndk::ScopedAStatus::ok();
1163}
1164
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001165ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001166 if (!isValidAudioMode(in_mode)) {
1167 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1168 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1169 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001170 // No checks for supported audio modes here, it's an informative notification.
1171 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1172 return ndk::ScopedAStatus::ok();
1173}
1174
1175ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1176 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1177 return ndk::ScopedAStatus::ok();
1178}
1179
1180ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1181 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1182 return ndk::ScopedAStatus::ok();
1183}
1184
Vlad Popa83a6d822022-11-07 13:53:57 +01001185ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001186 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001187 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001188 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001189 *_aidl_return = mSoundDose.getInstance();
Vlad Popa943b7e22022-12-08 14:24:12 +01001190 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001191 return ndk::ScopedAStatus::ok();
1192}
1193
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001194ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1195 LOG(DEBUG) << __func__;
1196 (void)_aidl_return;
1197 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1198}
1199
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001200const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001201const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001202
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001203ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1204 std::vector<VendorParameter>* _aidl_return) {
1205 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001206 bool allParametersKnown = true;
1207 for (const auto& id : in_ids) {
1208 if (id == VendorDebug::kForceTransientBurstName) {
1209 VendorParameter forceTransientBurst{.id = id};
1210 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1211 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001212 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1213 VendorParameter forceSynchronousDrain{.id = id};
1214 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1215 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001216 } else {
1217 allParametersKnown = false;
1218 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1219 }
1220 }
1221 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1222 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001223}
1224
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001225namespace {
1226
1227template <typename W>
1228bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1229 std::optional<W> value;
1230 binder_status_t result = p.ext.getParcelable(&value);
1231 if (result == STATUS_OK && value.has_value()) {
1232 *v = value.value().value;
1233 return true;
1234 }
1235 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1236 << "\": " << result;
1237 return false;
1238}
1239
1240} // namespace
1241
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001242ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1243 bool in_async) {
1244 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1245 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001246 bool allParametersKnown = true;
1247 for (const auto& p : in_parameters) {
1248 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001249 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1250 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1251 }
1252 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1253 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001254 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1255 }
1256 } else {
1257 allParametersKnown = false;
1258 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1259 }
1260 }
1261 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1262 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001263}
1264
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001265ndk::ScopedAStatus Module::addDeviceEffect(
1266 int32_t in_portConfigId,
1267 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1268 if (in_effect == nullptr) {
1269 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1270 } else {
1271 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1272 << in_effect->asBinder().get();
1273 }
1274 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1275}
1276
1277ndk::ScopedAStatus Module::removeDeviceEffect(
1278 int32_t in_portConfigId,
1279 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1280 if (in_effect == nullptr) {
1281 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1282 } else {
1283 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1284 << in_effect->asBinder().get();
1285 }
1286 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1287}
1288
jiabin9a8e6862023-01-12 23:06:37 +00001289ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1290 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1291 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1292 std::set<int32_t> mmapSinks;
1293 std::set<int32_t> mmapSources;
1294 auto& ports = getConfig().ports;
1295 for (const auto& port : ports) {
1296 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1297 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1298 AudioInputFlags::MMAP_NOIRQ)) {
1299 mmapSinks.insert(port.id);
1300 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1301 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1302 AudioOutputFlags::MMAP_NOIRQ)) {
1303 mmapSources.insert(port.id);
1304 }
1305 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001306 if (mmapSources.empty() && mmapSinks.empty()) {
1307 AudioMMapPolicyInfo never;
1308 never.mmapPolicy = AudioMMapPolicy::NEVER;
1309 _aidl_return->push_back(never);
1310 return ndk::ScopedAStatus::ok();
1311 }
jiabin9a8e6862023-01-12 23:06:37 +00001312 for (const auto& route : getConfig().routes) {
1313 if (mmapSinks.count(route.sinkPortId) != 0) {
1314 // The sink is a mix port, add the sources if they are device ports.
1315 for (int sourcePortId : route.sourcePortIds) {
1316 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1317 if (sourcePortIt == ports.end()) {
1318 // This must not happen
1319 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1320 continue;
1321 }
1322 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1323 // The source is not a device port, skip
1324 continue;
1325 }
1326 AudioMMapPolicyInfo policyInfo;
1327 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1328 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1329 // default implementation.
1330 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1331 _aidl_return->push_back(policyInfo);
1332 }
1333 } else {
1334 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1335 if (sinkPortIt == ports.end()) {
1336 // This must not happen
1337 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1338 continue;
1339 }
1340 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1341 // The sink is not a device port, skip
1342 continue;
1343 }
1344 if (count_any(mmapSources, route.sourcePortIds)) {
1345 AudioMMapPolicyInfo policyInfo;
1346 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1347 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1348 // default implementation.
1349 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1350 _aidl_return->push_back(policyInfo);
1351 }
1352 }
1353 }
1354 return ndk::ScopedAStatus::ok();
1355}
1356
Eric Laurente2432ea2023-01-12 17:47:31 +01001357ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1358 LOG(DEBUG) << __func__;
1359 *_aidl_return = false;
1360 return ndk::ScopedAStatus::ok();
1361}
1362
jiabinb76981e2023-01-18 00:58:30 +00001363ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1364 if (!isMmapSupported()) {
1365 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1366 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1367 }
1368 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1369 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1370 return ndk::ScopedAStatus::ok();
1371}
1372
1373ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1374 if (!isMmapSupported()) {
1375 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1376 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1377 }
1378 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1379 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1380 return ndk::ScopedAStatus::ok();
1381}
1382
1383bool Module::isMmapSupported() {
1384 if (mIsMmapSupported.has_value()) {
1385 return mIsMmapSupported.value();
1386 }
1387 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1388 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1389 mIsMmapSupported = false;
1390 } else {
1391 mIsMmapSupported =
1392 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1393 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1394 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1395 }) != mmapPolicyInfos.end();
1396 }
1397 return mIsMmapSupported.value();
1398}
1399
jiabin253bd322023-01-25 23:57:31 +00001400ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001401 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001402 return ndk::ScopedAStatus::ok();
1403}
1404
1405ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1406 const std::vector<AudioPortConfig*>& sources __unused,
1407 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001408 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001409 return ndk::ScopedAStatus::ok();
1410}
1411
jiabin783c48b2023-02-28 18:28:06 +00001412void Module::onExternalDeviceConnectionChanged(
1413 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1414 bool connected __unused) {
1415 LOG(DEBUG) << __func__ << ": do nothing and return";
1416}
1417
1418ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1419 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1420 return ndk::ScopedAStatus::ok();
1421}
1422
1423ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1424 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1425 return ndk::ScopedAStatus::ok();
1426}
1427
Ram Mohan18f0d512023-07-01 00:47:09 +05301428Module::BtProfileHandles Module::getBtProfileManagerHandles() {
1429 return std::make_tuple(std::weak_ptr<IBluetooth>(), std::weak_ptr<IBluetoothA2dp>(),
1430 std::weak_ptr<IBluetoothLe>());
1431}
1432
1433ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1434 return mStreams.bluetoothParametersUpdated();
1435}
1436
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001437} // namespace aidl::android::hardware::audio::core