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
415bc95d
Commit
415bc95d
authored
Sep 02, 2021
by
ziyue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善ntp时间戳计算逻辑
parent
72c2df05
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
36 行增加
和
32 行删除
+36
-32
src/Common/Stamp.cpp
+23
-17
src/Common/Stamp.h
+2
-3
src/Rtp/GB28181Process.cpp
+1
-1
src/Rtsp/RtpReceiver.cpp
+4
-4
src/Rtsp/RtpReceiver.h
+3
-4
src/Rtsp/RtspPlayer.cpp
+1
-1
src/Rtsp/RtspSession.cpp
+1
-1
webrtc/WebRtcTransport.cpp
+1
-1
没有找到文件。
src/Common/Stamp.cpp
查看文件 @
415bc95d
...
...
@@ -218,13 +218,12 @@ bool DtsGenerator::getDts_l(uint32_t pts, uint32_t &dts){
return
false
;
}
void
NtpStamp
::
setNtpStamp
(
uint32_t
rtp_stamp
,
uint32_t
sample_rate
,
uint64_t
ntp_stamp_ms
)
{
assert
(
sample_rate
);
update
(
uint64_t
(
rtp_stamp
)
*
1000
/
sample_rate
,
ntp_stamp_ms
);
void
NtpStamp
::
setNtpStamp
(
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_ms
)
{
update
(
rtp_stamp
,
ntp_stamp_ms
);
}
void
NtpStamp
::
update
(
uint32_t
rtp_stamp
_ms
,
uint64_t
ntp_stamp_ms
)
{
_last_rtp_stamp
_ms
=
rtp_stamp_ms
;
void
NtpStamp
::
update
(
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_ms
)
{
_last_rtp_stamp
=
rtp_stamp
;
_last_ntp_stamp_ms
=
ntp_stamp_ms
;
}
...
...
@@ -238,38 +237,45 @@ uint64_t NtpStamp::getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate) {
}
uint64_t
NtpStamp
::
getNtpStamp_l
(
uint32_t
rtp_stamp
,
uint32_t
sample_rate
)
{
uint64_t
rtp_stamp_ms
=
uint64_t
(
rtp_stamp
)
*
1000
/
sample_rate
;
if
(
!
_last_rtp_stamp_ms
&&
!
_last_ntp_stamp_ms
)
{
if
(
!
_last_ntp_stamp_ms
)
{
//尚未收到sender report rtcp包,那么赋值为本地系统时间戳吧
update
(
rtp_stamp
_ms
,
getCurrentMillisecond
(
true
));
update
(
rtp_stamp
,
getCurrentMillisecond
(
true
));
}
if
(
rtp_stamp_ms
>=
_last_rtp_stamp_ms
)
{
auto
diff
=
rtp_stamp_ms
-
_last_rtp_stamp_ms
;
//rtp时间戳正增长
if
(
rtp_stamp
>=
_last_rtp_stamp
)
{
auto
diff
=
(
rtp_stamp
-
_last_rtp_stamp
)
/
(
sample_rate
/
1000.0
f
);
if
(
diff
<
MAX_DELTA_STAMP
)
{
//时间戳正常增长
update
(
rtp_stamp
_ms
,
_last_ntp_stamp_ms
+
diff
);
update
(
rtp_stamp
,
_last_ntp_stamp_ms
+
diff
);
return
_last_ntp_stamp_ms
;
}
uint64_t
max_rtp_ms
=
uint64_t
(
UINT32_MAX
)
*
1000
/
sample_rate
;
//时间戳大幅跳跃
if
(
_last_rtp_stamp_ms
<
STAMP_LOOP_DELTA
&&
rtp_stamp_ms
>
max_rtp_ms
-
STAMP_LOOP_DELTA
)
{
uint64_t
loop_delta
=
STAMP_LOOP_DELTA
*
sample_rate
/
1000
;
if
(
_last_rtp_stamp
<
loop_delta
&&
rtp_stamp
>
UINT32_MAX
-
loop_delta
)
{
//应该是rtp时间戳溢出+乱序
uint64_t
max_rtp_ms
=
uint64_t
(
UINT32_MAX
)
*
1000
/
sample_rate
;
return
_last_ntp_stamp_ms
+
diff
-
max_rtp_ms
;
}
//不明原因的时间戳大幅跳跃,直接返回上次值
WarnL
<<
"rtp stamp abnormal increased:"
<<
_last_rtp_stamp
<<
" -> "
<<
rtp_stamp
;
return
_last_ntp_stamp_ms
;
}
auto
diff
=
_last_rtp_stamp_ms
-
rtp_stamp_ms
;
//rtp时间戳负增长
auto
diff
=
(
_last_rtp_stamp
-
rtp_stamp
)
/
(
sample_rate
/
1000.0
f
);
if
(
diff
<
MAX_DELTA_STAMP
)
{
//正常范围的时间戳回退,说明收到rtp乱序了
return
_last_ntp_stamp_ms
-
diff
;
}
uint64_t
max_rtp_ms
=
uint64_t
(
UINT32_MAX
)
*
1000
/
sample_rate
;
if
(
rtp_stamp_ms
<
STAMP_LOOP_DELTA
&&
_last_rtp_stamp_ms
>
max_rtp_ms
-
STAMP_LOOP_DELTA
)
{
//时间戳大幅度回退
uint64_t
loop_delta
=
STAMP_LOOP_DELTA
*
sample_rate
/
1000
;
if
(
rtp_stamp
<
loop_delta
&&
_last_rtp_stamp
>
UINT32_MAX
-
loop_delta
)
{
//确定是时间戳溢出
update
(
rtp_stamp_ms
,
_last_ntp_stamp_ms
+
(
max_rtp_ms
-
diff
));
uint64_t
max_rtp_ms
=
uint64_t
(
UINT32_MAX
)
*
1000
/
sample_rate
;
update
(
rtp_stamp
,
_last_ntp_stamp_ms
+
(
max_rtp_ms
-
diff
));
return
_last_ntp_stamp_ms
;
}
//不明原因的时间戳回退,直接返回上次值
...
...
src/Common/Stamp.h
查看文件 @
415bc95d
...
...
@@ -119,16 +119,15 @@ public:
NtpStamp
()
=
default
;
~
NtpStamp
()
=
default
;
void
setNtpStamp
(
uint32_t
rtp_stamp
,
uint
32_t
sample_rate
,
uint
64_t
ntp_stamp_ms
);
void
setNtpStamp
(
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_ms
);
uint64_t
getNtpStamp
(
uint32_t
rtp_stamp
,
uint32_t
sample_rate
);
private
:
void
update
(
uint32_t
rtp_stamp
_ms
,
uint64_t
ntp_stamp_ms
);
void
update
(
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_ms
);
uint64_t
getNtpStamp_l
(
uint32_t
rtp_stamp
,
uint32_t
sample_rate
);
private
:
uint32_t
_last_rtp_stamp
=
0
;
uint64_t
_last_rtp_stamp_ms
=
0
;
uint64_t
_last_ntp_stamp_ms
=
0
;
};
...
...
src/Rtp/GB28181Process.cpp
查看文件 @
415bc95d
...
...
@@ -33,7 +33,7 @@ public:
setOnSorted
(
std
::
move
(
cb
));
setBeforeSorted
(
std
::
move
(
cb_before
));
//GB28181推流不支持ntp时间戳
setNtpStamp
(
0
,
0
,
0
);
setNtpStamp
(
0
,
0
);
}
~
RtpReceiverImp
()
override
=
default
;
...
...
src/Rtsp/RtpReceiver.cpp
查看文件 @
415bc95d
...
...
@@ -101,10 +101,10 @@ RtpPacket::Ptr RtpTrack::inputRtp(TrackType type, int sample_rate, uint8_t *ptr,
return
rtp
;
}
void
RtpTrack
::
setNtpStamp
(
uint32_t
rtp_stamp
,
uint
32_t
sample_rate
,
uint
64_t
ntp_stamp_ms
)
{
_disable_ntp
=
rtp_stamp
==
0
&&
sample_rate
==
0
&&
ntp_stamp_ms
==
0
;
if
(
sample_rate
)
{
_ntp_stamp
.
setNtpStamp
(
rtp_stamp
,
sample_rate
,
ntp_stamp_ms
);
void
RtpTrack
::
setNtpStamp
(
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_ms
)
{
_disable_ntp
=
rtp_stamp
==
0
&&
ntp_stamp_ms
==
0
;
if
(
!
_disable_ntp
)
{
_ntp_stamp
.
setNtpStamp
(
rtp_stamp
,
ntp_stamp_ms
);
}
}
...
...
src/Rtsp/RtpReceiver.h
查看文件 @
415bc95d
...
...
@@ -176,7 +176,7 @@ public:
void
clear
();
uint32_t
getSSRC
()
const
;
RtpPacket
::
Ptr
inputRtp
(
TrackType
type
,
int
sample_rate
,
uint8_t
*
ptr
,
size_t
len
);
void
setNtpStamp
(
uint32_t
rtp_stamp
,
uint
32_t
sample_rate
,
uint
64_t
ntp_stamp_ms
);
void
setNtpStamp
(
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_ms
);
protected
:
virtual
void
onRtpSorted
(
RtpPacket
::
Ptr
rtp
)
{}
...
...
@@ -245,11 +245,10 @@ public:
* 如果rtp_stamp/sample_rate/ntp_stamp_ms都为0,那么采用rtp时间戳为ntp时间戳
* @param index track下标索引
* @param rtp_stamp rtp时间戳
* @param sample_rate 时间戳采样率
* @param ntp_stamp_ms ntp时间戳
*/
void
setNtpStamp
(
int
index
,
uint32_t
rtp_stamp
,
uint
32_t
sample_rate
,
uint
64_t
ntp_stamp_ms
){
_track
[
index
].
setNtpStamp
(
rtp_stamp
,
sample_rate
,
ntp_stamp_ms
);
void
setNtpStamp
(
int
index
,
uint32_t
rtp_stamp
,
uint64_t
ntp_stamp_ms
){
_track
[
index
].
setNtpStamp
(
rtp_stamp
,
ntp_stamp_ms
);
}
void
clear
()
{
...
...
src/Rtsp/RtspPlayer.cpp
查看文件 @
415bc95d
...
...
@@ -496,7 +496,7 @@ void RtspPlayer::onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data
if
((
RtcpType
)
rtcp
->
pt
==
RtcpType
::
RTCP_SR
)
{
auto
sr
=
(
RtcpSR
*
)
(
rtcp
);
//设置rtp时间戳与ntp时间戳的对应关系
setNtpStamp
(
track_idx
,
sr
->
rtpts
,
track
->
_samplerate
,
sr
->
getNtpUnixStampMS
());
setNtpStamp
(
track_idx
,
sr
->
rtpts
,
sr
->
getNtpUnixStampMS
());
}
}
}
...
...
src/Rtsp/RtspSession.cpp
查看文件 @
415bc95d
...
...
@@ -189,7 +189,7 @@ void RtspSession::onRtcpPacket(int track_idx, SdpTrack::Ptr &track, const char *
if
((
RtcpType
)
rtcp
->
pt
==
RtcpType
::
RTCP_SR
)
{
auto
sr
=
(
RtcpSR
*
)
(
rtcp
);
//设置rtp时间戳与ntp时间戳的对应关系
setNtpStamp
(
track_idx
,
sr
->
rtpts
,
track
->
_samplerate
,
sr
->
getNtpUnixStampMS
());
setNtpStamp
(
track_idx
,
sr
->
rtpts
,
sr
->
getNtpUnixStampMS
());
}
}
}
...
...
webrtc/WebRtcTransport.cpp
查看文件 @
415bc95d
...
...
@@ -674,7 +674,7 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
}
else
{
//InfoL << "接收丢包率,ssrc:" << sr->ssrc << ",loss rate(%):" << rtp_chn->getLossRate();
//设置rtp时间戳与ntp时间戳的对应关系
rtp_chn
->
setNtpStamp
(
sr
->
rtpts
,
track
->
plan_rtp
->
sample_rate
,
sr
->
getNtpUnixStampMS
());
rtp_chn
->
setNtpStamp
(
sr
->
rtpts
,
sr
->
getNtpUnixStampMS
());
auto
rr
=
rtp_chn
->
createRtcpRR
(
sr
,
track
->
answer_ssrc_rtp
);
sendRtcpPacket
(
rr
->
data
(),
rr
->
size
(),
true
);
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论