mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
refine edge ingester, use upstream adapter.
This commit is contained in:
parent
7ae2c6bf50
commit
86a7db0adb
3 changed files with 145 additions and 56 deletions
|
@ -62,69 +62,32 @@ using namespace std;
|
||||||
// when edge error, wait for quit
|
// when edge error, wait for quit
|
||||||
#define SRS_EDGE_FORWARDER_ERROR_US (int64_t)(50*1000LL)
|
#define SRS_EDGE_FORWARDER_ERROR_US (int64_t)(50*1000LL)
|
||||||
|
|
||||||
SrsEdgeIngester::SrsEdgeIngester()
|
SrsEdgeUpstream::SrsEdgeUpstream()
|
||||||
{
|
{
|
||||||
source = NULL;
|
|
||||||
edge = NULL;
|
|
||||||
req = NULL;
|
|
||||||
|
|
||||||
sdk = new SrsSimpleRtmpClient();
|
|
||||||
lb = new SrsLbRoundRobin();
|
|
||||||
pthread = new SrsReusableThread2("edge-igs", this, SRS_EDGE_INGESTER_SLEEP_US);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsEdgeIngester::~SrsEdgeIngester()
|
SrsEdgeUpstream::~SrsEdgeUpstream()
|
||||||
{
|
{
|
||||||
stop();
|
}
|
||||||
|
|
||||||
|
SrsEdgeRtmpUpstream::SrsEdgeRtmpUpstream()
|
||||||
|
{
|
||||||
|
sdk = new SrsSimpleRtmpClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsEdgeRtmpUpstream::~SrsEdgeRtmpUpstream()
|
||||||
|
{
|
||||||
|
close();
|
||||||
|
|
||||||
srs_freep(sdk);
|
srs_freep(sdk);
|
||||||
srs_freep(lb);
|
|
||||||
srs_freep(pthread);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsEdgeIngester::initialize(SrsSource* s, SrsPlayEdge* e, SrsRequest* r)
|
int SrsEdgeRtmpUpstream::connect(SrsRequest* r, SrsLbRoundRobin* lb)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
source = s;
|
SrsRequest* req = r;
|
||||||
edge = e;
|
|
||||||
req = r;
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int SrsEdgeIngester::start()
|
|
||||||
{
|
|
||||||
int ret = ERROR_SUCCESS;
|
|
||||||
|
|
||||||
if ((ret = source->on_publish()) != ERROR_SUCCESS) {
|
|
||||||
srs_error("edge pull stream then publish to edge failed. ret=%d", ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return pthread->start();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SrsEdgeIngester::stop()
|
|
||||||
{
|
|
||||||
pthread->stop();
|
|
||||||
sdk->close();
|
|
||||||
|
|
||||||
// notice to unpublish.
|
|
||||||
source->on_unpublish();
|
|
||||||
}
|
|
||||||
|
|
||||||
string SrsEdgeIngester::get_curr_origin()
|
|
||||||
{
|
|
||||||
return lb->selected();
|
|
||||||
}
|
|
||||||
|
|
||||||
int SrsEdgeIngester::cycle()
|
|
||||||
{
|
|
||||||
int ret = ERROR_SUCCESS;
|
|
||||||
|
|
||||||
source->on_source_id_changed(_srs_context->get_id());
|
|
||||||
|
|
||||||
std::string url;
|
std::string url;
|
||||||
if (true) {
|
if (true) {
|
||||||
SrsConfDirective* conf = _srs_config->get_vhost_edge_origin(req->vhost);
|
SrsConfDirective* conf = _srs_config->get_vhost_edge_origin(req->vhost);
|
||||||
|
@ -163,6 +126,98 @@ int SrsEdgeIngester::cycle()
|
||||||
return 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()
|
||||||
|
{
|
||||||
|
sdk->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SrsEdgeRtmpUpstream::kbps_sample(const char* label, int64_t age)
|
||||||
|
{
|
||||||
|
sdk->kbps_sample(label, age);
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsEdgeIngester::SrsEdgeIngester()
|
||||||
|
{
|
||||||
|
source = NULL;
|
||||||
|
edge = NULL;
|
||||||
|
req = NULL;
|
||||||
|
|
||||||
|
upstream = new SrsEdgeRtmpUpstream();
|
||||||
|
lb = new SrsLbRoundRobin();
|
||||||
|
pthread = new SrsReusableThread2("edge-igs", this, SRS_EDGE_INGESTER_SLEEP_US);
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsEdgeIngester::~SrsEdgeIngester()
|
||||||
|
{
|
||||||
|
stop();
|
||||||
|
|
||||||
|
srs_freep(upstream);
|
||||||
|
srs_freep(lb);
|
||||||
|
srs_freep(pthread);
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsEdgeIngester::initialize(SrsSource* s, SrsPlayEdge* e, SrsRequest* r)
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
source = s;
|
||||||
|
edge = e;
|
||||||
|
req = r;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsEdgeIngester::start()
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
if ((ret = source->on_publish()) != ERROR_SUCCESS) {
|
||||||
|
srs_error("edge pull stream then publish to edge failed. ret=%d", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pthread->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SrsEdgeIngester::stop()
|
||||||
|
{
|
||||||
|
pthread->stop();
|
||||||
|
upstream->close();
|
||||||
|
|
||||||
|
// notice to unpublish.
|
||||||
|
source->on_unpublish();
|
||||||
|
}
|
||||||
|
|
||||||
|
string SrsEdgeIngester::get_curr_origin()
|
||||||
|
{
|
||||||
|
return lb->selected();
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsEdgeIngester::cycle()
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
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) {
|
if ((ret = edge->on_ingest_play()) != ERROR_SUCCESS) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -188,12 +243,12 @@ int SrsEdgeIngester::ingest()
|
||||||
|
|
||||||
// pithy print
|
// pithy print
|
||||||
if (pprint->can_print()) {
|
if (pprint->can_print()) {
|
||||||
sdk->kbps_sample(SRS_CONSTS_LOG_EDGE_PLAY, pprint->age());
|
upstream->kbps_sample(SRS_CONSTS_LOG_EDGE_PLAY, pprint->age());
|
||||||
}
|
}
|
||||||
|
|
||||||
// read from client.
|
// read from client.
|
||||||
SrsCommonMessage* msg = NULL;
|
SrsCommonMessage* msg = NULL;
|
||||||
if ((ret = sdk->recv_message(&msg)) != ERROR_SUCCESS) {
|
if ((ret = upstream->recv_message(&msg)) != ERROR_SUCCESS) {
|
||||||
if (!srs_is_client_gracefully_close(ret)) {
|
if (!srs_is_client_gracefully_close(ret)) {
|
||||||
srs_error("pull origin server message failed. ret=%d", ret);
|
srs_error("pull origin server message failed. ret=%d", ret);
|
||||||
}
|
}
|
||||||
|
@ -244,7 +299,7 @@ int SrsEdgeIngester::process_publish_message(SrsCommonMessage* msg)
|
||||||
// process onMetaData
|
// process onMetaData
|
||||||
if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) {
|
if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) {
|
||||||
SrsPacket* pkt = NULL;
|
SrsPacket* pkt = NULL;
|
||||||
if ((ret = sdk->decode_message(msg, &pkt)) != ERROR_SUCCESS) {
|
if ((ret = upstream->decode_message(msg, &pkt)) != ERROR_SUCCESS) {
|
||||||
srs_error("decode onMetaData message failed. ret=%d", ret);
|
srs_error("decode onMetaData message failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,7 @@ class SrsKbps;
|
||||||
class SrsLbRoundRobin;
|
class SrsLbRoundRobin;
|
||||||
class SrsTcpClient;
|
class SrsTcpClient;
|
||||||
class SrsSimpleRtmpClient;
|
class SrsSimpleRtmpClient;
|
||||||
|
class SrsPacket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the state of edge, auto machine
|
* the state of edge, auto machine
|
||||||
|
@ -75,6 +76,37 @@ enum SrsEdgeUserState
|
||||||
SrsEdgeUserStateReloading = 100,
|
SrsEdgeUserStateReloading = 100,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the upstream of edge, can be rtmp or http.
|
||||||
|
*/
|
||||||
|
class SrsEdgeUpstream
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SrsEdgeUpstream();
|
||||||
|
virtual ~SrsEdgeUpstream();
|
||||||
|
public:
|
||||||
|
virtual int connect(SrsRequest* r, SrsLbRoundRobin* lb) = 0;
|
||||||
|
virtual int recv_message(SrsCommonMessage** pmsg) = 0;
|
||||||
|
virtual int decode_message(SrsCommonMessage* msg, SrsPacket** ppacket) = 0;
|
||||||
|
virtual void close() = 0;
|
||||||
|
virtual void kbps_sample(const char* label, int64_t age) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class SrsEdgeRtmpUpstream : public SrsEdgeUpstream
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
SrsSimpleRtmpClient* sdk;
|
||||||
|
public:
|
||||||
|
SrsEdgeRtmpUpstream();
|
||||||
|
virtual ~SrsEdgeRtmpUpstream();
|
||||||
|
public:
|
||||||
|
virtual int connect(SrsRequest* r, SrsLbRoundRobin* lb);
|
||||||
|
virtual int recv_message(SrsCommonMessage** pmsg);
|
||||||
|
virtual int decode_message(SrsCommonMessage* msg, SrsPacket** ppacket);
|
||||||
|
virtual void close();
|
||||||
|
virtual void kbps_sample(const char* label, int64_t age);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* edge used to ingest stream from origin.
|
* edge used to ingest stream from origin.
|
||||||
*/
|
*/
|
||||||
|
@ -85,8 +117,8 @@ private:
|
||||||
SrsPlayEdge* edge;
|
SrsPlayEdge* edge;
|
||||||
SrsRequest* req;
|
SrsRequest* req;
|
||||||
SrsReusableThread2* pthread;
|
SrsReusableThread2* pthread;
|
||||||
SrsSimpleRtmpClient* sdk;
|
|
||||||
SrsLbRoundRobin* lb;
|
SrsLbRoundRobin* lb;
|
||||||
|
SrsEdgeUpstream* upstream;
|
||||||
public:
|
public:
|
||||||
SrsEdgeIngester();
|
SrsEdgeIngester();
|
||||||
virtual ~SrsEdgeIngester();
|
virtual ~SrsEdgeIngester();
|
||||||
|
|
|
@ -2054,7 +2054,9 @@ int SrsSource::on_publish()
|
||||||
|
|
||||||
// whatever, the publish thread is the source or edge source,
|
// whatever, the publish thread is the source or edge source,
|
||||||
// save its id to srouce id.
|
// save its id to srouce id.
|
||||||
on_source_id_changed(_srs_context->get_id());
|
if ((ret = on_source_id_changed(_srs_context->get_id())) != ERROR_SUCCESS) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
// reset the mix queue.
|
// reset the mix queue.
|
||||||
mix_queue->clear();
|
mix_queue->clear();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue