blob: 024c0eaeed7a702c61fd70ee7ad9ae91edd84c77 [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 Yaob49631f2023-02-03 01:44:32 +000073 return handleStartStop(false /* stop */);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000074}
75
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000076RetCode EffectThread::stopThread() {
Shunkai Yaob49631f2023-02-03 01:44:32 +000077 return handleStartStop(true /* stop */);
78}
79
80RetCode EffectThread::handleStartStop(bool stop) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000081 if (!mThread.joinable()) {
82 LOG(ERROR) << __func__ << " thread already destroyed";
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000083 return RetCode::ERROR_THREAD;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000084 }
85
86 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053087 std::lock_guard lg(mThreadMutex);
Shunkai Yaob49631f2023-02-03 01:44:32 +000088 if (stop == mStop) {
Greg Kaiser17927df2023-02-09 17:46:52 +000089 LOG(WARNING) << __func__ << " already " << (stop ? "stop" : "start");
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000090 return RetCode::SUCCESS;
91 }
Shunkai Yaob49631f2023-02-03 01:44:32 +000092 mStop = stop;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000093 }
Shunkai Yaob49631f2023-02-03 01:44:32 +000094
95 mCv.notify_one();
Greg Kaiser17927df2023-02-09 17:46:52 +000096 LOG(DEBUG) << (stop ? "stop done" : "start done");
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000097 return RetCode::SUCCESS;
98}
99
100void EffectThread::threadLoop() {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530101 pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str());
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000102 setpriority(PRIO_PROCESS, 0, mPriority);
103 while (true) {
Shunkai Yaob49631f2023-02-03 01:44:32 +0000104 std::unique_lock l(mThreadMutex);
105 ::android::base::ScopedLockAssertion lock_assertion(mThreadMutex);
106 mCv.wait(l, [&]() REQUIRES(mThreadMutex) { return mExit || !mStop; });
107 if (mExit) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000108 LOG(WARNING) << __func__ << " EXIT!";
109 return;
110 }
Shunkai Yaob49631f2023-02-03 01:44:32 +0000111 process_l();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000112 }
113}
114
Shunkai Yaob49631f2023-02-03 01:44:32 +0000115void EffectThread::process_l() {
116 RETURN_VALUE_IF(!mThreadContext, void(), "nullContext");
117 std::shared_ptr<EffectContext::StatusMQ> statusMQ = mThreadContext->getStatusFmq();
118 std::shared_ptr<EffectContext::DataMQ> inputMQ = mThreadContext->getInputDataFmq();
119 std::shared_ptr<EffectContext::DataMQ> outputMQ = mThreadContext->getOutputDataFmq();
120 auto buffer = mThreadContext->getWorkBuffer();
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530121
122 // Only this worker will read from input data MQ and write to output data MQ.
123 auto readSamples = inputMQ->availableToRead(), writeSamples = outputMQ->availableToWrite();
124 if (readSamples && writeSamples) {
125 auto processSamples = std::min(readSamples, writeSamples);
126 LOG(DEBUG) << __func__ << " available to read " << readSamples << " available to write "
127 << writeSamples << " process " << processSamples;
128
129 inputMQ->read(buffer, processSamples);
130
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530131 IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples);
132 outputMQ->write(buffer, status.fmqProduced);
133 statusMQ->writeBlocking(&status, 1);
134 LOG(DEBUG) << __func__ << " done processing, effect consumed " << status.fmqConsumed
135 << " produced " << status.fmqProduced;
136 } else {
137 // TODO: maybe add some sleep here to avoid busy waiting
138 }
139}
140
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000141} // namespace aidl::android::hardware::audio::effect