blob: b44f37b293f37ad558046cf27ac50fd605af564d [file] [log] [blame]
Shraddha Basantwani6bb69632023-04-25 15:26:38 +05301/*
2 * Copyright (C) 2023 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#define LOG_TAG "AHAL_ModuleRemoteSubmix"
18
Mikhail Naganov9eb33142024-01-09 14:06:49 -080019#include <stdio.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053020#include <vector>
21
22#include <android-base/logging.h>
David Lib089c0c2023-08-10 12:47:44 +080023#include <error/expected_utils.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053024
Mikhail Naganov13501872023-10-18 16:15:46 -070025#include "SubmixRoute.h"
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053026#include "core-impl/ModuleRemoteSubmix.h"
27#include "core-impl/StreamRemoteSubmix.h"
28
29using aidl::android::hardware::audio::common::SinkMetadata;
30using aidl::android::hardware::audio::common::SourceMetadata;
Mikhail Naganov3b732892023-12-20 16:05:01 -080031using aidl::android::media::audio::common::AudioDeviceAddress;
Dean Wheatleyce9767a2023-10-24 11:23:49 +110032using aidl::android::media::audio::common::AudioFormatType;
Mikhail Naganov3b732892023-12-20 16:05:01 -080033using aidl::android::media::audio::common::AudioIoFlags;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053034using aidl::android::media::audio::common::AudioOffloadInfo;
35using aidl::android::media::audio::common::AudioPort;
36using aidl::android::media::audio::common::AudioPortConfig;
Mikhail Naganov3b732892023-12-20 16:05:01 -080037using aidl::android::media::audio::common::AudioPortExt;
38using aidl::android::media::audio::common::AudioProfile;
39using aidl::android::media::audio::common::Int;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053040using aidl::android::media::audio::common::MicrophoneInfo;
41
42namespace aidl::android::hardware::audio::core {
43
Mikhail Naganov3b732892023-12-20 16:05:01 -080044namespace {
45
46std::optional<r_submix::AudioConfig> getRemoteEndConfig(const AudioPort& audioPort) {
47 const auto& deviceAddress = audioPort.ext.get<AudioPortExt::device>().device.address;
48 const bool isInput = audioPort.flags.getTag() == AudioIoFlags::input;
49 if (auto submixRoute = r_submix::SubmixRoute::findRoute(deviceAddress);
50 submixRoute != nullptr) {
51 if ((isInput && submixRoute->isStreamOutOpen()) ||
52 (!isInput && submixRoute->isStreamInOpen())) {
53 return submixRoute->getPipeConfig();
54 }
55 }
56 return {};
57}
58
59} // namespace
60
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053061ndk::ScopedAStatus ModuleRemoteSubmix::getMicMute(bool* _aidl_return __unused) {
62 LOG(DEBUG) << __func__ << ": is not supported";
63 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
64}
65
66ndk::ScopedAStatus ModuleRemoteSubmix::setMicMute(bool in_mute __unused) {
67 LOG(DEBUG) << __func__ << ": is not supported";
68 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
69}
70
Mikhail Naganov3b732892023-12-20 16:05:01 -080071ndk::ScopedAStatus ModuleRemoteSubmix::setAudioPortConfig(const AudioPortConfig& in_requested,
72 AudioPortConfig* out_suggested,
73 bool* _aidl_return) {
74 auto fillConfig = [this](const AudioPort& port, AudioPortConfig* config) {
75 if (port.ext.getTag() == AudioPortExt::device) {
76 if (auto pipeConfig = getRemoteEndConfig(port); pipeConfig.has_value()) {
77 LOG(DEBUG) << "setAudioPortConfig: suggesting port config from the remote end.";
78 config->format = pipeConfig->format;
79 config->channelMask = pipeConfig->channelLayout;
80 config->sampleRate = Int{.value = pipeConfig->sampleRate};
81 config->flags = port.flags;
82 config->ext = port.ext;
83 return true;
84 }
85 }
86 return generateDefaultPortConfig(port, config);
87 };
88 return Module::setAudioPortConfigImpl(in_requested, fillConfig, out_suggested, _aidl_return);
89}
90
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053091ndk::ScopedAStatus ModuleRemoteSubmix::createInputStream(
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -070092 StreamContext&& context, const SinkMetadata& sinkMetadata,
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053093 const std::vector<MicrophoneInfo>& microphones, std::shared_ptr<StreamIn>* result) {
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -070094 return createStreamInstance<StreamInRemoteSubmix>(result, std::move(context), sinkMetadata,
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053095 microphones);
96}
97
98ndk::ScopedAStatus ModuleRemoteSubmix::createOutputStream(
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -070099 StreamContext&& context, const SourceMetadata& sourceMetadata,
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530100 const std::optional<AudioOffloadInfo>& offloadInfo, std::shared_ptr<StreamOut>* result) {
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -0700101 return createStreamInstance<StreamOutRemoteSubmix>(result, std::move(context), sourceMetadata,
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530102 offloadInfo);
103}
104
Mikhail Naganova92039a2023-12-20 14:27:22 -0800105ndk::ScopedAStatus ModuleRemoteSubmix::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
Mikhail Naganov3b732892023-12-20 16:05:01 -0800106 if (audioPort->ext.getTag() != AudioPortExt::device) {
107 LOG(ERROR) << __func__ << ": not a device port: " << audioPort->toString();
108 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
109 }
110 // If there is already a pipe with a stream for the port address, provide its configuration as
111 // the only option. Otherwise, find the corresponding mix port and copy its profiles.
112 if (auto pipeConfig = getRemoteEndConfig(*audioPort); pipeConfig.has_value()) {
113 audioPort->profiles.clear();
114 audioPort->profiles.push_back(AudioProfile{
115 .format = pipeConfig->format,
116 .channelMasks = std::vector<AudioChannelLayout>({pipeConfig->channelLayout}),
117 .sampleRates = std::vector<int>({pipeConfig->sampleRate})});
118 LOG(DEBUG) << __func__ << ": populated from remote end as: " << audioPort->toString();
119 return ndk::ScopedAStatus::ok();
120 }
121
David Lib089c0c2023-08-10 12:47:44 +0800122 // At this moment, the port has the same ID as the template port, see connectExternalDevice.
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700123 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(audioPort->id);
David Lib089c0c2023-08-10 12:47:44 +0800124 if (routes.empty()) {
125 LOG(ERROR) << __func__ << ": no routes found for the port " << audioPort->toString();
126 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530127 }
David Lib089c0c2023-08-10 12:47:44 +0800128 const auto& route = *routes.begin();
129 AudioPort mixPort;
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700130 if (route->sinkPortId == audioPort->id) {
131 if (route->sourcePortIds.empty()) {
132 LOG(ERROR) << __func__ << ": invalid route " << route->toString();
David Lib089c0c2023-08-10 12:47:44 +0800133 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
134 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700135 RETURN_STATUS_IF_ERROR(getAudioPort(*route->sourcePortIds.begin(), &mixPort));
David Lib089c0c2023-08-10 12:47:44 +0800136 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -0700137 RETURN_STATUS_IF_ERROR(getAudioPort(route->sinkPortId, &mixPort));
David Lib089c0c2023-08-10 12:47:44 +0800138 }
139 audioPort->profiles = mixPort.profiles;
Mikhail Naganov3b732892023-12-20 16:05:01 -0800140 LOG(DEBUG) << __func__ << ": populated from the mix port as: " << audioPort->toString();
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530141 return ndk::ScopedAStatus::ok();
142}
143
144ndk::ScopedAStatus ModuleRemoteSubmix::checkAudioPatchEndpointsMatch(
145 const std::vector<AudioPortConfig*>& sources, const std::vector<AudioPortConfig*>& sinks) {
146 for (const auto& source : sources) {
147 for (const auto& sink : sinks) {
148 if (source->sampleRate != sink->sampleRate ||
149 source->channelMask != sink->channelMask || source->format != sink->format) {
150 LOG(ERROR) << __func__
151 << ": mismatch port configuration, source=" << source->toString()
152 << ", sink=" << sink->toString();
153 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
154 }
155 }
156 }
157 return ndk::ScopedAStatus::ok();
158}
159
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530160ndk::ScopedAStatus ModuleRemoteSubmix::onMasterMuteChanged(bool __unused) {
161 LOG(DEBUG) << __func__ << ": is not supported";
162 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
163}
164
165ndk::ScopedAStatus ModuleRemoteSubmix::onMasterVolumeChanged(float __unused) {
166 LOG(DEBUG) << __func__ << ": is not supported";
167 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
168}
169
Mikhail Naganov13501872023-10-18 16:15:46 -0700170int32_t ModuleRemoteSubmix::getNominalLatencyMs(const AudioPortConfig&) {
171 // See the note on kDefaultPipePeriodCount.
172 static constexpr int32_t kMaxLatencyMs =
173 (r_submix::kDefaultPipeSizeInFrames * 1000) / r_submix::kDefaultSampleRateHz;
174 static constexpr int32_t kMinLatencyMs = kMaxLatencyMs / r_submix::kDefaultPipePeriodCount;
Mikhail Naganov7b234d42023-12-05 11:28:56 -0800175 return kMinLatencyMs;
Mikhail Naganov13501872023-10-18 16:15:46 -0700176}
177
Mikhail Naganov9eb33142024-01-09 14:06:49 -0800178binder_status_t ModuleRemoteSubmix::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
179 dprintf(fd, "\nSubmixRoutes:\n%s\n", r_submix::SubmixRoute::dumpRoutes().c_str());
180 return STATUS_OK;
181}
182
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530183} // namespace aidl::android::hardware::audio::core