RTP packets carry two distinct kinds of time information:Documentation Index
Fetch the complete documentation index at: https://mintlify.com/bluenviron/gortsplib/llms.txt
Use this file to discover all available pages before exploring further.
- PTS (presentation timestamp) — a relative timestamp derived from the RTP
Timestampfield divided by the codec clock rate. It tells you when to present a sample relative to the start of the stream, but carries no wall-clock information. - NTP timestamp — an absolute wall-clock time derived from RTCP Sender Report (SR) packets that the server sends alongside the RTP stream. It maps RTP timestamps onto real UTC time.
Extracting timestamps
PTS
Callc.PacketPTS(medi, pkt) inside any packet callback. It returns an int64 clock-tick value and a boolean indicating whether the value is currently available. The returned value is in the codec’s native clock units (e.g. 90000 ticks = 1 second for H.264 at 90 kHz). PTS is computed by gortsplib’s global decoder which accounts for RTP timestamp rollover and cross-track synchronisation.
PTS is always available once at least one packet has been received. You do not need the server to send RTCP SR packets to compute it.
NTP
Callc.PacketNTP(medi, pkt) to retrieve the wall-clock time associated with that RTP timestamp. It returns a time.Time and a boolean. The boolean is false until the server sends at least one RTCP Sender Report for that media — some servers never send SR packets, in which case NTP is never available.
Complete example
main.go
Converting PTS to time.Duration
PacketPTS returns an int64 in clock-tick units. To convert it to a time.Duration for display or container muxing:
int64 type accommodates negative timestamps and values larger than ~2.5 hours at 90 kHz without overflow. gortsplib also handles 32-bit RTP timestamp rollover (the counter wraps after ~13 hours at 90 kHz) internally.
Audio/video synchronisation
NTP timestamps are the recommended mechanism for synchronised multi-track recording:- Wait until
ntpAvailableistrueon both the audio and video medias. - Use the
time.Timevalues as the presentation wall-clock anchor for each sample. - Write samples to disk (e.g., a container muxer) using these absolute times to keep tracks aligned regardless of clock drift between audio and video encoders.
NTP encoding
Thegithub.com/bluenviron/gortsplib/v5/pkg/ntp package exposes the low-level NTP encode/decode functions used internally:
c.PacketNTP() returns a time.Time already decoded.