Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up source and add publisher status #1566

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions trunk/src/app/srs_app_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,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 = -1;
source->active_at = srs_get_system_time_ms();
return source;
}

Expand Down Expand Up @@ -827,9 +829,9 @@ int 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()) {
if (source->expired() || !source->activable()) {
int cid = source->source_id();
if (cid == -1 && source->pre_source_id() > 0) {
cid = source->pre_source_id();
Expand Down Expand Up @@ -959,6 +961,7 @@ SrsSource::SrsSource()
_can_publish = true;
_pre_source_id = _source_id = -1;
die_at = -1;
active_at = srs_get_system_time_ms();

play_edge = new SrsPlayEdge();
publish_edge = new SrsPublishEdge();
Expand Down Expand Up @@ -1068,6 +1071,23 @@ bool SrsSource::expired()
return false;
}

bool SrsSource::activable()
{
// has any consumers?
if (!consumers.empty()) {
return true;
}
// still publishing?
if (!_can_publish || !publish_edge->can_publish()) {
return true;
}
int64_t now = srs_get_system_time_ms();
if (now > active_at + SRS_SOURCE_CLEANUP * 1.5) { // use SRS_CONSTS_RTMP_RECV_TIMEOUT_US ?
return false;
}
return true;
}

int SrsSource::initialize(SrsRequest* r, ISrsSourceHandler* h)
{
int ret = ERROR_SUCCESS;
Expand Down Expand Up @@ -1489,6 +1509,8 @@ bool SrsSource::can_publish(bool is_edge)

int SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata)
{
active_at = srs_get_system_time_ms();

int ret = ERROR_SUCCESS;

#ifdef SRS_AUTO_HLS
Expand Down Expand Up @@ -1610,6 +1632,8 @@ int SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata

int SrsSource::on_audio(SrsCommonMessage* shared_audio)
{
active_at = srs_get_system_time_ms();

int ret = ERROR_SUCCESS;

// monotically increase detect.
Expand Down Expand Up @@ -1828,6 +1852,8 @@ int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg)

int SrsSource::on_video(SrsCommonMessage* shared_video)
{
active_at = srs_get_system_time_ms();

int ret = ERROR_SUCCESS;

// monotically increase detect.
Expand Down
3 changes: 3 additions & 0 deletions trunk/src/app/srs_app_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ class SrsSource : public ISrsReloadHandler
// last die time, when all consumers quit and no publisher,
// we will remove the source when source die.
int64_t die_at;
// last active time
int64_t active_at;
private:
SrsSharedPtrMessage* cache_metadata;
// the cached video sequence header.
Expand All @@ -519,6 +521,7 @@ class SrsSource : public ISrsReloadHandler
virtual int cycle();
// remove source when expired.
virtual bool expired();
virtual bool activable();
// initialize, get and setter.
public:
/**
Expand Down