Latency measure for every frame

1.pip install cffi

2.apply the following patch to deepstream_test_3.py

diff --git a/apps/deepstream-test3/deepstream_test_3.py b/apps/deepstream-test3/deepstream_test_3.py
index d81ec92..21d2f3b 100755
--- a/apps/deepstream-test3/deepstream_test_3.py
+++ b/apps/deepstream-test3/deepstream_test_3.py
@@ -36,6 +36,28 @@ from common.FPS import PERF_DATA
 
 import pyds
 
+from cffi import FFI
+
+ffi = FFI()
+
+clib = None
+
+ffi.cdef("""
+typedef struct
+{
+  uint32_t source_id;
+  uint32_t frame_num;
+  double comp_in_timestamp;
+  double latency;
+} NvDsFrameLatencyInfo;
+
+uint32_t nvds_measure_buffer_latency(void *buf, NvDsFrameLatencyInfo *latency_info);
+bool nvds_get_enable_latency_measurement();
+""")
+
+# Compile the C sources to produce the following .dll (or .so under *nix)
+clib = ffi.dlopen("/opt/nvidia/deepstream/deepstream/lib/libnvdsgst_meta.so")
+
 no_display = False
 silent = False
 file_loop = False
@@ -56,6 +78,27 @@ OSD_PROCESS_MODE= 0
 OSD_DISPLAY_TEXT= 1
 pgie_classes_str= ["Vehicle", "TwoWheeler", "Person","RoadSign"]
 
+batch_num = 0
+
+def osd_src_pad_buffer_probe(pad, info, u_data):
+    number_source = u_data
+    gst_buffer = info.get_buffer()
+    if not gst_buffer:
+        print("Unable to get GstBuffer ")
+        return
+    global batch_num
+    if clib.nvds_get_enable_latency_measurement:
+        print(f"************BATCH-NUM = {batch_num}**************")
+        c_gst_buf = ffi.cast("void *", hash(gst_buffer))
+        cNvDsFrameLatencyInfo = ffi.new(f"NvDsFrameLatencyInfo[{number_source}]")
+        sources = clib.nvds_measure_buffer_latency(c_gst_buf, cNvDsFrameLatencyInfo)
+        for i in range(sources):
+            print(f"Source id = {cNvDsFrameLatencyInfo[i].source_id} "
+                  f"Frame_num = {cNvDsFrameLatencyInfo[i].frame_num} "
+                  f"Frame latency = {cNvDsFrameLatencyInfo[i].latency} (ms) ")
+        batch_num += 1
+    return Gst.PadProbeReturn.OK
+
 # pgie_src_pad_buffer_probe  will extract metadata received on tiler sink pad
 # and update params for drawing rectangle, object information etc.
 def pgie_src_pad_buffer_probe(pad,info,u_data):
@@ -199,7 +242,7 @@ def create_source_bin(index,uri):
         return None
     return nbin
 
-def main(args, requested_pgie=None, config=None, disable_probe=False):
+def main(args, requested_pgie=None, config=None, disable_probe=True):
     global perf_data
     perf_data = PERF_DATA(len(args))
 
@@ -380,6 +423,12 @@ def main(args, requested_pgie=None, config=None, disable_probe=False):
             # perf callback function to print fps every 5 sec
             GLib.timeout_add(5000, perf_data.perf_print_callback)
 
+    osd_src_pad=nvosd.get_static_pad("src")
+    if not osd_src_pad:
+        sys.stderr.write(" Unable to get src pad \n")
+    else:
+        osd_src_pad.add_probe(Gst.PadProbeType.BUFFER, osd_src_pad_buffer_probe, number_sources)
+
     # List the sources
     print("Now playing...")
     for i, source in enumerate(args):

3.Execute the following command in the shell

export NVDS_ENABLE_COMPONENT_LATENCY_MEASUREMENT=1
export NVDS_ENABLE_LATENCY_MEASUREMENT=1

python3 deepstream_test_3.py --no-display -i rtsp://"yourtspuri0" uri1

You will see the following log

************BATCH-NUM = 504**************
Comp name = nvv4l2decoder1 in_system_timestamp = 1704882298795.132080 out_system_timestamp = 1704882298795.524902               component latency= 0.392822
Comp name = nvstreammux-Stream-muxer source_id = 1 pad_index = 1 frame_num = 504               in_system_timestamp = 1704882298795.561035 out_system_timestamp = 1704882298929.228027               component_latency = 133.666992
Comp name = nvv4l2decoder0 in_system_timestamp = 1704882298928.666016 out_system_timestamp = 1704882298929.083008               component latency= 0.416992
Comp name = nvstreammux-Stream-muxer source_id = 0 pad_index = 0 frame_num = 504               in_system_timestamp = 1704882298929.117920 out_system_timestamp = 1704882298929.229004               component_latency = 0.111084
Comp name = primary-inference in_system_timestamp = 1704882298929.272949 out_system_timestamp = 1704882298929.907959               component latency= 0.635010
Comp name = nvtiler in_system_timestamp = 1704882298930.197998 out_system_timestamp = 1704882298930.374023               component latency= 0.176025
Comp name = convertor in_system_timestamp = 1704882298930.561035 out_system_timestamp = 1704882298930.635010               component latency= 0.073975
Comp name = onscreendisplay in_system_timestamp = 1704882298930.710938 out_system_timestamp = 1704882298930.733887               component latency= 0.022949
Source id = 1 Frame_num = 504 Frame latency = 135.739013671875 (ms) 
Source id = 0 Frame_num = 504 Frame latency = 2.205078125 (ms)