From 51f924c55382810a8d1d67d81a89e56a3ede6b23 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 6 Jul 2014 19:06:25 +0800 Subject: [PATCH] refine RTMP protocol stack, refine the packet encode/decode, remove class_name --- trunk/src/rtmp/srs_protocol_rtmp_stack.cpp | 103 +++++----- trunk/src/rtmp/srs_protocol_rtmp_stack.hpp | 228 ++++++--------------- 2 files changed, 109 insertions(+), 222 deletions(-) diff --git a/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp b/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp index 1fa380c10..446448c59 100644 --- a/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp +++ b/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp @@ -447,8 +447,7 @@ int SrsProtocol::do_send_message(SrsMessage* msg, SrsPacket* packet) // we donot use the complex basic header, // ensure the basic header is 1bytes. if (msg->header.perfer_cid < 2) { - srs_warn("change the chunk_id=%d to default=%d", - msg->header.perfer_cid, RTMP_CID_ProtocolControl); + srs_warn("change the chunk_id=%d to default=%d", msg->header.perfer_cid, RTMP_CID_ProtocolControl); msg->header.perfer_cid = RTMP_CID_ProtocolControl; } @@ -1689,29 +1688,6 @@ SrsPacket::~SrsPacket() { } -int SrsPacket::decode(SrsStream* stream) -{ - int ret = ERROR_SUCCESS; - - srs_assert(stream != NULL); - - ret = ERROR_SYSTEM_PACKET_INVALID; - srs_error("current packet is not support to decode. " - "paket=%s, ret=%d", get_class_name(), ret); - - return ret; -} - -int SrsPacket::get_perfer_cid() -{ - return 0; -} - -int SrsPacket::get_message_type() -{ - return 0; -} - int SrsPacket::encode(int& psize, char*& ppayload) { int ret = ERROR_SUCCESS; @@ -1744,6 +1720,28 @@ int SrsPacket::encode(int& psize, char*& ppayload) return ret; } +int SrsPacket::decode(SrsStream* stream) +{ + int ret = ERROR_SUCCESS; + + srs_assert(stream != NULL); + + ret = ERROR_SYSTEM_PACKET_INVALID; + srs_error("current packet is not support to decode. ret=%d", ret); + + return ret; +} + +int SrsPacket::get_perfer_cid() +{ + return 0; +} + +int SrsPacket::get_message_type() +{ + return 0; +} + int SrsPacket::get_size() { return 0; @@ -1756,8 +1754,7 @@ int SrsPacket::encode_packet(SrsStream* stream) srs_assert(stream != NULL); ret = ERROR_SYSTEM_PACKET_INVALID; - srs_error("current packet is not support to encode. " - "paket=%s, ret=%d", get_class_name(), ret); + srs_error("current packet is not support to encode. ret=%d", ret); return ret; } @@ -3141,6 +3138,32 @@ SrsBandwidthPacket::~SrsBandwidthPacket() srs_freep(data); } +int SrsBandwidthPacket::decode(SrsStream *stream) +{ + int ret = ERROR_SUCCESS; + + if ((ret = srs_amf0_read_string(stream, command_name)) != ERROR_SUCCESS) { + srs_error("amf0 decode play command_name failed. ret=%d", ret); + return ret; + } + + if ((ret = srs_amf0_read_number(stream, transaction_id)) != ERROR_SUCCESS) { + srs_error("amf0 decode play transaction_id failed. ret=%d", ret); + return ret; + } + + if ((ret = srs_amf0_read_null(stream)) != ERROR_SUCCESS) { + srs_error("amf0 decode play command_object failed. ret=%d", ret); + return ret; + } + + // @remark, for bandwidth test, ignore the data field. + + srs_info("decode SrsBandwidthPacket success."); + + return ret; +} + int SrsBandwidthPacket::get_perfer_cid() { return RTMP_CID_OverStream; @@ -3190,32 +3213,6 @@ int SrsBandwidthPacket::encode_packet(SrsStream* stream) return ret; } -int SrsBandwidthPacket::decode(SrsStream *stream) -{ - int ret = ERROR_SUCCESS; - - if ((ret = srs_amf0_read_string(stream, command_name)) != ERROR_SUCCESS) { - srs_error("amf0 decode play command_name failed. ret=%d", ret); - return ret; - } - - if ((ret = srs_amf0_read_number(stream, transaction_id)) != ERROR_SUCCESS) { - srs_error("amf0 decode play transaction_id failed. ret=%d", ret); - return ret; - } - - if ((ret = srs_amf0_read_null(stream)) != ERROR_SUCCESS) { - srs_error("amf0 decode play command_object failed. ret=%d", ret); - return ret; - } - - // @remark, for bandwidth test, ignore the data field. - - srs_info("decode SrsBandwidthPacket success."); - - return ret; -} - bool SrsBandwidthPacket::is_starting_play() { return command_name == SRS_BW_CHECK_STARTING_PLAY; diff --git a/trunk/src/rtmp/srs_protocol_rtmp_stack.hpp b/trunk/src/rtmp/srs_protocol_rtmp_stack.hpp index 5d2a98eb5..b7ca9b1b5 100644 --- a/trunk/src/rtmp/srs_protocol_rtmp_stack.hpp +++ b/trunk/src/rtmp/srs_protocol_rtmp_stack.hpp @@ -62,9 +62,6 @@ class SrsChunkStream; // generally, it's the pulse time for data seding. #define SRS_PULSE_TIMEOUT_US (int64_t)(200*1000LL) -// convert class name to string. -#define CLASS_NAME_STRING(className) #className - /** * max rtmp header size: * 1bytes basic header, @@ -435,43 +432,38 @@ public: */ class SrsPacket { -protected: - /** - * subpacket must override to provide the right class name. - */ - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsPacket); - } public: SrsPacket(); virtual ~SrsPacket(); -/** -* decode functions. -*/ +public: + /** + * 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); +// decode functions for concrete packet to override. public: /** * subpacket must override to decode packet from stream. * @remark never invoke the super.decode, it always failed. */ virtual int decode(SrsStream* stream); -/** -* encode functions. -*/ +// encode functions for concrete packet to override. public: + /** + * the cid(chunk id) specifies the chunk to send data over. + * generally, each message perfer some cid, for example, + * all protocol control messages perfer RTMP_CID_ProtocolControl, + * SrsSetWindowAckSizePacket is protocol control message. + */ virtual int get_perfer_cid(); -public: /** * subpacket must override to provide the right message type. + * the message type set the RTMP message type in header. */ 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); protected: /** * subpacket can override to calc the packet size. @@ -491,11 +483,6 @@ protected: */ class SrsConnectAppPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsConnectAppPacket); - } public: std::string command_name; double transaction_id; @@ -512,11 +499,12 @@ public: public: SrsConnectAppPacket(); virtual ~SrsConnectAppPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -527,11 +515,6 @@ protected: */ class SrsConnectAppResPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsConnectAppResPacket); - } public: std::string command_name; double transaction_id; @@ -540,11 +523,12 @@ public: public: SrsConnectAppResPacket(); virtual ~SrsConnectAppResPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -559,11 +543,6 @@ protected: */ class SrsCallPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsCallPacket); - } public: std::string command_name; double transaction_id; @@ -577,11 +556,12 @@ public: public: SrsCallPacket(); virtual ~SrsCallPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -592,11 +572,6 @@ protected: */ class SrsCallResPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsCallResPacket); - } public: std::string command_name; double transaction_id; @@ -609,9 +584,9 @@ public: public: SrsCallResPacket(double _transaction_id); virtual ~SrsCallResPacket(); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -627,11 +602,6 @@ protected: */ class SrsCreateStreamPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsCreateStreamPacket); - } public: std::string command_name; double transaction_id; @@ -639,11 +609,12 @@ public: public: SrsCreateStreamPacket(); virtual ~SrsCreateStreamPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -654,11 +625,6 @@ protected: */ class SrsCreateStreamResPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsCreateStreamResPacket); - } public: std::string command_name; double transaction_id; @@ -667,11 +633,12 @@ public: public: SrsCreateStreamResPacket(double _transaction_id, double _stream_id); virtual ~SrsCreateStreamResPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -683,11 +650,6 @@ protected: */ class SrsCloseStreamPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsCloseStreamPacket); - } public: std::string command_name; double transaction_id; @@ -695,6 +657,7 @@ public: public: SrsCloseStreamPacket(); virtual ~SrsCloseStreamPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); }; @@ -704,11 +667,6 @@ public: */ class SrsFMLEStartPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsFMLEStartPacket); - } public: std::string command_name; double transaction_id; @@ -717,15 +675,17 @@ public: public: SrsFMLEStartPacket(); virtual ~SrsFMLEStartPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); virtual int encode_packet(SrsStream* stream); +// factory method to create specified FMLE packet. public: static SrsFMLEStartPacket* create_release_stream(std::string stream); static SrsFMLEStartPacket* create_FC_publish(std::string stream); @@ -735,11 +695,6 @@ public: */ class SrsFMLEStartResPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsFMLEStartResPacket); - } public: std::string command_name; double transaction_id; @@ -748,11 +703,12 @@ public: public: SrsFMLEStartResPacket(double _transaction_id); virtual ~SrsFMLEStartResPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -768,11 +724,6 @@ protected: */ class SrsPublishPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsPublishPacket); - } public: std::string command_name; double transaction_id; @@ -783,11 +734,12 @@ public: public: SrsPublishPacket(); virtual ~SrsPublishPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -801,11 +753,6 @@ protected: */ class SrsPausePacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsPausePacket); - } public: std::string command_name; double transaction_id; @@ -815,6 +762,7 @@ public: public: SrsPausePacket(); virtual ~SrsPausePacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); }; @@ -825,11 +773,6 @@ public: */ class SrsPlayPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsPlayPacket); - } public: std::string command_name; double transaction_id; @@ -841,11 +784,12 @@ public: public: SrsPlayPacket(); virtual ~SrsPlayPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -857,11 +801,6 @@ protected: */ class SrsPlayResPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsPlayResPacket); - } public: std::string command_name; double transaction_id; @@ -870,9 +809,9 @@ public: public: SrsPlayResPacket(); virtual ~SrsPlayResPacket(); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -884,11 +823,6 @@ protected: */ class SrsOnBWDonePacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsOnBWDonePacket); - } public: std::string command_name; double transaction_id; @@ -896,9 +830,9 @@ public: public: SrsOnBWDonePacket(); virtual ~SrsOnBWDonePacket(); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -911,11 +845,6 @@ protected: */ class SrsOnStatusCallPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsOnStatusCallPacket); - } public: std::string command_name; double transaction_id; @@ -924,9 +853,9 @@ public: public: SrsOnStatusCallPacket(); virtual ~SrsOnStatusCallPacket(); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -943,11 +872,6 @@ class SrsBandwidthPacket : public SrsPacket { private: disable_default_copy(SrsBandwidthPacket); -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsBandwidthPacket); - } public: std::string command_name; double transaction_id; @@ -956,15 +880,17 @@ public: public: SrsBandwidthPacket(); virtual ~SrsBandwidthPacket(); +// decode functions for concrete packet to override. public: - virtual int get_perfer_cid(); + virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: + virtual int get_perfer_cid(); virtual int get_message_type(); protected: virtual int get_size(); virtual int encode_packet(SrsStream* stream); -public: - virtual int decode(SrsStream* stream); +// help function for bandwidth packet. public: virtual bool is_starting_play(); virtual bool is_stopped_play(); @@ -987,20 +913,15 @@ private: */ class SrsOnStatusDataPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsOnStatusDataPacket); - } public: std::string command_name; SrsAmf0Object* data; public: SrsOnStatusDataPacket(); virtual ~SrsOnStatusDataPacket(); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -1013,11 +934,6 @@ protected: */ class SrsSampleAccessPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsSampleAccessPacket); - } public: std::string command_name; bool video_sample_access; @@ -1025,9 +941,9 @@ public: public: SrsSampleAccessPacket(); virtual ~SrsSampleAccessPacket(); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -1041,22 +957,18 @@ protected: */ class SrsOnMetaDataPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsOnMetaDataPacket); - } public: std::string name; SrsAmf0Object* metadata; public: SrsOnMetaDataPacket(); virtual ~SrsOnMetaDataPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -1070,21 +982,17 @@ protected: */ class SrsSetWindowAckSizePacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsSetWindowAckSizePacket); - } public: int32_t ackowledgement_window_size; public: SrsSetWindowAckSizePacket(); virtual ~SrsSetWindowAckSizePacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -1098,19 +1006,14 @@ protected: */ class SrsAcknowledgementPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsAcknowledgementPacket); - } public: int32_t sequence_number; public: SrsAcknowledgementPacket(); virtual ~SrsAcknowledgementPacket(); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -1124,21 +1027,17 @@ protected: */ class SrsSetChunkSizePacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsSetChunkSizePacket); - } public: int32_t chunk_size; public: SrsSetChunkSizePacket(); virtual ~SrsSetChunkSizePacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -1152,20 +1051,15 @@ protected: */ class SrsSetPeerBandwidthPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsSetPeerBandwidthPacket); - } public: int32_t bandwidth; int8_t type; public: SrsSetPeerBandwidthPacket(); virtual ~SrsSetPeerBandwidthPacket(); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size(); @@ -1203,11 +1097,6 @@ enum SrcPCUCEventType */ class SrsUserControlPacket : public SrsPacket { -protected: - virtual const char* get_class_name() - { - return CLASS_NAME_STRING(SrsUserControlPacket); - } public: // @see: SrcPCUCEventType int16_t event_type; @@ -1219,11 +1108,12 @@ public: public: SrsUserControlPacket(); virtual ~SrsUserControlPacket(); +// decode functions for concrete packet to override. public: virtual int decode(SrsStream* stream); +// encode functions for concrete packet to override. public: virtual int get_perfer_cid(); -public: virtual int get_message_type(); protected: virtual int get_size();