blob: d9825da7adeffb272c94635319580f6dc6d5aa2a [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:
Shunkai Yao812d5b42022-11-16 18:08:50 +000033 EffectImpl() = default;
34 virtual ~EffectImpl() = default;
Shunkai Yao6afc8552022-10-26 22:47:20 +000035
36 /**
37 * Each effect implementation CAN override these methods if necessary
38 * If you would like implement IEffect::open completely, override EffectImpl::open(), if you
39 * want to keep most of EffectImpl logic but have a little customize, try override openImpl().
40 * openImpl() will be called at the beginning of EffectImpl::open() without lock protection.
41 *
42 * Same for closeImpl().
43 */
44 virtual ndk::ScopedAStatus open(const Parameter::Common& common,
45 const std::optional<Parameter::Specific>& specific,
46 OpenEffectReturn* ret) override;
47 virtual ndk::ScopedAStatus close() override;
48 virtual ndk::ScopedAStatus command(CommandId id) override;
49 virtual ndk::ScopedAStatus commandStart() { return ndk::ScopedAStatus::ok(); }
50 virtual ndk::ScopedAStatus commandStop() { return ndk::ScopedAStatus::ok(); }
51 virtual ndk::ScopedAStatus commandReset() { return ndk::ScopedAStatus::ok(); }
52
53 virtual ndk::ScopedAStatus getState(State* state) override;
54 virtual ndk::ScopedAStatus setParameter(const Parameter& param) override;
55 virtual ndk::ScopedAStatus getParameter(const Parameter::Id& id, Parameter* param) override;
56 virtual IEffect::Status effectProcessImpl(float* in, float* out, int process) override;
57
58 virtual ndk::ScopedAStatus setParameterCommon(const Parameter& param);
59 virtual ndk::ScopedAStatus getParameterCommon(const Parameter::Tag& tag, Parameter* param);
60
61 /* Methods MUST be implemented by each effect instances */
62 virtual ndk::ScopedAStatus getDescriptor(Descriptor* desc) = 0;
63 virtual ndk::ScopedAStatus setParameterSpecific(const Parameter::Specific& specific) = 0;
64 virtual ndk::ScopedAStatus getParameterSpecific(const Parameter::Id& id,
65 Parameter::Specific* specific) = 0;
66 virtual std::shared_ptr<EffectContext> createContext(const Parameter::Common& common) = 0;
67 virtual RetCode releaseContext() = 0;
68
69 protected:
70 /*
71 * Lock is required if effectProcessImpl (which is running in an independent thread) needs to
72 * access state and parameters.
73 */
74 std::mutex mMutex;
75 State mState GUARDED_BY(mMutex) = State::INIT;
76
77 IEffect::Status status(binder_status_t status, size_t consumed, size_t produced);
Shunkai Yao812d5b42022-11-16 18:08:50 +000078 void cleanUp();
Shunkai Yao6afc8552022-10-26 22:47:20 +000079
80 private:
Shunkai Yao6afc8552022-10-26 22:47:20 +000081 std::shared_ptr<EffectContext> mContext GUARDED_BY(mMutex);
82};
83} // namespace aidl::android::hardware::audio::effect