From 3476f2a268d8b406104bd71aca7dd4b5980a0670 Mon Sep 17 00:00:00 2001 From: Barzan Hayati Date: Tue, 1 Jul 2025 23:26:43 +0000 Subject: [PATCH] Add fps pad probe --- src/pipeline_manager.cpp | 35 ++++++++++++++++++++++++++++++++++- src/pipeline_manager.hpp | 2 ++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/pipeline_manager.cpp b/src/pipeline_manager.cpp index ea8faff..6049b8f 100644 --- a/src/pipeline_manager.cpp +++ b/src/pipeline_manager.cpp @@ -48,6 +48,39 @@ char* createName(const char* str, int num) { return result; } +GstPadProbeReturn PipelineManager::fps_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_fps_probe = 0; + static auto last_time_fps_probe = std::chrono::steady_clock::now(); + if (GST_PAD_PROBE_INFO_TYPE(info) & GST_PAD_PROBE_TYPE_BUFFER) { + frame_count_fps_probe++; + + if (frame_count_fps_probe % 30 == 0) { // Calculate FPS every 30 frames + auto current_time = std::chrono::steady_clock::now(); + auto duration = + std::chrono::duration_cast( + current_time - last_time_fps_probe) + .count(); + double fps = 30000.0 / duration; + g_print("fps_probe FPS: %.2f\n", fps); + last_time_fps_probe = current_time; + } + } + return GST_PAD_PROBE_OK; +} + +void PipelineManager::get_fps() { + // 2. Add pad probe to get FPS + GstElement* element = gst_bin_get_by_name( + GST_BIN(pipeline), "nvvideo-converter"); // or any processing element + GstPad* pad = gst_element_get_static_pad(element, "src"); + gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER, fps_probe, NULL, NULL); + gst_object_unref(pad); + gst_object_unref(element); +} + void PipelineManager::playing_pipeline(int num_sources, char** url_camera) { /* Set the pipeline to "playing" state */ @@ -59,7 +92,6 @@ void PipelineManager::playing_pipeline(int num_sources, char** url_camera) { GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, sink_manager->output_sink.c_str()); - gst_element_set_state(pipeline, GST_STATE_PLAYING); } @@ -246,6 +278,7 @@ bool PipelineManager::create_pipeline_elements(int num_sources, NULL); gst_object_unref(sink_pad); + get_fps(); playing_pipeline(num_sources, url_camera); check_playing_pipeline(); diff --git a/src/pipeline_manager.hpp b/src/pipeline_manager.hpp index 0085468..7041a41 100644 --- a/src/pipeline_manager.hpp +++ b/src/pipeline_manager.hpp @@ -53,6 +53,8 @@ class PipelineManager { gpointer); static gboolean event_thread_func(gpointer); static gboolean check_pipeline_state(gpointer); + static GstPadProbeReturn fps_probe(GstPad *, GstPadProbeInfo *, gpointer); + void get_fps(); void check_playing_pipeline(); ~PipelineManager(); }; \ No newline at end of file