新增支持获取gop大小与间隔信息: #1570

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
}
This commit is contained in:
ziyue 2023-02-20 16:10:22 +08:00
parent 2f8dab66c5
commit 1f2ef82b46
2 changed files with 41 additions and 5 deletions

View File

@ -378,8 +378,16 @@ Value makeMediaSourceJson(MediaSource &media){
auto video_track = dynamic_pointer_cast<VideoTrack>(track); auto video_track = dynamic_pointer_cast<VideoTrack>(track);
obj["width"] = video_track->getVideoWidth(); obj["width"] = video_track->getVideoWidth();
obj["height"] = video_track->getVideoHeight(); obj["height"] = video_track->getVideoHeight();
obj["fps"] = round(video_track->getVideoFps());
obj["key_frames"] = video_track->getVideoKeyFrames(); 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; break;
} }
default: default:

View File

@ -15,6 +15,7 @@
#include <mutex> #include <mutex>
#include <functional> #include <functional>
#include "Util/List.h" #include "Util/List.h"
#include "Util/TimeTicker.h"
#include "Network/Buffer.h" #include "Network/Buffer.h"
namespace mediakit { namespace mediakit {
@ -311,10 +312,7 @@ public:
*/ */
bool inputFrame(const Frame::Ptr &frame) override { bool inputFrame(const Frame::Ptr &frame) override {
std::lock_guard<std::recursive_mutex> lck(_mtx); std::lock_guard<std::recursive_mutex> lck(_mtx);
++_frames; doStatistics(frame);
if (frame->keyFrame() && frame->getTrackType() == TrackVideo) {
++_video_key_frames;
}
bool ret = false; bool ret = false;
for (auto &pr : _delegates) { for (auto &pr : _delegates) {
if (pr.second->inputFrame(frame)) { if (pr.second->inputFrame(frame)) {
@ -353,7 +351,37 @@ public:
return _frames; return _frames;
} }
size_t getVideoGopSize() const {
std::lock_guard<std::recursive_mutex> lck(_mtx);
return _gop_size;
}
size_t getVideoGopInterval() const {
std::lock_guard<std::recursive_mutex> lck(_mtx);
return _gop_interval_ms;
}
private: 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 _frames = 0;
uint64_t _video_key_frames = 0; uint64_t _video_key_frames = 0;
mutable std::recursive_mutex _mtx; mutable std::recursive_mutex _mtx;