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
83400290
Commit
83400290
authored
Apr 04, 2020
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
整理rtmp相关代码
parent
7019e471
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
106 行增加
和
104 行删除
+106
-104
src/Extension/AACRtmp.cpp
+19
-2
src/Extension/Factory.cpp
+8
-4
src/Extension/H264Rtmp.cpp
+75
-20
src/Rtmp/Rtmp.h
+4
-78
没有找到文件。
src/Extension/AACRtmp.cpp
查看文件 @
83400290
...
...
@@ -9,6 +9,7 @@
*/
#include "AACRtmp.h"
#include "Rtmp/Rtmp.h"
namespace
mediakit
{
...
...
@@ -24,9 +25,25 @@ AACFrame::Ptr AACRtmpDecoder::obtainFrame() {
return
frame
;
}
static
string
getAacCfg
(
const
RtmpPacket
&
thiz
)
{
string
ret
;
if
(
thiz
.
getMediaType
()
!=
FLV_CODEC_AAC
)
{
return
ret
;
}
if
(
!
thiz
.
isCfgFrame
())
{
return
ret
;
}
if
(
thiz
.
strBuf
.
size
()
<
4
)
{
WarnL
<<
"bad aac cfg!"
;
return
ret
;
}
ret
=
thiz
.
strBuf
.
substr
(
2
,
2
);
return
ret
;
}
bool
AACRtmpDecoder
::
inputRtmp
(
const
RtmpPacket
::
Ptr
&
pkt
,
bool
key_pos
)
{
if
(
pkt
->
isCfgFrame
())
{
_aac_cfg
=
pkt
->
getAacCfg
(
);
_aac_cfg
=
getAacCfg
(
*
pkt
);
return
false
;
}
if
(
!
_aac_cfg
.
empty
())
{
...
...
@@ -126,7 +143,7 @@ void AACRtmpEncoder::makeAudioConfigPkt() {
break
;
}
uint8_t
flvSampleBit
=
iSampleBit
==
16
;
uint8_t
flvAudioType
=
10
;
//aac
uint8_t
flvAudioType
=
FLV_CODEC_AAC
;
_ui8AudioFlags
=
(
flvAudioType
<<
4
)
|
(
flvSampleRate
<<
2
)
|
(
flvSampleBit
<<
1
)
|
flvStereoOrMono
;
...
...
src/Extension/Factory.cpp
查看文件 @
83400290
...
...
@@ -9,7 +9,9 @@
*/
#include "Factory.h"
#include "Rtmp/Rtmp.h"
#include "H264Rtmp.h"
#include "H265Rtmp.h"
#include "AACRtmp.h"
#include "H264Rtp.h"
#include "AACRtp.h"
...
...
@@ -173,9 +175,9 @@ CodecId Factory::getCodecIdByAmf(const AMFValue &val){
if
(
val
.
type
()
!=
AMF_NULL
){
auto
type_id
=
val
.
as_integer
();
switch
(
type_id
){
case
7
:
return
CodecH264
;
case
10
:
return
CodecAAC
;
case
12
:
return
CodecH265
;
case
FLV_CODEC_H264
:
return
CodecH264
;
case
FLV_CODEC_AAC
:
return
CodecAAC
;
case
FLV_CODEC_H265
:
return
CodecH265
;
default
:
WarnL
<<
"暂不支持该Amf:"
<<
type_id
;
return
CodecInvalid
;
...
...
@@ -194,6 +196,8 @@ RtmpCodec::Ptr Factory::getRtmpCodecByTrack(const Track::Ptr &track) {
return
std
::
make_shared
<
H264RtmpEncoder
>
(
track
);
case
CodecAAC
:
return
std
::
make_shared
<
AACRtmpEncoder
>
(
track
);
case
CodecH265
:
return
std
::
make_shared
<
H265RtmpEncoder
>
(
track
);
default
:
WarnL
<<
"暂不支持该CodecId:"
<<
track
->
getCodecName
();
return
nullptr
;
...
...
@@ -204,7 +208,7 @@ AMFValue Factory::getAmfByCodecId(CodecId codecId) {
switch
(
codecId
){
case
CodecAAC
:
return
AMFValue
(
"mp4a"
);
case
CodecH264
:
return
AMFValue
(
"avc1"
);
case
CodecH265
:
return
AMFValue
(
12
);
case
CodecH265
:
return
AMFValue
(
FLV_CODEC_H265
);
default
:
return
AMFValue
(
AMF_NULL
);
}
}
...
...
src/Extension/H264Rtmp.cpp
查看文件 @
83400290
...
...
@@ -9,7 +9,6 @@
*/
#include "H264Rtmp.h"
namespace
mediakit
{
H264RtmpDecoder
::
H264RtmpDecoder
()
{
...
...
@@ -28,11 +27,74 @@ bool H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos) {
return
decodeRtmp
(
rtmp
);
}
/**
* 返回不带0x00 00 00 01头的sps
* @return
*/
static
string
getH264SPS
(
const
RtmpPacket
&
thiz
)
{
string
ret
;
if
(
thiz
.
getMediaType
()
!=
FLV_CODEC_H264
)
{
return
ret
;
}
if
(
!
thiz
.
isCfgFrame
())
{
return
ret
;
}
if
(
thiz
.
strBuf
.
size
()
<
13
)
{
WarnL
<<
"bad H264 cfg!"
;
return
ret
;
}
uint16_t
sps_size
;
memcpy
(
&
sps_size
,
thiz
.
strBuf
.
data
()
+
11
,
2
);
sps_size
=
ntohs
(
sps_size
);
if
((
int
)
thiz
.
strBuf
.
size
()
<
13
+
sps_size
)
{
WarnL
<<
"bad H264 cfg!"
;
return
ret
;
}
ret
.
assign
(
thiz
.
strBuf
.
data
()
+
13
,
sps_size
);
return
ret
;
}
/**
* 返回不带0x00 00 00 01头的pps
* @return
*/
static
string
getH264PPS
(
const
RtmpPacket
&
thiz
)
{
string
ret
;
if
(
thiz
.
getMediaType
()
!=
FLV_CODEC_H264
)
{
return
ret
;
}
if
(
!
thiz
.
isCfgFrame
())
{
return
ret
;
}
if
(
thiz
.
strBuf
.
size
()
<
13
)
{
WarnL
<<
"bad H264 cfg!"
;
return
ret
;
}
uint16_t
sps_size
;
memcpy
(
&
sps_size
,
thiz
.
strBuf
.
data
()
+
11
,
2
);
sps_size
=
ntohs
(
sps_size
);
if
((
int
)
thiz
.
strBuf
.
size
()
<
13
+
sps_size
+
1
+
2
)
{
WarnL
<<
"bad H264 cfg!"
;
return
ret
;
}
uint16_t
pps_size
;
memcpy
(
&
pps_size
,
thiz
.
strBuf
.
data
()
+
13
+
sps_size
+
1
,
2
);
pps_size
=
ntohs
(
pps_size
);
if
((
int
)
thiz
.
strBuf
.
size
()
<
13
+
sps_size
+
1
+
2
+
pps_size
)
{
WarnL
<<
"bad H264 cfg!"
;
return
ret
;
}
ret
.
assign
(
thiz
.
strBuf
.
data
()
+
13
+
sps_size
+
1
+
2
,
pps_size
);
return
ret
;
}
bool
H264RtmpDecoder
::
decodeRtmp
(
const
RtmpPacket
::
Ptr
&
pkt
)
{
if
(
pkt
->
isCfgFrame
())
{
//缓存sps pps,后续插入到I帧之前
_sps
=
pkt
->
getH264SPS
(
);
_pps
=
pkt
->
getH264PPS
(
);
_sps
=
getH264SPS
(
*
pkt
);
_pps
=
getH264PPS
(
*
pkt
);
onGetH264
(
_sps
.
data
(),
_sps
.
size
(),
pkt
->
timeStamp
,
pkt
->
timeStamp
);
onGetH264
(
_pps
.
data
(),
_pps
.
size
(),
pkt
->
timeStamp
,
pkt
->
timeStamp
);
return
false
;
...
...
@@ -61,6 +123,9 @@ bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
}
inline
void
H264RtmpDecoder
::
onGetH264
(
const
char
*
pcData
,
int
iLen
,
uint32_t
dts
,
uint32_t
pts
)
{
if
(
iLen
==
0
){
return
;
}
#if 1
_h264frame
->
_dts
=
dts
;
_h264frame
->
_pts
=
pts
;
...
...
@@ -77,8 +142,6 @@ inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dt
#endif
}
////////////////////////////////////////////////////////////////////////
H264RtmpEncoder
::
H264RtmpEncoder
(
const
Track
::
Ptr
&
track
)
{
...
...
@@ -135,7 +198,7 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
if
(
!
_lastPacket
)
{
//I or P or B frame
int8_t
flags
=
7
;
//h.264
int8_t
flags
=
FLV_CODEC_H264
;
bool
is_config
=
false
;
flags
|=
(((
frame
->
configFrame
()
||
frame
->
keyFrame
())
?
FLV_KEY_FRAME
:
FLV_INTER_FRAME
)
<<
4
);
...
...
@@ -159,35 +222,33 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
_lastPacket
->
bodySize
=
_lastPacket
->
strBuf
.
size
();
}
void
H264RtmpEncoder
::
makeVideoConfigPkt
()
{
int8_t
flags
=
7
;
//h.264
int8_t
flags
=
FLV_CODEC_H264
;
flags
|=
(
FLV_KEY_FRAME
<<
4
);
bool
is_config
=
true
;
RtmpPacket
::
Ptr
rtmpPkt
=
ResourcePoolHelper
<
RtmpPacket
>::
obtainObj
();
rtmpPkt
->
strBuf
.
clear
();
//
////////
header
//header
rtmpPkt
->
strBuf
.
push_back
(
flags
);
rtmpPkt
->
strBuf
.
push_back
(
!
is_config
);
//cts
rtmpPkt
->
strBuf
.
append
(
"\x0\x0\x0"
,
3
);
//
//////////sps
//
AVCDecoderConfigurationRecord start
rtmpPkt
->
strBuf
.
push_back
(
1
);
// version
//DebugL<<hexdump(_sps.data(), _sps.size());
rtmpPkt
->
strBuf
.
push_back
(
_sps
[
1
]);
// profile
rtmpPkt
->
strBuf
.
push_back
(
_sps
[
2
]);
// compat
rtmpPkt
->
strBuf
.
push_back
(
_sps
[
3
]);
// level
rtmpPkt
->
strBuf
.
push_back
(
0xff
);
// 6 bits reserved + 2 bits nal size length - 1 (11)
rtmpPkt
->
strBuf
.
push_back
(
0xe1
);
// 3 bits reserved + 5 bits number of sps (00001)
//sps
uint16_t
size
=
_sps
.
size
();
size
=
htons
(
size
);
rtmpPkt
->
strBuf
.
append
((
char
*
)
&
size
,
2
);
rtmpPkt
->
strBuf
.
append
(
_sps
);
/////////////pps
//pps
rtmpPkt
->
strBuf
.
push_back
(
1
);
// version
size
=
_pps
.
size
();
size
=
htons
(
size
);
...
...
@@ -202,10 +263,4 @@ void H264RtmpEncoder::makeVideoConfigPkt() {
RtmpCodec
::
inputRtmp
(
rtmpPkt
,
false
);
}
}
//namespace mediakit
src/Rtmp/Rtmp.h
查看文件 @
83400290
...
...
@@ -72,6 +72,10 @@ using namespace toolkit;
#define FLV_KEY_FRAME 1
#define FLV_INTER_FRAME 2
#define FLV_CODEC_AAC 10
#define FLV_CODEC_H264 7
#define FLV_CODEC_H265 12
namespace
mediakit
{
#if defined(_WIN32)
...
...
@@ -200,84 +204,6 @@ public:
const
static
int
channel
[]
=
{
1
,
2
};
return
channel
[
flvStereoOrMono
];
}
/**
* 返回不带0x00 00 00 01头的sps
* @return
*/
string
getH264SPS
()
const
{
string
ret
;
if
(
getMediaType
()
!=
7
)
{
return
ret
;
}
if
(
!
isCfgFrame
())
{
return
ret
;
}
if
(
strBuf
.
size
()
<
13
)
{
WarnL
<<
"bad H264 cfg!"
;
return
ret
;
}
uint16_t
sps_size
;
memcpy
(
&
sps_size
,
strBuf
.
data
()
+
11
,
2
);
sps_size
=
ntohs
(
sps_size
);
if
((
int
)
strBuf
.
size
()
<
13
+
sps_size
)
{
WarnL
<<
"bad H264 cfg!"
;
return
ret
;
}
ret
.
assign
(
strBuf
.
data
()
+
13
,
sps_size
);
return
ret
;
}
/**
* 返回不带0x00 00 00 01头的pps
* @return
*/
string
getH264PPS
()
const
{
string
ret
;
if
(
getMediaType
()
!=
7
)
{
return
ret
;
}
if
(
!
isCfgFrame
())
{
return
ret
;
}
if
(
strBuf
.
size
()
<
13
)
{
WarnL
<<
"bad H264 cfg!"
;
return
ret
;
}
uint16_t
sps_size
;
memcpy
(
&
sps_size
,
strBuf
.
data
()
+
11
,
2
);
sps_size
=
ntohs
(
sps_size
);
if
((
int
)
strBuf
.
size
()
<
13
+
sps_size
+
1
+
2
)
{
WarnL
<<
"bad H264 cfg!"
;
return
ret
;
}
uint16_t
pps_size
;
memcpy
(
&
pps_size
,
strBuf
.
data
()
+
13
+
sps_size
+
1
,
2
);
pps_size
=
ntohs
(
pps_size
);
if
((
int
)
strBuf
.
size
()
<
13
+
sps_size
+
1
+
2
+
pps_size
)
{
WarnL
<<
"bad H264 cfg!"
;
return
ret
;
}
ret
.
assign
(
strBuf
.
data
()
+
13
+
sps_size
+
1
+
2
,
pps_size
);
return
ret
;
}
string
getAacCfg
()
const
{
string
ret
;
if
(
getMediaType
()
!=
10
)
{
return
ret
;
}
if
(
!
isCfgFrame
())
{
return
ret
;
}
if
(
strBuf
.
size
()
<
4
)
{
WarnL
<<
"bad aac cfg!"
;
return
ret
;
}
ret
=
strBuf
.
substr
(
2
,
2
);
return
ret
;
}
};
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论