Need help getting timestamp from IMX390 camera using nvarguscamerasrc

Hello,
I downloaded the source code of nvarguscamerasrc and I would like to get the timestamp of each frame and add it as a buffer metadata to the GstBuffer.
I understand that I need to use getSensorTimestamp() but I don’t really know where to start and where I should use this function.

Could you help me achieve this ?

  • Get the timestamp using getSensorTimestamp()
  • Adding the sensor to the GstBuffer

Thanks a lot !

please refer to Topic 159220 for checking capture timestamp, Argus Timestamp, SensorTimestampTsc,

Thanks for the answer.
Is there a working example of nvarguscamerasrc where getSensorTimestamp()is used ?

If not, could you give an example ?

Thanks

hello user27558,

you may fetch the sensor timestamp via metadata,
here shows the brief example,

                const IEventCaptureComplete *ieventCaptureComplete
                         = interface_cast<const IEventCaptureComplete>(event);
                const CaptureMetadata *metaData = ieventCaptureComplete->getMetadata();

                if (metaData)
                {
                    const ICaptureMetadata *iCaptureMeta =
                        interface_cast<const ICaptureMetadata>(metaData);

                    printf("timestamp: %lld\n", iCaptureMeta->getSensorTimestamp());
                 }

Thanks for the help,
I believe that this snippet should be used in the bool StreamConsumer::threadExecute(GstNvArgusCameraSrc *src) function. Is that right ?

hello user27558,

that’s correct,
actually, you may refer to Argus/public/samples/yuvJpeg/main.cpp for printing out some capture metadata from the frame.

@JerryChang

Thanks for the help,
When I try to get the ICaptureMetadata interface it fails :

Error generated. gstnvarguscamerasrc.cpp, threadExecute:631 Failed to get ICaptureMetadata interface.
Error generated. gstnvarguscamerasrc.cpp, threadFunction:247 (propagating)

And here’s the snippet of code I am using right now :
(It comes from the yuvJpeg sample)

      // --------
      // Print out some capture metadata from the frame.
      IArgusCaptureMetadata *iArgusCaptureMetadata = interface_cast<IArgusCaptureMetadata>(frame);
      if (!iArgusCaptureMetadata)
        ORIGINATE_ERROR("Failed to get IArgusCaptureMetadata interface.");

      CaptureMetadata *metadata = iArgusCaptureMetadata->getMetadata();
      ICaptureMetadata *iMetadata = interface_cast<ICaptureMetadata>(metadata);
      if (!iMetadata)
        ORIGINATE_ERROR("Failed to get ICaptureMetadata interface.");
      // --------

Any idea what could cause the error ?

I am using IMX-390 cameras and jetpack 4.6.1

Thanks !

Answering my own question :

Metadata needs to be enabled in the stream settings, e.g. :

    IEGLOutputStreamSettings *iStreamSettings =
        interface_cast<IEGLOutputStreamSettings>(streamSettings);
    if (iStreamSettings)
    {
        iStreamSettings->setPixelFormat(PIXEL_FMT_YCbCr_420_888);
        iStreamSettings->setResolution(iSensorMode->getResolution());
        iStreamSettings->setMode(EGL_STREAM_MODE_FIFO);
        iStreamSettings->setMetadataEnable(true);
    }

@JerryChang

What is the most reliable way to convert the timestamp given by getSensorTimestamp() to a unix timestamp ?

hello user27558,

iCaptureMetadata->getSensorTimestamp() return the kernel timestamp (i.e. MONOTONIC_RAW) of sensor start-of-frame.

Hello, @JerryChang

Do I need to use the following formula to get the unix timestamp ?

clock_gettime(MONOTONIC_RAW) = cycle_ns(TSC) - offset_ns

Also, what is cycle_ns(TSC) ?

hello user27558,

iCaptureMetadata->getSensorTimestamp() return the kernel timestamp.
TSC it’s the HW capture timestamp, VI drivers to capture frame and store the timestamp with TSC.
please refer to Topic 159220 for the explanation.
thanks

Thanks, @JerryChang for the answer, but I think I still need some help,

I use the following code to get the sensor timestamp :

      // Print out some capture metadata from the frame.
      IArgusCaptureMetadata *iArgusCaptureMetadata = interface_cast<IArgusCaptureMetadata>(frame);
      if (!iArgusCaptureMetadata) {
        ORIGINATE_ERROR("Failed to get IArgusCaptureMetadata interface.");
      }

      CaptureMetadata *metadata = iArgusCaptureMetadata->getMetadata();
      ICaptureMetadata *iMetadata = interface_cast<ICaptureMetadata>(metadata);
      if (!iMetadata) {
        ORIGINATE_ERROR("Failed to get ICaptureMetadata interface.");
      }
      unsigned long sensorTimestamp = static_cast<unsigned long>(iMetadata->getSensorTimestamp());

      CONSUMER_PRINT("\tsensorTimestamp : %lu\n",
                     sensorTimestamp
      );

And it gives me this value : 5502998961000 (in ns)

Which according to the documentation is :

Returns the kernel (SOF) timestamp for the sensor (in nanoseconds).
This is the time that the first data from this capture arrives from the sensor.

I don’t understand what cycle_ns(TSC), or offset_ns has to do with any of that. Could you give a detailed explanation ? Because the link you sent doesn’t explain that.

hello user27558,

please convert the units from nanosecond to seconds, use the same unit as kernel. for example, please check the timestamp in $ dmesg.
TSC is hardware timestamp, that’s captured by RCE. don’t bother that if you’re using Argus APIs.

Thanks for the answer @JerryChang

The sensor timestamp in seconds is : 8156.687446000001 which makes sense with the timestamp in dmesg.


I am using the Argus API. Do I need to calculate the adjusted SOF ? Like that ?

      struct timespec t;
      clock_gettime(CLOCK_MONOTONIC_RAW, &t);
      uint64_t monotonicRaw = (uint64_t) (t.tv_sec) * (uint64_t) 1000000000 + (uint64_t) (t.tv_nsec);
      uint64_t adjustedSof = monotonicRaw - offset_ns;

It would then allow me to calculate offset between the adjustedSof and the sensorTimestamp :

int64_t timestampOffset = static_cast<int64_t>(adjustedSof) - static_cast<int64_t>(sensorTimestamp);

Does it makes sense ?

The timestamp able transfer to kernel time by (getSensorTimestamp() - offset_ns)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.