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

Timer: Apply timer(HourGlass) to server and sources

This commit is contained in:
winlin 2021-02-09 17:15:25 +08:00
parent 77cffd3e04
commit 7114682eec
6 changed files with 167 additions and 178 deletions

View file

@ -1689,6 +1689,7 @@ SrsSourceManager* _srs_sources = new SrsSourceManager();
SrsSourceManager::SrsSourceManager()
{
lock = NULL;
timer_ = NULL;
}
SrsSourceManager::~SrsSourceManager()
@ -1696,6 +1697,11 @@ SrsSourceManager::~SrsSourceManager()
srs_mutex_destroy(lock);
}
srs_error_t SrsSourceManager::initialize()
{
return setup_ticks();
}
srs_error_t SrsSourceManager::fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** pps)
{
srs_error_t err = srs_success;
@ -1789,28 +1795,37 @@ void SrsSourceManager::dispose()
return;
}
srs_error_t SrsSourceManager::cycle()
srs_error_t SrsSourceManager::setup_ticks()
{
SrsContextId cid = _srs_context->get_id();
srs_error_t err = do_cycle();
_srs_context->set_id(cid);
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) {
return srs_error_wrap(err, "tick");
}
if ((err = timer_->start()) != srs_success) {
return srs_error_wrap(err, "timer");
}
return err;
}
srs_error_t SrsSourceManager::do_cycle()
srs_error_t SrsSourceManager::notify(int event, srs_utime_t interval, srs_utime_t tick)
{
srs_error_t err = srs_success;
std::map<std::string, SrsSource*>::iterator it;
for (it = pool.begin(); it != pool.end();) {
SrsSource* source = it->second;
// Do cycle source to cleanup components, such as hls dispose.
if ((err = source->cycle()) != srs_success) {
return srs_error_wrap(err, "source=%s/%s cycle", source->source_id().c_str(), source->pre_source_id().c_str());
}
// TODO: FIXME: support source cleanup.
// @see https://github.com/ossrs/srs/issues/713
// @see https://github.com/ossrs/srs/issues/714
@ -1825,7 +1840,7 @@ srs_error_t SrsSourceManager::do_cycle()
_srs_context->set_id(cid);
}
srs_trace("cleanup die source, total=%d", (int)pool.size());
srs_freep(source);
pool.erase(it++);
} else {
@ -1835,7 +1850,7 @@ srs_error_t SrsSourceManager::do_cycle()
++it;
#endif
}
return err;
}