blob: c17e0becfb36314a06003def0ac99d97d322d389 [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};
Vlad Popace338642023-09-21 18:54:03 -0700192 std::shared_ptr<ISoundDose> soundDose;
193 if (!getSoundDose(&soundDose).isOk()) {
194 LOG(ERROR) << __func__ << ": could not create sound dose instance";
195 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
196 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000197 StreamContext temp(
198 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
199 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530200 portConfigIt->portId, portConfigIt->format.value(),
201 portConfigIt->channelMask.value(), portConfigIt->sampleRate.value().value, flags,
Mikhail Naganovb42a69e2023-06-16 12:38:25 -0700202 portConfigIt->ext.get<AudioPortExt::mix>().handle,
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000203 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Vlad Popace338642023-09-21 18:54:03 -0700204 asyncCallback, outEventCallback, mSoundDose.getInstance(), params);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000205 if (temp.isValid()) {
206 *out_context = std::move(temp);
207 } else {
208 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
209 }
210 } else {
211 // TODO: Implement simulation of MMAP buffer allocation
212 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000213 return ndk::ScopedAStatus::ok();
214}
215
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000216std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
217 std::vector<AudioDevice> result;
218 auto& ports = getConfig().ports;
219 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
220 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
221 auto portIt = findById<AudioPort>(ports, *it);
222 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
223 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
224 }
225 }
226 return result;
227}
228
229std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
230 std::set<int32_t> result;
231 auto patchIdsRange = mPatches.equal_range(portConfigId);
232 auto& patches = getConfig().patches;
233 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
234 auto patchIt = findById<AudioPatch>(patches, it->second);
235 if (patchIt == patches.end()) {
236 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
237 << "not found in the configuration";
238 }
239 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
240 portConfigId) != patchIt->sourcePortConfigIds.end()) {
241 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
242 } else {
243 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
244 }
245 }
246 return result;
247}
248
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000249ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
250 auto& configs = getConfig().portConfigs;
251 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
252 if (portConfigIt == configs.end()) {
253 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
254 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
255 }
256 const int32_t portId = portConfigIt->portId;
257 // In our implementation, configs of mix ports always have unique IDs.
258 CHECK(portId != in_portConfigId);
259 auto& ports = getConfig().ports;
260 auto portIt = findById<AudioPort>(ports, portId);
261 if (portIt == ports.end()) {
262 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
263 << in_portConfigId << " not found";
264 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
265 }
266 if (mStreams.count(in_portConfigId) != 0) {
267 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
268 << " already has a stream opened on it";
269 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
270 }
271 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
272 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
273 << " does not correspond to a mix port";
274 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
275 }
Mikhail Naganovb511b8a2023-05-15 14:35:24 -0700276 const size_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000277 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
278 LOG(ERROR) << __func__ << ": port id " << portId
279 << " has already reached maximum allowed opened stream count: "
280 << maxOpenStreamCount;
281 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
282 }
283 *port = &(*portIt);
284 return ndk::ScopedAStatus::ok();
285}
286
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000287template <typename C>
288std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
289 std::set<int32_t> result;
290 auto& portConfigs = getConfig().portConfigs;
291 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
292 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
293 if (portConfigIt != portConfigs.end()) {
294 result.insert(portConfigIt->portId);
295 }
296 }
297 return result;
298}
299
Peter Yoon918a6a52023-07-13 17:04:37 +0900300std::unique_ptr<internal::Configuration> Module::initializeConfig() {
301 std::unique_ptr<internal::Configuration> config;
302 switch (getType()) {
303 case Type::DEFAULT:
304 config = std::move(internal::getPrimaryConfiguration());
305 break;
306 case Type::R_SUBMIX:
307 config = std::move(internal::getRSubmixConfiguration());
308 break;
309 case Type::STUB:
310 config = std::move(internal::getStubConfiguration());
311 break;
312 case Type::USB:
313 config = std::move(internal::getUsbConfiguration());
314 break;
Mikhail Naganovb03b5c42023-07-26 13:13:35 -0700315 case Type::BLUETOOTH:
316 config = std::move(internal::getBluetoothConfiguration());
317 break;
Peter Yoon918a6a52023-07-13 17:04:37 +0900318 }
319 return config;
320}
321
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000322internal::Configuration& Module::getConfig() {
323 if (!mConfig) {
Peter Yoon918a6a52023-07-13 17:04:37 +0900324 mConfig = std::move(initializeConfig());
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000325 }
326 return *mConfig;
327}
328
329void Module::registerPatch(const AudioPatch& patch) {
330 auto& configs = getConfig().portConfigs;
331 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
332 for (auto portConfigId : portConfigIds) {
333 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
334 if (configIt != configs.end()) {
335 mPatches.insert(std::pair{portConfigId, patch.id});
336 if (configIt->portId != portConfigId) {
337 mPatches.insert(std::pair{configIt->portId, patch.id});
338 }
339 }
340 };
341 };
342 do_insert(patch.sourcePortConfigIds);
343 do_insert(patch.sinkPortConfigIds);
344}
345
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700346ndk::ScopedAStatus Module::updateStreamsConnectedState(const AudioPatch& oldPatch,
347 const AudioPatch& newPatch) {
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700348 // Notify streams about the new set of devices they are connected to.
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700349 auto maybeFailure = ndk::ScopedAStatus::ok();
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700350 using Connections =
351 std::map<int32_t /*mixPortConfigId*/, std::set<int32_t /*devicePortConfigId*/>>;
352 Connections oldConnections, newConnections;
353 auto fillConnectionsHelper = [&](Connections& connections,
354 const std::vector<int32_t>& mixPortCfgIds,
355 const std::vector<int32_t>& devicePortCfgIds) {
356 for (int32_t mixPortCfgId : mixPortCfgIds) {
357 connections[mixPortCfgId].insert(devicePortCfgIds.begin(), devicePortCfgIds.end());
358 }
359 };
360 auto fillConnections = [&](Connections& connections, const AudioPatch& patch) {
361 if (std::find_if(patch.sourcePortConfigIds.begin(), patch.sourcePortConfigIds.end(),
362 [&](int32_t portConfigId) { return mStreams.count(portConfigId) > 0; }) !=
363 patch.sourcePortConfigIds.end()) {
364 // Sources are mix ports.
365 fillConnectionsHelper(connections, patch.sourcePortConfigIds, patch.sinkPortConfigIds);
366 } else if (std::find_if(patch.sinkPortConfigIds.begin(), patch.sinkPortConfigIds.end(),
367 [&](int32_t portConfigId) {
368 return mStreams.count(portConfigId) > 0;
369 }) != patch.sinkPortConfigIds.end()) {
370 // Sources are device ports.
371 fillConnectionsHelper(connections, patch.sinkPortConfigIds, patch.sourcePortConfigIds);
372 } // Otherwise, there are no streams to notify.
373 };
374 fillConnections(oldConnections, oldPatch);
375 fillConnections(newConnections, newPatch);
376
377 std::for_each(oldConnections.begin(), oldConnections.end(), [&](const auto& connectionPair) {
378 const int32_t mixPortConfigId = connectionPair.first;
379 if (auto it = newConnections.find(mixPortConfigId);
380 it == newConnections.end() || it->second != connectionPair.second) {
381 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, {});
382 status.isOk()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700383 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700384 << mixPortConfigId << " has been disconnected";
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700385 } else {
386 // Disconnection is tricky to roll back, just register a failure.
387 maybeFailure = std::move(status);
388 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000389 }
390 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700391 if (!maybeFailure.isOk()) return maybeFailure;
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700392 std::set<int32_t> idsToDisconnectOnFailure;
393 std::for_each(newConnections.begin(), newConnections.end(), [&](const auto& connectionPair) {
394 const int32_t mixPortConfigId = connectionPair.first;
395 if (auto it = oldConnections.find(mixPortConfigId);
396 it == oldConnections.end() || it->second != connectionPair.second) {
397 const auto connectedDevices = findConnectedDevices(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700398 if (connectedDevices.empty()) {
399 // This is important as workers use the vector size to derive the connection status.
400 LOG(FATAL) << "updateStreamsConnectedState: No connected devices found for port "
401 "config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700402 << mixPortConfigId;
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700403 }
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700404 if (auto status = mStreams.setStreamConnectedDevices(mixPortConfigId, connectedDevices);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700405 status.isOk()) {
406 LOG(DEBUG) << "updateStreamsConnectedState: The stream on port config id "
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700407 << mixPortConfigId << " has been connected to: "
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700408 << ::android::internal::ToString(connectedDevices);
409 } else {
410 maybeFailure = std::move(status);
Mikhail Naganov89a8ea92023-09-29 17:02:12 -0700411 idsToDisconnectOnFailure.insert(mixPortConfigId);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700412 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000413 }
414 });
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700415 if (!maybeFailure.isOk()) {
416 LOG(WARNING) << __func__ << ": Due to a failure, disconnecting streams on port config ids "
417 << ::android::internal::ToString(idsToDisconnectOnFailure);
418 std::for_each(idsToDisconnectOnFailure.begin(), idsToDisconnectOnFailure.end(),
419 [&](const auto& portConfigId) {
420 auto status = mStreams.setStreamConnectedDevices(portConfigId, {});
421 (void)status.isOk(); // Can't do much about a failure here.
422 });
423 return maybeFailure;
424 }
425 return ndk::ScopedAStatus::ok();
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000426}
427
Mikhail Naganov00603d12022-05-02 22:52:13 +0000428ndk::ScopedAStatus Module::setModuleDebug(
429 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700430 LOG(DEBUG) << __func__ << ": " << mType << ": old flags:" << mDebug.toString()
Mikhail Naganov00603d12022-05-02 22:52:13 +0000431 << ", new flags: " << in_debug.toString();
432 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
433 !mConnectedDevicePorts.empty()) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700434 LOG(ERROR) << __func__ << ": " << mType
435 << ": attempting to change device connections simulation while having external "
436 << "devices connected";
Mikhail Naganov00603d12022-05-02 22:52:13 +0000437 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
438 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000439 if (in_debug.streamTransientStateDelayMs < 0) {
Mikhail Naganovd5536d92023-03-24 18:27:58 -0700440 LOG(ERROR) << __func__ << ": " << mType << ": streamTransientStateDelayMs is negative: "
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000441 << in_debug.streamTransientStateDelayMs;
442 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
443 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000444 mDebug = in_debug;
445 return ndk::ScopedAStatus::ok();
446}
447
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000448ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700449 *_aidl_return = nullptr;
450 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000451 return ndk::ScopedAStatus::ok();
452}
453
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000454ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700455 *_aidl_return = nullptr;
456 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000457 return ndk::ScopedAStatus::ok();
458}
459
Mikhail Naganov7499a002023-02-27 18:51:44 -0800460ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700461 *_aidl_return = nullptr;
462 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganov7499a002023-02-27 18:51:44 -0800463 return ndk::ScopedAStatus::ok();
464}
465
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800466ndk::ScopedAStatus Module::getBluetoothLe(std::shared_ptr<IBluetoothLe>* _aidl_return) {
Mikhail Naganov521fc492023-07-11 17:24:08 -0700467 *_aidl_return = nullptr;
468 LOG(DEBUG) << __func__ << ": returning null";
Mikhail Naganovb5647da2023-03-06 14:37:38 -0800469 return ndk::ScopedAStatus::ok();
470}
471
Mikhail Naganov00603d12022-05-02 22:52:13 +0000472ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
473 AudioPort* _aidl_return) {
474 const int32_t templateId = in_templateIdAndAdditionalData.id;
475 auto& ports = getConfig().ports;
476 AudioPort connectedPort;
477 { // Scope the template port so that we don't accidentally modify it.
478 auto templateIt = findById<AudioPort>(ports, templateId);
479 if (templateIt == ports.end()) {
480 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
481 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
482 }
483 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
484 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
485 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
486 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000487 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
488 if (templateDevicePort.device.type.connection.empty()) {
489 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
490 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
491 }
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700492 if (mConnectedDevicePorts.find(templateId) != mConnectedDevicePorts.end()) {
493 LOG(ERROR) << __func__ << ": port id " << templateId << " is a connected device port";
494 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
495 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000496 // Postpone id allocation until we ensure that there are no client errors.
497 connectedPort = *templateIt;
498 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
499 const auto& inputDevicePort =
500 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
501 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
502 connectedDevicePort.device.address = inputDevicePort.device.address;
503 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
504 << connectedDevicePort.device.toString();
505 // Check if there is already a connected port with for the same external device.
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700506 for (auto connectedPortPair : mConnectedDevicePorts) {
507 auto connectedPortIt = findById<AudioPort>(ports, connectedPortPair.first);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000508 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
509 connectedDevicePort.device) {
510 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700511 << " is already connected at the device port id "
512 << connectedPortPair.first;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000513 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
514 }
515 }
516 }
517
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700518 if (connectedPort.profiles.empty()) {
Mikhail Naganovfcf980e2023-09-07 16:30:11 -0700519 if (!mDebug.simulateDeviceConnections) {
520 RETURN_STATUS_IF_ERROR(populateConnectedDevicePort(&connectedPort));
521 } else {
522 auto& connectedProfiles = getConfig().connectedProfiles;
523 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
524 connectedProfilesIt != connectedProfiles.end()) {
525 connectedPort.profiles = connectedProfilesIt->second;
526 }
527 }
528 if (connectedPort.profiles.empty()) {
529 LOG(ERROR) << __func__
530 << ": profiles of a connected port still empty after connecting external "
531 "device "
532 << connectedPort.toString();
533 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
534 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000535 }
536
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530537 for (auto profile : connectedPort.profiles) {
538 if (profile.channelMasks.empty()) {
539 LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no channel masks";
540 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
541 }
542 if (profile.sampleRates.empty()) {
543 LOG(ERROR) << __func__ << ": the profile " << profile.name << " has no sample rates";
544 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
545 }
546 }
547
Mikhail Naganovdc417732023-09-29 15:49:35 -0700548 connectedPort.id = getConfig().nextPortId++;
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700549 auto [connectedPortsIt, _] =
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700550 mConnectedDevicePorts.insert(std::pair(connectedPort.id, std::set<int32_t>()));
Mikhail Naganov00603d12022-05-02 22:52:13 +0000551 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
552 << "connected port ID " << connectedPort.id;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000553 ports.push_back(connectedPort);
jiabin783c48b2023-02-28 18:28:06 +0000554 onExternalDeviceConnectionChanged(connectedPort, true /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000555
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700556 std::vector<int32_t> routablePortIds;
Mikhail Naganov00603d12022-05-02 22:52:13 +0000557 std::vector<AudioRoute> newRoutes;
558 auto& routes = getConfig().routes;
559 for (auto& r : routes) {
560 if (r.sinkPortId == templateId) {
561 AudioRoute newRoute;
562 newRoute.sourcePortIds = r.sourcePortIds;
563 newRoute.sinkPortId = connectedPort.id;
564 newRoute.isExclusive = r.isExclusive;
565 newRoutes.push_back(std::move(newRoute));
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700566 routablePortIds.insert(routablePortIds.end(), r.sourcePortIds.begin(),
567 r.sourcePortIds.end());
Mikhail Naganov00603d12022-05-02 22:52:13 +0000568 } else {
569 auto& srcs = r.sourcePortIds;
570 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
571 srcs.push_back(connectedPort.id);
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700572 routablePortIds.push_back(r.sinkPortId);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000573 }
574 }
575 }
576 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
577
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700578 // Note: this is a simplistic approach assuming that a mix port can only be populated
579 // from a single device port. Implementing support for stuffing dynamic profiles with a superset
580 // of all profiles from all routable dynamic device ports would be more involved.
581 for (const auto mixPortId : routablePortIds) {
582 auto portsIt = findById<AudioPort>(ports, mixPortId);
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700583 if (portsIt != ports.end()) {
584 if (portsIt->profiles.empty()) {
585 portsIt->profiles = connectedPort.profiles;
586 connectedPortsIt->second.insert(portsIt->id);
587 } else {
588 // Check if profiles are non empty because they were populated by
589 // a previous connection. Otherwise, it means that they are not empty because
590 // the mix port has static profiles.
591 for (const auto cp : mConnectedDevicePorts) {
592 if (cp.second.count(portsIt->id) > 0) {
593 connectedPortsIt->second.insert(portsIt->id);
594 break;
595 }
596 }
597 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700598 }
599 }
600 *_aidl_return = std::move(connectedPort);
601
Mikhail Naganov00603d12022-05-02 22:52:13 +0000602 return ndk::ScopedAStatus::ok();
603}
604
605ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
606 auto& ports = getConfig().ports;
607 auto portIt = findById<AudioPort>(ports, in_portId);
608 if (portIt == ports.end()) {
609 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
610 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
611 }
612 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
613 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
614 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
615 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700616 auto connectedPortsIt = mConnectedDevicePorts.find(in_portId);
617 if (connectedPortsIt == mConnectedDevicePorts.end()) {
Mikhail Naganov00603d12022-05-02 22:52:13 +0000618 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
619 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
620 }
621 auto& configs = getConfig().portConfigs;
622 auto& initials = getConfig().initialConfigs;
623 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
624 if (config.portId == in_portId) {
625 // Check if the configuration was provided by the client.
626 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
627 return initialIt == initials.end() || config != *initialIt;
628 }
629 return false;
630 });
631 if (configIt != configs.end()) {
632 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
633 << configIt->id;
634 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
635 }
jiabin783c48b2023-02-28 18:28:06 +0000636 onExternalDeviceConnectionChanged(*portIt, false /*connected*/);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000637 ports.erase(portIt);
Mikhail Naganov00603d12022-05-02 22:52:13 +0000638 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
639
640 auto& routes = getConfig().routes;
641 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
642 if (routesIt->sinkPortId == in_portId) {
643 routesIt = routes.erase(routesIt);
644 } else {
645 // Note: the list of sourcePortIds can't become empty because there must
646 // be the id of the template port in the route.
647 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
648 ++routesIt;
649 }
650 }
651
Mikhail Naganov0e128dd2023-09-13 18:01:18 -0700652 // Clear profiles for mix ports that are not connected to any other ports.
653 std::set<int32_t> mixPortsToClear = std::move(connectedPortsIt->second);
654 mConnectedDevicePorts.erase(connectedPortsIt);
655 for (const auto& connectedPort : mConnectedDevicePorts) {
656 for (int32_t mixPortId : connectedPort.second) {
657 mixPortsToClear.erase(mixPortId);
658 }
659 }
660 for (int32_t mixPortId : mixPortsToClear) {
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700661 auto mixPortIt = findById<AudioPort>(ports, mixPortId);
662 if (mixPortIt != ports.end()) {
663 mixPortIt->profiles = {};
664 }
665 }
Mikhail Naganov7b2d12b2023-03-24 18:29:14 -0700666
Mikhail Naganov00603d12022-05-02 22:52:13 +0000667 return ndk::ScopedAStatus::ok();
668}
669
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000670ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
671 *_aidl_return = getConfig().patches;
672 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
673 return ndk::ScopedAStatus::ok();
674}
675
676ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
677 auto& ports = getConfig().ports;
678 auto portIt = findById<AudioPort>(ports, in_portId);
679 if (portIt != ports.end()) {
680 *_aidl_return = *portIt;
681 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
682 return ndk::ScopedAStatus::ok();
683 }
684 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
685 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
686}
687
688ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
689 *_aidl_return = getConfig().portConfigs;
690 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
691 return ndk::ScopedAStatus::ok();
692}
693
694ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
695 *_aidl_return = getConfig().ports;
696 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
697 return ndk::ScopedAStatus::ok();
698}
699
700ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
701 *_aidl_return = getConfig().routes;
702 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
703 return ndk::ScopedAStatus::ok();
704}
705
Mikhail Naganov00603d12022-05-02 22:52:13 +0000706ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
707 std::vector<AudioRoute>* _aidl_return) {
708 auto& ports = getConfig().ports;
709 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
710 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
711 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
712 }
713 auto& routes = getConfig().routes;
714 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
715 [&](const auto& r) {
716 const auto& srcs = r.sourcePortIds;
717 return r.sinkPortId == in_portId ||
718 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
719 });
720 return ndk::ScopedAStatus::ok();
721}
722
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000723ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
724 OpenInputStreamReturn* _aidl_return) {
725 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
726 << in_args.bufferSizeFrames << " frames";
727 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700728 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000729 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
730 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000731 << " does not correspond to an input mix port";
732 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
733 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000734 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700735 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
736 nullptr, nullptr, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000737 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000738 std::shared_ptr<StreamIn> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700739 RETURN_STATUS_IF_ERROR(createInputStream(std::move(context), in_args.sinkMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700740 mConfig->microphones, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000741 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700742 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
743 RETURN_STATUS_IF_ERROR(
744 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
745 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000746 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
747 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000748 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000749 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000750 return ndk::ScopedAStatus::ok();
751}
752
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000753ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
754 OpenOutputStreamReturn* _aidl_return) {
755 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
756 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
757 << " frames";
758 AudioPort* port = nullptr;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700759 RETURN_STATUS_IF_ERROR(findPortIdForNewStream(in_args.portConfigId, &port));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000760 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
761 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000762 << " does not correspond to an output mix port";
763 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
764 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000765 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
766 AudioOutputFlags::COMPRESS_OFFLOAD);
767 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000768 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000769 << " has COMPRESS_OFFLOAD flag set, requires offload info";
770 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
771 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000772 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
773 AudioOutputFlags::NON_BLOCKING);
774 if (isNonBlocking && in_args.callback == nullptr) {
775 LOG(ERROR) << __func__ << ": port id " << port->id
776 << " has NON_BLOCKING flag set, requires async callback";
777 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
778 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000779 StreamContext context;
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700780 RETURN_STATUS_IF_ERROR(createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
781 isNonBlocking ? in_args.callback : nullptr,
782 in_args.eventCallback, &context));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000783 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000784 std::shared_ptr<StreamOut> stream;
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700785 RETURN_STATUS_IF_ERROR(createOutputStream(std::move(context), in_args.sourceMetadata,
Mikhail Naganov9d16a6a2023-06-26 17:21:04 -0700786 in_args.offloadInfo, &stream));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000787 StreamWrapper streamWrapper(stream);
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700788 if (auto patchIt = mPatches.find(in_args.portConfigId); patchIt != mPatches.end()) {
789 RETURN_STATUS_IF_ERROR(
790 streamWrapper.setConnectedDevices(findConnectedDevices(in_args.portConfigId)));
791 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000792 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
793 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000794 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000795 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000796 return ndk::ScopedAStatus::ok();
797}
798
Mikhail Naganov74927202022-12-19 16:37:14 +0000799ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
800 SupportedPlaybackRateFactors* _aidl_return) {
801 LOG(DEBUG) << __func__;
802 (void)_aidl_return;
803 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
804}
805
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000806ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000807 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000808 if (in_requested.sourcePortConfigIds.empty()) {
809 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
810 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
811 }
812 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
813 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
814 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
815 }
816 if (in_requested.sinkPortConfigIds.empty()) {
817 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
818 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
819 }
820 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
821 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
822 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
823 }
824
825 auto& configs = getConfig().portConfigs;
826 std::vector<int32_t> missingIds;
827 auto sources =
828 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
829 if (!missingIds.empty()) {
830 LOG(ERROR) << __func__ << ": following source port config ids not found: "
831 << ::android::internal::ToString(missingIds);
832 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
833 }
834 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
835 if (!missingIds.empty()) {
836 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
837 << ::android::internal::ToString(missingIds);
838 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
839 }
840 // bool indicates whether a non-exclusive route is available.
841 // If only an exclusive route is available, that means the patch can not be
842 // established if there is any other patch which currently uses the sink port.
843 std::map<int32_t, bool> allowedSinkPorts;
844 auto& routes = getConfig().routes;
845 for (auto src : sources) {
846 for (const auto& r : routes) {
847 const auto& srcs = r.sourcePortIds;
848 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
849 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
850 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
851 }
852 }
853 }
854 }
855 for (auto sink : sinks) {
856 if (allowedSinkPorts.count(sink->portId) == 0) {
857 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
858 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
859 }
860 }
Mikhail Naganov26dc9ad2023-06-23 13:55:37 -0700861 RETURN_STATUS_IF_ERROR(checkAudioPatchEndpointsMatch(sources, sinks));
jiabin253bd322023-01-25 23:57:31 +0000862
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000863 auto& patches = getConfig().patches;
864 auto existing = patches.end();
865 std::optional<decltype(mPatches)> patchesBackup;
866 if (in_requested.id != 0) {
867 existing = findById<AudioPatch>(patches, in_requested.id);
868 if (existing != patches.end()) {
869 patchesBackup = mPatches;
870 cleanUpPatch(existing->id);
871 } else {
872 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
873 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
874 }
875 }
876 // Validate the requested patch.
877 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
878 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
879 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
880 << "is exclusive and is already used by some other patch";
881 if (patchesBackup.has_value()) {
882 mPatches = std::move(*patchesBackup);
883 }
884 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
885 }
886 }
887 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000888 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
889 _aidl_return->latenciesMs.clear();
890 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
891 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000892 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000893 if (existing == patches.end()) {
894 _aidl_return->id = getConfig().nextPatchId++;
895 patches.push_back(*_aidl_return);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000896 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000897 oldPatch = *existing;
Mikhail Naganovdc417732023-09-29 15:49:35 -0700898 *existing = *_aidl_return;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000899 }
Mikhail Naganov75b59df2023-06-23 13:39:40 -0700900 patchesBackup = mPatches;
901 registerPatch(*_aidl_return);
902 if (auto status = updateStreamsConnectedState(oldPatch, *_aidl_return); !status.isOk()) {
903 mPatches = std::move(*patchesBackup);
904 if (existing == patches.end()) {
905 patches.pop_back();
906 } else {
907 *existing = oldPatch;
908 }
909 return status;
910 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000911
912 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
913 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000914 return ndk::ScopedAStatus::ok();
915}
916
917ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
918 AudioPortConfig* out_suggested, bool* _aidl_return) {
919 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
920 auto& configs = getConfig().portConfigs;
921 auto existing = configs.end();
922 if (in_requested.id != 0) {
923 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
924 existing == configs.end()) {
925 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
926 << " not found";
927 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
928 }
929 }
930
931 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
932 if (portId == 0) {
933 LOG(ERROR) << __func__ << ": input port config does not specify portId";
934 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
935 }
936 auto& ports = getConfig().ports;
937 auto portIt = findById<AudioPort>(ports, portId);
938 if (portIt == ports.end()) {
939 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
940 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
941 }
942 if (existing != configs.end()) {
943 *out_suggested = *existing;
944 } else {
945 AudioPortConfig newConfig;
946 if (generateDefaultPortConfig(*portIt, &newConfig)) {
947 *out_suggested = newConfig;
948 } else {
949 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
950 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
951 }
952 }
953 // From this moment, 'out_suggested' is either an existing port config,
954 // or a new generated config. Now attempt to update it according to the specified
955 // fields of 'in_requested'.
956
957 bool requestedIsValid = true, requestedIsFullySpecified = true;
958
959 AudioIoFlags portFlags = portIt->flags;
960 if (in_requested.flags.has_value()) {
961 if (in_requested.flags.value() != portFlags) {
962 LOG(WARNING) << __func__ << ": requested flags "
963 << in_requested.flags.value().toString() << " do not match port's "
964 << portId << " flags " << portFlags.toString();
965 requestedIsValid = false;
966 }
967 } else {
968 requestedIsFullySpecified = false;
969 }
970
971 AudioProfile portProfile;
972 if (in_requested.format.has_value()) {
973 const auto& format = in_requested.format.value();
974 if (findAudioProfile(*portIt, format, &portProfile)) {
975 out_suggested->format = format;
976 } else {
977 LOG(WARNING) << __func__ << ": requested format " << format.toString()
978 << " is not found in port's " << portId << " profiles";
979 requestedIsValid = false;
980 }
981 } else {
982 requestedIsFullySpecified = false;
983 }
984 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
985 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
986 << out_suggested->format.value().toString() << " anymore";
987 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
988 }
989
990 if (in_requested.channelMask.has_value()) {
991 const auto& channelMask = in_requested.channelMask.value();
992 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
993 portProfile.channelMasks.end()) {
994 out_suggested->channelMask = channelMask;
995 } else {
996 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
997 << " is not supported for the format " << portProfile.format.toString()
998 << " by the port " << portId;
999 requestedIsValid = false;
1000 }
1001 } else {
1002 requestedIsFullySpecified = false;
1003 }
1004
1005 if (in_requested.sampleRate.has_value()) {
1006 const auto& sampleRate = in_requested.sampleRate.value();
1007 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
1008 sampleRate.value) != portProfile.sampleRates.end()) {
1009 out_suggested->sampleRate = sampleRate;
1010 } else {
1011 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
1012 << " is not supported for the format " << portProfile.format.toString()
1013 << " by the port " << portId;
1014 requestedIsValid = false;
1015 }
1016 } else {
1017 requestedIsFullySpecified = false;
1018 }
1019
1020 if (in_requested.gain.has_value()) {
1021 // Let's pretend that gain can always be applied.
1022 out_suggested->gain = in_requested.gain.value();
1023 }
1024
Mikhail Naganov248e9502023-02-21 16:32:40 -08001025 if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) {
1026 if (in_requested.ext.getTag() == out_suggested->ext.getTag()) {
1027 if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) {
1028 // 'AudioMixPortExt.handle' is set by the client, copy from in_requested
1029 out_suggested->ext.get<AudioPortExt::Tag::mix>().handle =
1030 in_requested.ext.get<AudioPortExt::Tag::mix>().handle;
1031 }
1032 } else {
1033 LOG(WARNING) << __func__ << ": requested ext tag "
1034 << toString(in_requested.ext.getTag()) << " do not match port's tag "
1035 << toString(out_suggested->ext.getTag());
1036 requestedIsValid = false;
1037 }
1038 }
1039
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001040 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
1041 out_suggested->id = getConfig().nextPortId++;
1042 configs.push_back(*out_suggested);
1043 *_aidl_return = true;
1044 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
1045 } else if (existing != configs.end() && requestedIsValid) {
1046 *existing = *out_suggested;
1047 *_aidl_return = true;
1048 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
1049 } else {
1050 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
1051 << "; requested is valid? " << requestedIsValid << ", fully specified? "
1052 << requestedIsFullySpecified;
1053 *_aidl_return = false;
1054 }
1055 return ndk::ScopedAStatus::ok();
1056}
1057
1058ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
1059 auto& patches = getConfig().patches;
1060 auto patchIt = findById<AudioPatch>(patches, in_patchId);
1061 if (patchIt != patches.end()) {
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001062 auto patchesBackup = mPatches;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001063 cleanUpPatch(patchIt->id);
Mikhail Naganov75b59df2023-06-23 13:39:40 -07001064 if (auto status = updateStreamsConnectedState(*patchIt, AudioPatch{}); !status.isOk()) {
1065 mPatches = std::move(patchesBackup);
1066 return status;
1067 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001068 patches.erase(patchIt);
1069 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
1070 return ndk::ScopedAStatus::ok();
1071 }
1072 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
1073 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1074}
1075
1076ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
1077 auto& configs = getConfig().portConfigs;
1078 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
1079 if (configIt != configs.end()) {
1080 if (mStreams.count(in_portConfigId) != 0) {
1081 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1082 << " has a stream opened on it";
1083 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1084 }
1085 auto patchIt = mPatches.find(in_portConfigId);
1086 if (patchIt != mPatches.end()) {
1087 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
1088 << " is used by the patch with id " << patchIt->second;
1089 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
1090 }
1091 auto& initials = getConfig().initialConfigs;
1092 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
1093 if (initialIt == initials.end()) {
1094 configs.erase(configIt);
1095 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
1096 } else if (*configIt != *initialIt) {
1097 *configIt = *initialIt;
1098 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
1099 }
1100 return ndk::ScopedAStatus::ok();
1101 }
1102 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
1103 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1104}
1105
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001106ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
1107 *_aidl_return = mMasterMute;
1108 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1109 return ndk::ScopedAStatus::ok();
1110}
1111
1112ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
1113 LOG(DEBUG) << __func__ << ": " << in_mute;
jiabin783c48b2023-02-28 18:28:06 +00001114 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1115 : onMasterMuteChanged(in_mute);
1116 if (result.isOk()) {
1117 mMasterMute = in_mute;
1118 } else {
1119 LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute
1120 << "), error=" << result;
1121 // Reset master mute if it failed.
1122 onMasterMuteChanged(mMasterMute);
1123 }
1124 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001125}
1126
1127ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
1128 *_aidl_return = mMasterVolume;
1129 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1130 return ndk::ScopedAStatus::ok();
1131}
1132
1133ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
1134 LOG(DEBUG) << __func__ << ": " << in_volume;
1135 if (in_volume >= 0.0f && in_volume <= 1.0f) {
jiabin783c48b2023-02-28 18:28:06 +00001136 auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok()
1137 : onMasterVolumeChanged(in_volume);
1138 if (result.isOk()) {
1139 mMasterVolume = in_volume;
1140 } else {
1141 // Reset master volume if it failed.
1142 LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume
1143 << "), error=" << result;
1144 onMasterVolumeChanged(mMasterVolume);
1145 }
1146 return std::move(result);
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001147 }
1148 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
1149 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1150}
1151
1152ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
1153 *_aidl_return = mMicMute;
1154 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1155 return ndk::ScopedAStatus::ok();
1156}
1157
1158ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
1159 LOG(DEBUG) << __func__ << ": " << in_mute;
1160 mMicMute = in_mute;
1161 return ndk::ScopedAStatus::ok();
1162}
1163
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001164ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
Pawan Wagh6f57cd92023-02-01 21:14:34 +00001165 *_aidl_return = getConfig().microphones;
Mikhail Naganovef6bc742022-10-06 00:14:19 +00001166 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
1167 return ndk::ScopedAStatus::ok();
1168}
1169
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001170ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
Mikhail Naganov04ae8222023-01-11 15:48:10 -08001171 if (!isValidAudioMode(in_mode)) {
1172 LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode);
1173 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1174 }
Mikhail Naganov3b125b72022-10-05 02:12:39 +00001175 // No checks for supported audio modes here, it's an informative notification.
1176 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
1177 return ndk::ScopedAStatus::ok();
1178}
1179
1180ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
1181 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
1182 return ndk::ScopedAStatus::ok();
1183}
1184
1185ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
1186 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
1187 return ndk::ScopedAStatus::ok();
1188}
1189
Vlad Popa83a6d822022-11-07 13:53:57 +01001190ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Mikhail Naganov7499a002023-02-27 18:51:44 -08001191 if (!mSoundDose) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +01001192 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Vlad Popa943b7e22022-12-08 14:24:12 +01001193 }
Mikhail Naganov780fefb2023-07-21 17:01:38 -07001194 *_aidl_return = mSoundDose.getInstance();
Vlad Popa943b7e22022-12-08 14:24:12 +01001195 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +01001196 return ndk::ScopedAStatus::ok();
1197}
1198
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001199ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
1200 LOG(DEBUG) << __func__;
1201 (void)_aidl_return;
1202 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1203}
1204
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001205const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst";
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001206const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain";
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001207
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001208ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
1209 std::vector<VendorParameter>* _aidl_return) {
1210 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001211 bool allParametersKnown = true;
1212 for (const auto& id : in_ids) {
1213 if (id == VendorDebug::kForceTransientBurstName) {
1214 VendorParameter forceTransientBurst{.id = id};
1215 forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst});
1216 _aidl_return->push_back(std::move(forceTransientBurst));
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001217 } else if (id == VendorDebug::kForceSynchronousDrainName) {
1218 VendorParameter forceSynchronousDrain{.id = id};
1219 forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain});
1220 _aidl_return->push_back(std::move(forceSynchronousDrain));
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001221 } else {
1222 allParametersKnown = false;
1223 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\"";
1224 }
1225 }
1226 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1227 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001228}
1229
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001230namespace {
1231
1232template <typename W>
1233bool extractParameter(const VendorParameter& p, decltype(W::value)* v) {
1234 std::optional<W> value;
1235 binder_status_t result = p.ext.getParcelable(&value);
1236 if (result == STATUS_OK && value.has_value()) {
1237 *v = value.value().value;
1238 return true;
1239 }
1240 LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id
1241 << "\": " << result;
1242 return false;
1243}
1244
1245} // namespace
1246
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001247ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
1248 bool in_async) {
1249 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
1250 << ", async: " << in_async;
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001251 bool allParametersKnown = true;
1252 for (const auto& p : in_parameters) {
1253 if (p.id == VendorDebug::kForceTransientBurstName) {
Mikhail Naganov194daaa2023-01-05 22:34:20 +00001254 if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) {
1255 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1256 }
1257 } else if (p.id == VendorDebug::kForceSynchronousDrainName) {
1258 if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) {
Mikhail Naganov20047bc2023-01-05 20:16:07 +00001259 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
1260 }
1261 } else {
1262 allParametersKnown = false;
1263 LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\"";
1264 }
1265 }
1266 if (allParametersKnown) return ndk::ScopedAStatus::ok();
1267 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +00001268}
1269
Mikhail Naganovfb1acde2022-12-12 18:57:36 +00001270ndk::ScopedAStatus Module::addDeviceEffect(
1271 int32_t in_portConfigId,
1272 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1273 if (in_effect == nullptr) {
1274 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1275 } else {
1276 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1277 << in_effect->asBinder().get();
1278 }
1279 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1280}
1281
1282ndk::ScopedAStatus Module::removeDeviceEffect(
1283 int32_t in_portConfigId,
1284 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1285 if (in_effect == nullptr) {
1286 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1287 } else {
1288 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1289 << in_effect->asBinder().get();
1290 }
1291 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1292}
1293
jiabin9a8e6862023-01-12 23:06:37 +00001294ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType,
1295 std::vector<AudioMMapPolicyInfo>* _aidl_return) {
1296 LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType);
1297 std::set<int32_t> mmapSinks;
1298 std::set<int32_t> mmapSources;
1299 auto& ports = getConfig().ports;
1300 for (const auto& port : ports) {
1301 if (port.flags.getTag() == AudioIoFlags::Tag::input &&
1302 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(),
1303 AudioInputFlags::MMAP_NOIRQ)) {
1304 mmapSinks.insert(port.id);
1305 } else if (port.flags.getTag() == AudioIoFlags::Tag::output &&
1306 isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(),
1307 AudioOutputFlags::MMAP_NOIRQ)) {
1308 mmapSources.insert(port.id);
1309 }
1310 }
Mikhail Naganov85064912023-09-26 17:10:08 -07001311 if (mmapSources.empty() && mmapSinks.empty()) {
1312 AudioMMapPolicyInfo never;
1313 never.mmapPolicy = AudioMMapPolicy::NEVER;
1314 _aidl_return->push_back(never);
1315 return ndk::ScopedAStatus::ok();
1316 }
jiabin9a8e6862023-01-12 23:06:37 +00001317 for (const auto& route : getConfig().routes) {
1318 if (mmapSinks.count(route.sinkPortId) != 0) {
1319 // The sink is a mix port, add the sources if they are device ports.
1320 for (int sourcePortId : route.sourcePortIds) {
1321 auto sourcePortIt = findById<AudioPort>(ports, sourcePortId);
1322 if (sourcePortIt == ports.end()) {
1323 // This must not happen
1324 LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found";
1325 continue;
1326 }
1327 if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) {
1328 // The source is not a device port, skip
1329 continue;
1330 }
1331 AudioMMapPolicyInfo policyInfo;
1332 policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device;
1333 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1334 // default implementation.
1335 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1336 _aidl_return->push_back(policyInfo);
1337 }
1338 } else {
1339 auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId);
1340 if (sinkPortIt == ports.end()) {
1341 // This must not happen
1342 LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found";
1343 continue;
1344 }
1345 if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) {
1346 // The sink is not a device port, skip
1347 continue;
1348 }
1349 if (count_any(mmapSources, route.sourcePortIds)) {
1350 AudioMMapPolicyInfo policyInfo;
1351 policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device;
1352 // Always return AudioMMapPolicy.AUTO if the device supports mmap for
1353 // default implementation.
1354 policyInfo.mmapPolicy = AudioMMapPolicy::AUTO;
1355 _aidl_return->push_back(policyInfo);
1356 }
1357 }
1358 }
1359 return ndk::ScopedAStatus::ok();
1360}
1361
Eric Laurente2432ea2023-01-12 17:47:31 +01001362ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) {
1363 LOG(DEBUG) << __func__;
1364 *_aidl_return = false;
1365 return ndk::ScopedAStatus::ok();
1366}
1367
jiabinb76981e2023-01-18 00:58:30 +00001368ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) {
1369 if (!isMmapSupported()) {
1370 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1371 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1372 }
1373 *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT;
1374 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1375 return ndk::ScopedAStatus::ok();
1376}
1377
1378ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) {
1379 if (!isMmapSupported()) {
1380 LOG(DEBUG) << __func__ << ": mmap is not supported ";
1381 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1382 }
1383 *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US;
1384 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
1385 return ndk::ScopedAStatus::ok();
1386}
1387
1388bool Module::isMmapSupported() {
1389 if (mIsMmapSupported.has_value()) {
1390 return mIsMmapSupported.value();
1391 }
1392 std::vector<AudioMMapPolicyInfo> mmapPolicyInfos;
1393 if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) {
1394 mIsMmapSupported = false;
1395 } else {
1396 mIsMmapSupported =
1397 std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) {
1398 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
1399 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
1400 }) != mmapPolicyInfos.end();
1401 }
1402 return mIsMmapSupported.value();
1403}
1404
jiabin253bd322023-01-25 23:57:31 +00001405ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) {
jiabin116d8392023-03-01 22:52:57 +00001406 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001407 return ndk::ScopedAStatus::ok();
1408}
1409
1410ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch(
1411 const std::vector<AudioPortConfig*>& sources __unused,
1412 const std::vector<AudioPortConfig*>& sinks __unused) {
jiabin116d8392023-03-01 22:52:57 +00001413 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
jiabin253bd322023-01-25 23:57:31 +00001414 return ndk::ScopedAStatus::ok();
1415}
1416
jiabin783c48b2023-02-28 18:28:06 +00001417void Module::onExternalDeviceConnectionChanged(
1418 const ::aidl::android::media::audio::common::AudioPort& audioPort __unused,
1419 bool connected __unused) {
1420 LOG(DEBUG) << __func__ << ": do nothing and return";
1421}
1422
1423ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) {
1424 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1425 return ndk::ScopedAStatus::ok();
1426}
1427
1428ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) {
1429 LOG(VERBOSE) << __func__ << ": do nothing and return ok";
1430 return ndk::ScopedAStatus::ok();
1431}
1432
Ram Mohan18f0d512023-07-01 00:47:09 +05301433Module::BtProfileHandles Module::getBtProfileManagerHandles() {
1434 return std::make_tuple(std::weak_ptr<IBluetooth>(), std::weak_ptr<IBluetoothA2dp>(),
1435 std::weak_ptr<IBluetoothLe>());
1436}
1437
1438ndk::ScopedAStatus Module::bluetoothParametersUpdated() {
1439 return mStreams.bluetoothParametersUpdated();
1440}
1441
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001442} // namespace aidl::android::hardware::audio::core