1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-24 06:54:22 +00:00
srs/trunk/src/app/srs_app_edge.cpp

746 lines
20 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2017-03-25 13:29:29 +00:00
* Copyright (c) 2013-2017 OSSRS(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.
*/
2014-04-25 08:35:03 +00:00
#include <srs_app_edge.hpp>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
#include <srs_kernel_error.hpp>
2015-06-13 08:04:59 +00:00
#include <srs_rtmp_stack.hpp>
2015-09-22 01:07:07 +00:00
#include <srs_protocol_io.hpp>
#include <srs_app_config.hpp>
#include <srs_protocol_utility.hpp>
2015-06-14 00:43:38 +00:00
#include <srs_app_st.hpp>
#include <srs_app_source.hpp>
#include <srs_app_pithy_print.hpp>
#include <srs_core_autofree.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_utility.hpp>
#include <srs_kernel_balance.hpp>
2015-10-14 06:37:24 +00:00
#include <srs_app_rtmp_conn.hpp>
2014-04-26 10:08:21 +00:00
// when error, edge ingester sleep for a while and retry.
#define SRS_EDGE_INGESTER_CIMS (3*1000)
// when edge timeout, retry next.
#define SRS_EDGE_INGESTER_TMMS (5*1000)
2014-04-26 10:08:21 +00:00
// when error, edge ingester sleep for a while and retry.
#define SRS_EDGE_FORWARDER_CIMS (3*1000)
2014-04-28 09:20:35 +00:00
// when edge error, wait for quit
#define SRS_EDGE_FORWARDER_TMMS (150)
2014-04-28 09:20:35 +00:00
SrsEdgeUpstream::SrsEdgeUpstream()
{
}
SrsEdgeUpstream::~SrsEdgeUpstream()
{
}
2016-01-11 07:46:23 +00:00
SrsEdgeRtmpUpstream::SrsEdgeRtmpUpstream(string r)
{
2016-01-11 07:46:23 +00:00
redirect = r;
sdk = NULL;
}
SrsEdgeRtmpUpstream::~SrsEdgeRtmpUpstream()
{
close();
}
int SrsEdgeRtmpUpstream::connect(SrsRequest* r, SrsLbRoundRobin* lb)
{
int ret = ERROR_SUCCESS;
SrsRequest* req = r;
std::string url;
if (true) {
SrsConfDirective* conf = _srs_config->get_vhost_edge_origin(req->vhost);
// @see https://github.com/ossrs/srs/issues/79
// when origin is error, for instance, server is shutdown,
// then user remove the vhost then reload, the conf is empty.
if (!conf) {
ret = ERROR_EDGE_VHOST_REMOVED;
srs_warn("vhost %s removed. ret=%d", req->vhost.c_str(), ret);
return ret;
}
// select the origin.
std::string server = lb->select(conf->args);
int port = SRS_CONSTS_RTMP_DEFAULT_PORT;
srs_parse_hostport(server, server, port);
2016-01-11 07:46:23 +00:00
// override the origin info by redirect.
if (!redirect.empty()) {
int _port;
string _schema, _vhost, _app, _param, _host;
srs_discovery_tc_url(redirect, _schema, _host, _vhost, _app, _port, _param);
srs_warn("RTMP redirect %s:%d to %s:%d", server.c_str(), port, _host.c_str(), _port);
server = _host;
port = _port;
}
// support vhost tranform for edge,
// @see https://github.com/ossrs/srs/issues/372
std::string vhost = _srs_config->get_vhost_edge_transform_vhost(req->vhost);
vhost = srs_string_replace(vhost, "[vhost]", req->vhost);
url = srs_generate_rtmp_url(server, port, vhost, req->app, req->stream);
}
srs_freep(sdk);
int64_t cto = SRS_EDGE_INGESTER_TMMS;
int64_t sto = SRS_CONSTS_RTMP_PULSE_TMMS;
sdk = new SrsSimpleRtmpClient(url, cto, sto);
if ((ret = sdk->connect()) != ERROR_SUCCESS) {
srs_error("edge pull %s failed, cto=%" PRId64 ", sto=%" PRId64 ". ret=%d", url.c_str(), cto, sto, ret);
return ret;
}
if ((ret = sdk->play()) != ERROR_SUCCESS) {
srs_error("edge pull %s stream failed. ret=%d", url.c_str(), ret);
return ret;
}
return ret;
}
int SrsEdgeRtmpUpstream::recv_message(SrsCommonMessage** pmsg)
{
return sdk->recv_message(pmsg);
}
int SrsEdgeRtmpUpstream::decode_message(SrsCommonMessage* msg, SrsPacket** ppacket)
{
return sdk->decode_message(msg, ppacket);
}
void SrsEdgeRtmpUpstream::close()
{
srs_freep(sdk);
}
void SrsEdgeRtmpUpstream::set_recv_timeout(int64_t tm)
{
sdk->set_recv_timeout(tm);
}
void SrsEdgeRtmpUpstream::kbps_sample(const char* label, int64_t age)
{
sdk->kbps_sample(label, age);
}
2014-04-26 10:08:21 +00:00
SrsEdgeIngester::SrsEdgeIngester()
{
2015-10-14 06:37:24 +00:00
source = NULL;
edge = NULL;
req = NULL;
2016-01-11 07:46:23 +00:00
upstream = new SrsEdgeRtmpUpstream(redirect);
lb = new SrsLbRoundRobin();
pthread = new SrsReusableThread2("edge-igs", this, SRS_EDGE_INGESTER_CIMS);
2014-04-26 10:08:21 +00:00
}
SrsEdgeIngester::~SrsEdgeIngester()
2017-03-25 09:21:39 +00:00
{
stop();
srs_freep(upstream);
srs_freep(lb);
srs_freep(pthread);
2014-04-26 10:08:21 +00:00
}
2015-10-14 06:37:24 +00:00
int SrsEdgeIngester::initialize(SrsSource* s, SrsPlayEdge* e, SrsRequest* r)
2014-04-26 10:08:21 +00:00
{
int ret = ERROR_SUCCESS;
2015-10-14 06:37:24 +00:00
source = s;
edge = e;
req = r;
2014-04-26 10:08:21 +00:00
return ret;
}
int SrsEdgeIngester::start()
{
int ret = ERROR_SUCCESS;
2017-03-25 09:21:39 +00:00
2015-10-14 06:37:24 +00:00
if ((ret = source->on_publish()) != ERROR_SUCCESS) {
srs_error("edge pull stream then publish to edge failed. ret=%d", ret);
return ret;
}
2017-03-25 09:21:39 +00:00
return pthread->start();
}
void SrsEdgeIngester::stop()
{
pthread->stop();
upstream->close();
// notice to unpublish.
2017-02-06 10:33:26 +00:00
if (source) {
source->on_unpublish();
}
}
2015-09-17 05:36:02 +00:00
string SrsEdgeIngester::get_curr_origin()
{
return lb->selected();
2015-09-17 05:36:02 +00:00
}
int SrsEdgeIngester::cycle()
2014-04-26 10:08:21 +00:00
{
int ret = ERROR_SUCCESS;
2016-01-11 07:46:23 +00:00
for (;;) {
srs_freep(upstream);
upstream = new SrsEdgeRtmpUpstream(redirect);
// we only use the redict once.
// reset the redirect to empty, for maybe the origin changed.
redirect = "";
if ((ret = source->on_source_id_changed(_srs_context->get_id())) != ERROR_SUCCESS) {
return ret;
}
if ((ret = upstream->connect(req, lb)) != ERROR_SUCCESS) {
return ret;
}
if ((ret = edge->on_ingest_play()) != ERROR_SUCCESS) {
return ret;
}
ret = ingest();
// retry for rtmp 302 immediately.
if (ret == ERROR_CONTROL_REDIRECT) {
ret = ERROR_SUCCESS;
continue;
}
if (srs_is_client_gracefully_close(ret)) {
srs_warn("origin disconnected, retry. ret=%d", ret);
ret = ERROR_SUCCESS;
}
break;
}
2014-04-26 10:08:21 +00:00
return ret;
}
int SrsEdgeIngester::ingest()
2014-04-26 10:08:21 +00:00
{
int ret = ERROR_SUCCESS;
SrsPithyPrint* pprint = SrsPithyPrint::create_edge();
SrsAutoFree(SrsPithyPrint, pprint);
2015-10-14 06:37:24 +00:00
// set to larger timeout to read av data from origin.
upstream->set_recv_timeout(SRS_EDGE_INGESTER_TMMS);
2015-05-23 01:20:16 +00:00
while (!pthread->interrupted()) {
pprint->elapse();
// pithy print
if (pprint->can_print()) {
upstream->kbps_sample(SRS_CONSTS_LOG_EDGE_PLAY, pprint->age());
}
2015-10-14 06:37:24 +00:00
// read from client.
SrsCommonMessage* msg = NULL;
if ((ret = upstream->recv_message(&msg)) != ERROR_SUCCESS) {
2014-05-29 06:16:34 +00:00
if (!srs_is_client_gracefully_close(ret)) {
2014-07-21 07:30:44 +00:00
srs_error("pull origin server message failed. ret=%d", ret);
2014-05-29 06:16:34 +00:00
}
return ret;
}
srs_verbose("edge loop recv message. ret=%d", ret);
srs_assert(msg);
SrsAutoFree(SrsCommonMessage, msg);
if ((ret = process_publish_message(msg)) != ERROR_SUCCESS) {
return ret;
}
}
return ret;
}
int SrsEdgeIngester::process_publish_message(SrsCommonMessage* msg)
{
int ret = ERROR_SUCCESS;
2017-03-25 09:21:39 +00:00
// process audio packet
if (msg->header.is_audio()) {
if ((ret = source->on_audio(msg)) != ERROR_SUCCESS) {
srs_error("source process audio message failed. ret=%d", ret);
return ret;
}
}
// process video packet
if (msg->header.is_video()) {
if ((ret = source->on_video(msg)) != ERROR_SUCCESS) {
srs_error("source process video message failed. ret=%d", ret);
return ret;
}
}
// process aggregate packet
if (msg->header.is_aggregate()) {
if ((ret = source->on_aggregate(msg)) != ERROR_SUCCESS) {
srs_error("source process aggregate message failed. ret=%d", ret);
return ret;
}
return ret;
}
2017-03-25 09:21:39 +00:00
// process onMetaData
if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) {
SrsPacket* pkt = NULL;
if ((ret = upstream->decode_message(msg, &pkt)) != ERROR_SUCCESS) {
srs_error("decode onMetaData message failed. ret=%d", ret);
return ret;
}
SrsAutoFree(SrsPacket, pkt);
2017-03-25 09:21:39 +00:00
if (dynamic_cast<SrsOnMetaDataPacket*>(pkt)) {
SrsOnMetaDataPacket* metadata = dynamic_cast<SrsOnMetaDataPacket*>(pkt);
if ((ret = source->on_meta_data(msg, metadata)) != ERROR_SUCCESS) {
srs_error("source process onMetaData message failed. ret=%d", ret);
return ret;
}
2014-05-04 02:45:13 +00:00
srs_info("process onMetaData message success.");
return ret;
}
2014-05-04 02:45:13 +00:00
srs_info("ignore AMF0/AMF3 data message.");
return ret;
}
2016-01-11 07:46:23 +00:00
// call messages, for example, reject, redirect.
if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) {
SrsPacket* pkt = NULL;
if ((ret = upstream->decode_message(msg, &pkt)) != ERROR_SUCCESS) {
srs_error("decode call message failed. ret=%d", ret);
return ret;
}
SrsAutoFree(SrsPacket, pkt);
// RTMP 302 redirect
if (dynamic_cast<SrsCallPacket*>(pkt)) {
SrsCallPacket* call = dynamic_cast<SrsCallPacket*>(pkt);
if (!call->arguments->is_object()) {
return ret;
}
SrsAmf0Any* prop = NULL;
SrsAmf0Object* evt = call->arguments->to_object();
if ((prop = evt->ensure_property_string("level")) == NULL) {
return ret;
} else if (prop->to_str() != StatusLevelError) {
return ret;
}
if ((prop = evt->get_property("ex")) == NULL || !prop->is_object()) {
return ret;
}
SrsAmf0Object* ex = prop->to_object();
if ((prop = ex->ensure_property_string("redirect")) == NULL) {
return ret;
}
redirect = prop->to_str();
ret = ERROR_CONTROL_REDIRECT;
srs_info("RTMP 302 redirect to %s, ret=%d", redirect.c_str(), ret);
return ret;
}
}
return ret;
}
2014-04-27 01:29:37 +00:00
SrsEdgeForwarder::SrsEdgeForwarder()
{
2015-10-14 07:08:55 +00:00
edge = NULL;
req = NULL;
send_error_code = ERROR_SUCCESS;
sdk = NULL;
2015-09-24 10:33:07 +00:00
lb = new SrsLbRoundRobin();
pthread = new SrsReusableThread2("edge-fwr", this, SRS_EDGE_FORWARDER_CIMS);
2014-04-28 09:20:35 +00:00
queue = new SrsMessageQueue();
2014-04-27 01:29:37 +00:00
}
SrsEdgeForwarder::~SrsEdgeForwarder()
{
stop();
2015-09-24 10:33:07 +00:00
srs_freep(lb);
srs_freep(pthread);
srs_freep(queue);
2014-04-27 01:29:37 +00:00
}
2014-04-28 09:20:35 +00:00
void SrsEdgeForwarder::set_queue_size(double queue_size)
{
return queue->set_queue_size(queue_size);
}
2015-10-14 07:08:55 +00:00
int SrsEdgeForwarder::initialize(SrsSource* s, SrsPublishEdge* e, SrsRequest* r)
2014-04-27 01:29:37 +00:00
{
int ret = ERROR_SUCCESS;
2015-10-14 07:08:55 +00:00
source = s;
edge = e;
req = r;
2014-04-27 01:29:37 +00:00
return ret;
}
int SrsEdgeForwarder::start()
{
int ret = ERROR_SUCCESS;
2015-10-14 07:08:55 +00:00
// reset the error code.
2014-04-28 09:20:35 +00:00
send_error_code = ERROR_SUCCESS;
2015-10-14 07:08:55 +00:00
std::string url;
if (true) {
SrsConfDirective* conf = _srs_config->get_vhost_edge_origin(req->vhost);
srs_assert(conf);
// select the origin.
std::string server = lb->select(conf->args);
int port = SRS_CONSTS_RTMP_DEFAULT_PORT;
srs_parse_hostport(server, server, port);
// support vhost tranform for edge,
2015-11-11 02:45:45 +00:00
// @see https://github.com/ossrs/srs/issues/372
2015-10-14 07:08:55 +00:00
std::string vhost = _srs_config->get_vhost_edge_transform_vhost(req->vhost);
vhost = srs_string_replace(vhost, "[vhost]", req->vhost);
url = srs_generate_rtmp_url(server, port, vhost, req->app, req->stream);
2014-04-27 01:29:37 +00:00
}
2015-10-14 07:08:55 +00:00
// open socket.
srs_freep(sdk);
int64_t cto = SRS_EDGE_FORWARDER_TMMS;
int64_t sto = SRS_CONSTS_RTMP_TMMS;
sdk = new SrsSimpleRtmpClient(url, cto, sto);
if ((ret = sdk->connect()) != ERROR_SUCCESS) {
srs_warn("edge push %s failed, cto=%" PRId64 ", sto=%" PRId64 ". ret=%d", url.c_str(), cto, sto, ret);
2014-04-27 01:29:37 +00:00
return ret;
}
2015-10-14 07:08:55 +00:00
if ((ret = sdk->publish()) != ERROR_SUCCESS) {
srs_error("edge push publish failed. ret=%d", ret);
2014-04-27 01:29:37 +00:00
return ret;
}
return pthread->start();
2014-04-27 01:29:37 +00:00
}
void SrsEdgeForwarder::stop()
{
pthread->stop();
2015-09-14 07:19:49 +00:00
queue->clear();
srs_freep(sdk);
}
#define SYS_MAX_EDGE_SEND_MSGS 128
int SrsEdgeForwarder::cycle()
{
int ret = ERROR_SUCCESS;
sdk->set_recv_timeout(SRS_CONSTS_RTMP_PULSE_TMMS);
SrsPithyPrint* pprint = SrsPithyPrint::create_edge();
SrsAutoFree(SrsPithyPrint, pprint);
SrsMessageArray msgs(SYS_MAX_EDGE_SEND_MSGS);
2017-03-25 09:21:39 +00:00
2015-05-23 01:20:16 +00:00
while (!pthread->interrupted()) {
2014-04-28 09:20:35 +00:00
if (send_error_code != ERROR_SUCCESS) {
st_usleep(SRS_EDGE_FORWARDER_TMMS * 1000);
2014-04-28 09:20:35 +00:00
continue;
}
2017-03-25 09:21:39 +00:00
2014-04-28 09:20:35 +00:00
// read from client.
if (true) {
SrsCommonMessage* msg = NULL;
2015-10-14 07:08:55 +00:00
ret = sdk->recv_message(&msg);
2014-04-28 09:20:35 +00:00
srs_verbose("edge loop recv message. ret=%d", ret);
if (ret != ERROR_SUCCESS && ret != ERROR_SOCKET_TIMEOUT) {
2014-07-21 07:30:44 +00:00
srs_error("edge push get server control message failed. ret=%d", ret);
2014-04-28 09:20:35 +00:00
send_error_code = ret;
continue;
}
srs_freep(msg);
}
// forward all messages.
// each msg in msgs.msgs must be free, for the SrsMessageArray never free them.
2014-04-28 09:20:35 +00:00
int count = 0;
if ((ret = queue->dump_packets(msgs.max, msgs.msgs, count)) != ERROR_SUCCESS) {
2014-07-21 07:30:44 +00:00
srs_error("get message to push to origin failed. ret=%d", ret);
2014-04-28 09:20:35 +00:00
return ret;
}
pprint->elapse();
// pithy print
if (pprint->can_print()) {
2015-10-14 07:08:55 +00:00
sdk->kbps_sample(SRS_CONSTS_LOG_EDGE_PUBLISH, pprint->age(), count);
}
2014-04-28 09:20:35 +00:00
// ignore when no messages.
if (count <= 0) {
2015-10-14 07:08:55 +00:00
srs_verbose("edge no packets to push.");
continue;
}
2017-03-25 09:21:39 +00:00
// sendout messages, all messages are freed by send_and_free_messages().
2015-10-14 07:08:55 +00:00
if ((ret = sdk->send_and_free_messages(msgs.msgs, count)) != ERROR_SUCCESS) {
srs_error("edge publish push message to server failed. ret=%d", ret);
return ret;
2014-04-28 09:20:35 +00:00
}
}
return ret;
}
int SrsEdgeForwarder::proxy(SrsCommonMessage* msg)
2014-04-27 01:29:37 +00:00
{
int ret = ERROR_SUCCESS;
2014-04-28 09:20:35 +00:00
if ((ret = send_error_code) != ERROR_SUCCESS) {
srs_error("publish edge proxy thread send error, ret=%d", ret);
return ret;
}
2014-04-27 06:57:28 +00:00
// the msg is auto free by source,
// so we just ignore, or copy then send it.
if (msg->size <= 0
|| msg->header.is_set_chunk_size()
|| msg->header.is_window_ackledgement_size()
|| msg->header.is_ackledgement()
2017-03-25 09:21:39 +00:00
) {
return ret;
}
SrsSharedPtrMessage copy;
if ((ret = copy.create(msg)) != ERROR_SUCCESS) {
2014-04-27 06:57:28 +00:00
srs_error("initialize the msg failed. ret=%d", ret);
return ret;
}
2014-04-27 06:57:28 +00:00
srs_verbose("initialize shared ptr msg success.");
2015-10-14 07:08:55 +00:00
copy.stream_id = sdk->sid();
if ((ret = queue->enqueue(copy.copy())) != ERROR_SUCCESS) {
2014-04-28 09:20:35 +00:00
srs_error("enqueue edge publish msg failed. ret=%d", ret);
2014-04-27 01:29:37 +00:00
}
return ret;
}
SrsPlayEdge::SrsPlayEdge()
{
state = SrsEdgeStateInit;
2014-04-26 10:08:21 +00:00
ingester = new SrsEdgeIngester();
}
2014-04-27 01:29:37 +00:00
SrsPlayEdge::~SrsPlayEdge()
{
2014-04-26 10:08:21 +00:00
srs_freep(ingester);
}
2014-04-27 01:29:37 +00:00
int SrsPlayEdge::initialize(SrsSource* source, SrsRequest* req)
2014-04-26 09:16:18 +00:00
{
int ret = ERROR_SUCCESS;
if ((ret = ingester->initialize(source, this, req)) != ERROR_SUCCESS) {
2014-04-26 10:08:21 +00:00
return ret;
}
2014-04-26 09:16:18 +00:00
return ret;
}
2014-04-27 01:29:37 +00:00
int SrsPlayEdge::on_client_play()
{
int ret = ERROR_SUCCESS;
2014-04-26 10:08:21 +00:00
// start ingest when init state.
if (state == SrsEdgeStateInit) {
state = SrsEdgeStatePlay;
return ingester->start();
}
2017-03-25 09:21:39 +00:00
return ret;
}
2014-04-27 01:29:37 +00:00
void SrsPlayEdge::on_all_client_stop()
{
// when all client disconnected,
// and edge is ingesting origin stream, abort it.
if (state == SrsEdgeStatePlay || state == SrsEdgeStateIngestConnected) {
ingester->stop();
2017-03-25 09:21:39 +00:00
SrsEdgeState pstate = state;
state = SrsEdgeStateInit;
srs_trace("edge change from %d to state %d (init).", pstate, state);
return;
}
}
2015-09-17 05:36:02 +00:00
string SrsPlayEdge::get_curr_origin()
{
return ingester->get_curr_origin();
}
2014-04-27 01:29:37 +00:00
int SrsPlayEdge::on_ingest_play()
{
int ret = ERROR_SUCCESS;
// when already connected(for instance, reconnect for error), ignore.
if (state == SrsEdgeStateIngestConnected) {
return ret;
}
srs_assert(state == SrsEdgeStatePlay);
SrsEdgeState pstate = state;
state = SrsEdgeStateIngestConnected;
2014-07-21 07:30:44 +00:00
srs_trace("edge change from %d to state %d (pull).", pstate, state);
return ret;
}
2014-04-27 01:29:37 +00:00
SrsPublishEdge::SrsPublishEdge()
{
state = SrsEdgeStateInit;
forwarder = new SrsEdgeForwarder();
}
SrsPublishEdge::~SrsPublishEdge()
{
srs_freep(forwarder);
}
2014-04-28 09:20:35 +00:00
void SrsPublishEdge::set_queue_size(double queue_size)
{
return forwarder->set_queue_size(queue_size);
}
2014-04-27 01:29:37 +00:00
int SrsPublishEdge::initialize(SrsSource* source, SrsRequest* req)
{
int ret = ERROR_SUCCESS;
2017-03-25 09:21:39 +00:00
2014-04-27 01:29:37 +00:00
if ((ret = forwarder->initialize(source, this, req)) != ERROR_SUCCESS) {
return ret;
}
return ret;
}
bool SrsPublishEdge::can_publish()
{
return state != SrsEdgeStatePublish;
}
2014-04-27 01:29:37 +00:00
int SrsPublishEdge::on_client_publish()
{
int ret = ERROR_SUCCESS;
// error when not init state.
if (state != SrsEdgeStateInit) {
ret = ERROR_RTMP_EDGE_PUBLISH_STATE;
srs_error("invalid state for client to publish stream on edge. "
2017-03-25 09:21:39 +00:00
"state=%d, ret=%d", state, ret);
return ret;
2014-04-27 01:29:37 +00:00
}
2015-11-11 02:37:50 +00:00
// @see https://github.com/ossrs/srs/issues/180
// to avoid multiple publish the same stream on the same edge,
// directly enter the publish stage.
if (true) {
SrsEdgeState pstate = state;
state = SrsEdgeStatePublish;
srs_trace("edge change from %d to state %d (push).", pstate, state);
2014-04-28 09:20:35 +00:00
}
// start to forward stream to origin.
ret = forwarder->start();
2015-11-11 02:37:50 +00:00
// @see https://github.com/ossrs/srs/issues/180
// when failed, revert to init
if (ret != ERROR_SUCCESS) {
SrsEdgeState pstate = state;
state = SrsEdgeStateInit;
srs_trace("edge revert from %d to state %d (push). ret=%d", pstate, state, ret);
}
2014-04-28 09:20:35 +00:00
return ret;
2014-04-27 01:29:37 +00:00
}
int SrsPublishEdge::on_proxy_publish(SrsCommonMessage* msg)
2014-04-27 01:29:37 +00:00
{
2014-04-27 06:57:28 +00:00
return forwarder->proxy(msg);
}
void SrsPublishEdge::on_proxy_unpublish()
{
if (state == SrsEdgeStatePublish) {
forwarder->stop();
}
SrsEdgeState pstate = state;
state = SrsEdgeStateInit;
srs_trace("edge change from %d to state %d (init).", pstate, state);
2014-04-27 01:29:37 +00:00
}
2014-08-02 14:18:39 +00:00