Blame view

trunk/src/rtmp/srs_protocol_rtmp.hpp 9.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
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.
*/
24 25
#ifndef SRS_RTMP_PROTOCOL_RTMP_HPP
#define SRS_RTMP_PROTOCOL_RTMP_HPP
26 27

/*
28
#include <srs_protocol_rtmp.hpp>
29 30 31 32 33 34 35
*/

#include <srs_core.hpp>

#include <string>

class SrsProtocol;
36
class ISrsProtocolReaderWriter;
37 38 39 40 41 42
class ISrsMessage;
class SrsCommonMessage;
class SrsCreateStreamPacket;
class SrsFMLEStartPacket;
class SrsPublishPacket;
class SrsOnMetaDataPacket;
43
class SrsPlayPacket;
44
class SrsMessage;
45
class SrsPacket;
46 47 48 49

/**
* the original request from client.
*/
50
class SrsRequest
51
{
52
public:
53 54 55 56 57 58 59 60 61 62 63 64 65
    /**
    * tcUrl: rtmp://request_vhost:port/app/stream
    * support pass vhost in query string, such as:
    *    rtmp://ip:port/app?vhost=request_vhost/stream
    *    rtmp://ip:port/app...vhost...request_vhost/stream
    */
    std::string tcUrl;
    std::string pageUrl;
    std::string swfUrl;
    double objectEncoding;
    
    std::string schema;
    std::string vhost;
66
    std::string host;
67 68 69 70
    std::string port;
    std::string app;
    std::string stream;
    
71 72 73 74 75 76
    // for play live stream, 
    // used to specified the stop when exceed the duration.
    // @see https://github.com/winlinvip/simple-rtmp-server/issues/45
    // in ms.
    double duration;
    
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    SrsRequest();
    virtual ~SrsRequest();

    /**
    * deep copy the request, for source to use it to support reload,
    * for when initialize the source, the request is valid,
    * when reload it, the request maybe invalid, so need to copy it.
    */
    virtual SrsRequest* copy();
    
    /**
    * disconvery vhost/app from tcUrl.
    */
    virtual int discovery_app();
    virtual std::string get_stream_url();
92 93 94
    
    // strip url, user must strip when update the url.
    virtual void strip();
95 96 97 98 99 100 101
};

/**
* the response to client.
*/
struct SrsResponse
{
102 103 104 105
    int stream_id;
    
    SrsResponse();
    virtual ~SrsResponse();
106 107 108 109 110
};

/**
* the rtmp client type.
*/
111
enum SrsRtmpConnType
112
{
113 114 115 116
    SrsRtmpConnUnknown,
    SrsRtmpConnPlay,
    SrsRtmpConnFMLEPublish,
    SrsRtmpConnFlashPublish,
117
};
118
std::string srs_client_type_string(SrsRtmpConnType type);
119 120

/**
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
* store the handshake bytes, 
* for smart switch between complex and simple handshake.
*/
class SrsHandshakeBytes
{
public:
    // [1+1536]
    char* c0c1;
    // [1+1536+1536]
    char* s0s1s2;
    // [1536]
    char* c2;
public:
    SrsHandshakeBytes();
    virtual ~SrsHandshakeBytes();
public:
    virtual int read_c0c1(ISrsProtocolReaderWriter* io);
    virtual int read_s0s1s2(ISrsProtocolReaderWriter* io);
    virtual int read_c2(ISrsProtocolReaderWriter* io);
    virtual int create_c0c1();
141
    virtual int create_s0s1s2(const char* c1 = NULL);
142 143 144 145
    virtual int create_c2();
};

/**
146 147 148 149
* implements the client role protocol.
*/
class SrsRtmpClient
{
150 151
private:
    SrsHandshakeBytes* hs_bytes;
152
protected:
153 154
    SrsProtocol* protocol;
    ISrsProtocolReaderWriter* io;
155
public:
156 157
    SrsRtmpClient(ISrsProtocolReaderWriter* skt);
    virtual ~SrsRtmpClient();
158
public:
159
    virtual SrsProtocol* get_protocol();
160 161 162 163 164 165
    virtual void set_recv_timeout(int64_t timeout_us);
    virtual void set_send_timeout(int64_t timeout_us);
    virtual int64_t get_recv_bytes();
    virtual int64_t get_send_bytes();
    virtual int get_recv_kbps();
    virtual int get_send_kbps();
166 167 168 169
    virtual int recv_message(SrsMessage** pmsg);
    virtual int decode_message(SrsMessage* msg, SrsPacket** ppacket);
    virtual int send_and_free_message(SrsMessage* msg);
    virtual int send_and_free_packet(SrsPacket* packet, int stream_id);
170
public:
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    // try complex, then simple handshake.
    virtual int handshake();
    // only use simple handshake
    virtual int simple_handshake();
    // only use complex handshake
    virtual int complex_handshake();
    virtual int connect_app(std::string app, std::string tc_url);
    virtual int create_stream(int& stream_id);
    virtual int play(std::string stream, int stream_id);
    // flash publish schema:
    // connect-app => create-stream => flash-publish
    virtual int publish(std::string stream, int stream_id);
    // FMLE publish schema:
    // connect-app => FMLE publish
    virtual int fmle_publish(std::string stream, int& stream_id);
186 187 188 189 190 191 192
};

/**
* the rtmp provices rtmp-command-protocol services,
* a high level protocol, media stream oriented services,
* such as connect to vhost/app, play stream, get audio/video data.
*/
193
// TODO: FIXME: rename to SrsRtmpServer
194
class SrsRtmpServer
195 196
{
private:
197
    SrsHandshakeBytes* hs_bytes;
198 199
    SrsProtocol* protocol;
    ISrsProtocolReaderWriter* io;
200
public:
201 202
    SrsRtmpServer(ISrsProtocolReaderWriter* skt);
    virtual ~SrsRtmpServer();
203
public:
204 205 206 207 208 209 210 211 212
    virtual SrsProtocol* get_protocol();
    virtual void set_recv_timeout(int64_t timeout_us);
    virtual int64_t get_recv_timeout();
    virtual void set_send_timeout(int64_t timeout_us);
    virtual int64_t get_send_timeout();
    virtual int64_t get_recv_bytes();
    virtual int64_t get_send_bytes();
    virtual int get_recv_kbps();
    virtual int get_send_kbps();
213 214 215 216
    virtual int recv_message(SrsMessage** pmsg);
    virtual int decode_message(SrsMessage* msg, SrsPacket** ppacket);
    virtual int send_and_free_message(SrsMessage* msg);
    virtual int send_and_free_packet(SrsPacket* packet, int stream_id);
217
public:
218 219 220 221 222 223 224 225 226 227 228
    virtual int handshake();
    virtual int connect_app(SrsRequest* req);
    virtual int set_window_ack_size(int ack_size);
    /**
    * @type: The sender can mark this message hard (0), soft (1), or dynamic (2)
    * using the Limit type field.
    */
    virtual int set_peer_bandwidth(int bandwidth, int type);
    /**
    * @param server_ip the ip of server.
    */
229 230
    virtual int response_connect_app(SrsRequest* req, const char* server_ip = NULL);
    virtual void response_connect_reject(SrsRequest* req, const char* desc);
231 232 233 234 235 236
    virtual int on_bw_done();
    /**
    * recv some message to identify the client.
    * @stream_id, client will createStream to play or publish by flash, 
    *         the stream_id used to response the createStream request.
    * @type, output the client type.
237 238
    * @stream_name, output the client publish/play stream name. @see: SrsRequest.stream
    * @duration, output the play client duration. @see: SrsRequest.duration
239
    */
240
    virtual int identify_client(int stream_id, SrsRtmpConnType& type, std::string& stream_name, double& duration);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    /**
    * set the chunk size when client type identified.
    */
    virtual int set_chunk_size(int chunk_size);
    /**
    * when client type is play, response with packets:
    * StreamBegin, 
    * onStatus(NetStream.Play.Reset), onStatus(NetStream.Play.Start).,
    * |RtmpSampleAccess(false, false),
    * onStatus(NetStream.Data.Start).
    */
    virtual int start_play(int stream_id);
    /**
    * when client(type is play) send pause message,
    * if is_pause, response the following packets:
    *     onStatus(NetStream.Pause.Notify)
    *     StreamEOF
    * if not is_pause, response the following packets:
    *     onStatus(NetStream.Unpause.Notify)
    *     StreamBegin
    */
    virtual int on_play_client_pause(int stream_id, bool is_pause);
    /**
    * when client type is publish, response with packets:
    * releaseStream response
    * FCPublish
    * FCPublish response
    * createStream response
    * onFCPublish(NetStream.Publish.Start)
    * onStatus(NetStream.Publish.Start)
    */
    virtual int start_fmle_publish(int stream_id);
    /**
    * process the FMLE unpublish event.
    * @unpublish_tid the unpublish request transaction id.
    */
    virtual int fmle_unpublish(int stream_id, double unpublish_tid);
    /**
    * when client type is publish, response with packets:
    * onStatus(NetStream.Publish.Start)
    */
    virtual int start_flash_publish(int stream_id);
283
private:
284
    virtual int identify_create_stream_client(SrsCreateStreamPacket* req, int stream_id, SrsRtmpConnType& type, std::string& stream_name, double& duration);
285 286
    virtual int identify_fmle_publish_client(SrsFMLEStartPacket* req, SrsRtmpConnType& type, std::string& stream_name);
    virtual int identify_flash_publish_client(SrsPublishPacket* req, SrsRtmpConnType& type, std::string& stream_name);
287
private:
288
    virtual int identify_play_client(SrsPlayPacket* req, SrsRtmpConnType& type, std::string& stream_name, double& duration);
289 290
};
291
#endif