2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2019-08-02 18:06:37 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2019-08-02 18:06:37 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2019-08-02 18:06:37 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Stamp.h"
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 时间戳最大允许跳变3秒,主要是防止网络抖动导致的跳变
|
2022-06-18 21:08:34 +08:00
|
|
|
|
#define MAX_DELTA_STAMP (3 * 1000)
|
2021-07-13 17:30:54 +08:00
|
|
|
|
#define STAMP_LOOP_DELTA (60 * 1000)
|
2020-01-13 15:48:55 +08:00
|
|
|
|
#define MAX_CTS 500
|
2020-01-13 11:51:29 +08:00
|
|
|
|
#define ABS(x) ((x) > 0 ? (x) : (-x))
|
2019-12-16 16:17:52 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2019-08-02 18:06:37 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2023-11-25 10:43:51 +08:00
|
|
|
|
DeltaStamp::DeltaStamp() {
|
|
|
|
|
// 时间戳最大允许跳跃300ms
|
|
|
|
|
_max_delta = 300;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t DeltaStamp::relativeStamp(int64_t stamp, bool enable_rollback) {
|
|
|
|
|
_relative_stamp += deltaStamp(stamp, enable_rollback);
|
2023-03-22 21:18:46 +08:00
|
|
|
|
return _relative_stamp;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
int64_t DeltaStamp::relativeStamp() {
|
2023-03-22 21:18:46 +08:00
|
|
|
|
return _relative_stamp;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-25 10:43:51 +08:00
|
|
|
|
int64_t DeltaStamp::deltaStamp(int64_t stamp, bool enable_rollback) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
if (!_last_stamp) {
|
|
|
|
|
// 第一次计算时间戳增量,时间戳增量为0
|
|
|
|
|
if (stamp) {
|
2020-01-13 11:51:29 +08:00
|
|
|
|
_last_stamp = stamp;
|
|
|
|
|
}
|
2019-10-08 12:55:19 +08:00
|
|
|
|
return 0;
|
2019-08-22 14:56:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 12:55:19 +08:00
|
|
|
|
int64_t ret = stamp - _last_stamp;
|
2023-06-03 17:34:34 +08:00
|
|
|
|
if (ret >= 0) {
|
|
|
|
|
// 时间戳增量为正,返回之
|
2019-10-08 12:55:19 +08:00
|
|
|
|
_last_stamp = stamp;
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 在直播情况下,时间戳增量不得大于MAX_DELTA_STAMP,否则强制相对时间戳加1
|
2023-11-25 10:43:51 +08:00
|
|
|
|
if (ret > _max_delta) {
|
|
|
|
|
needSync();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
2019-08-02 18:06:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 时间戳增量为负,说明时间戳回环了或回退了
|
2019-10-08 12:55:19 +08:00
|
|
|
|
_last_stamp = stamp;
|
2023-11-25 10:43:51 +08:00
|
|
|
|
if (!enable_rollback || -ret > _max_delta) {
|
|
|
|
|
// 不允许回退或者回退太多了, 强制时间戳加1
|
|
|
|
|
needSync();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2023-06-13 17:39:43 +08:00
|
|
|
|
|
2023-11-25 10:43:51 +08:00
|
|
|
|
void DeltaStamp::setMaxDelta(size_t max_delta) {
|
|
|
|
|
_max_delta = max_delta;
|
2019-10-08 12:55:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-04 15:37:37 +08:00
|
|
|
|
void Stamp::setPlayBack(bool playback) {
|
2019-10-08 12:55:19 +08:00
|
|
|
|
_playback = playback;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
void Stamp::syncTo(Stamp &other) {
|
2023-11-25 10:43:51 +08:00
|
|
|
|
_need_sync = true;
|
2020-05-15 18:08:54 +08:00
|
|
|
|
_sync_master = &other;
|
2020-05-06 18:54:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-25 10:43:51 +08:00
|
|
|
|
void Stamp::needSync() {
|
|
|
|
|
_need_sync = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Stamp::enableRollback(bool flag) {
|
|
|
|
|
_enable_rollback = flag;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 限制dts回退
|
|
|
|
|
void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, bool modifyStamp) {
|
2020-08-30 09:15:39 +08:00
|
|
|
|
revise_l(dts, pts, dts_out, pts_out, modifyStamp);
|
|
|
|
|
if (_playback) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 回放允许时间戳回退
|
2020-08-30 09:15:39 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dts_out < _last_dts_out) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// WarnL << "dts回退:" << dts_out << " < " << _last_dts_out;
|
2020-08-30 09:15:39 +08:00
|
|
|
|
dts_out = _last_dts_out;
|
|
|
|
|
pts_out = _last_pts_out;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_last_dts_out = dts_out;
|
|
|
|
|
_last_pts_out = pts_out;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 音视频时间戳同步
|
|
|
|
|
void Stamp::revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, bool modifyStamp) {
|
2020-08-30 09:15:39 +08:00
|
|
|
|
revise_l2(dts, pts, dts_out, pts_out, modifyStamp);
|
|
|
|
|
if (!_sync_master || modifyStamp || _playback) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 自动生成时间戳或回放或同步完毕
|
2020-05-06 18:54:28 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-25 10:43:51 +08:00
|
|
|
|
// 需要同步时间戳
|
|
|
|
|
if (_sync_master && _sync_master->_last_dts_in && (_need_sync || _sync_master->_need_sync)) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 音视频dts当前时间差
|
2020-08-30 09:15:39 +08:00
|
|
|
|
int64_t dts_diff = _last_dts_in - _sync_master->_last_dts_in;
|
|
|
|
|
if (ABS(dts_diff) < 5000) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 如果绝对时间戳小于5秒,那么说明他们的起始时间戳是一致的,那么强制同步
|
2023-11-25 10:43:51 +08:00
|
|
|
|
auto target_stamp = _sync_master->_relative_stamp + dts_diff;
|
|
|
|
|
if (target_stamp > _relative_stamp || _enable_rollback) {
|
|
|
|
|
// 强制同步后,时间戳增加跳跃了,或允许回退
|
|
|
|
|
TraceL << "Relative stamp changed: " << _relative_stamp << " -> " << target_stamp;
|
|
|
|
|
_relative_stamp = target_stamp;
|
|
|
|
|
} else {
|
|
|
|
|
// 不允许回退, 则让另外一个Track的时间戳增长
|
|
|
|
|
target_stamp = _relative_stamp - dts_diff;
|
|
|
|
|
TraceL << "Relative stamp changed: " << _sync_master->_relative_stamp << " -> " << target_stamp;
|
|
|
|
|
_sync_master->_relative_stamp = target_stamp;
|
|
|
|
|
}
|
2020-05-06 18:54:28 +08:00
|
|
|
|
}
|
2023-11-25 10:43:51 +08:00
|
|
|
|
_need_sync = false;
|
|
|
|
|
_sync_master->_need_sync = false;
|
2020-05-06 18:54:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 求取相对时间戳
|
|
|
|
|
void Stamp::revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, bool modifyStamp) {
|
2020-08-30 09:15:39 +08:00
|
|
|
|
if (!pts) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 没有播放时间戳,使其赋值为解码时间戳
|
2019-10-08 12:55:19 +08:00
|
|
|
|
pts = dts;
|
2019-08-02 18:06:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 09:15:39 +08:00
|
|
|
|
if (_playback) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 这是点播
|
2020-04-04 15:37:37 +08:00
|
|
|
|
dts_out = dts;
|
|
|
|
|
pts_out = pts;
|
2020-08-30 09:15:39 +08:00
|
|
|
|
_relative_stamp = dts_out;
|
|
|
|
|
_last_dts_in = dts;
|
2020-04-04 15:37:37 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// pts和dts的差值
|
2021-01-19 16:05:38 +08:00
|
|
|
|
int64_t pts_dts_diff = pts - dts;
|
2019-09-30 16:51:17 +08:00
|
|
|
|
|
2020-08-30 09:15:39 +08:00
|
|
|
|
if (_last_dts_in != dts) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 时间戳发生变更
|
2020-08-30 09:15:39 +08:00
|
|
|
|
if (modifyStamp) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 内部自己生产时间戳
|
2020-08-30 09:15:39 +08:00
|
|
|
|
_relative_stamp = _ticker.elapsedTime();
|
|
|
|
|
} else {
|
2023-11-25 10:43:51 +08:00
|
|
|
|
_relative_stamp += deltaStamp(dts, _enable_rollback);
|
2019-12-16 15:49:52 +08:00
|
|
|
|
}
|
2020-08-30 09:15:39 +08:00
|
|
|
|
_last_dts_in = dts;
|
2019-11-29 09:54:48 +08:00
|
|
|
|
}
|
2020-08-30 09:15:39 +08:00
|
|
|
|
dts_out = _relative_stamp;
|
2019-08-02 18:06:37 +08:00
|
|
|
|
|
|
|
|
|
//////////////以下是播放时间戳的计算//////////////////
|
2020-08-30 09:15:39 +08:00
|
|
|
|
if (ABS(pts_dts_diff) > MAX_CTS) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 如果差值太大,则认为由于回环导致时间戳错乱了
|
2019-08-02 18:06:37 +08:00
|
|
|
|
pts_dts_diff = 0;
|
|
|
|
|
}
|
2019-10-08 12:55:19 +08:00
|
|
|
|
|
2019-08-02 18:06:37 +08:00
|
|
|
|
pts_out = dts_out + pts_dts_diff;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 12:55:19 +08:00
|
|
|
|
void Stamp::setRelativeStamp(int64_t relativeStamp) {
|
2020-08-30 09:15:39 +08:00
|
|
|
|
_relative_stamp = relativeStamp;
|
2019-10-08 12:55:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t Stamp::getRelativeStamp() const {
|
2020-08-30 09:15:39 +08:00
|
|
|
|
return _relative_stamp;
|
2019-09-03 17:20:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
bool DtsGenerator::getDts(uint64_t pts, uint64_t &dts) {
|
2020-01-14 10:04:24 +08:00
|
|
|
|
bool ret = false;
|
2020-04-29 18:19:51 +08:00
|
|
|
|
if (pts == _last_pts) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// pts未变,说明dts也不会变,返回上次dts
|
2020-04-29 18:19:51 +08:00
|
|
|
|
if (_last_dts) {
|
2020-01-14 10:04:24 +08:00
|
|
|
|
dts = _last_dts;
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
2020-04-29 18:19:51 +08:00
|
|
|
|
} else {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// pts变了,尝试计算dts
|
2020-04-29 18:19:51 +08:00
|
|
|
|
ret = getDts_l(pts, dts);
|
|
|
|
|
if (ret) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 获取到了dts,保存本次结果
|
2020-04-29 18:19:51 +08:00
|
|
|
|
_last_dts = dts;
|
|
|
|
|
}
|
2020-01-14 10:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 18:19:51 +08:00
|
|
|
|
if (!ret) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// pts排序列队长度还不知道,也就是不知道有没有B帧,
|
|
|
|
|
// 那么先强制dts == pts,这样可能导致有B帧的情况下,起始画面有几帧回退
|
2020-04-29 18:05:29 +08:00
|
|
|
|
dts = pts;
|
2020-01-14 10:04:24 +08:00
|
|
|
|
}
|
2020-04-29 18:19:51 +08:00
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 记录上次pts
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_last_pts = pts;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 该算法核心思想是对pts进行排序,排序好的pts就是dts。
|
|
|
|
|
// 排序有一定的滞后性,那么需要加上排序导致的时间戳偏移量
|
|
|
|
|
bool DtsGenerator::getDts_l(uint64_t pts, uint64_t &dts) {
|
|
|
|
|
if (_sorter_max_size == 1) {
|
|
|
|
|
// 没有B帧,dts就等于pts
|
2020-01-14 10:06:35 +08:00
|
|
|
|
dts = pts;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
if (!_sorter_max_size) {
|
|
|
|
|
// 尚未计算出pts排序列队长度(也就是P帧间B帧个数)
|
|
|
|
|
if (pts > _last_max_pts) {
|
|
|
|
|
// pts时间戳增加了,那么说明这帧画面不是B帧(说明是P帧或关键帧)
|
|
|
|
|
if (_frames_since_last_max_pts && _count_sorter_max_size++ > 0) {
|
|
|
|
|
// 已经出现多次非B帧的情况,那么我们就能知道P帧间B帧的个数
|
2020-01-14 10:25:14 +08:00
|
|
|
|
_sorter_max_size = _frames_since_last_max_pts;
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 我们记录P帧间时间间隔(也就是多个B帧时间戳增量累计)
|
2020-06-11 23:06:01 +08:00
|
|
|
|
_dts_pts_offset = (pts - _last_max_pts);
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 除以2,防止dts大于pts
|
2020-06-28 15:24:08 +08:00
|
|
|
|
_dts_pts_offset /= 2;
|
2020-01-14 10:25:14 +08:00
|
|
|
|
}
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 遇到P帧或关键帧,连续B帧计数清零
|
2020-01-14 10:25:14 +08:00
|
|
|
|
_frames_since_last_max_pts = 0;
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 记录上次非B帧的pts时间戳(同时也是dts),用于统计连续B帧时间戳增量
|
2020-01-14 10:25:14 +08:00
|
|
|
|
_last_max_pts = pts;
|
2020-01-14 10:04:24 +08:00
|
|
|
|
}
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 如果pts时间戳小于上一个P帧,那么断定这个是B帧,我们记录B帧连续个数
|
2020-01-14 10:25:14 +08:00
|
|
|
|
++_frames_since_last_max_pts;
|
2020-01-14 10:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// pts放入排序缓存列队,缓存列队最大等于连续B帧个数
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_pts_sorter.emplace(pts);
|
2020-04-29 18:05:29 +08:00
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
if (_sorter_max_size && _pts_sorter.size() > _sorter_max_size) {
|
|
|
|
|
// 如果启用了pts排序(意味着存在B帧),并且pts排序缓存列队长度大于连续B帧个数,
|
|
|
|
|
// 意味着后续的pts都会比最早的pts大,那么说明可以取出最早的pts了,这个pts将当做该帧的dts基准
|
2020-01-14 10:04:24 +08:00
|
|
|
|
auto it = _pts_sorter.begin();
|
2020-04-29 18:05:29 +08:00
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 由于该pts是前面偏移了个_sorter_max_size帧的pts(也就是那帧画面的dts),
|
|
|
|
|
// 那么我们加上时间戳偏移量,基本等于该帧的dts
|
2020-01-14 10:04:24 +08:00
|
|
|
|
dts = *it + _dts_pts_offset;
|
2023-06-03 17:34:34 +08:00
|
|
|
|
if (dts > pts) {
|
|
|
|
|
// dts不能大于pts(基本不可能到达这个逻辑)
|
2020-01-14 10:04:24 +08:00
|
|
|
|
dts = pts;
|
|
|
|
|
}
|
2020-04-29 18:05:29 +08:00
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// pts排序缓存出列
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_pts_sorter.erase(it);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-04-29 18:05:29 +08:00
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 排序缓存尚未满
|
2020-01-14 10:04:24 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-02 21:17:59 +08:00
|
|
|
|
void NtpStamp::setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) {
|
2023-06-17 10:13:29 +08:00
|
|
|
|
if (!ntp_stamp_ms || !rtp_stamp) {
|
|
|
|
|
// 实测发现有些rtsp服务器发送的rtp时间戳和ntp时间戳一直为0
|
|
|
|
|
WarnL << "Invalid sender report rtcp, ntp_stamp_ms = " << ntp_stamp_ms << ", rtp_stamp = " << rtp_stamp;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-21 17:20:39 +08:00
|
|
|
|
update(rtp_stamp, ntp_stamp_ms * 1000);
|
2021-07-22 20:31:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 17:20:39 +08:00
|
|
|
|
void NtpStamp::update(uint32_t rtp_stamp, uint64_t ntp_stamp_us) {
|
2021-09-02 21:17:59 +08:00
|
|
|
|
_last_rtp_stamp = rtp_stamp;
|
2023-06-21 17:20:39 +08:00
|
|
|
|
_last_ntp_stamp_us = ntp_stamp_us;
|
2021-07-12 21:18:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t NtpStamp::getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate) {
|
2021-07-13 10:10:23 +08:00
|
|
|
|
if (rtp_stamp == _last_rtp_stamp) {
|
2023-06-21 17:20:39 +08:00
|
|
|
|
return _last_ntp_stamp_us / 1000;
|
2021-07-13 10:10:23 +08:00
|
|
|
|
}
|
2023-06-21 17:20:39 +08:00
|
|
|
|
return getNtpStampUS(rtp_stamp, sample_rate) / 1000;
|
2021-07-22 20:31:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 17:20:39 +08:00
|
|
|
|
uint64_t NtpStamp::getNtpStampUS(uint32_t rtp_stamp, uint32_t sample_rate) {
|
|
|
|
|
if (!_last_ntp_stamp_us) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 尚未收到sender report rtcp包,那么赋值为本地系统时间戳吧
|
2023-06-21 17:20:39 +08:00
|
|
|
|
update(rtp_stamp, getCurrentMicrosecond(true));
|
2021-07-12 21:18:22 +08:00
|
|
|
|
}
|
2021-07-14 21:41:13 +08:00
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// rtp时间戳正增长
|
2021-09-02 21:17:59 +08:00
|
|
|
|
if (rtp_stamp >= _last_rtp_stamp) {
|
2023-06-21 17:20:39 +08:00
|
|
|
|
auto diff_us = static_cast<int64_t>((rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000000.0f));
|
|
|
|
|
if (diff_us < MAX_DELTA_STAMP * 1000) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 时间戳正常增长
|
2023-06-21 17:20:39 +08:00
|
|
|
|
update(rtp_stamp, _last_ntp_stamp_us + diff_us);
|
|
|
|
|
return _last_ntp_stamp_us;
|
2021-07-12 21:39:47 +08:00
|
|
|
|
}
|
2021-09-02 21:17:59 +08:00
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 时间戳大幅跳跃
|
2023-06-21 17:20:39 +08:00
|
|
|
|
uint64_t loop_delta_hz = STAMP_LOOP_DELTA * sample_rate / 1000;
|
|
|
|
|
if (_last_rtp_stamp < loop_delta_hz && rtp_stamp > UINT32_MAX - loop_delta_hz) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 应该是rtp时间戳溢出+乱序
|
2023-06-21 17:20:39 +08:00
|
|
|
|
uint64_t max_rtp_us = uint64_t(UINT32_MAX) * 1000000 / sample_rate;
|
|
|
|
|
return _last_ntp_stamp_us + diff_us - max_rtp_us;
|
2021-07-12 21:39:47 +08:00
|
|
|
|
}
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 不明原因的时间戳大幅跳跃,直接返回上次值
|
2021-07-22 20:31:02 +08:00
|
|
|
|
WarnL << "rtp stamp abnormal increased:" << _last_rtp_stamp << " -> " << rtp_stamp;
|
2023-06-21 17:20:39 +08:00
|
|
|
|
update(rtp_stamp, _last_ntp_stamp_us);
|
|
|
|
|
return _last_ntp_stamp_us;
|
2021-07-12 21:18:22 +08:00
|
|
|
|
}
|
2021-09-02 21:17:59 +08:00
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// rtp时间戳负增长
|
2023-06-21 17:20:39 +08:00
|
|
|
|
auto diff_us = static_cast<int64_t>((_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000000.0f));
|
|
|
|
|
if (diff_us < MAX_DELTA_STAMP * 1000) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 正常范围的时间戳回退,说明收到rtp乱序了
|
2023-06-21 17:20:39 +08:00
|
|
|
|
return _last_ntp_stamp_us - diff_us;
|
2021-07-12 21:18:22 +08:00
|
|
|
|
}
|
2021-09-02 21:17:59 +08:00
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 时间戳大幅度回退
|
2023-06-21 17:20:39 +08:00
|
|
|
|
uint64_t loop_delta_hz = STAMP_LOOP_DELTA * sample_rate / 1000;
|
|
|
|
|
if (rtp_stamp < loop_delta_hz && _last_rtp_stamp > UINT32_MAX - loop_delta_hz) {
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 确定是时间戳溢出
|
2023-06-21 17:20:39 +08:00
|
|
|
|
uint64_t max_rtp_us = uint64_t(UINT32_MAX) * 1000000 / sample_rate;
|
|
|
|
|
update(rtp_stamp, _last_ntp_stamp_us + (max_rtp_us - diff_us));
|
|
|
|
|
return _last_ntp_stamp_us;
|
2021-07-12 21:18:22 +08:00
|
|
|
|
}
|
2023-06-03 17:34:34 +08:00
|
|
|
|
// 不明原因的时间戳回退,直接返回上次值
|
2021-07-22 20:31:02 +08:00
|
|
|
|
WarnL << "rtp stamp abnormal reduced:" << _last_rtp_stamp << " -> " << rtp_stamp;
|
2023-06-21 17:20:39 +08:00
|
|
|
|
update(rtp_stamp, _last_ntp_stamp_us);
|
|
|
|
|
return _last_ntp_stamp_us;
|
2021-07-12 21:18:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 17:34:34 +08:00
|
|
|
|
} // namespace mediakit
|