Skip to content

Commit

Permalink
HLS: Support reload HLS asynchronously.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Aug 23, 2023
1 parent a4be45f commit 5827864
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 55 deletions.
53 changes: 48 additions & 5 deletions trunk/src/app/srs_app_hls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@ SrsHls::SrsHls()
enabled = false;
disposable = false;
unpublishing_ = false;
reloading_ = false;
last_update_time = 0;
hls_dts_directly = false;

Expand All @@ -1156,6 +1157,39 @@ SrsHls::~SrsHls()
srs_freep(pprint);
}

void SrsHls::async_reload()
{
reloading_ = true;
}

srs_error_t SrsHls::reload()
{
srs_error_t err = srs_success;

// Ignore if not active.
if (!enabled || unpublishing_) return err;

srs_trace("start async reload hls %s", req->get_stream_url().c_str());

on_unpublish();
if ((err = on_publish()) != srs_success) {
return srs_error_wrap(err, "hls publish failed");
}
srs_trace("vhost hls async reload ok");

// Before feed the sequence header, must reset the reloading.
reloading_ = false;

// After reloading, we must request the sequence header again.
if ((err = hub->on_hls_request_sh()) != srs_success) {
return srs_error_wrap(err, "hls request sh");
}

srs_trace("vhost hls async reload done");

return err;
}

void SrsHls::dispose()
{
if (enabled) {
Expand Down Expand Up @@ -1186,7 +1220,14 @@ srs_error_t SrsHls::cycle()

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


// When reloading, we must wait for it done.
if (reloading_) {
reload();
return err;
}

// If not unpublishing and not reloading, try to dispose HLS stream.
srs_utime_t hls_dispose = _srs_config->get_hls_dispose(req->vhost);
if (hls_dispose <= 0) {
return err;
Expand Down Expand Up @@ -1281,8 +1322,9 @@ void SrsHls::on_unpublish()
srs_error_t SrsHls::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format)
{
srs_error_t err = srs_success;

if (!enabled) {

// If not able to transmux to HLS, ignore.
if (!enabled || unpublishing_ || reloading_) {
return err;
}

Expand Down Expand Up @@ -1363,8 +1405,9 @@ srs_error_t SrsHls::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* forma
srs_error_t SrsHls::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format)
{
srs_error_t err = srs_success;

if (!enabled) {

// If not able to transmux to HLS, ignore.
if (!enabled || unpublishing_ || reloading_) {
return err;
}

Expand Down
10 changes: 10 additions & 0 deletions trunk/src/app/srs_app_hls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,15 @@ class SrsHls
SrsHlsController* controller;
private:
SrsRequest* req;
// Whether the HLS is enabled.
bool enabled;
// Whether the HLS stream is able to be disposed.
bool disposable;
// Whether the HLS stream is unpublishing, should never do other action util unpublish finish.
bool unpublishing_;
// Whether requires HLS to do reload asynchronously.
bool reloading_;
// To detect heartbeat and dipose it if configured.
srs_utime_t last_update_time;
private:
// If the diff=dts-previous_audio_dts is about 23,
Expand All @@ -294,6 +300,10 @@ class SrsHls
public:
SrsHls();
virtual ~SrsHls();
public:
virtual void async_reload();
private:
srs_error_t reload();
public:
virtual void dispose();
virtual srs_error_t cycle();
Expand Down
84 changes: 34 additions & 50 deletions trunk/src/app/srs_app_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,27 @@ srs_error_t SrsOriginHub::on_dvr_request_sh()
return err;
}

srs_error_t SrsOriginHub::on_hls_request_sh()
{
srs_error_t err = srs_success;

SrsSharedPtrMessage* cache_sh_video = source->meta->vsh();
if (cache_sh_video) {
if ((err = hls->on_video(cache_sh_video, source->meta->vsh_format())) != srs_success) {
return srs_error_wrap(err, "hls video");
}
}

SrsSharedPtrMessage* cache_sh_audio = source->meta->ash();
if (cache_sh_audio) {
if ((err = hls->on_audio(cache_sh_audio, source->meta->ash_format())) != srs_success) {
return srs_error_wrap(err, "hls audio");
}
}

return err;
}

srs_error_t SrsOriginHub::on_reload_vhost_forward(string vhost)
{
srs_error_t err = srs_success;
Expand All @@ -1236,7 +1257,7 @@ srs_error_t SrsOriginHub::on_reload_vhost_forward(string vhost)
return err;
}

// TODO: FIXME: maybe should ignore when publish already stopped?
// TODO: FIXME: Must do async reload, see SrsHls::async_reload.

// forwarders
destroy_forwarders();
Expand All @@ -1262,7 +1283,9 @@ srs_error_t SrsOriginHub::on_reload_vhost_dash(string vhost)
if (req_->vhost != vhost) {
return err;
}


// TODO: FIXME: Must do async reload, see SrsHls::async_reload.

dash->on_unpublish();

// Don't start DASH when source is not active.
Expand Down Expand Up @@ -1306,47 +1329,8 @@ srs_error_t SrsOriginHub::on_reload_vhost_hls(string vhost)
if (req_->vhost != vhost) {
return err;
}

// TODO: FIXME: maybe should ignore when publish already stopped?

hls->on_unpublish();

// Don't start HLS when source is not active.
if (!is_active) {
return err;
}

if ((err = hls->on_publish()) != srs_success) {
return srs_error_wrap(err, "hls publish failed");
}
srs_trace("vhost %s hls reload success", vhost.c_str());

SrsRtmpFormat* format = source->format_;

// when publish, don't need to fetch sequence header, which is old and maybe corrupt.
// when reload, we must fetch the sequence header from source cache.
// notice the source to get the cached sequence header.
// when reload to start hls, hls will never get the sequence header in stream,
// use the SrsLiveSource.on_hls_start to push the sequence header to HLS.
SrsSharedPtrMessage* cache_sh_video = source->meta->vsh();
if (cache_sh_video) {
if ((err = format->on_video(cache_sh_video)) != srs_success) {
return srs_error_wrap(err, "format on_video");
}
if ((err = hls->on_video(cache_sh_video, format)) != srs_success) {
return srs_error_wrap(err, "hls on_video");
}
}

SrsSharedPtrMessage* cache_sh_audio = source->meta->ash();
if (cache_sh_audio) {
if ((err = format->on_audio(cache_sh_audio)) != srs_success) {
return srs_error_wrap(err, "format on_audio");
}
if ((err = hls->on_audio(cache_sh_audio, format)) != srs_success) {
return srs_error_wrap(err, "hls on_audio");
}
}
hls->async_reload();

return err;
}
Expand All @@ -1358,8 +1342,8 @@ srs_error_t SrsOriginHub::on_reload_vhost_hds(string vhost)
if (req_->vhost != vhost) {
return err;
}
// TODO: FIXME: maybe should ignore when publish already stopped?

// TODO: FIXME: Must do async reload, see SrsHls::async_reload.

#ifdef SRS_HDS
hds->on_unpublish();
Expand All @@ -1385,8 +1369,8 @@ srs_error_t SrsOriginHub::on_reload_vhost_dvr(string vhost)
if (req_->vhost != vhost) {
return err;
}
// TODO: FIXME: maybe should ignore when publish already stopped?

// TODO: FIXME: Must do async reload, see SrsHls::async_reload.

// cleanup dvr
dvr->on_unpublish();
Expand Down Expand Up @@ -1422,8 +1406,8 @@ srs_error_t SrsOriginHub::on_reload_vhost_transcode(string vhost)
if (req_->vhost != vhost) {
return err;
}
// TODO: FIXME: maybe should ignore when publish already stopped?

// TODO: FIXME: Must do async reload, see SrsHls::async_reload.

encoder->on_unpublish();

Expand All @@ -1447,8 +1431,8 @@ srs_error_t SrsOriginHub::on_reload_vhost_exec(string vhost)
if (req_->vhost != vhost) {
return err;
}
// TODO: FIXME: maybe should ignore when publish already stopped?

// TODO: FIXME: Must do async reload, see SrsHls::async_reload.

ng_exec->on_unpublish();

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 @@ -368,6 +368,8 @@ class SrsOriginHub : public ISrsReloadHandler
virtual srs_error_t on_forwarder_start(SrsForwarder* forwarder);
// For the SrsDvr to callback to request the sequence headers.
virtual srs_error_t on_dvr_request_sh();
// For the SrsHls to callback to request the sequence headers.
virtual srs_error_t on_hls_request_sh();
// Interface ISrsReloadHandler
public:
virtual srs_error_t on_reload_vhost_forward(std::string vhost);
Expand Down

0 comments on commit 5827864

Please sign in to comment.