• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 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 #include "performance_utils.h"
26 
27 namespace OHOS {
28 namespace Media {
29 namespace Pipeline {
30 class VideoSink : public MediaSynchronousSink {
31 public:
32     VideoSink();
33     ~VideoSink();
34     int64_t DoSyncWrite(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer) override; // true and render
35     void ResetSyncInfo() override;
36     Status GetLatency(uint64_t& nanoSec);
37     int64_t CheckBufferLatenessMayWait(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer);
38     void SetSyncCenter(std::shared_ptr<MediaSyncManager> syncCenter);
39     void SetEventReceiver(const std::shared_ptr<EventReceiver> &receiver);
40     void SetFirstPts(int64_t pts);
41     void SetSeekFlag();
42     void SetLastPts(int64_t lastPts, int64_t renderDelay = 0);
43     Status SetParameter(const std::shared_ptr<Meta>& meta);
44     void UpdateTimeAnchorActually(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer, int64_t renderDelay = 0);
45     Status GetLagInfo(int32_t& lagTimes, int32_t& maxLagDuration, int32_t& avgLagDuration);
46     Status SetPerfRecEnabled(bool isPerfRecEnabled);
47     void SetMediaMuted(bool isMuted);
48 
49 private:
50     int64_t CalcBufferDiff(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer,
51         int64_t bufferAnchoredClockTime, int64_t currentClockTime, float playbackRate);
52     float AdjustPlaybackRate(float speed);
53     int64_t SmoothDeltaTime(int64_t accumulatedDeltaTime, int64_t currentDeltaTime);
54     void UpdateTimeAnchorIfNeeded(int64_t nowCt, int64_t waitTime,
55         const std::shared_ptr<OHOS::Media::AVBuffer>& buffer);
56     void RenderAtTimeLog(int64_t waitTime);
57     void PerfRecord(int64_t waitTime);
58     void ReportPts(int64_t nowPts);
59     void InitWaitPeriod();
60 
61     class VideoLagDetector : public LagDetector {
62     public:
63         void Reset() override;
64         bool CalcLag(std::shared_ptr<AVBuffer> buffer) override;
65         void GetLagInfo(int32_t& lagTimes, int32_t& maxLagDuration, int32_t& avgLagDuration);
66         void ResolveLagEvent(const int64_t &lagTimeMs);
67         void SetEventReceiver(const std::shared_ptr<EventReceiver> eventReceiver);
68     private:
69         int64_t lagTimes_ = 0;
70         int64_t maxLagDuration_ = 0;
71         int64_t lastSystemTimeMs_ = 0;
72         int64_t lastBufferTimeMs_ = 0;
73         int64_t totalLagDuration_ = 0;
74         std::shared_ptr<EventReceiver> eventReceiver_ { nullptr };
75     };
76 
77     std::atomic<bool> needUpdateTimeAnchor_ {true};
78     int64_t refreshTime_ {0};
79     bool isFirstFrame_ {true};
80     int64_t firstFramePts_ {0};
81     int64_t firstFrameClockTime_ {0};
82     int64_t lastBufferRelativePts_ {HST_TIME_NONE};
83     int64_t lastBufferAnchoredClockTime_ {HST_TIME_NONE};
84     int64_t deltaTimeAccu_ {0};
85 
86     int64_t initialVideoWaitPeriod_ {0};
87 
88     std::shared_ptr<OHOS::Media::Task> frameRateTask_ {nullptr};
89     std::atomic<uint64_t> renderFrameCnt_ {0};
90     std::atomic<uint64_t> discardFrameCnt_ {0};
91     std::shared_ptr<EventReceiver> eventReceiver_ {nullptr};
92     int64_t firstPts_ {HST_TIME_NONE};
93     int64_t fixDelay_ {0};
94     bool seekFlag_{false};
95     bool isPerfRecEnabled_ { false };
96     std::atomic<int32_t> dropFrameContinuouslyCnt_ {0};
97     int64_t lastPts_ = -1;
98     int64_t lastClockTime_ = -1;
99     std::atomic<bool> isRenderStarted_{false};
100     VideoLagDetector lagDetector_ {};
101     int64_t renderAdvanceThreshold_ {80000};
102     bool enableRenderAtTime_ {true};
103     PerfRecorder perfRecorder_ {};
104     bool isMuted_ = false;
105     std::atomic<bool> needDropOnMute_ {false};
106 };
107 } // namespace Pipeline
108 } // namespace Media
109 } // namespace OHOS
110 
111 #endif // MEDIA_PIPELINE_VIDEO_SINK_FILTER_H