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

Support RTMPS publish and forward #3949

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from
22 changes: 22 additions & 0 deletions trunk/conf/forward.master.rtmps.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# the config for srs to forward
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/sample-forward
# @see full.conf for detail config.

listen 1935;
max_connections 1000;
pid ./objs/srs.master.pid;
daemon off;
srs_log_tank console;
rtmps {
enabled on;
listen 1943;
key ./conf/server.key;
cert ./conf/server.crt;
}
vhost __defaultVhost__ {
forward {
rtmps on;
enabled on;
destination 127.0.0.1:19430;
}
}
17 changes: 17 additions & 0 deletions trunk/conf/forward.slave.rtmps.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# the config for srs to forward
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/sample-forward
# @see full.conf for detail config.

listen 19350;
max_connections 1000;
pid ./objs/srs.slave.pid;
daemon off;
srs_log_tank console;
rtmps {
enabled on;
listen 19430;
key ./conf/server.key;
cert ./conf/server.crt;
}
vhost __defaultVhost__ {
}
24 changes: 24 additions & 0 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ listen 1935;
# Overwrite by env SRS_CHUNK_SIZE
# default: 60000
chunk_size 60000;
# the rtmps listen ports
rtmps {
# Whether rtmps is enabled.
# Overwrite by env SRS_RTMPS_ENABLED
# default: off
enabled on;
# The rtmps listen port
# Default: 1943
listen 1943;
# The SSL private key file, generated by:
# openssl genrsa -out server.key 2048
# Overwrite by env SRS_RTMPS_KEY
# default: ./conf/server.key
key ./conf/server.key;
# The SSL public cert file, generated by:
# openssl req -new -x509 -key server.key -out server.crt -days 3650 -subj "/C=CN/ST=Beijing/L=Beijing/O=Me/OU=Me/CN=ossrs.net"
# Overwrite by env SRS_RTMPS_CERT
# default: ./conf/server.crt
cert ./conf/server.crt;
}

#############################################################################################
# HTTP sections
Expand Down Expand Up @@ -1087,6 +1107,10 @@ vhost same.vhost.forward.srs.com {
# active-active for cdn to build high available fault tolerance system.
# format: {ip}:{port} {ip_N}:{port_N}
destination 127.0.0.1:1936 127.0.0.1:1937;

# Whether enable forward whith rtmps format.
# default: off
rtmps on;

# when client(encoder) publish to vhost/app/stream, call the hook in creating backend forwarder.
# the request in the POST data string is a object encode by json:
Expand Down
13 changes: 13 additions & 0 deletions trunk/conf/rtmps.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

listen 1935;
max_connections 1000;
daemon off;
srs_log_tank console;
rtmps {
enabled on;
listen 1943;
key ./conf/server.key;
cert ./conf/server.crt;
}
vhost __defaultVhost__ {
}
125 changes: 123 additions & 2 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2330,7 +2330,7 @@ srs_error_t SrsConfig::check_normal_config()
&& n != "inotify_auto_reload" && n != "auto_reload_for_docker" && n != "tcmalloc_release_rate"
&& n != "query_latest_version" && n != "first_wait_for_qlv" && n != "threads"
&& n != "circuit_breaker" && n != "is_full" && n != "in_docker" && n != "tencentcloud_cls"
&& n != "exporter"
&& n != "exporter" && n != "rtmps"
) {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal directive %s", n.c_str());
}
Expand Down Expand Up @@ -2426,6 +2426,15 @@ srs_error_t SrsConfig::check_normal_config()
}
}
}
if (true) {
SrsConfDirective* conf = root->get("rtmps");
for (int i = 0; conf && i < (int)conf->directives.size(); i++) {
string n = conf->at(i)->name;
if (n != "enabled" && n != "listen" && n != "key" && n != "cert") {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal rtmps.%s", n.c_str());
}
}
}

////////////////////////////////////////////////////////////////////////
// check listen for rtmp.
Expand Down Expand Up @@ -2686,7 +2695,7 @@ srs_error_t SrsConfig::check_normal_config()
} else if (n == "forward") {
for (int j = 0; j < (int)conf->directives.size(); j++) {
string m = conf->at(j)->name;
if (m != "enabled" && m != "destination" && m != "backend") {
if (m != "enabled" && m != "destination" && m != "backend" && m != "rtmps") {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.forward.%s of %s", m.c_str(), vhost->arg0().c_str());
}
}
Expand Down Expand Up @@ -5586,6 +5595,26 @@ SrsConfDirective* SrsConfig::get_forward_backend(string vhost)
return conf->get("backend");
}

bool SrsConfig::get_forward_rtmps(string vhost)
{
SrsConfDirective* conf = get_vhost(vhost);
if (!conf) {
return false;
}

conf = conf->get("forward");
if (!conf) {
return false;
}

conf = conf->get("rtmps");
if (!conf) {
return false;
}

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

SrsConfDirective* SrsConfig::get_vhost_http_hooks(string vhost)
{
SrsConfDirective* conf = get_vhost(vhost);
Expand Down Expand Up @@ -8823,3 +8852,95 @@ SrsConfDirective* SrsConfig::get_stats_disk_device()

return conf;
}

SrsConfDirective* SrsConfig::get_rtmps()
{
SrsConfDirective* conf = root->get("rtmps");
if (!conf) {
return NULL;
}

return conf;
}

bool SrsConfig::get_rtmps_enabled()
{
SRS_OVERWRITE_BY_ENV_BOOL("srs.rtmps.enabled"); // SRS_RTMPS_ENABLED

static bool DEFAULT = false;

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

conf = conf->get("enabled");
if (!conf) {
return DEFAULT;
}

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

vector<string> SrsConfig::get_rtmps_listen()
{
if (!srs_getenv("srs.rtmps.listen").empty()) { // SRS_LISTEN
return srs_string_split(srs_getenv("srs.rtmps.listen"), " ");
}

std::vector<string> ports;

SrsConfDirective* conf = get_rtmps();
if (!conf) {
return ports;
}

conf = conf->get("listen");
if (!conf) {
return ports;
}

for (int i = 0; i < (int)conf->args.size(); i++) {
ports.push_back(conf->args.at(i));
}

return ports;
}

string SrsConfig::get_rtmps_ssl_key()
{
SRS_OVERWRITE_BY_ENV_STRING("srs.rtmps.key"); // SRS_RTMPS_KEY

static string DEFAULT = "./conf/server.key";

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

conf = conf->get("key");
if (!conf) {
return DEFAULT;
}

return conf->arg0();
}

string SrsConfig::get_rtmps_ssl_cert()
{
SRS_OVERWRITE_BY_ENV_STRING("srs.rtmps.cert"); // SRS_RTMPS_CERT

static string DEFAULT = "./conf/server.crt";

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

conf = conf->get("cert");
if (!conf) {
return DEFAULT;
}

return conf->arg0();
}
10 changes: 10 additions & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@ class SrsConfig
virtual SrsConfDirective* get_forwards(std::string vhost);
// Get the forward directive of backend.
virtual SrsConfDirective* get_forward_backend(std::string vhost);
// Get forward rtmps enabled.
virtual bool get_forward_rtmps(std::string vhost);

public:
// Whether the srt sevice enabled
Expand Down Expand Up @@ -1136,6 +1138,14 @@ class SrsConfig
virtual std::string get_exporter_listen();
virtual std::string get_exporter_label();
virtual std::string get_exporter_tag();
// rtmps section
private:
SrsConfDirective* get_rtmps();
public:
virtual bool get_rtmps_enabled();
virtual std::vector<std::string> get_rtmps_listen();
virtual std::string get_rtmps_ssl_key();
virtual std::string get_rtmps_ssl_cert();
};

#endif
Expand Down