How to extract the timestamp or frame number from rtsp server

I have tried to extract with following code but its not matching with server timestamp please suggest if there are any particular elements to use for this.

import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject

def decode_h264_stream(rtsp_url):
    """Decodes the H.264 stream of an RTSP stream and extracts the timestamp.

    Args:
        rtsp_url: The URL of the RTSP stream.

    Returns:
        The timestamp in nanoseconds.
    """

    # Initialize GStreamer.
    Gst.init(None)

    # Create a GStreamer pipeline.
    pipeline = Gst.parse_launch(f"rtspsrc location={rtsp_url} ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! appsink name=sink emit-signals=True sync=False")

    # Get the appsink element from the pipeline.
    appsink = pipeline.get_by_name("sink")

    # Set properties to enable signal emission and asynchronous processing.
    appsink.set_property("emit-signals", True)
    appsink.set_property("sync", False)

    # Define a callback function to handle new samples.
    def on_new_sample(appsink):
        sample = appsink.emit("pull-sample")
        if sample:
            buffer = sample.get_buffer()
            timestamp = buffer.pts  # Get the timestamp from the buffer (in nanoseconds)
            print("Timestamp:", timestamp)

    # Connect the callback function to the "new-sample" signal of the appsink.
    appsink.connect("new-sample", on_new_sample)

    # Start the GStreamer pipeline.
    pipeline.set_state(Gst.State.PLAYING)

    # Create a GLib main loop and run it.
    loop = GObject.MainLoop()
    try:
        loop.run()
    except KeyboardInterrupt:
        pass

    # Clean up and stop the pipeline.
    pipeline.set_state(Gst.State.NULL)

    return

# RTSP URL of the server-side pipeline
rtsp_url = "rtsp://0.0.0.0:8554/hnvfahmo"

# Call the function to decode the H.264 stream and print the timestamps.
decode_h264_stream(rtsp_url)

What kind of “server timestamp” do you want to extract here?

The server’s “NTP” time as provided via RTCP that corresponds to a specific RTP packet? For that you could enable the add-reference-timestamp-meta property on rtspsrc and then you can grab these times from the GstReferenceTimestampMeta once the first RTCP SR from the server was received.

If you want some other kind of “server timestamp”, a different approach is needed.

on server side i am serving the data to rtsp server as below here i am setting the timestamp i want to extract this time stamp on client side so that both client and server time stamp will be same.

 data = frame.tostring()
            buf = Gst.Buffer.new_allocate(None, len(data), None)
            buf.fill(0, data)
            buf.duration = self.duration
            timestamp = self.number_frames * self.duration
            buf.pts = buf.dts = int(timestamp)
            buf.offset = timestamp
            self.number_frames += 1
            retval = src.emit("push-buffer", buf)
            print(
                "pushed buffer, frame {}, duration {} ns, durations {} s".format(
                    self.number_frames, self.duration, self.duration / Gst.SECOND
                )
            )
            if retval != Gst.FlowReturn.OK:
                print(retval)

That’s not sufficient and you need to configure more there. I described one approach in Instantaneous RTP synchronization & retrieval of absolute sender clock times with GStreamer – coaxion.net – slomo's blog

ok thank you so much for the info i will try this approach and update.