2021-05-31 05:42:20 +00:00
|
|
|
//
|
2021-07-08 06:30:47 +00:00
|
|
|
// Copyright (c) 2013-2021 The SRS Authors
|
2021-05-31 05:42:20 +00:00
|
|
|
//
|
2022-01-13 10:40:17 +00:00
|
|
|
// SPDX-License-Identifier: MIT or MulanPSL-2.0
|
2021-05-31 05:42:20 +00:00
|
|
|
//
|
2018-02-15 12:55:34 +00:00
|
|
|
|
|
|
|
#include <srs_app_coworkers.hpp>
|
|
|
|
|
2018-03-03 01:33:15 +00:00
|
|
|
#include <stdlib.h>
|
2018-02-15 12:55:34 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <srs_protocol_json.hpp>
|
|
|
|
#include <srs_kernel_error.hpp>
|
2022-06-09 12:11:52 +00:00
|
|
|
#include <srs_protocol_rtmp_stack.hpp>
|
2018-02-16 01:37:31 +00:00
|
|
|
#include <srs_app_config.hpp>
|
|
|
|
#include <srs_protocol_utility.hpp>
|
2022-06-09 11:59:51 +00:00
|
|
|
#include <srs_protocol_utility.hpp>
|
2018-02-16 06:13:14 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2018-02-15 12:55:34 +00:00
|
|
|
|
|
|
|
SrsCoWorkers* SrsCoWorkers::_instance = NULL;
|
|
|
|
|
|
|
|
SrsCoWorkers::SrsCoWorkers()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsCoWorkers::~SrsCoWorkers()
|
|
|
|
{
|
2018-02-16 01:37:31 +00:00
|
|
|
map<string, SrsRequest*>::iterator it;
|
2018-02-16 02:19:48 +00:00
|
|
|
for (it = streams.begin(); it != streams.end(); ++it) {
|
2018-02-16 01:37:31 +00:00
|
|
|
SrsRequest* r = it->second;
|
|
|
|
srs_freep(r);
|
|
|
|
}
|
2018-02-16 02:19:48 +00:00
|
|
|
streams.clear();
|
2018-02-15 12:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsCoWorkers* SrsCoWorkers::instance()
|
|
|
|
{
|
|
|
|
if (!_instance) {
|
|
|
|
_instance = new SrsCoWorkers();
|
|
|
|
}
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
2019-12-01 11:24:17 +00:00
|
|
|
SrsJsonAny* SrsCoWorkers::dumps(string vhost, string coworker, string app, string stream)
|
2018-02-15 12:55:34 +00:00
|
|
|
{
|
2018-02-16 01:37:31 +00:00
|
|
|
SrsRequest* r = find_stream_info(vhost, app, stream);
|
|
|
|
if (!r) {
|
|
|
|
// TODO: FIXME: Find stream from our origin util return to the start point.
|
|
|
|
return SrsJsonAny::null();
|
|
|
|
}
|
2019-11-30 10:50:24 +00:00
|
|
|
|
|
|
|
// The service port parsing from listen port.
|
|
|
|
string listen_host;
|
|
|
|
int listen_port = SRS_CONSTS_RTMP_DEFAULT_PORT;
|
|
|
|
vector<string> listen_hostports = _srs_config->get_listens();
|
|
|
|
if (!listen_hostports.empty()) {
|
|
|
|
string list_hostport = listen_hostports.at(0);
|
|
|
|
|
|
|
|
if (list_hostport.find(":") != string::npos) {
|
|
|
|
srs_parse_hostport(list_hostport, listen_host, listen_port);
|
|
|
|
} else {
|
|
|
|
listen_port = ::atoi(list_hostport.c_str());
|
|
|
|
}
|
2018-02-16 01:37:31 +00:00
|
|
|
}
|
2019-11-30 10:50:24 +00:00
|
|
|
|
2019-12-01 11:24:17 +00:00
|
|
|
// The ip of server, we use the request coworker-host as ip, if listen host is localhost or loopback.
|
2019-11-30 10:50:24 +00:00
|
|
|
// For example, the server may behind a NAT(192.x.x.x), while its ip is a docker ip(172.x.x.x),
|
|
|
|
// we should use the NAT(192.x.x.x) address as it's the exposed ip.
|
|
|
|
// @see https://github.com/ossrs/srs/issues/1501
|
|
|
|
string service_ip;
|
|
|
|
if (listen_host != SRS_CONSTS_LOCALHOST && listen_host != SRS_CONSTS_LOOPBACK && listen_host != SRS_CONSTS_LOOPBACK6) {
|
|
|
|
service_ip = listen_host;
|
2018-02-16 08:39:07 +00:00
|
|
|
}
|
2019-11-30 10:50:24 +00:00
|
|
|
if (service_ip.empty()) {
|
2019-12-01 11:24:17 +00:00
|
|
|
int coworker_port;
|
|
|
|
string coworker_host = coworker;
|
|
|
|
if (coworker.find(":") != string::npos) {
|
|
|
|
srs_parse_hostport(coworker, coworker_host, coworker_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
service_ip = coworker_host;
|
2019-11-30 10:50:24 +00:00
|
|
|
}
|
|
|
|
if (service_ip.empty()) {
|
|
|
|
service_ip = srs_get_public_internet_address();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The backend API endpoint.
|
2018-02-16 06:13:14 +00:00
|
|
|
string backend = _srs_config->get_http_api_listen();
|
|
|
|
if (backend.find(":") == string::npos) {
|
|
|
|
backend = service_ip + ":" + backend;
|
2018-02-16 01:37:31 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 06:13:14 +00:00
|
|
|
// The routers to detect loop and identify path.
|
|
|
|
SrsJsonArray* routers = SrsJsonAny::array()->append(SrsJsonAny::str(backend.c_str()));
|
2019-12-01 11:24:17 +00:00
|
|
|
|
|
|
|
srs_trace("Redirect vhost=%s, path=%s/%s to ip=%s, port=%d, api=%s",
|
|
|
|
vhost.c_str(), app.c_str(), stream.c_str(), service_ip.c_str(), listen_port, backend.c_str());
|
|
|
|
|
2018-02-16 06:13:14 +00:00
|
|
|
return SrsJsonAny::object()
|
|
|
|
->set("ip", SrsJsonAny::str(service_ip.c_str()))
|
2019-11-30 10:50:24 +00:00
|
|
|
->set("port", SrsJsonAny::integer(listen_port))
|
2018-02-16 06:13:14 +00:00
|
|
|
->set("vhost", SrsJsonAny::str(r->vhost.c_str()))
|
|
|
|
->set("api", SrsJsonAny::str(backend.c_str()))
|
|
|
|
->set("routers", routers);
|
2018-02-16 01:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsRequest* SrsCoWorkers::find_stream_info(string vhost, string app, string stream)
|
|
|
|
{
|
|
|
|
// First, we should parse the vhost, if not exists, try default vhost instead.
|
|
|
|
SrsConfDirective* conf = _srs_config->get_vhost(vhost, true);
|
|
|
|
if (!conf) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get stream information from local cache.
|
|
|
|
string url = srs_generate_stream_url(conf->arg0(), app, stream);
|
2018-02-16 02:19:48 +00:00
|
|
|
map<string, SrsRequest*>::iterator it = streams.find(url);
|
|
|
|
if (it == streams.end()) {
|
2018-02-16 01:37:31 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return it->second;
|
2018-02-15 12:55:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 08:14:00 +00:00
|
|
|
srs_error_t SrsCoWorkers::on_publish(SrsLiveSource* s, SrsRequest* r)
|
2018-02-15 12:55:34 +00:00
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
2018-02-16 01:37:31 +00:00
|
|
|
|
|
|
|
string url = r->get_stream_url();
|
|
|
|
|
|
|
|
// Delete the previous stream informations.
|
2018-02-16 02:19:48 +00:00
|
|
|
map<string, SrsRequest*>::iterator it = streams.find(url);
|
|
|
|
if (it != streams.end()) {
|
2018-02-16 01:37:31 +00:00
|
|
|
srs_freep(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always use the latest one.
|
2018-02-16 02:19:48 +00:00
|
|
|
streams[url] = r->copy();
|
2018-02-16 01:37:31 +00:00
|
|
|
|
2018-02-15 12:55:34 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-05-16 08:14:00 +00:00
|
|
|
void SrsCoWorkers::on_unpublish(SrsLiveSource* s, SrsRequest* r)
|
2018-02-15 12:55:34 +00:00
|
|
|
{
|
2018-02-16 01:37:31 +00:00
|
|
|
string url = r->get_stream_url();
|
|
|
|
|
2018-02-16 02:19:48 +00:00
|
|
|
map<string, SrsRequest*>::iterator it = streams.find(url);
|
|
|
|
if (it != streams.end()) {
|
2018-02-16 01:37:31 +00:00
|
|
|
srs_freep(it->second);
|
2018-02-16 02:19:48 +00:00
|
|
|
streams.erase(it);
|
2018-02-16 01:37:31 +00:00
|
|
|
}
|
2018-02-15 12:55:34 +00:00
|
|
|
}
|
|
|
|
|