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 48 private: 49 int64_t CalcBufferDiff(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer, 50 int64_t bufferAnchoredClockTime, int64_t currentClockTime, float playbackRate); 51 float AdjustPlaybackRate(float speed); 52 int64_t SmoothDeltaTime(int64_t accumulatedDeltaTime, int64_t currentDeltaTime); 53 void UpdateTimeAnchorIfNeeded(int64_t nowCt, int64_t waitTime, 54 const std::shared_ptr<OHOS::Media::AVBuffer>& buffer); 55 void PerfRecord(int64_t waitTime); 56 57 class VideoLagDetector : public LagDetector { 58 public: 59 void Reset() override; 60 bool CalcLag(std::shared_ptr<AVBuffer> buffer) override; 61 void GetLagInfo(int32_t& lagTimes, int32_t& maxLagDuration, int32_t& avgLagDuration); 62 void ResolveLagEvent(const int64_t &lagTimeMs); 63 void SetEventReceiver(const std::shared_ptr<EventReceiver> eventReceiver); 64 private: 65 int64_t lagTimes_ = 0; 66 int64_t maxLagDuration_ = 0; 67 int64_t lastSystemTimeMs_ = 0; 68 int64_t lastBufferTimeMs_ = 0; 69 int64_t totalLagDuration_ = 0; 70 std::shared_ptr<EventReceiver> eventReceiver_ { nullptr }; 71 }; 72 73 std::atomic<bool> needUpdateTimeAnchor_ {true}; 74 int64_t refreshTime_ {0}; 75 bool isFirstFrame_ {true}; 76 int64_t firstFramePts_ {0}; 77 int64_t firstFrameClockTime_ {0}; 78 int64_t lastBufferRelativePts_ {HST_TIME_NONE}; 79 int64_t lastBufferAnchoredClockTime_ {HST_TIME_NONE}; 80 int64_t deltaTimeAccu_ {0}; 81 82 std::shared_ptr<OHOS::Media::Task> frameRateTask_ {nullptr}; 83 std::atomic<uint64_t> renderFrameCnt_ {0}; 84 std::atomic<uint64_t> discardFrameCnt_ {0}; 85 std::shared_ptr<EventReceiver> eventReceiver_ {nullptr}; 86 int64_t firstPts_ {HST_TIME_NONE}; 87 int64_t fixDelay_ {0}; 88 bool seekFlag_{false}; 89 bool isPerfRecEnabled_ { false }; 90 std::atomic<int32_t> dropFrameContinuouslyCnt_ {0}; 91 int64_t lastPts_ = -1; 92 int64_t lastClockTime_ = -1; 93 std::atomic<bool> isRenderStarted_{false}; 94 VideoLagDetector lagDetector_ {}; 95 PerfRecorder perfRecorder_ {}; 96 }; 97 } // namespace Pipeline 98 } // namespace Media 99 } // namespace OHOS 100 101 #endif // MEDIA_PIPELINE_VIDEO_SINK_FILTER_H