Skip to content

Commit b475d55

Browse files
winlinvipsuzp1984
andauthored
Heartbeat: Report ports for proxy server. v5.0.215 v6.0.156 v7.0.15 (#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 d70e735 commit b475d55

File tree

9 files changed

+110
-14
lines changed

9 files changed

+110
-14
lines changed

.run/private.run.xml

Lines changed: 0 additions & 1 deletion
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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,14 @@ heartbeat {
907907
# Overwrite by env SRS_HEARTBEAT_SUMMARIES
908908
# default: off
909909
summaries off;
910+
# Whether report with listen ports.
911+
# if on, request with the ports of SRS:
912+
# {
913+
# "rtmp": ["1935"], "http": ["8080"], "api": ["1985"], "srt": ["10080"], "rtc": ["8000"]
914+
# }
915+
# Overwrite by env SRS_HEARTBEAT_PORTS
916+
# default: off
917+
ports off;
910918
}
911919

912920
# system statistics section.

trunk/doc/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The changelog for SRS.
77
<a name="v7-changes"></a>
88

99
## SRS 7.0 Changelog
10+
* v7.0, 2024-09-09, Merge [#4171](https://github.com/ossrs/srs/pull/4171): Heartbeat: Report ports for proxy server. v7.0.15 (#4171)
1011
* v7.0, 2024-09-01, Merge [#4165](https://github.com/ossrs/srs/pull/4165): FLV: Refine source and http handler. v7.0.14 (#4165)
1112
* v7.0, 2024-09-01, Merge [#4166](https://github.com/ossrs/srs/pull/4166): Edge: Fix flv edge crash when http unmount. v7.0.13 (#4166)
1213
* v7.0, 2024-08-31, Merge [#4162](https://github.com/ossrs/srs/pull/4162): Fix #3767: RTMP: Do not response empty data packet. v7.0.12 (#4162)
@@ -26,6 +27,7 @@ The changelog for SRS.
2627
<a name="v6-changes"></a>
2728

2829
## SRS 6.0 Changelog
30+
* v6.0, 2024-09-09, Merge [#4171](https://github.com/ossrs/srs/pull/4171): Heartbeat: Report ports for proxy server. v6.0.156 (#4171)
2931
* v6.0, 2024-09-01, Merge [#4165](https://github.com/ossrs/srs/pull/4165): FLV: Refine source and http handler. v6.0.155 (#4165)
3032
* v6.0, 2024-09-01, Merge [#4166](https://github.com/ossrs/srs/pull/4166): Edge: Fix flv edge crash when http unmount. v6.0.154 (#4166)
3133
* v6.0, 2024-08-31, Merge [#4162](https://github.com/ossrs/srs/pull/4162): Fix #3767: RTMP: Do not response empty data packet. v6.0.153 (#4162)
@@ -185,6 +187,7 @@ The changelog for SRS.
185187
<a name="v5-changes"></a>
186188

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,7 +2409,7 @@ srs_error_t SrsConfig::check_normal_config()
24092409
for (int i = 0; conf && i < (int)conf->directives.size(); i++) {
24102410
string n = conf->at(i)->name;
24112411
if (n != "enabled" && n != "interval" && n != "url"
2412-
&& n != "device_id" && n != "summaries") {
2412+
&& n != "device_id" && n != "summaries" && n != "ports") {
24132413
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal heartbeat.%s", n.c_str());
24142414
}
24152415
}
@@ -8794,17 +8794,36 @@ bool SrsConfig::get_heartbeat_summaries()
87948794
SRS_OVERWRITE_BY_ENV_BOOL("srs.heartbeat.summaries"); // SRS_HEARTBEAT_SUMMARIES
87958795

87968796
static bool DEFAULT = false;
8797-
8797+
87988798
SrsConfDirective* conf = get_heartbeart();
87998799
if (!conf) {
88008800
return DEFAULT;
88018801
}
8802-
8802+
88038803
conf = conf->get("summaries");
88048804
if (!conf || conf->arg0().empty()) {
88058805
return DEFAULT;
88068806
}
8807-
8807+
8808+
return SRS_CONF_PREFER_FALSE(conf->arg0());
8809+
}
8810+
8811+
bool SrsConfig::get_heartbeat_ports()
8812+
{
8813+
SRS_OVERWRITE_BY_ENV_BOOL("srs.heartbeat.ports"); // SRS_HEARTBEAT_PORTS
8814+
8815+
static bool DEFAULT = false;
8816+
8817+
SrsConfDirective* conf = get_heartbeart();
8818+
if (!conf) {
8819+
return DEFAULT;
8820+
}
8821+
8822+
conf = conf->get("ports");
8823+
if (!conf || conf->arg0().empty()) {
8824+
return DEFAULT;
8825+
}
8826+
88088827
return SRS_CONF_PREFER_FALSE(conf->arg0());
88098828
}
88108829

trunk/src/app/srs_app_config.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,7 @@ class SrsConfig
11191119
virtual std::string get_heartbeat_device_id();
11201120
// Whether report with summaries of http api: /api/v1/summaries.
11211121
virtual bool get_heartbeat_summaries();
1122+
bool get_heartbeat_ports();
11221123
// stats section
11231124
private:
11241125
// Get the stats directive.

trunk/src/app/srs_app_heartbeat.cpp

Lines changed: 72 additions & 6 deletions
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,25 +50,89 @@ 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
SrsUniquePtr<SrsJsonObject> obj(SrsJsonAny::object());
6067

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

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

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

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

trunk/src/core/srs_core_version5.hpp

Lines changed: 1 addition & 1 deletion
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

trunk/src/core/srs_core_version6.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
#define VERSION_MAJOR 6
1111
#define VERSION_MINOR 0
12-
#define VERSION_REVISION 155
12+
#define VERSION_REVISION 156
1313

1414
#endif

trunk/src/core/srs_core_version7.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
#define VERSION_MAJOR 7
1111
#define VERSION_MINOR 0
12-
#define VERSION_REVISION 14
12+
#define VERSION_REVISION 15
1313

1414
#endif

0 commit comments

Comments
 (0)