blob: 1a5ee00885649e8f075c1bd99e4010bc64ebdd9a [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
19#include <vector>
20
21#include <android-base/logging.h>
David Lib089c0c2023-08-10 12:47:44 +080022#include <error/expected_utils.h>
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053023
Mikhail Naganov13501872023-10-18 16:15:46 -070024#include "SubmixRoute.h"
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053025#include "core-impl/ModuleRemoteSubmix.h"
26#include "core-impl/StreamRemoteSubmix.h"
27
28using aidl::android::hardware::audio::common::SinkMetadata;
29using aidl::android::hardware::audio::common::SourceMetadata;
Dean Wheatleyce9767a2023-10-24 11:23:49 +110030using aidl::android::media::audio::common::AudioFormatType;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053031using aidl::android::media::audio::common::AudioOffloadInfo;
32using aidl::android::media::audio::common::AudioPort;
33using aidl::android::media::audio::common::AudioPortConfig;
34using aidl::android::media::audio::common::MicrophoneInfo;
35
36namespace aidl::android::hardware::audio::core {
37
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053038ndk::ScopedAStatus ModuleRemoteSubmix::getMicMute(bool* _aidl_return __unused) {
39 LOG(DEBUG) << __func__ << ": is not supported";
40 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
41}
42
43ndk::ScopedAStatus ModuleRemoteSubmix::setMicMute(bool in_mute __unused) {
44 LOG(DEBUG) << __func__ << ": is not supported";
45 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
46}
47
48ndk::ScopedAStatus ModuleRemoteSubmix::createInputStream(
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -070049 StreamContext&& context, const SinkMetadata& sinkMetadata,
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053050 const std::vector<MicrophoneInfo>& microphones, std::shared_ptr<StreamIn>* result) {
Dean Wheatleyce9767a2023-10-24 11:23:49 +110051 if (context.getFormat().type != AudioFormatType::PCM) {
52 LOG(DEBUG) << __func__ << ": not supported for format " << context.getFormat().toString();
53 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
54 }
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -070055 return createStreamInstance<StreamInRemoteSubmix>(result, std::move(context), sinkMetadata,
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053056 microphones);
57}
58
59ndk::ScopedAStatus ModuleRemoteSubmix::createOutputStream(
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -070060 StreamContext&& context, const SourceMetadata& sourceMetadata,
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053061 const std::optional<AudioOffloadInfo>& offloadInfo, std::shared_ptr<StreamOut>* result) {
Dean Wheatleyce9767a2023-10-24 11:23:49 +110062 if (context.getFormat().type != AudioFormatType::PCM) {
63 LOG(DEBUG) << __func__ << ": not supported for format " << context.getFormat().toString();
64 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
65 }
Mikhail Naganov6ddefdb2023-07-19 17:30:06 -070066 return createStreamInstance<StreamOutRemoteSubmix>(result, std::move(context), sourceMetadata,
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053067 offloadInfo);
68}
69
Mikhail Naganova92039a2023-12-20 14:27:22 -080070ndk::ScopedAStatus ModuleRemoteSubmix::populateConnectedDevicePort(AudioPort* audioPort, int32_t) {
David Lib089c0c2023-08-10 12:47:44 +080071 // Find the corresponding mix port and copy its profiles.
David Lib089c0c2023-08-10 12:47:44 +080072 // At this moment, the port has the same ID as the template port, see connectExternalDevice.
Mikhail Naganov84bcc042023-10-05 17:36:57 -070073 std::vector<AudioRoute*> routes = getAudioRoutesForAudioPortImpl(audioPort->id);
David Lib089c0c2023-08-10 12:47:44 +080074 if (routes.empty()) {
75 LOG(ERROR) << __func__ << ": no routes found for the port " << audioPort->toString();
76 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053077 }
David Lib089c0c2023-08-10 12:47:44 +080078 const auto& route = *routes.begin();
79 AudioPort mixPort;
Mikhail Naganov84bcc042023-10-05 17:36:57 -070080 if (route->sinkPortId == audioPort->id) {
81 if (route->sourcePortIds.empty()) {
82 LOG(ERROR) << __func__ << ": invalid route " << route->toString();
David Lib089c0c2023-08-10 12:47:44 +080083 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
84 }
Mikhail Naganov84bcc042023-10-05 17:36:57 -070085 RETURN_STATUS_IF_ERROR(getAudioPort(*route->sourcePortIds.begin(), &mixPort));
David Lib089c0c2023-08-10 12:47:44 +080086 } else {
Mikhail Naganov84bcc042023-10-05 17:36:57 -070087 RETURN_STATUS_IF_ERROR(getAudioPort(route->sinkPortId, &mixPort));
David Lib089c0c2023-08-10 12:47:44 +080088 }
89 audioPort->profiles = mixPort.profiles;
Shraddha Basantwani6bb69632023-04-25 15:26:38 +053090 return ndk::ScopedAStatus::ok();
91}
92
93ndk::ScopedAStatus ModuleRemoteSubmix::checkAudioPatchEndpointsMatch(
94 const std::vector<AudioPortConfig*>& sources, const std::vector<AudioPortConfig*>& sinks) {
95 for (const auto& source : sources) {
96 for (const auto& sink : sinks) {
97 if (source->sampleRate != sink->sampleRate ||
98 source->channelMask != sink->channelMask || source->format != sink->format) {
99 LOG(ERROR) << __func__
100 << ": mismatch port configuration, source=" << source->toString()
101 << ", sink=" << sink->toString();
102 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
103 }
104 }
105 }
106 return ndk::ScopedAStatus::ok();
107}
108
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530109ndk::ScopedAStatus ModuleRemoteSubmix::onMasterMuteChanged(bool __unused) {
110 LOG(DEBUG) << __func__ << ": is not supported";
111 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
112}
113
114ndk::ScopedAStatus ModuleRemoteSubmix::onMasterVolumeChanged(float __unused) {
115 LOG(DEBUG) << __func__ << ": is not supported";
116 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
117}
118
Mikhail Naganov13501872023-10-18 16:15:46 -0700119int32_t ModuleRemoteSubmix::getNominalLatencyMs(const AudioPortConfig&) {
120 // See the note on kDefaultPipePeriodCount.
121 static constexpr int32_t kMaxLatencyMs =
122 (r_submix::kDefaultPipeSizeInFrames * 1000) / r_submix::kDefaultSampleRateHz;
123 static constexpr int32_t kMinLatencyMs = kMaxLatencyMs / r_submix::kDefaultPipePeriodCount;
Mikhail Naganov7b234d42023-12-05 11:28:56 -0800124 return kMinLatencyMs;
Mikhail Naganov13501872023-10-18 16:15:46 -0700125}
126
Shraddha Basantwani6bb69632023-04-25 15:26:38 +0530127} // namespace aidl::android::hardware::audio::core