1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_coworkers.cpp

154 lines
4.5 KiB
C++
Raw Normal View History

//
// Copyright (c) 2013-2021 The SRS Authors
//
// SPDX-License-Identifier: MIT or MulanPSL-2.0
//
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>
#include <srs_protocol_rtmp_stack.hpp>
#include <srs_app_config.hpp>
#include <srs_protocol_utility.hpp>
2022-06-09 11:59:51 +00:00
#include <srs_protocol_utility.hpp>
#include <srs_kernel_utility.hpp>
2018-02-15 12:55:34 +00:00
SrsCoWorkers* SrsCoWorkers::_instance = NULL;
SrsCoWorkers::SrsCoWorkers()
{
}
SrsCoWorkers::~SrsCoWorkers()
{
map<string, SrsRequest*>::iterator it;
2018-02-16 02:19:48 +00:00
for (it = streams.begin(); it != streams.end(); ++it) {
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
{
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();
}
// 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());
}
}
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.
// 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
}
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;
}
if (service_ip.empty()) {
service_ip = srs_get_public_internet_address();
}
// The backend API endpoint.
string backend = _srs_config->get_http_api_listen();
if (backend.find(":") == string::npos) {
backend = service_ip + ":" + backend;
}
// 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());
return SrsJsonAny::object()
->set("ip", SrsJsonAny::str(service_ip.c_str()))
->set("port", SrsJsonAny::integer(listen_port))
->set("vhost", SrsJsonAny::str(r->vhost.c_str()))
->set("api", SrsJsonAny::str(backend.c_str()))
->set("routers", routers);
}
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()) {
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;
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()) {
srs_freep(it->second);
}
// Always use the latest one.
2018-02-16 02:19:48 +00:00
streams[url] = r->copy();
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
{
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()) {
srs_freep(it->second);
2018-02-16 02:19:48 +00:00
streams.erase(it);
}
2018-02-15 12:55:34 +00:00
}