winlin

refine bandwidth test, use function ptr.

@@ -63,6 +63,52 @@ void SrsBandwidthSample::calc_kbps(int _bytes, int _duration) @@ -63,6 +63,52 @@ void SrsBandwidthSample::calc_kbps(int _bytes, int _duration)
63 kbps = bytes * 8 / actual_duration_ms; 63 kbps = bytes * 8 / actual_duration_ms;
64 } 64 }
65 65
  66 +/**
  67 +* recv bandwidth helper.
  68 +*/
  69 +typedef bool (*_CheckPacketType)(SrsBandwidthPacket* pkt);
  70 +bool _bandwidth_is_flash_final(SrsBandwidthPacket* pkt)
  71 +{
  72 + return pkt->is_flash_final();
  73 +}
  74 +bool _bandwidth_is_starting_play(SrsBandwidthPacket* pkt)
  75 +{
  76 + return pkt->is_starting_play();
  77 +}
  78 +bool _bandwidth_is_stopped_play(SrsBandwidthPacket* pkt)
  79 +{
  80 + return pkt->is_stopped_play();
  81 +}
  82 +bool _bandwidth_is_starting_publish(SrsBandwidthPacket* pkt)
  83 +{
  84 + return pkt->is_starting_publish();
  85 +}
  86 +bool _bandwidth_is_stopped_publish(SrsBandwidthPacket* pkt)
  87 +{
  88 + return pkt->is_stopped_publish();
  89 +}
  90 +int _srs_expect_bandwidth_packet(SrsRtmpServer* rtmp, _CheckPacketType pfn)
  91 +{
  92 + int ret = ERROR_SUCCESS;
  93 +
  94 + while (true) {
  95 + SrsMessage* msg = NULL;
  96 + SrsBandwidthPacket* pkt = NULL;
  97 + if ((ret = rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {
  98 + return ret;
  99 + }
  100 + SrsAutoFree(SrsMessage, msg);
  101 + SrsAutoFree(SrsBandwidthPacket, pkt);
  102 + srs_info("get final message success.");
  103 +
  104 + if (pfn(pkt)) {
  105 + return ret;
  106 + }
  107 + }
  108 +
  109 + return ret;
  110 +}
  111 +
66 SrsBandwidth::SrsBandwidth() 112 SrsBandwidth::SrsBandwidth()
67 { 113 {
68 _req = NULL; 114 _req = NULL;
@@ -190,22 +236,10 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) @@ -190,22 +236,10 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit)
190 // we notice the result, and expect a final packet if not flash. 236 // we notice the result, and expect a final packet if not flash.
191 // if flash client, client will disconnect when got finish packet. 237 // if flash client, client will disconnect when got finish packet.
192 bool is_flash = (_req->swfUrl != ""); 238 bool is_flash = (_req->swfUrl != "");
193 - while (!is_flash) {  
194 - SrsMessage* msg = NULL;  
195 - SrsBandwidthPacket* pkt = NULL;  
196 - if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {  
197 - // info level to ignore and return success.  
198 - srs_info("expect final message failed. ret=%d", ret);  
199 - return ERROR_SUCCESS;  
200 - }  
201 - SrsAutoFree(SrsMessage, msg);  
202 - SrsAutoFree(SrsBandwidthPacket, pkt);  
203 - srs_info("get final message success.");  
204 -  
205 - if (pkt->is_flash_final()) {  
206 - srs_info("BW check recv flash final response.");  
207 - break;  
208 - } 239 + if (!is_flash) {
  240 + // ignore any error.
  241 + _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_flash_final);
  242 + srs_info("BW check recv flash final response.");
209 } 243 }
210 244
211 srs_info("BW check finished."); 245 srs_info("BW check finished.");
@@ -231,21 +265,8 @@ int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit) @@ -231,21 +265,8 @@ int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
231 } 265 }
232 srs_info("BW check begin."); 266 srs_info("BW check begin.");
233 267
234 - while (true) {  
235 - // recv client's starting play response  
236 - SrsMessage* msg = NULL;  
237 - SrsBandwidthPacket* pkt = NULL;  
238 - if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {  
239 - srs_error("expect bandwidth message failed. ret=%d", ret);  
240 - return ret;  
241 - }  
242 - SrsAutoFree(SrsMessage, msg);  
243 - SrsAutoFree(SrsBandwidthPacket, pkt);  
244 - srs_info("get bandwidth message succes.");  
245 -  
246 - if (pkt->is_starting_play()) {  
247 - break;  
248 - } 268 + if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_starting_play)) != ERROR_SUCCESS) {
  269 + return ret;
249 } 270 }
250 srs_info("BW check recv play begin response."); 271 srs_info("BW check recv play begin response.");
251 272
@@ -297,21 +318,8 @@ int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit) @@ -297,21 +318,8 @@ int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
297 } 318 }
298 srs_info("BW check stop play bytes."); 319 srs_info("BW check stop play bytes.");
299 320
300 - while (true) {  
301 - // recv client's stop play response.  
302 - SrsMessage* msg = NULL;  
303 - SrsBandwidthPacket* pkt = NULL;  
304 - if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {  
305 - srs_error("expect bandwidth message failed. ret=%d", ret);  
306 - return ret;  
307 - }  
308 - SrsAutoFree(SrsMessage, msg);  
309 - SrsAutoFree(SrsBandwidthPacket, pkt);  
310 - srs_info("get bandwidth message succes.");  
311 -  
312 - if (pkt->is_stopped_play()) {  
313 - break;  
314 - } 321 + if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_stopped_play)) != ERROR_SUCCESS) {
  322 + return ret;
315 } 323 }
316 srs_info("BW check recv stop play response."); 324 srs_info("BW check recv stop play response.");
317 325
@@ -336,21 +344,8 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit) @@ -336,21 +344,8 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
336 } 344 }
337 srs_info("BW check publish begin."); 345 srs_info("BW check publish begin.");
338 346
339 - while (true) {  
340 - // read client's notification of starting publish  
341 - SrsMessage* msg = NULL;  
342 - SrsBandwidthPacket* pkt = NULL;  
343 - if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {  
344 - srs_error("expect bandwidth message failed. ret=%d", ret);  
345 - return ret;  
346 - }  
347 - SrsAutoFree(SrsMessage, msg);  
348 - SrsAutoFree(SrsBandwidthPacket, pkt);  
349 - srs_info("get bandwidth message succes.");  
350 -  
351 - if (pkt->is_starting_publish()) {  
352 - break;  
353 - } 347 + if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_starting_publish)) != ERROR_SUCCESS) {
  348 + return ret;
354 } 349 }
355 srs_info("BW check recv publish begin response."); 350 srs_info("BW check recv publish begin response.");
356 351
@@ -392,23 +387,12 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit) @@ -392,23 +387,12 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
392 // there are many many packets in the queue. 387 // there are many many packets in the queue.
393 // we just ignore the packet and send the bandwidth test data. 388 // we just ignore the packet and send the bandwidth test data.
394 bool is_flash = (_req->swfUrl != ""); 389 bool is_flash = (_req->swfUrl != "");
395 - while (!is_flash) {  
396 - // recv client's stop publish response.  
397 - SrsMessage* msg = NULL;  
398 - SrsBandwidthPacket* pkt = NULL;  
399 - if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {  
400 - srs_error("expect bandwidth message failed. ret=%d", ret); 390 + if (!is_flash) {
  391 + if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_stopped_publish)) != ERROR_SUCCESS) {
401 return ret; 392 return ret;
402 } 393 }
403 - SrsAutoFree(SrsMessage, msg);  
404 - SrsAutoFree(SrsBandwidthPacket, pkt);  
405 - srs_info("get bandwidth message succes.");  
406 -  
407 - if (pkt->is_stopped_publish()) {  
408 - break;  
409 - } 394 + srs_info("BW check recv stop publish response.");
410 } 395 }
411 - srs_info("BW check recv stop publish response.");  
412 396
413 return ret; 397 return ret;
414 } 398 }
@@ -259,16 +259,22 @@ SrsKbpsLimit::~SrsKbpsLimit() @@ -259,16 +259,22 @@ SrsKbpsLimit::~SrsKbpsLimit()
259 259
260 void SrsKbpsLimit::recv_limit() 260 void SrsKbpsLimit::recv_limit()
261 { 261 {
  262 + _kbps->sample();
  263 +
262 while (_kbps->get_recv_kbps() > _limit_kbps) { 264 while (_kbps->get_recv_kbps() > _limit_kbps) {
263 _kbps->sample(); 265 _kbps->sample();
  266 +
264 st_usleep(_SRS_BANDWIDTH_LIMIT_INTERVAL_MS * 1000); 267 st_usleep(_SRS_BANDWIDTH_LIMIT_INTERVAL_MS * 1000);
265 } 268 }
266 } 269 }
267 270
268 void SrsKbpsLimit::send_limit() 271 void SrsKbpsLimit::send_limit()
269 { 272 {
  273 + _kbps->sample();
  274 +
270 while (_kbps->get_send_kbps() > _limit_kbps) { 275 while (_kbps->get_send_kbps() > _limit_kbps) {
271 _kbps->sample(); 276 _kbps->sample();
  277 +
272 st_usleep(_SRS_BANDWIDTH_LIMIT_INTERVAL_MS * 1000); 278 st_usleep(_SRS_BANDWIDTH_LIMIT_INTERVAL_MS * 1000);
273 } 279 }
274 } 280 }