Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
24d210f6
Unverified
Commit
24d210f6
authored
Jun 21, 2023
by
Luosh
Committed by
GitHub
Jun 21, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复NTP时间戳计算精度不足导致误差累积问题 (#2576 #2570 )
ntp时间戳计算精度由毫秒调整为微秒,解决误差累积问题。
parent
9f753b5e
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
31 行增加
和
31 行删除
+31
-31
src/Common/Stamp.cpp
+28
-28
src/Common/Stamp.h
+3
-3
没有找到文件。
src/Common/Stamp.cpp
查看文件 @
24d210f6
...
...
@@ -236,68 +236,68 @@ void NtpStamp::setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) {
WarnL
<<
"Invalid sender report rtcp, ntp_stamp_ms = "
<<
ntp_stamp_ms
<<
", rtp_stamp = "
<<
rtp_stamp
;
return
;
}
update
(
rtp_stamp
,
ntp_stamp_ms
);
update
(
rtp_stamp
,
ntp_stamp_ms
*
1000
);
}
void
NtpStamp
::
update
(
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_
m
s
)
{
void
NtpStamp
::
update
(
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_
u
s
)
{
_last_rtp_stamp
=
rtp_stamp
;
_last_ntp_stamp_
ms
=
ntp_stamp_m
s
;
_last_ntp_stamp_
us
=
ntp_stamp_u
s
;
}
uint64_t
NtpStamp
::
getNtpStamp
(
uint32_t
rtp_stamp
,
uint32_t
sample_rate
)
{
if
(
rtp_stamp
==
_last_rtp_stamp
)
{
return
_last_ntp_stamp_
ms
;
return
_last_ntp_stamp_
us
/
1000
;
}
return
getNtpStamp
_l
(
rtp_stamp
,
sample_rate
)
;
return
getNtpStamp
US
(
rtp_stamp
,
sample_rate
)
/
1000
;
}
uint64_t
NtpStamp
::
getNtpStamp
_l
(
uint32_t
rtp_stamp
,
uint32_t
sample_rate
)
{
if
(
!
_last_ntp_stamp_
m
s
)
{
uint64_t
NtpStamp
::
getNtpStamp
US
(
uint32_t
rtp_stamp
,
uint32_t
sample_rate
)
{
if
(
!
_last_ntp_stamp_
u
s
)
{
// 尚未收到sender report rtcp包,那么赋值为本地系统时间戳吧
update
(
rtp_stamp
,
getCurrentMi
lli
second
(
true
));
update
(
rtp_stamp
,
getCurrentMi
cro
second
(
true
));
}
// rtp时间戳正增长
if
(
rtp_stamp
>=
_last_rtp_stamp
)
{
auto
diff
=
static_cast
<
int
>
((
rtp_stamp
-
_last_rtp_stamp
)
/
(
sample_rate
/
1
000.0
f
));
if
(
diff
<
MAX_DELTA_STAMP
)
{
auto
diff
_us
=
static_cast
<
int64_t
>
((
rtp_stamp
-
_last_rtp_stamp
)
/
(
sample_rate
/
1000
000.0
f
));
if
(
diff
_us
<
MAX_DELTA_STAMP
*
1000
)
{
// 时间戳正常增长
update
(
rtp_stamp
,
_last_ntp_stamp_
ms
+
diff
);
return
_last_ntp_stamp_
m
s
;
update
(
rtp_stamp
,
_last_ntp_stamp_
us
+
diff_us
);
return
_last_ntp_stamp_
u
s
;
}
// 时间戳大幅跳跃
uint64_t
loop_delta
=
STAMP_LOOP_DELTA
*
sample_rate
/
1000
;
if
(
_last_rtp_stamp
<
loop_delta
&&
rtp_stamp
>
UINT32_MAX
-
loop_delta
)
{
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
)
{
// 应该是rtp时间戳溢出+乱序
uint64_t
max_rtp_
ms
=
uint64_t
(
UINT32_MAX
)
*
1
000
/
sample_rate
;
return
_last_ntp_stamp_
ms
+
diff
-
max_rtp_m
s
;
uint64_t
max_rtp_
us
=
uint64_t
(
UINT32_MAX
)
*
1000
000
/
sample_rate
;
return
_last_ntp_stamp_
us
+
diff_us
-
max_rtp_u
s
;
}
// 不明原因的时间戳大幅跳跃,直接返回上次值
WarnL
<<
"rtp stamp abnormal increased:"
<<
_last_rtp_stamp
<<
" -> "
<<
rtp_stamp
;
update
(
rtp_stamp
,
_last_ntp_stamp_
m
s
);
return
_last_ntp_stamp_
m
s
;
update
(
rtp_stamp
,
_last_ntp_stamp_
u
s
);
return
_last_ntp_stamp_
u
s
;
}
// rtp时间戳负增长
auto
diff
=
static_cast
<
int
>
((
_last_rtp_stamp
-
rtp_stamp
)
/
(
sample_rate
/
1
000.0
f
));
if
(
diff
<
MAX_DELTA_STAMP
)
{
auto
diff
_us
=
static_cast
<
int64_t
>
((
_last_rtp_stamp
-
rtp_stamp
)
/
(
sample_rate
/
1000
000.0
f
));
if
(
diff
_us
<
MAX_DELTA_STAMP
*
1000
)
{
// 正常范围的时间戳回退,说明收到rtp乱序了
return
_last_ntp_stamp_
ms
-
diff
;
return
_last_ntp_stamp_
us
-
diff_us
;
}
// 时间戳大幅度回退
uint64_t
loop_delta
=
STAMP_LOOP_DELTA
*
sample_rate
/
1000
;
if
(
rtp_stamp
<
loop_delta
&&
_last_rtp_stamp
>
UINT32_MAX
-
loop_delta
)
{
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
)
{
// 确定是时间戳溢出
uint64_t
max_rtp_
ms
=
uint64_t
(
UINT32_MAX
)
*
1
000
/
sample_rate
;
update
(
rtp_stamp
,
_last_ntp_stamp_
ms
+
(
max_rtp_ms
-
diff
));
return
_last_ntp_stamp_
m
s
;
uint64_t
max_rtp_
us
=
uint64_t
(
UINT32_MAX
)
*
1000
000
/
sample_rate
;
update
(
rtp_stamp
,
_last_ntp_stamp_
us
+
(
max_rtp_us
-
diff_us
));
return
_last_ntp_stamp_
u
s
;
}
// 不明原因的时间戳回退,直接返回上次值
WarnL
<<
"rtp stamp abnormal reduced:"
<<
_last_rtp_stamp
<<
" -> "
<<
rtp_stamp
;
update
(
rtp_stamp
,
_last_ntp_stamp_
m
s
);
return
_last_ntp_stamp_
m
s
;
update
(
rtp_stamp
,
_last_ntp_stamp_
u
s
);
return
_last_ntp_stamp_
u
s
;
}
}
// namespace mediakit
src/Common/Stamp.h
查看文件 @
24d210f6
...
...
@@ -125,12 +125,12 @@ public:
uint64_t
getNtpStamp
(
uint32_t
rtp_stamp
,
uint32_t
sample_rate
);
private
:
void
update
(
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_
m
s
);
uint64_t
getNtpStamp
_l
(
uint32_t
rtp_stamp
,
uint32_t
sample_rate
);
void
update
(
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_
u
s
);
uint64_t
getNtpStamp
US
(
uint32_t
rtp_stamp
,
uint32_t
sample_rate
);
private
:
uint32_t
_last_rtp_stamp
=
0
;
uint64_t
_last_ntp_stamp_
m
s
=
0
;
uint64_t
_last_ntp_stamp_
u
s
=
0
;
};
}
//namespace mediakit
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论