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 | |
| 17 | #include <cstddef> |
| 18 | #define LOG_TAG "AHAL_ReverbSw" |
| 19 | #include <Utils.h> |
| 20 | #include <algorithm> |
| 21 | #include <unordered_set> |
| 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | #include <fmq/AidlMessageQueue.h> |
| 25 | |
| 26 | #include "ReverbSw.h" |
| 27 | |
| 28 | using aidl::android::hardware::audio::effect::IEffect; |
| 29 | using aidl::android::hardware::audio::effect::ReverbSw; |
| 30 | using aidl::android::hardware::audio::effect::ReverbSwImplUUID; |
| 31 | using aidl::android::hardware::audio::effect::State; |
| 32 | using aidl::android::media::audio::common::AudioUuid; |
| 33 | |
| 34 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 35 | std::shared_ptr<IEffect>* instanceSpp) { |
| 36 | if (!in_impl_uuid || *in_impl_uuid != ReverbSwImplUUID) { |
| 37 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 38 | return EX_ILLEGAL_ARGUMENT; |
| 39 | } |
| 40 | if (instanceSpp) { |
| 41 | *instanceSpp = ndk::SharedRefBase::make<ReverbSw>(); |
| 42 | LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created"; |
| 43 | return EX_NONE; |
| 44 | } else { |
| 45 | LOG(ERROR) << __func__ << " invalid input parameter!"; |
| 46 | return EX_ILLEGAL_ARGUMENT; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | extern "C" binder_exception_t destroyEffect(const std::shared_ptr<IEffect>& instanceSp) { |
| 51 | if (!instanceSp) { |
| 52 | return EX_NONE; |
| 53 | } |
| 54 | State state; |
| 55 | ndk::ScopedAStatus status = instanceSp->getState(&state); |
| 56 | if (!status.isOk() || State::INIT != state) { |
| 57 | LOG(ERROR) << __func__ << " instance " << instanceSp.get() |
| 58 | << " in state: " << toString(state) << ", status: " << status.getDescription(); |
| 59 | return EX_ILLEGAL_STATE; |
| 60 | } |
| 61 | LOG(DEBUG) << __func__ << " instance " << instanceSp.get() << " destroyed"; |
| 62 | return EX_NONE; |
| 63 | } |
| 64 | |
| 65 | namespace aidl::android::hardware::audio::effect { |
| 66 | |
| 67 | ndk::ScopedAStatus ReverbSw::getDescriptor(Descriptor* _aidl_return) { |
| 68 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 69 | *_aidl_return = kDescriptor; |
| 70 | return ndk::ScopedAStatus::ok(); |
| 71 | } |
| 72 | |
| 73 | ndk::ScopedAStatus ReverbSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 74 | RETURN_IF(Parameter::Specific::reverb != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 75 | "EffectNotSupported"); |
| 76 | std::lock_guard lg(mMutex); |
| 77 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 78 | |
| 79 | mSpecificParam = specific.get<Parameter::Specific::reverb>(); |
| 80 | LOG(DEBUG) << __func__ << " success with: " << specific.toString(); |
| 81 | return ndk::ScopedAStatus::ok(); |
| 82 | } |
| 83 | |
| 84 | ndk::ScopedAStatus ReverbSw::getParameterSpecific(const Parameter::Id& id, |
| 85 | Parameter::Specific* specific) { |
| 86 | auto tag = id.getTag(); |
| 87 | RETURN_IF(Parameter::Id::reverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 88 | specific->set<Parameter::Specific::reverb>(mSpecificParam); |
| 89 | return ndk::ScopedAStatus::ok(); |
| 90 | } |
| 91 | |
| 92 | std::shared_ptr<EffectContext> ReverbSw::createContext(const Parameter::Common& common) { |
| 93 | if (mContext) { |
| 94 | LOG(DEBUG) << __func__ << " context already exist"; |
| 95 | return mContext; |
| 96 | } |
| 97 | mContext = std::make_shared<ReverbSwContext>(1 /* statusFmqDepth */, common); |
| 98 | return mContext; |
| 99 | } |
| 100 | |
| 101 | RetCode ReverbSw::releaseContext() { |
| 102 | if (mContext) { |
| 103 | mContext.reset(); |
| 104 | } |
| 105 | return RetCode::SUCCESS; |
| 106 | } |
| 107 | |
| 108 | // Processing method running in EffectWorker thread. |
| 109 | IEffect::Status ReverbSw::effectProcessImpl(float* in, float* out, int process) { |
| 110 | // TODO: get data buffer and process. |
| 111 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " process " << process; |
| 112 | for (int i = 0; i < process; i++) { |
| 113 | *out++ = *in++; |
| 114 | } |
| 115 | return {STATUS_OK, process, process}; |
| 116 | } |
| 117 | |
| 118 | } // namespace aidl::android::hardware::audio::effect |