Skip to content

Commit

Permalink
Heartbeat: Report ports for proxy server. v5.0.215 (#4171)
Browse files Browse the repository at this point in the history
The heartbeat of SRS is a timer that requests an HTTP URL. We can use
this heartbeat to report the necessary information for registering the
backend server with the proxy server.

```text
SRS(backend) --heartbeat---> Proxy server
```

A proxy server is a specialized load balancer for media servers. It
operates at the application level rather than the TCP level. For more
information about the proxy server, see issue #4158.

Note that we will merge this PR into SRS 5.0+, allowing the use of SRS
5.0+ as the backend server, not limited to SRS 7.0. However, the proxy
server is introduced in SRS 7.0.

It's also possible to implement a registration service, allowing you to
use other media servers as backend servers. For example, if you gather
information about an nginx-rtmp server and register it with the proxy
server, the proxy will forward RTMP streams to nginx-rtmp. The backend
server is not limited to SRS.

---------

Co-authored-by: Jacob Su <[email protected]>
  • Loading branch information
winlinvip and suzp1984 committed Sep 9, 2024
1 parent ad0d510 commit bfdbbb9
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 12 deletions.
1 change: 0 additions & 1 deletion .run/private.run.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="private" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="-c console.conf" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" WORKING_DIR="file://$CMakeCurrentBuildDir$/../../../" PASS_PARENT_ENVS_2="true" PROJECT_NAME="srs" TARGET_NAME="srs" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="srs" RUN_TARGET_NAME="srs">
<envs>
<env name="SRS_RTC_SERVER_ENABLED" value="on" />
<env name="MallocNanoZone" value="0" />
</envs>
<method v="2">
Expand Down
8 changes: 8 additions & 0 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,14 @@ heartbeat {
# Overwrite by env SRS_HEARTBEAT_SUMMARIES
# default: off
summaries off;
# Whether report with listen ports.
# if on, request with the ports of SRS:
# {
# "rtmp": ["1935"], "http": ["8080"], "api": ["1985"], "srt": ["10080"], "rtc": ["8000"]
# }
# Overwrite by env SRS_HEARTBEAT_PORTS
# default: off
ports off;
}

# system statistics section.
Expand Down
1 change: 1 addition & 0 deletions trunk/doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The changelog for SRS.
<a name="v5-changes"></a>

## SRS 5.0 Changelog
* v5.0, 2024-09-09, Merge [#4171](https://github.com/ossrs/srs/pull/4171): Heartbeat: Report ports for proxy server. v5.0.215 (#4171)
* v5.0, 2024-07-24, Merge [#4126](https://github.com/ossrs/srs/pull/4126): Edge: Improve stability for state and fd closing. v5.0.214 (#4126)
* v5.0, 2024-06-03, Merge [#4057](https://github.com/ossrs/srs/pull/4057): RTC: Support dropping h.264 SEI from NALUs. v5.0.213 (#4057)
* v5.0, 2024-04-23, Merge [#4038](https://github.com/ossrs/srs/pull/4038): RTMP: Do not response publish start message if hooks fail. v5.0.212 (#4038)
Expand Down
27 changes: 23 additions & 4 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2391,7 +2391,7 @@ srs_error_t SrsConfig::check_normal_config()
for (int i = 0; conf && i < (int)conf->directives.size(); i++) {
string n = conf->at(i)->name;
if (n != "enabled" && n != "interval" && n != "url"
&& n != "device_id" && n != "summaries") {
&& n != "device_id" && n != "summaries" && n != "ports") {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal heartbeat.%s", n.c_str());
}
}
Expand Down Expand Up @@ -8775,17 +8775,36 @@ bool SrsConfig::get_heartbeat_summaries()
SRS_OVERWRITE_BY_ENV_BOOL("srs.heartbeat.summaries"); // SRS_HEARTBEAT_SUMMARIES

static bool DEFAULT = false;

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

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


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

bool SrsConfig::get_heartbeat_ports()
{
SRS_OVERWRITE_BY_ENV_BOOL("srs.heartbeat.ports"); // SRS_HEARTBEAT_PORTS

static bool DEFAULT = false;

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

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

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

Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,7 @@ class SrsConfig
virtual std::string get_heartbeat_device_id();
// Whether report with summaries of http api: /api/v1/summaries.
virtual bool get_heartbeat_summaries();
bool get_heartbeat_ports();
// stats section
private:
// Get the stats directive.
Expand Down
78 changes: 72 additions & 6 deletions trunk/src/app/srs_app_heartbeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ using namespace std;
#include <srs_core_autofree.hpp>
#include <srs_app_http_conn.hpp>
#include <srs_protocol_amf0.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_app_statistic.hpp>

SrsHttpHeartbeat::SrsHttpHeartbeat()
{
Expand Down Expand Up @@ -48,26 +50,90 @@ srs_error_t SrsHttpHeartbeat::do_heartbeat()
return srs_error_wrap(err, "http uri parse hartbeart url failed. url=%s", url.c_str());
}

SrsIPAddress* ip = NULL;
string ip;
std::string device_id = _srs_config->get_heartbeat_device_id();

vector<SrsIPAddress*>& ips = srs_get_local_ips();
if (!ips.empty()) {
ip = ips[_srs_config->get_stats_network() % (int)ips.size()];

// Try to load the ip from the environment variable.
ip = srs_getenv("srs.device.ip"); // SRS_DEVICE_IP
if (ip.empty()) {
// Use the local ip address specified by the stats.network config.
vector<SrsIPAddress*>& ips = srs_get_local_ips();
if (!ips.empty()) {
ip = ips[_srs_config->get_stats_network() % (int) ips.size()]->ip;
}
}

SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj);

obj->set("device_id", SrsJsonAny::str(device_id.c_str()));
obj->set("ip", SrsJsonAny::str(ip->ip.c_str()));
obj->set("ip", SrsJsonAny::str(ip.c_str()));

SrsStatistic* stat = SrsStatistic::instance();
obj->set("server", SrsJsonAny::str(stat->server_id().c_str()));
obj->set("service", SrsJsonAny::str(stat->service_id().c_str()));
obj->set("pid", SrsJsonAny::str(stat->service_pid().c_str()));

if (_srs_config->get_heartbeat_summaries()) {
SrsJsonObject* summaries = SrsJsonAny::object();
obj->set("summaries", summaries);

srs_api_dump_summaries(summaries);
}

if (_srs_config->get_heartbeat_ports()) {
// For RTMP listen endpoints.
if (true) {
SrsJsonArray* o = SrsJsonAny::array();
obj->set("rtmp", o);

vector<string> endpoints = _srs_config->get_listens();
for (int i = 0; i < (int) endpoints.size(); i++) {
o->append(SrsJsonAny::str(endpoints.at(i).c_str()));
}
}

// For HTTP Stream listen endpoints.
if (_srs_config->get_http_stream_enabled()) {
SrsJsonArray* o = SrsJsonAny::array();
obj->set("http", o);

string endpoint = _srs_config->get_http_stream_listen();
o->append(SrsJsonAny::str(endpoint.c_str()));
}

// For HTTP API listen endpoints.
if (_srs_config->get_http_api_enabled()) {
SrsJsonArray* o = SrsJsonAny::array();
obj->set("api", o);

string endpoint = _srs_config->get_http_api_listen();
o->append(SrsJsonAny::str(endpoint.c_str()));
}

// For SRT listen endpoints.
if (_srs_config->get_srt_enabled()) {
SrsJsonArray* o = SrsJsonAny::array();
obj->set("srt", o);

uint16_t endpoint = _srs_config->get_srt_listen_port();
o->append(SrsJsonAny::str(srs_fmt("udp://0.0.0.0:%d", endpoint).c_str()));
}

// For WebRTC listen endpoints.
if (_srs_config->get_rtc_server_enabled()) {
SrsJsonArray* o = SrsJsonAny::array();
obj->set("rtc", o);

int endpoint = _srs_config->get_rtc_server_listen();
o->append(SrsJsonAny::str(srs_fmt("udp://0.0.0.0:%d", endpoint).c_str()));

if (_srs_config->get_rtc_server_tcp_enabled()) {
endpoint = _srs_config->get_rtc_server_tcp_listen();
o->append(SrsJsonAny::str(srs_fmt("tcp://0.0.0.0:%d", endpoint).c_str()));
}
}
}

SrsHttpClient http;
if ((err = http.initialize(uri.get_schema(), uri.get_host(), uri.get_port())) != srs_success) {
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_version5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

#define VERSION_MAJOR 5
#define VERSION_MINOR 0
#define VERSION_REVISION 214
#define VERSION_REVISION 215

#endif

0 comments on commit bfdbbb9

Please sign in to comment.