wenjiegit

add bandcheck code and modified srs code for merge

@@ -8,16 +8,18 @@ log_dir ./objs/logs; @@ -8,16 +8,18 @@ log_dir ./objs/logs;
8 # if exceed the max connections, server will drop the new connection. 8 # if exceed the max connections, server will drop the new connection.
9 # default: 2000 9 # default: 2000
10 max_connections 2000; 10 max_connections 2000;
  11 +# the default chunk size is 128, max is 65536,
  12 +# some client does not support chunk size change,
  13 +# however, most clients supports it and it can improve
  14 +# performance about 10%.
  15 +# if not specified, set to 4096.
  16 +# priority of chunk size in vhost > priority of chunk size in global.
  17 +chunk_size 65000;
11 # vhost list, the __defaultVhost__ is the default vhost 18 # vhost list, the __defaultVhost__ is the default vhost
12 # for example, user use ip to access the stream: rtmp://192.168.1.2/live/livestream. 19 # for example, user use ip to access the stream: rtmp://192.168.1.2/live/livestream.
13 # for which cannot identify the required vhost. 20 # for which cannot identify the required vhost.
14 # for default demo. 21 # for default demo.
15 vhost __defaultVhost__ { 22 vhost __defaultVhost__ {
16 - # the default chunk size is 128, max is 65536,  
17 - # some client does not support chunk size change,  
18 - # however, most clients supports it and it can improve  
19 - # performance about 10%.  
20 - # if not specified, set to 4096.  
21 chunk_size 65000; 23 chunk_size 65000;
22 enabled on; 24 enabled on;
23 gop_cache on; 25 gop_cache on;
@@ -157,9 +159,8 @@ vhost bandcheck.srs.com { @@ -157,9 +159,8 @@ vhost bandcheck.srs.com {
157 bandcheck{ 159 bandcheck{
158 enabled on; 160 enabled on;
159 key test kate; 161 key test kate;
160 - interval 30;  
161 - max_play_kbps 45000;  
162 - max_pub_kbps 25000; 162 + interval 5;
  163 + limit_kbps 4000;
163 } 164 }
164 } 165 }
165 166
@@ -747,4 +748,3 @@ pithy_print { @@ -747,4 +748,3 @@ pithy_print {
747 hls 3000; 748 hls 3000;
748 } 749 }
749 750
750 -  
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 The MIT License (MIT) 2 The MIT License (MIT)
3 3
4 Copyright (c) 2013 winlin 4 Copyright (c) 2013 winlin
  5 +Copyright (c) 2013 wenjiegit
5 6
6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in 8 this software and associated documentation files (the "Software"), to deal in
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 The MIT License (MIT) 2 The MIT License (MIT)
3 3
4 Copyright (c) 2013 winlin 4 Copyright (c) 2013 winlin
  5 +Copyright (c) 2013 wenjiegit
5 6
6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in 8 this software and associated documentation files (the "Software"), to deal in
@@ -78,8 +79,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -78,8 +79,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
78 #define RTMP_SIG_SRS_WEB "http://blog.csdn.net/win_lin" 79 #define RTMP_SIG_SRS_WEB "http://blog.csdn.net/win_lin"
79 #define RTMP_SIG_SRS_EMAIL "winterserver@126.com" 80 #define RTMP_SIG_SRS_EMAIL "winterserver@126.com"
80 #define RTMP_SIG_SRS_LICENSE "The MIT License (MIT)" 81 #define RTMP_SIG_SRS_LICENSE "The MIT License (MIT)"
81 -#define RTMP_SIG_SRS_COPYRIGHT "Copyright (c) 2013 winlin"  
82 -#define RTMP_SIG_SRS_CONTRIBUTOR "winlin" 82 +#define RTMP_SIG_SRS_COPYRIGHT "Copyright (c) 2013 winlin,wenjiegit"
  83 +#define RTMP_SIG_SRS_CONTRIBUTOR "winlin,wenjiegit"
83 84
84 // compare 85 // compare
85 #define srs_min(a, b) (((a) < (b))? (a) : (b)) 86 #define srs_min(a, b) (((a) < (b))? (a) : (b))
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 The MIT License (MIT) 2 The MIT License (MIT)
3 3
4 Copyright (c) 2013 winlin 4 Copyright (c) 2013 winlin
  5 +Copyright (c) 2013 wenjiegit
5 6
6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in 8 this software and associated documentation files (the "Software"), to deal in
@@ -153,32 +154,35 @@ int SrsClient::service_cycle() @@ -153,32 +154,35 @@ int SrsClient::service_cycle()
153 } 154 }
154 srs_verbose("set peer bandwidth success"); 155 srs_verbose("set peer bandwidth success");
155 156
156 - if(config->get_bw_check_enabled(req->vhost, req->bw_key))  
157 - { 157 + if (config->get_bw_check_enabled(req->vhost, req->bw_key)) {
158 static int64_t last_check_time_ms = srs_get_system_time_ms(); 158 static int64_t last_check_time_ms = srs_get_system_time_ms();
159 int64_t interval_ms = 0; 159 int64_t interval_ms = 0;
160 - int play_kbps = 0;  
161 - int pub_kbps = 0;  
162 - config->get_bw_check_settings(req->vhost, interval_ms, play_kbps, pub_kbps); 160 + int limit_kbps = 0;
  161 +
  162 + config->get_bw_check_settings(req->vhost, interval_ms, limit_kbps);
163 163
164 if((srs_get_system_time_ms() - last_check_time_ms) < interval_ms 164 if((srs_get_system_time_ms() - last_check_time_ms) < interval_ms
165 && last_check_time_ms != srs_get_system_time_ms()) 165 && last_check_time_ms != srs_get_system_time_ms())
166 { 166 {
167 srs_trace("bandcheck interval less than limted interval. last time=%lld, current time=%lld" 167 srs_trace("bandcheck interval less than limted interval. last time=%lld, current time=%lld"
168 , last_check_time_ms, srs_get_system_time_ms()); 168 , last_check_time_ms, srs_get_system_time_ms());
  169 +
169 return rtmp->response_connect_reject(req, "your bandcheck frequency is too high!"); 170 return rtmp->response_connect_reject(req, "your bandcheck frequency is too high!");
170 } else { 171 } else {
171 last_check_time_ms = srs_get_system_time_ms(); // update last check time 172 last_check_time_ms = srs_get_system_time_ms(); // update last check time
172 char* local_ip = 0; 173 char* local_ip = 0;
173 - if((ret = get_local_ip(local_ip)) != ERROR_SUCCESS){ 174 +
  175 + if ((ret = get_local_ip(local_ip)) != ERROR_SUCCESS) {
174 srs_error("get local ip failed. ret = %d", ret); 176 srs_error("get local ip failed. ret = %d", ret);
175 return ret; 177 return ret;
176 } 178 }
  179 +
177 if ((ret = rtmp->response_connect_app(req, local_ip)) != ERROR_SUCCESS) { 180 if ((ret = rtmp->response_connect_app(req, local_ip)) != ERROR_SUCCESS) {
178 srs_error("response connect app failed. ret=%d", ret); 181 srs_error("response connect app failed. ret=%d", ret);
179 return ret; 182 return ret;
180 } 183 }
181 - return rtmp->start_bandwidth_check(play_kbps, pub_kbps); 184 +
  185 + return rtmp->start_bandwidth_check(limit_kbps);
182 } 186 }
183 } 187 }
184 188
@@ -203,7 +207,7 @@ int SrsClient::service_cycle() @@ -203,7 +207,7 @@ int SrsClient::service_cycle()
203 req->strip(); 207 req->strip();
204 srs_trace("identify client success. type=%d, stream_name=%s", type, req->stream.c_str()); 208 srs_trace("identify client success. type=%d, stream_name=%s", type, req->stream.c_str());
205 209
206 - int chunk_size = config->get_chunk_size(); 210 + int chunk_size = config->get_chunk_size(req->vhost);
207 if ((ret = rtmp->set_chunk_size(chunk_size)) != ERROR_SUCCESS) { 211 if ((ret = rtmp->set_chunk_size(chunk_size)) != ERROR_SUCCESS) {
208 srs_error("set chunk_size=%d failed. ret=%d", chunk_size, ret); 212 srs_error("set chunk_size=%d failed. ret=%d", chunk_size, ret);
209 return ret; 213 return ret;
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 The MIT License (MIT) 2 The MIT License (MIT)
3 3
4 Copyright (c) 2013 winlin 4 Copyright (c) 2013 winlin
  5 +Copyright (c) 2013 wenjiegit
5 6
6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in 8 this software and associated documentation files (the "Software"), to deal in
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 The MIT License (MIT) 2 The MIT License (MIT)
3 3
4 Copyright (c) 2013 winlin 4 Copyright (c) 2013 winlin
  5 +Copyright (c) 2013 wenjiegit
5 6
6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in 8 this software and associated documentation files (the "Software"), to deal in
@@ -1449,7 +1450,23 @@ int SrsConfig::get_chunk_size() @@ -1449,7 +1450,23 @@ int SrsConfig::get_chunk_size()
1449 return SRS_CONF_DEFAULT_CHUNK_SIZE; 1450 return SRS_CONF_DEFAULT_CHUNK_SIZE;
1450 } 1451 }
1451 1452
1452 - return ::atoi(conf->arg0().c_str()); 1453 + return ::atoi(conf->arg0().c_str());
  1454 +}
  1455 +
  1456 +int SrsConfig::get_chunk_size(const std::string &vhost)
  1457 +{
  1458 + SrsConfDirective* conf = get_vhost(vhost);
  1459 +
  1460 + if (!conf) {
  1461 + return get_chunk_size();
  1462 + }
  1463 +
  1464 + SrsConfDirective* conf_vhost = conf->get("chunk_size");
  1465 + if (!conf_vhost) {
  1466 + return get_chunk_size();
  1467 + }
  1468 +
  1469 + return ::atoi(conf_vhost->arg0().c_str());
1453 } 1470 }
1454 1471
1455 int SrsConfig::get_pithy_print_publish() 1472 int SrsConfig::get_pithy_print_publish()
@@ -1524,12 +1541,11 @@ bool SrsConfig::get_bw_check_enabled(const std::string &vhost, const std::string @@ -1524,12 +1541,11 @@ bool SrsConfig::get_bw_check_enabled(const std::string &vhost, const std::string
1524 return false; 1541 return false;
1525 } 1542 }
1526 1543
1527 -void SrsConfig::get_bw_check_settings(const std::string &vhost, int64_t &interval_ms, int &play_kbps, int &pub_kbps) 1544 +void SrsConfig::get_bw_check_settings(const std::string &vhost, int64_t &interval_ms, int &limit_kbps)
1528 { 1545 {
1529 // set default value; 1546 // set default value;
1530 interval_ms = 30 * 1000; 1547 interval_ms = 30 * 1000;
1531 - play_kbps = 45000;  
1532 - pub_kbps = 25000; 1548 + limit_kbps = 32000;
1533 1549
1534 SrsConfDirective* conf = get_vhost(vhost); 1550 SrsConfDirective* conf = get_vhost(vhost);
1535 if (!conf) { 1551 if (!conf) {
@@ -1537,20 +1553,19 @@ void SrsConfig::get_bw_check_settings(const std::string &vhost, int64_t &interva @@ -1537,20 +1553,19 @@ void SrsConfig::get_bw_check_settings(const std::string &vhost, int64_t &interva
1537 } 1553 }
1538 1554
1539 SrsConfDirective* bw_test = conf->get("bandcheck"); 1555 SrsConfDirective* bw_test = conf->get("bandcheck");
1540 - if(!bw_test) 1556 + if (!bw_test) {
1541 return; 1557 return;
  1558 + }
1542 1559
1543 SrsConfDirective* interval_conf = bw_test->get("interval"); 1560 SrsConfDirective* interval_conf = bw_test->get("interval");
1544 - if(interval_conf) 1561 + if (interval_conf) {
1545 interval_ms = ::atoll(interval_conf->arg0().c_str()) * 1000; 1562 interval_ms = ::atoll(interval_conf->arg0().c_str()) * 1000;
  1563 + }
1546 1564
1547 - SrsConfDirective* play_conf = bw_test->get("max_play_kbps");  
1548 - if(play_conf)  
1549 - play_kbps = ::atoi(play_conf->arg0().c_str());  
1550 -  
1551 - SrsConfDirective* pub_conf = bw_test->get("max_pub_kbps");  
1552 - if(pub_conf)  
1553 - pub_kbps = ::atoi(pub_conf->arg0().c_str()); 1565 + SrsConfDirective* limit_kbps_conf = bw_test->get("limit_kbps");
  1566 + if (limit_kbps_conf) {
  1567 + limit_kbps = ::atoi(limit_kbps_conf->arg0().c_str());
  1568 + }
1554 } 1569 }
1555 1570
1556 int SrsConfig::get_pithy_print_encoder() 1571 int SrsConfig::get_pithy_print_encoder()
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 The MIT License (MIT) 2 The MIT License (MIT)
3 3
4 Copyright (c) 2013 winlin 4 Copyright (c) 2013 winlin
  5 +Copyright (c) 2013 wenjiegit
5 6
6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in 8 this software and associated documentation files (the "Software"), to deal in
@@ -164,13 +165,14 @@ public: @@ -164,13 +165,14 @@ public:
164 virtual SrsConfDirective* get_refer_publish(std::string vhost); 165 virtual SrsConfDirective* get_refer_publish(std::string vhost);
165 virtual SrsConfDirective* get_listen(); 166 virtual SrsConfDirective* get_listen();
166 virtual int get_chunk_size(); 167 virtual int get_chunk_size();
  168 + virtual int get_chunk_size(const std::string& vhost);
167 virtual int get_pithy_print_publish(); 169 virtual int get_pithy_print_publish();
168 virtual int get_pithy_print_forwarder(); 170 virtual int get_pithy_print_forwarder();
169 virtual int get_pithy_print_encoder(); 171 virtual int get_pithy_print_encoder();
170 virtual int get_pithy_print_hls(); 172 virtual int get_pithy_print_hls();
171 virtual int get_pithy_print_play(); 173 virtual int get_pithy_print_play();
172 virtual bool get_bw_check_enabled(const std::string &vhost, const std::string &key); 174 virtual bool get_bw_check_enabled(const std::string &vhost, const std::string &key);
173 - virtual void get_bw_check_settings(const std::string &vhost, int64_t &interval_ms, int &play_kbps, int &pub_kbps); 175 + virtual void get_bw_check_settings(const std::string &vhost, int64_t &interval_ms, int &limit_kbps);
174 }; 176 };
175 177
176 /** 178 /**
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 The MIT License (MIT) 2 The MIT License (MIT)
3 3
4 Copyright (c) 2013 winlin 4 Copyright (c) 2013 winlin
  5 +Copyright (c) 2013 wenjiegit
5 6
6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in 8 this software and associated documentation files (the "Software"), to deal in
@@ -51,7 +52,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -51,7 +52,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
51 #define ERROR_SOCKET_WRITE 209 52 #define ERROR_SOCKET_WRITE 209
52 #define ERROR_SOCKET_WAIT 210 53 #define ERROR_SOCKET_WAIT 210
53 #define ERROR_SOCKET_TIMEOUT 211 54 #define ERROR_SOCKET_TIMEOUT 211
54 -#define ERROR_SOCKET_GET_LOCAL_IP 222 55 +#define ERROR_SOCKET_GET_LOCAL_IP 212
55 56
56 #define ERROR_RTMP_PLAIN_REQUIRED 300 57 #define ERROR_RTMP_PLAIN_REQUIRED 300
57 #define ERROR_RTMP_CHUNK_START 301 58 #define ERROR_RTMP_CHUNK_START 301
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 The MIT License (MIT) 2 The MIT License (MIT)
3 3
4 Copyright (c) 2013 winlin 4 Copyright (c) 2013 winlin
  5 +Copyright (c) 2013 wenjiegit
5 6
6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in 8 this software and associated documentation files (the "Software"), to deal in
@@ -1355,10 +1356,6 @@ int SrsCommonMessage::decode_packet(SrsProtocol* protocol) @@ -1355,10 +1356,6 @@ int SrsCommonMessage::decode_packet(SrsProtocol* protocol)
1355 srs_verbose("start to decode set chunk size message."); 1356 srs_verbose("start to decode set chunk size message.");
1356 packet = new SrsSetChunkSizePacket(); 1357 packet = new SrsSetChunkSizePacket();
1357 return packet->decode(stream); 1358 return packet->decode(stream);
1358 - } else if(header.is_windows_ackledgement()) {  
1359 - srs_verbose("start to decode AcknowledgementPacket message.");  
1360 - packet = new SrsAcknowledgementPacket();  
1361 - return packet->decode(stream);  
1362 } else { 1359 } else {
1363 // default packet to drop message. 1360 // default packet to drop message.
1364 srs_trace("drop the unknown message, type=%d", header.message_type); 1361 srs_trace("drop the unknown message, type=%d", header.message_type);
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 The MIT License (MIT) 2 The MIT License (MIT)
3 3
4 Copyright (c) 2013 winlin 4 Copyright (c) 2013 winlin
  5 +Copyright (c) 2013 wenjiegit
5 6
6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in 8 this software and associated documentation files (the "Software"), to deal in
@@ -67,6 +68,25 @@ class ISrsMessage; @@ -67,6 +68,25 @@ class ISrsMessage;
67 #define RTMP_MAX_FMT3_HEADER_SIZE 5 68 #define RTMP_MAX_FMT3_HEADER_SIZE 5
68 69
69 /** 70 /**
  71 +* band width check method name, which will be invoked by client.
  72 +* band width check mothods use SrsOnStatusCallPacket as its internal packet type,
  73 +* so ensure you set command name when you use it.
  74 +*/
  75 +// for play
  76 +#define SRS_BW_CHECK_START_PLAY "onSrsBandCheckStartPlayBytes"
  77 +#define SRS_BW_CHECK_STARTING_PLAY "onSrsBandCheckStartingPlayBytes"
  78 +#define SRS_BW_CHECK_STOP_PLAY "onSrsBandCheckStopPlayBytes"
  79 +#define SRS_BW_CHECK_STOPPED_PLAY "onSrsBandCheckStoppedPlayBytes"
  80 +#define SRS_BW_CHECK_PLAYING "onSrsBandCheckPlaying"
  81 +
  82 +// for publish
  83 +#define SRS_BW_CHECK_START_PUBLISH "onSrsBandCheckStartPublishBytes"
  84 +#define SRS_BW_CHECK_STARTING_PUBLISH "onSrsBandCheckStartingPublishBytes"
  85 +#define SRS_BW_CHECK_STOP_PUBLISH "onSrsBandCheckStopPublishBytes"
  86 +#define SRS_BW_CHECK_FINISHED "onSrsBandCheckFinished"
  87 +#define SRS_BW_CHECK_PUBLISHING "onSrsBandCheckPublishing"
  88 +
  89 +/**
70 * the protocol provides the rtmp-message-protocol services, 90 * the protocol provides the rtmp-message-protocol services,
71 * to recv RTMP message from RTMP chunk stream, 91 * to recv RTMP message from RTMP chunk stream,
72 * and to send out RTMP message over RTMP chunk stream. 92 * and to send out RTMP message over RTMP chunk stream.
@@ -802,26 +822,6 @@ protected: @@ -802,26 +822,6 @@ protected:
802 virtual int encode_packet(SrsStream* stream); 822 virtual int encode_packet(SrsStream* stream);
803 }; 823 };
804 824
805 -  
806 -/**  
807 -* band width check method name, which will be invoked by client.  
808 -* band width check mothods use SrsOnStatusCallPacket as its internal packet type,  
809 -* so ensure you set command name when you use it.  
810 -*/  
811 -// for play  
812 -#define SRS_BW_CHECK_START_PLAY "onSrsBandCheckStartPlayBytes"  
813 -#define SRS_BW_CHECK_STARTING_PLAY "onSrsBandCheckStartingPlayBytes"  
814 -#define SRS_BW_CHECK_STOP_PLAY "onSrsBandCheckStopPlayBytes"  
815 -#define SRS_BW_CHECK_STOPPED_PLAY "onSrsBandCheckStoppedPlayBytes"  
816 -#define SRS_BW_CHECK_PLAYING "onSrsBandCheckPlaying"  
817 -  
818 -// for publish  
819 -#define SRS_BW_CHECK_START_PUBLISH "onSrsBandCheckStartPublishBytes"  
820 -#define SRS_BW_CHECK_STARTING_PUBLISH "onSrsBandCheckStartingPublishBytes"  
821 -#define SRS_BW_CHECK_STOP_PUBLISH "onSrsBandCheckStopPublishBytes"  
822 -#define SRS_BW_CHECK_FINISHED "onSrsBandCheckFinished"  
823 -#define SRS_BW_CHECK_PUBLISHING "onSrsBandCheckPublishing"  
824 -  
825 /** 825 /**
826 * onStatus command, AMF0 Call 826 * onStatus command, AMF0 Call
827 * @remark, user must set the stream_id by SrsMessage.set_packet(). 827 * @remark, user must set the stream_id by SrsMessage.set_packet().
@@ -1106,7 +1106,7 @@ int SrsRtmp::start_flash_publish(int stream_id) @@ -1106,7 +1106,7 @@ int SrsRtmp::start_flash_publish(int stream_id)
1106 return ret; 1106 return ret;
1107 } 1107 }
1108 1108
1109 -int SrsRtmp::start_bandwidth_check(int max_play_kbps, int max_pub_kbps) 1109 +int SrsRtmp::start_bandwidth_check(int limit_kbps)
1110 { 1110 {
1111 int ret = ERROR_SUCCESS; 1111 int ret = ERROR_SUCCESS;
1112 1112
@@ -1121,11 +1121,11 @@ int SrsRtmp::start_bandwidth_check(int max_play_kbps, int max_pub_kbps) @@ -1121,11 +1121,11 @@ int SrsRtmp::start_bandwidth_check(int max_play_kbps, int max_pub_kbps)
1121 int publish_bytes = 0; 1121 int publish_bytes = 0;
1122 1122
1123 int64_t start_time = srs_get_system_time_ms(); 1123 int64_t start_time = srs_get_system_time_ms();
1124 - if((ret = bandwidth_check_play(play_duration_ms, play_interval_ms,  
1125 - play_actual_duration_ms, play_bytes, max_play_kbps) != ERROR_SUCCESS) 1124 + if ((ret = bandwidth_check_play(play_duration_ms, play_interval_ms,
  1125 + play_actual_duration_ms, play_bytes, limit_kbps) != ERROR_SUCCESS)
1126 || (ret = bandwidth_check_publish(publish_duration_ms, publish_interval_ms, 1126 || (ret = bandwidth_check_publish(publish_duration_ms, publish_interval_ms,
1127 - publish_actual_duration_ms, publish_bytes, max_pub_kbps)) != ERROR_SUCCESS)  
1128 - { 1127 + publish_actual_duration_ms, publish_bytes, limit_kbps)) != ERROR_SUCCESS) {
  1128 +
1129 srs_error("band width check failed. ret = %d", ret); 1129 srs_error("band width check failed. ret = %d", ret);
1130 1130
1131 return ret; 1131 return ret;
@@ -1272,8 +1272,7 @@ int SrsRtmp::bandwidth_check_play(int duration_ms, int interval_ms, int &actual_ @@ -1272,8 +1272,7 @@ int SrsRtmp::bandwidth_check_play(int duration_ms, int interval_ms, int &actual_
1272 // recv client's starting play response 1272 // recv client's starting play response
1273 while (true) { 1273 while (true) {
1274 SrsCommonMessage* msg = 0; 1274 SrsCommonMessage* msg = 0;
1275 - if( (ret = protocol->recv_message(&msg)) != ERROR_SUCCESS)  
1276 - { 1275 + if ( (ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) {
1277 srs_error("recv client's starting play response failed. ret= %d", ret); 1276 srs_error("recv client's starting play response failed. ret= %d", ret);
1278 return ret; 1277 return ret;
1279 } 1278 }
@@ -1286,21 +1285,19 @@ int SrsRtmp::bandwidth_check_play(int duration_ms, int interval_ms, int &actual_ @@ -1286,21 +1285,19 @@ int SrsRtmp::bandwidth_check_play(int duration_ms, int interval_ms, int &actual_
1286 srs_trace("BW check recv play begin response."); 1285 srs_trace("BW check recv play begin response.");
1287 1286
1288 // send play data to client 1287 // send play data to client
1289 - int64_t current_Time = srs_get_system_time_ms();  
1290 - int size = 1024*4; // 32KB 1288 + int64_t current_time = srs_get_system_time_ms();
  1289 + int size = 1024;
1291 char random_data[size]; 1290 char random_data[size];
1292 memset(random_data, 0x01, size); 1291 memset(random_data, 0x01, size);
1293 1292
1294 - int64_t last_time = current_Time;  
1295 int interval = 0; 1293 int interval = 0;
1296 - while ( (srs_get_system_time_ms() - current_Time) < duration_ms ){ 1294 + while ( (srs_get_system_time_ms() - current_time) < duration_ms ) {
1297 st_usleep(interval); 1295 st_usleep(interval);
1298 SrsCommonMessage* msg = new SrsCommonMessage; 1296 SrsCommonMessage* msg = new SrsCommonMessage;
1299 SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket; 1297 SrsOnStatusCallPacket* pkt = new SrsOnStatusCallPacket;
1300 pkt->command_name = SRS_BW_CHECK_PLAYING; 1298 pkt->command_name = SRS_BW_CHECK_PLAYING;
1301 1299
1302 - for(int i = 0; i < 10; ++i)  
1303 - { 1300 + for (int i = 0; i < 100; ++i) {
1304 char buf[32]; 1301 char buf[32];
1305 sprintf(buf, "%d", i); 1302 sprintf(buf, "%d", i);
1306 pkt->data->set(buf, new SrsAmf0String(random_data)); 1303 pkt->data->set(buf, new SrsAmf0String(random_data));
@@ -1310,22 +1307,24 @@ int SrsRtmp::bandwidth_check_play(int duration_ms, int interval_ms, int &actual_ @@ -1310,22 +1307,24 @@ int SrsRtmp::bandwidth_check_play(int duration_ms, int interval_ms, int &actual_
1310 play_bytes += pkt->get_payload_length(); 1307 play_bytes += pkt->get_payload_length();
1311 1308
1312 if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) { 1309 if ((ret = protocol->send_message(msg)) != ERROR_SUCCESS) {
1313 - srs_error("send bandwidth check play messages failed. ret=%d", ret);  
1314 - return ret;  
1315 - }  
1316 -  
1317 - if((srs_get_system_time_ms() - last_time) > 5){ // check kbps every 5 ms;  
1318 - int kbps = play_bytes * 8 / (srs_get_system_time_ms() - current_Time);  
1319 - if(kbps > max_play_kbps){  
1320 - interval += 1000*3; // 2 ms 1310 + srs_error("send bandwidth check play messages failed. ret=%d", ret);
  1311 + return ret;
  1312 + }
  1313 +
  1314 + // sleep while current kbps <= max_play_kbps
  1315 + int kbps = 0;
  1316 + while (true) {
  1317 + if(srs_get_system_time_ms() - current_time != 0)
  1318 + kbps = play_bytes * 8 / (srs_get_system_time_ms() - current_time);
  1319 +
  1320 + if (kbps > max_play_kbps) {
  1321 + st_usleep(500);
1321 } else { 1322 } else {
1322 - interval -= 1000*3;  
1323 - if(interval < 0)  
1324 - interval = 0; 1323 + break;
1325 } 1324 }
1326 } 1325 }
1327 } 1326 }
1328 - actual_duration_ms = srs_get_system_time_ms() - current_Time; 1327 + actual_duration_ms = srs_get_system_time_ms() - current_time;
1329 srs_trace("BW check send play bytes over."); 1328 srs_trace("BW check send play bytes over.");
1330 1329
1331 // notify client to stop play 1330 // notify client to stop play
@@ -1348,8 +1347,7 @@ int SrsRtmp::bandwidth_check_play(int duration_ms, int interval_ms, int &actual_ @@ -1348,8 +1347,7 @@ int SrsRtmp::bandwidth_check_play(int duration_ms, int interval_ms, int &actual_
1348 // recv client's stop play response. 1347 // recv client's stop play response.
1349 while (true) { 1348 while (true) {
1350 SrsCommonMessage* msg = 0; 1349 SrsCommonMessage* msg = 0;
1351 - if((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS)  
1352 - { 1350 + if ((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) {
1353 srs_error("recv client's stop play response failed. ret = %d", ret); 1351 srs_error("recv client's stop play response failed. ret = %d", ret);
1354 return ret; 1352 return ret;
1355 } 1353 }
@@ -1386,8 +1384,7 @@ int SrsRtmp::bandwidth_check_publish(int duration_ms, int interval_ms, int &actu @@ -1386,8 +1384,7 @@ int SrsRtmp::bandwidth_check_publish(int duration_ms, int interval_ms, int &actu
1386 // read client's notification of starting publish 1384 // read client's notification of starting publish
1387 while (true) { 1385 while (true) {
1388 SrsCommonMessage* msg = 0; 1386 SrsCommonMessage* msg = 0;
1389 - if((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS)  
1390 - { 1387 + if ((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) {
1391 srs_error("recv client's notification of starting publish failed. ret = %d", ret); 1388 srs_error("recv client's notification of starting publish failed. ret = %d", ret);
1392 return ret; 1389 return ret;
1393 } 1390 }
@@ -1401,12 +1398,8 @@ int SrsRtmp::bandwidth_check_publish(int duration_ms, int interval_ms, int &actu @@ -1401,12 +1398,8 @@ int SrsRtmp::bandwidth_check_publish(int duration_ms, int interval_ms, int &actu
1401 1398
1402 // recv publish msgs until @duration_ms ms 1399 // recv publish msgs until @duration_ms ms
1403 int64_t current_time = srs_get_system_time_ms(); 1400 int64_t current_time = srs_get_system_time_ms();
1404 - int64_t last_time = current_time;  
1405 - int interval = 0;  
1406 -  
1407 - while( (srs_get_system_time_ms() - current_time) < duration_ms )  
1408 - {  
1409 - st_usleep(interval); 1401 + while ( (srs_get_system_time_ms() - current_time) < duration_ms ) {
  1402 + st_usleep(0);
1410 SrsCommonMessage* msg = NULL; 1403 SrsCommonMessage* msg = NULL;
1411 if ((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) { 1404 if ((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) {
1412 srs_error("recv message failed. ret=%d", ret); 1405 srs_error("recv message failed. ret=%d", ret);
@@ -1416,14 +1409,15 @@ int SrsRtmp::bandwidth_check_publish(int duration_ms, int interval_ms, int &actu @@ -1416,14 +1409,15 @@ int SrsRtmp::bandwidth_check_publish(int duration_ms, int interval_ms, int &actu
1416 1409
1417 publish_bytes += msg->header.payload_length; 1410 publish_bytes += msg->header.payload_length;
1418 1411
1419 - if((srs_get_system_time_ms() - last_time) > 5){ // check kbps every 5 ms;  
1420 - int kbps = publish_bytes * 8 / (srs_get_system_time_ms() - current_time);  
1421 - if(kbps > max_pub_kbps){  
1422 - interval += 1000*3; // 2 ms 1412 + int kbps = 0;
  1413 + while (true) {
  1414 + if(srs_get_system_time_ms() - current_time != 0)
  1415 + kbps = publish_bytes * 8 / (srs_get_system_time_ms() - current_time);
  1416 +
  1417 + if (kbps > max_pub_kbps) {
  1418 + st_usleep(500);
1423 } else { 1419 } else {
1424 - interval -= 1000*3;  
1425 - if(interval < 0)  
1426 - interval = 0; 1420 + break;
1427 } 1421 }
1428 } 1422 }
1429 } 1423 }
@@ -1448,11 +1442,10 @@ int SrsRtmp::bandwidth_check_publish(int duration_ms, int interval_ms, int &actu @@ -1448,11 +1442,10 @@ int SrsRtmp::bandwidth_check_publish(int duration_ms, int interval_ms, int &actu
1448 1442
1449 // recv left msg 1443 // recv left msg
1450 while (true) { 1444 while (true) {
1451 - if((ret = st_netfd_poll(stfd, POLLIN, 1000*500)) == ERROR_SUCCESS)  
1452 - { 1445 + if((ret = st_netfd_poll(stfd, POLLIN, 1000*500)) == ERROR_SUCCESS) {
1453 SrsCommonMessage* msg = 0; 1446 SrsCommonMessage* msg = 0;
1454 - if((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS)  
1455 - { 1447 +
  1448 + if ((ret = protocol->recv_message(&msg)) != ERROR_SUCCESS) {
1456 srs_error("recv client's left msg failed, ret = %d", ret); 1449 srs_error("recv client's left msg failed, ret = %d", ret);
1457 return ret; 1450 return ret;
1458 } 1451 }
@@ -225,7 +225,7 @@ public: @@ -225,7 +225,7 @@ public:
225 /** 225 /**
226 * used to process band width check from client. 226 * used to process band width check from client.
227 */ 227 */
228 - virtual int start_bandwidth_check(int max_play_kbps, int max_pub_kbps); 228 + virtual int start_bandwidth_check(int limit_kbps);
229 229
230 private: 230 private:
231 virtual int identify_create_stream_client(SrsCreateStreamPacket* req, int stream_id, SrsClientType& type, std::string& stream_name); 231 virtual int identify_create_stream_client(SrsCreateStreamPacket* req, int stream_id, SrsClientType& type, std::string& stream_name);