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 16 #ifndef HISTREAMER_FOUNDATION_OSAL_TASK_INNER_H 17 #define HISTREAMER_FOUNDATION_OSAL_TASK_INNER_H 18 19 #include <atomic> 20 #include <functional> 21 #include <string> 22 #include <list> 23 #include <map> 24 25 #include "osal/task/task.h" 26 #include "osal/task/condition_variable.h" 27 #include "osal/task/mutex.h" 28 #include "osal/task/autolock.h" 29 #include "osal/task/pipeline_threadpool.h" 30 31 #ifdef MEDIA_FOUNDATION_FFRT 32 #include "ffrt.h" 33 #else 34 #include <map> 35 #endif 36 37 38 namespace OHOS { 39 namespace Media { 40 41 class TaskInner : public std::enable_shared_from_this<TaskInner> { 42 public: TaskInner(const std::string & name,const std::string & groupId,TaskType type,TaskPriority priority,bool singleLoop)43 explicit TaskInner(const std::string& name, const std::string& groupId, TaskType type, 44 TaskPriority priority, bool singleLoop) 45 { 46 (void)name; 47 (void)groupId; 48 (void)type; 49 (void)priority; 50 (void)singleLoop; 51 } 52 ~TaskInner()53 virtual ~TaskInner() {} 54 Init()55 virtual void Init() {} 56 DeInit()57 virtual void DeInit() {} 58 Start()59 virtual void Start() {} 60 Stop()61 virtual void Stop() {} 62 StopAsync()63 virtual void StopAsync() {} 64 Pause()65 virtual void Pause() {} 66 PauseAsync()67 virtual void PauseAsync() {} 68 RegisterJob(const std::function<int64_t ()> & job)69 virtual void RegisterJob(const std::function<int64_t()>& job) 70 { 71 (void)job; 72 } 73 SubmitJobOnce(const std::function<void ()> & job,int64_t delay,bool wait)74 virtual void SubmitJobOnce(const std::function<void()>& job, int64_t delay, bool wait) 75 { 76 (void)job; 77 (void)delay; 78 (void)wait; 79 } 80 SubmitJob(const std::function<void ()> & job,int64_t delay,bool wait)81 virtual void SubmitJob(const std::function<void()>& job, int64_t delay, bool wait) 82 { 83 (void)job; 84 (void)delay; 85 (void)wait; 86 } 87 IsTaskRunning()88 virtual bool IsTaskRunning() { return runningState_ == RunningState::STARTED; } 89 UpdateDelayTime(int64_t delayUs)90 virtual void UpdateDelayTime(int64_t delayUs) 91 { 92 (void)delayUs; 93 } 94 SetEnableStateChangeLog(bool enable)95 void SetEnableStateChangeLog(bool enable) { isStateLogEnabled_ = enable; } 96 NextJobUs()97 int64_t NextJobUs() 98 { 99 return 0; 100 } 101 HandleJob()102 void HandleJob() {} 103 SleepInTask(unsigned ms)104 static void SleepInTask(unsigned ms) 105 { 106 (void)ms; 107 } 108 UpdateThreadPriority(const uint32_t newPriority,const std::string & strBundleName)109 void UpdateThreadPriority(const uint32_t newPriority, const std::string &strBundleName) 110 { 111 (void)newPriority; 112 (void)strBundleName; 113 } 114 115 private: 116 enum class RunningState : int { 117 STARTED, 118 PAUSING, 119 PAUSED, 120 STOPPING, 121 STOPPED, 122 }; 123 124 const std::string name_; 125 std::atomic<RunningState> runningState_{RunningState::PAUSED}; 126 std::atomic<bool> jobState_{false}; 127 std::function<int64_t()> job_; 128 bool singleLoop_ = false; 129 int64_t topProcessUs_ {-1}; 130 bool topIsJob_ = false; 131 std::shared_ptr<PipeLineThread> pipelineThread_; 132 std::atomic<bool> isStateLogEnabled_{true}; 133 #ifdef MEDIA_FOUNDATION_FFRT 134 void DoJob(const std::function<void()>& job); 135 std::shared_ptr<ffrt::queue> jobQueue_; 136 Mutex stateMutex_; 137 ConditionVariable syncCond_; 138 ffrt::recursive_mutex jobMutex_; 139 #else UpdateTop()140 void UpdateTop() {} 141 InsertJob(const std::function<void ()> & job,int64_t delayUs,bool inJobQueue)142 int64_t InsertJob(const std::function<void()>& job, int64_t delayUs, bool inJobQueue) 143 { 144 (void)job; 145 (void)delayUs; 146 (void)inJobQueue; 147 return 0; 148 } 149 150 Mutex stateMutex_{}; 151 FairMutex jobMutex_{}; 152 ConditionVariable syncCond_{}; 153 ConditionVariable replyCond_{}; 154 std::map<int64_t, std::function<void()>> msgQueue_; // msg will be sorted by timeUs 155 std::map<int64_t, std::function<void()>> jobQueue_; // msg will be sorted by timeUs 156 #endif 157 }; 158 } // namespace Media 159 } // namespace OHOS 160 #endif // HISTREAMER_FOUNDATION_OSAL_TASK_H 161 162