RTP Timestamp Calculation

RTP timestamp is an important attribute in RTP header and is used plug the packet in right order for playback. Also it is used to synchronize audio video packets. Lets see how these RTP timestamps are calculated.

RTP timestamp calculation involves two parameters explained below.

  • Packetization time - Packetization time represents one RTP packet duration in milliseconds. For example, In G711 case, one RTP packet may represent 20 millisecond. Please note that other than 20ms packetization time is a valid case as well.

  • Sampling rate - Sampling rate is number of analog samples taken per second to convert to digital form. In a typical G711 case, sampling rate is 8kHz. So 8000 analog samples are taken per second to convert to digital form. Higher the sampling rate, better is the quality.
Audio RTP Timestamps

One can choose a random value for audio RTP timestamp. And for successive Audio RTP packets, timestamp should be incremented by sampling rate / packets per second. Lets consider a case where sampling rate is 8kHz and packetization time is 20ms.

One frame corresponds to 20ms
For 1 second, there will be 1000ms / 20ms = 50 frames

Audio RTP packet timestamp incremental value = 8kHz / 50 = 8000Hz / 50 = 160.

Video RTP Timestamps

Typically in video case there are 30 frames per second or 24 frames per second video. Lets consider a typical case, where sampling rate is 90kHz and fps is 30.

Then video RTP packet timestamp incremental value = 90kHz / 30 = 90,000Hz / 30 = 3000.
Hence each video RTP frame timestamp should be incremented by 3000.

In practice, one video frame may be sent as more than one RTP packet because of bigger size. Say one video frame you are sending as 3 RTP packets. For all these 3 RTP packets, you need to keep timestamp same. For next video frame you can increase RTP timestamp by 3000.

In certain cases, if you do not know fps, probably you need to go for system clock time and derive timestamp.