From e4025a681103f511bb18034eddfe64752b624715 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sun, 13 Oct 2024 00:23:34 +0800 Subject: [PATCH] Improve timestamp corrector --- src/Common/Stamp.cpp | 19 +++++++++++++++++-- src/Common/Stamp.h | 9 +++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Common/Stamp.cpp b/src/Common/Stamp.cpp index 0913b7fa..2cc23dfc 100644 --- a/src/Common/Stamp.cpp +++ b/src/Common/Stamp.cpp @@ -27,6 +27,12 @@ DeltaStamp::DeltaStamp() { _max_delta = 300; } +void DeltaStamp::reset() { + _last_stamp = 0; + _relative_stamp = 0; + _last_delta = 1; +} + int64_t DeltaStamp::relativeStamp(int64_t stamp, bool enable_rollback) { _relative_stamp += deltaStamp(stamp, enable_rollback); return _relative_stamp; @@ -55,8 +61,9 @@ int64_t DeltaStamp::deltaStamp(int64_t stamp, bool enable_rollback) { // In the live broadcast case, the timestamp increment must not be greater than MAX_DELTA_STAMP, otherwise the relative timestamp is forced to add 1 if (ret > _max_delta) { needSync(); - return 1; + return _last_delta; } + _last_delta = ret; return ret; } @@ -67,7 +74,7 @@ int64_t DeltaStamp::deltaStamp(int64_t stamp, bool enable_rollback) { // 不允许回退或者回退太多了, 强制时间戳加1 [AUTO-TRANSLATED:152f5ffa] // Not allowed to retreat or retreat too much, force the timestamp to add 1 needSync(); - return 1; + return _last_delta; } return ret; } @@ -93,6 +100,14 @@ void Stamp::enableRollback(bool flag) { _enable_rollback = flag; } +void Stamp::reset() { + DeltaStamp::reset(); + _relative_stamp = 0; + _last_dts_in = 0; + _last_dts_out = 0; + _last_pts_out = 0; +} + // 限制dts回退 [AUTO-TRANSLATED:6bc53b31] // Limit dts retreat void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, bool modifyStamp) { diff --git a/src/Common/Stamp.h b/src/Common/Stamp.h index a21aacfa..beeadcb9 100644 --- a/src/Common/Stamp.h +++ b/src/Common/Stamp.h @@ -42,11 +42,15 @@ public: // Set the maximum allowed rollback or jump amplitude void setMaxDelta(size_t max_delta); + // 重置 + void reset(); + protected: virtual void needSync() {} protected: int _max_delta; + int _last_delta = 1; int64_t _last_stamp = 0; int64_t _relative_stamp = 0; }; @@ -123,6 +127,11 @@ public: */ void enableRollback(bool flag); + /** + * 重置 + */ + void reset(); + private: // 主要实现音视频时间戳同步功能 [AUTO-TRANSLATED:45863fce] // Mainly implements audio and video timestamp synchronization function