blob: 09a0000001cd71e8e299eb2e6c322af8f0ccca4a [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>
19#include <string>
20#include <thread>
21
22#include <android-base/thread_annotations.h>
23#include <system/thread_defs.h>
24
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000025#include "effect-impl/EffectTypes.h"
26
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000027namespace aidl::android::hardware::audio::effect {
28
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000029class EffectThread {
30 public:
31 // default priority is same as HIDL: ANDROID_PRIORITY_URGENT_AUDIO
32 EffectThread();
33 virtual ~EffectThread();
34
35 // called by effect implementation.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000036 RetCode createThread(const std::string& name,
37 const int priority = ANDROID_PRIORITY_URGENT_AUDIO);
38 RetCode destroyThread();
39 RetCode startThread();
40 RetCode stopThread();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000041
42 // Will call process() in a loop if the thread is running.
43 void threadLoop();
44
45 // User of EffectThread must implement the effect processing logic in this method.
46 virtual void process() = 0;
47 const int MAX_TASK_COMM_LEN = 15;
48
49 private:
50 std::mutex mMutex;
51 std::condition_variable mCv;
52 bool mExit GUARDED_BY(mMutex) = false;
53 bool mStop GUARDED_BY(mMutex) = true;
54 std::thread mThread;
55 int mPriority;
56 std::string mName;
57};
58} // namespace aidl::android::hardware::audio::effect