winlin

add edge framework

@@ -25,21 +25,63 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -25,21 +25,63 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 25
26 #include <srs_kernel_error.hpp> 26 #include <srs_kernel_error.hpp>
27 #include <srs_protocol_rtmp.hpp> 27 #include <srs_protocol_rtmp.hpp>
  28 +#include <srs_kernel_log.hpp>
  29 +
  30 +// when error, edge ingester sleep for a while and retry.
  31 +#define SRS_EDGE_INGESTER_SLEEP_US (int64_t)(3*1000*1000LL)
  32 +
  33 +SrsEdgeIngester::SrsEdgeIngester()
  34 +{
  35 + _edge = NULL;
  36 + _req = NULL;
  37 + pthread = new SrsThread(this, SRS_EDGE_INGESTER_SLEEP_US);
  38 +}
  39 +
  40 +SrsEdgeIngester::~SrsEdgeIngester()
  41 +{
  42 +}
  43 +
  44 +int SrsEdgeIngester::initialize(SrsEdge* edge, SrsRequest* req)
  45 +{
  46 + int ret = ERROR_SUCCESS;
  47 +
  48 + _edge = edge;
  49 + _req = req;
  50 +
  51 + return ret;
  52 +}
  53 +
  54 +int SrsEdgeIngester::start()
  55 +{
  56 + int ret = ERROR_SUCCESS;
  57 + return ret;
  58 + //return pthread->start();
  59 +}
  60 +
  61 +int SrsEdgeIngester::cycle()
  62 +{
  63 + int ret = ERROR_SUCCESS;
  64 + return ret;
  65 +}
28 66
29 SrsEdge::SrsEdge() 67 SrsEdge::SrsEdge()
30 { 68 {
31 state = SrsEdgeStateInit; 69 state = SrsEdgeStateInit;
  70 + ingester = new SrsEdgeIngester();
32 } 71 }
33 72
34 SrsEdge::~SrsEdge() 73 SrsEdge::~SrsEdge()
35 { 74 {
  75 + srs_freep(ingester);
36 } 76 }
37 77
38 int SrsEdge::initialize(SrsRequest* req) 78 int SrsEdge::initialize(SrsRequest* req)
39 { 79 {
40 int ret = ERROR_SUCCESS; 80 int ret = ERROR_SUCCESS;
41 81
42 - _req = req; 82 + if ((ret = ingester->initialize(this, req)) != ERROR_SUCCESS) {
  83 + return ret;
  84 + }
43 85
44 return ret; 86 return ret;
45 } 87 }
@@ -47,6 +89,20 @@ int SrsEdge::initialize(SrsRequest* req) @@ -47,6 +89,20 @@ int SrsEdge::initialize(SrsRequest* req)
47 int SrsEdge::on_client_play() 89 int SrsEdge::on_client_play()
48 { 90 {
49 int ret = ERROR_SUCCESS; 91 int ret = ERROR_SUCCESS;
  92 +
  93 + // error state.
  94 + if (state == SrsEdgeStateAborting || state == SrsEdgeStateReloading) {
  95 + ret = ERROR_RTMP_EDGE_PLAY_STATE;
  96 + srs_error("invalid state for client to play stream on edge. state=%d, ret=%d", state, ret);
  97 + return ret;
  98 + }
  99 +
  100 + // start ingest when init state.
  101 + if (state == SrsEdgeStateInit) {
  102 + state = SrsEdgeStatePlay;
  103 + return ingester->start();
  104 + }
  105 +
50 return ret; 106 return ret;
51 } 107 }
52 108
@@ -30,6 +30,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,6 +30,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 30
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
  33 +#include <srs_app_thread.hpp>
  34 +
  35 +class SrsEdge;
33 class SrsRequest; 36 class SrsRequest;
34 37
35 /** 38 /**
@@ -46,13 +49,33 @@ enum SrsEdgeState @@ -46,13 +49,33 @@ enum SrsEdgeState
46 }; 49 };
47 50
48 /** 51 /**
  52 +* edge used to ingest stream from origin.
  53 +*/
  54 +class SrsEdgeIngester : public ISrsThreadHandler
  55 +{
  56 +private:
  57 + SrsEdge* _edge;
  58 + SrsRequest* _req;
  59 + SrsThread* pthread;
  60 +public:
  61 + SrsEdgeIngester();
  62 + virtual ~SrsEdgeIngester();
  63 +public:
  64 + virtual int initialize(SrsEdge* edge, SrsRequest* req);
  65 + virtual int start();
  66 +// interface ISrsThreadHandler
  67 +public:
  68 + virtual int cycle();
  69 +};
  70 +
  71 +/**
49 * edge control service. 72 * edge control service.
50 */ 73 */
51 class SrsEdge 74 class SrsEdge
52 { 75 {
53 private: 76 private:
54 - SrsRequest* _req;  
55 SrsEdgeState state; 77 SrsEdgeState state;
  78 + SrsEdgeIngester* ingester;
56 public: 79 public:
57 SrsEdge(); 80 SrsEdge();
58 virtual ~SrsEdge(); 81 virtual ~SrsEdge();
@@ -76,6 +76,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -76,6 +76,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 // 2. srs-librtmp return error, to terminate the program. 76 // 2. srs-librtmp return error, to terminate the program.
77 #define ERROR_RTMP_HS_SSL_REQUIRE 318 77 #define ERROR_RTMP_HS_SSL_REQUIRE 318
78 #define ERROR_RTMP_DURATION_EXCEED 319 78 #define ERROR_RTMP_DURATION_EXCEED 319
  79 +// edge specified errors
  80 +// invalid state for client to play edge stream.
  81 +#define ERROR_RTMP_EDGE_PLAY_STATE 320
79 82
80 #define ERROR_SYSTEM_STREAM_INIT 400 83 #define ERROR_SYSTEM_STREAM_INIT 400
81 #define ERROR_SYSTEM_PACKET_INVALID 401 84 #define ERROR_SYSTEM_PACKET_INVALID 401