1 /*
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 #define LOG_TAG "AHAL_VisualizerSw"
18 
19 #include <android-base/logging.h>
20 #include <system/audio_effects/effect_uuid.h>
21 
22 #include "VisualizerSw.h"
23 
24 using aidl::android::hardware::audio::effect::Descriptor;
25 using aidl::android::hardware::audio::effect::getEffectImplUuidVisualizerSw;
26 using aidl::android::hardware::audio::effect::getEffectTypeUuidVisualizer;
27 using aidl::android::hardware::audio::effect::IEffect;
28 using aidl::android::hardware::audio::effect::State;
29 using aidl::android::hardware::audio::effect::VisualizerSw;
30 using aidl::android::media::audio::common::AudioUuid;
31 
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)32 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
33                                            std::shared_ptr<IEffect>* instanceSpp) {
34     if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidVisualizerSw()) {
35         LOG(ERROR) << __func__ << "uuid not supported";
36         return EX_ILLEGAL_ARGUMENT;
37     }
38     if (instanceSpp) {
39         *instanceSpp = ndk::SharedRefBase::make<VisualizerSw>();
40         LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
41         return EX_NONE;
42     } else {
43         LOG(ERROR) << __func__ << " invalid input parameter!";
44         return EX_ILLEGAL_ARGUMENT;
45     }
46 }
47 
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)48 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
49     if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidVisualizerSw()) {
50         LOG(ERROR) << __func__ << "uuid not supported";
51         return EX_ILLEGAL_ARGUMENT;
52     }
53     *_aidl_return = VisualizerSw::kDescriptor;
54     return EX_NONE;
55 }
56 
57 namespace aidl::android::hardware::audio::effect {
58 
59 const std::string VisualizerSw::kEffectName = "VisualizerSw";
60 
61 /* capabilities */
62 const std::vector<Range::VisualizerRange> VisualizerSw::kRanges = {
63         MAKE_RANGE(Visualizer, latencyMs, 0, VisualizerSwContext::kMaxLatencyMs),
64         MAKE_RANGE(Visualizer, captureSamples, VisualizerSwContext::kMinCaptureSize,
65                    VisualizerSwContext::kMaxCaptureSize)};
66 
67 const Capability VisualizerSw::kCapability = {
68         .range = Range::make<Range::visualizer>(VisualizerSw::kRanges)};
69 
70 const Descriptor VisualizerSw::kDescriptor = {
71         .common = {.id = {.type = getEffectTypeUuidVisualizer(),
72                           .uuid = getEffectImplUuidVisualizerSw(),
73                           .proxy = std::nullopt},
74                    .flags = {.type = Flags::Type::INSERT,
75                              .insert = Flags::Insert::FIRST,
76                              .volume = Flags::Volume::NONE},
77                    .name = VisualizerSw::kEffectName,
78                    .implementor = "The Android Open Source Project"},
79         .capability = VisualizerSw::kCapability};
80 
getDescriptor(Descriptor * _aidl_return)81 ndk::ScopedAStatus VisualizerSw::getDescriptor(Descriptor* _aidl_return) {
82     LOG(DEBUG) << __func__ << kDescriptor.toString();
83     *_aidl_return = kDescriptor;
84     return ndk::ScopedAStatus::ok();
85 }
86 
setParameterSpecific(const Parameter::Specific & specific)87 ndk::ScopedAStatus VisualizerSw::setParameterSpecific(const Parameter::Specific& specific) {
88     RETURN_IF(Parameter::Specific::visualizer != specific.getTag(), EX_ILLEGAL_ARGUMENT,
89               "EffectNotSupported");
90 
91     auto& vsParam = specific.get<Parameter::Specific::visualizer>();
92     RETURN_IF(!inRange(vsParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
93     auto tag = vsParam.getTag();
94 
95     switch (tag) {
96         case Visualizer::captureSamples: {
97             RETURN_IF(mContext->setVsCaptureSize(vsParam.get<Visualizer::captureSamples>()) !=
98                               RetCode::SUCCESS,
99                       EX_ILLEGAL_ARGUMENT, "setCaptureSizeFailed");
100             return ndk::ScopedAStatus::ok();
101         }
102         case Visualizer::scalingMode: {
103             RETURN_IF(mContext->setVsScalingMode(vsParam.get<Visualizer::scalingMode>()) !=
104                               RetCode::SUCCESS,
105                       EX_ILLEGAL_ARGUMENT, "setScalingModeFailed");
106             return ndk::ScopedAStatus::ok();
107         }
108         case Visualizer::measurementMode: {
109             RETURN_IF(mContext->setVsMeasurementMode(vsParam.get<Visualizer::measurementMode>()) !=
110                               RetCode::SUCCESS,
111                       EX_ILLEGAL_ARGUMENT, "setMeasurementModeFailed");
112             return ndk::ScopedAStatus::ok();
113         }
114         case Visualizer::latencyMs: {
115             RETURN_IF(mContext->setVsLatency(vsParam.get<Visualizer::latencyMs>()) !=
116                               RetCode::SUCCESS,
117                       EX_ILLEGAL_ARGUMENT, "setLatencyFailed");
118             return ndk::ScopedAStatus::ok();
119         }
120         default: {
121             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
122             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
123                                                                     "VisualizerTagNotSupported");
124         }
125     }
126 }
127 
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)128 ndk::ScopedAStatus VisualizerSw::getParameterSpecific(const Parameter::Id& id,
129                                                       Parameter::Specific* specific) {
130     auto tag = id.getTag();
131     RETURN_IF(Parameter::Id::visualizerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
132     auto vsId = id.get<Parameter::Id::visualizerTag>();
133     auto vsIdTag = vsId.getTag();
134     switch (vsIdTag) {
135         case Visualizer::Id::commonTag:
136             return getParameterVisualizer(vsId.get<Visualizer::Id::commonTag>(), specific);
137         default:
138             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
139             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
140                                                                     "VisualizerTagNotSupported");
141     }
142 }
getParameterVisualizer(const Visualizer::Tag & tag,Parameter::Specific * specific)143 ndk::ScopedAStatus VisualizerSw::getParameterVisualizer(const Visualizer::Tag& tag,
144                                                         Parameter::Specific* specific) {
145     RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
146 
147     Visualizer vsParam;
148     switch (tag) {
149         case Visualizer::captureSamples: {
150             vsParam.set<Visualizer::captureSamples>(mContext->getVsCaptureSize());
151             break;
152         }
153         case Visualizer::scalingMode: {
154             vsParam.set<Visualizer::scalingMode>(mContext->getVsScalingMode());
155             break;
156         }
157         case Visualizer::measurementMode: {
158             vsParam.set<Visualizer::measurementMode>(mContext->getVsMeasurementMode());
159             break;
160         }
161         case Visualizer::measurement: {
162             vsParam.set<Visualizer::measurement>(mContext->getVsMeasurement());
163             break;
164         }
165         case Visualizer::captureSampleBuffer: {
166             vsParam.set<Visualizer::captureSampleBuffer>(mContext->getVsCaptureSampleBuffer());
167             break;
168         }
169         case Visualizer::latencyMs: {
170             vsParam.set<Visualizer::latencyMs>(mContext->getVsLatency());
171             break;
172         }
173         default: {
174             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
175             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
176                                                                     "VisualizerTagNotSupported");
177         }
178     }
179     specific->set<Parameter::Specific::visualizer>(vsParam);
180     return ndk::ScopedAStatus::ok();
181 }
182 
createContext(const Parameter::Common & common)183 std::shared_ptr<EffectContext> VisualizerSw::createContext(const Parameter::Common& common) {
184     if (mContext) {
185         LOG(DEBUG) << __func__ << " context already exist";
186     } else {
187         mContext = std::make_shared<VisualizerSwContext>(1 /* statusFmqDepth */, common);
188     }
189 
190     return mContext;
191 }
192 
releaseContext()193 RetCode VisualizerSw::releaseContext() {
194     if (mContext) {
195         mContext.reset();
196     }
197     return RetCode::SUCCESS;
198 }
199 
200 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)201 IEffect::Status VisualizerSw::effectProcessImpl(float* in, float* out, int samples) {
202     // TODO: get data buffer and process.
203     LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
204     for (int i = 0; i < samples; i++) {
205         *out++ = *in++;
206     }
207     return {STATUS_OK, samples, samples};
208 }
209 
setVsCaptureSize(int captureSize)210 RetCode VisualizerSwContext::setVsCaptureSize(int captureSize) {
211     mCaptureSize = captureSize;
212     return RetCode::SUCCESS;
213 }
214 
setVsScalingMode(Visualizer::ScalingMode scalingMode)215 RetCode VisualizerSwContext::setVsScalingMode(Visualizer::ScalingMode scalingMode) {
216     mScalingMode = scalingMode;
217     return RetCode::SUCCESS;
218 }
219 
setVsMeasurementMode(Visualizer::MeasurementMode measurementMode)220 RetCode VisualizerSwContext::setVsMeasurementMode(Visualizer::MeasurementMode measurementMode) {
221     mMeasurementMode = measurementMode;
222     return RetCode::SUCCESS;
223 }
224 
setVsLatency(int latency)225 RetCode VisualizerSwContext::setVsLatency(int latency) {
226     mLatency = latency;
227     return RetCode::SUCCESS;
228 }
229 
230 }  // namespace aidl::android::hardware::audio::effect
231