blob: ae51ef70ab6ae98d46ce215cfa70a7dfb4816e1f [file] [log] [blame]
Shunkai Yaoea24c1a2022-09-28 17:39:23 +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 <atomic>
Shunkai Yao41888a22023-04-10 17:13:30 +000019#include <memory>
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000020#include <string>
21#include <thread>
22
23#include <android-base/thread_annotations.h>
Shunkai Yao41888a22023-04-10 17:13:30 +000024#include <fmq/EventFlag.h>
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000025#include <system/thread_defs.h>
26
Shraddha Basantwanif627d802022-11-08 14:45:07 +053027#include "effect-impl/EffectContext.h"
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000028#include "effect-impl/EffectTypes.h"
29
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000030namespace aidl::android::hardware::audio::effect {
31
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000032class EffectThread {
33 public:
34 // default priority is same as HIDL: ANDROID_PRIORITY_URGENT_AUDIO
35 EffectThread();
36 virtual ~EffectThread();
37
38 // called by effect implementation.
Shraddha Basantwanif627d802022-11-08 14:45:07 +053039 RetCode createThread(std::shared_ptr<EffectContext> context, const std::string& name,
Shunkai Yao41888a22023-04-10 17:13:30 +000040 int priority = ANDROID_PRIORITY_URGENT_AUDIO);
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000041 RetCode destroyThread();
42 RetCode startThread();
43 RetCode stopThread();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000044
45 // Will call process() in a loop if the thread is running.
46 void threadLoop();
47
Shraddha Basantwanif627d802022-11-08 14:45:07 +053048 /**
49 * @brief effectProcessImpl is running in worker thread which created in EffectThread.
50 *
51 * Effect implementation should think about concurrency in the implementation if necessary.
52 * Parameter setting usually implemented in context (derived from EffectContext), and some
53 * parameter maybe used in the processing, then effect implementation should consider using a
54 * mutex to protect these parameter.
55 *
56 * EffectThread will make sure effectProcessImpl only be called after startThread() successful
57 * and before stopThread() successful.
58 *
Shunkai Yaob49631f2023-02-03 01:44:32 +000059 * effectProcessImpl implementation must not call any EffectThread interface, otherwise it will
60 * cause deadlock.
61 *
Shraddha Basantwanif627d802022-11-08 14:45:07 +053062 * @param in address of input float buffer.
63 * @param out address of output float buffer.
64 * @param samples number of samples to process.
65 * @return IEffect::Status
66 */
67 virtual IEffect::Status effectProcessImpl(float* in, float* out, int samples) = 0;
68
69 /**
Shunkai Yaob49631f2023-02-03 01:44:32 +000070 * process() call effectProcessImpl() for effect data processing, it is necessary for the
71 * processing to be called under Effect thread mutex mThreadMutex, to avoid the effect state
72 * change before/during data processing, and keep the thread and effect state consistent.
Shraddha Basantwanif627d802022-11-08 14:45:07 +053073 */
Shunkai Yaob49631f2023-02-03 01:44:32 +000074 virtual void process_l() REQUIRES(mThreadMutex);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000075
76 private:
Shunkai Yaoeae5c562023-03-09 02:24:00 +000077 static constexpr int kMaxTaskNameLen = 15;
Shunkai Yao41888a22023-04-10 17:13:30 +000078
Shraddha Basantwanif627d802022-11-08 14:45:07 +053079 std::mutex mThreadMutex;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000080 std::condition_variable mCv;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053081 bool mStop GUARDED_BY(mThreadMutex) = true;
Shunkai Yao41888a22023-04-10 17:13:30 +000082 bool mExit GUARDED_BY(mThreadMutex) = false;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053083 std::shared_ptr<EffectContext> mThreadContext GUARDED_BY(mThreadMutex);
Shunkai Yao41888a22023-04-10 17:13:30 +000084
85 struct EventFlagDeleter {
86 void operator()(::android::hardware::EventFlag* flag) const {
87 if (flag) {
88 ::android::hardware::EventFlag::deleteEventFlag(&flag);
89 }
90 }
91 };
92 std::unique_ptr<::android::hardware::EventFlag, EventFlagDeleter> mEfGroup;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000093 std::thread mThread;
94 int mPriority;
95 std::string mName;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000096};
97} // namespace aidl::android::hardware::audio::effect