Blame view

trunk/src/app/srs_app_edge.hpp 5.0 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
class SrsSocket;
class SrsRtmpServer;
38
class SrsSource;
39
class SrsRequest;
winlin authored
40 41
class SrsPlayEdge;
class SrsPublishEdge;
42
class SrsRtmpClient;
43
class SrsMessage;
44
class SrsMessageQueue;
45
class ISrsProtocolReaderWriter;
46
47
/**
48
* the state of edge, auto machine
49 50 51 52
*/
enum SrsEdgeState
{
    SrsEdgeStateInit = 0,
winlin authored
53 54

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

/**
* the state of edge from user, manual machine
*/
enum SrsEdgeUserState
{
    SrsEdgeUserStateInit = 0,
    SrsEdgeUserStateReloading = 100,
70 71 72
};

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

/**
winlin authored
106 107
* edge used to forward stream to origin.
*/
108
class SrsEdgeForwarder : public ISrsThreadHandler
winlin authored
109 110 111 112 113 114 115
{
private:
    int stream_id;
private:
    SrsSource* _source;
    SrsPublishEdge* _edge;
    SrsRequest* _req;
116
    SrsThread* pthread;
winlin authored
117 118 119 120
    st_netfd_t stfd;
    ISrsProtocolReaderWriter* io;
    SrsRtmpClient* client;
    int origin_index;
121 122 123 124 125 126 127 128 129 130 131
    /**
    * 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
132 133 134 135
public:
    SrsEdgeForwarder();
    virtual ~SrsEdgeForwarder();
public:
136 137
    virtual void set_queue_size(double queue_size);
public:
winlin authored
138 139 140
    virtual int initialize(SrsSource* source, SrsPublishEdge* edge, SrsRequest* req);
    virtual int start();
    virtual void stop();
141 142 143
// interface ISrsThreadHandler
public:
    virtual int cycle();
winlin authored
144
public:
145
    virtual int proxy(SrsMessage* msg);
winlin authored
146 147 148 149 150 151 152 153
private:
    virtual void close_underlayer_socket();
    virtual int connect_server();
};

/**
* play edge control service.
* downloading edge speed-up.
154
*/
winlin authored
155
class SrsPlayEdge
156 157 158
{
private:
    SrsEdgeState state;
159
    SrsEdgeUserState user_state;
winlin authored
160
    SrsEdgeIngester* ingester;
161
public:
winlin authored
162 163
    SrsPlayEdge();
    virtual ~SrsPlayEdge();
164
public:
165
    virtual int initialize(SrsSource* source, SrsRequest* req);
166 167 168 169
    /**
    * when client play stream on edge.
    */
    virtual int on_client_play();
170 171 172 173 174 175 176 177 178
    /**
    * 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();
179 180
};
winlin authored
181 182 183 184 185 186 187 188 189 190 191 192 193 194
/**
* publish edge control service.
* uploading edge speed-up.
*/
class SrsPublishEdge
{
private:
    SrsEdgeState state;
    SrsEdgeUserState user_state;
    SrsEdgeForwarder* forwarder;
public:
    SrsPublishEdge();
    virtual ~SrsPublishEdge();
public:
195 196
    virtual void set_queue_size(double queue_size);
public:
winlin authored
197 198 199 200 201 202
    virtual int initialize(SrsSource* source, SrsRequest* req);
    /**
    * when client publish stream on edge.
    */
    virtual int on_client_publish();
    /**
203
    * proxy publish stream to edge
winlin authored
204
    */
205
    virtual int on_proxy_publish(SrsMessage* msg);
206 207 208 209
    /**
    * proxy unpublish stream to edge.
    */
    virtual void on_proxy_unpublish();
winlin authored
210 211
};
212
#endif