1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Refine init of global objects

This commit is contained in:
winlin 2021-05-08 11:25:48 +08:00
parent b1e85664a1
commit 8b58d18a5a
6 changed files with 53 additions and 29 deletions

View file

@ -166,7 +166,7 @@ bool SrsErrorPithyPrint::can_print(int error_code, uint32_t* pnn)
} }
// The global stage manager for pithy print, multiple stages. // The global stage manager for pithy print, multiple stages.
static SrsStageManager* _srs_stages = new SrsStageManager(); SrsStageManager* _srs_stages = NULL;
SrsPithyPrint::SrsPithyPrint(int _stage_id) SrsPithyPrint::SrsPithyPrint(int _stage_id)
{ {

View file

@ -132,10 +132,10 @@ void SrsRtcBlackhole::sendto(void* data, int len)
srs_sendto(blackhole_stfd, data, len, (sockaddr*)blackhole_addr, sizeof(sockaddr_in), SRS_UTIME_NO_TIMEOUT); srs_sendto(blackhole_stfd, data, len, (sockaddr*)blackhole_addr, sizeof(sockaddr_in), SRS_UTIME_NO_TIMEOUT);
} }
SrsRtcBlackhole* _srs_blackhole = new SrsRtcBlackhole(); SrsRtcBlackhole* _srs_blackhole = NULL;
// @global dtls certficate for rtc module. // @global dtls certficate for rtc module.
SrsDtlsCertificate* _srs_rtc_dtls_certificate = new SrsDtlsCertificate(); SrsDtlsCertificate* _srs_rtc_dtls_certificate = NULL;
// TODO: Should support error response. // TODO: Should support error response.
// For STUN packet, 0x00 is binding request, 0x01 is binding success response. // For STUN packet, 0x00 is binding request, 0x01 is binding success response.
@ -777,5 +777,5 @@ void RtcServerAdapter::stop()
{ {
} }
SrsResourceManager* _srs_rtc_manager = new SrsResourceManager("RTC", true); SrsResourceManager* _srs_rtc_manager = NULL;

View file

@ -249,7 +249,7 @@ void SrsRtcConsumer::on_stream_change(SrsRtcStreamDescription* desc)
SrsRtcStreamManager::SrsRtcStreamManager() SrsRtcStreamManager::SrsRtcStreamManager()
{ {
lock = NULL; lock = srs_mutex_new();
} }
SrsRtcStreamManager::~SrsRtcStreamManager() SrsRtcStreamManager::~SrsRtcStreamManager()
@ -261,11 +261,6 @@ srs_error_t SrsRtcStreamManager::fetch_or_create(SrsRequest* r, SrsRtcStream** p
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
// Lazy create lock, because ST is not ready in SrsRtcStreamManager constructor.
if (!lock) {
lock = srs_mutex_new();
}
// Use lock to protect coroutine switch. // Use lock to protect coroutine switch.
// @bug https://github.com/ossrs/srs/issues/1230 // @bug https://github.com/ossrs/srs/issues/1230
SrsLocker(lock); SrsLocker(lock);
@ -315,7 +310,7 @@ SrsRtcStream* SrsRtcStreamManager::fetch(SrsRequest* r)
return source; return source;
} }
SrsRtcStreamManager* _srs_rtc_sources = new SrsRtcStreamManager(); SrsRtcStreamManager* _srs_rtc_sources = NULL;
ISrsRtcPublishStream::ISrsRtcPublishStream() ISrsRtcPublishStream::ISrsRtcPublishStream()
{ {

View file

@ -1684,17 +1684,18 @@ srs_error_t SrsMetaCache::update_vsh(SrsSharedPtrMessage* msg)
return vformat->on_video(msg); return vformat->on_video(msg);
} }
SrsSourceManager* _srs_sources = new SrsSourceManager(); SrsSourceManager* _srs_sources = NULL;
SrsSourceManager::SrsSourceManager() SrsSourceManager::SrsSourceManager()
{ {
lock = NULL; lock = srs_mutex_new();
timer_ = NULL; timer_ = new SrsHourGlass("sources", this, 1 * SRS_UTIME_SECONDS);
} }
SrsSourceManager::~SrsSourceManager() SrsSourceManager::~SrsSourceManager()
{ {
srs_mutex_destroy(lock); srs_mutex_destroy(lock);
srs_freep(timer_);
} }
srs_error_t SrsSourceManager::initialize() srs_error_t SrsSourceManager::initialize()
@ -1706,11 +1707,6 @@ srs_error_t SrsSourceManager::fetch_or_create(SrsRequest* r, ISrsSourceHandler*
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
// Lazy create lock, because ST is not ready in SrsSourceManager constructor.
if (!lock) {
lock = srs_mutex_new();
}
// Use lock to protect coroutine switch. // Use lock to protect coroutine switch.
// @bug https://github.com/ossrs/srs/issues/1230 // @bug https://github.com/ossrs/srs/issues/1230
// TODO: FIXME: Use smaller lock. // TODO: FIXME: Use smaller lock.
@ -1778,9 +1774,6 @@ srs_error_t SrsSourceManager::setup_ticks()
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
srs_freep(timer_);
timer_ = new SrsHourGlass("sources", this, 1 * SRS_UTIME_SECONDS);
if ((err = timer_->tick(1, 1 * SRS_UTIME_SECONDS)) != srs_success) { if ((err = timer_->tick(1, 1 * SRS_UTIME_SECONDS)) != srs_success) {
return srs_error_wrap(err, "tick"); return srs_error_wrap(err, "tick");
} }

View file

@ -27,10 +27,30 @@
#include <srs_app_hybrid.hpp> #include <srs_app_hybrid.hpp>
#include <srs_app_utility.hpp> #include <srs_app_utility.hpp>
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_app_rtc_source.hpp>
#include <srs_app_source.hpp>
#include <srs_app_pithy_print.hpp>
#include <srs_app_rtc_server.hpp>
#include <srs_app_rtc_dtls.hpp>
#include <srs_app_rtc_conn.hpp>
#include <string> #include <string>
using namespace std; using namespace std;
extern SrsStageManager* _srs_stages;
extern SrsRtcBlackhole* _srs_blackhole;
extern SrsResourceManager* _srs_rtc_manager;
extern SrsRtpObjectCacheManager<SrsRtpPacket2>* _srs_rtp_cache;
extern SrsRtpObjectCacheManager<SrsRtpRawPayload>* _srs_rtp_raw_cache;
extern SrsRtpObjectCacheManager<SrsRtpFUAPayload2>* _srs_rtp_fua_cache;
extern SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_buffers;
extern SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_objs;
extern SrsResourceManager* _srs_rtc_manager;
extern SrsDtlsCertificate* _srs_rtc_dtls_certificate;
#include <srs_protocol_kbps.hpp> #include <srs_protocol_kbps.hpp>
extern SrsPps* _srs_pps_snack2; extern SrsPps* _srs_pps_snack2;
@ -249,7 +269,7 @@ srs_error_t SrsCircuitBreaker::on_timer(srs_utime_t interval)
return err; return err;
} }
SrsCircuitBreaker* _srs_circuit_breaker = new SrsCircuitBreaker(); SrsCircuitBreaker* _srs_circuit_breaker = NULL;
srs_error_t srs_thread_initialize() srs_error_t srs_thread_initialize()
{ {
@ -267,8 +287,24 @@ srs_error_t srs_thread_initialize()
return srs_error_wrap(err, "initialize st failed"); return srs_error_wrap(err, "initialize st failed");
} }
// The global hybrid server. // The global objects which depends on ST.
_srs_hybrid = new SrsHybridServer(); _srs_hybrid = new SrsHybridServer();
_srs_rtc_sources = new SrsRtcStreamManager();
_srs_sources = new SrsSourceManager();
_srs_stages = new SrsStageManager();
_srs_blackhole = new SrsRtcBlackhole();
_srs_rtc_manager = new SrsResourceManager("RTC", true);
_srs_circuit_breaker = new SrsCircuitBreaker();
_srs_rtp_cache = new SrsRtpObjectCacheManager<SrsRtpPacket2>(sizeof(SrsRtpPacket2));
_srs_rtp_raw_cache = new SrsRtpObjectCacheManager<SrsRtpRawPayload>(sizeof(SrsRtpRawPayload));
_srs_rtp_fua_cache = new SrsRtpObjectCacheManager<SrsRtpFUAPayload2>(sizeof(SrsRtpFUAPayload2));
_srs_rtp_msg_cache_buffers = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>(sizeof(SrsSharedPtrMessage) + kRtpPacketSize);
_srs_rtp_msg_cache_objs = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>(sizeof(SrsSharedPtrMessage));
_srs_rtc_manager = new SrsResourceManager("RTC", true);
_srs_rtc_dtls_certificate = new SrsDtlsCertificate();
// Initialize global pps, which depends on _srs_clock // Initialize global pps, which depends on _srs_clock
_srs_pps_ids = new SrsPps(); _srs_pps_ids = new SrsPps();

View file

@ -1081,12 +1081,12 @@ bool SrsRtpPacket2::is_keyframe()
return false; return false;
} }
SrsRtpObjectCacheManager<SrsRtpPacket2>* _srs_rtp_cache = new SrsRtpObjectCacheManager<SrsRtpPacket2>(sizeof(SrsRtpPacket2)); SrsRtpObjectCacheManager<SrsRtpPacket2>* _srs_rtp_cache = NULL;
SrsRtpObjectCacheManager<SrsRtpRawPayload>* _srs_rtp_raw_cache = new SrsRtpObjectCacheManager<SrsRtpRawPayload>(sizeof(SrsRtpRawPayload)); SrsRtpObjectCacheManager<SrsRtpRawPayload>* _srs_rtp_raw_cache = NULL;
SrsRtpObjectCacheManager<SrsRtpFUAPayload2>* _srs_rtp_fua_cache = new SrsRtpObjectCacheManager<SrsRtpFUAPayload2>(sizeof(SrsRtpFUAPayload2)); SrsRtpObjectCacheManager<SrsRtpFUAPayload2>* _srs_rtp_fua_cache = NULL;
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_buffers = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>(sizeof(SrsSharedPtrMessage) + kRtpPacketSize); SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_buffers = NULL;
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_objs = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>(sizeof(SrsSharedPtrMessage)); SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_objs = NULL;
SrsRtpRawPayload::SrsRtpRawPayload() SrsRtpRawPayload::SrsRtpRawPayload()
{ {