Blame view

trunk/src/rtmp/srs_protocol_rtmp_stack.hpp 31.6 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>
winlin authored
38 39 40 41 42 43 44

class ISrsProtocolReaderWriter;
class SrsBuffer;
class SrsPacket;
class SrsStream;
class SrsAmf0Object;
class SrsAmf0Any;
45
class SrsMessageHeader;
46 47
class SrsMessage;
class SrsChunkStream;
48
 
49 50 51 52 53
// the following is the timeout for rtmp protocol, 
// to avoid death connection.

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

// the timeout to send data to client,
// if timeout, close the connection.
58
#define SRS_RECV_TIMEOUT_US (int64_t)(30*1000*1000LL)
59
winlin authored
60 61 62 63
// 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.
#define SRS_PULSE_TIMEOUT_US (int64_t)(200*1000LL)
winlin authored
64 65 66 67 68 69

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

/**
* max rtmp header size:
70 71 72
*     1bytes basic header,
*     11bytes message header,
*     4bytes timestamp header,
winlin authored
73 74 75 76 77
* that is, 1+11+4=16bytes.
*/
#define RTMP_MAX_FMT0_HEADER_SIZE 16
/**
* max rtmp header size:
78 79
*     1bytes basic header,
*     4bytes timestamp header,
winlin authored
80 81
* that is, 1+4=5bytes.
*/
82 83
// always use fmt0 as cache.
//#define RTMP_MAX_FMT3_HEADER_SIZE 5
winlin authored
84 85 86 87 88 89 90 91 92

/**
* 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:
93 94 95 96 97 98 99
    struct AckWindowSize
    {
        int ack_window_size;
        int64_t acked_size;
        
        AckWindowSize();
    };
winlin authored
100 101
// peer in/out
private:
102 103 104 105 106 107 108 109
    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;
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    /**
    * RTMP specification and ffmpeg/librtmp is false,
    * but, adobe changed the specification, so flash/FMLE/FMS always true.
    * default to true to support flash/FMLE/FMS.
    * 
    * ffmpeg/librtmp may donot send this filed, need to detect the value.
    * @see also: http://blog.csdn.net/win_lin/article/details/13363699
    * compare to the chunk timestamp, which is set by chunk message header
    * type 0,1 or 2.
    *
    * @remark, nginx send the extended-timestamp in sequence-header,
    * and timestamp delta in continue C1 chunks, and so compatible with ffmpeg,
    * that is, there is no continue chunks and extended-timestamp in nginx-rtmp.
    *
    * @remark, srs always send the extended-timestamp, to keep simple,
    * and compatible with adobe products.
    */
    bool send_extended_timestamp_for_C3_chunk;
winlin authored
128 129
// peer in
private:
130
    std::map<int, SrsChunkStream*> chunk_streams;
131
    SrsStream* decode_stream;
132 133 134
    SrsBuffer* buffer;
    int32_t in_chunk_size;
    AckWindowSize in_ack_size;
winlin authored
135 136
// peer out
private:
137
    char out_header_cache[RTMP_MAX_FMT0_HEADER_SIZE];
138 139 140 141 142 143 144 145 146
    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:
147
    // TODO: FIXME: to private.
148 149 150 151 152 153 154 155 156 157 158 159 160
    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();
161 162 163 164 165 166 167 168 169
public:
    /**
    * 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.
    */
170
    virtual int recv_message(SrsMessage** pmsg);
171 172 173 174 175 176
    /**
    * 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.
    */
177
    virtual int decode_message(SrsMessage* msg, SrsPacket** ppacket);
178 179 180 181 182 183
    /**
    * 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.
    */
184
    virtual int send_and_free_message(SrsMessage* msg);
185 186 187 188 189 190 191
    /**
    * 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.
    */
192
    virtual int send_and_free_packet(SrsPacket* packet, int stream_id);
193 194
private:
    /**
195
    * imp for send_and_free_message
196 197
    * @param packet the packet of message, NULL for raw message.
    */
198
    virtual int do_send_and_free_message(SrsMessage* msg, SrsPacket* packet);
199
    /**
200
    * imp for decode_message
201
    */
202
    virtual int do_decode_message(SrsMessageHeader& header, SrsStream* stream, SrsPacket** ppacket);
203 204 205 206 207 208
    /**
    * recv bytes oriented RTMP message from protocol stack.
    * 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.
    */
209
    virtual int recv_interlaced_message(SrsMessage** pmsg);
210 211 212 213 214
    /**
    * 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.
    */
215
    virtual int read_basic_header(char& fmt, int& cid, int& bh_size);
216 217 218 219 220
    /**
    * 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.
    */
221
    virtual int read_message_header(SrsChunkStream* chunk, char fmt, int bh_size, int& mh_size);
222 223 224 225 226
    /**
    * 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.
    */
227
    virtual int read_message_payload(SrsChunkStream* chunk, int bh_size, int mh_size, int& payload_size, SrsMessage** pmsg);
228 229 230
    /**
    * when recv message, update the context.
    */
231
    virtual int on_recv_message(SrsMessage* msg);
232 233 234
    /**
    * when message sentout, update the context.
    */
235
    virtual int on_send_message(SrsMessage* msg, SrsPacket* packet);
winlin authored
236
private:
237 238
    virtual int response_acknowledgement_message();
    virtual int response_ping_message(int32_t timestamp);
winlin authored
239 240 241 242 243 244 245
};

/**
* 4.1. Message Header
*/
struct SrsMessageHeader
{
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
    /**
    * 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;
    
276 277 278 279 280 281 282 283 284
public:
    /**
    * get the perfered cid(chunk stream id) which sendout over.
    * set at decoding, and canbe used for directly send message,
    * for example, dispatch to all connections.
    */
    int perfer_cid;
    
public:
285 286
    SrsMessageHeader();
    virtual ~SrsMessageHeader();
winlin authored
287
288 289 290 291 292 293 294
    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();
295
    bool is_ackledgement();
296 297 298 299 300 301
    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
302 303 304 305 306 307
};

/**
* incoming chunk stream maybe interlaced,
* use the chunk stream to cache the input RTMP chunk streams.
*/
308
class SrsChunkStream
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
{
public:
    /**
    * 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.
    */
332
    SrsMessage* msg;
333 334 335 336 337
    /**
    * decoded msg count, to identify whether the chunk stream is fresh.
    */
    int64_t msg_count;
public:
338 339
    SrsChunkStream(int _cid);
    virtual ~SrsChunkStream();
340 341 342 343 344 345 346
};

/**
* message is raw data RTMP message, bytes oriented,
* protcol always recv RTMP message, and can send RTMP message or RTMP packet.
* the shared-ptr message is a special RTMP message, use ref-count for performance issue.
*/
347
class SrsMessage
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
{
// 4.1. Message Header
public:
    SrsMessageHeader header;
// 4.2. Message Payload
public:
    /**
    * 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:
363 364
    SrsMessage();
    virtual ~SrsMessage();
365 366 367 368 369 370 371
};

/**
* shared ptr message.
* for audio/video/data message that need less memory copy.
* and only for output.
*/
372
class SrsSharedPtrMessage : public SrsMessage
373 374 375 376 377 378 379 380 381 382 383 384 385
{
private:
    struct __SrsSharedPtr
    {
        char* payload;
        int size;
        int shared_count;
        
        __SrsSharedPtr();
        virtual ~__SrsSharedPtr();
    };
    __SrsSharedPtr* ptr;
public:
386 387
    SrsSharedPtrMessage();
    virtual ~SrsSharedPtrMessage();
388 389 390 391 392 393
public:
    /**
    * set the shared payload.
    * we will detach the payload of source,
    * so ensure donot use it before.
    */
394
    virtual int initialize(SrsMessage* source);
395 396 397 398 399 400 401 402 403
    /**
    * set the shared payload.
    * use source header, and specified param payload.
    */
    virtual int initialize(SrsMessageHeader* source, char* payload, int size);
public:
    /**
    * copy current shared ptr message, use ref-count.
    */
404
    virtual SrsSharedPtrMessage* copy();
405 406 407
};

/**
winlin authored
408 409
* the decoded message payload.
* @remark we seperate the packet from message,
410 411 412 413
*        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
414 415 416 417
*/
class SrsPacket
{
protected:
418 419 420 421 422 423 424 425 426 427
    /**
    * 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
428 429 430 431
/**
* decode functions.
*/
public:
432 433 434 435 436
    /**
    * subpacket must override to decode packet from stream.
    * @remark never invoke the super.decode, it always failed.
    */
    virtual int decode(SrsStream* stream);
winlin authored
437 438 439 440
/**
* encode functions.
*/
public:
441 442 443 444 445 446 447 448 449 450 451 452 453
    virtual int get_perfer_cid();
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
454
protected:
455 456 457 458 459 460 461 462 463
    /**
    * 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
464 465 466 467 468 469 470 471 472 473
};

/**
* 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
{
protected:
474 475 476 477
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsConnectAppPacket);
    }
winlin authored
478
public:
479 480 481
    std::string command_name;
    double transaction_id;
    SrsAmf0Object* command_object;
winlin authored
482
public:
483 484
    SrsConnectAppPacket();
    virtual ~SrsConnectAppPacket();
winlin authored
485
public:
486
    virtual int decode(SrsStream* stream);
winlin authored
487
public:
488
    virtual int get_perfer_cid();
winlin authored
489
public:
490
    virtual int get_message_type();
winlin authored
491
protected:
492 493
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
494 495 496 497 498 499 500
};
/**
* response for SrsConnectAppPacket.
*/
class SrsConnectAppResPacket : public SrsPacket
{
protected:
501 502 503 504
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsConnectAppResPacket);
    }
winlin authored
505
public:
506 507 508 509
    std::string command_name;
    double transaction_id;
    SrsAmf0Object* props;
    SrsAmf0Object* info;
winlin authored
510
public:
511 512
    SrsConnectAppResPacket();
    virtual ~SrsConnectAppResPacket();
winlin authored
513
public:
514
    virtual int decode(SrsStream* stream);
winlin authored
515
public:
516
    virtual int get_perfer_cid();
winlin authored
517
public:
518
    virtual int get_message_type();
winlin authored
519
protected:
520 521
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
522 523 524 525 526 527 528 529 530 531 532 533
};

/**
* 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
{
protected:
534 535 536 537
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsCreateStreamPacket);
    }
winlin authored
538
public:
539 540 541
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
winlin authored
542
public:
543 544
    SrsCreateStreamPacket();
    virtual ~SrsCreateStreamPacket();
winlin authored
545
public:
546
    virtual int decode(SrsStream* stream);
winlin authored
547
public:
548
    virtual int get_perfer_cid();
winlin authored
549
public:
550
    virtual int get_message_type();
winlin authored
551
protected:
552 553
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
554 555 556 557 558 559 560
};
/**
* response for SrsCreateStreamPacket.
*/
class SrsCreateStreamResPacket : public SrsPacket
{
protected:
561 562 563 564
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsCreateStreamResPacket);
    }
winlin authored
565
public:
566 567 568 569
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    double stream_id;
winlin authored
570
public:
571 572
    SrsCreateStreamResPacket(double _transaction_id, double _stream_id);
    virtual ~SrsCreateStreamResPacket();
winlin authored
573
public:
574
    virtual int decode(SrsStream* stream);
winlin authored
575
public:
576
    virtual int get_perfer_cid();
winlin authored
577
public:
578
    virtual int get_message_type();
winlin authored
579
protected:
580 581
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
582
};
583 584 585 586 587 588 589 590 591 592 593 594 595
/**
* client close stream packet.
*/
class SrsCloseStreamPacket : public SrsPacket
{
protected:
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsCloseStreamPacket);
    }
public:
    std::string command_name;
    double transaction_id;
596
    SrsAmf0Any* command_object; // null
597 598 599 600 601 602
public:
    SrsCloseStreamPacket();
    virtual ~SrsCloseStreamPacket();
public:
    virtual int decode(SrsStream* stream);
};
winlin authored
603 604 605 606 607 608 609

/**
* FMLE start publish: ReleaseStream/PublishStream
*/
class SrsFMLEStartPacket : public SrsPacket
{
protected:
610 611 612 613
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsFMLEStartPacket);
    }
winlin authored
614
public:
615 616 617 618
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    std::string stream_name;
winlin authored
619
public:
620 621
    SrsFMLEStartPacket();
    virtual ~SrsFMLEStartPacket();
winlin authored
622
public:
623
    virtual int decode(SrsStream* stream);
624
public:
625
    virtual int get_perfer_cid();
626
public:
627
    virtual int get_message_type();
628
protected:
629 630
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
631
public:
632 633
    static SrsFMLEStartPacket* create_release_stream(std::string stream);
    static SrsFMLEStartPacket* create_FC_publish(std::string stream);
winlin authored
634 635 636 637 638 639 640
};
/**
* response for SrsFMLEStartPacket.
*/
class SrsFMLEStartResPacket : public SrsPacket
{
protected:
641 642 643 644
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsFMLEStartResPacket);
    }
winlin authored
645
public:
646 647 648 649
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    SrsAmf0Any* args; // undefined
winlin authored
650
public:
651 652
    SrsFMLEStartResPacket(double _transaction_id);
    virtual ~SrsFMLEStartResPacket();
winlin authored
653
public:
654
    virtual int decode(SrsStream* stream);
655
public:
656
    virtual int get_perfer_cid();
winlin authored
657
public:
658
    virtual int get_message_type();
winlin authored
659
protected:
660 661
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
662 663 664 665 666 667 668 669 670 671 672 673
};

/**
* 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
{
protected:
674 675 676 677
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsPublishPacket);
    }
winlin authored
678
public:
679 680 681 682 683 684
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    std::string stream_name;
    // optional, default to live.
    std::string type;
winlin authored
685
public:
686 687
    SrsPublishPacket();
    virtual ~SrsPublishPacket();
winlin authored
688
public:
689
    virtual int decode(SrsStream* stream);
winlin authored
690
public:
691
    virtual int get_perfer_cid();
winlin authored
692
public:
693
    virtual int get_message_type();
winlin authored
694
protected:
695 696
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
697 698 699 700 701 702 703 704 705 706
};

/**
* 4.2.8. pause
* The client sends the pause command to tell the server to pause or
* start playing.
*/
class SrsPausePacket : public SrsPacket
{
protected:
707 708 709 710
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsPausePacket);
    }
winlin authored
711
public:
712 713 714 715 716
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    bool is_pause;
    double time_ms;
winlin authored
717
public:
718 719
    SrsPausePacket();
    virtual ~SrsPausePacket();
winlin authored
720
public:
721
    virtual int decode(SrsStream* stream);
winlin authored
722 723 724 725 726 727 728 729 730
};

/**
* 4.2.1. play
* The client sends this command to the server to play a stream.
*/
class SrsPlayPacket : public SrsPacket
{
protected:
731 732 733 734
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsPlayPacket);
    }
winlin authored
735
public:
736 737 738 739 740 741 742
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    std::string stream_name;
    double start;
    double duration;
    bool reset;
winlin authored
743
public:
744 745
    SrsPlayPacket();
    virtual ~SrsPlayPacket();
winlin authored
746
public:
747
    virtual int decode(SrsStream* stream);
winlin authored
748
public:
749
    virtual int get_perfer_cid();
winlin authored
750
public:
751
    virtual int get_message_type();
winlin authored
752
protected:
753 754
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
755 756 757 758 759 760 761 762
};
/**
* response for SrsPlayPacket.
* @remark, user must set the stream_id in header.
*/
class SrsPlayResPacket : public SrsPacket
{
protected:
763 764 765 766
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsPlayResPacket);
    }
winlin authored
767
public:
768 769 770 771
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* command_object; // null
    SrsAmf0Object* desc;
winlin authored
772
public:
773 774
    SrsPlayResPacket();
    virtual ~SrsPlayResPacket();
winlin authored
775
public:
776
    virtual int get_perfer_cid();
winlin authored
777
public:
778
    virtual int get_message_type();
winlin authored
779
protected:
780 781
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
782 783 784 785 786 787 788 789
};

/**
* when bandwidth test done, notice client.
*/
class SrsOnBWDonePacket : public SrsPacket
{
protected:
790 791 792 793
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsOnBWDonePacket);
    }
winlin authored
794
public:
795 796 797
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* args; // null
winlin authored
798
public:
799 800
    SrsOnBWDonePacket();
    virtual ~SrsOnBWDonePacket();
winlin authored
801
public:
802
    virtual int get_perfer_cid();
winlin authored
803
public:
804
    virtual int get_message_type();
winlin authored
805
protected:
806 807
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
808 809 810 811 812 813 814 815 816
};

/**
* onStatus command, AMF0 Call
* @remark, user must set the stream_id by SrsMessage.set_packet().
*/
class SrsOnStatusCallPacket : public SrsPacket
{
protected:
817 818 819 820
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsOnStatusCallPacket);
    }
winlin authored
821
public:
822 823 824 825
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* args; // null
    SrsAmf0Object* data;
winlin authored
826
public:
827 828
    SrsOnStatusCallPacket();
    virtual ~SrsOnStatusCallPacket();
winlin authored
829
public:
830
    virtual int get_perfer_cid();
winlin authored
831
public:
832
    virtual int get_message_type();
winlin authored
833
protected:
834 835
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
836 837 838
};

/**
839 840 841 842 843 844 845 846
* 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:
847
    disable_default_copy(SrsBandwidthPacket);
848
protected:
849 850 851 852
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsBandwidthPacket);
    }
853
public:
854 855 856 857
    std::string command_name;
    double transaction_id;
    SrsAmf0Any* args; // null
    SrsAmf0Object* data;
858
public:
859 860
    SrsBandwidthPacket();
    virtual ~SrsBandwidthPacket();
861
public:
862
    virtual int get_perfer_cid();
863
public:
864
    virtual int get_message_type();
865
protected:
866 867
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
868 869 870
public:
    virtual int decode(SrsStream* stream);
public:
871 872 873 874 875 876 877 878 879 880 881
    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();
882
private:
883
    virtual SrsBandwidthPacket* set_command(std::string command);
884 885 886
};

/**
winlin authored
887 888 889 890 891 892
* onStatus data, AMF0 Data
* @remark, user must set the stream_id by SrsMessage.set_packet().
*/
class SrsOnStatusDataPacket : public SrsPacket
{
protected:
893 894 895 896
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsOnStatusDataPacket);
    }
winlin authored
897
public:
898 899
    std::string command_name;
    SrsAmf0Object* data;
winlin authored
900
public:
901 902
    SrsOnStatusDataPacket();
    virtual ~SrsOnStatusDataPacket();
winlin authored
903
public:
904
    virtual int get_perfer_cid();
winlin authored
905
public:
906
    virtual int get_message_type();
winlin authored
907
protected:
908 909
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
910 911 912 913 914 915 916 917 918
};

/**
* AMF0Data RtmpSampleAccess
* @remark, user must set the stream_id by SrsMessage.set_packet().
*/
class SrsSampleAccessPacket : public SrsPacket
{
protected:
919 920 921 922
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsSampleAccessPacket);
    }
winlin authored
923
public:
924 925 926
    std::string command_name;
    bool video_sample_access;
    bool audio_sample_access;
winlin authored
927
public:
928 929
    SrsSampleAccessPacket();
    virtual ~SrsSampleAccessPacket();
winlin authored
930
public:
931
    virtual int get_perfer_cid();
winlin authored
932
public:
933
    virtual int get_message_type();
winlin authored
934
protected:
935 936
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
937 938 939 940 941 942 943 944 945 946
};

/**
* the stream metadata.
* FMLE: @setDataFrame
* others: onMetaData
*/
class SrsOnMetaDataPacket : public SrsPacket
{
protected:
947 948 949 950
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsOnMetaDataPacket);
    }
winlin authored
951
public:
952 953
    std::string name;
    SrsAmf0Object* metadata;
winlin authored
954
public:
955 956
    SrsOnMetaDataPacket();
    virtual ~SrsOnMetaDataPacket();
winlin authored
957
public:
958
    virtual int decode(SrsStream* stream);
winlin authored
959
public:
960
    virtual int get_perfer_cid();
winlin authored
961
public:
962
    virtual int get_message_type();
winlin authored
963
protected:
964 965
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
966 967 968 969 970 971 972 973 974 975
};

/**
* 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
{
protected:
976 977 978 979
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsSetWindowAckSizePacket);
    }
winlin authored
980
public:
981
    int32_t ackowledgement_window_size;
winlin authored
982
public:
983 984
    SrsSetWindowAckSizePacket();
    virtual ~SrsSetWindowAckSizePacket();
winlin authored
985
public:
986
    virtual int decode(SrsStream* stream);
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 1003
};

/**
* 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
{
protected:
1004 1005 1006 1007
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsAcknowledgementPacket);
    }
winlin authored
1008
public:
1009
    int32_t sequence_number;
winlin authored
1010
public:
1011 1012
    SrsAcknowledgementPacket();
    virtual ~SrsAcknowledgementPacket();
winlin authored
1013
public:
1014
    virtual int get_perfer_cid();
winlin authored
1015
public:
1016
    virtual int get_message_type();
winlin authored
1017
protected:
1018 1019
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
};

/**
* 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
{
protected:
1030 1031 1032 1033
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsSetChunkSizePacket);
    }
winlin authored
1034
public:
1035
    int32_t chunk_size;
winlin authored
1036
public:
1037 1038
    SrsSetChunkSizePacket();
    virtual ~SrsSetChunkSizePacket();
winlin authored
1039
public:
1040
    virtual int decode(SrsStream* stream);
winlin authored
1041
public:
1042
    virtual int get_perfer_cid();
winlin authored
1043
public:
1044
    virtual int get_message_type();
winlin authored
1045
protected:
1046 1047
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
};

/**
* 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
{
protected:
1058 1059 1060 1061
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsSetPeerBandwidthPacket);
    }
winlin authored
1062
public:
1063 1064
    int32_t bandwidth;
    int8_t type;
winlin authored
1065
public:
1066 1067
    SrsSetPeerBandwidthPacket();
    virtual ~SrsSetPeerBandwidthPacket();
winlin authored
1068
public:
1069
    virtual int get_perfer_cid();
winlin authored
1070
public:
1071
    virtual int get_message_type();
winlin authored
1072
protected:
1073 1074
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1075 1076 1077 1078 1079
};

// 3.7. User Control message
enum SrcPCUCEventType
{
1080 1081 1082 1083 1084 1085 1086 1087
     // generally, 4bytes event-data
    SrcPCUCStreamBegin             = 0x00,
    SrcPCUCStreamEOF             = 0x01,
    SrcPCUCStreamDry             = 0x02,
    SrcPCUCSetBufferLength         = 0x03, // 8bytes event-data
    SrcPCUCStreamIsRecorded     = 0x04,
    SrcPCUCPingRequest             = 0x06,
    SrcPCUCPingResponse         = 0x07,
winlin authored
1088 1089 1090 1091
};

/**
* for the EventData is 4bytes.
1092 1093 1094 1095 1096 1097 1098
* 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
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
* 
* 3.7. User Control message
* +------------------------------+-------------------------
* | Event Type ( 2- bytes ) | Event Data
* +------------------------------+-------------------------
* Figure 5 Pay load for the ‘User Control Message’.
*/
class SrsUserControlPacket : public SrsPacket
{
protected:
1109 1110 1111 1112
    virtual const char* get_class_name()
    {
        return CLASS_NAME_STRING(SrsUserControlPacket);
    }
winlin authored
1113
public:
1114 1115 1116 1117 1118 1119 1120
    // @see: SrcPCUCEventType
    int16_t event_type;
    int32_t event_data;
    /**
    * 4bytes if event_type is SetBufferLength; otherwise 0.
    */
    int32_t extra_data;
winlin authored
1121
public:
1122 1123
    SrsUserControlPacket();
    virtual ~SrsUserControlPacket();
winlin authored
1124
public:
1125
    virtual int decode(SrsStream* stream);
winlin authored
1126
public:
1127
    virtual int get_perfer_cid();
winlin authored
1128
public:
1129
    virtual int get_message_type();
winlin authored
1130
protected:
1131 1132
    virtual int get_size();
    virtual int encode_packet(SrsStream* stream);
winlin authored
1133 1134 1135 1136 1137 1138 1139
};

/**
* 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.
1140
* for example:
1141 1142 1143 1144 1145 1146
         SrsCommonMessage* msg = NULL;
        SrsConnectAppResPacket* pkt = NULL;
        if ((ret = srs_rtmp_expect_message<SrsConnectAppResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
            return ret;
        }
        // use pkt
1147 1148
* user should never recv message and convert it, use this method instead.
* if need to set timeout, use set timeout of SrsProtocol.
winlin authored
1149 1150
*/
template<class T>
1151
int __srs_rtmp_expect_message(SrsProtocol* protocol, SrsMessage** pmsg, T** ppacket)
1152 1153 1154 1155 1156 1157 1158
{
    *pmsg = NULL;
    *ppacket = NULL;
    
    int ret = ERROR_SUCCESS;
    
    while (true) {
1159 1160
        SrsMessage* msg = NULL;
        if ((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) {
1161 1162 1163 1164 1165 1166
            srs_error("recv message failed. ret=%d", ret);
            return ret;
        }
        srs_verbose("recv message success.");
        
        SrsPacket* packet = NULL;
1167
        if ((ret = protocol->decode_message(msg, &packet)) != ERROR_SUCCESS) {
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
            srs_error("decode message failed. ret=%d", ret);
            srs_freep(msg);
            return ret;
        }
        
        T* pkt = dynamic_cast<T*>(packet);
        if (!pkt) {
            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);
            srs_freep(msg);
            continue;
        }
        
        *pmsg = msg;
        *ppacket = pkt;
        break;
    }
    
    return ret;
}
winlin authored
1189
1190
#endif