Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 1 | /* |
| 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 Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 17 | #include <cstddef> |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 18 | #include <memory> |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 19 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 20 | #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 Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 26 | #include "effect-impl/EffectTypes.h" |
| 27 | |
| 28 | using ::android::hardware::EventFlag; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 29 | |
| 30 | namespace aidl::android::hardware::audio::effect { |
| 31 | |
| 32 | EffectThread::EffectThread() { |
| 33 | LOG(DEBUG) << __func__; |
| 34 | } |
| 35 | |
| 36 | EffectThread::~EffectThread() { |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 37 | destroyThread(); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 38 | LOG(DEBUG) << __func__ << " done"; |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 39 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 40 | |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 41 | RetCode EffectThread::createThread(std::shared_ptr<EffectContext> context, const std::string& name, |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 42 | int priority) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 43 | if (mThread.joinable()) { |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 44 | LOG(WARNING) << mName << __func__ << " thread already created, no-op"; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 45 | return RetCode::SUCCESS; |
| 46 | } |
| 47 | mName = name; |
| 48 | mPriority = priority; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 49 | { |
| 50 | std::lock_guard lg(mThreadMutex); |
Jaideep Sharma | 632b1cd | 2023-09-20 11:43:17 +0530 | [diff] [blame] | 51 | mStop = true; |
| 52 | mExit = false; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 53 | mThreadContext = std::move(context); |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 54 | 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 Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 66 | } |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 67 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 68 | mThread = std::thread(&EffectThread::threadLoop, this); |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 69 | LOG(DEBUG) << mName << __func__ << " priority " << mPriority << " done"; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 70 | return RetCode::SUCCESS; |
| 71 | } |
| 72 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 73 | RetCode EffectThread::destroyThread() { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 74 | { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 75 | std::lock_guard lg(mThreadMutex); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 76 | mStop = mExit = true; |
| 77 | } |
| 78 | mCv.notify_one(); |
| 79 | |
| 80 | if (mThread.joinable()) { |
| 81 | mThread.join(); |
| 82 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 83 | |
| 84 | { |
| 85 | std::lock_guard lg(mThreadMutex); |
| 86 | mThreadContext.reset(); |
| 87 | } |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 88 | LOG(DEBUG) << mName << __func__; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 89 | return RetCode::SUCCESS; |
| 90 | } |
| 91 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 92 | RetCode EffectThread::startThread() { |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 93 | { |
| 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 104 | RetCode EffectThread::stopThread() { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 105 | { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 106 | std::lock_guard lg(mThreadMutex); |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 107 | mStop = true; |
| 108 | mCv.notify_one(); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 109 | } |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 110 | |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 111 | mEfGroup->wake(kEventFlagNotEmpty); |
| 112 | LOG(DEBUG) << mName << __func__; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 113 | return RetCode::SUCCESS; |
| 114 | } |
| 115 | |
| 116 | void EffectThread::threadLoop() { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 117 | pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str()); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 118 | setpriority(PRIO_PROCESS, 0, mPriority); |
| 119 | while (true) { |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 120 | /** |
| 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 136 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 140 | void EffectThread::process_l() { |
| 141 | RETURN_VALUE_IF(!mThreadContext, void(), "nullContext"); |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 142 | |
| 143 | auto statusMQ = mThreadContext->getStatusFmq(); |
| 144 | auto inputMQ = mThreadContext->getInputDataFmq(); |
| 145 | auto outputMQ = mThreadContext->getOutputDataFmq(); |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 146 | auto buffer = mThreadContext->getWorkBuffer(); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 147 | |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 148 | auto processSamples = inputMQ->availableToRead(); |
| 149 | if (processSamples) { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 150 | inputMQ->read(buffer, processSamples); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 151 | IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples); |
| 152 | outputMQ->write(buffer, status.fmqProduced); |
| 153 | statusMQ->writeBlocking(&status, 1); |
Shunkai Yao | 074cf23 | 2023-07-14 21:08:31 +0000 | [diff] [blame] | 154 | LOG(VERBOSE) << mName << __func__ << ": done processing, effect consumed " |
| 155 | << status.fmqConsumed << " produced " << status.fmqProduced; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 159 | } // namespace aidl::android::hardware::audio::effect |