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
a4d7b346
Commit
a4d7b346
authored
May 06, 2020
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rtmp播放时,取相对时间戳逻辑确保时间戳同步
parent
920f06a9
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
54 行增加
和
4 行删除
+54
-4
src/Common/Stamp.cpp
+35
-0
src/Common/Stamp.h
+14
-4
src/Rtmp/FlvMuxer.cpp
+3
-0
src/Rtmp/RtmpSession.cpp
+2
-0
没有找到文件。
src/Common/Stamp.cpp
查看文件 @
a4d7b346
...
@@ -42,7 +42,40 @@ void Stamp::setPlayBack(bool playback) {
...
@@ -42,7 +42,40 @@ void Stamp::setPlayBack(bool playback) {
_playback
=
playback
;
_playback
=
playback
;
}
}
void
Stamp
::
makeRelation
(
Stamp
&
other
){
_related
=
&
other
;
}
void
Stamp
::
revise
(
int64_t
dts
,
int64_t
pts
,
int64_t
&
dts_out
,
int64_t
&
pts_out
,
bool
modifyStamp
)
{
void
Stamp
::
revise
(
int64_t
dts
,
int64_t
pts
,
int64_t
&
dts_out
,
int64_t
&
pts_out
,
bool
modifyStamp
)
{
revise_l
(
dts
,
pts
,
dts_out
,
pts_out
,
modifyStamp
);
if
(
modifyStamp
||
_playback
){
//自动生成时间戳或回放,不需要做音视频同步
return
;
}
if
(
_related
&&
_related
->
_last_dts
){
//音视频dts当前时间差
int64_t
dts_diff
=
_last_dts
-
_related
->
_last_dts
;
if
(
ABS
(
dts_diff
)
<
5000
){
//如果绝对时间戳小于5秒,那么说明他们的起始时间戳是一致的,那么强制同步
_last_relativeStamp
=
_relativeStamp
;
_relativeStamp
=
_related
->
_relativeStamp
+
dts_diff
;
dts_out
+=
dts_diff
;
pts_out
+=
dts_diff
;
// DebugL << "音视频同步事件差:" << dts_diff;
}
//下次不用再强制同步
_related
=
nullptr
;
}
if
(
dts_out
<
0
){
//相对时间戳小于0,那么说明是同步时间戳导致的,在这个过渡期内,我们一直返回上次的结果(目的是为了防止时间戳回退)
pts_out
=
_last_relativeStamp
+
(
pts_out
-
dts_out
);
dts_out
=
_last_relativeStamp
;
}
}
void
Stamp
::
revise_l
(
int64_t
dts
,
int64_t
pts
,
int64_t
&
dts_out
,
int64_t
&
pts_out
,
bool
modifyStamp
)
{
if
(
!
pts
){
if
(
!
pts
){
//没有播放时间戳,使其赋值为解码时间戳
//没有播放时间戳,使其赋值为解码时间戳
pts
=
dts
;
pts
=
dts
;
...
@@ -53,6 +86,7 @@ void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,
...
@@ -53,6 +86,7 @@ void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,
dts_out
=
dts
;
dts_out
=
dts
;
pts_out
=
pts
;
pts_out
=
pts
;
_relativeStamp
=
dts_out
;
_relativeStamp
=
dts_out
;
_last_dts
=
dts
;
return
;
return
;
}
}
...
@@ -62,6 +96,7 @@ void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,
...
@@ -62,6 +96,7 @@ void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,
if
(
_last_dts
!=
dts
){
if
(
_last_dts
!=
dts
){
//时间戳发生变更
//时间戳发生变更
if
(
modifyStamp
){
if
(
modifyStamp
){
//内部自己生产时间戳
_relativeStamp
=
_ticker
.
elapsedTime
();
_relativeStamp
=
_ticker
.
elapsedTime
();
}
else
{
}
else
{
_relativeStamp
+=
deltaStamp
(
dts
);
_relativeStamp
+=
deltaStamp
(
dts
);
...
...
src/Common/Stamp.h
查看文件 @
a4d7b346
...
@@ -32,6 +32,7 @@ public:
...
@@ -32,6 +32,7 @@ public:
private
:
private
:
int64_t
_last_stamp
=
0
;
int64_t
_last_stamp
=
0
;
};
};
//该类解决时间戳回环、回退问题
//该类解决时间戳回环、回退问题
//计算相对时间戳或者产生平滑时间戳
//计算相对时间戳或者产生平滑时间戳
class
Stamp
:
public
DeltaStamp
{
class
Stamp
:
public
DeltaStamp
{
...
@@ -66,14 +67,25 @@ public:
...
@@ -66,14 +67,25 @@ public:
* @param playback 是否为回放模式
* @param playback 是否为回放模式
*/
*/
void
setPlayBack
(
bool
playback
=
true
);
void
setPlayBack
(
bool
playback
=
true
);
/**
* 产生关联,用于音视频同步用
*/
void
makeRelation
(
Stamp
&
other
);
private
:
void
revise_l
(
int64_t
dts
,
int64_t
pts
,
int64_t
&
dts_out
,
int64_t
&
pts_out
,
bool
modifyStamp
=
false
);
private
:
private
:
int64_t
_relativeStamp
=
0
;
int64_t
_relativeStamp
=
0
;
int64_t
_last_dts
=
-
1
;
int64_t
_last_relativeStamp
=
0
;
int64_t
_last_dts
=
0
;
SmoothTicker
_ticker
;
SmoothTicker
_ticker
;
bool
_playback
=
false
;
bool
_playback
=
false
;
Stamp
*
_related
=
nullptr
;
};
};
//dts生成器,
//pts排序后就是dts
class
DtsGenerator
{
class
DtsGenerator
{
public
:
public
:
DtsGenerator
()
=
default
;
DtsGenerator
()
=
default
;
...
@@ -90,8 +102,6 @@ private:
...
@@ -90,8 +102,6 @@ private:
int
_sorter_max_size
=
0
;
int
_sorter_max_size
=
0
;
int
_count_sorter_max_size
=
0
;
int
_count_sorter_max_size
=
0
;
set
<
uint32_t
>
_pts_sorter
;
set
<
uint32_t
>
_pts_sorter
;
};
};
}
//namespace mediakit
}
//namespace mediakit
...
...
src/Rtmp/FlvMuxer.cpp
查看文件 @
a4d7b346
...
@@ -50,6 +50,9 @@ void FlvMuxer::start(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &
...
@@ -50,6 +50,9 @@ void FlvMuxer::start(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &
}
}
strongSelf
->
onDetach
();
strongSelf
->
onDetach
();
});
});
//音频同步于视频
_stamp
[
0
].
makeRelation
(
_stamp
[
1
]);
_ring_reader
->
setReadCB
([
weakSelf
](
const
RtmpMediaSource
::
RingDataType
&
pkt
){
_ring_reader
->
setReadCB
([
weakSelf
](
const
RtmpMediaSource
::
RingDataType
&
pkt
){
auto
strongSelf
=
weakSelf
.
lock
();
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
){
if
(
!
strongSelf
){
...
...
src/Rtmp/RtmpSession.cpp
查看文件 @
a4d7b346
...
@@ -266,6 +266,8 @@ void RtmpSession::sendPlayResponse(const string &err,const RtmpMediaSource::Ptr
...
@@ -266,6 +266,8 @@ void RtmpSession::sendPlayResponse(const string &err,const RtmpMediaSource::Ptr
onSendMedia
(
pkt
);
onSendMedia
(
pkt
);
});
});
//音频同步于视频
_stamp
[
0
].
makeRelation
(
_stamp
[
1
]);
_pRingReader
=
src
->
getRing
()
->
attach
(
getPoller
());
_pRingReader
=
src
->
getRing
()
->
attach
(
getPoller
());
weak_ptr
<
RtmpSession
>
weakSelf
=
dynamic_pointer_cast
<
RtmpSession
>
(
shared_from_this
());
weak_ptr
<
RtmpSession
>
weakSelf
=
dynamic_pointer_cast
<
RtmpSession
>
(
shared_from_this
());
_pRingReader
->
setReadCB
([
weakSelf
](
const
RtmpMediaSource
::
RingDataType
&
pkt
)
{
_pRingReader
->
setReadCB
([
weakSelf
](
const
RtmpMediaSource
::
RingDataType
&
pkt
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论