winlin

refine bandwidth server-side, use sample and kbps limit

@@ -38,16 +38,28 @@ using namespace std; @@ -38,16 +38,28 @@ using namespace std;
38 #include <srs_app_utility.hpp> 38 #include <srs_app_utility.hpp>
39 #include <srs_app_kbps.hpp> 39 #include <srs_app_kbps.hpp>
40 40
  41 +// default sample duration, in ms
  42 +#define _SRS_BANDWIDTH_SAMPLE_DURATION_MS 3000
  43 +
41 SrsBandwidthSample::SrsBandwidthSample() 44 SrsBandwidthSample::SrsBandwidthSample()
42 { 45 {
43 - duration_ms = 3000;  
44 - interval_ms = actual_duration_ms = bytes = 0; 46 + duration_ms = _SRS_BANDWIDTH_SAMPLE_DURATION_MS;
  47 + kbps = interval_ms = actual_duration_ms = bytes = 0;
45 } 48 }
46 49
47 SrsBandwidthSample::~SrsBandwidthSample() 50 SrsBandwidthSample::~SrsBandwidthSample()
48 { 51 {
49 } 52 }
50 53
  54 +void SrsBandwidthSample::calc_kbps()
  55 +{
  56 + if (actual_duration_ms <= 0) {
  57 + return;
  58 + }
  59 +
  60 + kbps = bytes * 8 / actual_duration_ms;
  61 +}
  62 +
51 SrsBandwidth::SrsBandwidth() 63 SrsBandwidth::SrsBandwidth()
52 { 64 {
53 _req = NULL; 65 _req = NULL;
@@ -124,32 +136,35 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) @@ -124,32 +136,35 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit)
124 136
125 int64_t start_time = srs_get_system_time_ms(); 137 int64_t start_time = srs_get_system_time_ms();
126 138
  139 + // sample play
127 srs_info("start play test."); 140 srs_info("start play test.");
128 - ret = check_play(play_duration_ms,  
129 - play_interval_ms, play_actual_duration_ms, play_bytes, limit_kbps);  
130 - if (ret != ERROR_SUCCESS) { 141 +
  142 + if ((ret = check_play(&play_sample, limit)) != ERROR_SUCCESS) {
131 srs_error("band width play check failed. ret=%d", ret); 143 srs_error("band width play check failed. ret=%d", ret);
132 return ret; 144 return ret;
133 } 145 }
134 - srs_info("stop play test.");  
135 146
  147 + play_sample.calc_kbps();
  148 + srs_info("stop play test. kbps=%d", play_sample.kbps);
  149 +
  150 + // sample publish
136 srs_info("start publish test."); 151 srs_info("start publish test.");
137 - ret = check_publish(publish_duration_ms,  
138 - publish_interval_ms, publish_actual_duration_ms, publish_bytes, limit_kbps);  
139 - if (ret != ERROR_SUCCESS) { 152 +
  153 + if ((ret = check_publish(&publish_sample, limit)) != ERROR_SUCCESS) {
140 srs_error("band width publish check failed. ret=%d", ret); 154 srs_error("band width publish check failed. ret=%d", ret);
141 return ret; 155 return ret;
142 } 156 }
143 - srs_info("stop publish test."); 157 +
  158 + publish_sample.calc_kbps();
  159 + srs_info("stop publish test. kbps=%d", publish_sample.kbps);
144 160
  161 + // stop test.
145 int64_t end_time = srs_get_system_time_ms(); 162 int64_t end_time = srs_get_system_time_ms();
146 - int play_kbps = play_bytes * 8 / play_actual_duration_ms;  
147 - int publish_kbps = publish_bytes * 8 / publish_actual_duration_ms;  
148 163
149 - srs_trace("bandwidth check finished. start=%"PRId64"ms, end=%"PRId64"ms, "  
150 - "duartion=%dms, play=%dkbps, publish=%dkbps, tcUrl=%s, ret=%#x",  
151 - start_time, end_time, (int)(end_time - start_time), play_kbps, publish_kbps,  
152 - _req->tcUrl.c_str(), ret); 164 + srs_trace("bandwidth ok. duartion=%dms(%d+%d), play=%dkbps, publish=%dkbps",
  165 + (int)(end_time - start_time), play_sample.actual_duration_ms,
  166 + publish_sample.actual_duration_ms, play_sample.kbps,
  167 + publish_sample.kbps);
153 168
154 // send finished msg, 169 // send finished msg,
155 // flash client will close connection when got this packet, 170 // flash client will close connection when got this packet,
@@ -158,12 +173,12 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) @@ -158,12 +173,12 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit)
158 pkt->data->set("code", SrsAmf0Any::number(ERROR_SUCCESS)); 173 pkt->data->set("code", SrsAmf0Any::number(ERROR_SUCCESS));
159 pkt->data->set("start_time", SrsAmf0Any::number(start_time)); 174 pkt->data->set("start_time", SrsAmf0Any::number(start_time));
160 pkt->data->set("end_time", SrsAmf0Any::number(end_time)); 175 pkt->data->set("end_time", SrsAmf0Any::number(end_time));
161 - pkt->data->set("play_kbps", SrsAmf0Any::number(play_kbps));  
162 - pkt->data->set("publish_kbps", SrsAmf0Any::number(publish_kbps));  
163 - pkt->data->set("play_bytes", SrsAmf0Any::number(play_bytes));  
164 - pkt->data->set("play_time", SrsAmf0Any::number(play_actual_duration_ms));  
165 - pkt->data->set("publish_bytes", SrsAmf0Any::number(publish_bytes));  
166 - pkt->data->set("publish_time", SrsAmf0Any::number(publish_actual_duration_ms)); 176 + pkt->data->set("play_kbps", SrsAmf0Any::number(play_sample.kbps));
  177 + pkt->data->set("publish_kbps", SrsAmf0Any::number(publish_sample.kbps));
  178 + pkt->data->set("play_bytes", SrsAmf0Any::number(play_sample.bytes));
  179 + pkt->data->set("publish_bytes", SrsAmf0Any::number(publish_sample.bytes));
  180 + pkt->data->set("play_time", SrsAmf0Any::number(play_sample.actual_duration_ms));
  181 + pkt->data->set("publish_time", SrsAmf0Any::number(publish_sample.actual_duration_ms));
167 182
168 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 183 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
169 srs_error("send bandwidth check finish message failed. ret=%d", ret); 184 srs_error("send bandwidth check finish message failed. ret=%d", ret);
@@ -197,9 +212,7 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit) @@ -197,9 +212,7 @@ int SrsBandwidth::do_bandwidth_check(SrsKbpsLimit* limit)
197 return ret; 212 return ret;
198 } 213 }
199 214
200 -int SrsBandwidth::check_play(  
201 - int duration_ms, int interval_ms, int& actual_duration_ms,  
202 - int& play_bytes, int max_play_kbps) 215 +int SrsBandwidth::check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
203 { 216 {
204 int ret = ERROR_SUCCESS; 217 int ret = ERROR_SUCCESS;
205 218
@@ -207,8 +220,8 @@ int SrsBandwidth::check_play( @@ -207,8 +220,8 @@ int SrsBandwidth::check_play(
207 // send start play command to client 220 // send start play command to client
208 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_play(); 221 SrsBandwidthPacket* pkt = SrsBandwidthPacket::create_start_play();
209 222
210 - pkt->data->set("duration_ms", SrsAmf0Any::number(duration_ms));  
211 - pkt->data->set("interval_ms", SrsAmf0Any::number(interval_ms)); 223 + pkt->data->set("duration_ms", SrsAmf0Any::number(sample->duration_ms));
  224 + pkt->data->set("interval_ms", SrsAmf0Any::number(sample->interval_ms));
212 225
213 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) { 226 if ((ret = _rtmp->send_and_free_packet(pkt, 0)) != ERROR_SUCCESS) {
214 srs_error("send bandwidth check start play message failed. ret=%d", ret); 227 srs_error("send bandwidth check start play message failed. ret=%d", ret);
@@ -319,9 +332,7 @@ int SrsBandwidth::check_play( @@ -319,9 +332,7 @@ int SrsBandwidth::check_play(
319 return ret; 332 return ret;
320 } 333 }
321 334
322 -int SrsBandwidth::check_publish(  
323 - int duration_ms, int interval_ms, int& actual_duration_ms,  
324 - int& publish_bytes, int max_pub_kbps) 335 +int SrsBandwidth::check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit)
325 { 336 {
326 int ret = ERROR_SUCCESS; 337 int ret = ERROR_SUCCESS;
327 338
@@ -61,9 +61,18 @@ public: @@ -61,9 +61,18 @@ public:
61 * the actual test bytes 61 * the actual test bytes
62 */ 62 */
63 int bytes; 63 int bytes;
  64 + /**
  65 + * the actual test kbps
  66 + */
  67 + int kbps;
64 public: 68 public:
65 SrsBandwidthSample(); 69 SrsBandwidthSample();
66 virtual ~SrsBandwidthSample(); 70 virtual ~SrsBandwidthSample();
  71 +public:
  72 + /**
  73 + * use current sample data to calc the kbps.
  74 + */
  75 + virtual void calc_kbps();
67 }; 76 };
68 77
69 /** 78 /**
@@ -125,8 +134,14 @@ private: @@ -125,8 +134,14 @@ private:
125 * @param limit, the bandwidth limit object, to slowdown if exceed the kbps. 134 * @param limit, the bandwidth limit object, to slowdown if exceed the kbps.
126 */ 135 */
127 virtual int do_bandwidth_check(SrsKbpsLimit* limit); 136 virtual int do_bandwidth_check(SrsKbpsLimit* limit);
128 - virtual int check_play(int duration_ms, int interval_ms, int& actual_duration_ms, int& play_bytes, int max_play_kbps);  
129 - virtual int check_publish(int duration_ms, int interval_ms, int& actual_duration_ms, int& publish_bytes, int max_pub_kbps); 137 + /**
  138 + * play sample under specified kbps limit.
  139 + */
  140 + virtual int check_play(SrsBandwidthSample* sample, SrsKbpsLimit* limit);
  141 + /**
  142 + * publish sample under specified kbps limit.
  143 + */
  144 + virtual int check_publish(SrsBandwidthSample* sample, SrsKbpsLimit* limit);
130 }; 145 };
131 146
132 #endif 147 #endif