1fa7767c5Sopenharmony_ci/* 2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License. 5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at 6fa7767c5Sopenharmony_ci * 7fa7767c5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fa7767c5Sopenharmony_ci * 9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and 13fa7767c5Sopenharmony_ci * limitations under the License. 14fa7767c5Sopenharmony_ci */ 15fa7767c5Sopenharmony_ci 16fa7767c5Sopenharmony_ci#ifndef HISTREAMER_FOUNDATION_OSAL_TASK_INNER_H 17fa7767c5Sopenharmony_ci#define HISTREAMER_FOUNDATION_OSAL_TASK_INNER_H 18fa7767c5Sopenharmony_ci 19fa7767c5Sopenharmony_ci#include <atomic> 20fa7767c5Sopenharmony_ci#include <functional> 21fa7767c5Sopenharmony_ci#include <string> 22fa7767c5Sopenharmony_ci#include <list> 23fa7767c5Sopenharmony_ci#include <map> 24fa7767c5Sopenharmony_ci#include "osal/task/condition_variable.h" 25fa7767c5Sopenharmony_ci#include "osal/task/mutex.h" 26fa7767c5Sopenharmony_ci#include "osal/task/autolock.h" 27fa7767c5Sopenharmony_ci#include "osal/task/pipeline_threadpool.h" 28fa7767c5Sopenharmony_ci 29fa7767c5Sopenharmony_ci#ifdef MEDIA_FOUNDATION_FFRT 30fa7767c5Sopenharmony_ci #include "ffrt.h" 31fa7767c5Sopenharmony_ci#else 32fa7767c5Sopenharmony_ci #include <map> 33fa7767c5Sopenharmony_ci#endif 34fa7767c5Sopenharmony_ci 35fa7767c5Sopenharmony_ci 36fa7767c5Sopenharmony_cinamespace OHOS { 37fa7767c5Sopenharmony_cinamespace Media { 38fa7767c5Sopenharmony_ci 39fa7767c5Sopenharmony_ciclass TaskInner : public std::enable_shared_from_this<TaskInner> { 40fa7767c5Sopenharmony_cipublic: 41fa7767c5Sopenharmony_ci explicit TaskInner(const std::string& name, const std::string& groupId, TaskType type, 42fa7767c5Sopenharmony_ci TaskPriority priority, bool singleLoop); 43fa7767c5Sopenharmony_ci 44fa7767c5Sopenharmony_ci virtual ~TaskInner(); 45fa7767c5Sopenharmony_ci 46fa7767c5Sopenharmony_ci virtual void Init(); 47fa7767c5Sopenharmony_ci 48fa7767c5Sopenharmony_ci virtual void DeInit(); 49fa7767c5Sopenharmony_ci 50fa7767c5Sopenharmony_ci virtual void Start(); 51fa7767c5Sopenharmony_ci 52fa7767c5Sopenharmony_ci virtual void Stop(); 53fa7767c5Sopenharmony_ci 54fa7767c5Sopenharmony_ci virtual void StopAsync(); 55fa7767c5Sopenharmony_ci 56fa7767c5Sopenharmony_ci virtual void Pause(); 57fa7767c5Sopenharmony_ci 58fa7767c5Sopenharmony_ci virtual void PauseAsync(); 59fa7767c5Sopenharmony_ci 60fa7767c5Sopenharmony_ci virtual void RegisterJob(const std::function<int64_t()>& job); 61fa7767c5Sopenharmony_ci 62fa7767c5Sopenharmony_ci virtual void SubmitJobOnce(const std::function<void()>& job, int64_t delay, bool wait); 63fa7767c5Sopenharmony_ci 64fa7767c5Sopenharmony_ci virtual void SubmitJob(const std::function<void()>& job, int64_t delay, bool wait); 65fa7767c5Sopenharmony_ci 66fa7767c5Sopenharmony_ci virtual bool IsTaskRunning() { return runningState_ == RunningState::STARTED; } 67fa7767c5Sopenharmony_ci 68fa7767c5Sopenharmony_ci virtual void UpdateDelayTime(int64_t delayUs); 69fa7767c5Sopenharmony_ci 70fa7767c5Sopenharmony_ci void SetEnableStateChangeLog(bool enable) { isStateLogEnabled_ = enable; } 71fa7767c5Sopenharmony_ci 72fa7767c5Sopenharmony_ci int64_t NextJobUs(); 73fa7767c5Sopenharmony_ci 74fa7767c5Sopenharmony_ci void HandleJob(); 75fa7767c5Sopenharmony_ci 76fa7767c5Sopenharmony_ci static void SleepInTask(unsigned ms); 77fa7767c5Sopenharmony_ci 78fa7767c5Sopenharmony_ciprivate: 79fa7767c5Sopenharmony_ci enum class RunningState : int { 80fa7767c5Sopenharmony_ci STARTED, 81fa7767c5Sopenharmony_ci PAUSING, 82fa7767c5Sopenharmony_ci PAUSED, 83fa7767c5Sopenharmony_ci STOPPING, 84fa7767c5Sopenharmony_ci STOPPED, 85fa7767c5Sopenharmony_ci }; 86fa7767c5Sopenharmony_ci 87fa7767c5Sopenharmony_ci const std::string name_; 88fa7767c5Sopenharmony_ci std::atomic<RunningState> runningState_{RunningState::PAUSED}; 89fa7767c5Sopenharmony_ci std::atomic<bool> jobState_{false}; 90fa7767c5Sopenharmony_ci std::function<int64_t()> job_; 91fa7767c5Sopenharmony_ci bool singleLoop_ = false; 92fa7767c5Sopenharmony_ci int64_t topProcessUs_ {-1}; 93fa7767c5Sopenharmony_ci bool topIsJob_ = false; 94fa7767c5Sopenharmony_ci std::shared_ptr<PipeLineThread> pipelineThread_; 95fa7767c5Sopenharmony_ci std::atomic<bool> isStateLogEnabled_{true}; 96fa7767c5Sopenharmony_ci#ifdef MEDIA_FOUNDATION_FFRT 97fa7767c5Sopenharmony_ci void DoJob(const std::function<void()>& job); 98fa7767c5Sopenharmony_ci std::shared_ptr<ffrt::queue> jobQueue_; 99fa7767c5Sopenharmony_ci Mutex stateMutex_; 100fa7767c5Sopenharmony_ci ConditionVariable syncCond_; 101fa7767c5Sopenharmony_ci ffrt::recursive_mutex jobMutex_; 102fa7767c5Sopenharmony_ci#else 103fa7767c5Sopenharmony_ci void UpdateTop(); 104fa7767c5Sopenharmony_ci 105fa7767c5Sopenharmony_ci int64_t InsertJob(const std::function<void()>& job, int64_t delayUs, bool inJobQueue); 106fa7767c5Sopenharmony_ci 107fa7767c5Sopenharmony_ci Mutex stateMutex_{}; 108fa7767c5Sopenharmony_ci FairMutex jobMutex_{}; 109fa7767c5Sopenharmony_ci ConditionVariable syncCond_{}; 110fa7767c5Sopenharmony_ci ConditionVariable replyCond_{}; 111fa7767c5Sopenharmony_ci std::map<int64_t, std::function<void()>> msgQueue_; // msg will be sorted by timeUs 112fa7767c5Sopenharmony_ci std::map<int64_t, std::function<void()>> jobQueue_; // msg will be sorted by timeUs 113fa7767c5Sopenharmony_ci#endif 114fa7767c5Sopenharmony_ci}; 115fa7767c5Sopenharmony_ci} // namespace Media 116fa7767c5Sopenharmony_ci} // namespace OHOS 117fa7767c5Sopenharmony_ci#endif // HISTREAMER_FOUNDATION_OSAL_TASK_H 118fa7767c5Sopenharmony_ci 119