增加平均fps统计

This commit is contained in:
Alex 2023-11-17 11:12:51 +08:00
parent 88dc59ec68
commit a78ca2ea5b
5 changed files with 49 additions and 3 deletions

View File

@ -334,6 +334,7 @@ Value makeMediaSourceJson(MediaSource &media){
item["createStamp"] = (Json::UInt64) media.getCreateStamp(); item["createStamp"] = (Json::UInt64) media.getCreateStamp();
item["aliveSecond"] = (Json::UInt64) media.getAliveSecond(); item["aliveSecond"] = (Json::UInt64) media.getAliveSecond();
item["bytesSpeed"] = media.getBytesSpeed(); item["bytesSpeed"] = media.getBytesSpeed();
item["avgFps"] = media.getAvgFps();
item["readerCount"] = media.readerCount(); item["readerCount"] = media.readerCount();
item["totalReaderCount"] = media.totalReaderCount(); item["totalReaderCount"] = media.totalReaderCount();
item["originType"] = (int) media.getOriginType(); item["originType"] = (int) media.getOriginType();

View File

@ -159,6 +159,10 @@ int MediaSource::getBytesSpeed(TrackType type){
return _speed[type].getSpeed(); return _speed[type].getSpeed();
} }
int MediaSource::getAvgFps() {
return getMuxer()->getAvgFps();
}
uint64_t MediaSource::getAliveSecond() const { uint64_t MediaSource::getAliveSecond() const {
//使用Ticker对象获取存活时间的目的是防止修改系统时间导致回退 //使用Ticker对象获取存活时间的目的是防止修改系统时间导致回退
return _ticker.createdTime() / 1000; return _ticker.createdTime() / 1000;

View File

@ -299,6 +299,38 @@ public:
bool equalMediaTuple(const MediaTuple& a, const MediaTuple& b); bool equalMediaTuple(const MediaTuple& a, const MediaTuple& b);
class FrameFps {
public:
FrameFps() = default;
~FrameFps() = default;
void inputFrame() {
++frameCount;
}
int getDynamicFPS() {
if (_ticker.elapsedTime() < 1000) {
//获取频率小于1秒那么返回上次计算结果
return _fps;
}
return computeFps();
}
private:
int computeFps() {
auto elapsed = _ticker.elapsedTime();
if (!elapsed) {
return _fps;
}
_fps = (int)(frameCount * 1000 / elapsed);
_ticker.resetTime();
frameCount = 0;
return _fps;
}
private:
long frameCount = 0;
int _fps = 0;
toolkit::Ticker _ticker;
};
/** /**
* rtsp/rtmp的直播流都源自该对象 * rtsp/rtmp的直播流都源自该对象
*/ */
@ -340,7 +372,8 @@ public:
uint64_t getCreateStamp() const { return _create_stamp; } uint64_t getCreateStamp() const { return _create_stamp; }
// 获取流上线时间,单位秒 // 获取流上线时间,单位秒
uint64_t getAliveSecond() const; uint64_t getAliveSecond() const;
// 获取平均fps
int getAvgFps();
////////////////MediaSourceEvent相关接口实现//////////////// ////////////////MediaSourceEvent相关接口实现////////////////
// 设置监听者 // 设置监听者

View File

@ -539,6 +539,9 @@ bool MultiMediaSourceMuxer::onTrackFrame(const Frame::Ptr &frame_in) {
_ring->write(frame, !haveVideo()); _ring->write(frame, !haveVideo());
} }
} }
if(frame->getTrackType()==TrackVideo && !frame->dropAble()){
_dynamicFPS.inputFrame();
}
return ret; return ret;
} }
@ -564,4 +567,8 @@ bool MultiMediaSourceMuxer::isEnabled(){
return _is_enable; return _is_enable;
} }
int MultiMediaSourceMuxer::getAvgFps() {
return _dynamicFPS.getDynamicFPS();
}
}//namespace mediakit }//namespace mediakit

View File

@ -134,7 +134,8 @@ public:
const ProtocolOption &getOption() const; const ProtocolOption &getOption() const;
const MediaTuple &getMediaTuple() const; const MediaTuple &getMediaTuple() const;
std::string shortUrl() const; std::string shortUrl() const;
// 获取平均fps
int getAvgFps();
protected: protected:
/////////////////////////////////MediaSink override///////////////////////////////// /////////////////////////////////MediaSink override/////////////////////////////////
@ -178,7 +179,7 @@ private:
HlsFMP4Recorder::Ptr _hls_fmp4; HlsFMP4Recorder::Ptr _hls_fmp4;
toolkit::EventPoller::Ptr _poller; toolkit::EventPoller::Ptr _poller;
RingType::Ptr _ring; RingType::Ptr _ring;
FrameFps _dynamicFPS;
//对象个数统计 //对象个数统计
toolkit::ObjectStatistic<MultiMediaSourceMuxer> _statistic; toolkit::ObjectStatistic<MultiMediaSourceMuxer> _statistic;
}; };