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

HLS: support load exists m3u8, to continue publish stream.

This commit is contained in:
chundonglinlin 2022-06-28 21:20:18 +08:00 committed by chenhaibo
parent 26aabe413d
commit d6cbd277cc
5 changed files with 86 additions and 26 deletions

View file

@ -17,5 +17,6 @@ vhost __defaultVhost__ {
hls_path ./objs/nginx/html;
hls_fragment 10;
hls_window 60;
hls_continuous on;
}
}

View file

@ -2609,7 +2609,7 @@ srs_error_t SrsConfig::check_normal_config()
&& m != "hls_storage" && m != "hls_mount" && m != "hls_td_ratio" && m != "hls_aof_ratio" && m != "hls_acodec" && m != "hls_vcodec"
&& m != "hls_m3u8_file" && m != "hls_ts_file" && m != "hls_ts_floor" && m != "hls_cleanup" && m != "hls_nb_notify"
&& m != "hls_wait_keyframe" && m != "hls_dispose" && m != "hls_keys" && m != "hls_fragments_per_key" && m != "hls_key_file"
&& m != "hls_key_file_path" && m != "hls_key_url" && m != "hls_dts_directly" && m != "hls_ctx" && m != "hls_ts_ctx") {
&& m != "hls_key_file_path" && m != "hls_key_url" && m != "hls_dts_directly" && m != "hls_ctx" && m != "hls_ts_ctx" && m != "hls_continuous") {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.hls.%s of %s", m.c_str(), vhost->arg0().c_str());
}
@ -7234,6 +7234,23 @@ string SrsConfig::get_hls_key_url(std::string vhost)
return conf->arg0();
}
bool SrsConfig::get_hls_continuous(string vhost)
{
static bool DEFAULT = true;
SrsConfDirective* conf = get_hls(vhost);
if (!conf) {
return DEFAULT;
}
conf = conf->get("hls_continuous");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return SRS_CONF_PERFER_TRUE(conf->arg0());
}
SrsConfDirective *SrsConfig::get_hds(const string &vhost)
{
SrsConfDirective* conf = get_vhost(vhost);

View file

@ -967,6 +967,10 @@ public:
virtual bool get_hls_ctx_enabled(std::string vhost);
// Whether enable session for ts file.
virtual bool get_hls_ts_ctx_enabled(std::string vhost);
// Toggles HLS continuous mode.
// In this mode HLS sequence number is started from where it stopped last time.
// Old fragments are kept. Default is on.
virtual bool get_hls_continuous(std::string vhost);
// hds section
private:
// Get the hds directive of vhost.

View file

@ -383,6 +383,36 @@ srs_error_t SrsHlsMuxer::update_config(SrsRequest* r, string entry_prefix,
return err;
}
srs_error_t SrsHlsMuxer::restore_stream()
{
srs_error_t err = srs_success;
// exist the m3u8 file.
if (!srs_path_exists(m3u8)) {
return err;
}
srs_trace("hls: restore stream m3u8=%s, m3u8_url=%s", m3u8.c_str(), m3u8_url.c_str());
// read m3u8
SrsFileReader fr;
if ((err = fr.open(m3u8)) != srs_success) {
return srs_error_wrap(err, "open file");
}
int nb_fbuf = fr.filesize();
char* fbuf = new char[nb_fbuf];
SrsAutoFreeA(char, fbuf);
if ((err = fr.read(fbuf, nb_fbuf, NULL)) != srs_success) {
return srs_error_wrap(err, "read data");
}
fr.close();
// parse
return err;
}
srs_error_t SrsHlsMuxer::segment_open()
{
srs_error_t err = srs_success;
@ -949,6 +979,7 @@ srs_error_t SrsHlsController::on_publish(SrsRequest* req)
// TODO: FIXME: support load exists m3u8, to continue publish stream.
// for the HLS donot requires the EXT-X-MEDIA-SEQUENCE be monotonically increase.
bool continuous = _srs_config->get_hls_continuous(vhost);
if ((err = muxer->on_publish(req)) != srs_success) {
return srs_error_wrap(err, "muxer publish");
@ -960,6 +991,10 @@ srs_error_t SrsHlsController::on_publish(SrsRequest* req)
return srs_error_wrap(err, "hls: update config");
}
if (continuous && (err = muxer->restore_stream()) != srs_success ) {
return srs_error_wrap(err, "hls: restore stream");
}
if ((err = muxer->segment_open()) != srs_success) {
return srs_error_wrap(err, "hls: segment open");
}

View file

@ -211,6 +211,9 @@ private:
virtual srs_error_t write_hls_key();
virtual srs_error_t refresh_m3u8();
virtual srs_error_t _refresh_m3u8(std::string m3u8_file);
public:
// HLS continuous mode.
srs_error_t restore_stream();
};
// The hls stream cache,