winlin

refactor bandwidth test, refactor the interface of bandwidth server object.

@@ -90,10 +90,10 @@ public: @@ -90,10 +90,10 @@ public:
90 */ 90 */
91 int size; 91 int size;
92 char* bytes; 92 char* bytes;
93 - 93 +public:
94 SrsCodecBuffer(); 94 SrsCodecBuffer();
95 void append(void* data, int len); 95 void append(void* data, int len);
96 - 96 +public:
97 /** 97 /**
98 * free the bytes, 98 * free the bytes,
99 * user can invoke it to free the bytes, 99 * user can invoke it to free the bytes,
@@ -39,29 +39,31 @@ using namespace std; @@ -39,29 +39,31 @@ using namespace std;
39 39
40 SrsBandwidth::SrsBandwidth() 40 SrsBandwidth::SrsBandwidth()
41 { 41 {
  42 + _req = NULL;
  43 + _rtmp = NULL;
42 } 44 }
43 45
44 SrsBandwidth::~SrsBandwidth() 46 SrsBandwidth::~SrsBandwidth()
45 { 47 {
46 } 48 }
47 49
48 -int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServer* _rtmp) 50 +int SrsBandwidth::bandwidth_check(SrsRtmpServer* rtmp, SrsRequest* req, string local_ip)
49 { 51 {
50 int ret = ERROR_SUCCESS; 52 int ret = ERROR_SUCCESS;
51 53
52 - rtmp = _rtmp;  
53 - req = _req; 54 + _rtmp = rtmp;
  55 + _req = req;
54 56
55 - if (!_srs_config->get_bw_check_enabled(req->vhost)) { 57 + if (!_srs_config->get_bw_check_enabled(_req->vhost)) {
56 return ret; 58 return ret;
57 } 59 }
58 60
59 // validate the bandwidth check key 61 // validate the bandwidth check key
60 - std::string key = "key=" + _srs_config->get_bw_check_key(req->vhost);  
61 - if (req->tcUrl.find(key) == std::string::npos) { 62 + std::string key = "key=" + _srs_config->get_bw_check_key(_req->vhost);
  63 + if (_req->tcUrl.find(key) == std::string::npos) {
62 ret = ERROR_SYSTEM_BANDWIDTH_KEY; 64 ret = ERROR_SYSTEM_BANDWIDTH_KEY;
63 srs_error("check the vhost=%s %s failed, tcUrl=%s, ret=%d", 65 srs_error("check the vhost=%s %s failed, tcUrl=%s, ret=%d",
64 - req->vhost.c_str(), key.c_str(), req->tcUrl.c_str(), ret); 66 + _req->vhost.c_str(), key.c_str(), _req->tcUrl.c_str(), ret);
65 return ret; 67 return ret;
66 } 68 }
67 69
@@ -70,7 +72,7 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe @@ -70,7 +72,7 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe
70 // if client request check in the window(specifeid by interval), 72 // if client request check in the window(specifeid by interval),
71 // directly reject the request. 73 // directly reject the request.
72 static int64_t last_check_time = 0; 74 static int64_t last_check_time = 0;
73 - int interval_ms = _srs_config->get_bw_check_interval_ms(req->vhost); 75 + int interval_ms = _srs_config->get_bw_check_interval_ms(_req->vhost);
74 76
75 int64_t time_now = srs_get_system_time_ms(); 77 int64_t time_now = srs_get_system_time_ms();
76 // reject the connection in the interval window. 78 // reject the connection in the interval window.
@@ -80,15 +82,14 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe @@ -80,15 +82,14 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe
80 "last_check=%"PRId64", now=%"PRId64", interval=%d", 82 "last_check=%"PRId64", now=%"PRId64", interval=%d",
81 last_check_time, time_now, interval_ms); 83 last_check_time, time_now, interval_ms);
82 84
83 - rtmp->response_connect_reject(req, "bandcheck rejected"); 85 + _rtmp->response_connect_reject(_req, "bandcheck rejected");
84 return ret; 86 return ret;
85 } 87 }
86 88
87 // accept and do bandwidth check. 89 // accept and do bandwidth check.
88 last_check_time = time_now; 90 last_check_time = time_now;
89 91
90 - std::string local_ip = srs_get_local_ip(st_netfd_fileno(stfd));  
91 - if ((ret = rtmp->response_connect_app(req, local_ip.c_str())) != ERROR_SUCCESS) { 92 + if ((ret = _rtmp->response_connect_app(_req, local_ip.c_str())) != ERROR_SUCCESS) {
92 srs_error("response connect app failed. ret=%d", ret); 93 srs_error("response connect app failed. ret=%d", ret);
93 return ret; 94 return ret;
94 } 95 }
@@ -110,7 +111,7 @@ int SrsBandwidth::do_bandwidth_check() @@ -110,7 +111,7 @@ int SrsBandwidth::do_bandwidth_check()
110 int publish_actual_duration_ms = 0; 111 int publish_actual_duration_ms = 0;
111 int publish_bytes = 0; 112 int publish_bytes = 0;
112 113
113 - int limit_kbps = _srs_config->get_bw_check_limit_kbps(req->vhost); 114 + int limit_kbps = _srs_config->get_bw_check_limit_kbps(_req->vhost);
114 115
115 int64_t start_time = srs_get_system_time_ms(); 116 int64_t start_time = srs_get_system_time_ms();
116 117
@@ -135,7 +136,7 @@ int SrsBandwidth::do_bandwidth_check() @@ -135,7 +136,7 @@ int SrsBandwidth::do_bandwidth_check()
135 srs_trace("bandwidth check finished. start=%"PRId64"ms, end=%"PRId64"ms, " 136 srs_trace("bandwidth check finished. start=%"PRId64"ms, end=%"PRId64"ms, "
136 "duartion=%dms, play=%dkbps, publish=%dkbps, tcUrl=%s, ret=%#x", 137 "duartion=%dms, play=%dkbps, publish=%dkbps, tcUrl=%s, ret=%#x",
137 start_time, end_time, (int)(end_time - start_time), play_kbps, publish_kbps, 138 start_time, end_time, (int)(end_time - start_time), play_kbps, publish_kbps,
138 - req->tcUrl.c_str(), ret); 139 + _req->tcUrl.c_str(), ret);
139 140
140 // send finished msg 141 // send finished msg
141 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_finish(); 142 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_finish();
@@ -149,7 +150,7 @@ int SrsBandwidth::do_bandwidth_check() @@ -149,7 +150,7 @@ int SrsBandwidth::do_bandwidth_check()
149 pkt->data->set("publish_bytes", SrsAmf0Any::number(publish_bytes)); 150 pkt->data->set("publish_bytes", SrsAmf0Any::number(publish_bytes));
150 pkt->data->set("publish_time", SrsAmf0Any::number(publish_actual_duration_ms)); 151 pkt->data->set("publish_time", SrsAmf0Any::number(publish_actual_duration_ms));
151 152
152 - if ((ret = rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 153 + if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
153 srs_error("send bandwidth check finish message failed. ret=%d", ret); 154 srs_error("send bandwidth check finish message failed. ret=%d", ret);
154 return ret; 155 return ret;
155 } 156 }
@@ -158,7 +159,7 @@ int SrsBandwidth::do_bandwidth_check() @@ -158,7 +159,7 @@ int SrsBandwidth::do_bandwidth_check()
158 while (true) { 159 while (true) {
159 SrsMessage* msg = NULL; 160 SrsMessage* msg = NULL;
160 SrsBandwidthPacket* pkt = NULL; 161 SrsBandwidthPacket* pkt = NULL;
161 - if ((ret = rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) { 162 + if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {
162 // info level to ignore and return success. 163 // info level to ignore and return success.
163 srs_info("expect final message failed. ret=%d", ret); 164 srs_info("expect final message failed. ret=%d", ret);
164 return ERROR_SUCCESS; 165 return ERROR_SUCCESS;
@@ -191,7 +192,7 @@ int SrsBandwidth::check_play( @@ -191,7 +192,7 @@ int SrsBandwidth::check_play(
191 pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); 192 pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));
192 pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); 193 pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));
193 194
194 - if ((ret = rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 195 + if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
195 srs_error("send bandwidth check start play message failed. ret=%d", ret); 196 srs_error("send bandwidth check start play message failed. ret=%d", ret);
196 return ret; 197 return ret;
197 } 198 }
@@ -202,7 +203,7 @@ int SrsBandwidth::check_play( @@ -202,7 +203,7 @@ int SrsBandwidth::check_play(
202 // recv client's starting play response 203 // recv client's starting play response
203 SrsMessage* msg = NULL; 204 SrsMessage* msg = NULL;
204 SrsBandwidthPacket* pkt = NULL; 205 SrsBandwidthPacket* pkt = NULL;
205 - if ((ret = rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) { 206 + if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {
206 srs_error("expect bandwidth message failed. ret=%d", ret); 207 srs_error("expect bandwidth message failed. ret=%d", ret);
207 return ret; 208 return ret;
208 } 209 }
@@ -224,7 +225,7 @@ int SrsBandwidth::check_play( @@ -224,7 +225,7 @@ int SrsBandwidth::check_play(
224 225
225 int interval = 0; 226 int interval = 0;
226 int data_count = 1; 227 int data_count = 1;
227 - while ( (srs_get_system_time_ms() - current_time) < duration_ms ) { 228 + while ((srs_get_system_time_ms() - current_time) < duration_ms) {
228 st_usleep(interval); 229 st_usleep(interval);
229 230
230 // TODO: FIXME: use shared ptr message. 231 // TODO: FIXME: use shared ptr message.
@@ -234,15 +235,15 @@ int SrsBandwidth::check_play( @@ -234,15 +235,15 @@ int SrsBandwidth::check_play(
234 for (int i = 0; i < data_count; ++i) { 235 for (int i = 0; i < data_count; ++i) {
235 std::stringstream seq; 236 std::stringstream seq;
236 seq << i; 237 seq << i;
237 - std::string play_data = "SrS band check data from server's playing......"; 238 + std::string play_data = "SRS band check data from server's playing......";
238 pkt->data->set(seq.str(), SrsAmf0Any::str(play_data.c_str())); 239 pkt->data->set(seq.str(), SrsAmf0Any::str(play_data.c_str()));
239 } 240 }
240 data_count += 2; 241 data_count += 2;
241 242
242 // get length from the rtmp protocol stack. 243 // get length from the rtmp protocol stack.
243 - play_bytes = rtmp->get_send_bytes(); 244 + play_bytes = _rtmp->get_send_bytes();
244 245
245 - if ((ret = rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 246 + if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
246 srs_error("send bandwidth check play messages failed. ret=%d", ret); 247 srs_error("send bandwidth check play messages failed. ret=%d", ret);
247 return ret; 248 return ret;
248 } 249 }
@@ -272,7 +273,7 @@ int SrsBandwidth::check_play( @@ -272,7 +273,7 @@ int SrsBandwidth::check_play(
272 pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms)); 273 pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms));
273 pkt->data->set("bytes_delta", SrsAmf0Any::number(play_bytes)); 274 pkt->data->set("bytes_delta", SrsAmf0Any::number(play_bytes));
274 275
275 - if ((ret = rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 276 + if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
276 srs_error("send bandwidth check stop play message failed. ret=%d", ret); 277 srs_error("send bandwidth check stop play message failed. ret=%d", ret);
277 return ret; 278 return ret;
278 } 279 }
@@ -283,7 +284,7 @@ int SrsBandwidth::check_play( @@ -283,7 +284,7 @@ int SrsBandwidth::check_play(
283 // recv client's stop play response. 284 // recv client's stop play response.
284 SrsMessage* msg = NULL; 285 SrsMessage* msg = NULL;
285 SrsBandwidthPacket* pkt = NULL; 286 SrsBandwidthPacket* pkt = NULL;
286 - if ((ret = rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) { 287 + if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {
287 srs_error("expect bandwidth message failed. ret=%d", ret); 288 srs_error("expect bandwidth message failed. ret=%d", ret);
288 return ret; 289 return ret;
289 } 290 }
@@ -313,7 +314,7 @@ int SrsBandwidth::check_publish( @@ -313,7 +314,7 @@ int SrsBandwidth::check_publish(
313 pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); 314 pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));
314 pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); 315 pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms));
315 316
316 - if ((ret = rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 317 + if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
317 srs_error("send bandwidth check start publish message failed. ret=%d", ret); 318 srs_error("send bandwidth check start publish message failed. ret=%d", ret);
318 return ret; 319 return ret;
319 } 320 }
@@ -324,7 +325,7 @@ int SrsBandwidth::check_publish( @@ -324,7 +325,7 @@ int SrsBandwidth::check_publish(
324 // read client's notification of starting publish 325 // read client's notification of starting publish
325 SrsMessage* msg = NULL; 326 SrsMessage* msg = NULL;
326 SrsBandwidthPacket* pkt = NULL; 327 SrsBandwidthPacket* pkt = NULL;
327 - if ((ret = rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) { 328 + if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {
328 srs_error("expect bandwidth message failed. ret=%d", ret); 329 srs_error("expect bandwidth message failed. ret=%d", ret);
329 return ret; 330 return ret;
330 } 331 }
@@ -344,13 +345,13 @@ int SrsBandwidth::check_publish( @@ -344,13 +345,13 @@ int SrsBandwidth::check_publish(
344 st_usleep(0); 345 st_usleep(0);
345 346
346 SrsMessage* msg = NULL; 347 SrsMessage* msg = NULL;
347 - if ((ret = rtmp->recv_message(&msg)) != ERROR_SUCCESS) { 348 + if ((ret = _rtmp->recv_message(&msg)) != ERROR_SUCCESS) {
348 srs_error("recv message failed. ret=%d", ret); 349 srs_error("recv message failed. ret=%d", ret);
349 return ret; 350 return ret;
350 } 351 }
351 SrsAutoFree(SrsMessage, msg); 352 SrsAutoFree(SrsMessage, msg);
352 353
353 - publish_bytes = rtmp->get_recv_bytes(); 354 + publish_bytes = _rtmp->get_recv_bytes();
354 355
355 int kbps = 0; 356 int kbps = 0;
356 while (true) { 357 while (true) {
@@ -375,7 +376,7 @@ int SrsBandwidth::check_publish( @@ -375,7 +376,7 @@ int SrsBandwidth::check_publish(
375 pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms)); 376 pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms));
376 pkt->data->set("bytes_delta", SrsAmf0Any::number(publish_bytes)); 377 pkt->data->set("bytes_delta", SrsAmf0Any::number(publish_bytes));
377 378
378 - if ((ret = rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 379 + if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
379 srs_error("send bandwidth check stop publish message failed. ret=%d", ret); 380 srs_error("send bandwidth check stop publish message failed. ret=%d", ret);
380 return ret; 381 return ret;
381 } 382 }
@@ -392,7 +393,7 @@ int SrsBandwidth::check_publish( @@ -392,7 +393,7 @@ int SrsBandwidth::check_publish(
392 // recv client's stop publish response. 393 // recv client's stop publish response.
393 SrsMessage* msg = NULL; 394 SrsMessage* msg = NULL;
394 SrsBandwidthPacket* pkt = NULL; 395 SrsBandwidthPacket* pkt = NULL;
395 - if ((ret = rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) { 396 + if ((ret = _rtmp->expect_message<SrsBandwidthPacket>(&msg, &pkt)) != ERROR_SUCCESS) {
396 srs_error("expect bandwidth message failed. ret=%d", ret); 397 srs_error("expect bandwidth message failed. ret=%d", ret);
397 return ret; 398 return ret;
398 } 399 }
@@ -29,6 +29,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -29,6 +29,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 */ 29 */
30 #include <srs_core.hpp> 30 #include <srs_core.hpp>
31 31
  32 +#include <string>
  33 +
32 #include <srs_app_st.hpp> 34 #include <srs_app_st.hpp>
33 35
34 class SrsRequest; 36 class SrsRequest;
@@ -73,16 +75,19 @@ class SrsRtmpServer; @@ -73,16 +75,19 @@ class SrsRtmpServer;
73 class SrsBandwidth 75 class SrsBandwidth
74 { 76 {
75 private: 77 private:
76 - SrsRequest* req;  
77 - SrsRtmpServer* rtmp; 78 + SrsRequest* _req;
  79 + SrsRtmpServer* _rtmp;
78 public: 80 public:
79 SrsBandwidth(); 81 SrsBandwidth();
80 virtual ~SrsBandwidth(); 82 virtual ~SrsBandwidth();
81 public: 83 public:
82 /** 84 /**
83 - * do the bandwidth test. 85 + * do the bandwidth check.
  86 + * @param rtmp, server RTMP protocol object, send/recv RTMP packet to/from client.
  87 + * @param req, client request object, specifies the request info from client.
  88 + * @param local_ip, the ip of server which client connected at
84 */ 89 */
85 - virtual int bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServer* _rtmp); 90 + virtual int bandwidth_check(SrsRtmpServer* rtmp, SrsRequest* req, std::string local_ip);
86 private: 91 private:
87 /** 92 /**
88 * used to process band width check from client. 93 * used to process band width check from client.
@@ -211,12 +211,14 @@ int SrsRtmpConn::service_cycle() @@ -211,12 +211,14 @@ int SrsRtmpConn::service_cycle()
211 } 211 }
212 srs_verbose("set peer bandwidth success"); 212 srs_verbose("set peer bandwidth success");
213 213
  214 + // get the ip which client connected.
  215 + std::string local_ip = srs_get_local_ip(st_netfd_fileno(stfd));
  216 +
214 // 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.
215 if (_srs_config->get_bw_check_enabled(req->vhost)) { 218 if (_srs_config->get_bw_check_enabled(req->vhost)) {
216 - return bandwidth->bandwidth_test(req, stfd, rtmp); 219 + return bandwidth->bandwidth_check(rtmp, req, local_ip);
217 } 220 }
218 221
219 - std::string local_ip = srs_get_local_ip(st_netfd_fileno(stfd));  
220 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) {
221 srs_error("response connect app failed. ret=%d", ret); 223 srs_error("response connect app failed. ret=%d", ret);
222 return ret; 224 return ret;