Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
胡斌
/
srs
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
winlin
2014-12-01 22:39:22 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
31eb9bf1c12415f95a4062c5447161ae8e6342f8
31eb9bf1
1 parent
22524f39
for bug #237, extract a queue recv thread.
隐藏空白字符变更
内嵌
并排对比
正在显示
2 个修改的文件
包含
130 行增加
和
63 行删除
trunk/src/app/srs_app_recv_thread.cpp
trunk/src/app/srs_app_recv_thread.hpp
trunk/src/app/srs_app_recv_thread.cpp
查看文件 @
31eb9bf
...
...
@@ -26,95 +26,69 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_protocol_rtmp.hpp>
#include <srs_protocol_stack.hpp>
SrsQueueRecvThread
::
SrsQueueRecvThread
(
SrsRtmpServer
*
rtmp_sdk
)
ISrsMessageHandler
::
ISrsMessageHandler
(
)
{
rtmp
=
rtmp_sdk
;
trd
=
new
SrsThread
(
this
,
0
,
true
);
}
SrsQueueRecvThread
::~
SrsQueueRecvThread
()
ISrsMessageHandler
::~
ISrsMessageHandler
()
{
// stop recv thread.
stop
();
// destroy the thread.
srs_freep
(
trd
);
// clear all messages.
std
::
vector
<
SrsMessage
*>::
iterator
it
;
for
(
it
=
queue
.
begin
();
it
!=
queue
.
end
();
++
it
)
{
SrsMessage
*
msg
=
*
it
;
srs_freep
(
msg
);
}
queue
.
clear
();
}
bool
SrsQueueRecvThread
::
empty
(
)
SrsRecvThread
::
SrsRecvThread
(
ISrsMessageHandler
*
msg_handler
,
SrsRtmpServer
*
rtmp_sdk
)
{
return
queue
.
empty
();
handler
=
msg_handler
;
rtmp
=
rtmp_sdk
;
trd
=
new
SrsThread
(
this
,
0
,
true
);
}
int
SrsQueueRecvThread
::
size
()
SrsRecvThread
::~
SrsRecvThread
()
{
return
(
int
)
queue
.
size
();
}
// stop recv thread.
stop
();
SrsMessage
*
SrsQueueRecvThread
::
pump
()
{
srs_assert
(
!
queue
.
empty
());
SrsMessage
*
msg
=
*
queue
.
begin
();
queue
.
erase
(
queue
.
begin
());
return
msg
;
// destroy the thread.
srs_freep
(
trd
);
}
int
Srs
Queue
RecvThread
::
start
()
int
SrsRecvThread
::
start
()
{
return
trd
->
start
();
}
void
Srs
Queue
RecvThread
::
stop
()
void
SrsRecvThread
::
stop
()
{
trd
->
stop
();
}
int
Srs
Queue
RecvThread
::
cycle
()
int
SrsRecvThread
::
cycle
()
{
int
ret
=
ERROR_SUCCESS
;
// we only recv one message and then process it,
// for the message may cause the thread to stop,
// when stop, the thread is freed, so the messages
// are dropped.
if
(
!
queue
.
empty
())
{
if
(
!
handler
->
can_handle
())
{
st_usleep
(
SRS_CONSTS_RTMP_PULSE_TIMEOUT_US
);
return
ret
;
}
SrsMessage
*
msg
=
NULL
;
if
((
ret
=
rtmp
->
recv_message
(
&
msg
))
!=
ERROR_SUCCESS
)
{
if
(
!
srs_is_client_gracefully_close
(
ret
))
{
srs_error
(
"recv client control message failed. ret=%d"
,
ret
);
}
// we use no timeout to recv, should never got any error.
trd
->
stop_loop
();
return
ret
;
}
srs_verbose
(
"play loop recv message. ret=%d"
,
ret
);
// put into queue, the send thread will get and process it,
// @see SrsRtmpConn::process_play_control_msg
queue
.
push_back
(
msg
);
handler
->
handle
(
msg
);
return
ret
;
}
void
Srs
Queue
RecvThread
::
on_thread_start
()
void
SrsRecvThread
::
on_thread_start
()
{
// the multiple messages writev improve performance large,
// but the timeout recv will cause 33% sys call performance,
...
...
@@ -122,19 +96,75 @@ void SrsQueueRecvThread::on_thread_start()
// @see https://github.com/winlinvip/simple-rtmp-server/issues/194
// @see: https://github.com/winlinvip/simple-rtmp-server/issues/217
rtmp
->
set_recv_timeout
(
ST_UTIME_NO_TIMEOUT
);
// disable the protocol auto response,
// disable the protocol auto response,
// for the isolate recv thread should never send any messages.
rtmp
->
set_auto_response
(
false
);
}
void
Srs
Queue
RecvThread
::
on_thread_stop
()
void
SrsRecvThread
::
on_thread_stop
()
{
// enable the protocol auto response,
// for the isolate recv thread terminated.
rtmp
->
set_auto_response
(
true
);
// reset the timeout to pulse mode.
rtmp
->
set_recv_timeout
(
SRS_CONSTS_RTMP_PULSE_TIMEOUT_US
);
}
SrsQueueRecvThread
::
SrsQueueRecvThread
(
SrsRtmpServer
*
rtmp_sdk
)
:
SrsRecvThread
(
this
,
rtmp_sdk
)
{
}
SrsQueueRecvThread
::~
SrsQueueRecvThread
()
{
stop
();
// clear all messages.
std
::
vector
<
SrsMessage
*>::
iterator
it
;
for
(
it
=
queue
.
begin
();
it
!=
queue
.
end
();
++
it
)
{
SrsMessage
*
msg
=
*
it
;
srs_freep
(
msg
);
}
queue
.
clear
();
}
bool
SrsQueueRecvThread
::
empty
()
{
return
queue
.
empty
();
}
int
SrsQueueRecvThread
::
size
()
{
return
(
int
)
queue
.
size
();
}
SrsMessage
*
SrsQueueRecvThread
::
pump
()
{
srs_assert
(
!
queue
.
empty
());
SrsMessage
*
msg
=
*
queue
.
begin
();
queue
.
erase
(
queue
.
begin
());
return
msg
;
}
bool
SrsQueueRecvThread
::
can_handle
()
{
// we only recv one message and then process it,
// for the message may cause the thread to stop,
// when stop, the thread is freed, so the messages
// are dropped.
return
empty
();
}
int
SrsQueueRecvThread
::
handle
(
SrsMessage
*
msg
)
{
// put into queue, the send thread will get and process it,
// @see SrsRtmpConn::process_play_control_msg
queue
.
push_back
(
msg
);
return
ERROR_SUCCESS
;
}
...
...
trunk/src/app/srs_app_recv_thread.hpp
查看文件 @
31eb9bf
...
...
@@ -38,16 +38,57 @@ class SrsRtmpServer;
class
SrsMessage
;
/**
* for the recv thread to handle the message.
*/
class
ISrsMessageHandler
{
public
:
ISrsMessageHandler
();
virtual
~
ISrsMessageHandler
();
public
:
/**
* whether the handler can handle,
* for example, when queue recv handler got an message,
* it wait the user to process it, then the recv thread
* never recv message util the handler is ok.
*/
virtual
bool
can_handle
()
=
0
;
/**
* process the received message.
*/
virtual
int
handle
(
SrsMessage
*
msg
)
=
0
;
};
/**
* the recv thread, use message handler to handle each received message.
*/
class
SrsRecvThread
:
public
ISrsThreadHandler
{
protected
:
SrsThread
*
trd
;
ISrsMessageHandler
*
handler
;
SrsRtmpServer
*
rtmp
;
public
:
SrsRecvThread
(
ISrsMessageHandler
*
msg_handler
,
SrsRtmpServer
*
rtmp_sdk
);
virtual
~
SrsRecvThread
();
public
:
virtual
int
start
();
virtual
void
stop
();
virtual
int
cycle
();
public
:
virtual
void
on_thread_start
();
virtual
void
on_thread_stop
();
};
/**
* the recv thread used to replace the timeout recv,
* which hurt performance for the epoll_ctrl is frequently used.
* @see: SrsRtmpConn::playing
* @see: https://github.com/winlinvip/simple-rtmp-server/issues/217
*/
class
SrsQueueRecvThread
:
public
ISrsThreadHandler
class
SrsQueueRecvThread
:
virtual
public
ISrsMessageHandler
,
virtual
public
SrsRecvThread
{
private
:
SrsThread
*
trd
;
SrsRtmpServer
*
rtmp
;
std
::
vector
<
SrsMessage
*>
queue
;
public
:
SrsQueueRecvThread
(
SrsRtmpServer
*
rtmp_sdk
);
...
...
@@ -57,12 +98,8 @@ public:
virtual
int
size
();
virtual
SrsMessage
*
pump
();
public
:
virtual
int
start
();
virtual
void
stop
();
virtual
int
cycle
();
public
:
virtual
void
on_thread_start
();
virtual
void
on_thread_stop
();
virtual
bool
can_handle
();
virtual
int
handle
(
SrsMessage
*
msg
);
};
#endif
...
...
请
注册
或
登录
后发表评论