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 | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 20 | |
| 21 | #include "VisualizerSw.h" |
| 22 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 23 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 24 | using aidl::android::hardware::audio::effect::IEffect; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 25 | using aidl::android::hardware::audio::effect::kVisualizerSwImplUUID; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 26 | using aidl::android::hardware::audio::effect::State; |
| 27 | using aidl::android::hardware::audio::effect::VisualizerSw; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 28 | using aidl::android::media::audio::common::AudioUuid; |
| 29 | |
| 30 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 31 | std::shared_ptr<IEffect>* instanceSpp) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 32 | if (!in_impl_uuid || *in_impl_uuid != kVisualizerSwImplUUID) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 33 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 34 | return EX_ILLEGAL_ARGUMENT; |
| 35 | } |
| 36 | if (instanceSpp) { |
| 37 | *instanceSpp = ndk::SharedRefBase::make<VisualizerSw>(); |
| 38 | LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created"; |
| 39 | return EX_NONE; |
| 40 | } else { |
| 41 | LOG(ERROR) << __func__ << " invalid input parameter!"; |
| 42 | return EX_ILLEGAL_ARGUMENT; |
| 43 | } |
| 44 | } |
| 45 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 46 | extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
| 47 | if (!in_impl_uuid || *in_impl_uuid != kVisualizerSwImplUUID) { |
| 48 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 49 | return EX_ILLEGAL_ARGUMENT; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 50 | } |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 51 | *_aidl_return = VisualizerSw::kDescriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 52 | return EX_NONE; |
| 53 | } |
| 54 | |
| 55 | namespace aidl::android::hardware::audio::effect { |
| 56 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 57 | const std::string VisualizerSw::kEffectName = "VisualizerSw"; |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 58 | /* capabilities */ |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 59 | const Visualizer::CaptureSamplesRange VisualizerSwContext::kCaptureSamplesRange = { |
| 60 | VisualizerSwContext::kMinCaptureSize, VisualizerSwContext::kMaxCaptureSize}; |
| 61 | const Visualizer::Capability VisualizerSw::kCapability = { |
| 62 | .maxLatencyMs = VisualizerSwContext::kMaxLatencyMs, |
| 63 | .captureSampleRange = VisualizerSwContext::kCaptureSamplesRange}; |
| 64 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 65 | const Descriptor VisualizerSw::kDescriptor = { |
| 66 | .common = {.id = {.type = kVisualizerTypeUUID, |
| 67 | .uuid = kVisualizerSwImplUUID, |
| 68 | .proxy = std::nullopt}, |
| 69 | .flags = {.type = Flags::Type::INSERT, |
| 70 | .insert = Flags::Insert::FIRST, |
| 71 | .volume = Flags::Volume::CTRL}, |
| 72 | .name = VisualizerSw::kEffectName, |
| 73 | .implementor = "The Android Open Source Project"}, |
| 74 | .capability = Capability::make<Capability::visualizer>(VisualizerSw::kCapability)}; |
| 75 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 76 | ndk::ScopedAStatus VisualizerSw::getDescriptor(Descriptor* _aidl_return) { |
| 77 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 78 | *_aidl_return = kDescriptor; |
| 79 | return ndk::ScopedAStatus::ok(); |
| 80 | } |
| 81 | |
| 82 | ndk::ScopedAStatus VisualizerSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 83 | RETURN_IF(Parameter::Specific::visualizer != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 84 | "EffectNotSupported"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 85 | |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 86 | auto& vsParam = specific.get<Parameter::Specific::visualizer>(); |
| 87 | auto tag = vsParam.getTag(); |
| 88 | |
| 89 | switch (tag) { |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 90 | case Visualizer::captureSamples: { |
| 91 | RETURN_IF(mContext->setVsCaptureSize(vsParam.get<Visualizer::captureSamples>()) != |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 92 | RetCode::SUCCESS, |
| 93 | EX_ILLEGAL_ARGUMENT, "captureSizeNotSupported"); |
| 94 | return ndk::ScopedAStatus::ok(); |
| 95 | } |
| 96 | case Visualizer::scalingMode: { |
| 97 | RETURN_IF(mContext->setVsScalingMode(vsParam.get<Visualizer::scalingMode>()) != |
| 98 | RetCode::SUCCESS, |
| 99 | EX_ILLEGAL_ARGUMENT, "scalingModeNotSupported"); |
| 100 | return ndk::ScopedAStatus::ok(); |
| 101 | } |
| 102 | case Visualizer::measurementMode: { |
| 103 | RETURN_IF(mContext->setVsMeasurementMode(vsParam.get<Visualizer::measurementMode>()) != |
| 104 | RetCode::SUCCESS, |
| 105 | EX_ILLEGAL_ARGUMENT, "measurementModeNotSupported"); |
| 106 | return ndk::ScopedAStatus::ok(); |
| 107 | } |
| 108 | case Visualizer::setOnlyParameters: { |
| 109 | return setSetOnlyParameterVisualizer(vsParam.get<Visualizer::setOnlyParameters>()); |
| 110 | } |
| 111 | case Visualizer::getOnlyParameters: { |
| 112 | LOG(ERROR) << __func__ << " unsupported settable getOnlyParam"; |
| 113 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 114 | EX_ILLEGAL_ARGUMENT, "SetofGetOnlyParamsNotSupported"); |
| 115 | } |
| 116 | default: { |
| 117 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 118 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 119 | "VisualizerTagNotSupported"); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | ndk::ScopedAStatus VisualizerSw::setSetOnlyParameterVisualizer( |
| 125 | Visualizer::SetOnlyParameters setOnlyParam) { |
| 126 | auto tag = setOnlyParam.getTag(); |
| 127 | RETURN_IF(Visualizer::SetOnlyParameters::latencyMs != tag, EX_ILLEGAL_ARGUMENT, |
| 128 | "SetOnlyParametersTagNotSupported"); |
| 129 | RETURN_IF( |
| 130 | mContext->setVsLatency(setOnlyParam.get<Visualizer::SetOnlyParameters::latencyMs>()) != |
| 131 | RetCode::SUCCESS, |
| 132 | EX_ILLEGAL_ARGUMENT, "latencyNotSupported"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 133 | return ndk::ScopedAStatus::ok(); |
| 134 | } |
| 135 | |
| 136 | ndk::ScopedAStatus VisualizerSw::getParameterSpecific(const Parameter::Id& id, |
| 137 | Parameter::Specific* specific) { |
| 138 | auto tag = id.getTag(); |
| 139 | RETURN_IF(Parameter::Id::visualizerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 140 | auto vsId = id.get<Parameter::Id::visualizerTag>(); |
| 141 | auto vsIdTag = vsId.getTag(); |
| 142 | switch (vsIdTag) { |
| 143 | case Visualizer::Id::commonTag: |
| 144 | return getParameterVisualizer(vsId.get<Visualizer::Id::commonTag>(), specific); |
| 145 | case Visualizer::Id::getOnlyParamTag: |
| 146 | return getGetOnlyParameterVisualizer(vsId.get<Visualizer::Id::getOnlyParamTag>(), |
| 147 | specific); |
| 148 | case Visualizer::Id::setOnlyParamTag: { |
| 149 | LOG(ERROR) << __func__ << " unsupported gettable setOnlyParam"; |
| 150 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 151 | EX_ILLEGAL_ARGUMENT, "GetofSetOnlyParamsNotSupported"); |
| 152 | } |
| 153 | default: |
| 154 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 155 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 156 | "VisualizerTagNotSupported"); |
| 157 | } |
| 158 | } |
| 159 | ndk::ScopedAStatus VisualizerSw::getParameterVisualizer(const Visualizer::Tag& tag, |
| 160 | Parameter::Specific* specific) { |
| 161 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 162 | |
| 163 | Visualizer vsParam; |
| 164 | switch (tag) { |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 165 | case Visualizer::captureSamples: { |
| 166 | vsParam.set<Visualizer::captureSamples>(mContext->getVsCaptureSize()); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | case Visualizer::scalingMode: { |
| 170 | vsParam.set<Visualizer::scalingMode>(mContext->getVsScalingMode()); |
| 171 | break; |
| 172 | } |
| 173 | case Visualizer::measurementMode: { |
| 174 | vsParam.set<Visualizer::measurementMode>(mContext->getVsMeasurementMode()); |
| 175 | break; |
| 176 | } |
| 177 | default: { |
| 178 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 179 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 180 | "VisualizerTagNotSupported"); |
| 181 | } |
| 182 | } |
| 183 | specific->set<Parameter::Specific::visualizer>(vsParam); |
| 184 | return ndk::ScopedAStatus::ok(); |
| 185 | } |
| 186 | |
| 187 | ndk::ScopedAStatus VisualizerSw::getGetOnlyParameterVisualizer( |
| 188 | const Visualizer::GetOnlyParameters::Tag& tag, Parameter::Specific* specific) { |
| 189 | Visualizer::GetOnlyParameters getOnlyParam; |
| 190 | switch (tag) { |
| 191 | case Visualizer::GetOnlyParameters::measurement: { |
| 192 | getOnlyParam.set<Visualizer::GetOnlyParameters::measurement>( |
| 193 | mContext->getVsMeasurement()); |
| 194 | break; |
| 195 | } |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 196 | case Visualizer::GetOnlyParameters::captureSampleBuffer: { |
| 197 | getOnlyParam.set<Visualizer::GetOnlyParameters::captureSampleBuffer>( |
| 198 | mContext->getVsCaptureSampleBuffer()); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | default: { |
| 202 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 203 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 204 | EX_ILLEGAL_ARGUMENT, "GetOnlyParameterTagNotSupported"); |
| 205 | } |
| 206 | } |
| 207 | Visualizer vsParam; |
| 208 | vsParam.set<Visualizer::getOnlyParameters>(getOnlyParam); |
| 209 | specific->set<Parameter::Specific::visualizer>(vsParam); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 210 | return ndk::ScopedAStatus::ok(); |
| 211 | } |
| 212 | |
| 213 | std::shared_ptr<EffectContext> VisualizerSw::createContext(const Parameter::Common& common) { |
| 214 | if (mContext) { |
| 215 | LOG(DEBUG) << __func__ << " context already exist"; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 216 | } else { |
| 217 | mContext = std::make_shared<VisualizerSwContext>(1 /* statusFmqDepth */, common); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 218 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 219 | |
| 220 | return mContext; |
| 221 | } |
| 222 | |
| 223 | std::shared_ptr<EffectContext> VisualizerSw::getContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 224 | return mContext; |
| 225 | } |
| 226 | |
| 227 | RetCode VisualizerSw::releaseContext() { |
| 228 | if (mContext) { |
| 229 | mContext.reset(); |
| 230 | } |
| 231 | return RetCode::SUCCESS; |
| 232 | } |
| 233 | |
| 234 | // Processing method running in EffectWorker thread. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 235 | IEffect::Status VisualizerSw::effectProcessImpl(float* in, float* out, int samples) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 236 | // TODO: get data buffer and process. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 237 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 238 | for (int i = 0; i < samples; i++) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 239 | *out++ = *in++; |
| 240 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 241 | return {STATUS_OK, samples, samples}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Sham Rathod | dc7bcc8 | 2022-12-27 18:16:10 +0530 | [diff] [blame] | 244 | RetCode VisualizerSwContext::setVsCaptureSize(int captureSize) { |
| 245 | if (captureSize < VisualizerSw::kCapability.captureSampleRange.min || |
| 246 | captureSize > VisualizerSw::kCapability.captureSampleRange.max) { |
| 247 | LOG(ERROR) << __func__ << " invalid captureSize " << captureSize; |
| 248 | return RetCode::ERROR_ILLEGAL_PARAMETER; |
| 249 | } |
| 250 | // TODO : Add implementation to apply new captureSize |
| 251 | mCaptureSize = captureSize; |
| 252 | return RetCode::SUCCESS; |
| 253 | } |
| 254 | |
| 255 | RetCode VisualizerSwContext::setVsScalingMode(Visualizer::ScalingMode scalingMode) { |
| 256 | // TODO : Add implementation to apply new scalingMode |
| 257 | mScalingMode = scalingMode; |
| 258 | return RetCode::SUCCESS; |
| 259 | } |
| 260 | |
| 261 | RetCode VisualizerSwContext::setVsMeasurementMode(Visualizer::MeasurementMode measurementMode) { |
| 262 | // TODO : Add implementation to apply new measurementMode |
| 263 | mMeasurementMode = measurementMode; |
| 264 | return RetCode::SUCCESS; |
| 265 | } |
| 266 | |
| 267 | RetCode VisualizerSwContext::setVsLatency(int latency) { |
| 268 | if (latency < 0 || latency > VisualizerSw::kCapability.maxLatencyMs) { |
| 269 | LOG(ERROR) << __func__ << " invalid latency " << latency; |
| 270 | return RetCode::ERROR_ILLEGAL_PARAMETER; |
| 271 | } |
| 272 | // TODO : Add implementation to modify latency |
| 273 | mLatency = latency; |
| 274 | return RetCode::SUCCESS; |
| 275 | } |
| 276 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 277 | } // namespace aidl::android::hardware::audio::effect |