1 /*
2  * Copyright (c) 2023-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 MEDIA_PIPELINE_VIDEO_SINK_H
17 #define MEDIA_PIPELINE_VIDEO_SINK_H
18 
19 #include "osal/task/task.h"
20 #include "sink/media_synchronous_sink.h"
21 #include "buffer/avbuffer.h"
22 #include "common/status.h"
23 #include "meta/video_types.h"
24 #include "filter/filter.h"
25 
26 namespace OHOS {
27 namespace Media {
28 namespace Pipeline {
29 class VideoSink : public MediaSynchronousSink {
30 public:
31     VideoSink();
32     ~VideoSink();
33     int64_t DoSyncWrite(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer) override; // true and render
34     void ResetSyncInfo() override;
35     Status GetLatency(uint64_t& nanoSec);
36     int64_t CheckBufferLatenessMayWait(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer);
37     void SetSyncCenter(std::shared_ptr<MediaSyncManager> syncCenter);
38     void SetEventReceiver(const std::shared_ptr<EventReceiver> &receiver);
39     void SetFirstPts(int64_t pts);
40     void SetSeekFlag();
41     void SetLastPts(int64_t lastPts);
42     Status SetParameter(const std::shared_ptr<Meta>& meta);
43     void UpdateTimeAnchorActually(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer);
44     Status GetLagInfo(int32_t& lagTimes, int32_t& maxLagDuration, int32_t& avgLagDuration);
45 private:
46     float GetSpeed(float speed);
47 
48     class VideoLagDetector : public LagDetector {
49     public:
50         void Reset() override;
51         bool CalcLag(std::shared_ptr<AVBuffer> buffer) override;
52         void GetLagInfo(int32_t& lagTimes, int32_t& maxLagDuration, int32_t& avgLagDuration);
53         void ResolveLagEvent(const int64_t &lagTimeMs);
54         void SetEventReceiver(const std::shared_ptr<EventReceiver> eventReceiver);
55     private:
56         int64_t lagTimes_ = 0;
57         int64_t maxLagDuration_ = 0;
58         int64_t lastSystemTimeMs_ = 0;
59         int64_t lastBufferTimeMs_ = 0;
60         int64_t totalLagDuration_ = 0;
61         std::shared_ptr<EventReceiver> eventReceiver_ { nullptr };
62     };
63 
64     int64_t refreshTime_ {0};
65     bool isFirstFrame_ {true};
66     uint32_t frameRate_ {0};
67     bool forceRenderNextFrame_ {false};
68     int64_t firstFramePts_ {0};
69     int64_t firstFrameNowct_ {0};
70     int64_t lastTimeStamp_ {HST_TIME_NONE};
71     int64_t lastBufferTime_ {HST_TIME_NONE};
72     int64_t deltaTimeAccu_ {0};
73 
74     VideoScaleType videoScaleType_ {VideoScaleType::VIDEO_SCALE_TYPE_FIT};
75 
76     void CalcFrameRate();
77     std::shared_ptr<OHOS::Media::Task> frameRateTask_ {nullptr};
78     std::atomic<uint64_t> renderFrameCnt_ {0};
79     std::atomic<uint64_t> discardFrameCnt_ {0};
80     std::shared_ptr<EventReceiver> eventReceiver_ {nullptr};
81     int64_t firstPts_ {HST_TIME_NONE};
82     int64_t fixDelay_ {0};
83     bool seekFlag_{false};
84     std::atomic<bool> lastFrameDropped_ {false};
85     int64_t lastPts_ = -1;
86     int64_t lastClockTime_ = -1;
87     VideoLagDetector lagDetector_ {};
88 };
89 } // namespace Pipeline
90 } // namespace Media
91 } // namespace OHOS
92 
93 #endif // MEDIA_PIPELINE_VIDEO_SINK_FILTER_H
94