Blame view

trunk/src/app/srs_app_edge.hpp 5.5 KB
winlin authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
The MIT License (MIT)

Copyright (c) 2013-2014 winlin

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.
*/

#ifndef SRS_APP_EDGE_HPP
#define SRS_APP_EDGE_HPP

/*
#include <srs_app_edge.hpp>
*/

#include <srs_core.hpp>
33
#include <srs_app_st.hpp>
winlin authored
34 35
#include <srs_app_thread.hpp>
36 37
#include <string>
38
class SrsStSocket;
39
class SrsRtmpServer;
40
class SrsSource;
41
class SrsRequest;
winlin authored
42 43
class SrsPlayEdge;
class SrsPublishEdge;
44
class SrsRtmpClient;
45
class SrsMessage;
46
class SrsMessageQueue;
47
class ISrsProtocolReaderWriter;
48
class SrsKbps;
49
50
/**
51
* the state of edge, auto machine
52 53 54 55
*/
enum SrsEdgeState
{
    SrsEdgeStateInit = 0,
winlin authored
56 57

    // for play edge
58
    SrsEdgeStatePlay = 100,
59
    // play stream from origin, ingest stream
60
    SrsEdgeStateIngestConnected = 101,
winlin authored
61 62 63
    
    // for publish edge
    SrsEdgeStatePublish = 200,
64 65 66 67 68 69 70 71 72
};

/**
* the state of edge from user, manual machine
*/
enum SrsEdgeUserState
{
    SrsEdgeUserStateInit = 0,
    SrsEdgeUserStateReloading = 100,
73 74 75
};

/**
winlin authored
76 77 78 79 80
* edge used to ingest stream from origin.
*/
class SrsEdgeIngester : public ISrsThreadHandler
{
private:
81 82 83
    int stream_id;
private:
    SrsSource* _source;
winlin authored
84
    SrsPlayEdge* _edge;
winlin authored
85 86
    SrsRequest* _req;
    SrsThread* pthread;
87 88
    st_netfd_t stfd;
    ISrsProtocolReaderWriter* io;
89
    SrsKbps* kbps;
90 91
    SrsRtmpClient* client;
    int origin_index;
winlin authored
92 93 94 95
public:
    SrsEdgeIngester();
    virtual ~SrsEdgeIngester();
public:
winlin authored
96
    virtual int initialize(SrsSource* source, SrsPlayEdge* edge, SrsRequest* req);
winlin authored
97
    virtual int start();
98
    virtual void stop();
winlin authored
99 100 101
// interface ISrsThreadHandler
public:
    virtual int cycle();
102 103 104
private:
    virtual int ingest();
    virtual void close_underlayer_socket();
105 106
    virtual int connect_server(std::string& ep_server, std::string& ep_port);
    virtual int connect_app(std::string ep_server, std::string ep_port);
107
    virtual int process_publish_message(SrsMessage* msg);
winlin authored
108 109 110
};

/**
winlin authored
111 112
* edge used to forward stream to origin.
*/
113
class SrsEdgeForwarder : public ISrsThreadHandler
winlin authored
114 115 116 117 118 119 120
{
private:
    int stream_id;
private:
    SrsSource* _source;
    SrsPublishEdge* _edge;
    SrsRequest* _req;
121
    SrsThread* pthread;
winlin authored
122 123
    st_netfd_t stfd;
    ISrsProtocolReaderWriter* io;
124
    SrsKbps* kbps;
winlin authored
125 126
    SrsRtmpClient* client;
    int origin_index;
127 128 129 130 131 132 133 134 135 136 137
    /**
    * we must ensure one thread one fd principle,
    * that is, a fd must be write/read by the one thread.
    * the publish service thread will proxy(msg), and the edge forward thread
    * will cycle(), so we use queue for cycle to send the msg of proxy.
    */
    SrsMessageQueue* queue;
    /**
    * error code of send, for edge proxy thread to query.
    */
    int send_error_code;
winlin authored
138 139 140 141
public:
    SrsEdgeForwarder();
    virtual ~SrsEdgeForwarder();
public:
142 143
    virtual void set_queue_size(double queue_size);
public:
winlin authored
144 145 146
    virtual int initialize(SrsSource* source, SrsPublishEdge* edge, SrsRequest* req);
    virtual int start();
    virtual void stop();
147 148 149
// interface ISrsThreadHandler
public:
    virtual int cycle();
winlin authored
150
public:
151
    virtual int proxy(SrsMessage* msg);
winlin authored
152 153
private:
    virtual void close_underlayer_socket();
154 155
    virtual int connect_server(std::string& ep_server, std::string& ep_port);
    virtual int connect_app(std::string ep_server, std::string ep_port);
winlin authored
156 157 158 159 160
};

/**
* play edge control service.
* downloading edge speed-up.
161
*/
winlin authored
162
class SrsPlayEdge
163 164 165
{
private:
    SrsEdgeState state;
166
    SrsEdgeUserState user_state;
winlin authored
167
    SrsEdgeIngester* ingester;
168
public:
winlin authored
169 170
    SrsPlayEdge();
    virtual ~SrsPlayEdge();
171
public:
172 173 174 175 176
    /**
    * always use the req of source,
    * for we assume all client to edge is invalid,
    * if auth open, edge must valid it from origin, then service it.
    */
177
    virtual int initialize(SrsSource* source, SrsRequest* req);
178 179 180 181
    /**
    * when client play stream on edge.
    */
    virtual int on_client_play();
182 183 184 185 186 187 188 189 190
    /**
    * when all client stopped play, disconnect to origin.
    */
    virtual void on_all_client_stop();
public:
    /**
    * when ingester start to play stream.
    */
    virtual int on_ingest_play();
191 192
};
winlin authored
193 194 195 196 197 198 199 200 201 202 203 204 205 206
/**
* publish edge control service.
* uploading edge speed-up.
*/
class SrsPublishEdge
{
private:
    SrsEdgeState state;
    SrsEdgeUserState user_state;
    SrsEdgeForwarder* forwarder;
public:
    SrsPublishEdge();
    virtual ~SrsPublishEdge();
public:
207 208
    virtual void set_queue_size(double queue_size);
public:
winlin authored
209 210 211 212 213 214
    virtual int initialize(SrsSource* source, SrsRequest* req);
    /**
    * when client publish stream on edge.
    */
    virtual int on_client_publish();
    /**
215
    * proxy publish stream to edge
winlin authored
216
    */
217
    virtual int on_proxy_publish(SrsMessage* msg);
218 219 220 221
    /**
    * proxy unpublish stream to edge.
    */
    virtual void on_proxy_unpublish();
winlin authored
222 223
};
224
#endif
225