1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "hpae_signal_process_thread.h" 16 #include "audio_qosmanager.h" 17 #include "parameter.h" 18 19 namespace OHOS { 20 namespace AudioStandard { 21 namespace HPAE { ~HpaeSignalProcessThread()22HpaeSignalProcessThread::~HpaeSignalProcessThread() 23 { 24 DeactivateThread(); 25 } 26 ActivateThread(const std::weak_ptr<HpaeStreamManager> & streamManager)27void HpaeSignalProcessThread::ActivateThread(const std::weak_ptr<HpaeStreamManager> &streamManager) 28 { 29 streamManager_ = streamManager; 30 running_.store(true); 31 auto threadFunc = std::bind(&HpaeSignalProcessThread::Run, this); 32 thread_ = std::thread(threadFunc); 33 if (streamManager_.lock() != nullptr) { 34 pthread_setname_np(thread_.native_handle(), streamManager_.lock()->GetThreadName().c_str()); 35 } 36 } 37 DeactivateThread()38void HpaeSignalProcessThread::DeactivateThread() 39 { 40 running_.store(false); 41 Notify(); 42 if (thread_.joinable()) { 43 thread_.join(); 44 } 45 } 46 Notify()47void HpaeSignalProcessThread::Notify() 48 { 49 std::unique_lock<std::mutex> lock(mutex_); 50 recvSignal_.store(true); 51 condition_.notify_all(); 52 } 53 Run()54void HpaeSignalProcessThread::Run() 55 { 56 int32_t setPriority = GetIntParameter("const.multimedia.audio_setPriority", 1); 57 SetThreadQosLevelAsync(setPriority); 58 while (running_.load() && streamManager_.lock() != nullptr) { 59 { 60 std::unique_lock<std::mutex> lock(mutex_); 61 condition_.wait(lock, [this] { 62 return !running_.load() || streamManager_.lock()->IsRunning() || 63 streamManager_.lock()->IsMsgProcessing() || recvSignal_.load(); 64 }); 65 } 66 if (streamManager_.lock()) { 67 streamManager_.lock()->HandleMsg(); 68 streamManager_.lock()->Process(); 69 } 70 recvSignal_.store(false); 71 } 72 ResetThreadQosLevel(); 73 } 74 75 } // namespace HPAE 76 } // namespace AudioStandard 77 } // namespace OHOS 78