From b33a61a8da485ae9b13e7ecdd304faa10da0fd68 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 13 Jul 2014 12:06:24 +0800 Subject: [PATCH] finish refine bandwidth check/test server-side. --- trunk/src/app/srs_app_bandwidth.cpp | 364 ++++++++++++++++------------ trunk/src/app/srs_app_bandwidth.hpp | 16 +- 2 files changed, 222 insertions(+), 158 deletions(-) diff --git a/trunk/src/app/srs_app_bandwidth.cpp b/trunk/src/app/srs_app_bandwidth.cpp index dc343503e..f9dd2eb77 100644 --- a/trunk/src/app/srs_app_bandwidth.cpp +++ b/trunk/src/app/srs_app_bandwidth.cpp @@ -188,8 +188,16 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) // sample play srs_info("start play test."); - if ((ret = check_play(&play_sample, limit)) != ERROR_SUCCESS) { - srs_error("band width play check failed. ret=%d", ret); + if ((ret = play_start(&play_sample, limit)) != ERROR_SUCCESS) { + srs_error("bandwidth play check failed. ret=%d", ret); + return ret; + } + if ((ret = play_checking(&play_sample, limit)) != ERROR_SUCCESS) { + srs_error("bandwidth play check failed. ret=%d", ret); + return ret; + } + if ((ret = play_stop(&play_sample, limit)) != ERROR_SUCCESS) { + srs_error("bandwidth play check failed. ret=%d", ret); return ret; } @@ -198,8 +206,16 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) // sample publish srs_info("start publish test."); - if ((ret = check_publish(&publish_sample, limit)) != ERROR_SUCCESS) { - srs_error("band width publish check failed. ret=%d", ret); + if ((ret = publish_start(&publish_sample, limit)) != ERROR_SUCCESS) { + srs_error("bandwidth publish check failed. ret=%d", ret); + return ret; + } + if ((ret = publish_checking(&publish_sample, limit)) != ERROR_SUCCESS) { + srs_error("bandwidth publish check failed. ret=%d", ret); + return ret; + } + if ((ret = publish_stop(&publish_sample, limit)) != ERROR_SUCCESS) { + srs_error("bandwidth publish check failed. ret=%d", ret); return ret; } @@ -212,6 +228,196 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) (int)(end_time - start_time), play_sample.actual_duration_ms, publish_sample.actual_duration_ms, play_sample.kbps, publish_sample.kbps); + + if ((ret = finial(play_sample, publish_sample, start_time, end_time)) != ERROR_SUCCESS) { + return ret; + } + srs_info("BW check finished."); + + return ret; +} + +int SrsBandwidth::play_start(SrsBandwidthSample* sample, SrsKbpsLimit* limit) +{ + int ret = ERROR_SUCCESS; + + if (true) { + // send start play command to client + SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_play(); + + 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 play message failed. ret=%d", ret); + return ret; + } + } + srs_info("BW check begin."); + + if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_starting_play)) != ERROR_SUCCESS) { + return ret; + } + srs_info("BW check recv play begin response."); + + return ret; +} + +int SrsBandwidth::play_checking(SrsBandwidthSample* sample, SrsKbpsLimit* limit) +{ + int ret = ERROR_SUCCESS; + + // send play data to client + int size = 1024; // TODO: FIXME: magic number + char random_data[size]; + memset(random_data, 'A', size); + + int data_count = 1; + 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(); + + // TODO: FIXME: magic number + for (int i = 0; i < data_count; ++i) { + std::stringstream seq; + seq << i; + std::string play_data = "SRS band check data from server's playing......"; + pkt->data->set(seq.str(), SrsAmf0Any::str(play_data.c_str())); + } + data_count += 2; + + if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { + srs_error("send bandwidth check play messages failed. ret=%d", ret); + return ret; + } + + limit->send_limit(); + } + sample->calc_kbps(_rtmp->get_send_bytes(), srs_get_system_time_ms() - starttime); + srs_info("BW check send play bytes over."); + + return ret; +} + +int SrsBandwidth::play_stop(SrsBandwidthSample* sample, SrsKbpsLimit* limit) +{ + int ret = ERROR_SUCCESS; + + if (true) { + // notify client to stop play + SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_play(); + + 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); + return ret; + } + } + srs_info("BW check stop play bytes."); + + if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_stopped_play)) != ERROR_SUCCESS) { + return ret; + } + srs_info("BW check recv stop play response."); + + return ret; +} + +int SrsBandwidth::publish_start(SrsBandwidthSample* sample, SrsKbpsLimit* limit) +{ + int ret = ERROR_SUCCESS; + + if (true) { + // notify client to start publish + SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_publish(); + + 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); + return ret; + } + } + srs_info("BW check publish begin."); + + if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_starting_publish)) != ERROR_SUCCESS) { + return ret; + } + srs_info("BW check recv publish begin response."); + + return ret; +} + +int SrsBandwidth::publish_checking(SrsBandwidthSample* sample, SrsKbpsLimit* limit) +{ + int ret = ERROR_SUCCESS; + + // recv publish msgs until @duration_ms ms + 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) { + srs_error("recv message failed. ret=%d", ret); + return ret; + } + SrsAutoFree(SrsMessage, msg); + + limit->recv_limit(); + } + sample->calc_kbps(_rtmp->get_recv_bytes(), srs_get_system_time_ms() - starttime); + srs_info("BW check recv publish data over."); + + return ret; +} + +int SrsBandwidth::publish_stop(SrsBandwidthSample* sample, SrsKbpsLimit* limit) +{ + int ret = ERROR_SUCCESS; + + if (true) { + // notify client to stop publish + SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_stop_publish(); + 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); + return ret; + } + } + srs_info("BW check stop publish bytes."); + + // expect client to stop publish + // if flash client, we never expect the client stop publish bytes, + // for the flash send call packet to test publish bandwidth, + // there are many many packets in the queue. + // we just ignore the packet and send the bandwidth test data. + bool is_flash = (_req->swfUrl != ""); + if (!is_flash) { + if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_stopped_publish)) != ERROR_SUCCESS) { + return ret; + } + srs_info("BW check recv stop publish response."); + } + + return ret; +} + +int SrsBandwidth::finial(SrsBandwidthSample& play_sample, SrsBandwidthSample& publish_sample, int64_t start_time, int64_t& end_time) +{ + int ret = ERROR_SUCCESS; // send finished msg, // flash client will close connection when got this packet, @@ -246,153 +452,3 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) return ret; } - -int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit) -{ - int ret = ERROR_SUCCESS; - - if (true) { - // send start play command to client - SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_play(); - - 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 play message failed. ret=%d", ret); - return ret; - } - } - srs_info("BW check begin."); - - if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_starting_play)) != ERROR_SUCCESS) { - return ret; - } - srs_info("BW check recv play begin response."); - - // send play data to client - int size = 1024; // TODO: FIXME: magic number - char random_data[size]; - memset(random_data, 'A', size); - - int data_count = 1; - 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(); - - // TODO: FIXME: magic number - for (int i = 0; i < data_count; ++i) { - std::stringstream seq; - seq << i; - std::string play_data = "SRS band check data from server's playing......"; - pkt->data->set(seq.str(), SrsAmf0Any::str(play_data.c_str())); - } - data_count += 2; - - if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { - srs_error("send bandwidth check play messages failed. ret=%d", ret); - return ret; - } - - limit->send_limit(); - } - 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(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); - return ret; - } - } - srs_info("BW check stop play bytes."); - - if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_stopped_play)) != ERROR_SUCCESS) { - return ret; - } - srs_info("BW check recv stop play response."); - - return ret; -} - -int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit) -{ - int ret = ERROR_SUCCESS; - - if (true) { - // notify client to start publish - SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_publish(); - - 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); - return ret; - } - } - srs_info("BW check publish begin."); - - if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_starting_publish)) != ERROR_SUCCESS) { - return ret; - } - srs_info("BW check recv publish begin response."); - - // recv publish msgs until @duration_ms ms - 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) { - srs_error("recv message failed. ret=%d", ret); - return ret; - } - SrsAutoFree(SrsMessage, msg); - - limit->recv_limit(); - } - 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(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); - return ret; - } - } - srs_info("BW check stop publish bytes."); - - // expect client to stop publish - // if flash client, we never expect the client stop publish bytes, - // for the flash send call packet to test publish bandwidth, - // there are many many packets in the queue. - // we just ignore the packet and send the bandwidth test data. - bool is_flash = (_req->swfUrl != ""); - if (!is_flash) { - if ((ret = _srs_expect_bandwidth_packet(_rtmp, _bandwidth_is_stopped_publish)) != ERROR_SUCCESS) { - return ret; - } - srs_info("BW check recv stop publish response."); - } - - return ret; -} diff --git a/trunk/src/app/srs_app_bandwidth.hpp b/trunk/src/app/srs_app_bandwidth.hpp index da1dbbf97..967f50f12 100644 --- a/trunk/src/app/srs_app_bandwidth.hpp +++ b/trunk/src/app/srs_app_bandwidth.hpp @@ -138,13 +138,21 @@ private: */ virtual int do_bandwidth_check(SrsKbpsLimit* limit); /** - * play sample under specified kbps limit. + * play check/test, downloading bandwidth kbps. */ - virtual int check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit); + virtual int play_start(SrsBandwidthSample* sample, SrsKbpsLimit* limit); + virtual int play_checking(SrsBandwidthSample* sample, SrsKbpsLimit* limit); + virtual int play_stop(SrsBandwidthSample* sample, SrsKbpsLimit* limit); /** - * publish sample under specified kbps limit. + * publish check/test, publishing bandwidth kbps. */ - virtual int check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit); + virtual int publish_start(SrsBandwidthSample* sample, SrsKbpsLimit* limit); + virtual int publish_checking(SrsBandwidthSample* sample, SrsKbpsLimit* limit); + virtual int publish_stop(SrsBandwidthSample* sample, SrsKbpsLimit* limit); + /** + * report and final packet + */ + virtual int finial(SrsBandwidthSample& play_sample, SrsBandwidthSample& publish_sample, int64_t start_time, int64_t& end_time); }; #endif \ No newline at end of file