From a78ca2ea5bf22bb996166f33c56db00abb7c714e Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 17 Nov 2023 11:12:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B9=B3=E5=9D=87fps?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 1 + src/Common/MediaSource.cpp | 4 ++++ src/Common/MediaSource.h | 35 +++++++++++++++++++++++++++- src/Common/MultiMediaSourceMuxer.cpp | 7 ++++++ src/Common/MultiMediaSourceMuxer.h | 5 ++-- 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 63b4d5e3..f7bc467c 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -334,6 +334,7 @@ Value makeMediaSourceJson(MediaSource &media){ item["createStamp"] = (Json::UInt64) media.getCreateStamp(); item["aliveSecond"] = (Json::UInt64) media.getAliveSecond(); item["bytesSpeed"] = media.getBytesSpeed(); + item["avgFps"] = media.getAvgFps(); item["readerCount"] = media.readerCount(); item["totalReaderCount"] = media.totalReaderCount(); item["originType"] = (int) media.getOriginType(); diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 1977b616..2c14b274 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -159,6 +159,10 @@ int MediaSource::getBytesSpeed(TrackType type){ return _speed[type].getSpeed(); } +int MediaSource::getAvgFps() { + return getMuxer()->getAvgFps(); +} + uint64_t MediaSource::getAliveSecond() const { //使用Ticker对象获取存活时间的目的是防止修改系统时间导致回退 return _ticker.createdTime() / 1000; diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 51991c35..07fc9159 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -299,6 +299,38 @@ public: 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的直播流都源自该对象 */ @@ -340,7 +372,8 @@ public: uint64_t getCreateStamp() const { return _create_stamp; } // 获取流上线时间,单位秒 uint64_t getAliveSecond() const; - + // 获取平均fps + int getAvgFps(); ////////////////MediaSourceEvent相关接口实现//////////////// // 设置监听者 diff --git a/src/Common/MultiMediaSourceMuxer.cpp b/src/Common/MultiMediaSourceMuxer.cpp index e38c6134..dbd3627d 100644 --- a/src/Common/MultiMediaSourceMuxer.cpp +++ b/src/Common/MultiMediaSourceMuxer.cpp @@ -539,6 +539,9 @@ bool MultiMediaSourceMuxer::onTrackFrame(const Frame::Ptr &frame_in) { _ring->write(frame, !haveVideo()); } } + if(frame->getTrackType()==TrackVideo && !frame->dropAble()){ + _dynamicFPS.inputFrame(); + } return ret; } @@ -564,4 +567,8 @@ bool MultiMediaSourceMuxer::isEnabled(){ return _is_enable; } +int MultiMediaSourceMuxer::getAvgFps() { + return _dynamicFPS.getDynamicFPS(); +} + }//namespace mediakit diff --git a/src/Common/MultiMediaSourceMuxer.h b/src/Common/MultiMediaSourceMuxer.h index 45ab7623..59615bd8 100644 --- a/src/Common/MultiMediaSourceMuxer.h +++ b/src/Common/MultiMediaSourceMuxer.h @@ -134,7 +134,8 @@ public: const ProtocolOption &getOption() const; const MediaTuple &getMediaTuple() const; std::string shortUrl() const; - + // 获取平均fps + int getAvgFps(); protected: /////////////////////////////////MediaSink override///////////////////////////////// @@ -178,7 +179,7 @@ private: HlsFMP4Recorder::Ptr _hls_fmp4; toolkit::EventPoller::Ptr _poller; RingType::Ptr _ring; - + FrameFps _dynamicFPS; //对象个数统计 toolkit::ObjectStatistic _statistic; };