blob: 6d3c4bd16edddc51429fb245d77518ab10a26cf8 [file] [log] [blame]
Shunkai Yaof60fc372023-12-12 17:48:18 +00001/*
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_SpatializerSw"
18
19#include "SpatializerSw.h"
20
21#include <android-base/logging.h>
22#include <system/audio_effects/effect_uuid.h>
23
24#include <optional>
25
26using aidl::android::hardware::audio::common::getChannelCount;
27using aidl::android::hardware::audio::effect::Descriptor;
28using aidl::android::hardware::audio::effect::getEffectImplUuidSpatializerSw;
29using aidl::android::hardware::audio::effect::getEffectTypeUuidSpatializer;
30using aidl::android::hardware::audio::effect::IEffect;
31using aidl::android::hardware::audio::effect::SpatializerSw;
32using aidl::android::hardware::audio::effect::State;
33using aidl::android::media::audio::common::AudioChannelLayout;
34using aidl::android::media::audio::common::AudioUuid;
35using aidl::android::media::audio::common::HeadTracking;
36using aidl::android::media::audio::common::Spatialization;
37
38extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
39 std::shared_ptr<IEffect>* instanceSpp) {
40 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidSpatializerSw()) {
41 LOG(ERROR) << __func__ << "uuid not supported";
42 return EX_ILLEGAL_ARGUMENT;
43 }
44 if (!instanceSpp) {
45 LOG(ERROR) << __func__ << " invalid input parameter!";
46 return EX_ILLEGAL_ARGUMENT;
47 }
48
49 *instanceSpp = ndk::SharedRefBase::make<SpatializerSw>();
50 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
51 return EX_NONE;
52}
53
54extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
55 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidSpatializerSw()) {
56 LOG(ERROR) << __func__ << "uuid not supported";
57 return EX_ILLEGAL_ARGUMENT;
58 }
59 *_aidl_return = SpatializerSw::kDescriptor;
60 return EX_NONE;
61}
62
63namespace aidl::android::hardware::audio::effect {
64
65const std::string SpatializerSw::kEffectName = "SpatializerSw";
66
67const std::vector<Range::SpatializerRange> SpatializerSw::kRanges = {
68 MAKE_RANGE(Spatializer, supportedChannelLayout, std::vector<AudioChannelLayout>{},
69 std::vector<AudioChannelLayout>{}),
70 MAKE_RANGE(Spatializer, spatializationLevel, Spatialization::Level::NONE,
71 Spatialization::Level::BED_PLUS_OBJECTS),
72 MAKE_RANGE(Spatializer, spatializationMode, Spatialization::Mode::BINAURAL,
73 Spatialization::Mode::TRANSAURAL),
74 MAKE_RANGE(Spatializer, headTrackingSensorId, std::numeric_limits<int>::min(),
75 std::numeric_limits<int>::max()),
76 MAKE_RANGE(Spatializer, headTrackingMode, HeadTracking::Mode::OTHER,
77 HeadTracking::Mode::RELATIVE_SCREEN),
78 MAKE_RANGE(Spatializer, headTrackingConnectionMode,
79 HeadTracking::ConnectionMode::FRAMEWORK_PROCESSED,
80 HeadTracking::ConnectionMode::DIRECT_TO_SENSOR_TUNNEL)};
81const Capability SpatializerSw::kCapability = {.range = {SpatializerSw::kRanges}};
82const Descriptor SpatializerSw::kDescriptor = {
83 .common = {.id = {.type = getEffectTypeUuidSpatializer(),
84 .uuid = getEffectImplUuidSpatializerSw()},
85 .flags = {.type = Flags::Type::INSERT,
86 .insert = Flags::Insert::FIRST,
87 .hwAcceleratorMode = Flags::HardwareAccelerator::NONE},
88 .name = SpatializerSw::kEffectName,
89 .implementor = "The Android Open Source Project"},
90 .capability = SpatializerSw::kCapability};
91
92ndk::ScopedAStatus SpatializerSw::getDescriptor(Descriptor* _aidl_return) {
93 LOG(DEBUG) << __func__ << kDescriptor.toString();
94 *_aidl_return = kDescriptor;
95 return ndk::ScopedAStatus::ok();
96}
97
98ndk::ScopedAStatus SpatializerSw::setParameterSpecific(const Parameter::Specific& specific) {
99 RETURN_IF(Parameter::Specific::spatializer != specific.getTag(), EX_ILLEGAL_ARGUMENT,
100 "EffectNotSupported");
101 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
102
103 auto& param = specific.get<Parameter::Specific::spatializer>();
104 RETURN_IF(!inRange(param, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
105
106 return mContext->setParam(param.getTag(), param);
107}
108
109ndk::ScopedAStatus SpatializerSw::getParameterSpecific(const Parameter::Id& id,
110 Parameter::Specific* specific) {
111 auto tag = id.getTag();
112 RETURN_IF(Parameter::Id::spatializerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
113 auto spatializerId = id.get<Parameter::Id::spatializerTag>();
114 auto spatializerTag = spatializerId.getTag();
115 switch (spatializerTag) {
116 case Spatializer::Id::commonTag: {
117 auto specificTag = spatializerId.get<Spatializer::Id::commonTag>();
118 std::optional<Spatializer> param = mContext->getParam(specificTag);
119 if (!param.has_value()) {
120 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
121 EX_ILLEGAL_ARGUMENT, "SpatializerTagNotSupported");
122 }
123 specific->set<Parameter::Specific::spatializer>(param.value());
124 break;
125 }
126 default: {
127 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
128 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
129 "SpatializerTagNotSupported");
130 }
131 }
132 return ndk::ScopedAStatus::ok();
133}
134
135std::shared_ptr<EffectContext> SpatializerSw::createContext(const Parameter::Common& common) {
136 if (mContext) {
137 LOG(DEBUG) << __func__ << " context already exist";
138 } else {
139 mContext = std::make_shared<SpatializerSwContext>(1 /* statusFmqDepth */, common);
140 }
141 return mContext;
142}
143
Shunkai Yaof60fc372023-12-12 17:48:18 +0000144RetCode SpatializerSw::releaseContext() {
145 if (mContext) {
146 mContext.reset();
147 }
148 return RetCode::SUCCESS;
149}
150
151SpatializerSw::~SpatializerSw() {
152 cleanUp();
153 LOG(DEBUG) << __func__;
154}
155
156// Processing method running in EffectWorker thread.
157IEffect::Status SpatializerSw::effectProcessImpl(float* in, float* out, int samples) {
158 RETURN_VALUE_IF(!mContext, (IEffect::Status{EX_NULL_POINTER, 0, 0}), "nullContext");
159 return mContext->process(in, out, samples);
160}
161
162SpatializerSwContext::SpatializerSwContext(int statusDepth, const Parameter::Common& common)
163 : EffectContext(statusDepth, common) {
164 LOG(DEBUG) << __func__;
165}
166
167SpatializerSwContext::~SpatializerSwContext() {
168 LOG(DEBUG) << __func__;
169}
170
171template <typename TAG>
172std::optional<Spatializer> SpatializerSwContext::getParam(TAG tag) {
173 if (mParamsMap.find(tag) != mParamsMap.end()) {
174 return mParamsMap.at(tag);
175 }
176 return std::nullopt;
177}
178
179template <typename TAG>
180ndk::ScopedAStatus SpatializerSwContext::setParam(TAG tag, Spatializer spatializer) {
181 mParamsMap[tag] = spatializer;
182 return ndk::ScopedAStatus::ok();
183}
184
185IEffect::Status SpatializerSwContext::process(float* in, float* out, int samples) {
186 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
187 IEffect::Status status = {EX_ILLEGAL_ARGUMENT, 0, 0};
188
189 const auto inputChannelCount = getChannelCount(mCommon.input.base.channelMask);
190 const auto outputChannelCount = getChannelCount(mCommon.output.base.channelMask);
191 if (outputChannelCount < 2 || inputChannelCount < outputChannelCount) {
192 LOG(ERROR) << __func__ << " invalid channel count, in: " << inputChannelCount
193 << " out: " << outputChannelCount;
194 return status;
195 }
196
197 int iFrames = samples / inputChannelCount;
198 for (int i = 0; i < iFrames; i++) {
199 std::memcpy(out, in, outputChannelCount);
200 in += inputChannelCount;
201 out += outputChannelCount;
202 }
203 return {STATUS_OK, static_cast<int32_t>(iFrames * inputChannelCount),
204 static_cast<int32_t>(iFrames * outputChannelCount)};
205}
206
207} // namespace aidl::android::hardware::audio::effect