From c8f2ff0b532f819c03189c1bf3d4ced67af22e62 Mon Sep 17 00:00:00 2001 From: winlin Date: Fri, 9 Apr 2021 10:21:05 +0800 Subject: [PATCH] Threads-Hybrid: Merge api to master thread. --- trunk/src/app/srs_app_server.cpp | 39 ++++++++++-------------------- trunk/src/app/srs_app_server.hpp | 7 +++--- trunk/src/app/srs_app_threads.cpp | 7 +++++- trunk/src/main/srs_main_server.cpp | 13 ---------- 4 files changed, 22 insertions(+), 44 deletions(-) diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 06fb299b57..0834d74875 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -1678,6 +1678,18 @@ srs_error_t SrsApiServer::initialize() return srs_error_wrap(err, "listen api"); } + if ((err = listen_http_api()) != srs_success) { + return srs_error_wrap(err, "http api listen"); + } + + if ((err = listen_https_api()) != srs_success) { + return srs_error_wrap(err, "https api listen"); + } + + if ((err = conn_manager_->start()) != srs_success) { + return srs_error_wrap(err, "connection manager"); + } + return err; } @@ -2005,30 +2017,5 @@ srs_error_t SrsApiServer::create_session( return err; } -srs_error_t SrsApiServer::start(void* arg) -{ - SrsApiServer* api = (SrsApiServer*)arg; - return api->do_start(); -} - -srs_error_t SrsApiServer::do_start() -{ - srs_error_t err = srs_success; - - if ((err = listen_http_api()) != srs_success) { - return srs_error_wrap(err, "http api listen"); - } - - if ((err = listen_https_api()) != srs_success) { - return srs_error_wrap(err, "https api listen"); - } - - if ((err = conn_manager_->start()) != srs_success) { - return srs_error_wrap(err, "connection manager"); - } - - srs_usleep(SRS_UTIME_NO_TIMEOUT); - - return err; -} +SrsApiServer* _srs_api = new SrsApiServer(); diff --git a/trunk/src/app/srs_app_server.hpp b/trunk/src/app/srs_app_server.hpp index f7063785d4..61826c27c8 100644 --- a/trunk/src/app/srs_app_server.hpp +++ b/trunk/src/app/srs_app_server.hpp @@ -445,12 +445,11 @@ class SrsApiServer : public ISrsTcpMuxHandler, public ISrsResourceManager, publi bool publish, bool dtls, bool srtp, SrsRtcConnection** psession ); -public: - static srs_error_t start(void* arg); -private: - srs_error_t do_start(); }; +// It's only used in master/srs thread. +extern SrsApiServer* _srs_api; + // The RTC create session information. struct SrsThreadMessageRtcCreateSession { diff --git a/trunk/src/app/srs_app_threads.cpp b/trunk/src/app/srs_app_threads.cpp index d2fc310896..01869be51f 100644 --- a/trunk/src/app/srs_app_threads.cpp +++ b/trunk/src/app/srs_app_threads.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -755,6 +756,10 @@ srs_error_t SrsThreadPool::initialize() return srs_error_wrap(err, "acquire pid file"); } + if ((err = _srs_api->initialize()) != srs_success) { + return srs_error_wrap(err, "init api server"); + } + // Initialize the master primordial thread. SrsThreadEntry* entry = (SrsThreadEntry*)entry_; #ifndef SRS_OSX @@ -976,7 +981,7 @@ srs_error_t SrsThreadPool::run() } } - sleep(1); + srs_usleep(1 * SRS_UTIME_SECONDS); } // In normal state, gather status and log it. diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index ea9b51b435..270f0aed2b 100644 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -484,14 +484,6 @@ srs_error_t run_in_thread_pool() return srs_error_wrap(err, "init thread pool"); } - // Create and initialize the API server. - SrsApiServer* api = new SrsApiServer(); - SrsAutoFree(SrsApiServer, api); - - if ((err = api->initialize()) != srs_success) { - return srs_error_wrap(err, "init api server"); - } - // After all init(log, async log manager, thread pool), now we can start to // run the log manager thread. if ((err = _srs_thread_pool->execute("log", SrsAsyncLogManager::start, _srs_async_log)) != srs_success) { @@ -522,11 +514,6 @@ srs_error_t run_in_thread_pool() } } - // Start the api server thread, for server stat and RTC api, etc. - if ((err = _srs_thread_pool->execute("api", SrsApiServer::start, api)) != srs_success) { - return srs_error_wrap(err, "start api server thread"); - } - // Start a number of hybrid service threads. int hybrids = _srs_config->get_threads_hybrids(); for (int stream_index = 0; stream_index < hybrids; stream_index++) {