Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 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 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 17 | #define LOG_TAG "AHAL_VisualizerSw" |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 18 | |
| 19 | #include <android-base/logging.h> |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 20 | #include <system/audio_effects/effect_uuid.h> |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 21 | |
| 22 | #include "VisualizerSw.h" |
| 23 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 24 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 25 | using aidl::android::hardware::audio::effect::getEffectImplUuidVisualizerSw; |
| 26 | using aidl::android::hardware::audio::effect::getEffectTypeUuidVisualizer; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 27 | using aidl::android::hardware::audio::effect::IEffect; |
| 28 | using aidl::android::hardware::audio::effect::State; |
| 29 | using aidl::android::hardware::audio::effect::VisualizerSw; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 30 | using aidl::android::media::audio::common::AudioUuid; |
| 31 | |
| 32 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 33 | std::shared_ptr<IEffect>* instanceSpp) { |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 34 | if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidVisualizerSw()) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 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 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 48 | extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 49 | if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidVisualizerSw()) { |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 50 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 51 | return EX_ILLEGAL_ARGUMENT; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 52 | } |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 53 | *_aidl_return = VisualizerSw::kDescriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 54 | return EX_NONE; |
| 55 | } |
| 56 | |
| 57 | namespace aidl::android::hardware::audio::effect { |
| 58 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 59 | const std::string VisualizerSw::kEffectName = "VisualizerSw"; |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 60 | |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 61 | /* capabilities */ |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 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)}; |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 69 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 70 | const Descriptor VisualizerSw::kDescriptor = { |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 71 | .common = {.id = {.type = getEffectTypeUuidVisualizer(), |
| 72 | .uuid = getEffectImplUuidVisualizerSw(), |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 73 | .proxy = std::nullopt}, |
| 74 | .flags = {.type = Flags::Type::INSERT, |
| 75 | .insert = Flags::Insert::FIRST, |
| 76 | .volume = Flags::Volume::CTRL}, |
| 77 | .name = VisualizerSw::kEffectName, |
| 78 | .implementor = "The Android Open Source Project"}, |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 79 | .capability = VisualizerSw::kCapability}; |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 80 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 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 | |
| 87 | ndk::ScopedAStatus VisualizerSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 88 | RETURN_IF(Parameter::Specific::visualizer != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 89 | "EffectNotSupported"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 90 | |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 91 | auto& vsParam = specific.get<Parameter::Specific::visualizer>(); |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 92 | RETURN_IF(!inRange(vsParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange"); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 93 | auto tag = vsParam.getTag(); |
| 94 | |
| 95 | switch (tag) { |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 96 | case Visualizer::captureSamples: { |
| 97 | RETURN_IF(mContext->setVsCaptureSize(vsParam.get<Visualizer::captureSamples>()) != |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 98 | RetCode::SUCCESS, |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 99 | EX_ILLEGAL_ARGUMENT, "setCaptureSizeFailed"); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 100 | return ndk::ScopedAStatus::ok(); |
| 101 | } |
| 102 | case Visualizer::scalingMode: { |
| 103 | RETURN_IF(mContext->setVsScalingMode(vsParam.get<Visualizer::scalingMode>()) != |
| 104 | RetCode::SUCCESS, |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 105 | EX_ILLEGAL_ARGUMENT, "setScalingModeFailed"); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 106 | return ndk::ScopedAStatus::ok(); |
| 107 | } |
| 108 | case Visualizer::measurementMode: { |
| 109 | RETURN_IF(mContext->setVsMeasurementMode(vsParam.get<Visualizer::measurementMode>()) != |
| 110 | RetCode::SUCCESS, |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 111 | EX_ILLEGAL_ARGUMENT, "setMeasurementModeFailed"); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 112 | return ndk::ScopedAStatus::ok(); |
| 113 | } |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 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(); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 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 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 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"); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 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); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 137 | default: |
| 138 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 139 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 140 | "VisualizerTagNotSupported"); |
| 141 | } |
| 142 | } |
| 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) { |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 149 | case Visualizer::captureSamples: { |
| 150 | vsParam.set<Visualizer::captureSamples>(mContext->getVsCaptureSize()); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 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 | } |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 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 | } |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 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 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 183 | std::shared_ptr<EffectContext> VisualizerSw::createContext(const Parameter::Common& common) { |
| 184 | if (mContext) { |
| 185 | LOG(DEBUG) << __func__ << " context already exist"; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 186 | } else { |
| 187 | mContext = std::make_shared<VisualizerSwContext>(1 /* statusFmqDepth */, common); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 188 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 189 | |
| 190 | return mContext; |
| 191 | } |
| 192 | |
| 193 | std::shared_ptr<EffectContext> VisualizerSw::getContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 194 | return mContext; |
| 195 | } |
| 196 | |
| 197 | RetCode VisualizerSw::releaseContext() { |
| 198 | if (mContext) { |
| 199 | mContext.reset(); |
| 200 | } |
| 201 | return RetCode::SUCCESS; |
| 202 | } |
| 203 | |
| 204 | // Processing method running in EffectWorker thread. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 205 | IEffect::Status VisualizerSw::effectProcessImpl(float* in, float* out, int samples) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 206 | // TODO: get data buffer and process. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 207 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 208 | for (int i = 0; i < samples; i++) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 209 | *out++ = *in++; |
| 210 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 211 | return {STATUS_OK, samples, samples}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Sham Rathod | dc7bcc8 | 2022-12-27 18:16:10 +0530 | [diff] [blame] | 214 | RetCode VisualizerSwContext::setVsCaptureSize(int captureSize) { |
Sham Rathod | dc7bcc8 | 2022-12-27 18:16:10 +0530 | [diff] [blame] | 215 | mCaptureSize = captureSize; |
| 216 | return RetCode::SUCCESS; |
| 217 | } |
| 218 | |
| 219 | RetCode VisualizerSwContext::setVsScalingMode(Visualizer::ScalingMode scalingMode) { |
Sham Rathod | dc7bcc8 | 2022-12-27 18:16:10 +0530 | [diff] [blame] | 220 | mScalingMode = scalingMode; |
| 221 | return RetCode::SUCCESS; |
| 222 | } |
| 223 | |
| 224 | RetCode VisualizerSwContext::setVsMeasurementMode(Visualizer::MeasurementMode measurementMode) { |
Sham Rathod | dc7bcc8 | 2022-12-27 18:16:10 +0530 | [diff] [blame] | 225 | mMeasurementMode = measurementMode; |
| 226 | return RetCode::SUCCESS; |
| 227 | } |
| 228 | |
| 229 | RetCode VisualizerSwContext::setVsLatency(int latency) { |
Sham Rathod | dc7bcc8 | 2022-12-27 18:16:10 +0530 | [diff] [blame] | 230 | mLatency = latency; |
| 231 | return RetCode::SUCCESS; |
| 232 | } |
| 233 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 234 | } // namespace aidl::android::hardware::audio::effect |