1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef MEDIA_CAST_NET_RTCP_RECEIVER_RTCP_SESSION_H_
6 #define MEDIA_CAST_NET_RTCP_RECEIVER_RTCP_SESSION_H_
7 
8 #include "base/time/tick_clock.h"
9 #include "media/cast/common/clock_drift_smoother.h"
10 #include "media/cast/net/pacing/paced_sender.h"
11 #include "media/cast/net/rtcp/receiver_rtcp_event_subscriber.h"
12 #include "media/cast/net/rtcp/receiver_rtcp_session.h"
13 #include "media/cast/net/rtcp/rtcp_defines.h"
14 #include "media/cast/net/rtcp/rtcp_session.h"
15 #include "media/cast/net/rtcp/rtcp_utility.h"
16 
17 namespace media {
18 namespace cast {
19 
20 // The RTCP session on a receiver handles incoming RTCP SR packets and maintains
21 // the offset of local clock from the remote clock and lip sync info (RTP
22 // & NTP timestamps).
23 class ReceiverRtcpSession : public RtcpSession {
24  public:
25   ReceiverRtcpSession(const base::TickClock* clock,  // Not owned.
26                       uint32_t local_ssrc,
27                       uint32_t remote_ssrc);
28 
29   ~ReceiverRtcpSession() override;
30 
local_ssrc()31   uint32_t local_ssrc() const { return local_ssrc_; }
remote_ssrc()32   uint32_t remote_ssrc() const { return remote_ssrc_; }
33 
34   // Handle incoming RTCP packet.
35   // Returns false if it is not a RTCP packet or it is not directed to
36   // this session, e.g. SSRC doesn't match.
37   bool IncomingRtcpPacket(const uint8_t* data, size_t length) override;
38 
39   // If available, returns true and sets the output arguments to the latest
40   // lip-sync timestamps gleaned from the sender reports.  While the sender
41   // provides reference NTP times relative to its own wall clock, the
42   // |reference_time| returned here has been translated to the local
43   // CastEnvironment clock.
44   bool GetLatestLipSyncTimes(RtpTimeTicks* rtp_timestamp,
45                              base::TimeTicks* reference_time) const;
46 
last_report_truncated_ntp()47   uint32_t last_report_truncated_ntp() const {
48     return last_report_truncated_ntp_;
49   }
50 
time_last_report_received()51   base::TimeTicks time_last_report_received() const {
52     return time_last_report_received_;
53   }
54 
55  private:
56   // Received NTP timestamps from RTCP SR packets.
57   void OnReceivedNtp(uint32_t ntp_seconds, uint32_t ntp_fraction);
58 
59   // Received RTP and NTP timestamps from RTCP SR packets.
60   void OnReceivedLipSyncInfo(RtpTimeTicks rtp_timestamp,
61                              uint32_t ntp_seconds,
62                              uint32_t ntp_fraction);
63 
64   const base::TickClock* const clock_;  // Not owned.
65   const uint32_t local_ssrc_;
66   const uint32_t remote_ssrc_;
67 
68   // The truncated (i.e., 64-->32-bit) NTP timestamp provided in the last report
69   // from the remote peer, along with the local time at which the report was
70   // received.  These values are used for ping-pong'ing NTP timestamps between
71   // the peers so that they can estimate the network's round-trip time.
72   uint32_t last_report_truncated_ntp_;
73   base::TimeTicks time_last_report_received_;
74 
75   // Maintains a smoothed offset between the local clock and the remote clock.
76   // Calling this member's Current() method is only valid if
77   // |time_last_report_received_| has a valid value.
78   ClockDriftSmoother local_clock_ahead_by_;
79 
80   // Latest "lip sync" info from the sender.  The sender provides the RTP
81   // timestamp of some frame of its choosing and also a corresponding reference
82   // NTP timestamp sampled from a clock common to all media streams.  It is
83   // expected that the sender will update this data regularly and in a timely
84   // manner (e.g., about once per second).
85   RtpTimeTicks lip_sync_rtp_timestamp_;
86   uint64_t lip_sync_ntp_timestamp_;
87 
88   // The RTCP packet parser is re-used when parsing each RTCP packet.  It
89   // remembers state about prior RTP timestamps and other sequence values to
90   // re-construct "expanded" values.
91   RtcpParser parser_;
92 
93   DISALLOW_COPY_AND_ASSIGN(ReceiverRtcpSession);
94 };
95 
96 }  // namespace cast
97 }  // namespace media
98 
99 #endif  // MEDIA_CAST_NET_RTCP_RECEIVER_RTCP_SESSION_H_
100