blob: e8b5bfc8f39affb2e183730b40345102bea4f748 [file] [log] [blame]
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <algorithm>
18#include <set>
19
20#define LOG_TAG "AHAL_Module"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000021#include <android-base/logging.h>
Shunkai Yao39bf2c32022-12-06 03:25:59 +000022#include <android/binder_ibinder_platform.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000023
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000024#include <Utils.h>
25#include <aidl/android/media/audio/common/AudioInputFlags.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000026#include <aidl/android/media/audio/common/AudioOutputFlags.h>
27
Mikhail Naganov10c6fe22022-09-30 23:49:17 +000028#include "core-impl/Bluetooth.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000029#include "core-impl/Module.h"
Vlad Popa943b7e22022-12-08 14:24:12 +010030#include "core-impl/SoundDose.h"
Mikhail Naganov3b125b72022-10-05 02:12:39 +000031#include "core-impl/Telephony.h"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000032#include "core-impl/utils.h"
33
34using aidl::android::hardware::audio::common::SinkMetadata;
35using aidl::android::hardware::audio::common::SourceMetadata;
Vlad Popa2afbd1e2022-12-28 17:04:58 +010036using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000037using aidl::android::media::audio::common::AudioChannelLayout;
Mikhail Naganovef6bc742022-10-06 00:14:19 +000038using aidl::android::media::audio::common::AudioDevice;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000039using aidl::android::media::audio::common::AudioFormatDescription;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000040using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000041using aidl::android::media::audio::common::AudioInputFlags;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000042using aidl::android::media::audio::common::AudioIoFlags;
43using aidl::android::media::audio::common::AudioOffloadInfo;
44using aidl::android::media::audio::common::AudioOutputFlags;
45using aidl::android::media::audio::common::AudioPort;
46using aidl::android::media::audio::common::AudioPortConfig;
47using aidl::android::media::audio::common::AudioPortExt;
48using aidl::android::media::audio::common::AudioProfile;
49using aidl::android::media::audio::common::Int;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +000050using aidl::android::media::audio::common::PcmType;
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000051using android::hardware::audio::common::getFrameSizeInBytes;
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +000052using android::hardware::audio::common::isBitPositionFlagSet;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000053
54namespace aidl::android::hardware::audio::core {
55
56namespace {
57
58bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) {
59 *config = {};
60 config->portId = port.id;
61 if (port.profiles.empty()) {
62 LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles";
63 return false;
64 }
65 const auto& profile = port.profiles.begin();
66 config->format = profile->format;
67 if (profile->channelMasks.empty()) {
68 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
69 << " has no channel masks";
70 return false;
71 }
72 config->channelMask = *profile->channelMasks.begin();
73 if (profile->sampleRates.empty()) {
74 LOG(ERROR) << __func__ << ": the first profile in port " << port.id
75 << " has no sample rates";
76 return false;
77 }
78 Int sampleRate;
79 sampleRate.value = *profile->sampleRates.begin();
80 config->sampleRate = sampleRate;
81 config->flags = port.flags;
82 config->ext = port.ext;
83 return true;
84}
85
86bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format,
87 AudioProfile* profile) {
88 if (auto profilesIt =
89 find_if(port.profiles.begin(), port.profiles.end(),
90 [&format](const auto& profile) { return profile.format == format; });
91 profilesIt != port.profiles.end()) {
92 *profile = *profilesIt;
93 return true;
94 }
95 return false;
96}
Mikhail Naganov00603d12022-05-02 22:52:13 +000097
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000098} // namespace
99
100void Module::cleanUpPatch(int32_t patchId) {
101 erase_all_values(mPatches, std::set<int32_t>{patchId});
102}
103
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000104ndk::ScopedAStatus Module::createStreamContext(int32_t in_portConfigId, int64_t in_bufferSizeFrames,
Mikhail Naganov30301a42022-09-13 01:20:45 +0000105 std::shared_ptr<IStreamCallback> asyncCallback,
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000106 StreamContext* out_context) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000107 if (in_bufferSizeFrames <= 0) {
108 LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames;
109 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
110 }
111 if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) {
112 LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames
113 << ", must be at least " << kMinimumStreamBufferSizeFrames;
114 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
115 }
116 auto& configs = getConfig().portConfigs;
117 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000118 // Since this is a private method, it is assumed that
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000119 // validity of the portConfigId has already been checked.
120 const size_t frameSize =
121 getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value());
122 if (frameSize == 0) {
123 LOG(ERROR) << __func__ << ": could not calculate frame size for port config "
124 << portConfigIt->toString();
125 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
126 }
127 LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes";
128 if (frameSize > kMaximumStreamBufferSizeBytes / in_bufferSizeFrames) {
129 LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames
130 << " frames is too large, maximum size is "
131 << kMaximumStreamBufferSizeBytes / frameSize;
132 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
133 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000134 const auto& flags = portConfigIt->flags.value();
135 if ((flags.getTag() == AudioIoFlags::Tag::input &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000136 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(),
137 AudioInputFlags::MMAP_NOIRQ)) ||
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000138 (flags.getTag() == AudioIoFlags::Tag::output &&
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000139 !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(),
140 AudioOutputFlags::MMAP_NOIRQ))) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000141 StreamContext temp(
142 std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/),
143 std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/),
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000144 portConfigIt->format.value(), portConfigIt->channelMask.value(),
145 std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames),
Mikhail Naganov30301a42022-09-13 01:20:45 +0000146 asyncCallback, mDebug.streamTransientStateDelayMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000147 if (temp.isValid()) {
148 *out_context = std::move(temp);
149 } else {
150 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
151 }
152 } else {
153 // TODO: Implement simulation of MMAP buffer allocation
154 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000155 return ndk::ScopedAStatus::ok();
156}
157
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000158std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) {
159 std::vector<AudioDevice> result;
160 auto& ports = getConfig().ports;
161 auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId));
162 for (auto it = portIds.begin(); it != portIds.end(); ++it) {
163 auto portIt = findById<AudioPort>(ports, *it);
164 if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) {
165 result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device);
166 }
167 }
168 return result;
169}
170
171std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) {
172 std::set<int32_t> result;
173 auto patchIdsRange = mPatches.equal_range(portConfigId);
174 auto& patches = getConfig().patches;
175 for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) {
176 auto patchIt = findById<AudioPatch>(patches, it->second);
177 if (patchIt == patches.end()) {
178 LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches "
179 << "not found in the configuration";
180 }
181 if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(),
182 portConfigId) != patchIt->sourcePortConfigIds.end()) {
183 result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end());
184 } else {
185 result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end());
186 }
187 }
188 return result;
189}
190
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000191ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) {
192 auto& configs = getConfig().portConfigs;
193 auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId);
194 if (portConfigIt == configs.end()) {
195 LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found";
196 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
197 }
198 const int32_t portId = portConfigIt->portId;
199 // In our implementation, configs of mix ports always have unique IDs.
200 CHECK(portId != in_portConfigId);
201 auto& ports = getConfig().ports;
202 auto portIt = findById<AudioPort>(ports, portId);
203 if (portIt == ports.end()) {
204 LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id "
205 << in_portConfigId << " not found";
206 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
207 }
208 if (mStreams.count(in_portConfigId) != 0) {
209 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
210 << " already has a stream opened on it";
211 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
212 }
213 if (portIt->ext.getTag() != AudioPortExt::Tag::mix) {
214 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
215 << " does not correspond to a mix port";
216 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
217 }
218 const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount;
219 if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) {
220 LOG(ERROR) << __func__ << ": port id " << portId
221 << " has already reached maximum allowed opened stream count: "
222 << maxOpenStreamCount;
223 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
224 }
225 *port = &(*portIt);
226 return ndk::ScopedAStatus::ok();
227}
228
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000229template <typename C>
230std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) {
231 std::set<int32_t> result;
232 auto& portConfigs = getConfig().portConfigs;
233 for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) {
234 auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it);
235 if (portConfigIt != portConfigs.end()) {
236 result.insert(portConfigIt->portId);
237 }
238 }
239 return result;
240}
241
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000242internal::Configuration& Module::getConfig() {
243 if (!mConfig) {
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000244 switch (mType) {
245 case Type::DEFAULT:
246 mConfig = std::move(internal::getPrimaryConfiguration());
247 break;
248 case Type::R_SUBMIX:
249 mConfig = std::move(internal::getRSubmixConfiguration());
250 break;
251 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000252 }
253 return *mConfig;
254}
255
256void Module::registerPatch(const AudioPatch& patch) {
257 auto& configs = getConfig().portConfigs;
258 auto do_insert = [&](const std::vector<int32_t>& portConfigIds) {
259 for (auto portConfigId : portConfigIds) {
260 auto configIt = findById<AudioPortConfig>(configs, portConfigId);
261 if (configIt != configs.end()) {
262 mPatches.insert(std::pair{portConfigId, patch.id});
263 if (configIt->portId != portConfigId) {
264 mPatches.insert(std::pair{configIt->portId, patch.id});
265 }
266 }
267 };
268 };
269 do_insert(patch.sourcePortConfigIds);
270 do_insert(patch.sinkPortConfigIds);
271}
272
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000273void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) {
274 // Streams from the old patch need to be disconnected, streams from the new
275 // patch need to be connected. If the stream belongs to both patches, no need
276 // to update it.
277 std::set<int32_t> idsToDisconnect, idsToConnect;
278 idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(),
279 oldPatch.sourcePortConfigIds.end());
280 idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end());
281 idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end());
282 idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end());
283 std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) {
284 if (idsToConnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000285 LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected";
286 mStreams.setStreamIsConnected(portConfigId, {});
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000287 }
288 });
289 std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) {
290 if (idsToDisconnect.count(portConfigId) == 0) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000291 const auto connectedDevices = findConnectedDevices(portConfigId);
292 LOG(DEBUG) << "The stream on port config id " << portConfigId
293 << " is connected to: " << ::android::internal::ToString(connectedDevices);
294 mStreams.setStreamIsConnected(portConfigId, connectedDevices);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000295 }
296 });
297}
298
Mikhail Naganov00603d12022-05-02 22:52:13 +0000299ndk::ScopedAStatus Module::setModuleDebug(
300 const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) {
301 LOG(DEBUG) << __func__ << ": old flags:" << mDebug.toString()
302 << ", new flags: " << in_debug.toString();
303 if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections &&
304 !mConnectedDevicePorts.empty()) {
305 LOG(ERROR) << __func__ << ": attempting to change device connections simulation "
306 << "while having external devices connected";
307 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
308 }
Mikhail Naganovbd483c02022-11-17 20:33:39 +0000309 if (in_debug.streamTransientStateDelayMs < 0) {
310 LOG(ERROR) << __func__ << ": streamTransientStateDelayMs is negative: "
311 << in_debug.streamTransientStateDelayMs;
312 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
313 }
Mikhail Naganov00603d12022-05-02 22:52:13 +0000314 mDebug = in_debug;
315 return ndk::ScopedAStatus::ok();
316}
317
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000318ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) {
319 if (mTelephony == nullptr) {
320 mTelephony = ndk::SharedRefBase::make<Telephony>();
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000321 mTelephonyBinder = mTelephony->asBinder();
322 AIBinder_setMinSchedulerPolicy(mTelephonyBinder.get(), SCHED_NORMAL,
Shunkai Yao39bf2c32022-12-06 03:25:59 +0000323 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000324 }
325 *_aidl_return = mTelephony;
326 LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get();
327 return ndk::ScopedAStatus::ok();
328}
329
Mikhail Naganov10c6fe22022-09-30 23:49:17 +0000330ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) {
331 if (mBluetooth == nullptr) {
332 mBluetooth = ndk::SharedRefBase::make<Bluetooth>();
333 mBluetoothBinder = mBluetooth->asBinder();
334 AIBinder_setMinSchedulerPolicy(mBluetoothBinder.get(), SCHED_NORMAL,
335 ANDROID_PRIORITY_AUDIO);
336 }
337 *_aidl_return = mBluetooth;
338 LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get();
339 return ndk::ScopedAStatus::ok();
340}
341
Mikhail Naganov00603d12022-05-02 22:52:13 +0000342ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData,
343 AudioPort* _aidl_return) {
344 const int32_t templateId = in_templateIdAndAdditionalData.id;
345 auto& ports = getConfig().ports;
346 AudioPort connectedPort;
347 { // Scope the template port so that we don't accidentally modify it.
348 auto templateIt = findById<AudioPort>(ports, templateId);
349 if (templateIt == ports.end()) {
350 LOG(ERROR) << __func__ << ": port id " << templateId << " not found";
351 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
352 }
353 if (templateIt->ext.getTag() != AudioPortExt::Tag::device) {
354 LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port";
355 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
356 }
357 if (!templateIt->profiles.empty()) {
358 LOG(ERROR) << __func__ << ": port id " << templateId
359 << " does not have dynamic profiles";
360 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
361 }
362 auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>();
363 if (templateDevicePort.device.type.connection.empty()) {
364 LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached";
365 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
366 }
367 // Postpone id allocation until we ensure that there are no client errors.
368 connectedPort = *templateIt;
369 connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors;
370 const auto& inputDevicePort =
371 in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>();
372 auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>();
373 connectedDevicePort.device.address = inputDevicePort.device.address;
374 LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to "
375 << connectedDevicePort.device.toString();
376 // Check if there is already a connected port with for the same external device.
377 for (auto connectedPortId : mConnectedDevicePorts) {
378 auto connectedPortIt = findById<AudioPort>(ports, connectedPortId);
379 if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device ==
380 connectedDevicePort.device) {
381 LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString()
382 << " is already connected at the device port id " << connectedPortId;
383 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
384 }
385 }
386 }
387
388 if (!mDebug.simulateDeviceConnections) {
389 // In a real HAL here we would attempt querying the profiles from the device.
390 LOG(ERROR) << __func__ << ": failed to query supported device profiles";
391 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
392 }
393
394 connectedPort.id = ++getConfig().nextPortId;
395 mConnectedDevicePorts.insert(connectedPort.id);
396 LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, "
397 << "connected port ID " << connectedPort.id;
398 auto& connectedProfiles = getConfig().connectedProfiles;
399 if (auto connectedProfilesIt = connectedProfiles.find(templateId);
400 connectedProfilesIt != connectedProfiles.end()) {
401 connectedPort.profiles = connectedProfilesIt->second;
402 }
403 ports.push_back(connectedPort);
404 *_aidl_return = std::move(connectedPort);
405
406 std::vector<AudioRoute> newRoutes;
407 auto& routes = getConfig().routes;
408 for (auto& r : routes) {
409 if (r.sinkPortId == templateId) {
410 AudioRoute newRoute;
411 newRoute.sourcePortIds = r.sourcePortIds;
412 newRoute.sinkPortId = connectedPort.id;
413 newRoute.isExclusive = r.isExclusive;
414 newRoutes.push_back(std::move(newRoute));
415 } else {
416 auto& srcs = r.sourcePortIds;
417 if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) {
418 srcs.push_back(connectedPort.id);
419 }
420 }
421 }
422 routes.insert(routes.end(), newRoutes.begin(), newRoutes.end());
423
424 return ndk::ScopedAStatus::ok();
425}
426
427ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) {
428 auto& ports = getConfig().ports;
429 auto portIt = findById<AudioPort>(ports, in_portId);
430 if (portIt == ports.end()) {
431 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
432 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
433 }
434 if (portIt->ext.getTag() != AudioPortExt::Tag::device) {
435 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port";
436 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
437 }
438 if (mConnectedDevicePorts.count(in_portId) == 0) {
439 LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port";
440 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
441 }
442 auto& configs = getConfig().portConfigs;
443 auto& initials = getConfig().initialConfigs;
444 auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) {
445 if (config.portId == in_portId) {
446 // Check if the configuration was provided by the client.
447 const auto& initialIt = findById<AudioPortConfig>(initials, config.id);
448 return initialIt == initials.end() || config != *initialIt;
449 }
450 return false;
451 });
452 if (configIt != configs.end()) {
453 LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id "
454 << configIt->id;
455 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
456 }
457 ports.erase(portIt);
458 mConnectedDevicePorts.erase(in_portId);
459 LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released";
460
461 auto& routes = getConfig().routes;
462 for (auto routesIt = routes.begin(); routesIt != routes.end();) {
463 if (routesIt->sinkPortId == in_portId) {
464 routesIt = routes.erase(routesIt);
465 } else {
466 // Note: the list of sourcePortIds can't become empty because there must
467 // be the id of the template port in the route.
468 erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; });
469 ++routesIt;
470 }
471 }
472
473 return ndk::ScopedAStatus::ok();
474}
475
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000476ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) {
477 *_aidl_return = getConfig().patches;
478 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches";
479 return ndk::ScopedAStatus::ok();
480}
481
482ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) {
483 auto& ports = getConfig().ports;
484 auto portIt = findById<AudioPort>(ports, in_portId);
485 if (portIt != ports.end()) {
486 *_aidl_return = *portIt;
487 LOG(DEBUG) << __func__ << ": returning port by id " << in_portId;
488 return ndk::ScopedAStatus::ok();
489 }
490 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
491 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
492}
493
494ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) {
495 *_aidl_return = getConfig().portConfigs;
496 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs";
497 return ndk::ScopedAStatus::ok();
498}
499
500ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) {
501 *_aidl_return = getConfig().ports;
502 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports";
503 return ndk::ScopedAStatus::ok();
504}
505
506ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) {
507 *_aidl_return = getConfig().routes;
508 LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes";
509 return ndk::ScopedAStatus::ok();
510}
511
Mikhail Naganov00603d12022-05-02 22:52:13 +0000512ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId,
513 std::vector<AudioRoute>* _aidl_return) {
514 auto& ports = getConfig().ports;
515 if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) {
516 LOG(ERROR) << __func__ << ": port id " << in_portId << " not found";
517 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
518 }
519 auto& routes = getConfig().routes;
520 std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return),
521 [&](const auto& r) {
522 const auto& srcs = r.sourcePortIds;
523 return r.sinkPortId == in_portId ||
524 std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end();
525 });
526 return ndk::ScopedAStatus::ok();
527}
528
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000529ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args,
530 OpenInputStreamReturn* _aidl_return) {
531 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size "
532 << in_args.bufferSizeFrames << " frames";
533 AudioPort* port = nullptr;
534 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
535 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000536 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000537 if (port->flags.getTag() != AudioIoFlags::Tag::input) {
538 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000539 << " does not correspond to an input mix port";
540 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
541 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000542 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000543 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr,
544 &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000545 !status.isOk()) {
546 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000547 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000548 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000549 std::shared_ptr<StreamIn> stream;
550 if (auto status = StreamIn::createInstance(in_args.sinkMetadata, std::move(context),
551 mConfig->microphones, &stream);
552 !status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000553 return status;
554 }
555 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000556 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
557 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000558 auto patchIt = mPatches.find(in_args.portConfigId);
559 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000560 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000561 }
562 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000563 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000564 return ndk::ScopedAStatus::ok();
565}
566
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000567ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args,
568 OpenOutputStreamReturn* _aidl_return) {
569 LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? "
570 << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames
571 << " frames";
572 AudioPort* port = nullptr;
573 if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) {
574 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000575 }
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000576 if (port->flags.getTag() != AudioIoFlags::Tag::output) {
577 LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000578 << " does not correspond to an output mix port";
579 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
580 }
Mikhail Naganova2c5ddf2022-09-12 22:57:14 +0000581 const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
582 AudioOutputFlags::COMPRESS_OFFLOAD);
583 if (isOffload && !in_args.offloadInfo.has_value()) {
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000584 LOG(ERROR) << __func__ << ": port id " << port->id
Mikhail Naganov111e0ce2022-06-17 21:41:19 +0000585 << " has COMPRESS_OFFLOAD flag set, requires offload info";
586 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
587 }
Mikhail Naganov30301a42022-09-13 01:20:45 +0000588 const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(),
589 AudioOutputFlags::NON_BLOCKING);
590 if (isNonBlocking && in_args.callback == nullptr) {
591 LOG(ERROR) << __func__ << ": port id " << port->id
592 << " has NON_BLOCKING flag set, requires async callback";
593 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
594 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000595 StreamContext context;
Mikhail Naganov30301a42022-09-13 01:20:45 +0000596 if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames,
597 isNonBlocking ? in_args.callback : nullptr, &context);
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000598 !status.isOk()) {
599 return status;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000600 }
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000601 context.fillDescriptor(&_aidl_return->desc);
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000602 std::shared_ptr<StreamOut> stream;
603 if (auto status = StreamOut::createInstance(in_args.sourceMetadata, std::move(context),
604 in_args.offloadInfo, &stream);
605 !status.isOk()) {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000606 return status;
607 }
608 StreamWrapper streamWrapper(stream);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000609 AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL,
610 ANDROID_PRIORITY_AUDIO);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000611 auto patchIt = mPatches.find(in_args.portConfigId);
612 if (patchIt != mPatches.end()) {
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000613 streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId));
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000614 }
615 mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper));
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000616 _aidl_return->stream = std::move(stream);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000617 return ndk::ScopedAStatus::ok();
618}
619
Mikhail Naganov74927202022-12-19 16:37:14 +0000620ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors(
621 SupportedPlaybackRateFactors* _aidl_return) {
622 LOG(DEBUG) << __func__;
623 (void)_aidl_return;
624 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
625}
626
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000627ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) {
Mikhail Naganov16db9b72022-06-17 21:36:18 +0000628 LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000629 if (in_requested.sourcePortConfigIds.empty()) {
630 LOG(ERROR) << __func__ << ": requested patch has empty sources list";
631 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
632 }
633 if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) {
634 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list";
635 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
636 }
637 if (in_requested.sinkPortConfigIds.empty()) {
638 LOG(ERROR) << __func__ << ": requested patch has empty sinks list";
639 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
640 }
641 if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) {
642 LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list";
643 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
644 }
645
646 auto& configs = getConfig().portConfigs;
647 std::vector<int32_t> missingIds;
648 auto sources =
649 selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds);
650 if (!missingIds.empty()) {
651 LOG(ERROR) << __func__ << ": following source port config ids not found: "
652 << ::android::internal::ToString(missingIds);
653 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
654 }
655 auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds);
656 if (!missingIds.empty()) {
657 LOG(ERROR) << __func__ << ": following sink port config ids not found: "
658 << ::android::internal::ToString(missingIds);
659 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
660 }
661 // bool indicates whether a non-exclusive route is available.
662 // If only an exclusive route is available, that means the patch can not be
663 // established if there is any other patch which currently uses the sink port.
664 std::map<int32_t, bool> allowedSinkPorts;
665 auto& routes = getConfig().routes;
666 for (auto src : sources) {
667 for (const auto& r : routes) {
668 const auto& srcs = r.sourcePortIds;
669 if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) {
670 if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive
671 allowedSinkPorts[r.sinkPortId] = !r.isExclusive;
672 }
673 }
674 }
675 }
676 for (auto sink : sinks) {
677 if (allowedSinkPorts.count(sink->portId) == 0) {
678 LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId;
679 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
680 }
681 }
682
683 auto& patches = getConfig().patches;
684 auto existing = patches.end();
685 std::optional<decltype(mPatches)> patchesBackup;
686 if (in_requested.id != 0) {
687 existing = findById<AudioPatch>(patches, in_requested.id);
688 if (existing != patches.end()) {
689 patchesBackup = mPatches;
690 cleanUpPatch(existing->id);
691 } else {
692 LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id;
693 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
694 }
695 }
696 // Validate the requested patch.
697 for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) {
698 if (!nonExclusive && mPatches.count(sinkPortId) != 0) {
699 LOG(ERROR) << __func__ << ": sink port id " << sinkPortId
700 << "is exclusive and is already used by some other patch";
701 if (patchesBackup.has_value()) {
702 mPatches = std::move(*patchesBackup);
703 }
704 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
705 }
706 }
707 *_aidl_return = in_requested;
Mikhail Naganov6a4872d2022-06-15 21:39:04 +0000708 _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames;
709 _aidl_return->latenciesMs.clear();
710 _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(),
711 _aidl_return->sinkPortConfigIds.size(), kLatencyMs);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000712 AudioPatch oldPatch{};
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000713 if (existing == patches.end()) {
714 _aidl_return->id = getConfig().nextPatchId++;
715 patches.push_back(*_aidl_return);
716 existing = patches.begin() + (patches.size() - 1);
717 } else {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000718 oldPatch = *existing;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000719 *existing = *_aidl_return;
720 }
721 registerPatch(*existing);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000722 updateStreamsConnectedState(oldPatch, *_aidl_return);
723
724 LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch "
725 << _aidl_return->toString();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000726 return ndk::ScopedAStatus::ok();
727}
728
729ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested,
730 AudioPortConfig* out_suggested, bool* _aidl_return) {
731 LOG(DEBUG) << __func__ << ": requested " << in_requested.toString();
732 auto& configs = getConfig().portConfigs;
733 auto existing = configs.end();
734 if (in_requested.id != 0) {
735 if (existing = findById<AudioPortConfig>(configs, in_requested.id);
736 existing == configs.end()) {
737 LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id
738 << " not found";
739 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
740 }
741 }
742
743 const int portId = existing != configs.end() ? existing->portId : in_requested.portId;
744 if (portId == 0) {
745 LOG(ERROR) << __func__ << ": input port config does not specify portId";
746 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
747 }
748 auto& ports = getConfig().ports;
749 auto portIt = findById<AudioPort>(ports, portId);
750 if (portIt == ports.end()) {
751 LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId;
752 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
753 }
754 if (existing != configs.end()) {
755 *out_suggested = *existing;
756 } else {
757 AudioPortConfig newConfig;
758 if (generateDefaultPortConfig(*portIt, &newConfig)) {
759 *out_suggested = newConfig;
760 } else {
761 LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId;
762 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
763 }
764 }
765 // From this moment, 'out_suggested' is either an existing port config,
766 // or a new generated config. Now attempt to update it according to the specified
767 // fields of 'in_requested'.
768
769 bool requestedIsValid = true, requestedIsFullySpecified = true;
770
771 AudioIoFlags portFlags = portIt->flags;
772 if (in_requested.flags.has_value()) {
773 if (in_requested.flags.value() != portFlags) {
774 LOG(WARNING) << __func__ << ": requested flags "
775 << in_requested.flags.value().toString() << " do not match port's "
776 << portId << " flags " << portFlags.toString();
777 requestedIsValid = false;
778 }
779 } else {
780 requestedIsFullySpecified = false;
781 }
782
783 AudioProfile portProfile;
784 if (in_requested.format.has_value()) {
785 const auto& format = in_requested.format.value();
786 if (findAudioProfile(*portIt, format, &portProfile)) {
787 out_suggested->format = format;
788 } else {
789 LOG(WARNING) << __func__ << ": requested format " << format.toString()
790 << " is not found in port's " << portId << " profiles";
791 requestedIsValid = false;
792 }
793 } else {
794 requestedIsFullySpecified = false;
795 }
796 if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) {
797 LOG(ERROR) << __func__ << ": port " << portId << " does not support format "
798 << out_suggested->format.value().toString() << " anymore";
799 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
800 }
801
802 if (in_requested.channelMask.has_value()) {
803 const auto& channelMask = in_requested.channelMask.value();
804 if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) !=
805 portProfile.channelMasks.end()) {
806 out_suggested->channelMask = channelMask;
807 } else {
808 LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString()
809 << " is not supported for the format " << portProfile.format.toString()
810 << " by the port " << portId;
811 requestedIsValid = false;
812 }
813 } else {
814 requestedIsFullySpecified = false;
815 }
816
817 if (in_requested.sampleRate.has_value()) {
818 const auto& sampleRate = in_requested.sampleRate.value();
819 if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(),
820 sampleRate.value) != portProfile.sampleRates.end()) {
821 out_suggested->sampleRate = sampleRate;
822 } else {
823 LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value
824 << " is not supported for the format " << portProfile.format.toString()
825 << " by the port " << portId;
826 requestedIsValid = false;
827 }
828 } else {
829 requestedIsFullySpecified = false;
830 }
831
832 if (in_requested.gain.has_value()) {
833 // Let's pretend that gain can always be applied.
834 out_suggested->gain = in_requested.gain.value();
835 }
836
837 if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) {
838 out_suggested->id = getConfig().nextPortId++;
839 configs.push_back(*out_suggested);
840 *_aidl_return = true;
841 LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString();
842 } else if (existing != configs.end() && requestedIsValid) {
843 *existing = *out_suggested;
844 *_aidl_return = true;
845 LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString();
846 } else {
847 LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end())
848 << "; requested is valid? " << requestedIsValid << ", fully specified? "
849 << requestedIsFullySpecified;
850 *_aidl_return = false;
851 }
852 return ndk::ScopedAStatus::ok();
853}
854
855ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) {
856 auto& patches = getConfig().patches;
857 auto patchIt = findById<AudioPatch>(patches, in_patchId);
858 if (patchIt != patches.end()) {
859 cleanUpPatch(patchIt->id);
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +0000860 updateStreamsConnectedState(*patchIt, AudioPatch{});
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000861 patches.erase(patchIt);
862 LOG(DEBUG) << __func__ << ": erased patch " << in_patchId;
863 return ndk::ScopedAStatus::ok();
864 }
865 LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found";
866 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
867}
868
869ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) {
870 auto& configs = getConfig().portConfigs;
871 auto configIt = findById<AudioPortConfig>(configs, in_portConfigId);
872 if (configIt != configs.end()) {
873 if (mStreams.count(in_portConfigId) != 0) {
874 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
875 << " has a stream opened on it";
876 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
877 }
878 auto patchIt = mPatches.find(in_portConfigId);
879 if (patchIt != mPatches.end()) {
880 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId
881 << " is used by the patch with id " << patchIt->second;
882 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
883 }
884 auto& initials = getConfig().initialConfigs;
885 auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId);
886 if (initialIt == initials.end()) {
887 configs.erase(configIt);
888 LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId;
889 } else if (*configIt != *initialIt) {
890 *configIt = *initialIt;
891 LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId;
892 }
893 return ndk::ScopedAStatus::ok();
894 }
895 LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found";
896 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
897}
898
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000899ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) {
900 *_aidl_return = mMasterMute;
901 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
902 return ndk::ScopedAStatus::ok();
903}
904
905ndk::ScopedAStatus Module::setMasterMute(bool in_mute) {
906 LOG(DEBUG) << __func__ << ": " << in_mute;
907 mMasterMute = in_mute;
908 return ndk::ScopedAStatus::ok();
909}
910
911ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) {
912 *_aidl_return = mMasterVolume;
913 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
914 return ndk::ScopedAStatus::ok();
915}
916
917ndk::ScopedAStatus Module::setMasterVolume(float in_volume) {
918 LOG(DEBUG) << __func__ << ": " << in_volume;
919 if (in_volume >= 0.0f && in_volume <= 1.0f) {
920 mMasterVolume = in_volume;
921 return ndk::ScopedAStatus::ok();
922 }
923 LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume;
924 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
925}
926
927ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) {
928 *_aidl_return = mMicMute;
929 LOG(DEBUG) << __func__ << ": returning " << *_aidl_return;
930 return ndk::ScopedAStatus::ok();
931}
932
933ndk::ScopedAStatus Module::setMicMute(bool in_mute) {
934 LOG(DEBUG) << __func__ << ": " << in_mute;
935 mMicMute = in_mute;
936 return ndk::ScopedAStatus::ok();
937}
938
Mikhail Naganovef6bc742022-10-06 00:14:19 +0000939ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) {
940 *_aidl_return = mConfig->microphones;
941 LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return);
942 return ndk::ScopedAStatus::ok();
943}
944
Mikhail Naganov3b125b72022-10-05 02:12:39 +0000945ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) {
946 // No checks for supported audio modes here, it's an informative notification.
947 LOG(DEBUG) << __func__ << ": " << toString(in_mode);
948 return ndk::ScopedAStatus::ok();
949}
950
951ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) {
952 LOG(DEBUG) << __func__ << ": " << toString(in_rotation);
953 return ndk::ScopedAStatus::ok();
954}
955
956ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) {
957 LOG(DEBUG) << __func__ << ": " << in_isTurnedOn;
958 return ndk::ScopedAStatus::ok();
959}
960
Vlad Popa83a6d822022-11-07 13:53:57 +0100961ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) {
Vlad Popa943b7e22022-12-08 14:24:12 +0100962 if (mSoundDose == nullptr) {
Vlad Popa2afbd1e2022-12-28 17:04:58 +0100963 mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>();
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000964 mSoundDoseBinder = mSoundDose->asBinder();
965 AIBinder_setMinSchedulerPolicy(mSoundDoseBinder.get(), SCHED_NORMAL,
966 ANDROID_PRIORITY_AUDIO);
Vlad Popa943b7e22022-12-08 14:24:12 +0100967 }
968 *_aidl_return = mSoundDose;
969 LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get();
Vlad Popa83a6d822022-11-07 13:53:57 +0100970 return ndk::ScopedAStatus::ok();
971}
972
Mikhail Naganove9f10fc2022-10-14 23:31:52 +0000973ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) {
974 LOG(DEBUG) << __func__;
975 (void)_aidl_return;
976 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
977}
978
979ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids,
980 std::vector<VendorParameter>* _aidl_return) {
981 LOG(DEBUG) << __func__ << ": id count: " << in_ids.size();
982 (void)_aidl_return;
983 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
984}
985
986ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters,
987 bool in_async) {
988 LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size()
989 << ", async: " << in_async;
990 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
991}
992
Mikhail Naganovfb1acde2022-12-12 18:57:36 +0000993ndk::ScopedAStatus Module::addDeviceEffect(
994 int32_t in_portConfigId,
995 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
996 if (in_effect == nullptr) {
997 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
998 } else {
999 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1000 << in_effect->asBinder().get();
1001 }
1002 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1003}
1004
1005ndk::ScopedAStatus Module::removeDeviceEffect(
1006 int32_t in_portConfigId,
1007 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) {
1008 if (in_effect == nullptr) {
1009 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect";
1010 } else {
1011 LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder "
1012 << in_effect->asBinder().get();
1013 }
1014 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
1015}
1016
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001017} // namespace aidl::android::hardware::audio::core