diff --git a/trunk/src/app/srs_app_bandwidth.cpp b/trunk/src/app/srs_app_bandwidth.cpp index 48db8e4b8..36e1780e3 100644 --- a/trunk/src/app/srs_app_bandwidth.cpp +++ b/trunk/src/app/srs_app_bandwidth.cpp @@ -51,8 +51,11 @@ SrsBandwidthSample::~SrsBandwidthSample() { } -void SrsBandwidthSample::calc_kbps() +void SrsBandwidthSample::calc_kbps(int _bytes, int _duration) { + bytes = (int)_bytes; + actual_duration_ms = (int)_duration; + if (actual_duration_ms <= 0) { return; } @@ -144,7 +147,6 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) return ret; } - play_sample.calc_kbps(); srs_info("stop play test. kbps=%d", play_sample.kbps); // sample publish @@ -155,7 +157,6 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) return ret; } - publish_sample.calc_kbps(); srs_info("stop publish test. kbps=%d", publish_sample.kbps); // stop test. @@ -249,15 +250,14 @@ int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit) srs_info("BW check recv play begin response."); // send play data to client - int64_t current_time = srs_get_system_time_ms(); int size = 1024; // TODO: FIXME: magic number char random_data[size]; memset(random_data, 'A', size); - int interval = 0; int data_count = 1; - while ((srs_get_system_time_ms() - current_time) < duration_ms) { - st_usleep(interval); + int64_t starttime = srs_get_system_time_ms(); + while ((srs_get_system_time_ms() - starttime) < sample->duration_ms) { + st_usleep(sample->interval_ms); // TODO: FIXME: use shared ptr message. SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_playing(); @@ -271,38 +271,24 @@ int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit) } data_count += 2; - // get length from the rtmp protocol stack. - play_bytes = _rtmp->get_send_bytes(); - if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { srs_error("send bandwidth check play messages failed. ret=%d", ret); return ret; } - - // sleep while current kbps <= max_play_kbps - int kbps = 0; - while (true) { - if(srs_get_system_time_ms() - current_time != 0) - kbps = play_bytes * 8 / (srs_get_system_time_ms() - current_time); - - if (kbps > max_play_kbps) { - st_usleep(500); - } else { - break; - } - } + + limit->send_limit(); } - actual_duration_ms = srs_get_system_time_ms() - current_time; + sample->calc_kbps(_rtmp->get_send_bytes(), srs_get_system_time_ms() - starttime); srs_info("BW check send play bytes over."); if (true) { // notify client to stop play SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_play(); - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); - pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms)); - pkt->data->set("bytes_delta", SrsAmf0Any::number(play_bytes)); + pkt->data->set("duration_ms", SrsAmf0Any::number(sample->duration_ms)); + pkt->data->set("interval_ms", SrsAmf0Any::number(sample->interval_ms)); + pkt->data->set("duration_delta", SrsAmf0Any::number(sample->actual_duration_ms)); + pkt->data->set("bytes_delta", SrsAmf0Any::number(sample->bytes)); if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { srs_error("send bandwidth check stop play message failed. ret=%d", ret); @@ -340,8 +326,8 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit) // notify client to start publish SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_publish(); - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); + pkt->data->set("duration_ms", SrsAmf0Any::number(sample->duration_ms)); + pkt->data->set("interval_ms", SrsAmf0Any::number(sample->interval_ms)); if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { srs_error("send bandwidth check start publish message failed. ret=%d", ret); @@ -369,9 +355,9 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit) srs_info("BW check recv publish begin response."); // recv publish msgs until @duration_ms ms - int64_t current_time = srs_get_system_time_ms(); - while ((srs_get_system_time_ms() - current_time) < duration_ms) { - st_usleep(0); + int64_t starttime = srs_get_system_time_ms(); + while ((srs_get_system_time_ms() - starttime) < sample->duration_ms) { + st_usleep(sample->interval_ms); SrsMessage* msg = NULL; if ((ret = _rtmp->recv_message(&msg)) != ERROR_SUCCESS) { @@ -379,31 +365,19 @@ int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit) return ret; } SrsAutoFree(SrsMessage, msg); - - publish_bytes = _rtmp->get_recv_bytes(); - - int kbps = 0; - while (true) { - if(srs_get_system_time_ms() - current_time != 0) - kbps = publish_bytes * 8 / (srs_get_system_time_ms() - current_time); - - if (kbps > max_pub_kbps) { - st_usleep(500); - } else { - break; - } - } + + limit->recv_limit(); } - actual_duration_ms = srs_get_system_time_ms() - current_time; + sample->calc_kbps(_rtmp->get_recv_bytes(), srs_get_system_time_ms() - starttime); srs_info("BW check recv publish data over."); if (true) { // notify client to stop publish SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_publish(); - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); - pkt->data->set("duration_delta", SrsAmf0Any::number(actual_duration_ms)); - pkt->data->set("bytes_delta", SrsAmf0Any::number(publish_bytes)); + pkt->data->set("duration_ms", SrsAmf0Any::number(sample->duration_ms)); + pkt->data->set("interval_ms", SrsAmf0Any::number(sample->interval_ms)); + pkt->data->set("duration_delta", SrsAmf0Any::number(sample->actual_duration_ms)); + pkt->data->set("bytes_delta", SrsAmf0Any::number(sample->bytes)); if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { srs_error("send bandwidth check stop publish message failed. ret=%d", ret); diff --git a/trunk/src/app/srs_app_bandwidth.hpp b/trunk/src/app/srs_app_bandwidth.hpp index 7f4498e1c..da1dbbf97 100644 --- a/trunk/src/app/srs_app_bandwidth.hpp +++ b/trunk/src/app/srs_app_bandwidth.hpp @@ -53,6 +53,7 @@ public: * the plan, interval for each check/test packet, in ms */ int interval_ms; +public: /** * the actual test duration, in ms. */ @@ -70,9 +71,11 @@ public: virtual ~SrsBandwidthSample(); public: /** - * use current sample data to calc the kbps. + * update the bytes and actual duration, then calc the kbps. + * @param _bytes update the sample bytes. + * @param _duration update the actual duration, in ms. */ - virtual void calc_kbps(); + virtual void calc_kbps(int _bytes, int _duration); }; /** diff --git a/trunk/src/app/srs_app_kbps.cpp b/trunk/src/app/srs_app_kbps.cpp index 9a16472d4..10fc89220 100644 --- a/trunk/src/app/srs_app_kbps.cpp +++ b/trunk/src/app/srs_app_kbps.cpp @@ -27,6 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include +#include #define _SRS_BANDWIDTH_LIMIT_INTERVAL_MS 100 diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index f908b60ea..acee59250 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -216,7 +216,7 @@ int SrsRtmpConn::service_cycle() // do bandwidth test if connect to the vhost which is for bandwidth check. if (_srs_config->get_bw_check_enabled(req->vhost)) { - return bandwidth->bandwidth_check(rtmp, io, req, local_ip); + return bandwidth->bandwidth_check(rtmp, skt, req, local_ip); } if ((ret = rtmp->response_connect_app(req, local_ip.c_str())) != ERROR_SUCCESS) { diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 032485bdc..f74066b6c 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR "0" #define VERSION_MINOR "9" -#define VERSION_REVISION "156" +#define VERSION_REVISION "157" #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION // server info. #define RTMP_SIG_SRS_KEY "SRS"