winlin

refine rtmp client/server, add comments.

@@ -708,11 +708,6 @@ SrsRtmpServer::~SrsRtmpServer() @@ -708,11 +708,6 @@ SrsRtmpServer::~SrsRtmpServer()
708 srs_freep(hs_bytes); 708 srs_freep(hs_bytes);
709 } 709 }
710 710
711 -SrsProtocol* SrsRtmpServer::get_protocol()  
712 -{  
713 - return protocol;  
714 -}  
715 -  
716 void SrsRtmpServer::set_recv_timeout(int64_t timeout_us) 711 void SrsRtmpServer::set_recv_timeout(int64_t timeout_us)
717 { 712 {
718 protocol->set_recv_timeout(timeout_us); 713 protocol->set_recv_timeout(timeout_us);
@@ -172,33 +172,92 @@ protected: @@ -172,33 +172,92 @@ protected:
172 public: 172 public:
173 SrsRtmpClient(ISrsProtocolReaderWriter* skt); 173 SrsRtmpClient(ISrsProtocolReaderWriter* skt);
174 virtual ~SrsRtmpClient(); 174 virtual ~SrsRtmpClient();
  175 +// protocol methods proxy
175 public: 176 public:
  177 + /**
  178 + * set the recv timeout in us.
  179 + * if timeout, recv/send message return ERROR_SOCKET_TIMEOUT.
  180 + */
176 virtual void set_recv_timeout(int64_t timeout_us); 181 virtual void set_recv_timeout(int64_t timeout_us);
  182 + /**
  183 + * set the send timeout in us.
  184 + * if timeout, recv/send message return ERROR_SOCKET_TIMEOUT.
  185 + */
177 virtual void set_send_timeout(int64_t timeout_us); 186 virtual void set_send_timeout(int64_t timeout_us);
  187 + /**
  188 + * get recv/send bytes.
  189 + */
178 virtual int64_t get_recv_bytes(); 190 virtual int64_t get_recv_bytes();
179 virtual int64_t get_send_bytes(); 191 virtual int64_t get_send_bytes();
  192 + /**
  193 + * recv a RTMP message, which is bytes oriented.
  194 + * user can use decode_message to get the decoded RTMP packet.
  195 + * @param pmsg, set the received message,
  196 + * always NULL if error,
  197 + * NULL for unknown packet but return success.
  198 + * never NULL if decode success.
  199 + * @remark, drop message when msg is empty or payload length is empty.
  200 + */
180 virtual int recv_message(SrsMessage** pmsg); 201 virtual int recv_message(SrsMessage** pmsg);
  202 + /**
  203 + * decode bytes oriented RTMP message to RTMP packet,
  204 + * @param ppacket, output decoded packet,
  205 + * always NULL if error, never NULL if success.
  206 + * @return error when unknown packet, error when decode failed.
  207 + */
181 virtual int decode_message(SrsMessage* msg, SrsPacket** ppacket); 208 virtual int decode_message(SrsMessage* msg, SrsPacket** ppacket);
  209 + /**
  210 + * send the RTMP message and always free it.
  211 + * user must never free or use the msg after this method,
  212 + * for it will always free the msg.
  213 + * @param msg, the msg to send out, never be NULL.
  214 + * @param stream_id, the stream id of packet to send over, 0 for control message.
  215 + */
182 virtual int send_and_free_message(SrsMessage* msg, int stream_id); 216 virtual int send_and_free_message(SrsMessage* msg, int stream_id);
  217 + /**
  218 + * send the RTMP packet and always free it.
  219 + * user must never free or use the packet after this method,
  220 + * for it will always free the packet.
  221 + * @param packet, the packet to send out, never be NULL.
  222 + * @param stream_id, the stream id of packet to send over, 0 for control message.
  223 + */
183 virtual int send_and_free_packet(SrsPacket* packet, int stream_id); 224 virtual int send_and_free_packet(SrsPacket* packet, int stream_id);
184 public: 225 public:
185 - // try complex, then simple handshake. 226 + /**
  227 + * handshake with server, try complex, then simple handshake.
  228 + */
186 virtual int handshake(); 229 virtual int handshake();
187 - // only use simple handshake 230 + /**
  231 + * only use simple handshake
  232 + */
188 virtual int simple_handshake(); 233 virtual int simple_handshake();
189 - // only use complex handshake 234 + /**
  235 + * only use complex handshake
  236 + */
190 virtual int complex_handshake(); 237 virtual int complex_handshake();
191 - // set req to use the original request of client:  
192 - // pageUrl and swfUrl for refer antisuck.  
193 - // args for edge to origin traverse auth, @see SrsRequest.args 238 + /**
  239 + * set req to use the original request of client:
  240 + * pageUrl and swfUrl for refer antisuck.
  241 + * args for edge to origin traverse auth, @see SrsRequest.args
  242 + */
194 virtual int connect_app(std::string app, std::string tc_url, SrsRequest* req=NULL); 243 virtual int connect_app(std::string app, std::string tc_url, SrsRequest* req=NULL);
  244 + /**
  245 + * create a stream, then play/publish data over this stream.
  246 + */
195 virtual int create_stream(int& stream_id); 247 virtual int create_stream(int& stream_id);
  248 + /**
  249 + * start play stream.
  250 + */
196 virtual int play(std::string stream, int stream_id); 251 virtual int play(std::string stream, int stream_id);
197 - // flash publish schema:  
198 - // connect-app => create-stream => flash-publish 252 + /**
  253 + * start publish stream. use flash publish workflow:
  254 + * connect-app => create-stream => flash-publish
  255 + */
199 virtual int publish(std::string stream, int stream_id); 256 virtual int publish(std::string stream, int stream_id);
200 - // FMLE publish schema:  
201 - // connect-app => FMLE publish 257 + /**
  258 + * start publish stream. use FMLE publish workflow:
  259 + * connect-app => FMLE publish
  260 + */
202 virtual int fmle_publish(std::string stream, int& stream_id); 261 virtual int fmle_publish(std::string stream, int& stream_id);
203 public: 262 public:
204 /** 263 /**
@@ -237,21 +296,70 @@ private: @@ -237,21 +296,70 @@ private:
237 public: 296 public:
238 SrsRtmpServer(ISrsProtocolReaderWriter* skt); 297 SrsRtmpServer(ISrsProtocolReaderWriter* skt);
239 virtual ~SrsRtmpServer(); 298 virtual ~SrsRtmpServer();
  299 +// protocol methods proxy
240 public: 300 public:
241 - virtual SrsProtocol* get_protocol(); 301 + /**
  302 + * set/get the recv timeout in us.
  303 + * if timeout, recv/send message return ERROR_SOCKET_TIMEOUT.
  304 + */
242 virtual void set_recv_timeout(int64_t timeout_us); 305 virtual void set_recv_timeout(int64_t timeout_us);
243 virtual int64_t get_recv_timeout(); 306 virtual int64_t get_recv_timeout();
  307 + /**
  308 + * set/get the send timeout in us.
  309 + * if timeout, recv/send message return ERROR_SOCKET_TIMEOUT.
  310 + */
244 virtual void set_send_timeout(int64_t timeout_us); 311 virtual void set_send_timeout(int64_t timeout_us);
245 virtual int64_t get_send_timeout(); 312 virtual int64_t get_send_timeout();
  313 + /**
  314 + * get recv/send bytes.
  315 + */
246 virtual int64_t get_recv_bytes(); 316 virtual int64_t get_recv_bytes();
247 virtual int64_t get_send_bytes(); 317 virtual int64_t get_send_bytes();
  318 + /**
  319 + * recv a RTMP message, which is bytes oriented.
  320 + * user can use decode_message to get the decoded RTMP packet.
  321 + * @param pmsg, set the received message,
  322 + * always NULL if error,
  323 + * NULL for unknown packet but return success.
  324 + * never NULL if decode success.
  325 + * @remark, drop message when msg is empty or payload length is empty.
  326 + */
248 virtual int recv_message(SrsMessage** pmsg); 327 virtual int recv_message(SrsMessage** pmsg);
  328 + /**
  329 + * decode bytes oriented RTMP message to RTMP packet,
  330 + * @param ppacket, output decoded packet,
  331 + * always NULL if error, never NULL if success.
  332 + * @return error when unknown packet, error when decode failed.
  333 + */
249 virtual int decode_message(SrsMessage* msg, SrsPacket** ppacket); 334 virtual int decode_message(SrsMessage* msg, SrsPacket** ppacket);
  335 + /**
  336 + * send the RTMP message and always free it.
  337 + * user must never free or use the msg after this method,
  338 + * for it will always free the msg.
  339 + * @param msg, the msg to send out, never be NULL.
  340 + * @param stream_id, the stream id of packet to send over, 0 for control message.
  341 + */
250 virtual int send_and_free_message(SrsMessage* msg, int stream_id); 342 virtual int send_and_free_message(SrsMessage* msg, int stream_id);
  343 + /**
  344 + * send the RTMP packet and always free it.
  345 + * user must never free or use the packet after this method,
  346 + * for it will always free the packet.
  347 + * @param packet, the packet to send out, never be NULL.
  348 + * @param stream_id, the stream id of packet to send over, 0 for control message.
  349 + */
251 virtual int send_and_free_packet(SrsPacket* packet, int stream_id); 350 virtual int send_and_free_packet(SrsPacket* packet, int stream_id);
252 public: 351 public:
  352 + /**
  353 + * handshake with client, try complex then simple.
  354 + */
253 virtual int handshake(); 355 virtual int handshake();
  356 + /**
  357 + * do connect app with client, to discovery tcUrl.
  358 + */
254 virtual int connect_app(SrsRequest* req); 359 virtual int connect_app(SrsRequest* req);
  360 + /**
  361 + * set ack size to client, client will send ack-size for each ack window
  362 + */
255 virtual int set_window_ack_size(int ack_size); 363 virtual int set_window_ack_size(int ack_size);
256 /** 364 /**
257 * @type: The sender can mark this message hard (0), soft (1), or dynamic (2) 365 * @type: The sender can mark this message hard (0), soft (1), or dynamic (2)
@@ -262,7 +370,13 @@ public: @@ -262,7 +370,13 @@ public:
262 * @param server_ip the ip of server. 370 * @param server_ip the ip of server.
263 */ 371 */
264 virtual int response_connect_app(SrsRequest* req, const char* server_ip = NULL); 372 virtual int response_connect_app(SrsRequest* req, const char* server_ip = NULL);
  373 + /**
  374 + * reject the connect app request.
  375 + */
265 virtual void response_connect_reject(SrsRequest* req, const char* desc); 376 virtual void response_connect_reject(SrsRequest* req, const char* desc);
  377 + /**
  378 + * response client the onBWDone message.
  379 + */
266 virtual int on_bw_done(); 380 virtual int on_bw_done();
267 /** 381 /**
268 * recv some message to identify the client. 382 * recv some message to identify the client.
@@ -5318,6 +5318,60 @@ VOID TEST(ProtocolStackTest, ProtocolPingFlow) @@ -5318,6 +5318,60 @@ VOID TEST(ProtocolStackTest, ProtocolPingFlow)
5318 } 5318 }
5319 } 5319 }
5320 5320
  5321 +/**
  5322 +* expect specified message
  5323 +*/
  5324 +VOID TEST(ProtocolStackTest, ProtocolExcpectMessage)
  5325 +{
  5326 + MockBufferIO bio;
  5327 + SrsProtocol proto(&bio);
  5328 +
  5329 + // packet is SrsConnectAppPacket
  5330 + char data[] = {
  5331 + // 12bytes header, 1byts chunk header, 11bytes msg heder
  5332 + (char)0x03, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x01, (char)0xa1, (char)0x14, (char)0x00, (char)0x00, (char)0x00, (char)0x00,
  5333 + // msg payload start
  5334 + (char)0x02, (char)0x00, (char)0x07, (char)0x63,
  5335 + (char)0x6f, (char)0x6e, (char)0x6e, (char)0x65, (char)0x63, (char)0x74, (char)0x00, (char)0x3f, (char)0xf0, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x03,
  5336 + (char)0x00, (char)0x03, (char)0x61, (char)0x70, (char)0x70, (char)0x02, (char)0x00, (char)0x04, (char)0x6c, (char)0x69, (char)0x76, (char)0x65, (char)0x00, (char)0x08, (char)0x66, (char)0x6c,
  5337 + (char)0x61, (char)0x73, (char)0x68, (char)0x56, (char)0x65, (char)0x72, (char)0x02, (char)0x00, (char)0x0d, (char)0x57, (char)0x49, (char)0x4e, (char)0x20, (char)0x31, (char)0x32, (char)0x2c,
  5338 + (char)0x30, (char)0x2c, (char)0x30, (char)0x2c, (char)0x34, (char)0x31, (char)0x00, (char)0x06, (char)0x73, (char)0x77, (char)0x66, (char)0x55, (char)0x72, (char)0x6c, (char)0x02, (char)0x00,
  5339 + (char)0x51, (char)0x68, (char)0x74, (char)0x74, (char)0x70, (char)0x3a, (char)0x2f, (char)0x2f, (char)0x77, (char)0x77, (char)0x77, (char)0x2e, (char)0x6f, (char)0x73, (char)0x73, (char)0x72,
  5340 + (char)0x73, (char)0x2e, (char)0x6e, (char)0x65, (char)0x74, (char)0x3a, (char)0x38, (char)0x30, (char)0x38, (char)0x35, (char)0x2f, (char)0x70, (char)0x6c, (char)0x61, (char)0x79, (char)0x65,
  5341 + (char)0x72, (char)0x73, (char)0x2f, (char)0x73, (char)0x72, (char)0x73, (char)0x5f, (char)0x70, (char)0x6c, (char)0x61, (char)0x79, (char)0x65, (char)0x72, (char)0x2f, (char)0x72, (char)0x65,
  5342 + (char)0x6c, (char)0x65, (char)0x61, (char)0x73, (char)0x65, (char)0x2f, (char)0x73, (char)0x72, (char)0x73, (char)0x5f, (char)0x70, (char)0x6c,
  5343 + (char)0xC3, /*next chunk.*/ (char)0x61, (char)0x79, (char)0x65, (char)0x72,
  5344 + (char)0x2e, (char)0x73, (char)0x77, (char)0x66, (char)0x3f, (char)0x5f, (char)0x76, (char)0x65, (char)0x72, (char)0x73, (char)0x69, (char)0x6f, (char)0x6e, (char)0x3d, (char)0x31, (char)0x2e,
  5345 + (char)0x32, (char)0x33, (char)0x00, (char)0x05, (char)0x74, (char)0x63, (char)0x55, (char)0x72, (char)0x6c, (char)0x02, (char)0x00, (char)0x14, (char)0x72, (char)0x74, (char)0x6d, (char)0x70,
  5346 + (char)0x3a, (char)0x2f, (char)0x2f, (char)0x64, (char)0x65, (char)0x76, (char)0x3a, (char)0x31, (char)0x39, (char)0x33, (char)0x35, (char)0x2f, (char)0x6c, (char)0x69, (char)0x76, (char)0x65,
  5347 + (char)0x00, (char)0x04, (char)0x66, (char)0x70, (char)0x61, (char)0x64, (char)0x01, (char)0x00, (char)0x00, (char)0x0c, (char)0x63, (char)0x61, (char)0x70, (char)0x61, (char)0x62, (char)0x69,
  5348 + (char)0x6c, (char)0x69, (char)0x74, (char)0x69, (char)0x65, (char)0x73, (char)0x00, (char)0x40, (char)0x6d, (char)0xe0, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,
  5349 + (char)0x0b, (char)0x61, (char)0x75, (char)0x64, (char)0x69, (char)0x6f, (char)0x43, (char)0x6f, (char)0x64, (char)0x65, (char)0x63, (char)0x73, (char)0x00, (char)0x40, (char)0xab, (char)0xee,
  5350 + (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x0b, (char)0x76, (char)0x69, (char)0x64, (char)0x65, (char)0x6f, (char)0x43, (char)0x6f, (char)0x64, (char)0x65,
  5351 + (char)0x63, (char)0x73, (char)0x00, (char)0x40, (char)0x6f, (char)0x80, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,
  5352 + (char)0xC3, /*next chunk.*/ (char)0x0d, (char)0x76, (char)0x69, (char)0x64,
  5353 + (char)0x65, (char)0x6f, (char)0x46, (char)0x75, (char)0x6e, (char)0x63, (char)0x74, (char)0x69, (char)0x6f, (char)0x6e, (char)0x00, (char)0x3f, (char)0xf0, (char)0x00, (char)0x00, (char)0x00,
  5354 + (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x07, (char)0x70, (char)0x61, (char)0x67, (char)0x65, (char)0x55, (char)0x72, (char)0x6c, (char)0x02, (char)0x00, (char)0x62, (char)0x68,
  5355 + (char)0x74, (char)0x74, (char)0x70, (char)0x3a, (char)0x2f, (char)0x2f, (char)0x77, (char)0x77, (char)0x77, (char)0x2e, (char)0x6f, (char)0x73, (char)0x73, (char)0x72, (char)0x73, (char)0x2e,
  5356 + (char)0x6e, (char)0x65, (char)0x74, (char)0x3a, (char)0x38, (char)0x30, (char)0x38, (char)0x35, (char)0x2f, (char)0x70, (char)0x6c, (char)0x61, (char)0x79, (char)0x65, (char)0x72, (char)0x73,
  5357 + (char)0x2f, (char)0x73, (char)0x72, (char)0x73, (char)0x5f, (char)0x70, (char)0x6c, (char)0x61, (char)0x79, (char)0x65, (char)0x72, (char)0x2e, (char)0x68, (char)0x74, (char)0x6d, (char)0x6c,
  5358 + (char)0x3f, (char)0x76, (char)0x68, (char)0x6f, (char)0x73, (char)0x74, (char)0x3d, (char)0x64, (char)0x65, (char)0x76, (char)0x26, (char)0x73, (char)0x74, (char)0x72, (char)0x65, (char)0x61,
  5359 + (char)0x6d, (char)0x3d, (char)0x6c, (char)0x69, (char)0x76, (char)0x65, (char)0x73, (char)0x74, (char)0x72, (char)0x65, (char)0x61, (char)0x6d, (char)0x26, (char)0x73, (char)0x65, (char)0x72,
  5360 + (char)0x76, (char)0x65, (char)0x72, (char)0x3d, (char)0x64, (char)0x65, (char)0x76, (char)0x26, (char)0x70, (char)0x6f, (char)0x72, (char)0x74,
  5361 + (char)0xC3, /*next chunk.*/ (char)0x3d, (char)0x31, (char)0x39, (char)0x33,
  5362 + (char)0x35, (char)0x00, (char)0x0e, (char)0x6f, (char)0x62, (char)0x6a, (char)0x65, (char)0x63, (char)0x74, (char)0x45, (char)0x6e, (char)0x63, (char)0x6f, (char)0x64, (char)0x69, (char)0x6e,
  5363 + (char)0x67, (char)0x00, (char)0x40, (char)0x08, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x09
  5364 + };
  5365 + bio.in_buffer.append(data, sizeof(data));
  5366 +
  5367 + SrsMessage* msg = NULL;
  5368 + SrsConnectAppPacket* pkt = NULL;
  5369 + ASSERT_TRUE(ERROR_SUCCESS == proto.expect_message<SrsConnectAppPacket>(&msg, &pkt));
  5370 + SrsAutoFree(SrsMessage, msg);
  5371 + SrsAutoFree(SrsConnectAppPacket, pkt);
  5372 + ASSERT_TRUE(NULL != pkt);
  5373 +}
  5374 +
5321 VOID TEST(ProtocolRTMPTest, RTMPRequest) 5375 VOID TEST(ProtocolRTMPTest, RTMPRequest)
5322 { 5376 {
5323 SrsRequest req; 5377 SrsRequest req;