Blame view

trunk/src/rtmp/srs_protocol_rtmp.hpp 17.4 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
*/

#include <srs_core.hpp>

#include <string>
35 36
#include <srs_protocol_stack.hpp>
37
class SrsProtocol;
38
class ISrsProtocolReaderWriter;
39 40 41 42 43 44
class ISrsMessage;
class SrsCommonMessage;
class SrsCreateStreamPacket;
class SrsFMLEStartPacket;
class SrsPublishPacket;
class SrsOnMetaDataPacket;
45
class SrsPlayPacket;
46
class SrsMessage;
47
class SrsPacket;
48
class SrsAmf0Object;
49 50 51 52

/**
* the original request from client.
*/
53
class SrsRequest
54
{
55
public:
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;
66 67 68
// data discovery from request.
public:
    // discovery from tcUrl and play/publish.
69
    std::string schema;
70
    // the vhost in tcUrl.
71
    std::string vhost;
72
    // the host in tcUrl.
73
    std::string host;
74
    // the port in tcUrl.
75
    std::string port;
76
    // the app in tcUrl, without param.
77
    std::string app;
78 79 80
    // the param in tcUrl(app).
    std::string param;
    // the stream in play/publish
81
    std::string stream;
82 83 84 85 86
    // 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;
87 88 89 90
    // the token in the connect request,
    // used for edge traverse to origin authentication,
    // @see https://github.com/winlinvip/simple-rtmp-server/issues/104
    SrsAmf0Object* args;
91
public:
92 93
    SrsRequest();
    virtual ~SrsRequest();
94
public:
95 96 97 98 99 100
    /**
    * 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();
101 102 103 104 105 106
    /**
    * update the auth info of request,
    * to keep the current request ptr is ok,
    * for many components use the ptr of request.
    */
    virtual void update_auth(SrsRequest* req);
107
    /**
108
    * get the stream identify, vhost/app/stream.
109 110
    */
    virtual std::string get_stream_url();
111 112 113
    /**
    * strip url, user must strip when update the url.
    */
114
    virtual void strip();
115 116 117 118 119
};

/**
* the response to client.
*/
120
class SrsResponse
121
{
122
public:
123 124 125
    /**
    * the stream id to response client createStream.
    */
126
    int stream_id;
127
public:
128 129
    SrsResponse();
    virtual ~SrsResponse();
130 131 132 133 134
};

/**
* the rtmp client type.
*/
135
enum SrsRtmpConnType
136
{
137 138 139 140
    SrsRtmpConnUnknown,
    SrsRtmpConnPlay,
    SrsRtmpConnFMLEPublish,
    SrsRtmpConnFlashPublish,
141
};
142
std::string srs_client_type_string(SrsRtmpConnType type);
143 144

/**
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
* 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();
165
    virtual int create_s0s1s2(const char* c1 = NULL);
166 167 168 169
    virtual int create_c2();
};

/**
170 171 172 173
* implements the client role protocol.
*/
class SrsRtmpClient
{
174 175
private:
    SrsHandshakeBytes* hs_bytes;
176
protected:
177 178
    SrsProtocol* protocol;
    ISrsProtocolReaderWriter* io;
179
public:
180 181
    SrsRtmpClient(ISrsProtocolReaderWriter* skt);
    virtual ~SrsRtmpClient();
182
// protocol methods proxy
183
public:
184 185 186 187
    /**
    * set the recv timeout in us.
    * if timeout, recv/send message return ERROR_SOCKET_TIMEOUT.
    */
188
    virtual void set_recv_timeout(int64_t timeout_us);
189 190 191 192
    /**
    * set the send timeout in us.
    * if timeout, recv/send message return ERROR_SOCKET_TIMEOUT.
    */
193
    virtual void set_send_timeout(int64_t timeout_us);
194 195 196
    /**
    * get recv/send bytes.
    */
197 198
    virtual int64_t get_recv_bytes();
    virtual int64_t get_send_bytes();
199 200 201 202 203 204 205 206 207
    /**
    * recv a RTMP message, which is bytes oriented.
    * user can use decode_message to get the decoded RTMP packet.
    * @param pmsg, set the received message, 
    *       always NULL if error, 
    *       NULL for unknown packet but return success.
    *       never NULL if decode success.
    * @remark, drop message when msg is empty or payload length is empty.
    */
208
    virtual int recv_message(SrsMessage** pmsg);
209 210 211 212 213 214
    /**
    * decode bytes oriented RTMP message to RTMP packet,
    * @param ppacket, output decoded packet, 
    *       always NULL if error, never NULL if success.
    * @return error when unknown packet, error when decode failed.
    */
215
    virtual int decode_message(SrsMessage* msg, SrsPacket** ppacket);
216 217 218 219 220 221 222
    /**
    * send the RTMP message and always free it.
    * user must never free or use the msg after this method,
    * for it will always free the msg.
    * @param msg, the msg to send out, never be NULL.
    * @param stream_id, the stream id of packet to send over, 0 for control message.
    */
223
    virtual int send_and_free_message(SrsMessage* msg, int stream_id);
224
    /**
225 226 227 228 229 230 231 232 233
    * send the RTMP message and always free it.
    * user must never free or use the msg after this method,
    * for it will always free the msg.
    * @param msgs, the msgs to send out, never be NULL.
    * @param nb_msgs, the size of msgs to send out.
    * @param stream_id, the stream id of packet to send over, 0 for control message.
    */
    virtual int send_and_free_messages(SrsMessage** msgs, int nb_msgs, int stream_id);
    /**
234 235 236 237 238 239
    * send the RTMP packet and always free it.
    * user must never free or use the packet after this method,
    * for it will always free the packet.
    * @param packet, the packet to send out, never be NULL.
    * @param stream_id, the stream id of packet to send over, 0 for control message.
    */
240
    virtual int send_and_free_packet(SrsPacket* packet, int stream_id);
241
public:
242 243 244
    /**
    * handshake with server, try complex, then simple handshake.
    */
245
    virtual int handshake();
246 247 248
    /**
    * only use simple handshake
    */
249
    virtual int simple_handshake();
250 251 252
    /**
    * only use complex handshake
    */
253
    virtual int complex_handshake();
254 255 256 257 258
    /**
    * set req to use the original request of client:
    *      pageUrl and swfUrl for refer antisuck.
    *      args for edge to origin traverse auth, @see SrsRequest.args
    */
259
    virtual int connect_app(std::string app, std::string tc_url, 
260
        SrsRequest* req, bool debug_srs_upnode);
261
    /**
262 263 264 265 266 267 268 269 270
    * connect to server, get the debug srs info.
    * 
    * @param app, the app to connect at.
    * @param tc_url, the tcUrl to connect at.
    * @param req, the optional req object, use the swfUrl/pageUrl if specified. NULL to ignore.
    * 
    * SRS debug info:
    * @param srs_server_ip, debug info, server ip client connected at.
    * @param srs_server, server info.
271 272
    * @param srs_primary, primary authors.
    * @param srs_authors, authors.
273 274 275 276
    * @param srs_id, int, debug info, client id in server log.
    * @param srs_pid, int, debug info, server pid in log.
    */
    virtual int connect_app2(
277
        std::string app, std::string tc_url, SrsRequest* req, bool debug_srs_upnode,
278 279 280
        std::string& srs_server_ip, std::string& srs_server, std::string& srs_primary, 
        std::string& srs_authors, std::string& srs_version, int& srs_id, 
        int& srs_pid
281 282
    );
    /**
283 284
    * create a stream, then play/publish data over this stream.
    */
285
    virtual int create_stream(int& stream_id);
286 287 288
    /**
    * start play stream.
    */
289
    virtual int play(std::string stream, int stream_id);
290 291 292 293
    /**
    * start publish stream. use flash publish workflow:
    *       connect-app => create-stream => flash-publish
    */
294
    virtual int publish(std::string stream, int stream_id);
295 296 297 298
    /**
    * start publish stream. use FMLE publish workflow:
    *       connect-app => FMLE publish
    */
299
    virtual int fmle_publish(std::string stream, int& stream_id);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
public:
    /**
    * expect a specified message, drop others util got specified one.
    * @pmsg, user must free it. NULL if not success.
    * @ppacket, store in the pmsg, user must never free it. NULL if not success.
    * @remark, only when success, user can use and must free the pmsg/ppacket.
    * for example:
             SrsCommonMessage* msg = NULL;
            SrsConnectAppResPacket* pkt = NULL;
            if ((ret = srs_rtmp_expect_message<SrsConnectAppResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
                return ret;
            }
            // use pkt
    * user should never recv message and convert it, use this method instead.
    * if need to set timeout, use set timeout of SrsProtocol.
    */
    template<class T>
    int expect_message(SrsMessage** pmsg, T** ppacket)
    {
        return protocol->expect_message<T>(pmsg, ppacket);
    }
321 322 323 324 325 326 327
};

/**
* 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.
*/
328
class SrsRtmpServer
329 330
{
private:
331
    SrsHandshakeBytes* hs_bytes;
332 333
    SrsProtocol* protocol;
    ISrsProtocolReaderWriter* io;
334
public:
335 336
    SrsRtmpServer(ISrsProtocolReaderWriter* skt);
    virtual ~SrsRtmpServer();
337
// protocol methods proxy
338
public:
339
    /**
340 341 342 343 344 345
    * set the auto response message when recv for protocol stack.
    * @param v, whether auto response message when recv message.
    * @see: https://github.com/winlinvip/simple-rtmp-server/issues/217
    */
    virtual void set_auto_response(bool v);
    /**
346 347 348
    * set/get the recv timeout in us.
    * if timeout, recv/send message return ERROR_SOCKET_TIMEOUT.
    */
349 350
    virtual void set_recv_timeout(int64_t timeout_us);
    virtual int64_t get_recv_timeout();
351 352 353 354
    /**
    * set/get the send timeout in us.
    * if timeout, recv/send message return ERROR_SOCKET_TIMEOUT.
    */
355 356
    virtual void set_send_timeout(int64_t timeout_us);
    virtual int64_t get_send_timeout();
357 358 359
    /**
    * get recv/send bytes.
    */
360 361
    virtual int64_t get_recv_bytes();
    virtual int64_t get_send_bytes();
362 363 364 365 366 367 368 369 370
    /**
    * recv a RTMP message, which is bytes oriented.
    * user can use decode_message to get the decoded RTMP packet.
    * @param pmsg, set the received message, 
    *       always NULL if error, 
    *       NULL for unknown packet but return success.
    *       never NULL if decode success.
    * @remark, drop message when msg is empty or payload length is empty.
    */
371
    virtual int recv_message(SrsMessage** pmsg);
372 373 374 375 376 377
    /**
    * decode bytes oriented RTMP message to RTMP packet,
    * @param ppacket, output decoded packet, 
    *       always NULL if error, never NULL if success.
    * @return error when unknown packet, error when decode failed.
    */
378
    virtual int decode_message(SrsMessage* msg, SrsPacket** ppacket);
379 380 381 382 383 384 385
    /**
    * send the RTMP message and always free it.
    * user must never free or use the msg after this method,
    * for it will always free the msg.
    * @param msg, the msg to send out, never be NULL.
    * @param stream_id, the stream id of packet to send over, 0 for control message.
    */
386
    virtual int send_and_free_message(SrsMessage* msg, int stream_id);
387
    /**
388 389 390 391 392 393
    * send the RTMP message and always free it.
    * user must never free or use the msg after this method,
    * for it will always free the msg.
    * @param msgs, the msgs to send out, never be NULL.
    * @param nb_msgs, the size of msgs to send out.
    * @param stream_id, the stream id of packet to send over, 0 for control message.
394 395 396
    *
    * @remark performance issue, to support 6k+ 250kbps client,
    *       @see https://github.com/winlinvip/simple-rtmp-server/issues/194
397
    */
398
    virtual int send_and_free_messages(SrsMessage** msgs, int nb_msgs, int stream_id);
399
    /**
400 401 402 403 404 405
    * send the RTMP packet and always free it.
    * user must never free or use the packet after this method,
    * for it will always free the packet.
    * @param packet, the packet to send out, never be NULL.
    * @param stream_id, the stream id of packet to send over, 0 for control message.
    */
406
    virtual int send_and_free_packet(SrsPacket* packet, int stream_id);
407
public:
408 409 410
    /**
    * handshake with client, try complex then simple.
    */
411
    virtual int handshake();
412 413 414
    /**
    * do connect app with client, to discovery tcUrl.
    */
415
    virtual int connect_app(SrsRequest* req);
416 417 418
    /**
    * set ack size to client, client will send ack-size for each ack window
    */
419 420 421 422 423 424 425 426 427
    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.
    */
428
    virtual int response_connect_app(SrsRequest* req, const char* server_ip = NULL);
429 430 431
    /**
    * reject the connect app request.
    */
432
    virtual void response_connect_reject(SrsRequest* req, const char* desc);
433 434 435
    /**
    * response client the onBWDone message.
    */
436 437 438 439 440 441
    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.
442 443
    * @stream_name, output the client publish/play stream name. @see: SrsRequest.stream
    * @duration, output the play client duration. @see: SrsRequest.duration
444
    */
445
    virtual int identify_client(int stream_id, SrsRtmpConnType& type, std::string& stream_name, double& duration);
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    /**
    * 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);
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
public:
    /**
    * expect a specified message, drop others util got specified one.
    * @pmsg, user must free it. NULL if not success.
    * @ppacket, store in the pmsg, user must never free it. NULL if not success.
    * @remark, only when success, user can use and must free the pmsg/ppacket.
    * for example:
             SrsCommonMessage* msg = NULL;
            SrsConnectAppResPacket* pkt = NULL;
            if ((ret = srs_rtmp_expect_message<SrsConnectAppResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
                return ret;
            }
            // use pkt
    * user should never recv message and convert it, use this method instead.
    * if need to set timeout, use set timeout of SrsProtocol.
    */
    template<class T>
    int expect_message(SrsMessage** pmsg, T** ppacket)
    {
        return protocol->expect_message<T>(pmsg, ppacket);
    }
509
private:
510
    virtual int identify_create_stream_client(SrsCreateStreamPacket* req, int stream_id, SrsRtmpConnType& type, std::string& stream_name, double& duration);
511 512
    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);
513
private:
514
    virtual int identify_play_client(SrsPlayPacket* req, SrsRtmpConnType& type, std::string& stream_name, double& duration);
515 516
};
517
#endif
518