Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +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 | #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 Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 25 | #include "effect-impl/EffectTypes.h" |
| 26 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 27 | namespace aidl::android::hardware::audio::effect { |
| 28 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 29 | class 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 Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 36 | RetCode createThread(const std::string& name, |
| 37 | const int priority = ANDROID_PRIORITY_URGENT_AUDIO); |
| 38 | RetCode destroyThread(); |
| 39 | RetCode startThread(); |
| 40 | RetCode stopThread(); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 41 | |
| 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 |