Skip to content

Commit

Permalink
Add unpublishing state to avoid dispose during on_hls.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Aug 23, 2023
1 parent 4df280a commit a4be45f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
17 changes: 14 additions & 3 deletions trunk/src/app/srs_app_hls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,7 @@ SrsHls::SrsHls()

enabled = false;
disposable = false;
unpublishing_ = false;
last_update_time = 0;
hls_dts_directly = false;

Expand Down Expand Up @@ -1174,14 +1175,17 @@ void SrsHls::dispose()
srs_error_t SrsHls::cycle()
{
srs_error_t err = srs_success;

if (last_update_time <= 0) {
last_update_time = srs_get_system_time();
}

if (!req) {
return err;
}

// When unpublishing, we must wait for it done.
if (unpublishing_) return err;

srs_utime_t hls_dispose = _srs_config->get_hls_dispose(req->vhost);
if (hls_dispose <= 0) {
Expand All @@ -1191,12 +1195,12 @@ srs_error_t SrsHls::cycle()
return err;
}
last_update_time = srs_get_system_time();

if (!disposable) {
return err;
}
disposable = false;

srs_trace("hls cycle to dispose hls %s, timeout=%dms", req->get_stream_url().c_str(), hls_dispose);
dispose();

Expand Down Expand Up @@ -1243,6 +1247,8 @@ srs_error_t SrsHls::on_publish()

// if enabled, open the muxer.
enabled = true;
// Reset the unpublishing state.
unpublishing_ = false;

// ok, the hls can be dispose, or need to be dispose.
disposable = true;
Expand All @@ -1258,13 +1264,18 @@ void SrsHls::on_unpublish()
if (!enabled) {
return;
}

// During unpublishing, there maybe callback that switch to other coroutines.
if (unpublishing_) return;
unpublishing_ = true;

if ((err = controller->on_unpublish()) != srs_success) {
srs_warn("hls: ignore unpublish failed %s", srs_error_desc(err).c_str());
srs_freep(err);
}

enabled = false;
unpublishing_ = false;
}

srs_error_t SrsHls::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format)
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_hls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ class SrsHls
SrsRequest* req;
bool enabled;
bool disposable;
bool unpublishing_;
srs_utime_t last_update_time;
private:
// If the diff=dts-previous_audio_dts is about 23,
Expand Down

0 comments on commit a4be45f

Please sign in to comment.