2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-12-30 02:10:35 +00:00
|
|
|
* Copyright (c) 2013-2020 Winlin
|
2017-03-25 09:21:39 +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.
|
|
|
|
*/
|
2013-12-14 06:06:32 +00:00
|
|
|
|
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>
|
2015-06-14 00:43:38 +00:00
|
|
|
#include <srs_app_st.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-06-13 08:04:59 +00:00
|
|
|
#include <srs_rtmp_stack.hpp>
|
2015-09-22 01:11:07 +00:00
|
|
|
#include <srs_protocol_utility.hpp>
|
2015-05-23 01:58:00 +00:00
|
|
|
#include <srs_protocol_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-09-22 01:05:21 +00:00
|
|
|
#include <srs_protocol_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>
|
2015-09-24 10:33:07 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2015-10-14 07:34:18 +00:00
|
|
|
#include <srs_app_rtmp_conn.hpp>
|
2013-12-14 06:06:32 +00:00
|
|
|
|
2017-01-19 04:38:55 +00:00
|
|
|
SrsForwarder::SrsForwarder(SrsOriginHub* h)
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2017-01-19 04:38:55 +00:00
|
|
|
hub = h;
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2015-10-14 07:34:18 +00:00
|
|
|
req = NULL;
|
|
|
|
sh_video = sh_audio = NULL;
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2017-01-17 02:44:13 +00:00
|
|
|
sdk = NULL;
|
2017-06-04 11:13:56 +00:00
|
|
|
trd = new SrsDummyCoroutine();
|
2014-03-18 03:32:58 +00:00
|
|
|
queue = new SrsMessageQueue();
|
|
|
|
jitter = new SrsRtmpJitter();
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsForwarder::~SrsForwarder()
|
|
|
|
{
|
2015-10-14 07:34:18 +00:00
|
|
|
srs_freep(sdk);
|
2017-05-29 12:49:29 +00:00
|
|
|
srs_freep(trd);
|
2014-03-18 03:32:58 +00:00
|
|
|
srs_freep(queue);
|
|
|
|
srs_freep(jitter);
|
2014-08-24 14:34:38 +00:00
|
|
|
|
|
|
|
srs_freep(sh_video);
|
|
|
|
srs_freep(sh_audio);
|
2013-12-15 10:25:55 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsForwarder::initialize(SrsRequest* r, string ep)
|
2014-08-19 03:54:33 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-08-19 03:54:33 +00:00
|
|
|
|
|
|
|
// it's ok to use the request object,
|
|
|
|
// SrsSource already copy it and never delete it.
|
2015-10-14 07:34:18 +00:00
|
|
|
req = r;
|
2014-08-19 03:54:33 +00:00
|
|
|
|
|
|
|
// the ep(endpoint) to forward to
|
2015-10-14 07:34:18 +00:00
|
|
|
ep_forward = ep;
|
2014-08-19 03:54:33 +00:00
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2014-08-19 03:54:33 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:55:19 +00:00
|
|
|
void SrsForwarder::set_queue_size(srs_utime_t queue_size)
|
2013-12-15 10:25:55 +00:00
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
queue->set_queue_size(queue_size);
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsForwarder::on_publish()
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2017-05-29 12:49:29 +00:00
|
|
|
srs_freep(trd);
|
2017-06-04 11:13:56 +00:00
|
|
|
trd = new SrsSTCoroutine("forward", this);
|
2017-06-11 10:44:20 +00:00
|
|
|
if ((err = trd->start()) != srs_success) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_wrap(err, "start thread");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsForwarder::on_unpublish()
|
|
|
|
{
|
2017-05-29 12:49:29 +00:00
|
|
|
trd->stop();
|
2015-10-14 07:34:18 +00:00
|
|
|
sdk->close();
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsForwarder::on_meta_data(SrsSharedPtrMessage* shared_metadata)
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2017-09-22 13:50:54 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-03-21 03:55:28 +00:00
|
|
|
SrsSharedPtrMessage* metadata = shared_metadata->copy();
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2015-06-10 07:38:13 +00:00
|
|
|
// TODO: FIXME: config the jitter of Forwarder.
|
2017-09-22 13:50:54 +00:00
|
|
|
if ((err = jitter->correct(metadata, SrsRtmpJitterAlgorithmOFF)) != srs_success) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_wrap(err, "jitter");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = queue->enqueue(metadata)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "enqueue metadata");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsForwarder::on_audio(SrsSharedPtrMessage* shared_audio)
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2017-09-22 13:50:54 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2015-03-21 03:55:28 +00:00
|
|
|
SrsSharedPtrMessage* msg = shared_audio->copy();
|
2015-06-10 07:38:13 +00:00
|
|
|
|
|
|
|
// TODO: FIXME: config the jitter of Forwarder.
|
2017-09-22 13:50:54 +00:00
|
|
|
if ((err = jitter->correct(msg, SrsRtmpJitterAlgorithmOFF)) != srs_success) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_wrap(err, "jitter");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
if (SrsFlvAudio::sh(msg->payload, msg->size)) {
|
2014-08-24 14:34:38 +00:00
|
|
|
srs_freep(sh_audio);
|
|
|
|
sh_audio = msg->copy();
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = queue->enqueue(msg)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "enqueue audio");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsForwarder::on_video(SrsSharedPtrMessage* shared_video)
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2017-09-22 13:50:54 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-03-21 03:55:28 +00:00
|
|
|
SrsSharedPtrMessage* msg = shared_video->copy();
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2015-06-10 07:38:13 +00:00
|
|
|
// TODO: FIXME: config the jitter of Forwarder.
|
2017-09-22 13:50:54 +00:00
|
|
|
if ((err = jitter->correct(msg, SrsRtmpJitterAlgorithmOFF)) != srs_success) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_wrap(err, "jitter");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
if (SrsFlvVideo::sh(msg->payload, msg->size)) {
|
2014-08-24 14:34:38 +00:00
|
|
|
srs_freep(sh_video);
|
|
|
|
sh_video = msg->copy();
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = queue->enqueue(msg)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "enqueue video");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 12:49:29 +00:00
|
|
|
// when error, forwarder sleep for a while and retry.
|
2019-04-17 00:08:31 +00:00
|
|
|
#define SRS_FORWARDER_CIMS (3 * SRS_UTIME_SECONDS)
|
2017-05-29 12:49:29 +00:00
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t SrsForwarder::cycle()
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
while (true) {
|
2020-03-12 00:59:13 +00:00
|
|
|
// We always check status first.
|
|
|
|
// @see https://github.com/ossrs/srs/issues/1634#issuecomment-597571561
|
|
|
|
if ((err = trd->pull()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "forwarder");
|
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
if ((err = do_cycle()) != srs_success) {
|
|
|
|
srs_warn("Forwarder: Ignore error, %s", srs_error_desc(err).c_str());
|
|
|
|
srs_freep(err);
|
2017-05-29 12:49:29 +00:00
|
|
|
}
|
2020-03-12 00:59:13 +00:00
|
|
|
|
2019-04-07 08:25:52 +00:00
|
|
|
srs_usleep(SRS_FORWARDER_CIMS);
|
2017-05-29 12:49:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
return err;
|
2017-05-29 12:49:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t SrsForwarder::do_cycle()
|
2017-05-29 12:49:29 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-05-29 12:49:29 +00:00
|
|
|
|
2015-10-14 07:34:18 +00:00
|
|
|
std::string url;
|
|
|
|
if (true) {
|
|
|
|
std::string server;
|
|
|
|
int port = SRS_CONSTS_RTMP_DEFAULT_PORT;
|
|
|
|
|
|
|
|
// parse host:port from hostport.
|
|
|
|
srs_parse_hostport(ep_forward, server, port);
|
|
|
|
|
|
|
|
// generate url
|
2018-08-02 01:17:49 +00:00
|
|
|
url = srs_generate_rtmp_url(server, port, req->host, req->vhost, req->app, req->stream, req->param);
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 02:44:13 +00:00
|
|
|
srs_freep(sdk);
|
2019-04-22 00:03:12 +00:00
|
|
|
srs_utime_t cto = SRS_FORWARDER_CIMS;
|
|
|
|
srs_utime_t sto = SRS_CONSTS_RTMP_TIMEOUT;
|
2017-01-17 02:44:13 +00:00
|
|
|
sdk = new SrsSimpleRtmpClient(url, cto, sto);
|
|
|
|
|
2018-01-01 11:39:57 +00:00
|
|
|
if ((err = sdk->connect()) != srs_success) {
|
2019-04-22 00:03:12 +00:00
|
|
|
return srs_error_wrap(err, "sdk connect url=%s, cto=%dms, sto=%dms.", url.c_str(), srsu2msi(cto), srsu2msi(sto));
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 01:17:49 +00:00
|
|
|
if ((err = sdk->publish(_srs_config->get_chunk_size(req->vhost))) != srs_success) {
|
2018-01-01 11:39:57 +00:00
|
|
|
return srs_error_wrap(err, "sdk publish");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = hub->on_forwarder_start(this)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "notify hub start");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = forward()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "forward");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
return err;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-22 12:01:25 +00:00
|
|
|
#define SYS_MAX_FORWARD_SEND_MSGS 128
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsForwarder::forward()
|
2013-12-14 06:06:32 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2019-04-17 00:31:53 +00:00
|
|
|
sdk->set_recv_timeout(SRS_CONSTS_RTMP_PULSE);
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2015-02-19 10:56:21 +00:00
|
|
|
SrsPithyPrint* pprint = SrsPithyPrint::create_forwarder();
|
|
|
|
SrsAutoFree(SrsPithyPrint, pprint);
|
2017-03-25 09:21:39 +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) {
|
2018-01-01 11:39:57 +00:00
|
|
|
if ((err = sdk->send_and_free_message(sh_video->copy())) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "send video sh");
|
2014-08-24 14:34:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sh_audio) {
|
2018-01-01 11:39:57 +00:00
|
|
|
if ((err = sdk->send_and_free_message(sh_audio->copy())) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "send audio sh");
|
2014-08-24 14:34:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 04:45:17 +00:00
|
|
|
while (true) {
|
|
|
|
if ((err = trd->pull()) != srs_success) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_wrap(err, "thread quit");
|
2017-07-29 04:45:17 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 10:56:21 +00:00
|
|
|
pprint->elapse();
|
2017-03-25 09:21:39 +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;
|
2018-01-01 11:39:57 +00:00
|
|
|
err = sdk->recv_message(&msg);
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2018-01-01 11:39:57 +00:00
|
|
|
if (err != srs_success && srs_error_code(err) != ERROR_SOCKET_TIMEOUT) {
|
|
|
|
return srs_error_wrap(err, "receive control message");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
2018-01-01 11:39:57 +00:00
|
|
|
srs_error_reset(err);
|
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;
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = queue->dump_packets(msgs.max, msgs.msgs, count)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "dump packets");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 13:41:18 +00:00
|
|
|
// pithy print
|
2015-02-19 10:56:21 +00:00
|
|
|
if (pprint->can_print()) {
|
2015-10-14 07:34:18 +00:00
|
|
|
sdk->kbps_sample(SRS_CONSTS_LOG_FOWARDER, pprint->age(), count);
|
2014-04-26 13:41:18 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
// ignore when no messages.
|
|
|
|
if (count <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-11-19 02:44:50 +00:00
|
|
|
// sendout messages, all messages are freed by send_and_free_messages().
|
2018-01-01 11:39:57 +00:00
|
|
|
if ((err = sdk->send_and_free_messages(msgs.msgs, count)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "send messages");
|
2014-03-18 03:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2013-12-14 06:06:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 14:18:39 +00:00
|
|
|
|