blob: ef86ddb2a1d6405989700313d6395122eea6e6ed [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) {
Shunkai Yaoc7f5c2d2024-02-13 01:59:55 +0000111 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
112
Shunkai Yaof60fc372023-12-12 17:48:18 +0000113 auto tag = id.getTag();
114 RETURN_IF(Parameter::Id::spatializerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
115 auto spatializerId = id.get<Parameter::Id::spatializerTag>();
116 auto spatializerTag = spatializerId.getTag();
117 switch (spatializerTag) {
118 case Spatializer::Id::commonTag: {
119 auto specificTag = spatializerId.get<Spatializer::Id::commonTag>();
120 std::optional<Spatializer> param = mContext->getParam(specificTag);
121 if (!param.has_value()) {
122 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
123 EX_ILLEGAL_ARGUMENT, "SpatializerTagNotSupported");
124 }
125 specific->set<Parameter::Specific::spatializer>(param.value());
126 break;
127 }
128 default: {
129 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
130 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
131 "SpatializerTagNotSupported");
132 }
133 }
134 return ndk::ScopedAStatus::ok();
135}
136
137std::shared_ptr<EffectContext> SpatializerSw::createContext(const Parameter::Common& common) {
138 if (mContext) {
139 LOG(DEBUG) << __func__ << " context already exist";
140 } else {
141 mContext = std::make_shared<SpatializerSwContext>(1 /* statusFmqDepth */, common);
142 }
143 return mContext;
144}
145
Shunkai Yaof60fc372023-12-12 17:48:18 +0000146RetCode SpatializerSw::releaseContext() {
147 if (mContext) {
148 mContext.reset();
149 }
150 return RetCode::SUCCESS;
151}
152
153SpatializerSw::~SpatializerSw() {
154 cleanUp();
155 LOG(DEBUG) << __func__;
156}
157
158// Processing method running in EffectWorker thread.
159IEffect::Status SpatializerSw::effectProcessImpl(float* in, float* out, int samples) {
160 RETURN_VALUE_IF(!mContext, (IEffect::Status{EX_NULL_POINTER, 0, 0}), "nullContext");
161 return mContext->process(in, out, samples);
162}
163
164SpatializerSwContext::SpatializerSwContext(int statusDepth, const Parameter::Common& common)
165 : EffectContext(statusDepth, common) {
166 LOG(DEBUG) << __func__;
167}
168
169SpatializerSwContext::~SpatializerSwContext() {
170 LOG(DEBUG) << __func__;
171}
172
173template <typename TAG>
174std::optional<Spatializer> SpatializerSwContext::getParam(TAG tag) {
175 if (mParamsMap.find(tag) != mParamsMap.end()) {
176 return mParamsMap.at(tag);
177 }
178 return std::nullopt;
179}
180
181template <typename TAG>
182ndk::ScopedAStatus SpatializerSwContext::setParam(TAG tag, Spatializer spatializer) {
183 mParamsMap[tag] = spatializer;
184 return ndk::ScopedAStatus::ok();
185}
186
187IEffect::Status SpatializerSwContext::process(float* in, float* out, int samples) {
188 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
189 IEffect::Status status = {EX_ILLEGAL_ARGUMENT, 0, 0};
190
191 const auto inputChannelCount = getChannelCount(mCommon.input.base.channelMask);
192 const auto outputChannelCount = getChannelCount(mCommon.output.base.channelMask);
193 if (outputChannelCount < 2 || inputChannelCount < outputChannelCount) {
194 LOG(ERROR) << __func__ << " invalid channel count, in: " << inputChannelCount
195 << " out: " << outputChannelCount;
196 return status;
197 }
198
199 int iFrames = samples / inputChannelCount;
200 for (int i = 0; i < iFrames; i++) {
201 std::memcpy(out, in, outputChannelCount);
202 in += inputChannelCount;
203 out += outputChannelCount;
204 }
205 return {STATUS_OK, static_cast<int32_t>(iFrames * inputChannelCount),
206 static_cast<int32_t>(iFrames * outputChannelCount)};
207}
208
209} // namespace aidl::android::hardware::audio::effect