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); |
| 51 | mThreadContext = std::move(context); |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 52 | 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 Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 64 | } |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 65 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 66 | mThread = std::thread(&EffectThread::threadLoop, this); |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 67 | LOG(DEBUG) << mName << __func__ << " priority " << mPriority << " done"; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 68 | return RetCode::SUCCESS; |
| 69 | } |
| 70 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 71 | RetCode EffectThread::destroyThread() { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 72 | { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 73 | std::lock_guard lg(mThreadMutex); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 74 | mStop = mExit = true; |
| 75 | } |
| 76 | mCv.notify_one(); |
| 77 | |
| 78 | if (mThread.joinable()) { |
| 79 | mThread.join(); |
| 80 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 81 | |
| 82 | { |
| 83 | std::lock_guard lg(mThreadMutex); |
| 84 | mThreadContext.reset(); |
| 85 | } |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 86 | LOG(DEBUG) << mName << __func__; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 87 | return RetCode::SUCCESS; |
| 88 | } |
| 89 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 90 | RetCode EffectThread::startThread() { |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 91 | { |
| 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 102 | RetCode EffectThread::stopThread() { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 103 | { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 104 | std::lock_guard lg(mThreadMutex); |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 105 | mStop = true; |
| 106 | mCv.notify_one(); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 107 | } |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 108 | |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 109 | mEfGroup->wake(kEventFlagNotEmpty); |
| 110 | LOG(DEBUG) << mName << __func__; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 111 | return RetCode::SUCCESS; |
| 112 | } |
| 113 | |
| 114 | void EffectThread::threadLoop() { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 115 | pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str()); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 116 | setpriority(PRIO_PROCESS, 0, mPriority); |
| 117 | while (true) { |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 118 | /** |
| 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 134 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 138 | void EffectThread::process_l() { |
| 139 | RETURN_VALUE_IF(!mThreadContext, void(), "nullContext"); |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 140 | |
| 141 | auto statusMQ = mThreadContext->getStatusFmq(); |
| 142 | auto inputMQ = mThreadContext->getInputDataFmq(); |
| 143 | auto outputMQ = mThreadContext->getOutputDataFmq(); |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 144 | auto buffer = mThreadContext->getWorkBuffer(); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 145 | |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 146 | auto processSamples = inputMQ->availableToRead(); |
| 147 | if (processSamples) { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 148 | inputMQ->read(buffer, processSamples); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 149 | IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples); |
| 150 | outputMQ->write(buffer, status.fmqProduced); |
| 151 | statusMQ->writeBlocking(&status, 1); |
Shunkai Yao | 074cf23 | 2023-07-14 21:08:31 +0000 | [diff] [blame] | 152 | LOG(VERBOSE) << mName << __func__ << ": done processing, effect consumed " |
| 153 | << status.fmqConsumed << " produced " << status.fmqProduced; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 157 | } // namespace aidl::android::hardware::audio::effect |