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 | |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 17 | #include <memory> |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 18 | #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 | |
| 25 | namespace aidl::android::hardware::audio::effect { |
| 26 | |
| 27 | EffectThread::EffectThread() { |
| 28 | LOG(DEBUG) << __func__; |
| 29 | } |
| 30 | |
| 31 | EffectThread::~EffectThread() { |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 32 | destroyThread(); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 33 | LOG(DEBUG) << __func__ << " done"; |
| 34 | }; |
| 35 | |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 36 | RetCode EffectThread::createThread(std::shared_ptr<EffectContext> context, const std::string& name, |
| 37 | const int priority) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 38 | if (mThread.joinable()) { |
| 39 | LOG(WARNING) << __func__ << " thread already created, no-op"; |
| 40 | return RetCode::SUCCESS; |
| 41 | } |
| 42 | mName = name; |
| 43 | mPriority = priority; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 44 | { |
| 45 | std::lock_guard lg(mThreadMutex); |
| 46 | mThreadContext = std::move(context); |
| 47 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 48 | mThread = std::thread(&EffectThread::threadLoop, this); |
| 49 | LOG(DEBUG) << __func__ << " " << name << " priority " << mPriority << " done"; |
| 50 | return RetCode::SUCCESS; |
| 51 | } |
| 52 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 53 | RetCode EffectThread::destroyThread() { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 54 | { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 55 | std::lock_guard lg(mThreadMutex); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 56 | mStop = mExit = true; |
| 57 | } |
| 58 | mCv.notify_one(); |
| 59 | |
| 60 | if (mThread.joinable()) { |
| 61 | mThread.join(); |
| 62 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 63 | |
| 64 | { |
| 65 | std::lock_guard lg(mThreadMutex); |
| 66 | mThreadContext.reset(); |
| 67 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 68 | LOG(DEBUG) << __func__ << " done"; |
| 69 | return RetCode::SUCCESS; |
| 70 | } |
| 71 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 72 | RetCode EffectThread::startThread() { |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 73 | return handleStartStop(false /* stop */); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 76 | RetCode EffectThread::stopThread() { |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 77 | return handleStartStop(true /* stop */); |
| 78 | } |
| 79 | |
| 80 | RetCode EffectThread::handleStartStop(bool stop) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 81 | if (!mThread.joinable()) { |
| 82 | LOG(ERROR) << __func__ << " thread already destroyed"; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 83 | return RetCode::ERROR_THREAD; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 87 | std::lock_guard lg(mThreadMutex); |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 88 | if (stop == mStop) { |
| 89 | LOG(WARNING) << __func__ << " already " << stop ? "stop" : "start"; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 90 | return RetCode::SUCCESS; |
| 91 | } |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 92 | mStop = stop; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 93 | } |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 94 | |
| 95 | mCv.notify_one(); |
| 96 | LOG(DEBUG) << stop ? "stop done" : "start done"; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 97 | return RetCode::SUCCESS; |
| 98 | } |
| 99 | |
| 100 | void EffectThread::threadLoop() { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 101 | pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str()); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 102 | setpriority(PRIO_PROCESS, 0, mPriority); |
| 103 | while (true) { |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 104 | 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 108 | LOG(WARNING) << __func__ << " EXIT!"; |
| 109 | return; |
| 110 | } |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 111 | process_l(); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Shunkai Yao | b49631f | 2023-02-03 01:44:32 +0000 | [diff] [blame] | 115 | void 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 Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 121 | |
| 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 Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 131 | 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 141 | } // namespace aidl::android::hardware::audio::effect |