2013-12-14 06:06:32 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2014-12-31 12:32:09 +00:00
|
|
|
Copyright (c) 2013-2015 winlin
|
2013-12-14 06:06:32 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_forward.hpp>
|
2013-12-14 06:06:32 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2014-08-19 03:54:33 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_source.hpp>
|
2014-07-26 12:07:12 +00:00
|
|
|
#include <srs_app_st_socket.hpp>
|
2014-03-01 02:30:16 +00:00
|
|
|
#include <srs_kernel_error.hpp>
|
2014-03-01 02:42:55 +00:00
|
|
|
#include <srs_kernel_log.hpp>
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_config.hpp>
|
|
|
|
#include <srs_app_pithy_print.hpp>
|
2015-01-23 02:07:20 +00:00
|
|
|
#include <srs_rtmp_sdk.hpp>
|
|
|
|
#include <srs_rtmp_utility.hpp>
|
2014-05-12 09:27:50 +00:00
|
|
|
#include <srs_app_kbps.hpp>
|
2015-01-23 02:07:20 +00:00
|
|
|
#include <srs_rtmp_msg_array.hpp>
|
2014-06-29 06:39:56 +00:00
|
|
|
#include <srs_app_utility.hpp>
|
2015-01-23 02:07:20 +00:00
|
|
|
#include <srs_rtmp_amf0.hpp>
|
2014-08-24 14:34:38 +00:00
|
|
|
#include <srs_kernel_codec.hpp>
|
2015-02-19 10:56:21 +00:00
|
|
|
#include <srs_core_autofree.hpp>
|
2013-12-14 06:06:32 +00:00
|
|
|
|
2014-04-07 01:07:12 +00:00
|
|
|
// when error, forwarder sleep for a while and retry.
|
|
|
|
#define SRS_FORWARDER_SLEEP_US (int64_t)(3*1000*1000LL)
|
|
|
|
|
2013-12-15 09:09:25 +00:00
|
|
|
SrsForwarder::SrsForwarder(SrsSource* _source)
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
source = _source;
|
|
|
|
|
2014-08-19 03:54:33 +00:00
|
|
|
_req = NULL;
|
2014-03-18 03:32:58 +00:00
|
|
|
io = NULL;
|
|
|
|
client = NULL;
|
|
|
|
stfd = NULL;
|
2014-05-12 09:27:50 +00:00
|
|
|
kbps = new SrsKbps();
|
2014-03-18 03:32:58 +00:00
|
|
|
stream_id = 0;
|
2013-12-14 06:06:32 +00:00
|
|
|
|
2014-12-03 06:05:15 +00:00
|
|
|
pthread = new SrsThread("forward", this, SRS_FORWARDER_SLEEP_US, true);
|
2014-03-18 03:32:58 +00:00
|
|
|
queue = new SrsMessageQueue();
|
|
|
|
jitter = new SrsRtmpJitter();
|
2014-08-24 14:34:38 +00:00
|
|
|
|
|
|
|
sh_video = sh_audio = NULL;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsForwarder::~SrsForwarder()
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
on_unpublish();
|
|
|
|
|
|
|
|
srs_freep(pthread);
|
|
|
|
srs_freep(queue);
|
|
|
|
srs_freep(jitter);
|
2014-05-12 09:27:50 +00:00
|
|
|
srs_freep(kbps);
|
2014-08-24 14:34:38 +00:00
|
|
|
|
|
|
|
srs_freep(sh_video);
|
|
|
|
srs_freep(sh_audio);
|
2013-12-15 10:25:55 +00:00
|
|
|
}
|
|
|
|
|
2014-08-19 03:54:33 +00:00
|
|
|
int SrsForwarder::initialize(SrsRequest* req, string ep_forward)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
// it's ok to use the request object,
|
|
|
|
// SrsSource already copy it and never delete it.
|
|
|
|
_req = req;
|
|
|
|
|
|
|
|
// the ep(endpoint) to forward to
|
|
|
|
_ep_forward = ep_forward;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-15 10:25:55 +00:00
|
|
|
void SrsForwarder::set_queue_size(double queue_size)
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
queue->set_queue_size(queue_size);
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-19 03:54:33 +00:00
|
|
|
int SrsForwarder::on_publish()
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-08-19 03:54:33 +00:00
|
|
|
SrsRequest* req = _req;
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2014-08-19 03:54:33 +00:00
|
|
|
// discovery the server port and tcUrl from req and ep_forward.
|
|
|
|
std::string server, port, tc_url;
|
|
|
|
discovery_ep(server, port, tc_url);
|
2014-03-18 03:32:58 +00:00
|
|
|
|
|
|
|
// dead loop check
|
2014-03-26 04:34:35 +00:00
|
|
|
std::string source_ep = "rtmp://";
|
|
|
|
source_ep += req->host;
|
2014-03-18 03:32:58 +00:00
|
|
|
source_ep += ":";
|
|
|
|
source_ep += req->port;
|
2014-03-26 04:34:35 +00:00
|
|
|
source_ep += "?vhost=";
|
|
|
|
source_ep += req->vhost;
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2014-03-26 04:34:35 +00:00
|
|
|
std::string dest_ep = "rtmp://";
|
2014-08-19 03:54:33 +00:00
|
|
|
if (_ep_forward == SRS_CONSTS_LOCALHOST) {
|
2014-03-26 04:34:35 +00:00
|
|
|
dest_ep += req->host;
|
|
|
|
} else {
|
2014-12-29 01:05:56 +00:00
|
|
|
dest_ep += server;
|
2014-03-26 04:34:35 +00:00
|
|
|
}
|
2014-03-18 03:32:58 +00:00
|
|
|
dest_ep += ":";
|
2014-08-19 03:54:33 +00:00
|
|
|
dest_ep += port;
|
2014-03-26 04:34:35 +00:00
|
|
|
dest_ep += "?vhost=";
|
2014-08-19 03:54:33 +00:00
|
|
|
dest_ep += req->vhost;
|
2014-03-18 03:32:58 +00:00
|
|
|
|
|
|
|
if (source_ep == dest_ep) {
|
|
|
|
ret = ERROR_SYSTEM_FORWARD_LOOP;
|
2014-03-26 04:34:35 +00:00
|
|
|
srs_warn("forward loop detected. src=%s, dest=%s, ret=%d",
|
2014-03-18 03:32:58 +00:00
|
|
|
source_ep.c_str(), dest_ep.c_str(), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-05-17 06:53:04 +00:00
|
|
|
srs_trace("start forward %s to %s, tcUrl=%s, stream=%s",
|
2014-03-18 03:32:58 +00:00
|
|
|
source_ep.c_str(), dest_ep.c_str(), tc_url.c_str(),
|
2014-08-19 03:54:33 +00:00
|
|
|
req->stream.c_str());
|
2014-03-18 03:32:58 +00:00
|
|
|
|
|
|
|
if ((ret = pthread->start()) != ERROR_SUCCESS) {
|
2013-12-14 06:06:32 +00:00
|
|
|
srs_error("start srs thread failed. ret=%d", ret);
|
|
|
|
return ret;
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
2014-04-16 08:58:54 +00:00
|
|
|
srs_trace("forward thread cid=%d, current_cid=%d", pthread->cid(), _srs_context->get_id());
|
2014-03-18 03:32:58 +00:00
|
|
|
|
|
|
|
return ret;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsForwarder::on_unpublish()
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
pthread->stop();
|
|
|
|
|
|
|
|
close_underlayer_socket();
|
|
|
|
|
|
|
|
srs_freep(client);
|
|
|
|
srs_freep(io);
|
2014-05-12 09:27:50 +00:00
|
|
|
kbps->set_io(NULL, NULL);
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 03:55:28 +00:00
|
|
|
int SrsForwarder::on_meta_data(SrsSharedPtrMessage* shared_metadata)
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
int ret = ERROR_SUCCESS;
|
2014-12-05 15:49:53 +00:00
|
|
|
|
2015-03-21 03:55:28 +00:00
|
|
|
SrsSharedPtrMessage* metadata = shared_metadata->copy();
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2014-06-25 09:14:11 +00:00
|
|
|
if ((ret = jitter->correct(metadata, 0, 0, SrsRtmpJitterAlgorithmFULL)) != ERROR_SUCCESS) {
|
2014-03-18 03:32:58 +00:00
|
|
|
srs_freep(metadata);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = queue->enqueue(metadata)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 03:55:28 +00:00
|
|
|
int SrsForwarder::on_audio(SrsSharedPtrMessage* shared_audio)
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2015-03-21 03:55:28 +00:00
|
|
|
SrsSharedPtrMessage* msg = shared_audio->copy();
|
2014-12-05 15:49:53 +00:00
|
|
|
|
2014-06-25 09:14:11 +00:00
|
|
|
if ((ret = jitter->correct(msg, 0, 0, SrsRtmpJitterAlgorithmFULL)) != ERROR_SUCCESS) {
|
2014-03-18 03:32:58 +00:00
|
|
|
srs_freep(msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-24 14:34:38 +00:00
|
|
|
if (SrsFlvCodec::audio_is_sequence_header(msg->payload, msg->size)) {
|
|
|
|
srs_freep(sh_audio);
|
|
|
|
sh_audio = msg->copy();
|
|
|
|
}
|
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
if ((ret = queue->enqueue(msg)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 03:55:28 +00:00
|
|
|
int SrsForwarder::on_video(SrsSharedPtrMessage* shared_video)
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
int ret = ERROR_SUCCESS;
|
2014-12-05 15:49:53 +00:00
|
|
|
|
2015-03-21 03:55:28 +00:00
|
|
|
SrsSharedPtrMessage* msg = shared_video->copy();
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2014-06-25 09:14:11 +00:00
|
|
|
if ((ret = jitter->correct(msg, 0, 0, SrsRtmpJitterAlgorithmFULL)) != ERROR_SUCCESS) {
|
2014-03-18 03:32:58 +00:00
|
|
|
srs_freep(msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-24 14:34:38 +00:00
|
|
|
if (SrsFlvCodec::video_is_sequence_header(msg->payload, msg->size)) {
|
|
|
|
srs_freep(sh_video);
|
|
|
|
sh_video = msg->copy();
|
|
|
|
}
|
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
if ((ret = queue->enqueue(msg)) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SrsForwarder::cycle()
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-08-19 03:54:33 +00:00
|
|
|
std::string ep_server, ep_port;
|
|
|
|
if ((ret = connect_server(ep_server, ep_port)) != ERROR_SUCCESS) {
|
2014-03-18 03:32:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
srs_assert(client);
|
2013-12-14 06:06:32 +00:00
|
|
|
|
2014-07-20 05:25:25 +00:00
|
|
|
client->set_recv_timeout(SRS_CONSTS_RTMP_RECV_TIMEOUT_US);
|
|
|
|
client->set_send_timeout(SRS_CONSTS_RTMP_SEND_TIMEOUT_US);
|
2014-03-18 03:32:58 +00:00
|
|
|
|
|
|
|
if ((ret = client->handshake()) != ERROR_SUCCESS) {
|
|
|
|
srs_error("handshake with server failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-08-19 03:54:33 +00:00
|
|
|
if ((ret = connect_app(ep_server, ep_port)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("connect with server failed. ret=%d", ret);
|
2014-03-18 03:32:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if ((ret = client->create_stream(stream_id)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("connect with server failed, stream_id=%d. ret=%d", stream_id, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-19 03:54:33 +00:00
|
|
|
if ((ret = client->publish(_req->stream, stream_id)) != ERROR_SUCCESS) {
|
2014-03-18 03:32:58 +00:00
|
|
|
srs_error("connect with server failed, stream_name=%s, stream_id=%d. ret=%d",
|
2014-08-19 03:54:33 +00:00
|
|
|
_req->stream.c_str(), stream_id, ret);
|
2014-03-18 03:32:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = source->on_forwarder_start(this)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("callback the source to feed the sequence header failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = forward()) != ERROR_SUCCESS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsForwarder::close_underlayer_socket()
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
srs_close_stfd(stfd);
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-19 03:54:33 +00:00
|
|
|
void SrsForwarder::discovery_ep(string& server, string& port, string& tc_url)
|
|
|
|
{
|
|
|
|
SrsRequest* req = _req;
|
|
|
|
|
|
|
|
server = _ep_forward;
|
|
|
|
port = SRS_CONSTS_RTMP_DEFAULT_PORT;
|
|
|
|
|
|
|
|
// TODO: FIXME: parse complex params
|
|
|
|
size_t pos = _ep_forward.find(":");
|
|
|
|
if (pos != std::string::npos) {
|
|
|
|
port = _ep_forward.substr(pos + 1);
|
|
|
|
server = _ep_forward.substr(0, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate tcUrl
|
|
|
|
tc_url = srs_generate_tc_url(server, req->vhost, req->app, port, req->param);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SrsForwarder::connect_server(string& ep_server, string& ep_port)
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
// reopen
|
|
|
|
close_underlayer_socket();
|
|
|
|
|
2014-08-19 03:54:33 +00:00
|
|
|
// discovery the server port and tcUrl from req and ep_forward.
|
|
|
|
std::string server, s_port, tc_url;
|
|
|
|
discovery_ep(server, s_port, tc_url);
|
|
|
|
int port = ::atoi(s_port.c_str());
|
|
|
|
|
|
|
|
// output the connected server and port.
|
|
|
|
ep_server = server;
|
|
|
|
ep_port = s_port;
|
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
// open socket.
|
2014-06-29 06:39:56 +00:00
|
|
|
int64_t timeout = SRS_FORWARDER_SLEEP_US;
|
2014-08-19 03:54:33 +00:00
|
|
|
if ((ret = srs_socket_connect(ep_server, port, timeout, &stfd)) != ERROR_SUCCESS) {
|
2014-06-29 06:39:56 +00:00
|
|
|
srs_warn("forward failed, stream=%s, tcUrl=%s to server=%s, port=%d, timeout=%"PRId64", ret=%d",
|
2014-08-19 03:54:33 +00:00
|
|
|
_req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port, timeout, ret);
|
2013-12-14 06:06:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-03-01 04:43:04 +00:00
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
srs_freep(client);
|
|
|
|
srs_freep(io);
|
|
|
|
|
2014-06-29 06:39:56 +00:00
|
|
|
srs_assert(stfd);
|
2014-07-26 12:08:37 +00:00
|
|
|
io = new SrsStSocket(stfd);
|
2014-03-18 03:32:58 +00:00
|
|
|
client = new SrsRtmpClient(io);
|
|
|
|
|
2014-06-29 06:39:56 +00:00
|
|
|
kbps->set_io(io, io);
|
2013-12-14 06:06:32 +00:00
|
|
|
|
2014-06-29 06:39:56 +00:00
|
|
|
srs_trace("forward connected, stream=%s, tcUrl=%s to server=%s, port=%d",
|
2014-08-19 03:54:33 +00:00
|
|
|
_req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: FIXME: refine the connect_app.
|
|
|
|
int SrsForwarder::connect_app(string ep_server, string ep_port)
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
SrsRequest* req = _req;
|
|
|
|
|
|
|
|
// args of request takes the srs info.
|
|
|
|
if (req->args == NULL) {
|
|
|
|
req->args = SrsAmf0Any::object();
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify server the edge identity,
|
|
|
|
// @see https://github.com/winlinvip/simple-rtmp-server/issues/147
|
|
|
|
SrsAmf0Object* data = req->args;
|
|
|
|
data->set("srs_sig", SrsAmf0Any::str(RTMP_SIG_SRS_KEY));
|
2015-02-11 00:48:22 +00:00
|
|
|
data->set("srs_server", SrsAmf0Any::str(RTMP_SIG_SRS_SERVER));
|
2014-08-19 03:54:33 +00:00
|
|
|
data->set("srs_license", SrsAmf0Any::str(RTMP_SIG_SRS_LICENSE));
|
|
|
|
data->set("srs_role", SrsAmf0Any::str(RTMP_SIG_SRS_ROLE));
|
|
|
|
data->set("srs_url", SrsAmf0Any::str(RTMP_SIG_SRS_URL));
|
|
|
|
data->set("srs_version", SrsAmf0Any::str(RTMP_SIG_SRS_VERSION));
|
|
|
|
data->set("srs_site", SrsAmf0Any::str(RTMP_SIG_SRS_WEB));
|
|
|
|
data->set("srs_email", SrsAmf0Any::str(RTMP_SIG_SRS_EMAIL));
|
|
|
|
data->set("srs_copyright", SrsAmf0Any::str(RTMP_SIG_SRS_COPYRIGHT));
|
2014-11-25 02:41:55 +00:00
|
|
|
data->set("srs_primary", SrsAmf0Any::str(RTMP_SIG_SRS_PRIMARY));
|
|
|
|
data->set("srs_authors", SrsAmf0Any::str(RTMP_SIG_SRS_AUTHROS));
|
2014-08-19 03:54:33 +00:00
|
|
|
// for edge to directly get the id of client.
|
|
|
|
data->set("srs_pid", SrsAmf0Any::number(getpid()));
|
|
|
|
data->set("srs_id", SrsAmf0Any::number(_srs_context->get_id()));
|
|
|
|
|
|
|
|
// local ip of edge
|
|
|
|
std::vector<std::string> ips = srs_get_local_ipv4_ips();
|
|
|
|
assert(_srs_config->get_stats_network() < (int)ips.size());
|
|
|
|
std::string local_ip = ips[_srs_config->get_stats_network()];
|
|
|
|
data->set("srs_server_ip", SrsAmf0Any::str(local_ip.c_str()));
|
|
|
|
|
|
|
|
// generate the tcUrl
|
|
|
|
std::string param = "";
|
|
|
|
std::string tc_url = srs_generate_tc_url(ep_server, req->vhost, req->app, ep_port, param);
|
|
|
|
|
|
|
|
// upnode server identity will show in the connect_app of client.
|
|
|
|
// @see https://github.com/winlinvip/simple-rtmp-server/issues/160
|
|
|
|
// the debug_srs_upnode is config in vhost and default to true.
|
|
|
|
bool debug_srs_upnode = _srs_config->get_debug_srs_upnode(req->vhost);
|
|
|
|
if ((ret = client->connect_app(req->app, tc_url, req, debug_srs_upnode)) != ERROR_SUCCESS) {
|
2014-08-19 08:32:52 +00:00
|
|
|
srs_error("connect with server failed, tcUrl=%s, dsu=%d. ret=%d",
|
|
|
|
tc_url.c_str(), debug_srs_upnode, ret);
|
2014-08-19 03:54:33 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2013-12-14 06:06:32 +00:00
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
return ret;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-22 12:01:25 +00:00
|
|
|
#define SYS_MAX_FORWARD_SEND_MSGS 128
|
2013-12-14 06:06:32 +00:00
|
|
|
int SrsForwarder::forward()
|
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
2014-07-20 05:25:25 +00:00
|
|
|
client->set_recv_timeout(SRS_CONSTS_RTMP_PULSE_TIMEOUT_US);
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2015-02-19 10:56:21 +00:00
|
|
|
SrsPithyPrint* pprint = SrsPithyPrint::create_forwarder();
|
|
|
|
SrsAutoFree(SrsPithyPrint, pprint);
|
2013-12-14 06:06:32 +00:00
|
|
|
|
2014-11-13 08:56:41 +00:00
|
|
|
SrsMessageArray msgs(SYS_MAX_FORWARD_SEND_MSGS);
|
2014-06-22 12:01:25 +00:00
|
|
|
|
2014-08-24 14:34:38 +00:00
|
|
|
// update sequence header
|
|
|
|
// TODO: FIXME: maybe need to zero the sequence header timestamp.
|
|
|
|
if (sh_video) {
|
|
|
|
if ((ret = client->send_and_free_message(sh_video->copy(), stream_id)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("forwarder send sh_video to server failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sh_audio) {
|
|
|
|
if ((ret = client->send_and_free_message(sh_audio->copy(), stream_id)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("forwarder send sh_audio to server failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
while (pthread->can_loop()) {
|
2015-02-19 10:56:21 +00:00
|
|
|
pprint->elapse();
|
2014-04-26 13:41:18 +00:00
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
// read from client.
|
|
|
|
if (true) {
|
2014-12-05 15:03:52 +00:00
|
|
|
SrsCommonMessage* msg = NULL;
|
2014-04-29 06:44:07 +00:00
|
|
|
ret = client->recv_message(&msg);
|
2014-03-18 03:32:58 +00:00
|
|
|
|
|
|
|
srs_verbose("play loop recv message. ret=%d", ret);
|
|
|
|
if (ret != ERROR_SUCCESS && ret != ERROR_SOCKET_TIMEOUT) {
|
|
|
|
srs_error("recv server control message failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-28 09:20:35 +00:00
|
|
|
|
|
|
|
srs_freep(msg);
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// forward all messages.
|
2014-11-19 02:44:50 +00:00
|
|
|
// each msg in msgs.msgs must be free, for the SrsMessageArray never free them.
|
2014-03-18 03:32:58 +00:00
|
|
|
int count = 0;
|
2014-11-14 03:24:49 +00:00
|
|
|
if ((ret = queue->dump_packets(msgs.max, msgs.msgs, count)) != ERROR_SUCCESS) {
|
2014-03-18 03:32:58 +00:00
|
|
|
srs_error("get message to forward failed. ret=%d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-26 13:41:18 +00:00
|
|
|
// pithy print
|
2015-02-19 10:56:21 +00:00
|
|
|
if (pprint->can_print()) {
|
2014-05-14 05:56:12 +00:00
|
|
|
kbps->sample();
|
2014-07-20 05:23:45 +00:00
|
|
|
srs_trace("-> "SRS_CONSTS_LOG_FOWARDER
|
2014-05-14 05:56:12 +00:00
|
|
|
" time=%"PRId64", msgs=%d, okbps=%d,%d,%d, ikbps=%d,%d,%d",
|
2015-02-19 10:56:21 +00:00
|
|
|
pprint->age(), count,
|
2014-06-19 07:28:05 +00:00
|
|
|
kbps->get_send_kbps(), kbps->get_send_kbps_30s(), kbps->get_send_kbps_5m(),
|
|
|
|
kbps->get_recv_kbps(), kbps->get_recv_kbps_30s(), kbps->get_recv_kbps_5m());
|
2014-04-26 13:41:18 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
// ignore when no messages.
|
|
|
|
if (count <= 0) {
|
|
|
|
srs_verbose("no packets to forward.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-19 02:44:50 +00:00
|
|
|
// sendout messages, all messages are freed by send_and_free_messages().
|
2014-11-14 03:24:49 +00:00
|
|
|
if ((ret = client->send_and_free_messages(msgs.msgs, count, stream_id)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("forwarder messages to server failed. ret=%d", ret);
|
|
|
|
return ret;
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 14:18:39 +00:00
|
|
|
|