winlin

refine the bandwidth server-side, use kbps limit and sample. 0.9.157

@@ -51,8 +51,11 @@ SrsBandwidthSample::~SrsBandwidthSample() @@ -51,8 +51,11 @@ SrsBandwidthSample::~SrsBandwidthSample()
51 { 51 {
52 } 52 }
53 53
54 -void SrsBandwidthSample::calc_kbps() 54 +void SrsBandwidthSample::calc_kbps(int _bytes, int _duration)
55 { 55 {
  56 + bytes = (int)_bytes;
  57 + actual_duration_ms = (int)_duration;
  58 +
56 if (actual_duration_ms <= 0) { 59 if (actual_duration_ms <= 0) {
57 return; 60 return;
58 } 61 }
@@ -144,7 +147,6 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) @@ -144,7 +147,6 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit)
144 return ret; 147 return ret;
145 } 148 }
146 149
147 - play_sample.calc_kbps();  
148 srs_info("stop play test. kbps=%d", play_sample.kbps); 150 srs_info("stop play test. kbps=%d", play_sample.kbps);
149 151
150 // sample publish 152 // sample publish
@@ -155,7 +157,6 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) @@ -155,7 +157,6 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit)
155 return ret; 157 return ret;
156 } 158 }
157 159
158 - publish_sample.calc_kbps();  
159 srs_info("stop publish test. kbps=%d", publish_sample.kbps); 160 srs_info("stop publish test. kbps=%d", publish_sample.kbps);
160 161
161 // stop test. 162 // stop test.
@@ -249,15 +250,14 @@ int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit) @@ -249,15 +250,14 @@ int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
249 srs_info("BW check recv play begin response."); 250 srs_info("BW check recv play begin response.");
250 251
251 // send play data to client 252 // send play data to client
252 - int64_t current_time = srs_get_system_time_ms();  
253 int size = 1024; // TODO: FIXME: magic number 253 int size = 1024; // TODO: FIXME: magic number
254 char random_data[size]; 254 char random_data[size];
255 memset(random_data, 'A', size); 255 memset(random_data, 'A', size);
256 256
257 - int interval = 0;  
258 int data_count = 1; 257 int data_count = 1;
259 - while ((srs_get_system_time_ms() - current_time) < duration_ms) {  
260 - st_usleep(interval); 258 + int64_t starttime = srs_get_system_time_ms();
  259 + while ((srs_get_system_time_ms() - starttime) < sample->duration_ms) {
  260 + st_usleep(sample->interval_ms);
261 261
262 // TODO: FIXME: use shared ptr message. 262 // TODO: FIXME: use shared ptr message.
263 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_playing(); 263 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_playing();
@@ -271,38 +271,24 @@ int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit) @@ -271,38 +271,24 @@ int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
271 } 271 }
272 data_count += 2; 272 data_count += 2;
273 273
274 - // get length from the rtmp protocol stack.  
275 - play_bytes = _rtmp->get_send_bytes();  
276 -  
277 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 274 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
278 srs_error("send bandwidth check play messages failed. ret=%d", ret); 275 srs_error("send bandwidth check play messages failed. ret=%d", ret);
279 return ret; 276 return ret;
280 } 277 }
281 -  
282 - // sleep while current kbps <= max_play_kbps  
283 - int kbps = 0;  
284 - while (true) {  
285 - if(srs_get_system_time_ms() - current_time != 0)  
286 - kbps = play_bytes * 8 / (srs_get_system_time_ms() - current_time);  
287 -  
288 - if (kbps > max_play_kbps) {  
289 - st_usleep(500);  
290 - } else {  
291 - break;  
292 - }  
293 - } 278 +
  279 + limit->send_limit();
294 } 280 }
295 - actual_duration_ms = srs_get_system_time_ms() - current_time; 281 + sample->calc_kbps(_rtmp->get_send_bytes(), srs_get_system_time_ms() - starttime);
296 srs_info("BW check send play bytes over."); 282 srs_info("BW check send play bytes over.");
297 283
298 if (true) { 284 if (true) {
299 // notify client to stop play 285 // notify client to stop play
300 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_play(); 286 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_play();
301 287
302 - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));  
303 - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));  
304 - pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms));  
305 - pkt->data->set("bytes_delta", SrsAmf0Any::number(play_bytes)); 288 + pkt->data->set("duration_ms", SrsAmf0Any::number(sample->duration_ms));
  289 + pkt->data->set("interval_ms", SrsAmf0Any::number(sample->interval_ms));
  290 + pkt->data->set("duration_delta", SrsAmf0Any::number(sample->actual_duration_ms));
  291 + pkt->data->set("bytes_delta", SrsAmf0Any::number(sample->bytes));
306 292
307 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 293 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
308 srs_error("send bandwidth check stop play message failed. ret=%d", ret); 294 srs_error("send bandwidth check stop play message failed. ret=%d", ret);
@@ -340,8 +326,8 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit) @@ -340,8 +326,8 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
340 // notify client to start publish 326 // notify client to start publish
341 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_publish(); 327 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_publish();
342 328
343 - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));  
344 - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); 329 + pkt->data->set("duration_ms", SrsAmf0Any::number(sample->duration_ms));
  330 + pkt->data->set("interval_ms", SrsAmf0Any::number(sample->interval_ms));
345 331
346 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 332 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
347 srs_error("send bandwidth check start publish message failed. ret=%d", ret); 333 srs_error("send bandwidth check start publish message failed. ret=%d", ret);
@@ -369,9 +355,9 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit) @@ -369,9 +355,9 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
369 srs_info("BW check recv publish begin response."); 355 srs_info("BW check recv publish begin response.");
370 356
371 // recv publish msgs until @duration_ms ms 357 // recv publish msgs until @duration_ms ms
372 - int64_t current_time = srs_get_system_time_ms();  
373 - while ((srs_get_system_time_ms() - current_time) < duration_ms) {  
374 - st_usleep(0); 358 + int64_t starttime = srs_get_system_time_ms();
  359 + while ((srs_get_system_time_ms() - starttime) < sample->duration_ms) {
  360 + st_usleep(sample->interval_ms);
375 361
376 SrsMessage* msg = NULL; 362 SrsMessage* msg = NULL;
377 if ((ret = _rtmp->recv_message(&msg)) != ERROR_SUCCESS) { 363 if ((ret = _rtmp->recv_message(&msg)) != ERROR_SUCCESS) {
@@ -379,31 +365,19 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit) @@ -379,31 +365,19 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
379 return ret; 365 return ret;
380 } 366 }
381 SrsAutoFree(SrsMessage, msg); 367 SrsAutoFree(SrsMessage, msg);
382 -  
383 - publish_bytes = _rtmp->get_recv_bytes();  
384 -  
385 - int kbps = 0;  
386 - while (true) {  
387 - if(srs_get_system_time_ms() - current_time != 0)  
388 - kbps = publish_bytes * 8 / (srs_get_system_time_ms() - current_time);  
389 -  
390 - if (kbps > max_pub_kbps) {  
391 - st_usleep(500);  
392 - } else {  
393 - break;  
394 - }  
395 - } 368 +
  369 + limit->recv_limit();
396 } 370 }
397 - actual_duration_ms = srs_get_system_time_ms() - current_time; 371 + sample->calc_kbps(_rtmp->get_recv_bytes(), srs_get_system_time_ms() - starttime);
398 srs_info("BW check recv publish data over."); 372 srs_info("BW check recv publish data over.");
399 373
400 if (true) { 374 if (true) {
401 // notify client to stop publish 375 // notify client to stop publish
402 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_publish(); 376 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_publish();
403 - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));  
404 - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));  
405 - pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms));  
406 - pkt->data->set("bytes_delta", SrsAmf0Any::number(publish_bytes)); 377 + pkt->data->set("duration_ms", SrsAmf0Any::number(sample->duration_ms));
  378 + pkt->data->set("interval_ms", SrsAmf0Any::number(sample->interval_ms));
  379 + pkt->data->set("duration_delta", SrsAmf0Any::number(sample->actual_duration_ms));
  380 + pkt->data->set("bytes_delta", SrsAmf0Any::number(sample->bytes));
407 381
408 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 382 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
409 srs_error("send bandwidth check stop publish message failed. ret=%d", ret); 383 srs_error("send bandwidth check stop publish message failed. ret=%d", ret);
@@ -53,6 +53,7 @@ public: @@ -53,6 +53,7 @@ public:
53 * the plan, interval for each check/test packet, in ms 53 * the plan, interval for each check/test packet, in ms
54 */ 54 */
55 int interval_ms; 55 int interval_ms;
  56 +public:
56 /** 57 /**
57 * the actual test duration, in ms. 58 * the actual test duration, in ms.
58 */ 59 */
@@ -70,9 +71,11 @@ public: @@ -70,9 +71,11 @@ public:
70 virtual ~SrsBandwidthSample(); 71 virtual ~SrsBandwidthSample();
71 public: 72 public:
72 /** 73 /**
73 - * use current sample data to calc the kbps. 74 + * update the bytes and actual duration, then calc the kbps.
  75 + * @param _bytes update the sample bytes.
  76 + * @param _duration update the actual duration, in ms.
74 */ 77 */
75 - virtual void calc_kbps(); 78 + virtual void calc_kbps(int _bytes, int _duration);
76 }; 79 };
77 80
78 /** 81 /**
@@ -27,6 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -27,6 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <srs_kernel_log.hpp> 27 #include <srs_kernel_log.hpp>
28 #include <srs_protocol_io.hpp> 28 #include <srs_protocol_io.hpp>
29 #include <srs_kernel_utility.hpp> 29 #include <srs_kernel_utility.hpp>
  30 +#include <srs_app_st.hpp>
30 31
31 #define _SRS_BANDWIDTH_LIMIT_INTERVAL_MS 100 32 #define _SRS_BANDWIDTH_LIMIT_INTERVAL_MS 100
32 33
@@ -216,7 +216,7 @@ int SrsRtmpConn::service_cycle() @@ -216,7 +216,7 @@ int SrsRtmpConn::service_cycle()
216 216
217 // do bandwidth test if connect to the vhost which is for bandwidth check. 217 // do bandwidth test if connect to the vhost which is for bandwidth check.
218 if (_srs_config->get_bw_check_enabled(req->vhost)) { 218 if (_srs_config->get_bw_check_enabled(req->vhost)) {
219 - return bandwidth->bandwidth_check(rtmp, io, req, local_ip); 219 + return bandwidth->bandwidth_check(rtmp, skt, req, local_ip);
220 } 220 }
221 221
222 if ((ret = rtmp->response_connect_app(req, local_ip.c_str())) != ERROR_SUCCESS) { 222 if ((ret = rtmp->response_connect_app(req, local_ip.c_str())) != ERROR_SUCCESS) {
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "0" 32 #define VERSION_MAJOR "0"
33 #define VERSION_MINOR "9" 33 #define VERSION_MINOR "9"
34 -#define VERSION_REVISION "156" 34 +#define VERSION_REVISION "157"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "SRS" 37 #define RTMP_SIG_SRS_KEY "SRS"