From a6efa389f0776e2d0c759c8450e81a4d3d1826c3 Mon Sep 17 00:00:00 2001 From: Barzan Hayati Date: Tue, 1 Jul 2025 23:41:24 +0000 Subject: [PATCH] Add fps osd sink --- src/pipeline_manager.cpp | 34 ++++++++++++++++++++++++++++++++++ src/pipeline_manager.hpp | 4 ++++ 2 files changed, 38 insertions(+) diff --git a/src/pipeline_manager.cpp b/src/pipeline_manager.cpp index 6049b8f..0cd09a8 100644 --- a/src/pipeline_manager.cpp +++ b/src/pipeline_manager.cpp @@ -48,6 +48,29 @@ char* createName(const char* str, int num) { return result; } +GstPadProbeReturn PipelineManager::osd_sink_pad_buffer_probe( + GstPad* pad, GstPadProbeInfo* info, gpointer user_data) { + (void)pad; // This explicitly marks it as unused + (void)user_data; // This explicitly marks it as unused + static guint64 frame_count_osd_sink = 0; + static auto last_time_osd_sink = std::chrono::steady_clock::now(); + GstBuffer* buf = (GstBuffer*)info->data; + NvDsBatchMeta* batch_meta = gst_buffer_get_nvds_batch_meta(buf); + + frame_count_osd_sink += batch_meta->num_frames_in_batch; + + if (frame_count_osd_sink % 60 == 0) { + auto now = std::chrono::steady_clock::now(); + double ms = std::chrono::duration_cast( + now - last_time_osd_sink) + .count(); + double fps = 60000.0 / ms; + g_print("FPS_osd_sink: %.2f\n", fps); + last_time_osd_sink = now; + } + return GST_PAD_PROBE_OK; +} + GstPadProbeReturn PipelineManager::fps_probe(GstPad* pad, GstPadProbeInfo* info, gpointer user_data) { (void)pad; // This explicitly marks it as unused @@ -81,6 +104,16 @@ void PipelineManager::get_fps() { gst_object_unref(element); } +void PipelineManager::get_fps_osd() { + GstElement* osd = gst_bin_get_by_name( + GST_BIN(pipeline), "nv-onscreendisplay"); // Or "nvinfer", etc. + GstPad* sink_pad = gst_element_get_static_pad(osd, "sink"); + gst_pad_add_probe(sink_pad, GST_PAD_PROBE_TYPE_BUFFER, + osd_sink_pad_buffer_probe, NULL, NULL); + gst_object_unref(sink_pad); + gst_object_unref(osd); +} + void PipelineManager::playing_pipeline(int num_sources, char** url_camera) { /* Set the pipeline to "playing" state */ @@ -279,6 +312,7 @@ bool PipelineManager::create_pipeline_elements(int num_sources, gst_object_unref(sink_pad); get_fps(); + get_fps_osd(); playing_pipeline(num_sources, url_camera); check_playing_pipeline(); diff --git a/src/pipeline_manager.hpp b/src/pipeline_manager.hpp index 7041a41..3e51c28 100644 --- a/src/pipeline_manager.hpp +++ b/src/pipeline_manager.hpp @@ -54,7 +54,11 @@ class PipelineManager { static gboolean event_thread_func(gpointer); static gboolean check_pipeline_state(gpointer); static GstPadProbeReturn fps_probe(GstPad *, GstPadProbeInfo *, gpointer); + static GstPadProbeReturn osd_sink_pad_buffer_probe(GstPad *, + GstPadProbeInfo *, + gpointer); void get_fps(); + void get_fps_osd(); void check_playing_pipeline(); ~PipelineManager(); }; \ No newline at end of file