mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-04 01:37:33 +08:00
添加网速统计功能
This commit is contained in:
@@ -137,6 +137,52 @@ public:
|
||||
string _param_strs;
|
||||
};
|
||||
|
||||
class BytesSpeed {
|
||||
public:
|
||||
BytesSpeed() = default;
|
||||
~BytesSpeed() = default;
|
||||
|
||||
/**
|
||||
* 添加统计字节
|
||||
*/
|
||||
BytesSpeed& operator += (uint64_t bytes) {
|
||||
_bytes += bytes;
|
||||
if (_bytes > 1024 * 1024) {
|
||||
//数据大于1MB就计算一次网速
|
||||
computeSpeed();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取速度,单位bytes/s
|
||||
*/
|
||||
int getSpeed() {
|
||||
if (_ticker.elapsedTime() < 1000) {
|
||||
//获取频率小于1秒,那么返回上次计算结果
|
||||
return _speed;
|
||||
}
|
||||
return computeSpeed();
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t computeSpeed() {
|
||||
auto elapsed = _ticker.elapsedTime();
|
||||
if (!elapsed) {
|
||||
return _speed;
|
||||
}
|
||||
_speed = _bytes * 1000 / elapsed;
|
||||
_ticker.resetTime();
|
||||
_bytes = 0;
|
||||
return _speed;
|
||||
}
|
||||
|
||||
private:
|
||||
int _speed = 0;
|
||||
uint64_t _bytes = 0;
|
||||
Ticker _ticker;
|
||||
};
|
||||
|
||||
/**
|
||||
* 媒体源,任何rtsp/rtmp的直播流都源自该对象
|
||||
*/
|
||||
@@ -170,6 +216,9 @@ public:
|
||||
// 设置时间戳
|
||||
virtual void setTimeStamp(uint32_t stamp) {};
|
||||
|
||||
// 获取数据速率,单位bytes/s
|
||||
int getBytesSpeed();
|
||||
|
||||
////////////////MediaSourceEvent相关接口实现////////////////
|
||||
|
||||
// 设置监听者
|
||||
@@ -229,6 +278,9 @@ private:
|
||||
//触发媒体事件
|
||||
void emitEvent(bool regist);
|
||||
|
||||
protected:
|
||||
BytesSpeed _speed;
|
||||
|
||||
private:
|
||||
string _schema;
|
||||
string _vhost;
|
||||
|
||||
Reference in New Issue
Block a user