2014-11-22 09:58:02 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2015-04-29 09:38:23 +00:00
|
|
|
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
|
2014-11-22 09:58:02 +00:00
|
|
|
|
|
|
|
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>
|
|
|
|
|
2015-01-23 02:07:20 +00:00
|
|
|
#include <srs_rtmp_sdk.hpp>
|
|
|
|
#include <srs_rtmp_stack.hpp>
|
2014-12-01 15:38:51 +00:00
|
|
|
#include <srs_app_rtmp_conn.hpp>
|
2015-05-23 02:04:28 +00:00
|
|
|
#include <srs_protocol_buffer.hpp>
|
2014-12-03 14:39:25 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2014-12-04 05:43:55 +00:00
|
|
|
#include <srs_core_performance.hpp>
|
2014-12-04 10:21:04 +00:00
|
|
|
#include <srs_app_config.hpp>
|
2014-10-02 11:45:04 +00:00
|
|
|
#include <srs_app_source.hpp>
|
2014-12-04 10:21:04 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2014-12-03 11:27:27 +00:00
|
|
|
|
2014-12-04 10:35:50 +00:00
|
|
|
// the max small bytes to group
|
|
|
|
#define SRS_MR_SMALL_BYTES 4096
|
|
|
|
|
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;
|
2015-05-23 01:49:15 +00:00
|
|
|
trd = new SrsReusableThread2("recv", this);
|
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();
|
|
|
|
}
|
|
|
|
|
2015-05-23 01:49:15 +00:00
|
|
|
void SrsRecvThread::stop_loop()
|
|
|
|
{
|
|
|
|
trd->interrupt();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-05-23 01:20:16 +00:00
|
|
|
while (!trd->interrupted()) {
|
2014-12-02 11:28:36 +00:00
|
|
|
if (!handler->can_handle()) {
|
|
|
|
st_usleep(timeout * 1000);
|
|
|
|
continue;
|
2014-11-22 09:58:02 +00:00
|
|
|
}
|
2014-12-02 11:28:36 +00:00
|
|
|
|
2014-12-05 15:03:52 +00:00
|
|
|
SrsCommonMessage* msg = NULL;
|
2014-12-02 07:21:08 +00:00
|
|
|
|
2014-12-02 11:28:36 +00:00
|
|
|
// recv and handle message
|
|
|
|
ret = rtmp->recv_message(&msg);
|
|
|
|
if (ret == ERROR_SUCCESS) {
|
|
|
|
ret = handler->handle(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != ERROR_SUCCESS) {
|
|
|
|
if (!srs_is_client_gracefully_close(ret)) {
|
|
|
|
srs_error("thread process message failed. ret=%d", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we use no timeout to recv, should never got any error.
|
2015-05-23 01:20:16 +00:00
|
|
|
trd->interrupt();
|
2014-12-02 11:28:36 +00:00
|
|
|
|
|
|
|
// notice the handler got a recv error.
|
|
|
|
handler->on_recv_error(ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
srs_verbose("thread loop recv message. ret=%d", ret);
|
2014-11-22 09:58:02 +00:00
|
|
|
}
|
2014-12-01 14:39:22 +00:00
|
|
|
|
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.
|
2015-04-29 09:06:32 +00:00
|
|
|
// @see https://github.com/simple-rtmp-server/srs/issues/194
|
|
|
|
// @see: https://github.com/simple-rtmp-server/srs/issues/217
|
2014-11-22 10:08:45 +00:00
|
|
|
rtmp->set_recv_timeout(ST_UTIME_NO_TIMEOUT);
|
2014-12-02 09:16:20 +00:00
|
|
|
|
|
|
|
handler->on_thread_start();
|
2014-11-22 10:08:45 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
void SrsRecvThread::on_thread_stop()
|
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-12-02 09:16:20 +00:00
|
|
|
|
|
|
|
handler->on_thread_stop();
|
2014-11-22 10:08:45 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 06:31:42 +00:00
|
|
|
SrsQueueRecvThread::SrsQueueRecvThread(SrsConsumer* consumer, 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
|
|
|
{
|
2015-01-07 06:31:42 +00:00
|
|
|
_consumer = consumer;
|
2014-12-02 09:16:20 +00:00
|
|
|
rtmp = rtmp_sdk;
|
2014-12-02 07:21:08 +00:00
|
|
|
recv_error_code = ERROR_SUCCESS;
|
2014-12-01 14:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsQueueRecvThread::~SrsQueueRecvThread()
|
|
|
|
{
|
2014-10-02 11:45:04 +00:00
|
|
|
stop();
|
2014-12-01 14:39:22 +00:00
|
|
|
|
|
|
|
// clear all messages.
|
2014-12-05 15:03:52 +00:00
|
|
|
std::vector<SrsCommonMessage*>::iterator it;
|
2014-12-01 14:39:22 +00:00
|
|
|
for (it = queue.begin(); it != queue.end(); ++it) {
|
2014-12-05 15:03:52 +00:00
|
|
|
SrsCommonMessage* msg = *it;
|
2014-12-01 14:39:22 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-12-05 15:03:52 +00:00
|
|
|
SrsCommonMessage* SrsQueueRecvThread::pump()
|
2014-12-01 14:39:22 +00:00
|
|
|
{
|
|
|
|
srs_assert(!queue.empty());
|
|
|
|
|
2014-12-05 15:03:52 +00:00
|
|
|
SrsCommonMessage* msg = *queue.begin();
|
2014-12-01 14:39:22 +00:00
|
|
|
|
|
|
|
queue.erase(queue.begin());
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2014-12-02 07:21:08 +00:00
|
|
|
int SrsQueueRecvThread::error_code()
|
|
|
|
{
|
|
|
|
return recv_error_code;
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:39:22 +00:00
|
|
|
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.
|
2014-10-02 12:25:36 +00:00
|
|
|
return empty();
|
2014-12-01 14:39:22 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 15:03:52 +00:00
|
|
|
int SrsQueueRecvThread::handle(SrsCommonMessage* msg)
|
2014-12-01 14:39:22 +00:00
|
|
|
{
|
|
|
|
// put into queue, the send thread will get and process it,
|
|
|
|
// @see SrsRtmpConn::process_play_control_msg
|
|
|
|
queue.push_back(msg);
|
2014-10-02 12:25:36 +00:00
|
|
|
#ifdef SRS_PERF_QUEUE_COND_WAIT
|
|
|
|
if (_consumer) {
|
2014-12-19 01:19:47 +00:00
|
|
|
_consumer->wakeup();
|
2014-10-02 12:25:36 +00:00
|
|
|
}
|
|
|
|
#endif
|
2014-12-01 14:39:22 +00:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2014-12-01 15:38:51 +00:00
|
|
|
|
2014-12-02 07:21:08 +00:00
|
|
|
void SrsQueueRecvThread::on_recv_error(int ret)
|
|
|
|
{
|
2014-10-02 11:51:01 +00:00
|
|
|
recv_error_code = ret;
|
2014-10-02 11:45:04 +00:00
|
|
|
#ifdef SRS_PERF_QUEUE_COND_WAIT
|
|
|
|
if (_consumer) {
|
2014-12-19 01:19:47 +00:00
|
|
|
_consumer->wakeup();
|
2014-10-02 11:45:04 +00:00
|
|
|
}
|
|
|
|
#endif
|
2014-12-02 07:21:08 +00:00
|
|
|
}
|
|
|
|
|
2014-12-02 09:16:20 +00:00
|
|
|
void SrsQueueRecvThread::on_thread_start()
|
|
|
|
{
|
|
|
|
// disable the protocol auto response,
|
|
|
|
// for the isolate recv thread should never send any messages.
|
|
|
|
rtmp->set_auto_response(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsQueueRecvThread::on_thread_stop()
|
|
|
|
{
|
|
|
|
// enable the protocol auto response,
|
|
|
|
// for the isolate recv thread terminated.
|
|
|
|
rtmp->set_auto_response(true);
|
|
|
|
}
|
|
|
|
|
2014-12-01 15:38:51 +00:00
|
|
|
SrsPublishRecvThread::SrsPublishRecvThread(
|
2014-12-04 10:21:04 +00:00
|
|
|
SrsRtmpServer* rtmp_sdk,
|
|
|
|
SrsRequest* _req, int mr_sock_fd, int timeout_ms,
|
2014-12-01 15:38:51 +00:00
|
|
|
SrsRtmpConn* conn, SrsSource* source, bool is_fmle, bool is_edge
|
|
|
|
): trd(this, rtmp_sdk, timeout_ms)
|
|
|
|
{
|
2014-12-02 09:16:20 +00:00
|
|
|
rtmp = rtmp_sdk;
|
2014-12-04 10:21:04 +00:00
|
|
|
|
2014-12-01 15:38:51 +00:00
|
|
|
_conn = conn;
|
|
|
|
_source = source;
|
|
|
|
_is_fmle = is_fmle;
|
|
|
|
_is_edge = is_edge;
|
|
|
|
|
|
|
|
recv_error_code = ERROR_SUCCESS;
|
|
|
|
_nb_msgs = 0;
|
2014-12-03 04:08:29 +00:00
|
|
|
error = st_cond_new();
|
2014-12-04 10:21:04 +00:00
|
|
|
|
|
|
|
req = _req;
|
|
|
|
mr_fd = mr_sock_fd;
|
2014-12-03 14:39:25 +00:00
|
|
|
|
2014-12-04 10:21:04 +00:00
|
|
|
// the mr settings,
|
2015-04-29 09:06:32 +00:00
|
|
|
// @see https://github.com/simple-rtmp-server/srs/issues/241
|
2014-12-04 10:21:04 +00:00
|
|
|
mr = _srs_config->get_mr_enabled(req->vhost);
|
|
|
|
mr_sleep = _srs_config->get_mr_sleep_ms(req->vhost);
|
|
|
|
|
2014-12-12 13:51:06 +00:00
|
|
|
realtime = _srs_config->get_realtime_enabled(req->vhost);
|
|
|
|
|
2014-12-04 10:21:04 +00:00
|
|
|
_srs_config->subscribe(this);
|
2014-12-01 15:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsPublishRecvThread::~SrsPublishRecvThread()
|
|
|
|
{
|
2014-12-04 10:21:04 +00:00
|
|
|
_srs_config->unsubscribe(this);
|
|
|
|
|
2014-12-01 15:38:51 +00:00
|
|
|
trd.stop();
|
2014-12-03 04:08:29 +00:00
|
|
|
st_cond_destroy(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsPublishRecvThread::wait(int timeout_ms)
|
|
|
|
{
|
|
|
|
if (recv_error_code != ERROR_SUCCESS) {
|
|
|
|
return recv_error_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore any return of cond wait.
|
|
|
|
st_cond_timedwait(error, timeout_ms * 1000);
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
2014-12-01 15:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t SrsPublishRecvThread::nb_msgs()
|
|
|
|
{
|
|
|
|
return _nb_msgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsPublishRecvThread::error_code()
|
|
|
|
{
|
|
|
|
return recv_error_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsPublishRecvThread::start()
|
|
|
|
{
|
|
|
|
return trd.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsPublishRecvThread::stop()
|
|
|
|
{
|
|
|
|
trd.stop();
|
|
|
|
}
|
|
|
|
|
2014-12-03 11:27:27 +00:00
|
|
|
void SrsPublishRecvThread::on_thread_start()
|
|
|
|
{
|
|
|
|
// we donot set the auto response to false,
|
|
|
|
// for the main thread never send message.
|
|
|
|
|
2014-12-04 05:43:55 +00:00
|
|
|
#ifdef SRS_PERF_MERGED_READ
|
2014-12-04 13:50:23 +00:00
|
|
|
if (mr) {
|
|
|
|
// set underlayer buffer size
|
|
|
|
set_socket_buffer(mr_sleep);
|
|
|
|
|
|
|
|
// disable the merge read
|
2015-04-29 09:06:32 +00:00
|
|
|
// @see https://github.com/simple-rtmp-server/srs/issues/241
|
2014-12-04 13:50:23 +00:00
|
|
|
rtmp->set_merge_read(true, this);
|
|
|
|
}
|
2014-12-04 05:43:55 +00:00
|
|
|
#endif
|
2014-12-03 11:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsPublishRecvThread::on_thread_stop()
|
|
|
|
{
|
|
|
|
// we donot set the auto response to true,
|
|
|
|
// for we donot set to false yet.
|
|
|
|
|
|
|
|
// when thread stop, signal the conn thread which wait.
|
2015-04-29 09:06:32 +00:00
|
|
|
// @see https://github.com/simple-rtmp-server/srs/issues/244
|
2014-12-03 11:27:27 +00:00
|
|
|
st_cond_signal(error);
|
|
|
|
|
2014-12-04 05:43:55 +00:00
|
|
|
#ifdef SRS_PERF_MERGED_READ
|
2014-12-04 13:50:23 +00:00
|
|
|
if (mr) {
|
|
|
|
// disable the merge read
|
2015-04-29 09:06:32 +00:00
|
|
|
// @see https://github.com/simple-rtmp-server/srs/issues/241
|
2014-12-04 13:50:23 +00:00
|
|
|
rtmp->set_merge_read(false, NULL);
|
|
|
|
}
|
2014-12-04 05:43:55 +00:00
|
|
|
#endif
|
2014-12-03 11:27:27 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 15:38:51 +00:00
|
|
|
bool SrsPublishRecvThread::can_handle()
|
|
|
|
{
|
|
|
|
// publish thread always can handle message.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-05 15:03:52 +00:00
|
|
|
int SrsPublishRecvThread::handle(SrsCommonMessage* msg)
|
2014-12-01 15:38:51 +00:00
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
_nb_msgs++;
|
2014-12-12 13:51:06 +00:00
|
|
|
|
|
|
|
// log to show the time of recv thread.
|
|
|
|
srs_verbose("recv thread now=%"PRId64"us, got msg time=%"PRId64"ms, size=%d",
|
|
|
|
srs_update_system_time_ms(), msg->header.timestamp, msg->size);
|
2014-12-01 15:38:51 +00:00
|
|
|
|
2014-12-02 11:28:36 +00:00
|
|
|
// the rtmp connection will handle this message
|
|
|
|
ret = _conn->handle_publish_message(_source, msg, _is_fmle, _is_edge);
|
2014-12-01 15:38:51 +00:00
|
|
|
|
|
|
|
// must always free it,
|
|
|
|
// the source will copy it if need to use.
|
|
|
|
srs_freep(msg);
|
2014-12-02 11:28:36 +00:00
|
|
|
|
2014-12-01 15:38:51 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-12-02 07:21:08 +00:00
|
|
|
|
|
|
|
void SrsPublishRecvThread::on_recv_error(int ret)
|
|
|
|
{
|
|
|
|
recv_error_code = ret;
|
2014-12-03 04:08:29 +00:00
|
|
|
|
|
|
|
// when recv thread error, signal the conn thread to process it.
|
2015-04-29 09:06:32 +00:00
|
|
|
// @see https://github.com/simple-rtmp-server/srs/issues/244
|
2014-12-03 04:08:29 +00:00
|
|
|
st_cond_signal(error);
|
2014-12-02 07:21:08 +00:00
|
|
|
}
|
2014-12-02 09:16:20 +00:00
|
|
|
|
2014-12-04 05:43:55 +00:00
|
|
|
#ifdef SRS_PERF_MERGED_READ
|
2014-12-03 14:39:25 +00:00
|
|
|
void SrsPublishRecvThread::on_read(ssize_t nread)
|
2014-12-02 09:16:20 +00:00
|
|
|
{
|
2014-12-12 13:51:06 +00:00
|
|
|
if (!mr || realtime) {
|
2014-12-04 10:21:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread < 0 || mr_sleep <= 0) {
|
2014-12-03 11:27:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-03 04:08:29 +00:00
|
|
|
|
2014-12-03 11:27:27 +00:00
|
|
|
/**
|
|
|
|
* to improve read performance, merge some packets then read,
|
|
|
|
* when it on and read small bytes, we sleep to wait more data.,
|
|
|
|
* that is, we merge some data to read together.
|
2015-04-29 09:06:32 +00:00
|
|
|
* @see https://github.com/simple-rtmp-server/srs/issues/241
|
2014-12-03 11:27:27 +00:00
|
|
|
*/
|
2014-12-04 07:33:17 +00:00
|
|
|
if (nread < SRS_MR_SMALL_BYTES) {
|
2014-12-04 10:21:04 +00:00
|
|
|
st_usleep(mr_sleep * 1000);
|
2014-12-03 11:27:27 +00:00
|
|
|
}
|
2014-12-02 09:16:20 +00:00
|
|
|
}
|
2014-12-04 05:43:55 +00:00
|
|
|
#endif
|
2014-12-04 10:21:04 +00:00
|
|
|
|
|
|
|
int SrsPublishRecvThread::on_reload_vhost_mr(string vhost)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
2014-12-12 13:51:06 +00:00
|
|
|
|
|
|
|
if (req->vhost != vhost) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-12-04 10:21:04 +00:00
|
|
|
|
|
|
|
// the mr settings,
|
2015-04-29 09:06:32 +00:00
|
|
|
// @see https://github.com/simple-rtmp-server/srs/issues/241
|
2014-12-04 10:21:04 +00:00
|
|
|
bool mr_enabled = _srs_config->get_mr_enabled(req->vhost);
|
|
|
|
int sleep_ms = _srs_config->get_mr_sleep_ms(req->vhost);
|
|
|
|
|
2014-12-04 13:50:23 +00:00
|
|
|
// update buffer when sleep ms changed.
|
|
|
|
if (mr_sleep != sleep_ms) {
|
|
|
|
set_socket_buffer(sleep_ms);
|
|
|
|
}
|
|
|
|
|
2014-12-04 10:21:04 +00:00
|
|
|
#ifdef SRS_PERF_MERGED_READ
|
2014-12-04 13:50:23 +00:00
|
|
|
// mr enabled=>disabled
|
|
|
|
if (mr && !mr_enabled) {
|
|
|
|
// disable the merge read
|
2015-04-29 09:06:32 +00:00
|
|
|
// @see https://github.com/simple-rtmp-server/srs/issues/241
|
2014-12-04 13:50:23 +00:00
|
|
|
rtmp->set_merge_read(false, NULL);
|
|
|
|
}
|
|
|
|
// mr disabled=>enabled
|
|
|
|
if (!mr && mr_enabled) {
|
|
|
|
// enable the merge read
|
2015-04-29 09:06:32 +00:00
|
|
|
// @see https://github.com/simple-rtmp-server/srs/issues/241
|
2014-12-04 13:50:23 +00:00
|
|
|
rtmp->set_merge_read(true, this);
|
2014-12-04 10:21:04 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// update to new state
|
|
|
|
mr = mr_enabled;
|
|
|
|
mr_sleep = sleep_ms;
|
|
|
|
|
2014-12-04 13:50:23 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-12 13:51:06 +00:00
|
|
|
int SrsPublishRecvThread::on_reload_vhost_realtime(string vhost)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if (req->vhost != vhost) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool realtime_enabled = _srs_config->get_realtime_enabled(req->vhost);
|
|
|
|
srs_trace("realtime changed %d=>%d", realtime, realtime_enabled);
|
|
|
|
realtime = realtime_enabled;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-04 13:50:23 +00:00
|
|
|
void SrsPublishRecvThread::set_socket_buffer(int sleep_ms)
|
|
|
|
{
|
2014-12-05 03:24:05 +00:00
|
|
|
// the bytes:
|
2014-12-04 13:50:23 +00:00
|
|
|
// 4KB=4096, 8KB=8192, 16KB=16384, 32KB=32768, 64KB=65536,
|
|
|
|
// 128KB=131072, 256KB=262144, 512KB=524288
|
2014-12-05 03:24:05 +00:00
|
|
|
// the buffer should set to sleep*kbps/8,
|
2014-12-04 13:50:23 +00:00
|
|
|
// for example, your system delivery stream in 1000kbps,
|
|
|
|
// sleep 800ms for small bytes, the buffer should set to:
|
|
|
|
// 800*1000/8=100000B(about 128KB).
|
2014-12-05 03:24:05 +00:00
|
|
|
// other examples:
|
2014-12-04 13:50:23 +00:00
|
|
|
// 2000*3000/8=750000B(about 732KB).
|
2014-12-04 14:00:09 +00:00
|
|
|
// 2000*5000/8=1250000B(about 1220KB).
|
|
|
|
int kbps = 5000;
|
2014-12-04 13:50:23 +00:00
|
|
|
int socket_buffer_size = sleep_ms * kbps / 8;
|
2014-12-05 03:31:06 +00:00
|
|
|
|
|
|
|
int fd = mr_fd;
|
|
|
|
int onb_rbuf = 0;
|
|
|
|
socklen_t sock_buf_size = sizeof(int);
|
|
|
|
getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &onb_rbuf, &sock_buf_size);
|
2014-12-04 13:50:23 +00:00
|
|
|
|
|
|
|
// socket recv buffer, system will double it.
|
|
|
|
int nb_rbuf = socket_buffer_size / 2;
|
2014-12-05 03:31:06 +00:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nb_rbuf, sock_buf_size) < 0) {
|
2014-12-04 13:50:23 +00:00
|
|
|
srs_warn("set sock SO_RCVBUF=%d failed.", nb_rbuf);
|
2014-12-04 10:21:04 +00:00
|
|
|
}
|
2014-12-05 03:31:06 +00:00
|
|
|
getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &nb_rbuf, &sock_buf_size);
|
2014-12-04 13:50:23 +00:00
|
|
|
|
2014-12-12 13:51:06 +00:00
|
|
|
srs_trace("mr change sleep %d=>%d, erbuf=%d, rbuf %d=>%d, sbytes=%d, realtime=%d",
|
|
|
|
mr_sleep, sleep_ms, socket_buffer_size, onb_rbuf, nb_rbuf,
|
|
|
|
SRS_MR_SMALL_BYTES, realtime);
|
2014-12-04 13:50:23 +00:00
|
|
|
|
|
|
|
rtmp->set_recv_buffer(nb_rbuf);
|
2014-12-04 10:21:04 +00:00
|
|
|
}
|
|
|
|
|