winlin

merge srs2 for kbps

@@ -205,6 +205,7 @@ Please select your language: @@ -205,6 +205,7 @@ Please select your language:
205 205
206 ### V2 changes 206 ### V2 changes
207 207
  208 +* v2.0, 2017-01-11, fix [#588][bug #588], kbps interface error. 2.0.228
208 * v2.0, 2017-01-11, fix [#736][bug #736], recovery the hls dispose. 2.0.227 209 * v2.0, 2017-01-11, fix [#736][bug #736], recovery the hls dispose. 2.0.227
209 * v2.0, 2017-01-10, refine hls html5 video template. 210 * v2.0, 2017-01-10, refine hls html5 video template.
210 * v2.0, 2017-01-10, fix [#635][bug #635], hls support NonIDR(open gop). 2.0.226 211 * v2.0, 2017-01-10, fix [#635][bug #635], hls support NonIDR(open gop). 2.0.226
@@ -1368,6 +1369,9 @@ Winlin @@ -1368,6 +1369,9 @@ Winlin
1368 [bug #513]: https://github.com/ossrs/srs/issues/513 1369 [bug #513]: https://github.com/ossrs/srs/issues/513
1369 [bug #730]: https://github.com/ossrs/srs/issues/730 1370 [bug #730]: https://github.com/ossrs/srs/issues/730
1370 [bug #635]: https://github.com/ossrs/srs/issues/635 1371 [bug #635]: https://github.com/ossrs/srs/issues/635
  1372 +[bug #736]: https://github.com/ossrs/srs/issues/736
  1373 +[bug #588]: https://github.com/ossrs/srs/issues/588
  1374 +[bug #xxxxxxxxxx]: https://github.com/ossrs/srs/issues/xxxxxxxxxx
1371 [bug #735]: https://github.com/ossrs/srs/issues/735 1375 [bug #735]: https://github.com/ossrs/srs/issues/735
1372 [bug #xxxxxxxxxx]: https://github.com/ossrs/srs/issues/xxxxxxxxxx 1376 [bug #xxxxxxxxxx]: https://github.com/ossrs/srs/issues/xxxxxxxxxx
1373 1377
@@ -120,7 +120,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out) @@ -120,7 +120,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
120 } 120 }
121 // save the old in bytes. 121 // save the old in bytes.
122 if (is.io.in) { 122 if (is.io.in) {
123 - is.bytes += is.last_bytes - is.io_bytes_base; 123 + is.bytes += is.io.in->get_recv_bytes() - is.io_bytes_base;
124 } 124 }
125 // use new io. 125 // use new io.
126 is.io.in = in; 126 is.io.in = in;
@@ -138,7 +138,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out) @@ -138,7 +138,7 @@ void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
138 } 138 }
139 // save the old in bytes. 139 // save the old in bytes.
140 if (os.io.out) { 140 if (os.io.out) {
141 - os.bytes += os.last_bytes - os.io_bytes_base; 141 + os.bytes += os.io.out->get_send_bytes() - os.io_bytes_base;
142 } 142 }
143 // use new io. 143 // use new io.
144 os.io.out = out; 144 os.io.out = out;
@@ -192,12 +192,46 @@ int SrsKbps::get_recv_kbps_5m() @@ -192,12 +192,46 @@ int SrsKbps::get_recv_kbps_5m()
192 192
193 int64_t SrsKbps::get_send_bytes() 193 int64_t SrsKbps::get_send_bytes()
194 { 194 {
195 - return os.get_total_bytes(); 195 + // we must calc the send bytes dynamically,
  196 + // to not depends on the sample(which used to calc the kbps).
  197 + // @read https://github.com/ossrs/srs/issues/588
  198 +
  199 + // session start bytes.
  200 + int64_t bytes = os.bytes;
  201 +
  202 + // When exists active session, use it to get the last bytes.
  203 + if (os.io.out) {
  204 + bytes += os.io.out->get_send_bytes() - os.io_bytes_base;
  205 + return bytes;
  206 + }
  207 +
  208 + // When no active session, the last_bytes record the last valid bytes.
  209 + // TODO: Maybe the bellow bytes is zero, because the ios.io.out is NULL.
  210 + bytes += os.last_bytes - os.io_bytes_base;
  211 +
  212 + return bytes;
196 } 213 }
197 214
198 int64_t SrsKbps::get_recv_bytes() 215 int64_t SrsKbps::get_recv_bytes()
199 { 216 {
200 - return is.get_total_bytes(); 217 + // we must calc the send bytes dynamically,
  218 + // to not depends on the sample(which used to calc the kbps).
  219 + // @read https://github.com/ossrs/srs/issues/588
  220 +
  221 + // session start bytes.
  222 + int64_t bytes = is.bytes;
  223 +
  224 + // When exists active session, use it to get the last bytes.
  225 + if (is.io.in) {
  226 + bytes += is.io.in->get_recv_bytes() - is.io_bytes_base;
  227 + return bytes;
  228 + }
  229 +
  230 + // When no active session, the last_bytes record the last valid bytes.
  231 + // TODO: Maybe the bellow bytes is zero, because the ios.io.out is NULL.
  232 + bytes += is.last_bytes - is.io_bytes_base;
  233 +
  234 + return bytes;
201 } 235 }
202 236
203 void SrsKbps::resample() 237 void SrsKbps::resample()
@@ -250,3 +284,8 @@ void SrsKbps::sample() @@ -250,3 +284,8 @@ void SrsKbps::sample()
250 os.sample(); 284 os.sample();
251 } 285 }
252 286
  287 +int SrsKbps::size_memory()
  288 +{
  289 + return sizeof(SrsKbps);
  290 +}
  291 +
@@ -95,13 +95,9 @@ public: @@ -95,13 +95,9 @@ public:
95 SrsKbpsSlice(); 95 SrsKbpsSlice();
96 virtual ~SrsKbpsSlice(); 96 virtual ~SrsKbpsSlice();
97 public: 97 public:
98 - /**  
99 - * get current total bytes.  
100 - */ 98 + // Get current total bytes, not depend on sample().
101 virtual int64_t get_total_bytes(); 99 virtual int64_t get_total_bytes();
102 - /**  
103 - * resample all samples.  
104 - */ 100 + // Resample the slice to calculate the kbps.
105 virtual void sample(); 101 virtual void sample();
106 }; 102 };
107 103
@@ -161,6 +157,13 @@ public: @@ -161,6 +157,13 @@ public:
161 * delta->resample(); 157 * delta->resample();
162 * printf("delta is %d/%d", delta->get_send_bytes_delta(), delta->get_recv_bytes_delta()); 158 * printf("delta is %d/%d", delta->get_send_bytes_delta(), delta->get_recv_bytes_delta());
163 * delta->cleanup(); 159 * delta->cleanup();
  160 + * 4. kbps used as ISrsProtocolStatistic, to provides raw bytes:
  161 + * SrsKbps* kbps = ...;
  162 + * kbps->set_io(in, out);
  163 + * // both kbps->get_recv_bytes() and kbps->get_send_bytes() are available.
  164 +* // we can use the kbps as the data source of another kbps:
  165 + * SrsKbps* user = ...;
  166 + * user->set_io(kbps, kbps);
164 * the server never know how many bytes already send/recv, for the connection maybe closed. 167 * the server never know how many bytes already send/recv, for the connection maybe closed.
165 */ 168 */
166 class SrsKbps : public virtual ISrsProtocolStatistic, public virtual IKbpsDelta 169 class SrsKbps : public virtual ISrsProtocolStatistic, public virtual IKbpsDelta
@@ -195,26 +198,15 @@ public: @@ -195,26 +198,15 @@ public:
195 // 5m 198 // 5m
196 virtual int get_send_kbps_5m(); 199 virtual int get_send_kbps_5m();
197 virtual int get_recv_kbps_5m(); 200 virtual int get_recv_kbps_5m();
  201 +// interface ISrsProtocolStatistic
198 public: 202 public:
199 - /**  
200 - * get the total send/recv bytes, from the startup of the oldest io.  
201 - * @remark, use sample() to update data.  
202 - */  
203 virtual int64_t get_send_bytes(); 203 virtual int64_t get_send_bytes();
204 virtual int64_t get_recv_bytes(); 204 virtual int64_t get_recv_bytes();
  205 +// interface IKbpsDelta
205 public: 206 public:
206 - /**  
207 - * resample to get the delta.  
208 - */  
209 virtual void resample(); 207 virtual void resample();
210 - /**  
211 - * get the delta of send/recv bytes.  
212 - */  
213 virtual int64_t get_send_bytes_delta(); 208 virtual int64_t get_send_bytes_delta();
214 virtual int64_t get_recv_bytes_delta(); 209 virtual int64_t get_recv_bytes_delta();
215 - /**  
216 - * cleanup the delta.  
217 - */  
218 virtual void cleanup(); 210 virtual void cleanup();
219 public: 211 public:
220 /** 212 /**
@@ -232,6 +224,9 @@ public: @@ -232,6 +224,9 @@ public:
232 * use the add_delta() is better solutions. 224 * use the add_delta() is better solutions.
233 */ 225 */
234 virtual void sample(); 226 virtual void sample();
  227 +// interface ISrsMemorySizer
  228 +public:
  229 + virtual int size_memory();
235 }; 230 };
236 231
237 #endif 232 #endif