DeepStream SDK FAQ

38. [ALL_dGPU_APP] Supplement of NTP Timestamp in DeepStream

The following link explains how to use NTP timestamp in DeepStream.
Here are some additional information based on deepstream-test3.

https://docs.nvidia.com/metropolis/deepstream/dev-guide/text/DS_NTP_Timestamp.html#ntp-timestamp-in-deepstream

1.If you want to use rtcp’s ntp timestamp in nvstreammux, you need to set both attachment-sys-ts and frame-duration to 0.

2.After creating an rtspsrc element or an uridecodebin element, application must call configure_source_for_ntp_sync() function and pass the GstElement pointer to this API.

3.Access the ntp_timestamp member in framemeta. ntp_timestamp has been converted to UNIX time (nanoseconds since 1970.1.1)

Here is the complete patch for deepstream-test3

diff --git a/sources/apps/sample_apps/deepstream-test3/deepstream_test3_app.c b/sources/apps/sample_apps/deepstream-test3/deepstream_test3_app.c
index 4c2cf00..e9d46e4 100644
--- a/sources/apps/sample_apps/deepstream-test3/deepstream_test3_app.c
+++ b/sources/apps/sample_apps/deepstream-test3/deepstream_test3_app.c
@@ -17,6 +17,7 @@
 #include <string.h>
 #include <sys/time.h>
 #include <cuda_runtime_api.h>
+#include <inttypes.h>
 
 #include "gstnvdsmeta.h"
 #include "nvds_yml_parser.h"
@@ -101,6 +102,13 @@ tiler_src_pad_buffer_probe (GstPad * pad, GstPadProbeInfo * info,
                 num_rects++;
             }
         }
+
+        // GstClockTime
+        struct timeval tv;
+        gettimeofday(&tv, NULL);
+        uint64_t clock = tv.tv_sec * 1000 + tv.tv_usec / 1000;
+        uint64_t delta = clock - frame_meta->ntp_timestamp / 1000000;
+        printf("clock = %" PRIu64 " ntp = %" PRIu64 " delta: %" PRIu64 "ms\n", clock, frame_meta->ntp_timestamp, delta);
           g_print ("Frame Number = %d Number of objects = %d "
             "Vehicle Count = %d Person Count = %d\n",
             frame_meta->frame_num, num_rects, vehicle_count, person_count);
@@ -255,6 +263,7 @@ create_source_bin (guint index, gchar * uri)
     g_object_set (G_OBJECT (uri_decode_bin), "cudadec-memtype", 0, NULL);
   } else {
     uri_decode_bin = gst_element_factory_make ("uridecodebin", "uri-decode-bin");
+    configure_source_for_ntp_sync(uri_decode_bin);
   }
 
   if (!bin || !uri_decode_bin) {
@@ -445,7 +454,8 @@ main (int argc, char *argv[])
 #ifdef __aarch64__
       sink = gst_element_factory_make ("nv3dsink", "nvvideo-renderer");
 #else
-      sink = gst_element_factory_make ("nveglglessink", "nvvideo-renderer");
+      //sink = gst_element_factory_make ("nveglglessink", "nvvideo-renderer");
+      sink = gst_element_factory_make ("fakesink", "nvvideo-renderer");
 #endif
     }
   }
@@ -532,6 +542,10 @@ main (int argc, char *argv[])
       }
   }
 
+  g_object_set (G_OBJECT (streammux), "live-source", 1, NULL);
+  g_object_set (G_OBJECT (streammux), "frame-duration", 0, NULL);
+  g_object_set (G_OBJECT (streammux), "attach-sys-ts", 0, NULL);
+
   /* we add a message handler */
   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
   bus_watch_id = gst_bus_add_watch (bus, bus_call, loop);

Generally speaking, the following results can be obtained

TP: 2024-06-11T12:16:34.079Z(1718108194079700138). NTP diff:33364781. ntp_time_epoch_ns = 2024-06-11T12:16:32.977Z(1718108192977646331) ntp_frame_timestamp = 0:00:05.472326680(5472326680) System Time: 2024-06-11T12:16:34.192Z
clock = 1718108194193 ntp = 1718108194079700138 delta: 114ms
0:00:15.579209610 32314 0x7f39f4001f40 LOG          nvstreammux_ntp gstnvstreammux_ntp.cpp:259:gst_nvds_ntp_calculator_get_buffer_ntp:<stream-muxer> Frame NTP calculated. mode: RTCP SR. source 0: PTS:0:00:06.607745284(6607745284) NTP: 2024-06-11T12:16:34.113Z(1718108194113064935). NTP diff:33364797. ntp_time_epoch_ns = 2024-06-11T12:16:32.977Z(1718108192977646331) ntp_frame_timestamp = 0:00:05.472326680(5472326680) System Time: 2024-06-11T12:16:34.223Z
clock = 1718108194224 ntp = 1718108194113064935 delta: 111ms
0:00:15.613469316 32314 0x7f39f4001f40 LOG          nvstreammux_ntp gstnvstreammux_ntp.cpp:259:gst_nvds_ntp_calculator_get_buffer_ntp:<stream-muxer> Frame NTP calculated. mode: RTCP SR. source 0: PTS:0:00:06.641107884(6641107884) NTP: 2024-06-11T12:16:34.146Z(1718108194146427535). NTP diff:33362600. ntp_time_epoch_ns = 2024-06-11T12:16:32.977Z(1718108192977646331) ntp_frame_timestamp = 0:00:05.472326680(5472326680) System Time: 2024-06-11T12:16:34.257Z
clock = 1718108194258 ntp = 1718108194146427535 delta: 112ms
0:00:15.651864745 32314 0x7f39f4001f40 LOG          nvstreammux_ntp gstnvstreammux_ntp.cpp:259:gst_nvds_ntp_calculator_get_buffer_ntp:<stream-muxer> Frame NTP calculated. mode: RTCP SR. source 0: PTS:0:00:06.674470515(6674470515) NTP: 2024-06-11T12:16:34.179Z(1718108194179790166). NTP diff:33362631. ntp_time_epoch_ns = 2024-06-11T12:16:32.977Z(1718108192977646331) ntp_frame_timestamp = 0:00:05.472326680(5472326680) System Time: 2024-06-11T12:16:34.296Z
clock = 1718108194297 ntp = 1718108194179790166 delta: 118ms
0:00:15.680095551 32314 0x7f39f4001f40 LOG          nvstreammux_ntp gstnvstreammux_ntp.cpp:259:gst_nvds_ntp_calculator_get_buffer_ntp:<stream-muxer> Frame NTP calculated. mode: RTCP SR. source 0: PTS:0:00:06.707833179(6707833179) NTP: 2024-06-11T12:16:34.213Z(1718108194213152830). NTP diff:33362664. ntp_time_epoch_ns = 2024-06-11T12:16:32.977Z(1718108192977646331) ntp_frame_timestamp = 0:00:05.472326680(5472326680) System Time: 2024-06-11T12:16:34.324Z
clock = 1718108194325 ntp = 1718108194213152830 delta: 112ms
0:00:15.713640264 32314 0x7f39f4001f40 LOG          nvstreammux_ntp gstnvstreammux_ntp.cpp:259:gst_nvds_ntp_calculator_get_buffer_ntp:<stream-muxer> Frame NTP calculated. mode: RTCP SR. source 0: PTS:0:00:06.741195875(6741195875) NTP: 2024-06-11T12:16:34.246Z(1718108194246515526). NTP diff:33362696. ntp_time_epoch_ns = 2024-06-11T12:16:32.977Z(1718108192977646331) ntp_frame_timestamp = 0:00:05.472326680(5472326680) System Time: 2024-06-11T12:16:34.357Z
clock = 1718108194358 ntp = 1718108194246515526 delta: 112ms
0:00:15.755680945 32314 0x7f39f4001f40 LOG          nvstreammux_ntp gstnvstreammux_ntp.cpp:259:gst_nvds_ntp_calculator_get_buffer_ntp:<stream-muxer> Frame NTP calculated. mode: RTCP SR. source 0: PTS:0:00:06.774558602(6774558602) NTP: 2024-06-11T12:16:34.279Z(1718108194279878253). NTP diff:33362727. ntp_time_epoch_ns = 2024-06-11T12:16:32.977Z(1718108192977646331) ntp_frame_timestamp = 0:00:05.472326680(5472326680) System Time: 2024-06-11T12:16:34.399Z
clock = 1718108194400 ntp = 1718108194279878253 delta: 121ms
0:00:15.799888705 32314 0x7f39f4001f40 LOG          nvstreammux_ntp gstnvstreammux_ntp.cpp:259:gst_nvds_ntp_calculator_get_buffer_ntp:<stream-muxer> Frame NTP calculated. mode: RTCP SR. source 0: PTS:0:00:06.807921362(6807921362) NTP: 2024-06-11T12:16:34.313Z(1718108194313241013). NTP diff:33362760. ntp_time_epoch_ns = 2024-06-11T12:16:32.977Z(1718108192977646331) ntp_frame_timestamp = 0:00:05.472326680(5472326680) System Time: 2024-06-11T12:16:34.444Z
clock = 1718108194445 ntp = 1718108194313241013 delta: 132ms
0:00:15.819295893 32314 0x7f39f4001f40 LOG          nvstreammux_ntp gstnvstreammux_ntp.cpp:259:gst_nvds_ntp_calculator_get_buffer_ntp:<stream-muxer> Frame NTP calculated. mode: RTCP SR. source 0: PTS:0:00:06.841284153(6841284153) NTP: 2024-06-11T12:16:34.346Z(1718108194346603804). NTP diff:33362791. ntp_time_epoch_ns = 2024-06-11T12:16:32.977Z(1718108192977646331) ntp_frame_timestamp = 0:00:05.472326680(5472326680) System Time: 2024-06-11T12:16:34.463Z
clock = 1718108194464 ntp = 1718108194346603804 delta: 118ms