1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_RECEIVER_H_
12 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_RECEIVER_H_
13 
14 #include <map>
15 #include <set>
16 #include <vector>
17 
18 #include "webrtc/base/thread_annotations.h"
19 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
20 #include "webrtc/modules/rtp_rtcp/source/rtcp_receiver_help.h"
21 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
22 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
23 #include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
24 #include "webrtc/typedefs.h"
25 
26 namespace webrtc {
27 class ModuleRtpRtcpImpl;
28 
29 class RTCPReceiver : public TMMBRHelp
30 {
31 public:
32  RTCPReceiver(Clock* clock,
33               bool receiver_only,
34               RtcpPacketTypeCounterObserver* packet_type_counter_observer,
35               RtcpBandwidthObserver* rtcp_bandwidth_observer,
36               RtcpIntraFrameObserver* rtcp_intra_frame_observer,
37               TransportFeedbackObserver* transport_feedback_observer,
38               ModuleRtpRtcpImpl* owner);
39     virtual ~RTCPReceiver();
40 
41     RtcpMode Status() const;
42     void SetRTCPStatus(RtcpMode method);
43 
44     int64_t LastReceived();
45     int64_t LastReceivedReceiverReport() const;
46 
47     void SetSsrcs(uint32_t main_ssrc,
48                   const std::set<uint32_t>& registered_ssrcs);
49     void SetRelaySSRC(uint32_t ssrc);
50     void SetRemoteSSRC(uint32_t ssrc);
51     uint32_t RemoteSSRC() const;
52 
53     uint32_t RelaySSRC() const;
54 
55     int32_t IncomingRTCPPacket(
56         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation,
57         RTCPUtility::RTCPParserV2 *rtcpParser);
58 
59     void TriggerCallbacksFromRTCPPacket(
60         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation);
61 
62     // get received cname
63     int32_t CNAME(uint32_t remoteSSRC, char cName[RTCP_CNAME_SIZE]) const;
64 
65     // get received NTP
66     bool NTP(uint32_t* ReceivedNTPsecs,
67              uint32_t* ReceivedNTPfrac,
68              uint32_t* RTCPArrivalTimeSecs,
69              uint32_t* RTCPArrivalTimeFrac,
70              uint32_t* rtcp_timestamp) const;
71 
72    bool LastReceivedXrReferenceTimeInfo(RtcpReceiveTimeInfo* info) const;
73 
74     // get rtt
75     int32_t RTT(uint32_t remoteSSRC,
76                 int64_t* RTT,
77                 int64_t* avgRTT,
78                 int64_t* minRTT,
79                 int64_t* maxRTT) const;
80 
81     int32_t SenderInfoReceived(RTCPSenderInfo* senderInfo) const;
82 
83     bool GetAndResetXrRrRtt(int64_t* rtt_ms);
84 
85     // get statistics
86     int32_t StatisticsReceived(
87         std::vector<RTCPReportBlock>* receiveBlocks) const;
88 
89     // Returns true if we haven't received an RTCP RR for several RTCP
90     // intervals, but only triggers true once.
91     bool RtcpRrTimeout(int64_t rtcp_interval_ms);
92 
93     // Returns true if we haven't received an RTCP RR telling the receive side
94     // has not received RTP packets for too long, i.e. extended highest sequence
95     // number hasn't increased for several RTCP intervals. The function only
96     // returns true once until a new RR is received.
97     bool RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms);
98 
99     // Get TMMBR
100     int32_t TMMBRReceived(uint32_t size,
101                           uint32_t accNumCandidates,
102                           TMMBRSet* candidateSet) const;
103 
104     bool UpdateRTCPReceiveInformationTimers();
105 
106     int32_t BoundingSet(bool* tmmbrOwner, TMMBRSet* boundingSetRec);
107 
108     int32_t UpdateTMMBR();
109 
110     void RegisterRtcpStatisticsCallback(RtcpStatisticsCallback* callback);
111     RtcpStatisticsCallback* GetRtcpStatisticsCallback();
112 
113 protected:
114  RTCPUtility::RTCPCnameInformation* CreateCnameInformation(uint32_t remoteSSRC);
115  RTCPUtility::RTCPCnameInformation* GetCnameInformation(
116      uint32_t remoteSSRC) const;
117 
118  RTCPHelp::RTCPReceiveInformation* CreateReceiveInformation(
119      uint32_t remoteSSRC);
120  RTCPHelp::RTCPReceiveInformation* GetReceiveInformation(uint32_t remoteSSRC);
121 
122     void UpdateReceiveInformation(
123         RTCPHelp::RTCPReceiveInformation& receiveInformation);
124 
125     void HandleSenderReceiverReport(
126         RTCPUtility::RTCPParserV2& rtcpParser,
127         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
128         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
129 
130     void HandleReportBlock(
131         const RTCPUtility::RTCPPacket& rtcpPacket,
132         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation,
133         uint32_t remoteSSRC)
134         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
135 
136     void HandleSDES(RTCPUtility::RTCPParserV2& rtcpParser,
137                     RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
138         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
139 
140     void HandleSDESChunk(RTCPUtility::RTCPParserV2& rtcpParser)
141         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
142 
143     void HandleXrHeader(RTCPUtility::RTCPParserV2& parser,
144                         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
145         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
146 
147     void HandleXrReceiveReferenceTime(
148         RTCPUtility::RTCPParserV2& parser,
149         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
150         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
151 
152     void HandleXrDlrrReportBlock(
153         RTCPUtility::RTCPParserV2& parser,
154         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
155         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
156 
157     void HandleXrDlrrReportBlockItem(
158         const RTCPUtility::RTCPPacket& packet,
159         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
160         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
161 
162     void HandleXRVOIPMetric(
163         RTCPUtility::RTCPParserV2& rtcpParser,
164         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
165         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
166 
167     void HandleNACK(RTCPUtility::RTCPParserV2& rtcpParser,
168                     RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
169         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
170 
171     void HandleNACKItem(const RTCPUtility::RTCPPacket& rtcpPacket,
172                         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
173         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
174 
175     void HandleBYE(RTCPUtility::RTCPParserV2& rtcpParser)
176         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
177 
178     void HandlePLI(RTCPUtility::RTCPParserV2& rtcpParser,
179                    RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
180         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
181 
182     void HandleSLI(RTCPUtility::RTCPParserV2& rtcpParser,
183                    RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
184         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
185 
186     void HandleSLIItem(const RTCPUtility::RTCPPacket& rtcpPacket,
187                        RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
188         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
189 
190     void HandleRPSI(RTCPUtility::RTCPParserV2& rtcpParser,
191                     RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
192         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
193 
194     void HandlePsfbApp(RTCPUtility::RTCPParserV2& rtcpParser,
195                        RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
196         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
197 
198     void HandleREMBItem(RTCPUtility::RTCPParserV2& rtcpParser,
199                         RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
200         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
201 
202     void HandleIJ(RTCPUtility::RTCPParserV2& rtcpParser,
203                   RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
204         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
205 
206     void HandleIJItem(const RTCPUtility::RTCPPacket& rtcpPacket,
207                       RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
208         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
209 
210     void HandleTMMBR(RTCPUtility::RTCPParserV2& rtcpParser,
211                      RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
212         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
213 
214     void HandleTMMBRItem(RTCPHelp::RTCPReceiveInformation& receiveInfo,
215                          const RTCPUtility::RTCPPacket& rtcpPacket,
216                          RTCPHelp::RTCPPacketInformation& rtcpPacketInformation,
217                          uint32_t senderSSRC)
218         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
219 
220     void HandleTMMBN(RTCPUtility::RTCPParserV2& rtcpParser,
221                      RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
222         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
223 
224     void HandleSR_REQ(RTCPUtility::RTCPParserV2& rtcpParser,
225                       RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
226         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
227 
228     void HandleTMMBNItem(RTCPHelp::RTCPReceiveInformation& receiveInfo,
229                          const RTCPUtility::RTCPPacket& rtcpPacket)
230         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
231 
232     void HandleFIR(RTCPUtility::RTCPParserV2& rtcpParser,
233                    RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
234         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
235 
236     void HandleFIRItem(RTCPHelp::RTCPReceiveInformation* receiveInfo,
237                        const RTCPUtility::RTCPPacket& rtcpPacket,
238                        RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
239         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
240 
241     void HandleAPP(RTCPUtility::RTCPParserV2& rtcpParser,
242                    RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
243         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
244 
245     void HandleAPPItem(RTCPUtility::RTCPParserV2& rtcpParser,
246                        RTCPHelp::RTCPPacketInformation& rtcpPacketInformation)
247         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
248 
249     void HandleTransportFeedback(
250         RTCPUtility::RTCPParserV2* rtcp_parser,
251         RTCPHelp::RTCPPacketInformation* rtcp_packet_information)
252         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
253 
254  private:
255   typedef std::map<uint32_t, RTCPHelp::RTCPReceiveInformation*>
256       ReceivedInfoMap;
257   // RTCP report block information mapped by remote SSRC.
258   typedef std::map<uint32_t, RTCPHelp::RTCPReportBlockInformation*>
259       ReportBlockInfoMap;
260   // RTCP report block information map mapped by source SSRC.
261   typedef std::map<uint32_t, ReportBlockInfoMap> ReportBlockMap;
262 
263   RTCPHelp::RTCPReportBlockInformation* CreateOrGetReportBlockInformation(
264       uint32_t remote_ssrc, uint32_t source_ssrc)
265           EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
266   RTCPHelp::RTCPReportBlockInformation* GetReportBlockInformation(
267       uint32_t remote_ssrc, uint32_t source_ssrc) const
268           EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
269 
270   Clock* const _clock;
271   const bool receiver_only_;
272   RtcpMode _method;
273   int64_t _lastReceived;
274   ModuleRtpRtcpImpl& _rtpRtcp;
275 
276   CriticalSectionWrapper* _criticalSectionFeedbacks;
277   RtcpBandwidthObserver* const _cbRtcpBandwidthObserver;
278   RtcpIntraFrameObserver* const _cbRtcpIntraFrameObserver;
279   TransportFeedbackObserver* const _cbTransportFeedbackObserver;
280 
281   CriticalSectionWrapper* _criticalSectionRTCPReceiver;
282   uint32_t main_ssrc_ GUARDED_BY(_criticalSectionRTCPReceiver);
283   uint32_t _remoteSSRC GUARDED_BY(_criticalSectionRTCPReceiver);
284   std::set<uint32_t> registered_ssrcs_ GUARDED_BY(_criticalSectionRTCPReceiver);
285 
286   // Received send report
287   RTCPSenderInfo _remoteSenderInfo;
288   // when did we receive the last send report
289   uint32_t _lastReceivedSRNTPsecs;
290   uint32_t _lastReceivedSRNTPfrac;
291 
292   // Received XR receive time report.
293   RtcpReceiveTimeInfo _remoteXRReceiveTimeInfo;
294   // Time when the report was received.
295   uint32_t _lastReceivedXRNTPsecs;
296   uint32_t _lastReceivedXRNTPfrac;
297   // Estimated rtt, zero when there is no valid estimate.
298   int64_t xr_rr_rtt_ms_;
299 
300   // Received report blocks.
301   ReportBlockMap _receivedReportBlockMap
302       GUARDED_BY(_criticalSectionRTCPReceiver);
303   ReceivedInfoMap _receivedInfoMap;
304   std::map<uint32_t, RTCPUtility::RTCPCnameInformation*> _receivedCnameMap;
305 
306   uint32_t _packetTimeOutMS;
307 
308   // The last time we received an RTCP RR.
309   int64_t _lastReceivedRrMs;
310 
311   // The time we last received an RTCP RR telling we have successfully
312   // delivered RTP packet to the remote side.
313   int64_t _lastIncreasedSequenceNumberMs;
314 
315   RtcpStatisticsCallback* stats_callback_ GUARDED_BY(_criticalSectionFeedbacks);
316 
317   RtcpPacketTypeCounterObserver* const packet_type_counter_observer_;
318   RtcpPacketTypeCounter packet_type_counter_;
319 
320   RTCPUtility::NackStats nack_stats_;
321 
322   size_t num_skipped_packets_;
323   int64_t last_skipped_packets_warning_;
324 };
325 }  // namespace webrtc
326 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_RECEIVER_H_
327