1 /* 2 * Copyright (c) 2023 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 OHOS_IFEEDING_SMOOTHER_H 17 #define OHOS_IFEEDING_SMOOTHER_H 18 19 #include <condition_variable> 20 #include <mutex> 21 #include <queue> 22 #include <thread> 23 #include <memory> 24 #include "feeding_smoother_listener.h" 25 #include "ifeedable_data.h" 26 #include "time_statistician.h" 27 #include "smoother_constants.h" 28 29 namespace OHOS { 30 namespace DistributedHardware { 31 class IFeedingSmoother { 32 public: 33 virtual ~IFeedingSmoother(); 34 virtual void PrepareSmooth() = 0; 35 virtual void InitBaseline(const int64_t timeStampBaseline, const int64_t clockBaseline); 36 virtual void InitTimeStatistician(); 37 virtual int32_t NotifySmoothFinished(const std::shared_ptr<IFeedableData>& data); 38 virtual void SetClockTime(const int64_t clockTime); 39 40 public: 41 void PushData(const std::shared_ptr<IFeedableData>& data); 42 int32_t StartSmooth(); 43 int32_t StopSmooth(); 44 void RegisterListener(const std::shared_ptr<FeedingSmootherListener>& listener); 45 void UnregisterListener(); 46 void SetBufferTime(const int32_t time); 47 void SetAverIntervalDiffThre(const uint32_t thre); 48 void SetDynamicBalanceThre(const uint8_t thre); 49 void SetFeedOnceDiffThre(const uint32_t thre); 50 void SetTimeStampBaseline(const int64_t timeStmapBaseline); 51 void SetClockBaseline(const int64_t clockBaseline); 52 void SetBaselineInitState(const bool state); 53 void SetProcessDynamicBalanceState(const bool state); 54 void SetTimeInitState(const bool state); 55 void SetAdjustSleepFactor(const float factor); 56 void SetWaitClockFactor(const float factor); 57 void SetTrackClockFactor(const float factor); 58 int64_t GetBufferTime(); 59 int64_t GetClockTime(); 60 61 private: 62 void AdjustSleepTime(const int64_t interval); 63 bool CheckIsBaselineInit(); 64 bool CheckIsTimeInit(); 65 bool CheckIsProcessInDynamicBalance(); 66 bool CheckIsProcessInDynamicBalanceOnce(); 67 void LooperSmooth(); 68 void RecordTime(const int64_t enterTime, const int64_t timeStamp); 69 void SmoothFeeding(const std::shared_ptr<IFeedableData>& data); 70 void SyncClock(const int64_t timeStamp, const int64_t timeStampInterval, const int64_t clock); 71 72 protected: 73 std::queue<std::shared_ptr<IFeedableData>> dataQueue_; 74 std::shared_ptr<TimeStatistician> statistician_ = nullptr; 75 std::shared_ptr<FeedingSmootherListener> listener_ = nullptr; 76 77 private: 78 SmoothState state_ = SMOOTH_STOP; 79 std::thread smoothThread_; 80 std::condition_variable smoothCon_; 81 std::condition_variable sleepCon_; 82 std::mutex queueMutex_; 83 std::mutex sleepMutex_; 84 std::mutex stateMutex_; 85 std::atomic<bool> isInDynamicBalance_ = true; 86 std::atomic<bool> isBaselineInit_ = false; 87 std::atomic<bool> isTimeInit_ = false; 88 89 float adjustSleepFactor_ = 0.1; 90 float waitClockFactor_ = 0.1; 91 float trackClockFactor_ = 0.2; 92 uint8_t dynamicBalanceThre_ = 0; 93 uint8_t dynamicBalanceCount_ = 0; 94 uint32_t averIntervalDiffThre_ = 0; 95 uint32_t feedOnceDiffThre_ = 0; 96 int64_t bufferTime_ = 0; 97 int64_t lastEnterTime_ = 0; 98 int64_t lastTimeStamp_ = 0; 99 int64_t leaveTime_ = 0; 100 int64_t timeStampBaseline_ = 0; 101 int64_t clockTime_ = 0; 102 int64_t clockBaseline_ = 0; 103 int64_t delta_ = 0; 104 int64_t sleep_ = 0; 105 }; 106 } // namespace DistributedHardware 107 } // namespace OHOS 108 #endif // OHOS_IFEEDING_SMOOTHER_H