blob: cb395b729a4b2e871bd4ff09fb16d9e65ae40822 [file] [log] [blame]
Shunkai Yao6afc8552022-10-26 22:47:20 +00001/*
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#pragma once
18#include <aidl/android/hardware/audio/effect/BnEffect.h>
19#include <fmq/AidlMessageQueue.h>
20#include <cstdlib>
21#include <memory>
22#include <mutex>
23
24#include "EffectTypes.h"
25#include "effect-impl/EffectContext.h"
26#include "effect-impl/EffectTypes.h"
27#include "effect-impl/EffectWorker.h"
28
29namespace aidl::android::hardware::audio::effect {
30
31class EffectImpl : public BnEffect, public EffectWorker {
32 public:
33 EffectImpl() { LOG(DEBUG) << __func__; }
34 ~EffectImpl() {
35 cleanUp();
36 LOG(DEBUG) << __func__;
37 }
38
39 /**
40 * Each effect implementation CAN override these methods if necessary
41 * If you would like implement IEffect::open completely, override EffectImpl::open(), if you
42 * want to keep most of EffectImpl logic but have a little customize, try override openImpl().
43 * openImpl() will be called at the beginning of EffectImpl::open() without lock protection.
44 *
45 * Same for closeImpl().
46 */
47 virtual ndk::ScopedAStatus open(const Parameter::Common& common,
48 const std::optional<Parameter::Specific>& specific,
49 OpenEffectReturn* ret) override;
50 virtual ndk::ScopedAStatus close() override;
51 virtual ndk::ScopedAStatus command(CommandId id) override;
52 virtual ndk::ScopedAStatus commandStart() { return ndk::ScopedAStatus::ok(); }
53 virtual ndk::ScopedAStatus commandStop() { return ndk::ScopedAStatus::ok(); }
54 virtual ndk::ScopedAStatus commandReset() { return ndk::ScopedAStatus::ok(); }
55
56 virtual ndk::ScopedAStatus getState(State* state) override;
57 virtual ndk::ScopedAStatus setParameter(const Parameter& param) override;
58 virtual ndk::ScopedAStatus getParameter(const Parameter::Id& id, Parameter* param) override;
59 virtual IEffect::Status effectProcessImpl(float* in, float* out, int process) override;
60
61 virtual ndk::ScopedAStatus setParameterCommon(const Parameter& param);
62 virtual ndk::ScopedAStatus getParameterCommon(const Parameter::Tag& tag, Parameter* param);
63
64 /* Methods MUST be implemented by each effect instances */
65 virtual ndk::ScopedAStatus getDescriptor(Descriptor* desc) = 0;
66 virtual ndk::ScopedAStatus setParameterSpecific(const Parameter::Specific& specific) = 0;
67 virtual ndk::ScopedAStatus getParameterSpecific(const Parameter::Id& id,
68 Parameter::Specific* specific) = 0;
69 virtual std::shared_ptr<EffectContext> createContext(const Parameter::Common& common) = 0;
70 virtual RetCode releaseContext() = 0;
71
72 protected:
73 /*
74 * Lock is required if effectProcessImpl (which is running in an independent thread) needs to
75 * access state and parameters.
76 */
77 std::mutex mMutex;
78 State mState GUARDED_BY(mMutex) = State::INIT;
79
80 IEffect::Status status(binder_status_t status, size_t consumed, size_t produced);
81
82 private:
83 void cleanUp();
84 std::shared_ptr<EffectContext> mContext GUARDED_BY(mMutex);
85};
86} // namespace aidl::android::hardware::audio::effect