Blame view

trunk/src/rtmp/srs_protocol_rtmp_stack.hpp 33.4 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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_STACK_HPP
#define SRS_RTMP_PROTOCOL_RTMP_STACK_HPP
winlin authored
26 27

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

#include <srs_core.hpp>

#include <map>
#include <string>
36
#include <srs_kernel_log.hpp>
37
#include <srs_kernel_error.hpp>
38
 
39 40 41 42 43
// the following is the timeout for rtmp protocol, 
// to avoid death connection.

// when got a messae header, there must be some data,
// increase recv timeout to got an entire message.
44
#define SRS_MIN_RECV_TIMEOUT_US (int64_t)(60*1000*1000LL)
45 46 47 48

// the timeout to wait for client control message,
// if timeout, we generally ignore and send the data to client,
// generally, it's the pulse time for data seding.
49
#define SRS_PULSE_TIMEOUT_US (int64_t)(200*1000LL)
50 51 52

// the timeout to wait client data,
// if timeout, close the connection.
53
#define SRS_SEND_TIMEOUT_US (int64_t)(30*1000*1000LL)
54 55 56

// the timeout to send data to client,
// if timeout, close the connection.
57
#define SRS_RECV_TIMEOUT_US (int64_t)(30*1000*1000LL)
58
59 60
// the timeout to wait client data, when client paused
// if timeout, close the connection.
61
#define SRS_PAUSED_SEND_TIMEOUT_US (int64_t)(30*60*1000*1000LL)
62
// if timeout, close the connection.
63
#define SRS_PAUSED_RECV_TIMEOUT_US (int64_t)(30*60*1000*1000LL)
64
65 66 67 68 69 70
// the timeout to wait encoder to republish
// if timeout, close the connection.
#define SRS_REPUBLISH_SEND_TIMEOUT_US (int64_t)(3*60*1000*1000LL)
// if timeout, close the connection.
#define SRS_REPUBLISH_RECV_TIMEOUT_US (int64_t)(3*60*1000*1000LL)
71 72 73
// when stream is busy, for example, streaming is already
// publishing, when a new client to request to publish,
// sleep a while and close the connection.
74
#define SRS_STREAM_BUSY_SLEEP_US (int64_t)(3*1000*1000LL)
75 76

// when error, forwarder sleep for a while and retry.
77
#define SRS_FORWARDER_SLEEP_US (int64_t)(3*1000*1000LL)
78 79

// when error, encoder sleep for a while and retry.
80
#define SRS_ENCODER_SLEEP_US (int64_t)(3*1000*1000LL)
81
82
class ISrsProtocolReaderWriter;
winlin authored
83 84 85 86 87 88
class SrsBuffer;
class SrsPacket;
class SrsStream;
class SrsCommonMessage;
class SrsChunkStream;
class SrsAmf0Object;
89
class SrsAmf0Any;
winlin authored
90 91 92 93 94 95 96
class ISrsMessage;

// convert class name to string.
#define CLASS_NAME_STRING(className) #className

/**
* max rtmp header size:
97 98 99
*     1bytes basic header,
*     11bytes message header,
*     4bytes timestamp header,
winlin authored
100 101 102 103 104
* that is, 1+11+4=16bytes.
*/
#define RTMP_MAX_FMT0_HEADER_SIZE 16
/**
* max rtmp header size:
105 106
*     1bytes basic header,
*     4bytes timestamp header,
winlin authored
107 108 109 110 111 112 113 114 115 116 117 118
* that is, 1+4=5bytes.
*/
#define RTMP_MAX_FMT3_HEADER_SIZE 5

/**
* the protocol provides the rtmp-message-protocol services,
* to recv RTMP message from RTMP chunk stream,
* and to send out RTMP message over RTMP chunk stream.
*/
class SrsProtocol
{
private:
119 120 121 122 123 124 125
    struct AckWindowSize
    {
        int ack_window_size;
        int64_t acked_size;
        
        AckWindowSize();
    };
winlin authored
126 127
// peer in/out
private:
128 129 130 131 132 133 134 135
    ISrsProtocolReaderWriter* skt;
    char* pp;
    /**
    * requests sent out, used to build the response.
    * key: transactionId
    * value: the request command name
    */
    std::map<double, std::string> requests;
winlin authored
136 137
// peer in
private:
138 139 140 141
    std::map<int, SrsChunkStream*> chunk_streams;
    SrsBuffer* buffer;
    int32_t in_chunk_size;
    AckWindowSize in_ack_size;
winlin authored
142 143
// peer out
private:
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    char out_header_fmt0[RTMP_MAX_FMT0_HEADER_SIZE];
    char out_header_fmt3[RTMP_MAX_FMT3_HEADER_SIZE];
    int32_t out_chunk_size;
public:
    /**
    * use io to create the protocol stack,
    * @param io, provides io interfaces, user must free it.
    */
    SrsProtocol(ISrsProtocolReaderWriter* io);
    virtual ~SrsProtocol();
public:
    std::string get_request_name(double transcationId);
    /**
    * set the timeout in us.
    * if timeout, recv/send message return ERROR_SOCKET_TIMEOUT.
    */
    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();
    /**
    * recv a message with raw/undecoded payload from peer.
    * the payload is not decoded, use srs_rtmp_expect_message<T> if requires 
    * specifies message.
    * @pmsg, user must free it. NULL if not success.
    * @remark, only when success, user can use and must free the pmsg.
    */
    virtual int recv_message(SrsCommonMessage** pmsg);
    /**
    * send out message with encoded payload to peer.
    * use the message encode method to encode to payload,
    * then sendout over socket.
    * @msg this method will free it whatever return value.
    */
    virtual int send_message(ISrsMessage* msg);
winlin authored
183
private:
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    /**
    * when recv message, update the context.
    */
    virtual int on_recv_message(SrsCommonMessage* msg);
    virtual int response_acknowledgement_message();
    virtual int response_ping_message(int32_t timestamp);
    /**
    * when message sentout, update the context.
    */
    virtual int on_send_message(ISrsMessage* msg);
    /**
    * try to recv interlaced message from peer,
    * return error if error occur and nerver set the pmsg,
    * return success and pmsg set to NULL if no entire message got,
    * return success and pmsg set to entire message if got one.
    */
    virtual int recv_interlaced_message(SrsCommonMessage** pmsg);
    /**
    * read the chunk basic header(fmt, cid) from chunk stream.
    * user can discovery a SrsChunkStream by cid.
    * @bh_size return the chunk basic header size, to remove the used bytes when finished.
    */
    virtual int read_basic_header(char& fmt, int& cid, int& bh_size);
    /**
    * read the chunk message header(timestamp, payload_length, message_type, stream_id) 
    * from chunk stream and save to SrsChunkStream.
    * @mh_size return the chunk message header size, to remove the used bytes when finished.
    */
    virtual int read_message_header(SrsChunkStream* chunk, char fmt, int bh_size, int& mh_size);
    /**
    * read the chunk payload, remove the used bytes in buffer,
    * if got entire message, set the pmsg.
    * @payload_size read size in this roundtrip, generally a chunk size or left message size.
    */
    virtual int read_message_payload(SrsChunkStream* chunk, int bh_size, int mh_size, int& payload_size, SrsCommonMessage** pmsg);
winlin authored
219 220 221 222 223 224 225
};

/**
* 4.1. Message Header
*/
struct SrsMessageHeader
{
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    /**
    * One byte field to represent the message type. A range of type IDs
    * (1-7) are reserved for protocol control messages.
    */
    int8_t message_type;
    /**
    * Three-byte field that represents the size of the payload in bytes.
    * It is set in big-endian format.
    */
    int32_t payload_length;
    /**
    * Three-byte field that contains a timestamp delta of the message.
    * The 4 bytes are packed in the big-endian order.
    * @remark, only used for decoding message from chunk stream.
    */
    int32_t timestamp_delta;
    /**
    * Three-byte field that identifies the stream of the message. These
    * bytes are set in big-endian format.
    */
    int32_t stream_id;
    
    /**
    * Four-byte field that contains a timestamp of the message.
    * The 4 bytes are packed in the big-endian order.
    * @remark, used as calc timestamp when decode and encode time.
    * @remark, we use 64bits for large time for jitter detect and hls.
    */
    int64_t timestamp;
    
    SrsMessageHeader();
    virtual ~SrsMessageHeader();
winlin authored
258
259 260 261 262 263 264 265 266 267 268 269 270 271
    bool is_audio();
    bool is_video();
    bool is_amf0_command();
    bool is_amf0_data();
    bool is_amf3_command();
    bool is_amf3_data();
    bool is_window_ackledgement_size();
    bool is_set_chunk_size();
    bool is_user_control_message();
    
    void initialize_amf0_script(int size, int stream);
    void initialize_audio(int size, u_int32_t time, int stream);
    void initialize_video(int size, u_int32_t time, int stream);
winlin authored
272 273 274 275 276 277 278 279 280
};

/**
* incoming chunk stream maybe interlaced,
* use the chunk stream to cache the input RTMP chunk streams.
*/
class SrsChunkStream
{
public:
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    /**
    * represents the basic header fmt,
    * which used to identify the variant message header type.
    */
    char fmt;
    /**
    * represents the basic header cid,
    * which is the chunk stream id.
    */
    int cid;
    /**
    * cached message header
    */
    SrsMessageHeader header;
    /**
    * whether the chunk message header has extended timestamp.
    */
    bool extended_timestamp;
    /**
    * partially read message.
    */
    SrsCommonMessage* msg;
    /**
    * decoded msg count, to identify whether the chunk stream is fresh.
    */
    int64_t msg_count;
public:
    SrsChunkStream(int _cid);
    virtual ~SrsChunkStream();
winlin authored
310 311 312 313 314 315 316 317 318
};

/**
* message to output.
*/
class ISrsMessage
{
// 4.1. Message Header
public:
319
    SrsMessageHeader header;
winlin authored
320 321
// 4.2. Message Payload
public:
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    /**
    * The other part which is the payload is the actual data that is
    * contained in the message. For example, it could be some audio samples
    * or compressed video data. The payload format and interpretation are
    * beyond the scope of this document.
    */
    int32_t size;
    int8_t* payload;
public:
    ISrsMessage();
    virtual ~ISrsMessage();
public:
    /**
    * whether message canbe decoded.
    * only update the context when message canbe decoded.
    */
    virtual bool can_decode() = 0;
winlin authored
339 340 341 342
/**
* encode functions.
*/
public:
343 344 345 346 347 348 349 350 351
    /**
    * get the perfered cid(chunk stream id) which sendout over.
    */
    virtual int get_perfer_cid() = 0;
    /**
    * encode the packet to message payload bytes.
    * @remark there exists empty packet, so maybe the payload is NULL.
    */
    virtual int encode_packet() = 0;
winlin authored
352 353 354 355 356 357 358 359 360
};

/**
* common RTMP message defines in rtmp.part2.Message-Formats.pdf.
* cannbe parse and decode.
*/
class SrsCommonMessage : public ISrsMessage
{
private:
361 362
    typedef ISrsMessage super;
    disable_default_copy(SrsCommonMessage);
winlin authored
363 364
// decoded message payload.
private:
365 366
    SrsStream* stream;
    SrsPacket* packet;
winlin authored
367
public:
368 369
    SrsCommonMessage();
    virtual ~SrsCommonMessage();
winlin authored
370
public:
371
    virtual bool can_decode();
winlin authored
372 373 374 375
/**
* decode functions.
*/
public:
376 377 378 379 380 381 382 383 384 385
    /**
    * decode packet from message payload.
    */
    // TODO: use protocol to decode it.
    virtual int decode_packet(SrsProtocol* protocol);
    /**
    * get the decoded packet which decoded by decode_packet().
    * @remark, user never free the pkt, the message will auto free it.
    */
    virtual SrsPacket* get_packet();
winlin authored
386 387 388 389
/**
* encode functions.
*/
public:
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    /**
    * get the perfered cid(chunk stream id) which sendout over.
    */
    virtual int get_perfer_cid();
    /**
    * set the encoded packet to encode_packet() to payload.
    * @stream_id, the id of stream which is created by createStream.
    * @remark, user never free the pkt, the message will auto free it.
    * @return message itself.
    */
    // TODO: refine the send methods.
    virtual SrsCommonMessage* set_packet(SrsPacket* pkt, int stream_id);
    /**
    * encode the packet to message payload bytes.
    * @remark there exists empty packet, so maybe the payload is NULL.
    */
    virtual int encode_packet();
winlin authored
407 408 409 410 411 412 413 414 415 416
};

/**
* shared ptr message.
* for audio/video/data message that need less memory copy.
* and only for output.
*/
class SrsSharedPtrMessage : public ISrsMessage
{
private:
417
    typedef ISrsMessage super;
winlin authored
418
private:
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    struct SrsSharedPtr
    {
        char* payload;
        int size;
        int perfer_cid;
        int shared_count;
        
        SrsSharedPtr();
        virtual ~SrsSharedPtr();
    };
    SrsSharedPtr* ptr;
public:
    SrsSharedPtrMessage();
    virtual ~SrsSharedPtrMessage();
public:
    virtual bool can_decode();
public:
    /**
    * set the shared payload.
    * we will detach the payload of source,
    * so ensure donot use it before.
    */
    virtual int initialize(SrsCommonMessage* source);
    /**
    * set the shared payload.
    * use source header, and specified param payload.
    */
    virtual int initialize(SrsMessageHeader* source, char* payload, int size);
    virtual SrsSharedPtrMessage* copy();
public:
    /**
    * get the perfered cid(chunk stream id) which sendout over.
    */
    virtual int get_perfer_cid();
    /**
    * ignored.
    * for shared message, nothing should be done.
    * use initialize() to set the data.
    */
    virtual int encode_packet();
winlin authored
459 460 461 462 463
};

/**
* the decoded message payload.
* @remark we seperate the packet from message,
464 465 466 467
*        for the packet focus on logic and domain data,
*        the message bind to the protocol and focus on protocol, such as header.
*         we can merge the message and packet, using OOAD hierachy, packet extends from message,
*         it's better for me to use components -- the message use the packet as payload.
winlin authored
468 469 470 471
*/
class SrsPacket
{
protected:
472 473 474 475 476 477 478 479 480 481
    /**
    * subpacket must override to provide the right class name.
    */
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsPacket);
    }
public:
    SrsPacket();
    virtual ~SrsPacket();
winlin authored
482 483 484 485
/**
* decode functions.
*/
public:
486 487 488 489 490
    /**
    * subpacket must override to decode packet from stream.
    * @remark never invoke the super.decode, it always failed.
    */
    virtual int decode(SrsStream* stream);
winlin authored
491 492 493 494
/**
* encode functions.
*/
public:
495 496 497 498 499 500 501 502 503 504 505 506 507 508
    virtual int get_perfer_cid();
    virtual int get_payload_length();
public:
    /**
    * subpacket must override to provide the right message type.
    */
    virtual int get_message_type();
    /**
    * the subpacket can override this encode,
    * for example, video and audio will directly set the payload withou memory copy,
    * other packet which need to serialize/encode to bytes by override the 
    * get_size and encode_packet.
    */
    virtual int encode(int& size, char*& payload);
winlin authored
509
protected:
510 511 512 513 514 515 516 517 518
    /**
    * subpacket can override to calc the packet size.
    */
    virtual int get_size();
    /**
    * subpacket can override to encode the payload to stream.
    * @remark never invoke the super.encode_packet, it always failed.
    */
    virtual int encode_packet(SrsStream* stream);
winlin authored
519 520 521 522 523 524 525 526 527 528
};

/**
* 4.1.1. connect
* The client sends the connect command to the server to request
* connection to a server application instance.
*/
class SrsConnectAppPacket : public SrsPacket
{
private:
529
    typedef SrsPacket super;
winlin authored
530
protected:
531 532 533 534
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsConnectAppPacket);
    }
winlin authored
535
public:
536 537 538
    std::string command_name;
    double transaction_id;
    SrsAmf0Object* command_object;
winlin authored
539
public:
540 541
    SrsConnectAppPacket();
    virtual ~SrsConnectAppPacket();
winlin authored
542
public:
543
    virtual int decode(SrsStream* stream);
winlin authored
544
public:
545
    virtual int get_perfer_cid();
winlin authored
546
public:
547
    virtual int get_message_type();
winlin authored
548
protected:
549 550
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
551 552 553 554 555 556 557
};
/**
* response for SrsConnectAppPacket.
*/
class SrsConnectAppResPacket : public SrsPacket
{
private:
558
    typedef SrsPacket super;
winlin authored
559
protected:
560 561 562 563
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsConnectAppResPacket);
    }
winlin authored
564
public:
565 566 567 568
    std::string command_name;
    double transaction_id;
    SrsAmf0Object* props;
    SrsAmf0Object* info;
winlin authored
569
public:
570 571
    SrsConnectAppResPacket();
    virtual ~SrsConnectAppResPacket();
winlin authored
572
public:
573
    virtual int decode(SrsStream* stream);
winlin authored
574
public:
575
    virtual int get_perfer_cid();
winlin authored
576
public:
577
    virtual int get_message_type();
winlin authored
578
protected:
579 580
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
581 582 583 584 585 586 587 588 589 590 591 592
};

/**
* 4.1.3. createStream
* The client sends this command to the server to create a logical
* channel for message communication The publishing of audio, video, and
* metadata is carried out over stream channel created using the
* createStream command.
*/
class SrsCreateStreamPacket : public SrsPacket
{
private:
593
    typedef SrsPacket super;
winlin authored
594
protected:
595 596 597 598
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsCreateStreamPacket);
    }
winlin authored
599
public:
600 601 602
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
winlin authored
603
public:
604 605
    SrsCreateStreamPacket();
    virtual ~SrsCreateStreamPacket();
winlin authored
606
public:
607
    virtual int decode(SrsStream* stream);
winlin authored
608
public:
609
    virtual int get_perfer_cid();
winlin authored
610
public:
611
    virtual int get_message_type();
winlin authored
612
protected:
613 614
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
615 616 617 618 619 620 621
};
/**
* response for SrsCreateStreamPacket.
*/
class SrsCreateStreamResPacket : public SrsPacket
{
private:
622
    typedef SrsPacket super;
winlin authored
623
protected:
624 625 626 627
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsCreateStreamResPacket);
    }
winlin authored
628
public:
629 630 631 632
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    double stream_id;
winlin authored
633
public:
634 635
    SrsCreateStreamResPacket(double _transaction_id, double _stream_id);
    virtual ~SrsCreateStreamResPacket();
winlin authored
636
public:
637
    virtual int decode(SrsStream* stream);
winlin authored
638
public:
639
    virtual int get_perfer_cid();
winlin authored
640
public:
641
    virtual int get_message_type();
winlin authored
642
protected:
643 644
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
645
};
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
/**
* client close stream packet.
*/
class SrsCloseStreamPacket : public SrsPacket
{
private:
    typedef SrsPacket super;
protected:
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsCloseStreamPacket);
    }
public:
    std::string command_name;
    double transaction_id;
661
    SrsAmf0Any* command_object; // null
662 663 664 665 666 667
public:
    SrsCloseStreamPacket();
    virtual ~SrsCloseStreamPacket();
public:
    virtual int decode(SrsStream* stream);
};
winlin authored
668 669 670 671 672 673 674

/**
* FMLE start publish: ReleaseStream/PublishStream
*/
class SrsFMLEStartPacket : public SrsPacket
{
private:
675
    typedef SrsPacket super;
winlin authored
676
protected:
677 678 679 680
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsFMLEStartPacket);
    }
winlin authored
681
public:
682 683 684 685
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    std::string stream_name;
winlin authored
686
public:
687 688
    SrsFMLEStartPacket();
    virtual ~SrsFMLEStartPacket();
winlin authored
689
public:
690
    virtual int decode(SrsStream* stream);
691
public:
692
    virtual int get_perfer_cid();
693
public:
694
    virtual int get_message_type();
695
protected:
696 697
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
698
public:
699 700
    static SrsFMLEStartPacket* create_release_stream(std::string stream);
    static SrsFMLEStartPacket* create_FC_publish(std::string stream);
winlin authored
701 702 703 704 705 706 707
};
/**
* response for SrsFMLEStartPacket.
*/
class SrsFMLEStartResPacket : public SrsPacket
{
private:
708
    typedef SrsPacket super;
winlin authored
709
protected:
710 711 712 713
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsFMLEStartResPacket);
    }
winlin authored
714
public:
715 716 717 718
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    SrsAmf0Any* args; // undefined
winlin authored
719
public:
720 721
    SrsFMLEStartResPacket(double _transaction_id);
    virtual ~SrsFMLEStartResPacket();
winlin authored
722
public:
723
    virtual int decode(SrsStream* stream);
724
public:
725
    virtual int get_perfer_cid();
winlin authored
726
public:
727
    virtual int get_message_type();
winlin authored
728
protected:
729 730
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
731 732 733 734 735 736 737 738 739 740 741 742
};

/**
* FMLE/flash publish
* 4.2.6. Publish
* The client sends the publish command to publish a named stream to the
* server. Using this name, any client can play this stream and receive
* the published audio, video, and data messages.
*/
class SrsPublishPacket : public SrsPacket
{
private:
743
    typedef SrsPacket super;
winlin authored
744
protected:
745 746 747 748
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsPublishPacket);
    }
winlin authored
749
public:
750 751 752 753 754 755
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    std::string stream_name;
    // optional, default to live.
    std::string type;
winlin authored
756
public:
757 758
    SrsPublishPacket();
    virtual ~SrsPublishPacket();
winlin authored
759
public:
760
    virtual int decode(SrsStream* stream);
winlin authored
761
public:
762
    virtual int get_perfer_cid();
winlin authored
763
public:
764
    virtual int get_message_type();
winlin authored
765
protected:
766 767
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
768 769 770 771 772 773 774 775 776 777
};

/**
* 4.2.8. pause
* The client sends the pause command to tell the server to pause or
* start playing.
*/
class SrsPausePacket : public SrsPacket
{
private:
778
    typedef SrsPacket super;
winlin authored
779
protected:
780 781 782 783
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsPausePacket);
    }
winlin authored
784
public:
785 786 787 788 789
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    bool is_pause;
    double time_ms;
winlin authored
790
public:
791 792
    SrsPausePacket();
    virtual ~SrsPausePacket();
winlin authored
793
public:
794
    virtual int decode(SrsStream* stream);
winlin authored
795 796 797 798 799 800 801 802 803
};

/**
* 4.2.1. play
* The client sends this command to the server to play a stream.
*/
class SrsPlayPacket : public SrsPacket
{
private:
804
    typedef SrsPacket super;
winlin authored
805
protected:
806 807 808 809
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsPlayPacket);
    }
winlin authored
810
public:
811 812 813 814 815 816 817
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    std::string stream_name;
    double start;
    double duration;
    bool reset;
winlin authored
818
public:
819 820
    SrsPlayPacket();
    virtual ~SrsPlayPacket();
winlin authored
821
public:
822
    virtual int decode(SrsStream* stream);
winlin authored
823
public:
824
    virtual int get_perfer_cid();
winlin authored
825
public:
826
    virtual int get_message_type();
winlin authored
827
protected:
828 829
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
830 831 832 833 834 835 836 837
};
/**
* response for SrsPlayPacket.
* @remark, user must set the stream_id in header.
*/
class SrsPlayResPacket : public SrsPacket
{
private:
838
    typedef SrsPacket super;
winlin authored
839
protected:
840 841 842 843
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsPlayResPacket);
    }
winlin authored
844
public:
845 846 847 848
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    SrsAmf0Object* desc;
winlin authored
849
public:
850 851
    SrsPlayResPacket();
    virtual ~SrsPlayResPacket();
winlin authored
852
public:
853
    virtual int get_perfer_cid();
winlin authored
854
public:
855
    virtual int get_message_type();
winlin authored
856
protected:
857 858
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
859 860 861 862 863 864 865 866
};

/**
* when bandwidth test done, notice client.
*/
class SrsOnBWDonePacket : public SrsPacket
{
private:
867
    typedef SrsPacket super;
winlin authored
868
protected:
869 870 871 872
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsOnBWDonePacket);
    }
winlin authored
873
public:
874 875 876
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* args; // null
winlin authored
877
public:
878 879
    SrsOnBWDonePacket();
    virtual ~SrsOnBWDonePacket();
winlin authored
880
public:
881
    virtual int get_perfer_cid();
winlin authored
882
public:
883
    virtual int get_message_type();
winlin authored
884
protected:
885 886
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
887 888 889 890 891 892 893 894 895
};

/**
* onStatus command, AMF0 Call
* @remark, user must set the stream_id by SrsMessage.set_packet().
*/
class SrsOnStatusCallPacket : public SrsPacket
{
private:
896
    typedef SrsPacket super;
winlin authored
897
protected:
898 899 900 901
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsOnStatusCallPacket);
    }
winlin authored
902
public:
903 904 905 906
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* args; // null
    SrsAmf0Object* data;
winlin authored
907
public:
908 909
    SrsOnStatusCallPacket();
    virtual ~SrsOnStatusCallPacket();
winlin authored
910
public:
911
    virtual int get_perfer_cid();
winlin authored
912
public:
913
    virtual int get_message_type();
winlin authored
914
protected:
915 916
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
917 918 919
};

/**
920 921 922 923 924 925 926 927
* the special packet for the bandwidth test.
* actually, it's a SrsOnStatusCallPacket, but
* 1. encode with data field, to send data to client.
* 2. decode ignore the data field, donot care.
*/
class SrsBandwidthPacket : public SrsPacket
{
private:
928 929
    typedef SrsPacket super;
    disable_default_copy(SrsBandwidthPacket);
930
protected:
931 932 933 934
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsBandwidthPacket);
    }
935
public:
936 937 938 939
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* args; // null
    SrsAmf0Object* data;
940
public:
941 942
    SrsBandwidthPacket();
    virtual ~SrsBandwidthPacket();
943
public:
944
    virtual int get_perfer_cid();
945
public:
946
    virtual int get_message_type();
947
protected:
948 949
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
950 951 952
public:
    virtual int decode(SrsStream* stream);
public:
953 954 955 956 957 958 959 960 961 962 963
    virtual bool is_starting_play();
    virtual bool is_stopped_play();
    virtual bool is_starting_publish();
    virtual bool is_stopped_publish();
    virtual bool is_flash_final();
    static SrsBandwidthPacket* create_finish();
    static SrsBandwidthPacket* create_start_play();
    static SrsBandwidthPacket* create_playing();
    static SrsBandwidthPacket* create_stop_play();
    static SrsBandwidthPacket* create_start_publish();
    static SrsBandwidthPacket* create_stop_publish();
964
private:
965
    virtual SrsBandwidthPacket* set_command(std::string command);
966 967 968
};

/**
winlin authored
969 970 971 972 973 974
* onStatus data, AMF0 Data
* @remark, user must set the stream_id by SrsMessage.set_packet().
*/
class SrsOnStatusDataPacket : public SrsPacket
{
private:
975
    typedef SrsPacket super;
winlin authored
976
protected:
977 978 979 980
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsOnStatusDataPacket);
    }
winlin authored
981
public:
982 983
    std::string command_name;
    SrsAmf0Object* data;
winlin authored
984
public:
985 986
    SrsOnStatusDataPacket();
    virtual ~SrsOnStatusDataPacket();
winlin authored
987
public:
988
    virtual int get_perfer_cid();
winlin authored
989
public:
990
    virtual int get_message_type();
winlin authored
991
protected:
992 993
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
994 995 996 997 998 999 1000 1001 1002
};

/**
* AMF0Data RtmpSampleAccess
* @remark, user must set the stream_id by SrsMessage.set_packet().
*/
class SrsSampleAccessPacket : public SrsPacket
{
private:
1003
    typedef SrsPacket super;
winlin authored
1004
protected:
1005 1006 1007 1008
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsSampleAccessPacket);
    }
winlin authored
1009
public:
1010 1011 1012
    std::string command_name;
    bool video_sample_access;
    bool audio_sample_access;
winlin authored
1013
public:
1014 1015
    SrsSampleAccessPacket();
    virtual ~SrsSampleAccessPacket();
winlin authored
1016
public:
1017
    virtual int get_perfer_cid();
winlin authored
1018
public:
1019
    virtual int get_message_type();
winlin authored
1020
protected:
1021 1022
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
};

/**
* the stream metadata.
* FMLE: @setDataFrame
* others: onMetaData
*/
class SrsOnMetaDataPacket : public SrsPacket
{
private:
1033
    typedef SrsPacket super;
winlin authored
1034
protected:
1035 1036 1037 1038
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsOnMetaDataPacket);
    }
winlin authored
1039
public:
1040 1041
    std::string name;
    SrsAmf0Object* metadata;
winlin authored
1042
public:
1043 1044
    SrsOnMetaDataPacket();
    virtual ~SrsOnMetaDataPacket();
winlin authored
1045
public:
1046
    virtual int decode(SrsStream* stream);
winlin authored
1047
public:
1048
    virtual int get_perfer_cid();
winlin authored
1049
public:
1050
    virtual int get_message_type();
winlin authored
1051
protected:
1052 1053
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
};

/**
* 5.5. Window Acknowledgement Size (5)
* The client or the server sends this message to inform the peer which
* window size to use when sending acknowledgment.
*/
class SrsSetWindowAckSizePacket : public SrsPacket
{
private:
1064
    typedef SrsPacket super;
winlin authored
1065
protected:
1066 1067 1068 1069
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsSetWindowAckSizePacket);
    }
winlin authored
1070
public:
1071
    int32_t ackowledgement_window_size;
winlin authored
1072
public:
1073 1074
    SrsSetWindowAckSizePacket();
    virtual ~SrsSetWindowAckSizePacket();
winlin authored
1075
public:
1076
    virtual int decode(SrsStream* stream);
winlin authored
1077
public:
1078
    virtual int get_perfer_cid();
winlin authored
1079
public:
1080
    virtual int get_message_type();
winlin authored
1081
protected:
1082 1083
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
};

/**
* 5.3. Acknowledgement (3)
* The client or the server sends the acknowledgment to the peer after
* receiving bytes equal to the window size.
*/
class SrsAcknowledgementPacket : public SrsPacket
{
private:
1094
    typedef SrsPacket super;
winlin authored
1095
protected:
1096 1097 1098 1099
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsAcknowledgementPacket);
    }
winlin authored
1100
public:
1101
    int32_t sequence_number;
winlin authored
1102
public:
1103 1104
    SrsAcknowledgementPacket();
    virtual ~SrsAcknowledgementPacket();
winlin authored
1105
public:
1106
    virtual int get_perfer_cid();
winlin authored
1107
public:
1108
    virtual int get_message_type();
winlin authored
1109
protected:
1110 1111
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
};

/**
* 7.1. Set Chunk Size
* Protocol control message 1, Set Chunk Size, is used to notify the
* peer about the new maximum chunk size.
*/
class SrsSetChunkSizePacket : public SrsPacket
{
private:
1122
    typedef SrsPacket super;
winlin authored
1123
protected:
1124 1125 1126 1127
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsSetChunkSizePacket);
    }
winlin authored
1128
public:
1129
    int32_t chunk_size;
winlin authored
1130
public:
1131 1132
    SrsSetChunkSizePacket();
    virtual ~SrsSetChunkSizePacket();
winlin authored
1133
public:
1134
    virtual int decode(SrsStream* stream);
winlin authored
1135
public:
1136
    virtual int get_perfer_cid();
winlin authored
1137
public:
1138
    virtual int get_message_type();
winlin authored
1139
protected:
1140 1141
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
};

/**
* 5.6. Set Peer Bandwidth (6)
* The client or the server sends this message to update the output
* bandwidth of the peer.
*/
class SrsSetPeerBandwidthPacket : public SrsPacket
{
private:
1152
    typedef SrsPacket super;
winlin authored
1153
protected:
1154 1155 1156 1157
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsSetPeerBandwidthPacket);
    }
winlin authored
1158
public:
1159 1160
    int32_t bandwidth;
    int8_t type;
winlin authored
1161
public:
1162 1163
    SrsSetPeerBandwidthPacket();
    virtual ~SrsSetPeerBandwidthPacket();
winlin authored
1164
public:
1165
    virtual int get_perfer_cid();
winlin authored
1166
public:
1167
    virtual int get_message_type();
winlin authored
1168
protected:
1169 1170
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1171 1172 1173 1174 1175
};

// 3.7. User Control message
enum SrcPCUCEventType
{
1176 1177 1178 1179 1180 1181 1182 1183
     // generally, 4bytes event-data
    SrcPCUCStreamBegin             = 0x00,
    SrcPCUCStreamEOF             = 0x01,
    SrcPCUCStreamDry             = 0x02,
    SrcPCUCSetBufferLength         = 0x03, // 8bytes event-data
    SrcPCUCStreamIsRecorded     = 0x04,
    SrcPCUCPingRequest             = 0x06,
    SrcPCUCPingResponse         = 0x07,
winlin authored
1184 1185 1186 1187
};

/**
* for the EventData is 4bytes.
1188 1189 1190 1191 1192 1193 1194
* Stream Begin(=0)            4-bytes stream ID
* Stream EOF(=1)            4-bytes stream ID
* StreamDry(=2)                4-bytes stream ID
* SetBufferLength(=3)        8-bytes 4bytes stream ID, 4bytes buffer length.
* StreamIsRecorded(=4)        4-bytes stream ID
* PingRequest(=6)            4-bytes timestamp local server time
* PingResponse(=7)            4-bytes timestamp received ping request.
winlin authored
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
* 
* 3.7. User Control message
* +------------------------------+-------------------------
* | Event Type ( 2- bytes ) | Event Data
* +------------------------------+-------------------------
* Figure 5 Pay load for the ‘User Control Message’.
*/
class SrsUserControlPacket : public SrsPacket
{
private:
1205
    typedef SrsPacket super;
winlin authored
1206
protected:
1207 1208 1209 1210
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsUserControlPacket);
    }
winlin authored
1211
public:
1212 1213 1214 1215 1216 1217 1218
    // @see: SrcPCUCEventType
    int16_t event_type;
    int32_t event_data;
    /**
    * 4bytes if event_type is SetBufferLength; otherwise 0.
    */
    int32_t extra_data;
winlin authored
1219
public:
1220 1221
    SrsUserControlPacket();
    virtual ~SrsUserControlPacket();
winlin authored
1222
public:
1223
    virtual int decode(SrsStream* stream);
winlin authored
1224
public:
1225
    virtual int get_perfer_cid();
winlin authored
1226
public:
1227
    virtual int get_message_type();
winlin authored
1228
protected:
1229 1230
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1231 1232 1233 1234 1235 1236 1237
};

/**
* 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.
1238
* for example:
1239 1240 1241 1242 1243 1244
         SrsCommonMessage* msg = NULL;
        SrsConnectAppResPacket* pkt = NULL;
        if ((ret = srs_rtmp_expect_message<SrsConnectAppResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
            return ret;
        }
        // use pkt
1245 1246
* user should never recv message and convert it, use this method instead.
* if need to set timeout, use set timeout of SrsProtocol.
winlin authored
1247 1248 1249 1250
*/
template<class T>
int srs_rtmp_expect_message(SrsProtocol* protocol, SrsCommonMessage** pmsg, T** ppacket)
{
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    *pmsg = NULL;
    *ppacket = NULL;
    
    int ret = ERROR_SUCCESS;
    
    while (true) {
        SrsCommonMessage* msg = NULL;
        if ((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) {
            srs_error("recv message failed. ret=%d", ret);
            return ret;
        }
        srs_verbose("recv message success.");
        
        if ((ret = msg->decode_packet(protocol)) != ERROR_SUCCESS) {
            delete msg;
            srs_error("decode message failed. ret=%d", ret);
            return ret;
        }
        
        T* pkt = dynamic_cast<T*>(msg->get_packet());
        if (!pkt) {
            delete msg;
            srs_trace("drop message(type=%d, size=%d, time=%"PRId64", sid=%d).", 
                msg->header.message_type, msg->header.payload_length,
                msg->header.timestamp, msg->header.stream_id);
            continue;
        }
        
        *pmsg = msg;
        *ppacket = pkt;
        break;
    }
    
    return ret;
winlin authored
1285 1286
}
1287
#endif