winlin

fix #316, http api provides stream/vhost/srs/server bytes, codec and count. 2.0.136

@@ -550,6 +550,7 @@ Supported operating systems and hardware: @@ -550,6 +550,7 @@ Supported operating systems and hardware:
550 550
551 ### SRS 2.0 history 551 ### SRS 2.0 history
552 552
  553 +* v2.0, 2015-03-08, fix [#316](https://github.com/winlinvip/simple-rtmp-server/issues/316), http api provides stream/vhost/srs/server bytes, codec and count. 2.0.136.
553 * v2.0, 2015-03-08, fix [#310](https://github.com/winlinvip/simple-rtmp-server/issues/310), refine aac LC, support aac HE/HEv2. 2.0.134. 554 * v2.0, 2015-03-08, fix [#310](https://github.com/winlinvip/simple-rtmp-server/issues/310), refine aac LC, support aac HE/HEv2. 2.0.134.
554 * v2.0, 2015-03-06, for [#322](https://github.com/winlinvip/simple-rtmp-server/issues/322), fix http-flv stream bug, support multiple streams. 2.0.133. 555 * v2.0, 2015-03-06, for [#322](https://github.com/winlinvip/simple-rtmp-server/issues/322), fix http-flv stream bug, support multiple streams. 2.0.133.
555 * v2.0, 2015-03-06, refine http request parse. 2.0.132. 556 * v2.0, 2015-03-06, refine http request parse. 2.0.132.
@@ -30,6 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,6 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 30
31 SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd) 31 SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd)
32 { 32 {
  33 + id = 0;
33 server = srs_server; 34 server = srs_server;
34 stfd = client_stfd; 35 stfd = client_stfd;
35 36
@@ -55,6 +56,8 @@ int SrsConnection::cycle() @@ -55,6 +56,8 @@ int SrsConnection::cycle()
55 int ret = ERROR_SUCCESS; 56 int ret = ERROR_SUCCESS;
56 57
57 _srs_context->generate_id(); 58 _srs_context->generate_id();
  59 + id = _srs_context->get_id();
  60 +
58 ip = srs_get_peer_ip(st_netfd_fileno(stfd)); 61 ip = srs_get_peer_ip(st_netfd_fileno(stfd));
59 62
60 ret = do_cycle(); 63 ret = do_cycle();
@@ -86,6 +89,11 @@ void SrsConnection::on_thread_stop() @@ -86,6 +89,11 @@ void SrsConnection::on_thread_stop()
86 server->remove(this); 89 server->remove(this);
87 } 90 }
88 91
  92 +int SrsConnection::srs_id()
  93 +{
  94 + return id;
  95 +}
  96 +
89 void SrsConnection::stop() 97 void SrsConnection::stop()
90 { 98 {
91 srs_close_stfd(stfd); 99 srs_close_stfd(stfd);
@@ -51,6 +51,10 @@ private: @@ -51,6 +51,10 @@ private:
51 * when thread stop, the connection will be delete by server. 51 * when thread stop, the connection will be delete by server.
52 */ 52 */
53 SrsThread* pthread; 53 SrsThread* pthread;
  54 + /**
  55 + * the id of connection.
  56 + */
  57 + int id;
54 protected: 58 protected:
55 /** 59 /**
56 * the server object to manage the connection. 60 * the server object to manage the connection.
@@ -92,14 +96,9 @@ public: @@ -92,14 +96,9 @@ public:
92 virtual void on_thread_stop(); 96 virtual void on_thread_stop();
93 public: 97 public:
94 /** 98 /**
95 - * reset and start sample of bytes.  
96 - * when server to get the kbps of connection,  
97 - * it cannot wait the connection terminated then get the kbps,  
98 - * it must sample the kbps every some interval, for instance, 9s to sample all connections kbps,  
99 - * all connections will extends from IKbpsDelta which provides the bytes delta,  
100 - * while the delta must be update by the sample which invoke by the kbps_resample(). 99 + * get the srs id which identify the client.
101 */ 100 */
102 - virtual void kbps_resample() = 0; 101 + virtual int srs_id();
103 protected: 102 protected:
104 /** 103 /**
105 * for concrete connection to do the cycle. 104 * for concrete connection to do the cycle.
@@ -486,7 +486,7 @@ SrsHttpApi::~SrsHttpApi() @@ -486,7 +486,7 @@ SrsHttpApi::~SrsHttpApi()
486 srs_freep(parser); 486 srs_freep(parser);
487 } 487 }
488 488
489 -void SrsHttpApi::kbps_resample() 489 +void SrsHttpApi::resample()
490 { 490 {
491 // TODO: FIXME: implements it 491 // TODO: FIXME: implements it
492 } 492 }
@@ -503,6 +503,11 @@ int64_t SrsHttpApi::get_recv_bytes_delta() @@ -503,6 +503,11 @@ int64_t SrsHttpApi::get_recv_bytes_delta()
503 return 0; 503 return 0;
504 } 504 }
505 505
  506 +void SrsHttpApi::cleanup()
  507 +{
  508 + // TODO: FIXME: implements it
  509 +}
  510 +
506 int SrsHttpApi::do_cycle() 511 int SrsHttpApi::do_cycle()
507 { 512 {
508 int ret = ERROR_SUCCESS; 513 int ret = ERROR_SUCCESS;
@@ -168,12 +168,12 @@ private: @@ -168,12 +168,12 @@ private:
168 public: 168 public:
169 SrsHttpApi(SrsServer* svr, st_netfd_t fd, SrsHttpServeMux* m); 169 SrsHttpApi(SrsServer* svr, st_netfd_t fd, SrsHttpServeMux* m);
170 virtual ~SrsHttpApi(); 170 virtual ~SrsHttpApi();
171 -public:  
172 - virtual void kbps_resample();  
173 // interface IKbpsDelta 171 // interface IKbpsDelta
174 public: 172 public:
  173 + virtual void resample();
175 virtual int64_t get_send_bytes_delta(); 174 virtual int64_t get_send_bytes_delta();
176 virtual int64_t get_recv_bytes_delta(); 175 virtual int64_t get_recv_bytes_delta();
  176 + virtual void cleanup();
177 protected: 177 protected:
178 virtual int do_cycle(); 178 virtual int do_cycle();
179 private: 179 private:
@@ -1193,7 +1193,7 @@ SrsHttpConn::~SrsHttpConn() @@ -1193,7 +1193,7 @@ SrsHttpConn::~SrsHttpConn()
1193 srs_freep(parser); 1193 srs_freep(parser);
1194 } 1194 }
1195 1195
1196 -void SrsHttpConn::kbps_resample() 1196 +void SrsHttpConn::resample()
1197 { 1197 {
1198 // TODO: FIXME: implements it 1198 // TODO: FIXME: implements it
1199 } 1199 }
@@ -1210,6 +1210,11 @@ int64_t SrsHttpConn::get_recv_bytes_delta() @@ -1210,6 +1210,11 @@ int64_t SrsHttpConn::get_recv_bytes_delta()
1210 return 0; 1210 return 0;
1211 } 1211 }
1212 1212
  1213 +void SrsHttpConn::cleanup()
  1214 +{
  1215 + // TODO: FIXME: implements it
  1216 +}
  1217 +
1213 int SrsHttpConn::do_cycle() 1218 int SrsHttpConn::do_cycle()
1214 { 1219 {
1215 int ret = ERROR_SUCCESS; 1220 int ret = ERROR_SUCCESS;
@@ -360,12 +360,12 @@ private: @@ -360,12 +360,12 @@ private:
360 public: 360 public:
361 SrsHttpConn(SrsServer* svr, st_netfd_t fd, SrsHttpServer* m); 361 SrsHttpConn(SrsServer* svr, st_netfd_t fd, SrsHttpServer* m);
362 virtual ~SrsHttpConn(); 362 virtual ~SrsHttpConn();
363 -public:  
364 - virtual void kbps_resample();  
365 // interface IKbpsDelta 363 // interface IKbpsDelta
366 public: 364 public:
  365 + virtual void resample();
367 virtual int64_t get_send_bytes_delta(); 366 virtual int64_t get_send_bytes_delta();
368 virtual int64_t get_recv_bytes_delta(); 367 virtual int64_t get_recv_bytes_delta();
  368 + virtual void cleanup();
369 protected: 369 protected:
370 virtual int do_cycle(); 370 virtual int do_cycle();
371 private: 371 private:
@@ -203,20 +203,29 @@ int64_t SrsKbps::get_recv_bytes() @@ -203,20 +203,29 @@ int64_t SrsKbps::get_recv_bytes()
203 return is.get_total_bytes(); 203 return is.get_total_bytes();
204 } 204 }
205 205
  206 +void SrsKbps::resample()
  207 +{
  208 + sample();
  209 +}
  210 +
206 int64_t SrsKbps::get_send_bytes_delta() 211 int64_t SrsKbps::get_send_bytes_delta()
207 { 212 {
208 int64_t delta = os.get_total_bytes() - os.delta_bytes; 213 int64_t delta = os.get_total_bytes() - os.delta_bytes;
209 - os.delta_bytes = os.get_total_bytes();  
210 return delta; 214 return delta;
211 } 215 }
212 216
213 int64_t SrsKbps::get_recv_bytes_delta() 217 int64_t SrsKbps::get_recv_bytes_delta()
214 { 218 {
215 int64_t delta = is.get_total_bytes() - is.delta_bytes; 219 int64_t delta = is.get_total_bytes() - is.delta_bytes;
216 - is.delta_bytes = is.get_total_bytes();  
217 return delta; 220 return delta;
218 } 221 }
219 222
  223 +void SrsKbps::cleanup()
  224 +{
  225 + os.delta_bytes = os.get_total_bytes();
  226 + is.delta_bytes = is.get_total_bytes();
  227 +}
  228 +
220 void SrsKbps::add_delta(IKbpsDelta* delta) 229 void SrsKbps::add_delta(IKbpsDelta* delta)
221 { 230 {
222 srs_assert(delta); 231 srs_assert(delta);
@@ -107,6 +107,11 @@ public: @@ -107,6 +107,11 @@ public:
107 107
108 /** 108 /**
109 * the interface which provices delta of bytes. 109 * the interface which provices delta of bytes.
  110 +* for a delta, for example, a live stream connection, we can got the delta by:
  111 +* IKbpsDelta* delta = ...;
  112 +* delta->resample();
  113 +* kbps->add_delta(delta);
  114 +* delta->cleanup();
110 */ 115 */
111 class IKbpsDelta 116 class IKbpsDelta
112 { 117 {
@@ -114,8 +119,19 @@ public: @@ -114,8 +119,19 @@ public:
114 IKbpsDelta(); 119 IKbpsDelta();
115 virtual ~IKbpsDelta(); 120 virtual ~IKbpsDelta();
116 public: 121 public:
  122 + /**
  123 + * resample to generate the value of delta bytes.
  124 + */
  125 + virtual void resample() = 0;
  126 + /**
  127 + * get the send or recv bytes delta.
  128 + */
117 virtual int64_t get_send_bytes_delta() = 0; 129 virtual int64_t get_send_bytes_delta() = 0;
118 virtual int64_t get_recv_bytes_delta() = 0; 130 virtual int64_t get_recv_bytes_delta() = 0;
  131 + /**
  132 + * cleanup the value of delta bytes.
  133 + */
  134 + virtual void cleanup() = 0;
119 }; 135 };
120 136
121 /** 137 /**
@@ -123,16 +139,21 @@ public: @@ -123,16 +139,21 @@ public:
123 * itself can be a statistic source, for example, used for SRS bytes stat. 139 * itself can be a statistic source, for example, used for SRS bytes stat.
124 * there are two usage scenarios: 140 * there are two usage scenarios:
125 * 1. connections to calc kbps by sample(): 141 * 1. connections to calc kbps by sample():
126 -* set_io(in, out)  
127 -* sample()  
128 -* get_xxx_kbps(). 142 +* SrsKbps* kbps = ...;
  143 +* kbps->set_io(in, out)
  144 +* kbps->sample()
  145 +* kbps->get_xxx_kbps().
129 * the connections know how many bytes already send/recv. 146 * the connections know how many bytes already send/recv.
130 * 2. server to calc kbps by add_delta(): 147 * 2. server to calc kbps by add_delta():
131 -* set_io(NULL, NULL) 148 +* SrsKbps* kbps = ...;
  149 +* kbps->set_io(NULL, NULL)
132 * for each connection in connections: 150 * for each connection in connections:
133 -* add_delta(connections) // where connection is a IKbpsDelta*  
134 -* sample()  
135 -* get_xxx_kbps(). 151 +* IKbpsDelta* delta = connection; // where connection implements IKbpsDelta
  152 +* delta->resample()
  153 +* kbps->add_delta(delta)
  154 +* delta->cleanup()
  155 +* kbps->sample()
  156 +* kbps->get_xxx_kbps().
136 * the server never know how many bytes already send/recv, for the connection maybe closed. 157 * the server never know how many bytes already send/recv, for the connection maybe closed.
137 */ 158 */
138 class SrsKbps : public virtual ISrsProtocolStatistic, public virtual IKbpsDelta 159 class SrsKbps : public virtual ISrsProtocolStatistic, public virtual IKbpsDelta
@@ -174,18 +195,26 @@ public: @@ -174,18 +195,26 @@ public:
174 */ 195 */
175 virtual int64_t get_send_bytes(); 196 virtual int64_t get_send_bytes();
176 virtual int64_t get_recv_bytes(); 197 virtual int64_t get_recv_bytes();
  198 +public:
  199 + /**
  200 + * resample to get the delta.
  201 + */
  202 + virtual void resample();
177 /** 203 /**
178 * get the delta of send/recv bytes. 204 * get the delta of send/recv bytes.
179 - * @remark, used for add_delta to calc the total system bytes/kbps.  
180 */ 205 */
181 virtual int64_t get_send_bytes_delta(); 206 virtual int64_t get_send_bytes_delta();
182 virtual int64_t get_recv_bytes_delta(); 207 virtual int64_t get_recv_bytes_delta();
  208 + /**
  209 + * cleanup the delta.
  210 + */
  211 + virtual void cleanup();
183 public: 212 public:
184 /** 213 /**
185 * add delta to kbps clac mechenism. 214 * add delta to kbps clac mechenism.
186 * we donot know the total bytes, but know the delta, for instance, 215 * we donot know the total bytes, but know the delta, for instance,
187 * for rtmp server to calc total bytes and kbps. 216 * for rtmp server to calc total bytes and kbps.
188 - * @remark user must invoke sample() when invoke this method. 217 + * @remark user must invoke sample() to calc result after invoke this method.
189 * @param delta, assert should never be NULL. 218 * @param delta, assert should never be NULL.
190 */ 219 */
191 virtual void add_delta(IKbpsDelta* delta); 220 virtual void add_delta(IKbpsDelta* delta);
@@ -110,11 +110,6 @@ SrsRtmpConn::~SrsRtmpConn() @@ -110,11 +110,6 @@ SrsRtmpConn::~SrsRtmpConn()
110 srs_freep(kbps); 110 srs_freep(kbps);
111 } 111 }
112 112
113 -void SrsRtmpConn::kbps_resample()  
114 -{  
115 - kbps->sample();  
116 -}  
117 -  
118 // TODO: return detail message when error for client. 113 // TODO: return detail message when error for client.
119 int SrsRtmpConn::do_cycle() 114 int SrsRtmpConn::do_cycle()
120 { 115 {
@@ -255,6 +250,11 @@ int SrsRtmpConn::on_reload_vhost_realtime(string vhost) @@ -255,6 +250,11 @@ int SrsRtmpConn::on_reload_vhost_realtime(string vhost)
255 return ret; 250 return ret;
256 } 251 }
257 252
  253 +void SrsRtmpConn::resample()
  254 +{
  255 + kbps->resample();
  256 +}
  257 +
258 int64_t SrsRtmpConn::get_send_bytes_delta() 258 int64_t SrsRtmpConn::get_send_bytes_delta()
259 { 259 {
260 return kbps->get_send_bytes_delta(); 260 return kbps->get_send_bytes_delta();
@@ -264,6 +264,11 @@ int64_t SrsRtmpConn::get_recv_bytes_delta() @@ -264,6 +264,11 @@ int64_t SrsRtmpConn::get_recv_bytes_delta()
264 { 264 {
265 return kbps->get_recv_bytes_delta(); 265 return kbps->get_recv_bytes_delta();
266 } 266 }
  267 +
  268 +void SrsRtmpConn::cleanup()
  269 +{
  270 + kbps->cleanup();
  271 +}
267 272
268 int SrsRtmpConn::service_cycle() 273 int SrsRtmpConn::service_cycle()
269 { 274 {
@@ -83,8 +83,6 @@ private: @@ -83,8 +83,6 @@ private:
83 public: 83 public:
84 SrsRtmpConn(SrsServer* srs_server, st_netfd_t client_stfd); 84 SrsRtmpConn(SrsServer* srs_server, st_netfd_t client_stfd);
85 virtual ~SrsRtmpConn(); 85 virtual ~SrsRtmpConn();
86 -public:  
87 - virtual void kbps_resample();  
88 protected: 86 protected:
89 virtual int do_cycle(); 87 virtual int do_cycle();
90 // interface ISrsReloadHandler 88 // interface ISrsReloadHandler
@@ -94,8 +92,10 @@ public: @@ -94,8 +92,10 @@ public:
94 virtual int on_reload_vhost_realtime(std::string vhost); 92 virtual int on_reload_vhost_realtime(std::string vhost);
95 // interface IKbpsDelta 93 // interface IKbpsDelta
96 public: 94 public:
  95 + virtual void resample();
97 virtual int64_t get_send_bytes_delta(); 96 virtual int64_t get_send_bytes_delta();
98 virtual int64_t get_recv_bytes_delta(); 97 virtual int64_t get_recv_bytes_delta();
  98 + virtual void cleanup();
99 private: 99 private:
100 // when valid and connected to vhost/app, service the client. 100 // when valid and connected to vhost/app, service the client.
101 virtual int service_cycle(); 101 virtual int service_cycle();
@@ -45,6 +45,7 @@ using namespace std; @@ -45,6 +45,7 @@ using namespace std;
45 #include <srs_app_heartbeat.hpp> 45 #include <srs_app_heartbeat.hpp>
46 #include <srs_app_mpegts_udp.hpp> 46 #include <srs_app_mpegts_udp.hpp>
47 #include <srs_app_rtsp.hpp> 47 #include <srs_app_rtsp.hpp>
  48 +#include <srs_app_statistic.hpp>
48 49
49 // signal defines. 50 // signal defines.
50 #define SIGNAL_RELOAD SIGHUP 51 #define SIGNAL_RELOAD SIGHUP
@@ -392,7 +393,6 @@ SrsServer::SrsServer() @@ -392,7 +393,6 @@ SrsServer::SrsServer()
392 pid_fd = -1; 393 pid_fd = -1;
393 394
394 signal_manager = NULL; 395 signal_manager = NULL;
395 - kbps = NULL;  
396 396
397 // donot new object in constructor, 397 // donot new object in constructor,
398 // for some global instance is not ready now, 398 // for some global instance is not ready now,
@@ -452,7 +452,6 @@ void SrsServer::destroy() @@ -452,7 +452,6 @@ void SrsServer::destroy()
452 } 452 }
453 453
454 srs_freep(signal_manager); 454 srs_freep(signal_manager);
455 - srs_freep(kbps);  
456 455
457 // @remark never destroy the connections, 456 // @remark never destroy the connections,
458 // for it's still alive. 457 // for it's still alive.
@@ -478,10 +477,6 @@ int SrsServer::initialize() @@ -478,10 +477,6 @@ int SrsServer::initialize()
478 srs_assert(!signal_manager); 477 srs_assert(!signal_manager);
479 signal_manager = new SrsSignalManager(this); 478 signal_manager = new SrsSignalManager(this);
480 479
481 - srs_assert(!kbps);  
482 - kbps = new SrsKbps();  
483 - kbps->set_io(NULL, NULL);  
484 -  
485 #ifdef SRS_AUTO_HTTP_API 480 #ifdef SRS_AUTO_HTTP_API
486 if ((ret = http_api_mux->initialize()) != ERROR_SUCCESS) { 481 if ((ret = http_api_mux->initialize()) != ERROR_SUCCESS) {
487 return ret; 482 return ret;
@@ -745,12 +740,8 @@ void SrsServer::remove(SrsConnection* conn) @@ -745,12 +740,8 @@ void SrsServer::remove(SrsConnection* conn)
745 740
746 srs_info("conn removed. conns=%d", (int)conns.size()); 741 srs_info("conn removed. conns=%d", (int)conns.size());
747 742
748 - // resample the kbps to collect the delta.  
749 - conn->kbps_resample();  
750 -  
751 - // add delta of connection to server kbps.,  
752 - // for next sample() of server kbps can get the stat.  
753 - kbps->add_delta(conn); 743 + SrsStatistic* stat = SrsStatistic::instance();
  744 + stat->kbps_add_delta(conn);
754 745
755 // all connections are created by server, 746 // all connections are created by server,
756 // so we free it here. 747 // so we free it here.
@@ -868,7 +859,6 @@ int SrsServer::do_cycle() @@ -868,7 +859,6 @@ int SrsServer::do_cycle()
868 if ((i % SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES) == 0) { 859 if ((i % SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES) == 0) {
869 srs_info("update network server kbps info."); 860 srs_info("update network server kbps info.");
870 resample_kbps(); 861 resample_kbps();
871 - srs_update_rtmp_server((int)conns.size(), kbps);  
872 } 862 }
873 #ifdef SRS_AUTO_HTTP_PARSER 863 #ifdef SRS_AUTO_HTTP_PARSER
874 if (_srs_config->get_heartbeat_enabled()) { 864 if (_srs_config->get_heartbeat_enabled()) {
@@ -1019,22 +1009,23 @@ void SrsServer::close_listeners(SrsListenerType type) @@ -1019,22 +1009,23 @@ void SrsServer::close_listeners(SrsListenerType type)
1019 1009
1020 void SrsServer::resample_kbps() 1010 void SrsServer::resample_kbps()
1021 { 1011 {
  1012 + SrsStatistic* stat = SrsStatistic::instance();
  1013 +
1022 // collect delta from all clients. 1014 // collect delta from all clients.
1023 for (std::vector<SrsConnection*>::iterator it = conns.begin(); it != conns.end(); ++it) { 1015 for (std::vector<SrsConnection*>::iterator it = conns.begin(); it != conns.end(); ++it) {
1024 SrsConnection* conn = *it; 1016 SrsConnection* conn = *it;
1025 -  
1026 - // resample the kbps to collect the delta.  
1027 - conn->kbps_resample();  
1028 1017
1029 // add delta of connection to server kbps., 1018 // add delta of connection to server kbps.,
1030 // for next sample() of server kbps can get the stat. 1019 // for next sample() of server kbps can get the stat.
1031 - kbps->add_delta(conn); 1020 + stat->kbps_add_delta(conn);
1032 } 1021 }
1033 1022
1034 // TODO: FXME: support all other connections. 1023 // TODO: FXME: support all other connections.
1035 1024
1036 // sample the kbps, get the stat. 1025 // sample the kbps, get the stat.
1037 - kbps->sample(); 1026 + SrsKbps* kbps = stat->kbps_sample();
  1027 +
  1028 + srs_update_rtmp_server((int)conns.size(), kbps);
1038 } 1029 }
1039 1030
1040 int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd) 1031 int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
@@ -210,10 +210,6 @@ private: @@ -210,10 +210,6 @@ private:
210 */ 210 */
211 SrsSignalManager* signal_manager; 211 SrsSignalManager* signal_manager;
212 /** 212 /**
213 - * server total kbps.  
214 - */  
215 - SrsKbps* kbps;  
216 - /**  
217 * user send the signal, convert to variable. 213 * user send the signal, convert to variable.
218 */ 214 */
219 bool signal_reload; 215 bool signal_reload;
@@ -29,6 +29,8 @@ using namespace std; @@ -29,6 +29,8 @@ using namespace std;
29 29
30 #include <srs_rtmp_sdk.hpp> 30 #include <srs_rtmp_sdk.hpp>
31 #include <srs_app_json.hpp> 31 #include <srs_app_json.hpp>
  32 +#include <srs_app_kbps.hpp>
  33 +#include <srs_app_conn.hpp>
32 34
33 int64_t __srs_gvid = getpid(); 35 int64_t __srs_gvid = getpid();
34 36
@@ -40,10 +42,14 @@ int64_t __srs_generate_id() @@ -40,10 +42,14 @@ int64_t __srs_generate_id()
40 SrsStatisticVhost::SrsStatisticVhost() 42 SrsStatisticVhost::SrsStatisticVhost()
41 { 43 {
42 id = __srs_generate_id(); 44 id = __srs_generate_id();
  45 +
  46 + kbps = new SrsKbps();
  47 + kbps->set_io(NULL, NULL);
43 } 48 }
44 49
45 SrsStatisticVhost::~SrsStatisticVhost() 50 SrsStatisticVhost::~SrsStatisticVhost()
46 { 51 {
  52 + srs_freep(kbps);
47 } 53 }
48 54
49 SrsStatisticStream::SrsStatisticStream() 55 SrsStatisticStream::SrsStatisticStream()
@@ -61,10 +67,14 @@ SrsStatisticStream::SrsStatisticStream() @@ -61,10 +67,14 @@ SrsStatisticStream::SrsStatisticStream()
61 asample_rate = SrsCodecAudioSampleRateReserved; 67 asample_rate = SrsCodecAudioSampleRateReserved;
62 asound_type = SrsCodecAudioSoundTypeReserved; 68 asound_type = SrsCodecAudioSoundTypeReserved;
63 aac_object = SrsAacObjectTypeReserved; 69 aac_object = SrsAacObjectTypeReserved;
  70 +
  71 + kbps = new SrsKbps();
  72 + kbps->set_io(NULL, NULL);
64 } 73 }
65 74
66 SrsStatisticStream::~SrsStatisticStream() 75 SrsStatisticStream::~SrsStatisticStream()
67 { 76 {
  77 + srs_freep(kbps);
68 } 78 }
69 79
70 void SrsStatisticStream::close() 80 void SrsStatisticStream::close()
@@ -78,10 +88,15 @@ SrsStatistic* SrsStatistic::_instance = new SrsStatistic(); @@ -78,10 +88,15 @@ SrsStatistic* SrsStatistic::_instance = new SrsStatistic();
78 SrsStatistic::SrsStatistic() 88 SrsStatistic::SrsStatistic()
79 { 89 {
80 _server_id = __srs_generate_id(); 90 _server_id = __srs_generate_id();
  91 +
  92 + kbps = new SrsKbps();
  93 + kbps->set_io(NULL, NULL);
81 } 94 }
82 95
83 SrsStatistic::~SrsStatistic() 96 SrsStatistic::~SrsStatistic()
84 { 97 {
  98 + srs_freep(kbps);
  99 +
85 if (true) { 100 if (true) {
86 std::map<std::string, SrsStatisticVhost*>::iterator it; 101 std::map<std::string, SrsStatisticVhost*>::iterator it;
87 for (it = vhosts.begin(); it != vhosts.end(); it++) { 102 for (it = vhosts.begin(); it != vhosts.end(); it++) {
@@ -183,6 +198,49 @@ void SrsStatistic::on_disconnect(int id) @@ -183,6 +198,49 @@ void SrsStatistic::on_disconnect(int id)
183 } 198 }
184 } 199 }
185 200
  201 +void SrsStatistic::kbps_add_delta(SrsConnection* conn)
  202 +{
  203 + int id = conn->srs_id();
  204 + if (clients.find(id) == clients.end()) {
  205 + return;
  206 + }
  207 +
  208 + SrsStatisticClient* client = clients[id];
  209 +
  210 + // resample the kbps to collect the delta.
  211 + conn->resample();
  212 +
  213 + // add delta of connection to kbps.
  214 + // for next sample() of server kbps can get the stat.
  215 + kbps->add_delta(conn);
  216 + client->stream->kbps->add_delta(conn);
  217 + client->stream->vhost->kbps->add_delta(conn);
  218 +
  219 + // cleanup the delta.
  220 + conn->cleanup();
  221 +}
  222 +
  223 +SrsKbps* SrsStatistic::kbps_sample()
  224 +{
  225 + kbps->sample();
  226 + if (true) {
  227 + std::map<std::string, SrsStatisticVhost*>::iterator it;
  228 + for (it = vhosts.begin(); it != vhosts.end(); it++) {
  229 + SrsStatisticVhost* vhost = it->second;
  230 + vhost->kbps->sample();
  231 + }
  232 + }
  233 + if (true) {
  234 + std::map<std::string, SrsStatisticStream*>::iterator it;
  235 + for (it = streams.begin(); it != streams.end(); it++) {
  236 + SrsStatisticStream* stream = it->second;
  237 + stream->kbps->sample();
  238 + }
  239 + }
  240 +
  241 + return kbps;
  242 +}
  243 +
186 int64_t SrsStatistic::server_id() 244 int64_t SrsStatistic::server_id()
187 { 245 {
188 return _server_id; 246 return _server_id;
@@ -202,7 +260,9 @@ int SrsStatistic::dumps_vhosts(stringstream& ss) @@ -202,7 +260,9 @@ int SrsStatistic::dumps_vhosts(stringstream& ss)
202 260
203 ss << __SRS_JOBJECT_START 261 ss << __SRS_JOBJECT_START
204 << __SRS_JFIELD_ORG("id", vhost->id) << __SRS_JFIELD_CONT 262 << __SRS_JFIELD_ORG("id", vhost->id) << __SRS_JFIELD_CONT
205 - << __SRS_JFIELD_STR("name", vhost->vhost) 263 + << __SRS_JFIELD_STR("name", vhost->vhost) << __SRS_JFIELD_CONT
  264 + << __SRS_JFIELD_ORG("send_bytes", vhost->kbps->get_send_bytes()) << __SRS_JFIELD_CONT
  265 + << __SRS_JFIELD_ORG("recv_bytes", vhost->kbps->get_recv_bytes())
206 << __SRS_JOBJECT_END; 266 << __SRS_JOBJECT_END;
207 } 267 }
208 ss << __SRS_JARRAY_END; 268 ss << __SRS_JARRAY_END;
@@ -235,7 +295,9 @@ int SrsStatistic::dumps_streams(stringstream& ss) @@ -235,7 +295,9 @@ int SrsStatistic::dumps_streams(stringstream& ss)
235 << __SRS_JFIELD_ORG("id", stream->id) << __SRS_JFIELD_CONT 295 << __SRS_JFIELD_ORG("id", stream->id) << __SRS_JFIELD_CONT
236 << __SRS_JFIELD_STR("name", stream->stream) << __SRS_JFIELD_CONT 296 << __SRS_JFIELD_STR("name", stream->stream) << __SRS_JFIELD_CONT
237 << __SRS_JFIELD_ORG("vhost", stream->vhost->id) << __SRS_JFIELD_CONT 297 << __SRS_JFIELD_ORG("vhost", stream->vhost->id) << __SRS_JFIELD_CONT
238 - << __SRS_JFIELD_ORG("clients", client_num) << __SRS_JFIELD_CONT; 298 + << __SRS_JFIELD_ORG("clients", client_num) << __SRS_JFIELD_CONT
  299 + << __SRS_JFIELD_ORG("send_bytes", stream->kbps->get_send_bytes()) << __SRS_JFIELD_CONT
  300 + << __SRS_JFIELD_ORG("recv_bytes", stream->kbps->get_recv_bytes()) << __SRS_JFIELD_CONT;
239 301
240 if (!stream->has_video) { 302 if (!stream->has_video) {
241 ss << __SRS_JFIELD_NULL("video") << __SRS_JFIELD_CONT; 303 ss << __SRS_JFIELD_NULL("video") << __SRS_JFIELD_CONT;
@@ -35,7 +35,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -35,7 +35,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 35
36 #include <srs_kernel_codec.hpp> 36 #include <srs_kernel_codec.hpp>
37 37
  38 +class SrsKbps;
38 class SrsRequest; 39 class SrsRequest;
  40 +class SrsConnection;
39 41
40 struct SrsStatisticVhost 42 struct SrsStatisticVhost
41 { 43 {
@@ -43,6 +45,11 @@ public: @@ -43,6 +45,11 @@ public:
43 int64_t id; 45 int64_t id;
44 std::string vhost; 46 std::string vhost;
45 public: 47 public:
  48 + /**
  49 + * vhost total kbps.
  50 + */
  51 + SrsKbps* kbps;
  52 +public:
46 SrsStatisticVhost(); 53 SrsStatisticVhost();
47 virtual ~SrsStatisticVhost(); 54 virtual ~SrsStatisticVhost();
48 }; 55 };
@@ -56,6 +63,11 @@ public: @@ -56,6 +63,11 @@ public:
56 std::string stream; 63 std::string stream;
57 std::string url; 64 std::string url;
58 public: 65 public:
  66 + /**
  67 + * stream total kbps.
  68 + */
  69 + SrsKbps* kbps;
  70 +public:
59 bool has_video; 71 bool has_video;
60 SrsCodecVideo vcodec; 72 SrsCodecVideo vcodec;
61 // profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45. 73 // profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
@@ -103,6 +115,8 @@ private: @@ -103,6 +115,8 @@ private:
103 std::map<std::string, SrsStatisticStream*> streams; 115 std::map<std::string, SrsStatisticStream*> streams;
104 // key: client id, value: stream object. 116 // key: client id, value: stream object.
105 std::map<int, SrsStatisticClient*> clients; 117 std::map<int, SrsStatisticClient*> clients;
  118 + // server total kbps.
  119 + SrsKbps* kbps;
106 private: 120 private:
107 SrsStatistic(); 121 SrsStatistic();
108 virtual ~SrsStatistic(); 122 virtual ~SrsStatistic();
@@ -137,6 +151,16 @@ public: @@ -137,6 +151,16 @@ public:
137 * client disconnect 151 * client disconnect
138 */ 152 */
139 virtual void on_disconnect(int id); 153 virtual void on_disconnect(int id);
  154 + /**
  155 + * sample the kbps, add delta bytes of conn.
  156 + * use kbps_sample() to get all result of kbps stat.
  157 + */
  158 + virtual void kbps_add_delta(SrsConnection* conn);
  159 + /**
  160 + * calc the result for all kbps.
  161 + * @return the server kbps.
  162 + */
  163 + virtual SrsKbps* kbps_sample();
140 public: 164 public:
141 /** 165 /**
142 * get the server id, used to identify the server. 166 * get the server id, used to identify the server.
@@ -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 2 32 #define VERSION_MAJOR 2
33 #define VERSION_MINOR 0 33 #define VERSION_MINOR 0
34 -#define VERSION_REVISION 135 34 +#define VERSION_REVISION 136
35 35
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "SRS" 37 #define RTMP_SIG_SRS_KEY "SRS"
@@ -39,7 +39,7 @@ string srs_codec_video2str(SrsCodecVideo codec) @@ -39,7 +39,7 @@ string srs_codec_video2str(SrsCodecVideo codec)
39 return "H264"; 39 return "H264";
40 case SrsCodecVideoOn2VP6: 40 case SrsCodecVideoOn2VP6:
41 case SrsCodecVideoOn2VP6WithAlphaChannel: 41 case SrsCodecVideoOn2VP6WithAlphaChannel:
42 - return "H264"; 42 + return "VP6";
43 case SrsCodecVideoReserved: 43 case SrsCodecVideoReserved:
44 case SrsCodecVideoReserved1: 44 case SrsCodecVideoReserved1:
45 case SrsCodecVideoReserved2: 45 case SrsCodecVideoReserved2: