1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

refine bandwidth server-side, use sample and kbps limit

This commit is contained in:
winlin 2014-07-13 10:47:27 +08:00
parent 79e7e2d6cc
commit e220596675
2 changed files with 58 additions and 32 deletions

View file

@ -38,16 +38,28 @@ using namespace std;
#include <srs_app_utility.hpp> #include <srs_app_utility.hpp>
#include <srs_app_kbps.hpp> #include <srs_app_kbps.hpp>
// default sample duration, in ms
#define _SRS_BANDWIDTH_SAMPLE_DURATION_MS 3000
SrsBandwidthSample::SrsBandwidthSample() SrsBandwidthSample::SrsBandwidthSample()
{ {
duration_ms = 3000; duration_ms = _SRS_BANDWIDTH_SAMPLE_DURATION_MS;
interval_ms = actual_duration_ms = bytes = 0; kbps = interval_ms = actual_duration_ms = bytes = 0;
} }
SrsBandwidthSample::~SrsBandwidthSample() SrsBandwidthSample::~SrsBandwidthSample()
{ {
} }
void SrsBandwidthSample::calc_kbps()
{
if (actual_duration_ms <= 0) {
return;
}
kbps = bytes * 8 / actual_duration_ms;
}
SrsBandwidth::SrsBandwidth() SrsBandwidth::SrsBandwidth()
{ {
_req = NULL; _req = NULL;
@ -124,32 +136,35 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit)
int64_t start_time = srs_get_system_time_ms(); int64_t start_time = srs_get_system_time_ms();
// sample play
srs_info("start play test."); srs_info("start play test.");
ret = check_play(play_duration_ms,
play_interval_ms, play_actual_duration_ms, play_bytes, limit_kbps); if ((ret = check_play(&play_sample, limit)) != ERROR_SUCCESS) {
if (ret != ERROR_SUCCESS) {
srs_error("band width play check failed. ret=%d", ret); srs_error("band width play check failed. ret=%d", ret);
return ret; return ret;
} }
srs_info("stop play test.");
play_sample.calc_kbps();
srs_info("stop play test. kbps=%d", play_sample.kbps);
// sample publish
srs_info("start publish test."); srs_info("start publish test.");
ret = check_publish(publish_duration_ms,
publish_interval_ms, publish_actual_duration_ms, publish_bytes, limit_kbps); if ((ret = check_publish(&publish_sample, limit)) != ERROR_SUCCESS) {
if (ret != ERROR_SUCCESS) {
srs_error("band width publish check failed. ret=%d", ret); srs_error("band width publish check failed. ret=%d", ret);
return ret; return ret;
} }
srs_info("stop publish test.");
publish_sample.calc_kbps();
srs_info("stop publish test. kbps=%d", publish_sample.kbps);
// stop test.
int64_t end_time = srs_get_system_time_ms(); int64_t end_time = srs_get_system_time_ms();
int play_kbps = play_bytes * 8 / play_actual_duration_ms;
int publish_kbps = publish_bytes * 8 / publish_actual_duration_ms;
srs_trace("bandwidth check finished. start=%"PRId64"ms, end=%"PRId64"ms, " srs_trace("bandwidth ok. duartion=%dms(%d+%d), play=%dkbps, publish=%dkbps",
"duartion=%dms, play=%dkbps, publish=%dkbps, tcUrl=%s, ret=%#x", (int)(end_time - start_time), play_sample.actual_duration_ms,
start_time, end_time, (int)(end_time - start_time), play_kbps, publish_kbps, publish_sample.actual_duration_ms, play_sample.kbps,
_req->tcUrl.c_str(), ret); publish_sample.kbps);
// send finished msg, // send finished msg,
// flash client will close connection when got this packet, // flash client will close connection when got this packet,
@ -158,12 +173,12 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit)
pkt->data->set("code", SrsAmf0Any::number(ERROR_SUCCESS)); pkt->data->set("code", SrsAmf0Any::number(ERROR_SUCCESS));
pkt->data->set("start_time", SrsAmf0Any::number(start_time)); pkt->data->set("start_time", SrsAmf0Any::number(start_time));
pkt->data->set("end_time", SrsAmf0Any::number(end_time)); pkt->data->set("end_time", SrsAmf0Any::number(end_time));
pkt->data->set("play_kbps", SrsAmf0Any::number(play_kbps)); pkt->data->set("play_kbps", SrsAmf0Any::number(play_sample.kbps));
pkt->data->set("publish_kbps", SrsAmf0Any::number(publish_kbps)); pkt->data->set("publish_kbps", SrsAmf0Any::number(publish_sample.kbps));
pkt->data->set("play_bytes", SrsAmf0Any::number(play_bytes)); pkt->data->set("play_bytes", SrsAmf0Any::number(play_sample.bytes));
pkt->data->set("play_time", SrsAmf0Any::number(play_actual_duration_ms)); pkt->data->set("publish_bytes", SrsAmf0Any::number(publish_sample.bytes));
pkt->data->set("publish_bytes", SrsAmf0Any::number(publish_bytes)); pkt->data->set("play_time", SrsAmf0Any::number(play_sample.actual_duration_ms));
pkt->data->set("publish_time", SrsAmf0Any::number(publish_actual_duration_ms)); pkt->data->set("publish_time", SrsAmf0Any::number(publish_sample.actual_duration_ms));
if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
srs_error("send bandwidth check finish message failed. ret=%d", ret); srs_error("send bandwidth check finish message failed. ret=%d", ret);
@ -197,9 +212,7 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit)
return ret; return ret;
} }
int SrsBandwidth::check_play( int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
int duration_ms, int interval_ms, int& actual_duration_ms,
int& play_bytes, int max_play_kbps)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -207,8 +220,8 @@ int SrsBandwidth::check_play(
// send start play command to client // send start play command to client
SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_play(); SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_play();
pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms)); pkt->data->set("duration_ms", SrsAmf0Any::number(sample->duration_ms));
pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); pkt->data->set("interval_ms", SrsAmf0Any::number(sample->interval_ms));
if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
srs_error("send bandwidth check start play message failed. ret=%d", ret); srs_error("send bandwidth check start play message failed. ret=%d", ret);
@ -319,9 +332,7 @@ int SrsBandwidth::check_play(
return ret; return ret;
} }
int SrsBandwidth::check_publish( int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
int duration_ms, int interval_ms, int& actual_duration_ms,
int& publish_bytes, int max_pub_kbps)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;

View file

@ -61,9 +61,18 @@ public:
* the actual test bytes * the actual test bytes
*/ */
int bytes; int bytes;
/**
* the actual test kbps
*/
int kbps;
public: public:
SrsBandwidthSample(); SrsBandwidthSample();
virtual ~SrsBandwidthSample(); virtual ~SrsBandwidthSample();
public:
/**
* use current sample data to calc the kbps.
*/
virtual void calc_kbps();
}; };
/** /**
@ -125,8 +134,14 @@ private:
* @param limit, the bandwidth limit object, to slowdown if exceed the kbps. * @param limit, the bandwidth limit object, to slowdown if exceed the kbps.
*/ */
virtual int do_bandwidth_check(SrsKbpsLimit* limit); virtual int do_bandwidth_check(SrsKbpsLimit* limit);
virtual int check_play(int duration_ms, int interval_ms, int& actual_duration_ms, int& play_bytes, int max_play_kbps); /**
virtual int check_publish(int duration_ms, int interval_ms, int& actual_duration_ms, int& publish_bytes, int max_pub_kbps); * play sample under specified kbps limit.
*/
virtual int check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit);
/**
* publish sample under specified kbps limit.
*/
virtual int check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit);
}; };
#endif #endif