Navigation Menu

Skip to content

Commit

Permalink
Get UTC Time (NTP Time) from Sender Report
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerHardiman committed Oct 7, 2018
1 parent 851fac5 commit 68ea87e
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions RtspClientExample/RTSPClient.cs
Expand Up @@ -261,9 +261,9 @@ public void Rtp_DataReceived(object sender, Rtsp.RtspChunkEventArgs e)
int rtcp_padding = (e.Message.Data[packetIndex+0] >> 5) & 0x01;
int rtcp_reception_report_count = (e.Message.Data[packetIndex+0] & 0x1F);
byte rtcp_packet_type = e.Message.Data[packetIndex+1]; // Values from 200 to 207
uint rtcp_length = ((uint)e.Message.Data[packetIndex+2] << 8) + (uint)(e.Message.Data[packetIndex+3]); // number of 32 bit words
uint rtcp_ssrc = ((uint)e.Message.Data[packetIndex+4] << 24) + (uint)(e.Message.Data[packetIndex+5] << 16)
+ (uint)(e.Message.Data[packetIndex+6] << 8) + (uint)(e.Message.Data[packetIndex+7]);
uint rtcp_length = (uint)(e.Message.Data[packetIndex+2] << 8) + (uint)(e.Message.Data[packetIndex+3]); // number of 32 bit words
uint rtcp_ssrc = (uint)(e.Message.Data[packetIndex+4] << 24) + (uint)(e.Message.Data[packetIndex+5] << 16)
+ (uint)(e.Message.Data[packetIndex+6] << 8) + (uint)(e.Message.Data[packetIndex+7]);

// 200 = SR = Sender Report
// 201 = RR = Receiver Report
Expand All @@ -276,6 +276,28 @@ public void Rtp_DataReceived(object sender, Rtsp.RtspChunkEventArgs e)
+ " SSRC=" + rtcp_ssrc);

if (rtcp_packet_type == 200) {
// We have received a Sender Report
// Use it to convert the RTP timestamp into the UTC time

UInt32 ntp_msw_seconds = (uint)(e.Message.Data[packetIndex + 8] << 24) + (uint)(e.Message.Data[packetIndex + 9] << 16)
+ (uint)(e.Message.Data[packetIndex + 10] << 8) + (uint)(e.Message.Data[packetIndex + 11]);

UInt32 ntp_lsw_fractions = (uint)(e.Message.Data[packetIndex + 12] << 24) + (uint)(e.Message.Data[packetIndex + 13] << 16)
+ (uint)(e.Message.Data[packetIndex + 14] << 8) + (uint)(e.Message.Data[packetIndex + 15]);

UInt32 rtp_timestamp = (uint)(e.Message.Data[packetIndex + 16] << 24) + (uint)(e.Message.Data[packetIndex + 17] << 16)
+ (uint)(e.Message.Data[packetIndex + 18] << 8) + (uint)(e.Message.Data[packetIndex + 19]);

double ntp = ntp_msw_seconds + (ntp_lsw_fractions / UInt32.MaxValue);

// NTP Most Signigicant Word is relative to 0h, 1 Jan 1900
// This will wrap around in 2036
DateTime time = new DateTime(1900,1,1,0,0,0,DateTimeKind.Utc);

time = time.AddSeconds((double)ntp_msw_seconds); // adds 'double' (whole&fraction)

_logger.Debug("RTCP time (UTC) for RTP timestamp " + rtp_timestamp + " is " + time);

// Send a Receiver Report
try
{
Expand Down

0 comments on commit 68ea87e

Please sign in to comment.