blob: 4b6cecdcc318b1394d547a73dc498456030b6544 [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 *
57 * @param in address of input float buffer.
58 * @param out address of output float buffer.
59 * @param samples number of samples to process.
60 * @return IEffect::Status
61 */
62 virtual IEffect::Status effectProcessImpl(float* in, float* out, int samples) = 0;
63
64 /**
65 * The default EffectThread::process() implementation doesn't need to lock. It will only
66 * access the FMQ and mWorkBuffer in EffectContext, since they will only be changed in
67 * EffectImpl IEffect::open() (in this case EffectThread just created and not running yet) and
68 * IEffect::command(CommandId::RESET) (in this case EffectThread already stopped).
69 *
70 * process() call effectProcessImpl for effect processing, and because effectProcessImpl is
71 * implemented by effects, process() must not hold lock before call into effectProcessImpl to
72 * avoid deadlock.
73 */
74 virtual void process();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000075
76 private:
Shraddha Basantwanif627d802022-11-08 14:45:07 +053077 const int kMaxTaskNameLen = 15;
78 std::mutex mThreadMutex;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000079 std::condition_variable mCv;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053080 bool mExit GUARDED_BY(mThreadMutex) = false;
81 bool mStop GUARDED_BY(mThreadMutex) = true;
82 std::shared_ptr<EffectContext> mThreadContext GUARDED_BY(mThreadMutex);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000083 std::thread mThread;
84 int mPriority;
85 std::string mName;
86};
87} // namespace aidl::android::hardware::audio::effect