Skip to content

Commit

Permalink
feature: support HLS fmp4 segment.
Browse files Browse the repository at this point in the history
  • Loading branch information
suzp1984 committed Sep 2, 2024
1 parent 15fbe45 commit 3d0cbb5
Show file tree
Hide file tree
Showing 11 changed files with 1,830 additions and 126 deletions.
27 changes: 27 additions & 0 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,13 @@ vhost hls.srs.com {
# default: off
enabled on;

# whether to use fmp4 as container
# The default value is off, then HLS use ts as container format,
# if on, HLS use fmp4 as container format.
# Overwrite by env SRS_VHOST_HLS_HLS_USE_FMP4 for all vhosts.
# default: off
hls_use_fmp4 on;

# the hls fragment in seconds, the duration of a piece of ts.
# Overwrite by env SRS_VHOST_HLS_HLS_FRAGMENT for all vhosts.
# default: 10
Expand Down Expand Up @@ -1852,6 +1859,26 @@ vhost hls.srs.com {
# Overwrite by env SRS_VHOST_HLS_HLS_TS_FILE for all vhosts.
# default: [app]/[stream]-[seq].ts
hls_ts_file [app]/[stream]-[seq].ts;
# the hls fmp4 file name.
# we supports some variables to generate the filename.
# [vhost], the vhost of stream.
# [app], the app of stream.
# [stream], the stream name of stream.
# [2006], replace this const to current year.
# [01], replace this const to current month.
# [02], replace this const to current date.
# [15], replace this const to current hour.
# [04], replace this const to current minute.
# [05], replace this const to current second.p
# [999], replace this const to current millisecond.
# [timestamp],replace this const to current UNIX timestamp in ms.
# [seq], the sequence number of fmp4.
# [duration], replace this const to current ts duration.
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/dvr#custom-path
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/delivery-hls#hls-config
# Overwrite by env SRS_VHOST_HLS_HLS_FMP4_FILE for all vhosts.
# default: [app]/[stream]-[seq].m4s
hls_fmp4_file [app]/[stream]-[seq].m4s;
# the hls entry prefix, which is base url of ts url.
# for example, the prefix is:
# http://your-server/
Expand Down
22 changes: 22 additions & 0 deletions trunk/conf/hls.mp4.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# the config for srs to delivery hls
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/sample-hls
# @see full.conf for detail config.

listen 1935;
max_connections 1000;
daemon off;
srs_log_tank console;
http_server {
enabled on;
listen 8080;
dir ./objs/nginx/html;
}
vhost __defaultVhost__ {
hls {
enabled on;
hls_use_fmp4 on;
hls_path ./objs/nginx/html;
hls_fragment 10;
hls_window 60;
}
}
46 changes: 45 additions & 1 deletion trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2683,7 +2683,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_use_fmp4" && m != "hls_fmp4_file") {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.hls.%s of %s", m.c_str(), vhost->arg0().c_str());
}

Expand Down Expand Up @@ -6936,6 +6936,31 @@ bool SrsConfig::get_hls_enabled(SrsConfDirective* vhost)
return SRS_CONF_PREFER_FALSE(conf->arg0());
}

bool SrsConfig::get_hls_use_fmp4(std::string vhost)
{
SRS_OVERWRITE_BY_ENV_BOOL("srs.vhost.hls.hls_use_fmp4"); // SRS_VHOST_HLS_HLS_USE_FMP4

static bool DEFAULT = false;

SrsConfDirective* conf = get_vhost(vhost);
if (!conf) {
return DEFAULT;
}

conf = conf->get("hls");

if (!conf) {
return DEFAULT;
}

conf = conf->get("hls_use_fmp4");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}

return SRS_CONF_PREFER_FALSE(conf->arg0());
}

string SrsConfig::get_hls_entry_prefix(string vhost)
{
SRS_OVERWRITE_BY_ENV_STRING("srs.vhost.hls.hls_entry_prefix"); // SRS_VHOST_HLS_HLS_ENTRY_PREFIX
Expand Down Expand Up @@ -7012,6 +7037,25 @@ string SrsConfig::get_hls_ts_file(string vhost)
return conf->arg0();
}

string SrsConfig::get_hls_fmp4_file(std::string vhost)
{
SRS_OVERWRITE_BY_ENV_STRING("srs.vhost.hls.hls_fmp4_file"); // SRS_VHOST_HLS_HLS_FMP4_FILE

static string DEFAULT = "[app]/[stream]-[seq].m4s";

SrsConfDirective* conf = get_hls(vhost);
if (!conf) {
return DEFAULT;
}

conf = conf->get("hls_fmp4_file");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}

return conf->arg0();
}

bool SrsConfig::get_hls_ts_floor(string vhost)
{
SRS_OVERWRITE_BY_ENV_BOOL("srs.vhost.hls.hls_ts_floor"); // SRS_VHOST_HLS_HLS_TS_FLOOR
Expand Down
4 changes: 4 additions & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,8 @@ class SrsConfig
// Whether HLS is enabled.
virtual bool get_hls_enabled(std::string vhost);
virtual bool get_hls_enabled(SrsConfDirective* vhost);
// Whether HLS use fmp4 container format
virtual bool get_hls_use_fmp4(std::string vhost);
// Get the HLS m3u8 list ts segment entry prefix info.
virtual std::string get_hls_entry_prefix(std::string vhost);
// Get the HLS ts/m3u8 file store path.
Expand All @@ -941,6 +943,8 @@ class SrsConfig
virtual std::string get_hls_m3u8_file(std::string vhost);
// Get the HLS ts file path template.
virtual std::string get_hls_ts_file(std::string vhost);
// Get the HLS fmp4 file path template.
virtual std::string get_hls_fmp4_file(std::string vhost);
// Whether enable the floor(timestamp/hls_fragment) for variable timestamp.
virtual bool get_hls_ts_floor(std::string vhost);
// Get the hls fragment time, in srs_utime_t.
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_dash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SrsInitMp4 : public SrsFragment
};

// The FMP4(Fragmented MP4) for DASH streaming.
// TODO: The FMP4(Fragmented MP4) for HLS streaming.
class SrsFragmentedMp4 : public SrsFragment
{
private:
Expand Down
Loading

0 comments on commit 3d0cbb5

Please sign in to comment.