winlin

refine the kbps calc module. 0.9.93

@@ -229,6 +229,7 @@ Supported operating systems and hardware: @@ -229,6 +229,7 @@ Supported operating systems and hardware:
229 * 2013-10-17, Created.<br/> 229 * 2013-10-17, Created.<br/>
230 230
231 ## History 231 ## History
  232 +* v1.0, 2014-05-12, refine the kbps calc module. 0.9.93
232 * v1.0, 2014-05-08, edge support FMS origin server. 0.9.92 233 * v1.0, 2014-05-08, edge support FMS origin server. 0.9.92
233 * v1.0, 2014-04-28, [1.0 mainline2(0.9.79)](https://github.com/winlinvip/simple-rtmp-server/releases/tag/1.0.mainline2) released. 35255 lines. 234 * v1.0, 2014-04-28, [1.0 mainline2(0.9.79)](https://github.com/winlinvip/simple-rtmp-server/releases/tag/1.0.mainline2) released. 35255 lines.
234 * v1.0, 2014-04-28, support full edge RTMP server. 0.9.79 235 * v1.0, 2014-04-28, support full edge RTMP server. 0.9.79
@@ -26,11 +26,21 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -26,11 +26,21 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include <srs_kernel_error.hpp> 26 #include <srs_kernel_error.hpp>
27 #include <srs_kernel_log.hpp> 27 #include <srs_kernel_log.hpp>
28 #include <srs_protocol_io.hpp> 28 #include <srs_protocol_io.hpp>
  29 +#include <srs_kernel_utility.hpp>
  30 +
  31 +SrsKbpsSlice::SrsKbpsSlice()
  32 +{
  33 + io.in = NULL;
  34 + io.out = NULL;
  35 + last_bytes = io_bytes_base = starttime = bytes = 0;
  36 +}
  37 +
  38 +SrsKbpsSlice::~SrsKbpsSlice()
  39 +{
  40 +}
29 41
30 SrsKbps::SrsKbps() 42 SrsKbps::SrsKbps()
31 { 43 {
32 - _in = NULL;  
33 - _out = NULL;  
34 } 44 }
35 45
36 SrsKbps::~SrsKbps() 46 SrsKbps::~SrsKbps()
@@ -39,17 +49,72 @@ SrsKbps::~SrsKbps() @@ -39,17 +49,72 @@ SrsKbps::~SrsKbps()
39 49
40 void SrsKbps::set_io(ISrsProtocolReader* in, ISrsProtocolWriter* out) 50 void SrsKbps::set_io(ISrsProtocolReader* in, ISrsProtocolWriter* out)
41 { 51 {
42 - _in = in;  
43 - _out = out; 52 + // set input stream
  53 + // now, set start time.
  54 + if (is.starttime == 0) {
  55 + is.starttime = srs_get_system_time_ms();
  56 + }
  57 + // save the old in bytes.
  58 + if (is.io.in) {
  59 + is.bytes += is.last_bytes - is.io_bytes_base;
  60 + }
  61 + // use new io.
  62 + is.io.in = in;
  63 + is.last_bytes = is.io_bytes_base = 0;
  64 + if (in) {
  65 + is.last_bytes = is.io_bytes_base = in->get_recv_bytes();
  66 + }
  67 +
  68 + // set output stream
  69 + // now, set start time.
  70 + if (os.starttime == 0) {
  71 + os.starttime = srs_get_system_time_ms();
  72 + }
  73 + // save the old in bytes.
  74 + if (os.io.out) {
  75 + os.bytes += os.last_bytes - os.io_bytes_base;
  76 + }
  77 + // use new io.
  78 + os.io.out = out;
  79 + os.last_bytes = os.io_bytes_base = 0;
  80 + if (out) {
  81 + os.last_bytes = os.io_bytes_base = out->get_send_bytes();
  82 + }
44 } 83 }
45 84
46 int SrsKbps::get_send_kbps() 85 int SrsKbps::get_send_kbps()
47 { 86 {
  87 + int64_t duration = srs_get_system_time_ms() - is.starttime;
  88 + int64_t bytes = get_send_bytes();
  89 + if (duration <= 0) {
48 return 0; 90 return 0;
  91 + }
  92 + return bytes * 8 / duration;
49 } 93 }
50 94
51 int SrsKbps::get_recv_kbps() 95 int SrsKbps::get_recv_kbps()
52 { 96 {
  97 + int64_t duration = srs_get_system_time_ms() - os.starttime;
  98 + int64_t bytes = get_recv_bytes();
  99 + if (duration <= 0) {
53 return 0; 100 return 0;
  101 + }
  102 + return bytes * 8 / duration;
  103 +}
  104 +
  105 +int64_t SrsKbps::get_send_bytes()
  106 +{
  107 + if (os.io.out) {
  108 + os.last_bytes = os.io.out->get_send_bytes();
  109 + }
  110 + return os.bytes + os.last_bytes - os.io_bytes_base;
  111 +}
  112 +
  113 +int64_t SrsKbps::get_recv_bytes()
  114 +{
  115 + if (is.io.in) {
  116 + is.last_bytes = is.io.in->get_recv_bytes();
  117 + }
  118 + return is.bytes + is.last_bytes - is.io_bytes_base;
54 } 119 }
55 120
@@ -34,21 +34,63 @@ class ISrsProtocolReader; @@ -34,21 +34,63 @@ class ISrsProtocolReader;
34 class ISrsProtocolWriter; 34 class ISrsProtocolWriter;
35 35
36 /** 36 /**
  37 +* a slice of kbps statistic, for input or output.
  38 +*/
  39 +class SrsKbpsSlice
  40 +{
  41 +private:
  42 + union slice_io {
  43 + ISrsProtocolReader* in;
  44 + ISrsProtocolWriter* out;
  45 + };
  46 +public:
  47 + slice_io io;
  48 + int64_t bytes;
  49 + int64_t starttime;
  50 + // startup bytes number for io when set it,
  51 + // the base offset of bytes for io.
  52 + int64_t io_bytes_base;
  53 + // last updated bytes number,
  54 + // cache for io maybe freed.
  55 + int64_t last_bytes;
  56 +public:
  57 + SrsKbpsSlice();
  58 + virtual ~SrsKbpsSlice();
  59 +};
  60 +
  61 +/**
37 * to statistic the kbps of io. 62 * to statistic the kbps of io.
38 */ 63 */
39 class SrsKbps 64 class SrsKbps
40 { 65 {
41 private: 66 private:
42 - ISrsProtocolReader* _in;  
43 - ISrsProtocolWriter* _out; 67 + SrsKbpsSlice is;
  68 + SrsKbpsSlice os;
44 public: 69 public:
45 SrsKbps(); 70 SrsKbps();
46 virtual ~SrsKbps(); 71 virtual ~SrsKbps();
47 public: 72 public:
  73 + /**
  74 + * set the underlayer reader/writer,
  75 + * if the io destroied, for instance, the forwarder reconnect,
  76 + * user must set the io of SrsKbps to NULL to continue to use the kbps object.
  77 + * @param in the input stream statistic. can be NULL.
  78 + * @param out the output stream statistic. can be NULL.
  79 + * @remark if in/out is NULL, use the cached data for kbps.
  80 + */
48 virtual void set_io(ISrsProtocolReader* in, ISrsProtocolWriter* out); 81 virtual void set_io(ISrsProtocolReader* in, ISrsProtocolWriter* out);
49 public: 82 public:
  83 + /**
  84 + * get total kbps, duration is from the startup of io.
  85 + */
50 virtual int get_send_kbps(); 86 virtual int get_send_kbps();
51 virtual int get_recv_kbps(); 87 virtual int get_recv_kbps();
  88 +public:
  89 + /**
  90 + * get the total send/recv bytes, from the startup of the oldest io.
  91 + */
  92 + virtual int64_t get_send_bytes();
  93 + virtual int64_t get_recv_bytes();
52 }; 94 };
53 95
54 #endif 96 #endif
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "0" 32 #define VERSION_MAJOR "0"
33 #define VERSION_MINOR "9" 33 #define VERSION_MINOR "9"
34 -#define VERSION_REVISION "92" 34 +#define VERSION_REVISION "93"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "srs" 37 #define RTMP_SIG_SRS_KEY "srs"