Skip to content

Commit

Permalink
clean up source and add publisher status
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyifan committed Jan 9, 2020
1 parent 36bfbb9 commit 8ab2919
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
26 changes: 20 additions & 6 deletions trunk/src/app/srs_app_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,8 @@ SrsSource* SrsSource::fetch(SrsRequest* r)
// and we only need to update the token of request, it's simple.
source->req->update_auth(r);

source->die_at = 0;
source->active_at = srs_get_system_time();
return source;
}

Expand Down Expand Up @@ -1719,7 +1721,7 @@ srs_error_t SrsSource::do_cycle_all()
// TODO: FIXME: support source cleanup.
// @see https://github.com/ossrs/srs/issues/713
// @see https://github.com/ossrs/srs/issues/714
#if 0
#if 1
// When source expired, remove it.
if (source->expired()) {
int cid = source->source_id();
Expand Down Expand Up @@ -1764,6 +1766,7 @@ SrsSource::SrsSource()
_can_publish = true;
_pre_source_id = _source_id = -1;
die_at = 0;
active_at = srs_get_system_time();

play_edge = new SrsPlayEdge();
publish_edge = new SrsPublishEdge();
Expand Down Expand Up @@ -1816,11 +1819,6 @@ srs_error_t SrsSource::cycle()

bool SrsSource::expired()
{
// unknown state?
if (die_at == 0) {
return false;
}

// still publishing?
if (!_can_publish || !publish_edge->can_publish()) {
return false;
Expand All @@ -1830,6 +1828,16 @@ bool SrsSource::expired()
if (!consumers.empty()) {
return false;
}

// unknown state?
if (die_at == 0) {
// inactive source?
int64_t now = srs_get_system_time();
if (now > active_at + SRS_SOURCE_CLEANUP * 1.5) { // use SRS_CONSTS_RTMP_RECV_TIMEOUT_US ?
return true;
}
return false;
}

srs_utime_t now = srs_get_system_time();
if (now > die_at + SRS_SOURCE_CLEANUP) {
Expand Down Expand Up @@ -2005,6 +2013,8 @@ bool SrsSource::can_publish(bool is_edge)

srs_error_t SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata)
{
active_at = srs_get_system_time();

srs_error_t err = srs_success;

// if allow atc_auto and bravo-atc detected, open atc for vhost.
Expand Down Expand Up @@ -2051,6 +2061,8 @@ srs_error_t SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket*

srs_error_t SrsSource::on_audio(SrsCommonMessage* shared_audio)
{
active_at = srs_get_system_time();

srs_error_t err = srs_success;

// monotically increase detect.
Expand Down Expand Up @@ -2159,6 +2171,8 @@ srs_error_t SrsSource::on_audio_imp(SrsSharedPtrMessage* msg)

srs_error_t SrsSource::on_video(SrsCommonMessage* shared_video)
{
active_at = srs_get_system_time();

srs_error_t err = srs_success;

// monotically increase detect.
Expand Down
2 changes: 2 additions & 0 deletions trunk/src/app/srs_app_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ class SrsSource : public ISrsReloadHandler
// The last die time, when all consumers quit and no publisher,
// We will remove the source when source die.
srs_utime_t die_at;
// last active time
int64_t active_at;
public:
SrsSource();
virtual ~SrsSource();
Expand Down

0 comments on commit 8ab2919

Please sign in to comment.