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_forward.cpp

313 lines
8.6 KiB
C++
Raw Normal View History

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.
*/
#include <srs_app_forward.hpp>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
#include <srs_app_source.hpp>
2015-06-14 00:43:38 +00:00
#include <srs_app_st.hpp>
#include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>
#include <srs_app_config.hpp>
#include <srs_app_pithy_print.hpp>
2015-06-13 08:04:59 +00:00
#include <srs_rtmp_stack.hpp>
#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>
#include <srs_app_utility.hpp>
2015-09-22 01:05:21 +00:00
#include <srs_protocol_amf0.hpp>
#include <srs_kernel_codec.hpp>
#include <srs_core_autofree.hpp>
2015-09-24 10:33:07 +00:00
#include <srs_kernel_utility.hpp>
#include <srs_app_rtmp_conn.hpp>
SrsForwarder::SrsForwarder(SrsOriginHub* h)
{
hub = h;
2014-03-18 03:32:58 +00:00
req = NULL;
sh_video = sh_audio = NULL;
2017-03-25 09:21:39 +00:00
sdk = NULL;
trd = new SrsDummyCoroutine();
2014-03-18 03:32:58 +00:00
queue = new SrsMessageQueue();
jitter = new SrsRtmpJitter();
}
SrsForwarder::~SrsForwarder()
{
srs_freep(sdk);
srs_freep(trd);
2014-03-18 03:32:58 +00:00
srs_freep(queue);
srs_freep(jitter);
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)
{
2017-09-23 14:12:33 +00:00
srs_error_t err = srs_success;
// it's ok to use the request object,
// SrsSource already copy it and never delete it.
req = r;
// the ep(endpoint) to forward to
ep_forward = ep;
2017-09-23 14:12:33 +00:00
return err;
}
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);
}
2017-09-23 14:12:33 +00:00
srs_error_t SrsForwarder::on_publish()
{
srs_error_t err = srs_success;
2014-03-18 03:32:58 +00:00
srs_freep(trd);
trd = new SrsSTCoroutine("forward", this);
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;
}
void SrsForwarder::on_unpublish()
{
trd->stop();
sdk->close();
}
2017-09-23 14:12:33 +00:00
srs_error_t SrsForwarder::on_meta_data(SrsSharedPtrMessage* shared_metadata)
{
srs_error_t err = srs_success;
2017-03-25 09:21:39 +00:00
SrsSharedPtrMessage* metadata = shared_metadata->copy();
2014-03-18 03:32:58 +00:00
// TODO: FIXME: config the jitter of Forwarder.
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;
}
2017-09-23 14:12:33 +00:00
srs_error_t SrsForwarder::on_audio(SrsSharedPtrMessage* shared_audio)
{
srs_error_t err = srs_success;
2014-03-18 03:32:58 +00:00
SrsSharedPtrMessage* msg = shared_audio->copy();
// TODO: FIXME: config the jitter of Forwarder.
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)) {
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;
}
2017-09-23 14:12:33 +00:00
srs_error_t SrsForwarder::on_video(SrsSharedPtrMessage* shared_video)
{
srs_error_t err = srs_success;
2017-03-25 09:21:39 +00:00
SrsSharedPtrMessage* msg = shared_video->copy();
2014-03-18 03:32:58 +00:00
// TODO: FIXME: config the jitter of Forwarder.
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)) {
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;
}
// 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)
srs_error_t SrsForwarder::cycle()
{
srs_error_t err = srs_success;
2014-03-18 03:32:58 +00:00
while (true) {
// 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");
}
if ((err = do_cycle()) != srs_success) {
srs_warn("Forwarder: Ignore error, %s", srs_error_desc(err).c_str());
srs_freep(err);
}
2019-04-07 08:25:52 +00:00
srs_usleep(SRS_FORWARDER_CIMS);
}
return err;
}
srs_error_t SrsForwarder::do_cycle()
{
srs_error_t err = srs_success;
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
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
}
srs_freep(sdk);
srs_utime_t cto = SRS_FORWARDER_CIMS;
srs_utime_t sto = SRS_CONSTS_RTMP_TIMEOUT;
sdk = new SrsSimpleRtmpClient(url, cto, sto);
2018-01-01 11:39:57 +00:00
if ((err = sdk->connect()) != srs_success) {
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
}
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
}
return err;
}
#define SYS_MAX_FORWARD_SEND_MSGS 128
2017-09-23 14:12:33 +00:00
srs_error_t SrsForwarder::forward()
{
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
SrsPithyPrint* pprint = SrsPithyPrint::create_forwarder();
SrsAutoFree(SrsPithyPrint, pprint);
2017-03-25 09:21:39 +00:00
SrsMessageArray msgs(SYS_MAX_FORWARD_SEND_MSGS);
// 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");
}
}
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");
}
}
while (true) {
if ((err = trd->pull()) != srs_success) {
2017-09-23 14:12:33 +00:00
return srs_error_wrap(err, "thread quit");
}
pprint->elapse();
2017-03-25 09:21:39 +00:00
2014-03-18 03:32:58 +00:00
// read from client.
if (true) {
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.
// 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
}
// pithy print
if (pprint->can_print()) {
sdk->kbps_sample(SRS_CONSTS_LOG_FOWARDER, pprint->age(), count);
}
2014-03-18 03:32:58 +00:00
// ignore when no messages.
if (count <= 0) {
continue;
}
2017-03-25 09:21:39 +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;
}
2014-08-02 14:18:39 +00:00