blob: 844127d2c74e7b48ca0b338ac11fc5059b4396f2 [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()) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000039 LOG(WARNING) << "-" << mName << "-" << __func__ << " thread already created, no-op";
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000040 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);
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000050 LOG(DEBUG) << "-" << mName << "-" << __func__ << " priority " << mPriority << " done";
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000051 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 Yaof8be1ac2023-03-06 18:41:27 +000069 LOG(DEBUG) << "-" << mName << "-" << __func__ << " done";
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000070 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()) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000083 LOG(ERROR) << "-" << mName << "-" << __func__ << ": "
84 << " thread already destroyed";
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000085 return RetCode::ERROR_THREAD;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000086 }
87
88 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053089 std::lock_guard lg(mThreadMutex);
Shunkai Yaob49631f2023-02-03 01:44:32 +000090 if (stop == mStop) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000091 LOG(WARNING) << "-" << mName << "-" << __func__ << ": "
92 << " already " << (stop ? "stop" : "start");
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000093 return RetCode::SUCCESS;
94 }
Shunkai Yaob49631f2023-02-03 01:44:32 +000095 mStop = stop;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000096 }
Shunkai Yaob49631f2023-02-03 01:44:32 +000097
98 mCv.notify_one();
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000099 LOG(DEBUG) << ": " << mName << (stop ? " stop done" : " start done");
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000100 return RetCode::SUCCESS;
101}
102
103void EffectThread::threadLoop() {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530104 pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str());
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000105 setpriority(PRIO_PROCESS, 0, mPriority);
106 while (true) {
Shunkai Yaob49631f2023-02-03 01:44:32 +0000107 std::unique_lock l(mThreadMutex);
108 ::android::base::ScopedLockAssertion lock_assertion(mThreadMutex);
109 mCv.wait(l, [&]() REQUIRES(mThreadMutex) { return mExit || !mStop; });
110 if (mExit) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000111 LOG(WARNING) << __func__ << " EXIT!";
112 return;
113 }
Shunkai Yaob49631f2023-02-03 01:44:32 +0000114 process_l();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000115 }
116}
117
Shunkai Yaob49631f2023-02-03 01:44:32 +0000118void EffectThread::process_l() {
119 RETURN_VALUE_IF(!mThreadContext, void(), "nullContext");
120 std::shared_ptr<EffectContext::StatusMQ> statusMQ = mThreadContext->getStatusFmq();
121 std::shared_ptr<EffectContext::DataMQ> inputMQ = mThreadContext->getInputDataFmq();
122 std::shared_ptr<EffectContext::DataMQ> outputMQ = mThreadContext->getOutputDataFmq();
123 auto buffer = mThreadContext->getWorkBuffer();
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530124
125 // Only this worker will read from input data MQ and write to output data MQ.
126 auto readSamples = inputMQ->availableToRead(), writeSamples = outputMQ->availableToWrite();
127 if (readSamples && writeSamples) {
128 auto processSamples = std::min(readSamples, writeSamples);
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000129 LOG(DEBUG) << "-" << mName << "-" << __func__ << ": "
130 << " available to read " << readSamples << " available to write " << writeSamples
131 << " process " << processSamples;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530132
133 inputMQ->read(buffer, processSamples);
134
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530135 IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples);
136 outputMQ->write(buffer, status.fmqProduced);
137 statusMQ->writeBlocking(&status, 1);
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000138 LOG(DEBUG) << "-" << mName << "-" << __func__ << ": "
139 << " done processing, effect consumed " << status.fmqConsumed << " produced "
140 << status.fmqProduced;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530141 } else {
Shunkai Yaoeae5c562023-03-09 02:24:00 +0000142 usleep(mSleepTimeUs);
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530143 }
144}
145
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000146} // namespace aidl::android::hardware::audio::effect