From 1f2ef82b462f9a44d2bc2761a6eb433a03d2021f Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Mon, 20 Feb 2023 16:10:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81=E8=8E=B7?= =?UTF-8?q?=E5=8F=96gop=E5=A4=A7=E5=B0=8F=E4=B8=8E=E9=97=B4=E9=9A=94?= =?UTF-8?q?=E4=BF=A1=E6=81=AF:=20#1570?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getMediaList/getMediaInfo接口、on_media_changed hook新增支持字段如下: { "codec_id" : 0, "codec_id_name" : "H264", "codec_type" : 0, "fps" : 0.0, "frames" : 1119, #累计接收帧数,不包含sei/aud/sps/pps等不能解码的帧 "gop_interval_ms" : 1993, #gop间隔时间,单位毫秒 "gop_size" : 60, #gop大小,单位帧数 "height" : 556, "key_frames" : 21, #累计接收关键帧数 "ready" : true, "width" : 990 } --- server/WebApi.cpp | 10 +++++++++- src/Extension/Frame.h | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index dc50b2ed..887b1736 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -378,8 +378,16 @@ Value makeMediaSourceJson(MediaSource &media){ auto video_track = dynamic_pointer_cast(track); obj["width"] = video_track->getVideoWidth(); obj["height"] = video_track->getVideoHeight(); - obj["fps"] = round(video_track->getVideoFps()); obj["key_frames"] = video_track->getVideoKeyFrames(); + int gop_size = video_track->getVideoGopSize(); + int gop_interval_ms = video_track->getVideoGopInterval(); + float fps = video_track->getVideoFps(); + if (fps <= 1) { + fps = gop_size * 1000.0 / gop_interval_ms; + } + obj["fps"] = round(fps); + obj["gop_size"] = gop_size; + obj["gop_interval_ms"] = gop_interval_ms; break; } default: diff --git a/src/Extension/Frame.h b/src/Extension/Frame.h index 2ba0c94e..4328e544 100644 --- a/src/Extension/Frame.h +++ b/src/Extension/Frame.h @@ -15,6 +15,7 @@ #include #include #include "Util/List.h" +#include "Util/TimeTicker.h" #include "Network/Buffer.h" namespace mediakit { @@ -311,10 +312,7 @@ public: */ bool inputFrame(const Frame::Ptr &frame) override { std::lock_guard lck(_mtx); - ++_frames; - if (frame->keyFrame() && frame->getTrackType() == TrackVideo) { - ++_video_key_frames; - } + doStatistics(frame); bool ret = false; for (auto &pr : _delegates) { if (pr.second->inputFrame(frame)) { @@ -353,7 +351,37 @@ public: return _frames; } + size_t getVideoGopSize() const { + std::lock_guard lck(_mtx); + return _gop_size; + } + + size_t getVideoGopInterval() const { + std::lock_guard lck(_mtx); + return _gop_interval_ms; + } + private: + void doStatistics(const Frame::Ptr &frame) { + if (!frame->configFrame() && !frame->dropAble()) { + // 忽略配置帧与可丢弃的帧 + ++_frames; + if (frame->keyFrame() && frame->getTrackType() == TrackVideo) { + // 遇视频关键帧时统计 + ++_video_key_frames; + _gop_size = _frames - _last_frames; + _gop_interval_ms = _ticker.elapsedTime(); + _last_frames = _frames; + _ticker.resetTime(); + } + } + } + +private: + toolkit::Ticker _ticker; + size_t _gop_interval_ms = 0; + size_t _gop_size = 0; + uint64_t _last_frames = 0; uint64_t _frames = 0; uint64_t _video_key_frames = 0; mutable std::recursive_mutex _mtx;