blob: 9b1a75bbc9b0ae634dc4cd964ecbad6386355891 [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
Shraddha Basantwanif627d802022-11-08 14:45:07 +053025#include "effect-impl/EffectContext.h"
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000026#include "effect-impl/EffectTypes.h"
27
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000028namespace aidl::android::hardware::audio::effect {
29
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000030class EffectThread {
31 public:
32 // default priority is same as HIDL: ANDROID_PRIORITY_URGENT_AUDIO
33 EffectThread();
34 virtual ~EffectThread();
35
36 // called by effect implementation.
Shraddha Basantwanif627d802022-11-08 14:45:07 +053037 RetCode createThread(std::shared_ptr<EffectContext> context, const std::string& name,
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000038 const int priority = ANDROID_PRIORITY_URGENT_AUDIO);
39 RetCode destroyThread();
40 RetCode startThread();
41 RetCode stopThread();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000042
43 // Will call process() in a loop if the thread is running.
44 void threadLoop();
45
Shraddha Basantwanif627d802022-11-08 14:45:07 +053046 /**
47 * @brief effectProcessImpl is running in worker thread which created in EffectThread.
48 *
49 * Effect implementation should think about concurrency in the implementation if necessary.
50 * Parameter setting usually implemented in context (derived from EffectContext), and some
51 * parameter maybe used in the processing, then effect implementation should consider using a
52 * mutex to protect these parameter.
53 *
54 * EffectThread will make sure effectProcessImpl only be called after startThread() successful
55 * and before stopThread() successful.
56 *
Shunkai Yaob49631f2023-02-03 01:44:32 +000057 * effectProcessImpl implementation must not call any EffectThread interface, otherwise it will
58 * cause deadlock.
59 *
Shraddha Basantwanif627d802022-11-08 14:45:07 +053060 * @param in address of input float buffer.
61 * @param out address of output float buffer.
62 * @param samples number of samples to process.
63 * @return IEffect::Status
64 */
65 virtual IEffect::Status effectProcessImpl(float* in, float* out, int samples) = 0;
66
67 /**
Shunkai Yaob49631f2023-02-03 01:44:32 +000068 * process() call effectProcessImpl() for effect data processing, it is necessary for the
69 * processing to be called under Effect thread mutex mThreadMutex, to avoid the effect state
70 * change before/during data processing, and keep the thread and effect state consistent.
Shraddha Basantwanif627d802022-11-08 14:45:07 +053071 */
Shunkai Yaob49631f2023-02-03 01:44:32 +000072 virtual void process_l() REQUIRES(mThreadMutex);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000073
74 private:
Shraddha Basantwanif627d802022-11-08 14:45:07 +053075 const int kMaxTaskNameLen = 15;
76 std::mutex mThreadMutex;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000077 std::condition_variable mCv;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053078 bool mExit GUARDED_BY(mThreadMutex) = false;
79 bool mStop GUARDED_BY(mThreadMutex) = true;
80 std::shared_ptr<EffectContext> mThreadContext GUARDED_BY(mThreadMutex);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000081 std::thread mThread;
82 int mPriority;
83 std::string mName;
Shunkai Yaob49631f2023-02-03 01:44:32 +000084
85 RetCode handleStartStop(bool stop);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000086};
87} // namespace aidl::android::hardware::audio::effect