1 /*
2  * Copyright (c) 2022 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 #ifndef OHOS_FPS_CONTROLLER_PROCESS_H
16 #define OHOS_FPS_CONTROLLER_PROCESS_H
17 
18 #include <cstdint>
19 #include <vector>
20 
21 #include "abstract_data_process.h"
22 #include "dcamera_pipeline_source.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 class DCameraPipelineSource;
27 
28 class FpsControllerProcess : public AbstractDataProcess {
29 public:
FpsControllerProcess(const std::weak_ptr<DCameraPipelineSource> & callbackPipSource)30     explicit FpsControllerProcess(const std::weak_ptr<DCameraPipelineSource>& callbackPipSource)
31         : callbackPipelineSource_(callbackPipSource) {}
32     ~FpsControllerProcess() override;
33 
34     int32_t InitNode(const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig,
35         VideoConfigParams& processedConfig) override;
36     int32_t ProcessData(std::vector<std::shared_ptr<DataBuffer>>& inputBuffers) override;
37     void ReleaseProcessNode() override;
38 
39     int32_t GetProperty(const std::string& propertyName, PropertyCarrier& propertyCarrier) override;
40 
41 private:
42     void UpdateFPSControllerInfo(int64_t nowMs);
43     void UpdateFrameRateCorrectionFactor(int64_t nowMs);
44     void UpdateIncomingFrameTimes(int64_t nowMs);
45     float CalculateFrameRate(int64_t nowMs);
46     bool IsDropFrame(float incomingFps);
47     bool ReduceFrameRateByUniformStrategy(int32_t incomingFps);
48     int32_t FpsControllerDone(std::vector<std::shared_ptr<DataBuffer>>& outputBuffers);
49 
50 private:
51     constexpr static int32_t MAX_TARGET_FRAME_RATE = 30;
52     constexpr static int32_t VIDEO_FRAME_DROP_INTERVAL = 4;
53     constexpr static int32_t MIN_INCOME_FRAME_NUM_COEFFICIENT = 3;
54     constexpr static int32_t INCOME_FRAME_TIME_HISTORY_WINDOWS_SIZE = 60;
55     /* Receive video frame detect time windows */
56     constexpr static int32_t FRAME_HISTORY_TIME_WINDOWS_MS = 2000;
57     constexpr static int64_t FRMAE_MAX_INTERVAL_TIME_WINDOW_MS = 700;
58     constexpr static int32_t OVERSHOOT_MODIFY_COEFFICIENT = 3;
59     constexpr static int32_t DOUBLE_MULTIPLE = 2;
60 
61     std::weak_ptr<DCameraPipelineSource> callbackPipelineSource_;
62     std::mutex mtx;
63     VideoConfigParams sourceConfig_;
64     VideoConfigParams targetConfig_;
65     VideoConfigParams processedConfig_;
66     bool isFpsControllerProcess_ = false;
67     bool isFirstFrame_ = false;
68     int32_t targetFrameRate_ = 0;
69     int64_t lastFrameIncomeTimeMs_ = 0;
70     /* the time span between current and last frame */
71     int64_t recentFrameTimeSpanMs_ = -1;
72     int32_t keepCorrectionCount_ = 0;
73     int32_t keepLessThanDoubleCount_ = 0;
74     int32_t keepMoreThanDoubleCount_ = 0;
75     float frameRateCorrectionFactor_ = 0.0;
76     /* modify the frame rate controller argument */
77     int32_t frameRateOvershootMdf_ = 0;
78     int64_t incomingFrameTimesMs_[INCOME_FRAME_TIME_HISTORY_WINDOWS_SIZE] = { 0 };
79 };
80 } // namespace DistributedHardware
81 } // namespace OHOS
82 #endif // OHOS_FPS_CONTROLLER_PROCESS_H