Skip to content

Commit bfdbbb9

Browse files
winlinvipsuzp1984
andcommitted
Heartbeat: Report ports for proxy server. v5.0.215 (#4171)
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]>
1 parent ad0d510 commit bfdbbb9

7 files changed

+106
-12
lines changed

.run/private.run.xml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<component name="ProjectRunConfigurationManager">
22
<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">
33
<envs>
4-
<env name="SRS_RTC_SERVER_ENABLED" value="on" />
54
<env name="MallocNanoZone" value="0" />
65
</envs>
76
<method v="2">

trunk/conf/full.conf

+8
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,14 @@ heartbeat {
899899
# Overwrite by env SRS_HEARTBEAT_SUMMARIES
900900
# default: off
901901
summaries off;
902+
# Whether report with listen ports.
903+
# if on, request with the ports of SRS:
904+
# {
905+
# "rtmp": ["1935"], "http": ["8080"], "api": ["1985"], "srt": ["10080"], "rtc": ["8000"]
906+
# }
907+
# Overwrite by env SRS_HEARTBEAT_PORTS
908+
# default: off
909+
ports off;
902910
}
903911

904912
# system statistics section.

trunk/doc/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The changelog for SRS.
77
<a name="v5-changes"></a>
88

99
## SRS 5.0 Changelog
10+
* v5.0, 2024-09-09, Merge [#4171](https://github.com/ossrs/srs/pull/4171): Heartbeat: Report ports for proxy server. v5.0.215 (#4171)
1011
* 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)
1112
* 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)
1213
* 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)

trunk/src/app/srs_app_config.cpp

+23-4
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ srs_error_t SrsConfig::check_normal_config()
23912391
for (int i = 0; conf && i < (int)conf->directives.size(); i++) {
23922392
string n = conf->at(i)->name;
23932393
if (n != "enabled" && n != "interval" && n != "url"
2394-
&& n != "device_id" && n != "summaries") {
2394+
&& n != "device_id" && n != "summaries" && n != "ports") {
23952395
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal heartbeat.%s", n.c_str());
23962396
}
23972397
}
@@ -8775,17 +8775,36 @@ bool SrsConfig::get_heartbeat_summaries()
87758775
SRS_OVERWRITE_BY_ENV_BOOL("srs.heartbeat.summaries"); // SRS_HEARTBEAT_SUMMARIES
87768776

87778777
static bool DEFAULT = false;
8778-
8778+
87798779
SrsConfDirective* conf = get_heartbeart();
87808780
if (!conf) {
87818781
return DEFAULT;
87828782
}
8783-
8783+
87848784
conf = conf->get("summaries");
87858785
if (!conf || conf->arg0().empty()) {
87868786
return DEFAULT;
87878787
}
8788-
8788+
8789+
return SRS_CONF_PERFER_FALSE(conf->arg0());
8790+
}
8791+
8792+
bool SrsConfig::get_heartbeat_ports()
8793+
{
8794+
SRS_OVERWRITE_BY_ENV_BOOL("srs.heartbeat.ports"); // SRS_HEARTBEAT_PORTS
8795+
8796+
static bool DEFAULT = false;
8797+
8798+
SrsConfDirective* conf = get_heartbeart();
8799+
if (!conf) {
8800+
return DEFAULT;
8801+
}
8802+
8803+
conf = conf->get("ports");
8804+
if (!conf || conf->arg0().empty()) {
8805+
return DEFAULT;
8806+
}
8807+
87898808
return SRS_CONF_PERFER_FALSE(conf->arg0());
87908809
}
87918810

trunk/src/app/srs_app_config.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ class SrsConfig
11151115
virtual std::string get_heartbeat_device_id();
11161116
// Whether report with summaries of http api: /api/v1/summaries.
11171117
virtual bool get_heartbeat_summaries();
1118+
bool get_heartbeat_ports();
11181119
// stats section
11191120
private:
11201121
// Get the stats directive.

trunk/src/app/srs_app_heartbeat.cpp

+72-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ using namespace std;
1818
#include <srs_core_autofree.hpp>
1919
#include <srs_app_http_conn.hpp>
2020
#include <srs_protocol_amf0.hpp>
21+
#include <srs_kernel_utility.hpp>
22+
#include <srs_app_statistic.hpp>
2123

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

51-
SrsIPAddress* ip = NULL;
53+
string ip;
5254
std::string device_id = _srs_config->get_heartbeat_device_id();
53-
54-
vector<SrsIPAddress*>& ips = srs_get_local_ips();
55-
if (!ips.empty()) {
56-
ip = ips[_srs_config->get_stats_network() % (int)ips.size()];
55+
56+
// Try to load the ip from the environment variable.
57+
ip = srs_getenv("srs.device.ip"); // SRS_DEVICE_IP
58+
if (ip.empty()) {
59+
// Use the local ip address specified by the stats.network config.
60+
vector<SrsIPAddress*>& ips = srs_get_local_ips();
61+
if (!ips.empty()) {
62+
ip = ips[_srs_config->get_stats_network() % (int) ips.size()]->ip;
63+
}
5764
}
5865

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

6269
obj->set("device_id", SrsJsonAny::str(device_id.c_str()));
63-
obj->set("ip", SrsJsonAny::str(ip->ip.c_str()));
70+
obj->set("ip", SrsJsonAny::str(ip.c_str()));
71+
72+
SrsStatistic* stat = SrsStatistic::instance();
73+
obj->set("server", SrsJsonAny::str(stat->server_id().c_str()));
74+
obj->set("service", SrsJsonAny::str(stat->service_id().c_str()));
75+
obj->set("pid", SrsJsonAny::str(stat->service_pid().c_str()));
6476

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

6981
srs_api_dump_summaries(summaries);
7082
}
83+
84+
if (_srs_config->get_heartbeat_ports()) {
85+
// For RTMP listen endpoints.
86+
if (true) {
87+
SrsJsonArray* o = SrsJsonAny::array();
88+
obj->set("rtmp", o);
89+
90+
vector<string> endpoints = _srs_config->get_listens();
91+
for (int i = 0; i < (int) endpoints.size(); i++) {
92+
o->append(SrsJsonAny::str(endpoints.at(i).c_str()));
93+
}
94+
}
95+
96+
// For HTTP Stream listen endpoints.
97+
if (_srs_config->get_http_stream_enabled()) {
98+
SrsJsonArray* o = SrsJsonAny::array();
99+
obj->set("http", o);
100+
101+
string endpoint = _srs_config->get_http_stream_listen();
102+
o->append(SrsJsonAny::str(endpoint.c_str()));
103+
}
104+
105+
// For HTTP API listen endpoints.
106+
if (_srs_config->get_http_api_enabled()) {
107+
SrsJsonArray* o = SrsJsonAny::array();
108+
obj->set("api", o);
109+
110+
string endpoint = _srs_config->get_http_api_listen();
111+
o->append(SrsJsonAny::str(endpoint.c_str()));
112+
}
113+
114+
// For SRT listen endpoints.
115+
if (_srs_config->get_srt_enabled()) {
116+
SrsJsonArray* o = SrsJsonAny::array();
117+
obj->set("srt", o);
118+
119+
uint16_t endpoint = _srs_config->get_srt_listen_port();
120+
o->append(SrsJsonAny::str(srs_fmt("udp://0.0.0.0:%d", endpoint).c_str()));
121+
}
122+
123+
// For WebRTC listen endpoints.
124+
if (_srs_config->get_rtc_server_enabled()) {
125+
SrsJsonArray* o = SrsJsonAny::array();
126+
obj->set("rtc", o);
127+
128+
int endpoint = _srs_config->get_rtc_server_listen();
129+
o->append(SrsJsonAny::str(srs_fmt("udp://0.0.0.0:%d", endpoint).c_str()));
130+
131+
if (_srs_config->get_rtc_server_tcp_enabled()) {
132+
endpoint = _srs_config->get_rtc_server_tcp_listen();
133+
o->append(SrsJsonAny::str(srs_fmt("tcp://0.0.0.0:%d", endpoint).c_str()));
134+
}
135+
}
136+
}
71137

72138
SrsHttpClient http;
73139
if ((err = http.initialize(uri.get_schema(), uri.get_host(), uri.get_port())) != srs_success) {

trunk/src/core/srs_core_version5.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
#define VERSION_MAJOR 5
1111
#define VERSION_MINOR 0
12-
#define VERSION_REVISION 214
12+
#define VERSION_REVISION 215
1313

1414
#endif

0 commit comments

Comments
 (0)