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
e445c7e1
Commit
e445c7e1
authored
Mar 22, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
避免内存拷贝,大幅提高rtmp服务器的性能
parent
2b4d8a12
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
86 行增加
和
55 行删除
+86
-55
src/Rtmp/Rtmp.h
+9
-2
src/Rtmp/RtmpProtocol.cpp
+73
-50
src/Rtmp/RtmpProtocol.h
+1
-0
src/Rtmp/RtmpPusher.cpp
+2
-2
src/Rtmp/RtmpSession.cpp
+1
-1
没有找到文件。
src/Rtmp/Rtmp.h
查看文件 @
e445c7e1
...
...
@@ -31,6 +31,7 @@
#include <cstdlib>
#include "Util/util.h"
#include "Util/logger.h"
#include "Network/Buffer.h"
#include "Network/sockutil.h"
using
namespace
toolkit
;
...
...
@@ -127,7 +128,7 @@ public:
#pragma pack(pop)
#endif // defined(_WIN32)
class
RtmpPacket
{
class
RtmpPacket
:
public
Buffer
{
public
:
typedef
std
::
shared_ptr
<
RtmpPacket
>
Ptr
;
uint8_t
typeId
;
...
...
@@ -139,7 +140,13 @@ public:
uint32_t
streamId
;
uint32_t
chunkId
;
std
::
string
strBuf
;
public
:
char
*
data
()
const
override
{
return
(
char
*
)
strBuf
.
data
();
}
uint32_t
size
()
const
override
{
return
strBuf
.
size
();
};
public
:
RtmpPacket
()
=
default
;
RtmpPacket
(
const
RtmpPacket
&
that
)
=
default
;
...
...
src/Rtmp/RtmpProtocol.cpp
查看文件 @
e445c7e1
...
...
@@ -188,60 +188,83 @@ void RtmpProtocol::sendRequest(int iCmd, const string& str) {
sendRtmp
(
iCmd
,
_ui32StreamId
,
str
,
0
,
CHUNK_SERVER_REQUEST
);
}
class
BufferPartial
:
public
Buffer
{
public
:
BufferPartial
(
const
Buffer
::
Ptr
&
buffer
,
uint32_t
offset
,
uint32_t
size
){
_buffer
=
buffer
;
_data
=
buffer
->
data
()
+
offset
;
_size
=
size
;
}
~
BufferPartial
(){}
char
*
data
()
const
override
{
return
_data
;
}
uint32_t
size
()
const
override
{
return
_size
;
}
private
:
Buffer
::
Ptr
_buffer
;
char
*
_data
;
uint32_t
_size
;
};
void
RtmpProtocol
::
sendRtmp
(
uint8_t
ui8Type
,
uint32_t
ui32StreamId
,
const
std
::
string
&
strBuf
,
uint32_t
ui32TimeStamp
,
int
iChunkId
)
{
if
(
iChunkId
<
2
||
iChunkId
>
63
)
{
auto
strErr
=
StrPrinter
<<
"不支持发送该类型的块流 ID:"
<<
iChunkId
<<
endl
;
throw
std
::
runtime_error
(
strErr
);
}
const
std
::
string
&
strBuf
,
uint32_t
ui32TimeStamp
,
int
iChunkId
)
{
sendRtmp
(
ui8Type
,
ui32StreamId
,
std
::
make_shared
<
BufferString
>
(
strBuf
),
ui32TimeStamp
,
iChunkId
);
}
bool
bExtStamp
=
ui32TimeStamp
>=
0xFFFFFF
;
RtmpHeader
header
;
header
.
flags
=
(
iChunkId
&
0x3f
)
|
(
0
<<
6
);
header
.
typeId
=
ui8Type
;
set_be24
(
header
.
timeStamp
,
bExtStamp
?
0xFFFFFF
:
ui32TimeStamp
);
set_be24
(
header
.
bodySize
,
strBuf
.
size
());
set_le32
(
header
.
streamId
,
ui32StreamId
);
//估算rtmp包数据大小
uint32_t
capacity
=
((
bExtStamp
?
5
:
1
)
*
(
1
+
(
strBuf
.
size
()
/
_iChunkLenOut
)))
+
strBuf
.
size
()
+
sizeof
(
header
);
uint32_t
totalSize
=
0
;
BufferRaw
::
Ptr
buffer
=
obtainBuffer
();
buffer
->
setCapacity
(
capacity
);
memcpy
(
buffer
->
data
()
+
totalSize
,(
char
*
)
&
header
,
sizeof
(
header
));
totalSize
+=
sizeof
(
header
);
char
acExtStamp
[
4
];
if
(
bExtStamp
)
{
//扩展时间戳
set_be32
(
acExtStamp
,
ui32TimeStamp
);
}
size_t
pos
=
0
;
while
(
pos
<
strBuf
.
size
())
{
if
(
pos
)
{
uint8_t
flags
=
(
iChunkId
&
0x3f
)
|
(
3
<<
6
);
memcpy
(
buffer
->
data
()
+
totalSize
,
&
flags
,
1
);
totalSize
+=
1
;
}
if
(
bExtStamp
)
{
//扩展时间戳
memcpy
(
buffer
->
data
()
+
totalSize
,
acExtStamp
,
4
);
totalSize
+=
4
;
}
size_t
chunk
=
min
(
_iChunkLenOut
,
strBuf
.
size
()
-
pos
);
memcpy
(
buffer
->
data
()
+
totalSize
,
strBuf
.
data
()
+
pos
,
chunk
);
totalSize
+=
chunk
;
pos
+=
chunk
;
}
buffer
->
setSize
(
totalSize
);
onSendRawData
(
buffer
);
_ui32ByteSent
+=
totalSize
;
if
(
_ui32WinSize
>
0
&&
_ui32ByteSent
-
_ui32LastSent
>=
_ui32WinSize
)
{
_ui32LastSent
=
_ui32ByteSent
;
sendAcknowledgement
(
_ui32ByteSent
);
}
void
RtmpProtocol
::
sendRtmp
(
uint8_t
ui8Type
,
uint32_t
ui32StreamId
,
const
Buffer
::
Ptr
&
buf
,
uint32_t
ui32TimeStamp
,
int
iChunkId
){
if
(
iChunkId
<
2
||
iChunkId
>
63
)
{
auto
strErr
=
StrPrinter
<<
"不支持发送该类型的块流 ID:"
<<
iChunkId
<<
endl
;
throw
std
::
runtime_error
(
strErr
);
}
bool
bExtStamp
=
ui32TimeStamp
>=
0xFFFFFF
;
RtmpHeader
header
;
header
.
flags
=
(
iChunkId
&
0x3f
)
|
(
0
<<
6
);
header
.
typeId
=
ui8Type
;
set_be24
(
header
.
timeStamp
,
bExtStamp
?
0xFFFFFF
:
ui32TimeStamp
);
set_be24
(
header
.
bodySize
,
buf
->
size
());
set_le32
(
header
.
streamId
,
ui32StreamId
);
//估算rtmp包数据大小
uint32_t
totalSize
=
0
;
onSendRawData
(
obtainBuffer
((
char
*
)
&
header
,
sizeof
(
header
)));
totalSize
+=
sizeof
(
header
);
char
acExtStamp
[
4
];
if
(
bExtStamp
)
{
//扩展时间戳
set_be32
(
acExtStamp
,
ui32TimeStamp
);
}
size_t
pos
=
0
;
while
(
pos
<
buf
->
size
())
{
if
(
pos
)
{
uint8_t
flags
=
(
iChunkId
&
0x3f
)
|
(
3
<<
6
);
onSendRawData
(
obtainBuffer
((
char
*
)
&
flags
,
1
));
totalSize
+=
1
;
}
if
(
bExtStamp
)
{
//扩展时间戳
onSendRawData
(
obtainBuffer
(
acExtStamp
,
4
));
totalSize
+=
4
;
}
size_t
chunk
=
min
(
_iChunkLenOut
,
buf
->
size
()
-
pos
);
onSendRawData
(
std
::
make_shared
<
BufferPartial
>
(
buf
,
pos
,
chunk
));
totalSize
+=
chunk
;
pos
+=
chunk
;
}
_ui32ByteSent
+=
totalSize
;
if
(
_ui32WinSize
>
0
&&
_ui32ByteSent
-
_ui32LastSent
>=
_ui32WinSize
)
{
_ui32LastSent
=
_ui32ByteSent
;
sendAcknowledgement
(
_ui32ByteSent
);
}
}
void
RtmpProtocol
::
onParseRtmp
(
const
char
*
pcRawData
,
int
iSize
)
{
_strRcvBuf
.
append
(
pcRawData
,
iSize
);
auto
cb
=
_nextHandle
;
...
...
src/Rtmp/RtmpProtocol.h
查看文件 @
e445c7e1
...
...
@@ -74,6 +74,7 @@ protected:
void
sendRequest
(
int
iCmd
,
const
string
&
str
);
void
sendResponse
(
int
iType
,
const
string
&
str
);
void
sendRtmp
(
uint8_t
ui8Type
,
uint32_t
ui32StreamId
,
const
std
::
string
&
strBuf
,
uint32_t
ui32TimeStamp
,
int
iChunkID
);
void
sendRtmp
(
uint8_t
ui8Type
,
uint32_t
ui32StreamId
,
const
Buffer
::
Ptr
&
buffer
,
uint32_t
ui32TimeStamp
,
int
iChunkID
);
protected
:
int
_iReqID
=
0
;
uint32_t
_ui32StreamId
=
STREAM_CONTROL
;
...
...
src/Rtmp/RtmpPusher.cpp
查看文件 @
e445c7e1
...
...
@@ -200,7 +200,7 @@ inline void RtmpPusher::send_metaData(){
sendRequest
(
MSG_DATA
,
enc
.
data
());
src
->
getConfigFrame
([
&
](
const
RtmpPacket
::
Ptr
&
pkt
){
sendRtmp
(
pkt
->
typeId
,
_ui32StreamId
,
pkt
->
strBuf
,
pkt
->
timeStamp
,
pkt
->
chunkId
);
sendRtmp
(
pkt
->
typeId
,
_ui32StreamId
,
pkt
,
pkt
->
timeStamp
,
pkt
->
chunkId
);
});
_pRtmpReader
=
src
->
getRing
()
->
attach
(
getPoller
());
...
...
@@ -210,7 +210,7 @@ inline void RtmpPusher::send_metaData(){
if
(
!
strongSelf
)
{
return
;
}
strongSelf
->
sendRtmp
(
pkt
->
typeId
,
strongSelf
->
_ui32StreamId
,
pkt
->
strBuf
,
pkt
->
timeStamp
,
pkt
->
chunkId
);
strongSelf
->
sendRtmp
(
pkt
->
typeId
,
strongSelf
->
_ui32StreamId
,
pkt
,
pkt
->
timeStamp
,
pkt
->
chunkId
);
});
_pRtmpReader
->
setDetachCB
([
weakSelf
](){
auto
strongSelf
=
weakSelf
.
lock
();
...
...
src/Rtmp/RtmpSession.cpp
查看文件 @
e445c7e1
...
...
@@ -547,7 +547,7 @@ void RtmpSession::onSendMedia(const RtmpPacket::Ptr &pkt) {
CLEAR_ARR
(
_aui32FirstStamp
);
modifiedStamp
=
0
;
}
sendRtmp
(
pkt
->
typeId
,
pkt
->
streamId
,
pkt
->
strBuf
,
modifiedStamp
,
pkt
->
chunkId
);
sendRtmp
(
pkt
->
typeId
,
pkt
->
streamId
,
pkt
,
modifiedStamp
,
pkt
->
chunkId
);
}
void
RtmpSession
::
doDelay
(
int
delaySec
,
const
std
::
function
<
void
()
>
&
fun
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论