blob: 47ba9f44cb4c76763352abc1ea368a7584b85b90 [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);
Jaideep Sharma632b1cd2023-09-20 11:43:17 +053051 mStop = true;
52 mExit = false;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053053 mThreadContext = std::move(context);
Shunkai Yao41888a22023-04-10 17:13:30 +000054 auto statusMQ = mThreadContext->getStatusFmq();
55 EventFlag* efGroup = nullptr;
56 ::android::status_t status =
57 EventFlag::createEventFlag(statusMQ->getEventFlagWord(), &efGroup);
58 if (status != ::android::OK || !efGroup) {
59 LOG(ERROR) << mName << __func__ << " create EventFlagGroup failed " << status
60 << " efGroup " << efGroup;
61 return RetCode::ERROR_THREAD;
62 }
63 mEfGroup.reset(efGroup);
64 // kickoff and wait for commands (CommandId::START/STOP) or IEffect.close from client
65 mEfGroup->wake(kEventFlagNotEmpty);
Shraddha Basantwanif627d802022-11-08 14:45:07 +053066 }
Shunkai Yao41888a22023-04-10 17:13:30 +000067
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000068 mThread = std::thread(&EffectThread::threadLoop, this);
Shunkai Yao41888a22023-04-10 17:13:30 +000069 LOG(DEBUG) << mName << __func__ << " priority " << mPriority << " done";
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000070 return RetCode::SUCCESS;
71}
72
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000073RetCode EffectThread::destroyThread() {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000074 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053075 std::lock_guard lg(mThreadMutex);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000076 mStop = mExit = true;
77 }
78 mCv.notify_one();
79
80 if (mThread.joinable()) {
81 mThread.join();
82 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +053083
84 {
85 std::lock_guard lg(mThreadMutex);
86 mThreadContext.reset();
87 }
Shunkai Yao41888a22023-04-10 17:13:30 +000088 LOG(DEBUG) << mName << __func__;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000089 return RetCode::SUCCESS;
90}
91
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000092RetCode EffectThread::startThread() {
Shunkai Yao41888a22023-04-10 17:13:30 +000093 {
94 std::lock_guard lg(mThreadMutex);
95 mStop = false;
96 mCv.notify_one();
97 }
98
99 mEfGroup->wake(kEventFlagNotEmpty);
100 LOG(DEBUG) << mName << __func__;
101 return RetCode::SUCCESS;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000102}
103
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000104RetCode EffectThread::stopThread() {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000105 {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530106 std::lock_guard lg(mThreadMutex);
Shunkai Yao41888a22023-04-10 17:13:30 +0000107 mStop = true;
108 mCv.notify_one();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000109 }
Shunkai Yaob49631f2023-02-03 01:44:32 +0000110
Shunkai Yao41888a22023-04-10 17:13:30 +0000111 mEfGroup->wake(kEventFlagNotEmpty);
112 LOG(DEBUG) << mName << __func__;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000113 return RetCode::SUCCESS;
114}
115
116void EffectThread::threadLoop() {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530117 pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str());
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000118 setpriority(PRIO_PROCESS, 0, mPriority);
119 while (true) {
Shunkai Yao41888a22023-04-10 17:13:30 +0000120 /**
121 * wait for the EventFlag without lock, it's ok because the mEfGroup pointer will not change
122 * in the life cycle of workerThread (threadLoop).
123 */
124 uint32_t efState = 0;
125 mEfGroup->wait(kEventFlagNotEmpty, &efState);
126
127 {
128 std::unique_lock l(mThreadMutex);
129 ::android::base::ScopedLockAssertion lock_assertion(mThreadMutex);
130 mCv.wait(l, [&]() REQUIRES(mThreadMutex) { return mExit || !mStop; });
131 if (mExit) {
132 LOG(INFO) << __func__ << " EXIT!";
133 return;
134 }
135 process_l();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000136 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000137 }
138}
139
Shunkai Yaob49631f2023-02-03 01:44:32 +0000140void EffectThread::process_l() {
141 RETURN_VALUE_IF(!mThreadContext, void(), "nullContext");
Shunkai Yao41888a22023-04-10 17:13:30 +0000142
143 auto statusMQ = mThreadContext->getStatusFmq();
144 auto inputMQ = mThreadContext->getInputDataFmq();
145 auto outputMQ = mThreadContext->getOutputDataFmq();
Shunkai Yaob49631f2023-02-03 01:44:32 +0000146 auto buffer = mThreadContext->getWorkBuffer();
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530147
Shunkai Yao41888a22023-04-10 17:13:30 +0000148 auto processSamples = inputMQ->availableToRead();
149 if (processSamples) {
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530150 inputMQ->read(buffer, processSamples);
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530151 IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples);
152 outputMQ->write(buffer, status.fmqProduced);
153 statusMQ->writeBlocking(&status, 1);
Shunkai Yao074cf232023-07-14 21:08:31 +0000154 LOG(VERBOSE) << mName << __func__ << ": done processing, effect consumed "
155 << status.fmqConsumed << " produced " << status.fmqProduced;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530156 }
157}
158
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000159} // namespace aidl::android::hardware::audio::effect