blob: cd2ba5375b47eecdec6f5e8763966fb8a1f20b68 [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
Shunkai Yao41888a22023-04-10 17:13:30 +000017#include <cstddef>
Shraddha Basantwanif627d802022-11-08 14:45:07 +053018#include <memory>
Shunkai Yao41888a22023-04-10 17:13:30 +000019
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000020#define LOG_TAG "AHAL_EffectThread"
21#include <android-base/logging.h>
22#include <pthread.h>
23#include <sys/resource.h>
24
25#include "effect-impl/EffectThread.h"
Shunkai Yao41888a22023-04-10 17:13:30 +000026#include "effect-impl/EffectTypes.h"
27
28using ::android::hardware::EventFlag;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000029
30namespace aidl::android::hardware::audio::effect {
31
32EffectThread::EffectThread() {
33 LOG(DEBUG) << __func__;
34}
35
36EffectThread::~EffectThread() {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000037 destroyThread();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000038 LOG(DEBUG) << __func__ << " done";
Shunkai Yao41888a22023-04-10 17:13:30 +000039}
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000040
Shraddha Basantwanif627d802022-11-08 14:45:07 +053041RetCode EffectThread::createThread(std::shared_ptr<EffectContext> context, const std::string& name,
Shunkai Yao41888a22023-04-10 17:13:30 +000042 int priority) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000043 if (mThread.joinable()) {
Shunkai Yao41888a22023-04-10 17:13:30 +000044 LOG(WARNING) << mName << __func__ << " thread already created, no-op";
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000045 return RetCode::SUCCESS;
46 }
47 mName = name;
48 mPriority = priority;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053049 {
50 std::lock_guard lg(mThreadMutex);
51 mThreadContext = std::move(context);
Shunkai Yao41888a22023-04-10 17:13:30 +000052 auto statusMQ = mThreadContext->getStatusFmq();
53 EventFlag* efGroup = nullptr;
54 ::android::status_t status =
55 EventFlag::createEventFlag(statusMQ->getEventFlagWord(), &efGroup);
56 if (status != ::android::OK || !efGroup) {
57 LOG(ERROR) << mName << __func__ << " create EventFlagGroup failed " << status
58 << " efGroup " << efGroup;
59 return RetCode::ERROR_THREAD;
60 }
61 mEfGroup.reset(efGroup);
62 // kickoff and wait for commands (CommandId::START/STOP) or IEffect.close from client
63 mEfGroup->wake(kEventFlagNotEmpty);
Shraddha Basantwanif627d802022-11-08 14:45:07 +053064 }
Shunkai Yao41888a22023-04-10 17:13:30 +000065
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000066 mThread = std::thread(&EffectThread::threadLoop, this);
Shunkai Yao41888a22023-04-10 17:13:30 +000067 LOG(DEBUG) << mName << __func__ << " priority " << mPriority << " done";
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000068 return RetCode::SUCCESS;
69}
70
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000071RetCode EffectThread::destroyThread() {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000072 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053073 std::lock_guard lg(mThreadMutex);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000074 mStop = mExit = true;
75 }
76 mCv.notify_one();
77
78 if (mThread.joinable()) {
79 mThread.join();
80 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +053081
82 {
83 std::lock_guard lg(mThreadMutex);
84 mThreadContext.reset();
85 }
Shunkai Yao41888a22023-04-10 17:13:30 +000086 LOG(DEBUG) << mName << __func__;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000087 return RetCode::SUCCESS;
88}
89
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000090RetCode EffectThread::startThread() {
Shunkai Yao41888a22023-04-10 17:13:30 +000091 {
92 std::lock_guard lg(mThreadMutex);
93 mStop = false;
94 mCv.notify_one();
95 }
96
97 mEfGroup->wake(kEventFlagNotEmpty);
98 LOG(DEBUG) << mName << __func__;
99 return RetCode::SUCCESS;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000100}
101
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000102RetCode EffectThread::stopThread() {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000103 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530104 std::lock_guard lg(mThreadMutex);
Shunkai Yao41888a22023-04-10 17:13:30 +0000105 mStop = true;
106 mCv.notify_one();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000107 }
Shunkai Yaob49631f2023-02-03 01:44:32 +0000108
Shunkai Yao41888a22023-04-10 17:13:30 +0000109 mEfGroup->wake(kEventFlagNotEmpty);
110 LOG(DEBUG) << mName << __func__;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000111 return RetCode::SUCCESS;
112}
113
114void EffectThread::threadLoop() {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530115 pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str());
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000116 setpriority(PRIO_PROCESS, 0, mPriority);
117 while (true) {
Shunkai Yao41888a22023-04-10 17:13:30 +0000118 /**
119 * wait for the EventFlag without lock, it's ok because the mEfGroup pointer will not change
120 * in the life cycle of workerThread (threadLoop).
121 */
122 uint32_t efState = 0;
123 mEfGroup->wait(kEventFlagNotEmpty, &efState);
124
125 {
126 std::unique_lock l(mThreadMutex);
127 ::android::base::ScopedLockAssertion lock_assertion(mThreadMutex);
128 mCv.wait(l, [&]() REQUIRES(mThreadMutex) { return mExit || !mStop; });
129 if (mExit) {
130 LOG(INFO) << __func__ << " EXIT!";
131 return;
132 }
133 process_l();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000134 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000135 }
136}
137
Shunkai Yaob49631f2023-02-03 01:44:32 +0000138void EffectThread::process_l() {
139 RETURN_VALUE_IF(!mThreadContext, void(), "nullContext");
Shunkai Yao41888a22023-04-10 17:13:30 +0000140
141 auto statusMQ = mThreadContext->getStatusFmq();
142 auto inputMQ = mThreadContext->getInputDataFmq();
143 auto outputMQ = mThreadContext->getOutputDataFmq();
Shunkai Yaob49631f2023-02-03 01:44:32 +0000144 auto buffer = mThreadContext->getWorkBuffer();
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530145
Shunkai Yao41888a22023-04-10 17:13:30 +0000146 auto processSamples = inputMQ->availableToRead();
147 if (processSamples) {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530148 inputMQ->read(buffer, processSamples);
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530149 IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples);
150 outputMQ->write(buffer, status.fmqProduced);
151 statusMQ->writeBlocking(&status, 1);
Shunkai Yao074cf232023-07-14 21:08:31 +0000152 LOG(VERBOSE) << mName << __func__ << ": done processing, effect consumed "
153 << status.fmqConsumed << " produced " << status.fmqProduced;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530154 }
155}
156
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000157} // namespace aidl::android::hardware::audio::effect