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
bc36e000
Commit
bc36e000
authored
Oct 01, 2020
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
提炼rtp排序算法
parent
eee65a51
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
141 行增加
和
66 行删除
+141
-66
src/Rtsp/RtpReceiver.cpp
+19
-54
src/Rtsp/RtpReceiver.h
+122
-12
没有找到文件。
src/Rtsp/RtpReceiver.cpp
查看文件 @
bc36e000
...
...
@@ -11,11 +11,6 @@
#include "Common/config.h"
#include "RtpReceiver.h"
#define POP_HEAD(trackidx) \
auto it = _rtp_sort_cache_map[trackidx].begin(); \
onRtpSorted(it->second, trackidx); \
_rtp_sort_cache_map[trackidx].erase(it);
#define AV_RB16(x) \
((((const uint8_t*)(x))[0] << 8) | \
((const uint8_t*)(x))[1])
...
...
@@ -24,7 +19,18 @@
namespace
mediakit
{
RtpReceiver
::
RtpReceiver
()
{}
RtpReceiver
::
RtpReceiver
()
{
GET_CONFIG
(
uint32_t
,
clearCount
,
Rtp
::
kClearCount
);
GET_CONFIG
(
uint32_t
,
maxRtpCount
,
Rtp
::
kMaxRtpCount
);
int
index
=
0
;
for
(
auto
&
sortor
:
_rtp_sortor
)
{
sortor
.
setup
(
maxRtpCount
,
clearCount
);
sortor
.
setOnSort
([
this
,
index
](
uint16_t
seq
,
const
RtpPacket
::
Ptr
&
packet
)
{
onRtpSorted
(
packet
,
index
);
});
++
index
;
}
}
RtpReceiver
::~
RtpReceiver
()
{}
bool
RtpReceiver
::
handleOneRtp
(
int
track_index
,
TrackType
type
,
int
samplerate
,
unsigned
char
*
rtp_raw_ptr
,
unsigned
int
rtp_raw_len
)
{
...
...
@@ -80,7 +86,7 @@ bool RtpReceiver::handleOneRtp(int track_index, TrackType type, int samplerate,
if
(
_ssrc_err_count
[
track_index
]
++
>
10
)
{
//ssrc切换后清除老数据
WarnL
<<
"ssrc更换:"
<<
_ssrc
[
track_index
]
<<
" -> "
<<
rtp
.
ssrc
;
_rtp_sort
_cache_map
[
track_index
].
clear
();
_rtp_sort
or
[
track_index
].
clear
();
_ssrc
[
track_index
]
=
rtp
.
ssrc
;
}
return
false
;
...
...
@@ -127,56 +133,15 @@ bool RtpReceiver::handleOneRtp(int track_index, TrackType type, int samplerate,
}
void
RtpReceiver
::
sortRtp
(
const
RtpPacket
::
Ptr
&
rtp
,
int
track_index
){
if
(
rtp
->
sequence
!=
_last_seq
[
track_index
]
+
1
&&
_last_seq
[
track_index
]
!=
0
){
//包乱序或丢包
_seq_ok_count
[
track_index
]
=
0
;
_sort_started
[
track_index
]
=
true
;
if
(
_last_seq
[
track_index
]
>
rtp
->
sequence
&&
_last_seq
[
track_index
]
-
rtp
->
sequence
>
0xFF
){
//sequence回环,清空所有排序缓存
while
(
_rtp_sort_cache_map
[
track_index
].
size
())
{
POP_HEAD
(
track_index
)
}
++
_seq_cycle_count
[
track_index
];
}
}
else
{
//正确序列的包
_seq_ok_count
[
track_index
]
++
;
}
_last_seq
[
track_index
]
=
rtp
->
sequence
;
//开始排序缓存
if
(
_sort_started
[
track_index
])
{
_rtp_sort_cache_map
[
track_index
].
emplace
(
rtp
->
sequence
,
rtp
);
GET_CONFIG
(
uint32_t
,
clearCount
,
Rtp
::
kClearCount
);
GET_CONFIG
(
uint32_t
,
maxRtpCount
,
Rtp
::
kMaxRtpCount
);
if
(
_seq_ok_count
[
track_index
]
>=
clearCount
)
{
//网络环境改善,需要清空排序缓存
_seq_ok_count
[
track_index
]
=
0
;
_sort_started
[
track_index
]
=
false
;
while
(
_rtp_sort_cache_map
[
track_index
].
size
())
{
POP_HEAD
(
track_index
)
}
}
else
if
(
_rtp_sort_cache_map
[
track_index
].
size
()
>=
maxRtpCount
)
{
//排序缓存溢出
POP_HEAD
(
track_index
)
}
}
else
{
//正确序列
onRtpSorted
(
rtp
,
track_index
);
}
_rtp_sortor
[
track_index
].
sortPacket
(
rtp
->
sequence
,
rtp
);
}
void
RtpReceiver
::
clear
()
{
CLEAR_ARR
(
_last_seq
);
CLEAR_ARR
(
_ssrc
);
CLEAR_ARR
(
_ssrc_err_count
);
CLEAR_ARR
(
_seq_ok_count
);
CLEAR_ARR
(
_sort_started
);
CLEAR_ARR
(
_seq_cycle_count
);
_rtp_sort_cache_map
[
0
].
clear
();
_rtp_sort_cache_map
[
1
].
clear
();
for
(
auto
&
sortor
:
_rtp_sortor
)
{
sortor
.
clear
();
}
}
void
RtpReceiver
::
setPoolSize
(
int
size
)
{
...
...
@@ -184,11 +149,11 @@ void RtpReceiver::setPoolSize(int size) {
}
int
RtpReceiver
::
getJitterSize
(
int
track_index
){
return
_rtp_sort
_cache_map
[
track_index
].
s
ize
();
return
_rtp_sort
or
[
track_index
].
getJitterS
ize
();
}
int
RtpReceiver
::
getCycleCount
(
int
track_index
){
return
_
seq_cycle_count
[
track_index
]
;
return
_
rtp_sortor
[
track_index
].
getCycleCount
()
;
}
...
...
src/Rtsp/RtpReceiver.h
查看文件 @
bc36e000
...
...
@@ -11,24 +11,141 @@
#ifndef ZLMEDIAKIT_RTPRECEIVER_H
#define ZLMEDIAKIT_RTPRECEIVER_H
#include <map>
#include <string>
#include <memory>
#include "RtpCodec.h"
#include "RtspMediaSource.h"
using
namespace
std
;
using
namespace
toolkit
;
namespace
mediakit
{
template
<
typename
T
,
typename
SEQ
=
uint16_t
>
class
PacketSortor
{
public
:
PacketSortor
()
=
default
;
~
PacketSortor
()
=
default
;
/**
* 设置参数
* @param max_sort_size 最大排序缓存长度
* @param clear_sort_size seq连续次数超过该值后,清空并关闭排序缓存
*/
void
setup
(
uint32_t
max_sort_size
,
uint32_t
clear_sort_size
)
{
_max_sort_size
=
max_sort_size
;
_clear_sort_size
=
clear_sort_size
;
}
void
setOnSort
(
function
<
void
(
SEQ
seq
,
const
T
&
packet
)
>
cb
){
_cb
=
std
::
move
(
cb
);
}
/**
* 清空状态
*/
void
clear
()
{
_last_seq
=
0
;
_seq_ok_count
=
0
;
_sort_started
=
0
;
_seq_cycle_count
=
0
;
_rtp_sort_cache_map
.
clear
();
}
/**
* 获取排序缓存长度
*/
int
getJitterSize
(){
return
_rtp_sort_cache_map
.
size
();
}
/**
* 获取seq回环次数
*/
int
getCycleCount
(){
return
_seq_cycle_count
;
}
/**
* 输入并排序
* @param seq 序列号
* @param packet 包负载
*/
void
sortPacket
(
SEQ
seq
,
const
T
&
packet
){
if
(
seq
!=
_last_seq
+
1
&&
_last_seq
!=
0
)
{
//包乱序或丢包
_seq_ok_count
=
0
;
_sort_started
=
true
;
if
(
_last_seq
>
seq
&&
_last_seq
-
seq
>
0xFF
)
{
//sequence回环,清空所有排序缓存
while
(
_rtp_sort_cache_map
.
size
())
{
popPacket
();
}
++
_seq_cycle_count
;
}
}
else
{
//正确序列的包
_seq_ok_count
++
;
}
_last_seq
=
seq
;
//开始排序缓存
if
(
_sort_started
)
{
_rtp_sort_cache_map
.
emplace
(
seq
,
packet
);
if
(
_seq_ok_count
>=
_clear_sort_size
)
{
//网络环境改善,需要清空排序缓存
_seq_ok_count
=
0
;
_sort_started
=
false
;
while
(
_rtp_sort_cache_map
.
size
())
{
popPacket
();
}
}
else
if
(
_rtp_sort_cache_map
.
size
()
>=
_max_sort_size
)
{
//排序缓存溢出
popPacket
();
}
}
else
{
//正确序列
onPacketSorted
(
seq
,
packet
);
}
}
private
:
void
popPacket
()
{
auto
it
=
_rtp_sort_cache_map
.
begin
();
onPacketSorted
(
it
->
first
,
it
->
second
);
_rtp_sort_cache_map
.
erase
(
it
);
}
void
onPacketSorted
(
SEQ
seq
,
const
T
&
packet
)
{
_cb
(
seq
,
packet
);
}
private
:
//是否开始seq排序
bool
_sort_started
=
false
;
//上次seq
SEQ
_last_seq
=
0
;
//seq连续次数计数
uint32_t
_seq_ok_count
=
0
;
//seq回环次数计数
uint32_t
_seq_cycle_count
=
0
;
//排序缓存长度
uint32_t
_max_sort_size
;
//seq连续次数超过该值后,清空并关闭排序缓存
uint32_t
_clear_sort_size
;
//rtp排序缓存,根据seq排序
map
<
SEQ
,
T
>
_rtp_sort_cache_map
;
//回调
function
<
void
(
SEQ
seq
,
const
T
&
packet
)
>
_cb
;
};
class
RtpReceiver
{
public
:
RtpReceiver
();
virtual
~
RtpReceiver
();
protected
:
protected
:
/**
* 输入数据指针生成并排序rtp包
* @param track_index track下标索引
...
...
@@ -46,6 +163,7 @@ protected:
* @param track_index track索引
*/
virtual
void
onRtpSorted
(
const
RtpPacket
::
Ptr
&
rtp
,
int
track_index
){}
void
clear
();
void
setPoolSize
(
int
size
);
int
getJitterSize
(
int
track_index
);
...
...
@@ -58,16 +176,8 @@ private:
uint32_t
_ssrc
[
2
]
=
{
0
,
0
};
//ssrc不匹配计数
uint32_t
_ssrc_err_count
[
2
]
=
{
0
,
0
};
//上次seq
uint16_t
_last_seq
[
2
]
=
{
0
,
0
};
//seq连续次数计数
uint32_t
_seq_ok_count
[
2
]
=
{
0
,
0
};
//seq回环次数计数
uint32_t
_seq_cycle_count
[
2
]
=
{
0
,
0
};
//是否开始seq排序
bool
_sort_started
[
2
]
=
{
0
,
0
};
//rtp排序缓存,根据seq排序
map
<
uint16_t
,
RtpPacket
::
Ptr
>
_rtp_sort_cache_map
[
2
];
PacketSortor
<
RtpPacket
::
Ptr
>
_rtp_sortor
[
2
];
//rtp循环池
RtspMediaSource
::
PoolType
_rtp_pool
;
};
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论