2014-11-22 09:58:02 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2013-2014 winlin
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <srs_app_recv_thread.hpp>
|
|
|
|
|
|
|
|
#include <srs_protocol_rtmp.hpp>
|
|
|
|
#include <srs_protocol_stack.hpp>
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
ISrsMessageHandler::ISrsMessageHandler()
|
2014-11-22 09:58:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
ISrsMessageHandler::~ISrsMessageHandler()
|
2014-11-22 09:58:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:45:45 +00:00
|
|
|
SrsRecvThread::SrsRecvThread(ISrsMessageHandler* msg_handler, SrsRtmpServer* rtmp_sdk, int timeout_ms)
|
2014-11-22 09:58:02 +00:00
|
|
|
{
|
2014-12-01 14:45:45 +00:00
|
|
|
timeout = timeout_ms;
|
2014-12-01 14:39:22 +00:00
|
|
|
handler = msg_handler;
|
|
|
|
rtmp = rtmp_sdk;
|
|
|
|
trd = new SrsThread(this, 0, true);
|
2014-11-22 09:58:02 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
SrsRecvThread::~SrsRecvThread()
|
2014-11-22 11:15:40 +00:00
|
|
|
{
|
2014-12-01 14:39:22 +00:00
|
|
|
// stop recv thread.
|
|
|
|
stop();
|
2014-11-22 11:15:40 +00:00
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
// destroy the thread.
|
|
|
|
srs_freep(trd);
|
2014-11-22 09:58:02 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
int SrsRecvThread::start()
|
2014-11-22 09:58:02 +00:00
|
|
|
{
|
|
|
|
return trd->start();
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
void SrsRecvThread::stop()
|
2014-11-22 09:58:02 +00:00
|
|
|
{
|
|
|
|
trd->stop();
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
int SrsRecvThread::cycle()
|
2014-11-22 09:58:02 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-12-01 14:39:22 +00:00
|
|
|
|
|
|
|
if (!handler->can_handle()) {
|
2014-12-01 14:45:45 +00:00
|
|
|
st_usleep(timeout * 1000);
|
2014-11-22 11:15:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-12-01 14:39:22 +00:00
|
|
|
|
2014-11-22 09:58:02 +00:00
|
|
|
SrsMessage* msg = NULL;
|
2014-12-01 14:39:22 +00:00
|
|
|
|
2014-11-22 09:58:02 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-12-01 14:39:22 +00:00
|
|
|
|
2014-11-22 09:58:02 +00:00
|
|
|
// we use no timeout to recv, should never got any error.
|
|
|
|
trd->stop_loop();
|
2014-12-01 14:39:22 +00:00
|
|
|
|
2014-11-22 09:58:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
srs_verbose("play loop recv message. ret=%d", ret);
|
2014-12-01 14:39:22 +00:00
|
|
|
|
|
|
|
handler->handle(msg);
|
|
|
|
|
2014-11-22 09:58:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
void SrsRecvThread::on_thread_start()
|
2014-11-22 10:08:45 +00:00
|
|
|
{
|
|
|
|
// the multiple messages writev improve performance large,
|
|
|
|
// but the timeout recv will cause 33% sys call performance,
|
|
|
|
// to use isolate thread to recv, can improve about 33% performance.
|
|
|
|
// @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);
|
2014-12-01 14:39:22 +00:00
|
|
|
|
|
|
|
// disable the protocol auto response,
|
2014-11-22 10:08:45 +00:00
|
|
|
// for the isolate recv thread should never send any messages.
|
|
|
|
rtmp->set_auto_response(false);
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
void SrsRecvThread::on_thread_stop()
|
2014-11-22 10:08:45 +00:00
|
|
|
{
|
|
|
|
// enable the protocol auto response,
|
|
|
|
// for the isolate recv thread terminated.
|
|
|
|
rtmp->set_auto_response(true);
|
2014-12-01 14:39:22 +00:00
|
|
|
|
2014-11-22 10:08:45 +00:00
|
|
|
// reset the timeout to pulse mode.
|
2014-12-01 14:45:45 +00:00
|
|
|
rtmp->set_recv_timeout(timeout * 1000);
|
2014-11-22 10:08:45 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 14:45:45 +00:00
|
|
|
SrsQueueRecvThread::SrsQueueRecvThread(SrsRtmpServer* rtmp_sdk, int timeout_ms)
|
2014-12-01 14:53:03 +00:00
|
|
|
: trd(this, rtmp_sdk, timeout_ms)
|
2014-12-01 14:39:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsQueueRecvThread::~SrsQueueRecvThread()
|
|
|
|
{
|
2014-12-01 14:53:03 +00:00
|
|
|
trd.stop();
|
2014-12-01 14:39:22 +00:00
|
|
|
|
|
|
|
// clear all messages.
|
|
|
|
std::vector<SrsMessage*>::iterator it;
|
|
|
|
for (it = queue.begin(); it != queue.end(); ++it) {
|
|
|
|
SrsMessage* msg = *it;
|
|
|
|
srs_freep(msg);
|
|
|
|
}
|
|
|
|
queue.clear();
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:53:03 +00:00
|
|
|
int SrsQueueRecvThread::start()
|
|
|
|
{
|
|
|
|
return trd.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsQueueRecvThread::stop()
|
|
|
|
{
|
|
|
|
trd.stop();
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
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;
|
|
|
|
}
|