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> |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 19 | #include <memory> |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <thread> |
| 22 | |
| 23 | #include <android-base/thread_annotations.h> |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 24 | #include <fmq/EventFlag.h> |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 25 | #include <system/thread_defs.h> |
| 26 | |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 27 | #include "effect-impl/EffectContext.h" |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 28 | #include "effect-impl/EffectTypes.h" |
| 29 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 30 | namespace aidl::android::hardware::audio::effect { |
| 31 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 32 | class 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 Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 39 | RetCode createThread(std::shared_ptr<EffectContext> context, const std::string& name, |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 40 | int priority = ANDROID_PRIORITY_URGENT_AUDIO); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 41 | RetCode destroyThread(); |
| 42 | RetCode startThread(); |
| 43 | RetCode stopThread(); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 44 | |
| 45 | // Will call process() in a loop if the thread is running. |
| 46 | void threadLoop(); |
| 47 | |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 48 | /** |
| 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 Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 59 | * effectProcessImpl implementation must not call any EffectThread interface, otherwise it will |
| 60 | * cause deadlock. |
| 61 | * |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 62 | * @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 Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 70 | * 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 Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 73 | */ |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 74 | virtual void process_l() REQUIRES(mThreadMutex); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 75 | |
| 76 | private: |
Shunkai Yao | eae5c56 | 2023-03-09 02:24:00 +0000 | [diff] [blame] | 77 | static constexpr int kMaxTaskNameLen = 15; |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 78 | |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 79 | std::mutex mThreadMutex; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 80 | std::condition_variable mCv; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 81 | bool mStop GUARDED_BY(mThreadMutex) = true; |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 82 | bool mExit GUARDED_BY(mThreadMutex) = false; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 83 | std::shared_ptr<EffectContext> mThreadContext GUARDED_BY(mThreadMutex); |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 84 | |
| 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 93 | std::thread mThread; |
| 94 | int mPriority; |
| 95 | std::string mName; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 96 | }; |
| 97 | } // namespace aidl::android::hardware::audio::effect |