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
3acfb15c
Commit
3acfb15c
authored
Jul 26, 2020
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善H264 rtp解包打包相关代码
parent
c0fde624
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
44 行增加
和
66 行删除
+44
-66
src/Extension/H264Rtp.cpp
+43
-65
src/Extension/H264Rtp.h
+1
-1
没有找到文件。
src/Extension/H264Rtp.cpp
查看文件 @
3acfb15c
...
...
@@ -12,13 +12,6 @@
namespace
mediakit
{
typedef
struct
{
unsigned
forbidden_zero_bit
:
1
;
unsigned
nal_ref_idc
:
2
;
unsigned
type
:
5
;
}
NALU
;
typedef
struct
{
unsigned
S
:
1
;
unsigned
E
:
1
;
...
...
@@ -26,15 +19,6 @@ typedef struct {
unsigned
type
:
5
;
}
FU
;
static
bool
MakeNalu
(
uint8_t
in
,
NALU
&
nal
)
{
nal
.
forbidden_zero_bit
=
in
>>
7
;
if
(
nal
.
forbidden_zero_bit
)
{
return
false
;
}
nal
.
nal_ref_idc
=
(
in
&
0x60
)
>>
5
;
nal
.
type
=
in
&
0x1f
;
return
true
;
}
static
bool
MakeFU
(
uint8_t
in
,
FU
&
fu
)
{
fu
.
S
=
in
>>
7
;
fu
.
E
=
(
in
>>
6
)
&
0x01
;
...
...
@@ -86,30 +70,28 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
28 FU-A Fragmentation unit 5.8
29 FU-B Fragmentation unit 5.8
30-31 undefined -
*/
const
uint8_t
*
frame
=
(
uint8_t
*
)
rtppack
->
data
()
+
rtppack
->
offset
;
int
length
=
rtppack
->
size
()
-
rtppack
->
offset
;
NALU
nal
;
MakeNalu
(
*
frame
,
nal
);
int
nal_type
=
*
frame
&
0x1F
;
int
nal_suffix
=
*
frame
&
(
~
0x1F
);
if
(
nal
.
type
>=
0
&&
nal
.
type
<
24
)
{
if
(
nal
_type
>=
0
&&
nal_
type
<
24
)
{
//a full frame
_h264frame
->
_buffer
.
assign
(
"\x0\x0\x0\x1"
,
4
);
_h264frame
->
_buffer
.
append
((
char
*
)
frame
,
length
);
_h264frame
->
_buffer
.
append
((
char
*
)
frame
,
length
);
_h264frame
->
_pts
=
rtppack
->
timeStamp
;
auto
key
=
_h264frame
->
keyFrame
();
onGetH264
(
_h264frame
);
return
(
key
);
//i frame
}
switch
(
nal
.
type
){
switch
(
nal
_
type
){
case
24
:{
// 24 STAP-A 单一时间的组合包
bool
haveIDR
=
false
;
auto
ptr
=
frame
+
1
;
while
(
true
)
{
while
(
true
)
{
int
off
=
ptr
-
frame
;
if
(
off
>=
length
)
{
break
;
...
...
@@ -121,14 +103,12 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
if
(
off
+
len
>
length
)
{
break
;
}
if
(
len
>=
10
)
{
if
(
len
>
4
)
{
//过小的帧丢弃
NALU
nal
;
MakeNalu
(
ptr
[
0
],
nal
);
_h264frame
->
_buffer
.
assign
(
"\x0\x0\x0\x1"
,
4
);
_h264frame
->
_buffer
.
append
((
char
*
)
ptr
,
len
);
_h264frame
->
_buffer
.
append
((
char
*
)
ptr
,
len
);
_h264frame
->
_pts
=
rtppack
->
timeStamp
;
if
(
nal
.
type
==
H264Frame
::
NAL_IDR
)
{
if
((
ptr
[
0
]
&
0x1F
)
==
H264Frame
::
NAL_IDR
)
{
haveIDR
=
true
;
}
onGetH264
(
_h264frame
);
...
...
@@ -144,10 +124,9 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
MakeFU
(
frame
[
1
],
fu
);
if
(
fu
.
S
)
{
//该帧的第一个rtp包 FU-A start
char
tmp
=
(
nal
.
forbidden_zero_bit
<<
7
|
nal
.
nal_ref_idc
<<
5
|
fu
.
type
);
_h264frame
->
_buffer
.
assign
(
"\x0\x0\x0\x1"
,
4
);
_h264frame
->
_buffer
.
push_back
(
tmp
);
_h264frame
->
_buffer
.
append
((
char
*
)
frame
+
2
,
length
-
2
);
_h264frame
->
_buffer
.
push_back
(
nal_suffix
|
fu
.
type
);
_h264frame
->
_buffer
.
append
((
char
*
)
frame
+
2
,
length
-
2
);
_h264frame
->
_pts
=
rtppack
->
timeStamp
;
//该函数return时,保存下当前sequence,以便下次对比seq是否连续
_lastSeq
=
rtppack
->
sequence
;
...
...
@@ -163,20 +142,20 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
if
(
!
fu
.
E
)
{
//该帧的中间rtp包 FU-A mid
_h264frame
->
_buffer
.
append
((
char
*
)
frame
+
2
,
length
-
2
);
_h264frame
->
_buffer
.
append
((
char
*
)
frame
+
2
,
length
-
2
);
//该函数return时,保存下当前sequence,以便下次对比seq是否连续
_lastSeq
=
rtppack
->
sequence
;
return
false
;
}
//该帧最后一个rtp包 FU-A end
_h264frame
->
_buffer
.
append
((
char
*
)
frame
+
2
,
length
-
2
);
_h264frame
->
_buffer
.
append
((
char
*
)
frame
+
2
,
length
-
2
);
_h264frame
->
_pts
=
rtppack
->
timeStamp
;
onGetH264
(
_h264frame
);
return
false
;
}
default
:
{
default
:
{
// 29 FU-B 单NAL单元B模式
// 25 STAP-B 单一时间的组合包
// 26 MTAP16 多个时间的组合包
...
...
@@ -184,7 +163,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
// 0 udef
// 30 udef
// 31 udef
WarnL
<<
"不支持的rtp类型:"
<<
(
int
)
nal
.
type
<<
" "
<<
rtppack
->
sequence
;
WarnL
<<
"不支持的rtp类型:"
<<
(
int
)
nal_
type
<<
" "
<<
rtppack
->
sequence
;
return
false
;
}
}
...
...
@@ -215,63 +194,62 @@ H264RtpEncoder::H264RtpEncoder(uint32_t ui32Ssrc,
void
H264RtpEncoder
::
inputFrame
(
const
Frame
::
Ptr
&
frame
)
{
GET_CONFIG
(
uint32_t
,
cycleMS
,
Rtp
::
kCycleMS
);
auto
p
cData
=
frame
->
data
()
+
frame
->
prefixSize
();
auto
uiStamp
=
frame
->
pts
()
;
auto
iL
en
=
frame
->
size
()
-
frame
->
prefixSize
();
//获取NALU的5bit 帧类型
unsigned
char
naluType
=
H264_TYPE
(
pcData
[
0
])
;
auto
p
tr
=
frame
->
data
()
+
frame
->
prefixSize
();
auto
pts
=
frame
->
pts
()
%
cycleMS
;
auto
l
en
=
frame
->
size
()
-
frame
->
prefixSize
();
auto
nal_type
=
H264_TYPE
(
ptr
[
0
]);
auto
max_rtp_size
=
_ui32MtuSize
-
2
;
uiStamp
%=
cycleMS
;
int
iSize
=
_ui32MtuSize
-
2
;
//超过MTU则按照FU-A模式打包
if
(
iLen
>
iS
ize
)
{
if
(
len
>
max_rtp_s
ize
)
{
//最高位bit为forbidden_zero_bit,
//后面2bit为nal_ref_idc(帧重要程度),00:可以丢,11:不能丢
//末尾5bit为nalu type,固定为28(FU-A)
unsigned
char
f_nri_flags
=
(
*
((
unsigned
char
*
)
pcData
)
&
0x60
)
|
28
;
unsigned
char
nal_fu_a
=
(
*
((
unsigned
char
*
)
ptr
)
&
(
~
0x1F
)
)
|
28
;
unsigned
char
s_e_r_flags
;
bool
bFirs
t
=
true
;
bool
mark
=
false
;
int
nO
ffset
=
1
;
while
(
!
mark
)
{
if
(
iLen
<=
nOffset
+
iS
ize
)
{
bool
fu_a_star
t
=
true
;
bool
mark
_bit
=
false
;
int
o
ffset
=
1
;
while
(
!
mark
_bit
)
{
if
(
len
<=
offset
+
max_rtp_s
ize
)
{
//已经拆分结束
iSize
=
iLen
-
nO
ffset
;
mark
=
true
;
max_rtp_size
=
len
-
o
ffset
;
mark
_bit
=
true
;
//FU-A end
s_e_r_flags
=
(
1
<<
6
)
|
nal
uT
ype
;
}
else
if
(
bFirs
t
)
{
s_e_r_flags
=
(
1
<<
6
)
|
nal
_t
ype
;
}
else
if
(
fu_a_star
t
)
{
//FU-A start
s_e_r_flags
=
(
1
<<
7
)
|
nal
uT
ype
;
s_e_r_flags
=
(
1
<<
7
)
|
nal
_t
ype
;
}
else
{
//FU-A mid
s_e_r_flags
=
nal
uT
ype
;
s_e_r_flags
=
nal
_t
ype
;
}
{
//传入nullptr先不做payload的内存拷贝
auto
rtp
=
makeRtp
(
getTrackType
(),
nullptr
,
iSize
+
2
,
mark
,
uiStamp
);
auto
rtp
=
makeRtp
(
getTrackType
(),
nullptr
,
max_rtp_size
+
2
,
mark_bit
,
pts
);
//rtp payload 负载部分
uint8_t
*
payload
=
(
uint8_t
*
)
rtp
->
data
()
+
rtp
->
offset
;
//FU-A 第1个字节
payload
[
0
]
=
f_nri_flags
;
payload
[
0
]
=
nal_fu_a
;
//FU-A 第2个字节
payload
[
1
]
=
s_e_r_flags
;
//H264 数据
memcpy
(
payload
+
2
,
(
unsigned
char
*
)
p
cData
+
nOffset
,
iS
ize
);
memcpy
(
payload
+
2
,
(
unsigned
char
*
)
p
tr
+
offset
,
max_rtp_s
ize
);
//输入到rtp环形缓存
RtpCodec
::
inputRtp
(
rtp
,
bFirst
&&
naluT
ype
==
H264Frame
::
NAL_IDR
);
RtpCodec
::
inputRtp
(
rtp
,
fu_a_start
&&
nal_t
ype
==
H264Frame
::
NAL_IDR
);
}
nOffset
+=
iS
ize
;
bFirs
t
=
false
;
offset
+=
max_rtp_s
ize
;
fu_a_star
t
=
false
;
}
}
else
{
makeH264Rtp
(
naluType
,
pcData
,
iLen
,
true
,
true
,
uiStamp
);
//如果帧长度不超过mtu, 则按照Single NAL unit packet per H.264 方式打包
makeH264Rtp
(
ptr
,
len
,
false
,
false
,
pts
);
}
}
void
H264RtpEncoder
::
makeH264Rtp
(
int
nal_type
,
const
void
*
data
,
unsigned
int
len
,
bool
mark
,
bool
first_packet
,
uint32_t
uiStamp
)
{
RtpCodec
::
inputRtp
(
makeRtp
(
getTrackType
(),
data
,
len
,
mark
,
uiStamp
),
first_packet
&&
nal_type
==
H264Frame
::
NAL_IDR
);
void
H264RtpEncoder
::
makeH264Rtp
(
const
void
*
data
,
unsigned
int
len
,
bool
mark
,
bool
gop_pos
,
uint32_t
uiStamp
)
{
RtpCodec
::
inputRtp
(
makeRtp
(
getTrackType
(),
data
,
len
,
mark
,
uiStamp
),
gop_pos
);
}
}
//namespace mediakit
src/Extension/H264Rtp.h
查看文件 @
3acfb15c
...
...
@@ -78,7 +78,7 @@ public:
*/
void
inputFrame
(
const
Frame
::
Ptr
&
frame
)
override
;
private
:
void
makeH264Rtp
(
int
nal_type
,
const
void
*
pData
,
unsigned
int
uiLen
,
bool
bMark
,
bool
first_packet
,
uint32_t
uiStamp
);
void
makeH264Rtp
(
const
void
*
pData
,
unsigned
int
uiLen
,
bool
bMark
,
bool
gop_pos
,
uint32_t
uiStamp
);
};
}
//namespace mediakit{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论