blob: 4f8fb3c08e2b31bb60c851391f71ab9fab62a4dc [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,
Shunkai Yaoeae5c562023-03-09 02:24:00 +000037 int priority, int sleepUs /* kSleepTimeUs */) {
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;
Shunkai Yaoeae5c562023-03-09 02:24:00 +000044 mSleepTimeUs = sleepUs;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053045 {
46 std::lock_guard lg(mThreadMutex);
47 mThreadContext = std::move(context);
48 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000049 mThread = std::thread(&EffectThread::threadLoop, this);
50 LOG(DEBUG) << __func__ << " " << name << " priority " << mPriority << " done";
51 return RetCode::SUCCESS;
52}
53
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000054RetCode EffectThread::destroyThread() {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000055 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053056 std::lock_guard lg(mThreadMutex);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000057 mStop = mExit = true;
58 }
59 mCv.notify_one();
60
61 if (mThread.joinable()) {
62 mThread.join();
63 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +053064
65 {
66 std::lock_guard lg(mThreadMutex);
67 mThreadContext.reset();
68 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000069 LOG(DEBUG) << __func__ << " done";
70 return RetCode::SUCCESS;
71}
72
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000073RetCode EffectThread::startThread() {
Shunkai Yaob49631f2023-02-03 01:44:32 +000074 return handleStartStop(false /* stop */);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000075}
76
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000077RetCode EffectThread::stopThread() {
Shunkai Yaob49631f2023-02-03 01:44:32 +000078 return handleStartStop(true /* stop */);
79}
80
81RetCode EffectThread::handleStartStop(bool stop) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000082 if (!mThread.joinable()) {
83 LOG(ERROR) << __func__ << " thread already destroyed";
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000084 return RetCode::ERROR_THREAD;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000085 }
86
87 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053088 std::lock_guard lg(mThreadMutex);
Shunkai Yaob49631f2023-02-03 01:44:32 +000089 if (stop == mStop) {
Greg Kaiser17927df2023-02-09 17:46:52 +000090 LOG(WARNING) << __func__ << " already " << (stop ? "stop" : "start");
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000091 return RetCode::SUCCESS;
92 }
Shunkai Yaob49631f2023-02-03 01:44:32 +000093 mStop = stop;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000094 }
Shunkai Yaob49631f2023-02-03 01:44:32 +000095
96 mCv.notify_one();
Greg Kaiser17927df2023-02-09 17:46:52 +000097 LOG(DEBUG) << (stop ? "stop done" : "start done");
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000098 return RetCode::SUCCESS;
99}
100
101void EffectThread::threadLoop() {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530102 pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str());
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000103 setpriority(PRIO_PROCESS, 0, mPriority);
104 while (true) {
Shunkai Yaob49631f2023-02-03 01:44:32 +0000105 std::unique_lock l(mThreadMutex);
106 ::android::base::ScopedLockAssertion lock_assertion(mThreadMutex);
107 mCv.wait(l, [&]() REQUIRES(mThreadMutex) { return mExit || !mStop; });
108 if (mExit) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000109 LOG(WARNING) << __func__ << " EXIT!";
110 return;
111 }
Shunkai Yaob49631f2023-02-03 01:44:32 +0000112 process_l();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000113 }
114}
115
Shunkai Yaob49631f2023-02-03 01:44:32 +0000116void EffectThread::process_l() {
117 RETURN_VALUE_IF(!mThreadContext, void(), "nullContext");
118 std::shared_ptr<EffectContext::StatusMQ> statusMQ = mThreadContext->getStatusFmq();
119 std::shared_ptr<EffectContext::DataMQ> inputMQ = mThreadContext->getInputDataFmq();
120 std::shared_ptr<EffectContext::DataMQ> outputMQ = mThreadContext->getOutputDataFmq();
121 auto buffer = mThreadContext->getWorkBuffer();
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530122
123 // Only this worker will read from input data MQ and write to output data MQ.
124 auto readSamples = inputMQ->availableToRead(), writeSamples = outputMQ->availableToWrite();
125 if (readSamples && writeSamples) {
126 auto processSamples = std::min(readSamples, writeSamples);
127 LOG(DEBUG) << __func__ << " available to read " << readSamples << " available to write "
128 << writeSamples << " process " << processSamples;
129
130 inputMQ->read(buffer, processSamples);
131
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530132 IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples);
133 outputMQ->write(buffer, status.fmqProduced);
134 statusMQ->writeBlocking(&status, 1);
135 LOG(DEBUG) << __func__ << " done processing, effect consumed " << status.fmqConsumed
136 << " produced " << status.fmqProduced;
137 } else {
Shunkai Yaoeae5c562023-03-09 02:24:00 +0000138 usleep(mSleepTimeUs);
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530139 }
140}
141
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000142} // namespace aidl::android::hardware::audio::effect