blob: 2b3513d619c9bb9523eda4c65ccb5a658cb8cfeb [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
Shraddha Basantwanif627d802022-11-08 14:45:07 +053017#include <memory>
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000018#define LOG_TAG "AHAL_EffectThread"
19#include <android-base/logging.h>
20#include <pthread.h>
21#include <sys/resource.h>
22
23#include "effect-impl/EffectThread.h"
24
25namespace aidl::android::hardware::audio::effect {
26
27EffectThread::EffectThread() {
28 LOG(DEBUG) << __func__;
29}
30
31EffectThread::~EffectThread() {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000032 destroyThread();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000033 LOG(DEBUG) << __func__ << " done";
34};
35
Shraddha Basantwanif627d802022-11-08 14:45:07 +053036RetCode EffectThread::createThread(std::shared_ptr<EffectContext> context, const std::string& name,
37 const int priority) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000038 if (mThread.joinable()) {
39 LOG(WARNING) << __func__ << " thread already created, no-op";
40 return RetCode::SUCCESS;
41 }
42 mName = name;
43 mPriority = priority;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053044 {
45 std::lock_guard lg(mThreadMutex);
46 mThreadContext = std::move(context);
47 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000048 mThread = std::thread(&EffectThread::threadLoop, this);
49 LOG(DEBUG) << __func__ << " " << name << " priority " << mPriority << " done";
50 return RetCode::SUCCESS;
51}
52
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000053RetCode EffectThread::destroyThread() {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000054 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053055 std::lock_guard lg(mThreadMutex);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000056 mStop = mExit = true;
57 }
58 mCv.notify_one();
59
60 if (mThread.joinable()) {
61 mThread.join();
62 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +053063
64 {
65 std::lock_guard lg(mThreadMutex);
66 mThreadContext.reset();
67 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000068 LOG(DEBUG) << __func__ << " done";
69 return RetCode::SUCCESS;
70}
71
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000072RetCode EffectThread::startThread() {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000073 if (!mThread.joinable()) {
74 LOG(ERROR) << __func__ << " thread already destroyed";
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000075 return RetCode::ERROR_THREAD;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000076 }
77
78 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053079 std::lock_guard lg(mThreadMutex);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000080 if (!mStop) {
81 LOG(WARNING) << __func__ << " already start";
82 return RetCode::SUCCESS;
83 }
84 mStop = false;
85 }
86
87 mCv.notify_one();
88 LOG(DEBUG) << __func__ << " done";
89 return RetCode::SUCCESS;
90}
91
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000092RetCode EffectThread::stopThread() {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000093 if (!mThread.joinable()) {
94 LOG(ERROR) << __func__ << " thread already destroyed";
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000095 return RetCode::ERROR_THREAD;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000096 }
97
98 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053099 std::lock_guard lg(mThreadMutex);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000100 if (mStop) {
101 LOG(WARNING) << __func__ << " already stop";
102 return RetCode::SUCCESS;
103 }
104 mStop = true;
105 }
106 LOG(DEBUG) << __func__ << " done";
107 return RetCode::SUCCESS;
108}
109
110void EffectThread::threadLoop() {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530111 pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str());
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000112 setpriority(PRIO_PROCESS, 0, mPriority);
113 while (true) {
114 bool needExit = false;
115 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530116 std::unique_lock l(mThreadMutex);
117 mCv.wait(l, [&]() REQUIRES(mThreadMutex) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000118 needExit = mExit;
119 return mExit || !mStop;
120 });
121 }
122 if (needExit) {
123 LOG(WARNING) << __func__ << " EXIT!";
124 return;
125 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530126
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000127 process();
128 }
129}
130
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530131void EffectThread::process() {
132 std::shared_ptr<EffectContext> context;
133 {
134 std::lock_guard lg(mThreadMutex);
135 context = mThreadContext;
136 RETURN_VALUE_IF(!context, void(), "nullContext");
137 }
138 std::shared_ptr<EffectContext::StatusMQ> statusMQ = context->getStatusFmq();
139 std::shared_ptr<EffectContext::DataMQ> inputMQ = context->getInputDataFmq();
140 std::shared_ptr<EffectContext::DataMQ> outputMQ = context->getOutputDataFmq();
141 auto buffer = context->getWorkBuffer();
142
143 // Only this worker will read from input data MQ and write to output data MQ.
144 auto readSamples = inputMQ->availableToRead(), writeSamples = outputMQ->availableToWrite();
145 if (readSamples && writeSamples) {
146 auto processSamples = std::min(readSamples, writeSamples);
147 LOG(DEBUG) << __func__ << " available to read " << readSamples << " available to write "
148 << writeSamples << " process " << processSamples;
149
150 inputMQ->read(buffer, processSamples);
151
152 // call effectProcessImpl without lock
153 IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples);
154 outputMQ->write(buffer, status.fmqProduced);
155 statusMQ->writeBlocking(&status, 1);
156 LOG(DEBUG) << __func__ << " done processing, effect consumed " << status.fmqConsumed
157 << " produced " << status.fmqProduced;
158 } else {
159 // TODO: maybe add some sleep here to avoid busy waiting
160 }
161}
162
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000163} // namespace aidl::android::hardware::audio::effect