winlin

support set ack window size.

@@ -57,7 +57,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -57,7 +57,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 #define ERROR_RTMP_REQ_CONNECT 305 57 #define ERROR_RTMP_REQ_CONNECT 305
58 #define ERROR_RTMP_REQ_TCURL 306 58 #define ERROR_RTMP_REQ_TCURL 306
59 #define ERROR_RTMP_MESSAGE_DECODE 307 59 #define ERROR_RTMP_MESSAGE_DECODE 307
  60 +#define ERROR_RTMP_MESSAGE_ENCODE 308
60 61
61 #define ERROR_SYSTEM_STREAM_INIT 400 62 #define ERROR_SYSTEM_STREAM_INIT 400
  63 +#define ERROR_SYSTEM_PACKET_INVALID 401
62 64
63 #endif 65 #endif
@@ -884,12 +884,17 @@ int SrsMessage::get_perfer_cid() @@ -884,12 +884,17 @@ int SrsMessage::get_perfer_cid()
884 return packet->get_perfer_cid(); 884 return packet->get_perfer_cid();
885 } 885 }
886 886
887 -void SrsMessage::set_packet(SrsPacket* pkt) 887 +void SrsMessage::set_packet(SrsPacket* pkt, int stream_id)
888 { 888 {
889 if (packet) { 889 if (packet) {
890 delete packet; 890 delete packet;
891 } 891 }
  892 +
892 packet = pkt; 893 packet = pkt;
  894 +
  895 + header.message_type = packet->get_message_type();
  896 + header.payload_length = packet->get_payload_length();
  897 + header.stream_id = stream_id;
893 } 898 }
894 899
895 int SrsMessage::encode_packet() 900 int SrsMessage::encode_packet()
@@ -900,8 +905,14 @@ int SrsMessage::encode_packet() @@ -900,8 +905,14 @@ int SrsMessage::encode_packet()
900 srs_warn("packet is empty, send out empty message."); 905 srs_warn("packet is empty, send out empty message.");
901 return ret; 906 return ret;
902 } 907 }
  908 + // realloc the payload.
  909 + size = 0;
  910 + if (payload) {
  911 + delete[] payload;
  912 + payload = NULL;
  913 + }
903 914
904 - return ret; 915 + return packet->encode(size, (char*&)payload);
905 } 916 }
906 917
907 SrsPacket::SrsPacket() 918 SrsPacket::SrsPacket()
@@ -923,6 +934,66 @@ int SrsPacket::get_perfer_cid() @@ -923,6 +934,66 @@ int SrsPacket::get_perfer_cid()
923 return 0; 934 return 0;
924 } 935 }
925 936
  937 +int SrsPacket::get_message_type()
  938 +{
  939 + return 0;
  940 +}
  941 +
  942 +int SrsPacket::get_payload_length()
  943 +{
  944 + return get_size();
  945 +}
  946 +
  947 +int SrsPacket::encode(int& psize, char*& ppayload)
  948 +{
  949 + int ret = ERROR_SUCCESS;
  950 +
  951 + int size = get_size();
  952 + char* payload = NULL;
  953 +
  954 + SrsStream stream;
  955 +
  956 + if (size > 0) {
  957 + payload = new char[size];
  958 +
  959 + if ((ret = stream.initialize(payload, size)) != ERROR_SUCCESS) {
  960 + srs_error("initialize the stream failed. ret=%d", ret);
  961 + delete[] payload;
  962 + return ret;
  963 + }
  964 + }
  965 +
  966 + if ((ret = encode_packet(&stream)) != ERROR_SUCCESS) {
  967 + srs_error("encode the packet failed. ret=%d", ret);
  968 + delete[] payload;
  969 + return ret;
  970 + }
  971 +
  972 + psize = size;
  973 + ppayload = payload;
  974 + srs_verbose("encode the packet success. size=%d", size);
  975 +
  976 + return ret;
  977 +}
  978 +
  979 +int SrsPacket::get_size()
  980 +{
  981 + return 0;
  982 +}
  983 +
  984 +int SrsPacket::encode_packet(SrsStream* stream)
  985 +{
  986 + int ret = ERROR_SUCCESS;
  987 +
  988 + srs_assert(stream != NULL);
  989 +
  990 + ret = ERROR_SYSTEM_PACKET_INVALID;
  991 + srs_error("current packet is not support to sendout. "
  992 + "paket=%s, ret=%d", get_class_name(), ret);
  993 +
  994 + return ret;
  995 +}
  996 +
926 SrsConnectAppPacket::SrsConnectAppPacket() 997 SrsConnectAppPacket::SrsConnectAppPacket()
927 { 998 {
928 command_name = RTMP_AMF0_COMMAND_CONNECT; 999 command_name = RTMP_AMF0_COMMAND_CONNECT;
@@ -1013,3 +1084,31 @@ int SrsSetWindowAckSizePacket::get_perfer_cid() @@ -1013,3 +1084,31 @@ int SrsSetWindowAckSizePacket::get_perfer_cid()
1013 return RTMP_CID_ProtocolControl; 1084 return RTMP_CID_ProtocolControl;
1014 } 1085 }
1015 1086
  1087 +int SrsSetWindowAckSizePacket::get_message_type()
  1088 +{
  1089 + return RTMP_MSG_WindowAcknowledgementSize;
  1090 +}
  1091 +
  1092 +int SrsSetWindowAckSizePacket::get_size()
  1093 +{
  1094 + return 4;
  1095 +}
  1096 +
  1097 +int SrsSetWindowAckSizePacket::encode_packet(SrsStream* stream)
  1098 +{
  1099 + int ret = ERROR_SUCCESS;
  1100 +
  1101 + if (!stream->require(4)) {
  1102 + ret = ERROR_RTMP_MESSAGE_ENCODE;
  1103 + srs_error("encode ack size packet failed. ret=%d", ret);
  1104 + return ret;
  1105 + }
  1106 +
  1107 + stream->write_4bytes(ackowledgement_window_size);
  1108 +
  1109 + srs_verbose("encode ack size packet "
  1110 + "success. ack_size=%d", ackowledgement_window_size);
  1111 +
  1112 + return ret;
  1113 +}
  1114 +
@@ -46,6 +46,9 @@ class SrsMessage; @@ -46,6 +46,9 @@ class SrsMessage;
46 class SrsChunkStream; 46 class SrsChunkStream;
47 class SrsAmf0Object; 47 class SrsAmf0Object;
48 48
  49 +// convert class name to string.
  50 +#define CLASS_NAME_STRING(className) #className
  51 +
49 /** 52 /**
50 * max rtmp header size: 53 * max rtmp header size:
51 * 1bytes basic header, 54 * 1bytes basic header,
@@ -237,9 +240,10 @@ public: @@ -237,9 +240,10 @@ public:
237 virtual int get_perfer_cid(); 240 virtual int get_perfer_cid();
238 /** 241 /**
239 * set the encoded packet to encode_packet() to payload. 242 * set the encoded packet to encode_packet() to payload.
  243 + * @stream_id, the id of stream which is created by createStream.
240 * @remark, user never free the pkt, the message will auto free it. 244 * @remark, user never free the pkt, the message will auto free it.
241 */ 245 */
242 - virtual void set_packet(SrsPacket* pkt); 246 + virtual void set_packet(SrsPacket* pkt, int stream_id = 0);
243 /** 247 /**
244 * encode the packet to message payload bytes. 248 * encode the packet to message payload bytes.
245 * @remark there exists empty packet, so maybe the payload is NULL. 249 * @remark there exists empty packet, so maybe the payload is NULL.
@@ -259,6 +263,36 @@ public: @@ -259,6 +263,36 @@ public:
259 virtual int decode(SrsStream* stream); 263 virtual int decode(SrsStream* stream);
260 public: 264 public:
261 virtual int get_perfer_cid(); 265 virtual int get_perfer_cid();
  266 + virtual int get_payload_length();
  267 +public:
  268 + /**
  269 + * subpacket must override to provide the right message type.
  270 + */
  271 + virtual int get_message_type();
  272 + /**
  273 + * the subpacket can override this encode,
  274 + * for example, video and audio will directly set the payload withou memory copy,
  275 + * other packet which need to serialize/encode to bytes by override the
  276 + * get_size and encode_packet.
  277 + */
  278 + virtual int encode(int& size, char*& payload);
  279 +protected:
  280 + /**
  281 + * subpacket can override to calc the packet size.
  282 + */
  283 + virtual int get_size();
  284 + /**
  285 + * subpacket can override to encode the payload to stream.
  286 + */
  287 + virtual int encode_packet(SrsStream* stream);
  288 +protected:
  289 + /**
  290 + * subpacket must override to provide the right class name.
  291 + */
  292 + virtual const char* get_class_name()
  293 + {
  294 + return CLASS_NAME_STRING(SrsPacket);
  295 + }
262 }; 296 };
263 297
264 /** 298 /**
@@ -279,6 +313,11 @@ public: @@ -279,6 +313,11 @@ public:
279 virtual ~SrsConnectAppPacket(); 313 virtual ~SrsConnectAppPacket();
280 public: 314 public:
281 virtual int decode(SrsStream* stream); 315 virtual int decode(SrsStream* stream);
  316 +protected:
  317 + virtual const char* get_class_name()
  318 + {
  319 + return CLASS_NAME_STRING(SrsConnectAppPacket);
  320 + }
282 }; 321 };
283 322
284 /** 323 /**
@@ -299,6 +338,16 @@ public: @@ -299,6 +338,16 @@ public:
299 virtual int decode(SrsStream* stream); 338 virtual int decode(SrsStream* stream);
300 public: 339 public:
301 virtual int get_perfer_cid(); 340 virtual int get_perfer_cid();
  341 +public:
  342 + virtual int get_message_type();
  343 +protected:
  344 + virtual int get_size();
  345 + virtual int encode_packet(SrsStream* stream);
  346 +protected:
  347 + virtual const char* get_class_name()
  348 + {
  349 + return CLASS_NAME_STRING(SrsSetWindowAckSizePacket);
  350 + }
302 }; 351 };
303 352
304 /** 353 /**
@@ -141,3 +141,14 @@ std::string SrsStream::read_string(int len) @@ -141,3 +141,14 @@ std::string SrsStream::read_string(int len)
141 return value; 141 return value;
142 } 142 }
143 143
  144 +void SrsStream::write_4bytes(int32_t value)
  145 +{
  146 + srs_assert(require(4));
  147 +
  148 + pp = (char*)&value;
  149 + *p++ = pp[3];
  150 + *p++ = pp[2];
  151 + *p++ = pp[1];
  152 + *p++ = pp[0];
  153 +}
  154 +
@@ -91,6 +91,11 @@ public: @@ -91,6 +91,11 @@ public:
91 * get string from stream, length specifies by param len. 91 * get string from stream, length specifies by param len.
92 */ 92 */
93 virtual std::string read_string(int len); 93 virtual std::string read_string(int len);
  94 +public:
  95 + /**
  96 + * write 4bytes int to stream.
  97 + */
  98 + virtual void write_4bytes(int32_t value);
94 }; 99 };
95 100
96 #endif 101 #endif