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); 43 Status SetParameter(const std::shared_ptr<Meta>& meta); 44 void UpdateTimeAnchorActually(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer); 45 Status GetLagInfo(int32_t& lagTimes, int32_t& maxLagDuration, int32_t& avgLagDuration); 46 Status SetPerfRecEnabled(bool isPerfRecEnabled); 47 48 private: 49 float GetSpeed(float speed); 50 void PerfRecord(int64_t waitTime); 51 52 class VideoLagDetector : public LagDetector { 53 public: 54 void Reset() override; 55 bool CalcLag(std::shared_ptr<AVBuffer> buffer) override; 56 void GetLagInfo(int32_t& lagTimes, int32_t& maxLagDuration, int32_t& avgLagDuration); 57 void ResolveLagEvent(const int64_t &lagTimeMs); 58 void SetEventReceiver(const std::shared_ptr<EventReceiver> eventReceiver); 59 private: 60 int64_t lagTimes_ = 0; 61 int64_t maxLagDuration_ = 0; 62 int64_t lastSystemTimeMs_ = 0; 63 int64_t lastBufferTimeMs_ = 0; 64 int64_t totalLagDuration_ = 0; 65 std::shared_ptr<EventReceiver> eventReceiver_ { nullptr }; 66 }; 67 68 int64_t refreshTime_ {0}; 69 bool isFirstFrame_ {true}; 70 int64_t firstFramePts_ {0}; 71 int64_t firstFrameNowct_ {0}; 72 int64_t lastTimeStamp_ {HST_TIME_NONE}; 73 int64_t lastBufferTime_ {HST_TIME_NONE}; 74 int64_t deltaTimeAccu_ {0}; 75 76 std::shared_ptr<OHOS::Media::Task> frameRateTask_ {nullptr}; 77 std::atomic<uint64_t> renderFrameCnt_ {0}; 78 std::atomic<uint64_t> discardFrameCnt_ {0}; 79 std::shared_ptr<EventReceiver> eventReceiver_ {nullptr}; 80 int64_t firstPts_ {HST_TIME_NONE}; 81 int64_t fixDelay_ {0}; 82 bool seekFlag_{false}; 83 std::atomic<bool> lastFrameDropped_ {false}; 84 bool isPerfRecEnabled_ { false }; 85 int64_t lastPts_ = -1; 86 int64_t lastClockTime_ = -1; 87 VideoLagDetector lagDetector_ {}; 88 PerfRecorder perfRecorder_ {}; 89 }; 90 } // namespace Pipeline 91 } // namespace Media 92 } // namespace OHOS 93 94 #endif // MEDIA_PIPELINE_VIDEO_SINK_FILTER_H 95