From 133cc62b51c7eef01c2e6642aad9354fa12e874b Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 11 Nov 2014 17:54:02 +0800 Subject: [PATCH] for bug #194, use play fd poll, create the singleton poll --- trunk/configure | 3 +- trunk/src/app/srs_app_poll.cpp | 75 ++++++++++++++++++++++++ trunk/src/app/srs_app_poll.hpp | 88 +++++++++++++++++++++++++++++ trunk/src/app/srs_app_rtmp_conn.cpp | 6 ++ trunk/src/app/srs_app_server.cpp | 9 +++ trunk/src/srs/srs.upp | 2 + 6 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 trunk/src/app/srs_app_poll.cpp create mode 100644 trunk/src/app/srs_app_poll.hpp diff --git a/trunk/configure b/trunk/configure index a380aba72..c6daeedde 100755 --- a/trunk/configure +++ b/trunk/configure @@ -388,7 +388,8 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then "srs_app_thread" "srs_app_bandwidth" "srs_app_st" "srs_app_log" "srs_app_config" "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks" "srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge" - "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac") + "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac" + "srs_app_poll") APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh APP_OBJS="${MODULE_OBJS[@]}" fi diff --git a/trunk/src/app/srs_app_poll.cpp b/trunk/src/app/srs_app_poll.cpp new file mode 100644 index 000000000..fd7546643 --- /dev/null +++ b/trunk/src/app/srs_app_poll.cpp @@ -0,0 +1,75 @@ +/* +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 + +#include + +SrsPoll::SrsPoll() +{ + pthread = new SrsThread(this, 0, false); +} + +SrsPoll::~SrsPoll() +{ + srs_freep(pthread); + fds.clear(); +} + +int SrsPoll::start() +{ + return pthread->start(); +} + +int SrsPoll::cycle() +{ + int ret = ERROR_SUCCESS; + // TODO: FIXME: implements it. + return ret; +} + +SrsPoll* SrsPoll::_instance = new SrsPoll(); + +SrsPoll* SrsPoll::instance() +{ + return _instance; +} + +SrsPollFD::SrsPollFD() +{ + _stfd = NULL; +} + +SrsPollFD::~SrsPollFD() +{ +} + +int SrsPollFD::initialize(st_netfd_t stfd) +{ + int ret = ERROR_SUCCESS; + + _stfd = stfd; + + return ret; +} + diff --git a/trunk/src/app/srs_app_poll.hpp b/trunk/src/app/srs_app_poll.hpp new file mode 100644 index 000000000..8ad59d10b --- /dev/null +++ b/trunk/src/app/srs_app_poll.hpp @@ -0,0 +1,88 @@ +/* +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. +*/ + +#ifndef SRS_APP_POLL_HPP +#define SRS_APP_POLL_HPP + +/* +#include +*/ + +#include + +#include + +#include +#include + +class SrsPollFD; + +/** +* the poll for all play clients to finger the active fd out. +* for performance issue, @see: https://github.com/winlinvip/simple-rtmp-server/issues/194 +* the poll is shared by all SrsPollFD, and we start an isolate thread to finger the active fds. +*/ +class SrsPoll : public ISrsThreadHandler +{ +private: + SrsThread* pthread; + std::map fds; +public: + SrsPoll(); + virtual ~SrsPoll(); +public: + /** + * start the poll thread. + */ + virtual int start(); + /** + * start an cycle thread. + */ + virtual int cycle(); +// singleton +private: + static SrsPoll* _instance; +public: + static SrsPoll* instance(); +}; + +/** +* the poll fd to check whether the specified fd is active. +*/ +class SrsPollFD +{ +private: + st_netfd_t _stfd; +public: + SrsPollFD(); + virtual ~SrsPollFD(); +public: + /** + * initialize the poll. + * @param stfd the fd to poll. + */ + virtual int initialize(st_netfd_t stfd); +}; + +#endif + diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index df543c7a3..88a186d93 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -48,6 +48,7 @@ using namespace std; #include #include #include +#include // when stream is busy, for example, streaming is already // publishing, when a new client to request to publish, @@ -525,6 +526,11 @@ int SrsRtmpConn::playing(SrsSource* source) bool user_specified_duration_to_stop = (req->duration > 0); int64_t starttime = -1; + SrsPollFD poll; + if ((ret = poll.initialize(stfd)) != ERROR_SUCCESS) { + return ret; + } + while (true) { // collect elapse for pithy print. pithy_print.elapse(); diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 49df43998..2678f44eb 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -44,6 +44,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include +#include // signal defines. #define SIGNAL_RELOAD SIGHUP @@ -664,6 +665,14 @@ int SrsServer::do_cycle() { int ret = ERROR_SUCCESS; + // start the poll for play clients. + // performance issue, @see: https://github.com/winlinvip/simple-rtmp-server/issues/194 + SrsPoll* poll = SrsPoll::instance(); + if ((ret = poll->start()) != ERROR_SUCCESS) { + srs_error("start poll failed. ret=%d", ret); + return ret; + } + // find the max loop int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES); diff --git a/trunk/src/srs/srs.upp b/trunk/src/srs/srs.upp index 5fa0bfbf9..c73673d53 100755 --- a/trunk/src/srs/srs.upp +++ b/trunk/src/srs/srs.upp @@ -92,6 +92,8 @@ file ..\app\srs_app_kbps.cpp, ..\app\srs_app_log.hpp, ..\app\srs_app_log.cpp, + ..\app\srs_app_poll.hpp, + ..\app\srs_app_poll.cpp, ..\app\srs_app_refer.hpp, ..\app\srs_app_refer.cpp, ..\app\srs_app_reload.hpp,