blob: 8d1e2c23300e4bdf8deef5624978bcf83933d551 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000014#include <map>
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000015#include <set>
danilchap95321242016-09-27 07:05:32 -070016#include <string>
danilchapb8b6fbb2015-12-10 05:05:27 -080017#include <vector>
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +000018
danilchap7c9426c2016-04-14 03:05:31 -070019#include "webrtc/base/criticalsection.h"
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +000020#include "webrtc/base/thread_annotations.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010021#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
danilchap798896a2016-09-28 02:54:25 -070022#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000023#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
danilchap0b4b7272016-10-06 09:24:45 -070024#include "webrtc/system_wrappers/include/ntp_time.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000025#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
27namespace webrtc {
spranga790d832016-12-02 07:29:44 -080028class VideoBitrateAllocationObserver;
danilchap59cb2bd2016-08-29 11:08:47 -070029namespace rtcp {
danilchap1b1863a2016-09-20 01:39:54 -070030class CommonHeader;
danilchap1b1863a2016-09-20 01:39:54 -070031class ReportBlock;
32class Rrtr;
spranga790d832016-12-02 07:29:44 -080033class TargetBitrate;
danilchap59cb2bd2016-08-29 11:08:47 -070034class TmmbItem;
35} // namespace rtcp
pwestin@webrtc.org741da942011-09-20 13:52:04 +000036
danilchapda161d72016-08-19 07:29:46 -070037class RTCPReceiver {
38 public:
danilchap59cb2bd2016-08-29 11:08:47 -070039 class ModuleRtpRtcp {
40 public:
41 virtual void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) = 0;
42 virtual void OnRequestSendReport() = 0;
43 virtual void OnReceivedNack(
44 const std::vector<uint16_t>& nack_sequence_numbers) = 0;
45 virtual void OnReceivedRtcpReportBlocks(
46 const ReportBlockList& report_blocks) = 0;
47
48 protected:
49 virtual ~ModuleRtpRtcp() = default;
50 };
51
danilchapda161d72016-08-19 07:29:46 -070052 RTCPReceiver(Clock* clock,
53 bool receiver_only,
54 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
55 RtcpBandwidthObserver* rtcp_bandwidth_observer,
56 RtcpIntraFrameObserver* rtcp_intra_frame_observer,
57 TransportFeedbackObserver* transport_feedback_observer,
spranga790d832016-12-02 07:29:44 -080058 VideoBitrateAllocationObserver* bitrate_allocation_observer,
danilchap59cb2bd2016-08-29 11:08:47 -070059 ModuleRtpRtcp* owner);
danilchapda161d72016-08-19 07:29:46 -070060 virtual ~RTCPReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +000061
danilchap59cb2bd2016-08-29 11:08:47 -070062 bool IncomingPacket(const uint8_t* packet, size_t packet_size);
63
danilchapda161d72016-08-19 07:29:46 -070064 int64_t LastReceivedReceiverReport() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000065
danilchapda161d72016-08-19 07:29:46 -070066 void SetSsrcs(uint32_t main_ssrc, const std::set<uint32_t>& registered_ssrcs);
67 void SetRemoteSSRC(uint32_t ssrc);
68 uint32_t RemoteSSRC() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000069
danilchapda161d72016-08-19 07:29:46 -070070 // get received cname
71 int32_t CNAME(uint32_t remoteSSRC, char cName[RTCP_CNAME_SIZE]) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000072
danilchapda161d72016-08-19 07:29:46 -070073 // get received NTP
74 bool NTP(uint32_t* ReceivedNTPsecs,
75 uint32_t* ReceivedNTPfrac,
76 uint32_t* RTCPArrivalTimeSecs,
77 uint32_t* RTCPArrivalTimeFrac,
78 uint32_t* rtcp_timestamp) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000079
danilchap798896a2016-09-28 02:54:25 -070080 bool LastReceivedXrReferenceTimeInfo(rtcp::ReceiveTimeInfo* info) const;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000081
danilchap28b03eb2016-10-05 06:59:44 -070082 // Get rtt.
83 int32_t RTT(uint32_t remote_ssrc,
84 int64_t* last_rtt_ms,
85 int64_t* avg_rtt_ms,
86 int64_t* min_rtt_ms,
87 int64_t* max_rtt_ms) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000088
danilchapda161d72016-08-19 07:29:46 -070089 int32_t SenderInfoReceived(RTCPSenderInfo* senderInfo) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000090
danilchapda161d72016-08-19 07:29:46 -070091 void SetRtcpXrRrtrStatus(bool enable);
92 bool GetAndResetXrRrRtt(int64_t* rtt_ms);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +000093
danilchapda161d72016-08-19 07:29:46 -070094 // get statistics
95 int32_t StatisticsReceived(std::vector<RTCPReportBlock>* receiveBlocks) const;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +000096
danilchapda161d72016-08-19 07:29:46 -070097 // Returns true if we haven't received an RTCP RR for several RTCP
98 // intervals, but only triggers true once.
99 bool RtcpRrTimeout(int64_t rtcp_interval_ms);
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000100
danilchapda161d72016-08-19 07:29:46 -0700101 // Returns true if we haven't received an RTCP RR telling the receive side
102 // has not received RTP packets for too long, i.e. extended highest sequence
103 // number hasn't increased for several RTCP intervals. The function only
104 // returns true once until a new RR is received.
105 bool RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms);
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000106
danilchap7851bda2016-09-29 15:28:07 -0700107 std::vector<rtcp::TmmbItem> TmmbrReceived();
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
danilchapda161d72016-08-19 07:29:46 -0700109 bool UpdateRTCPReceiveInformationTimers();
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
danilchapda161d72016-08-19 07:29:46 -0700111 std::vector<rtcp::TmmbItem> BoundingSet(bool* tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112
danilchap853ecb22016-08-22 08:26:15 -0700113 void UpdateTmmbr();
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
danilchapda161d72016-08-19 07:29:46 -0700115 void RegisterRtcpStatisticsCallback(RtcpStatisticsCallback* callback);
116 RtcpStatisticsCallback* GetRtcpStatisticsCallback();
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000117
danilchapdd128922016-09-13 12:23:29 -0700118 private:
danilchap92ea6012016-09-23 10:36:03 -0700119 struct PacketInformation;
danilchap7851bda2016-09-29 15:28:07 -0700120 struct ReceiveInformation;
danilchap28b03eb2016-10-05 06:59:44 -0700121 struct ReportBlockWithRtt;
danilchap7851bda2016-09-29 15:28:07 -0700122 // Mapped by remote ssrc.
123 using ReceivedInfoMap = std::map<uint32_t, ReceiveInformation>;
danilchap28b03eb2016-10-05 06:59:44 -0700124 // RTCP report blocks mapped by remote SSRC.
125 using ReportBlockInfoMap = std::map<uint32_t, ReportBlockWithRtt>;
126 // RTCP report blocks map mapped by source SSRC.
danilchapdd128922016-09-13 12:23:29 -0700127 using ReportBlockMap = std::map<uint32_t, ReportBlockInfoMap>;
128
danilchap1b1863a2016-09-20 01:39:54 -0700129 bool ParseCompoundPacket(const uint8_t* packet_begin,
130 const uint8_t* packet_end,
danilchap92ea6012016-09-23 10:36:03 -0700131 PacketInformation* packet_information);
danilchapdd128922016-09-13 12:23:29 -0700132
133 void TriggerCallbacksFromRTCPPacket(
danilchap92ea6012016-09-23 10:36:03 -0700134 const PacketInformation& packet_information);
danilchapdd128922016-09-13 12:23:29 -0700135
danilchap7851bda2016-09-29 15:28:07 -0700136 void CreateReceiveInformation(uint32_t remote_ssrc)
137 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
138 ReceiveInformation* GetReceiveInformation(uint32_t remote_ssrc)
139 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
danilchap92ea6012016-09-23 10:36:03 -0700141 void HandleSenderReport(const rtcp::CommonHeader& rtcp_block,
142 PacketInformation* packet_information)
Danil Chapovalov91511f12016-09-15 16:24:02 +0200143 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
144
danilchap92ea6012016-09-23 10:36:03 -0700145 void HandleReceiverReport(const rtcp::CommonHeader& rtcp_block,
146 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700147 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
danilchap1b1863a2016-09-20 01:39:54 -0700149 void HandleReportBlock(const rtcp::ReportBlock& report_block,
danilchap92ea6012016-09-23 10:36:03 -0700150 PacketInformation* packet_information,
danilchapda161d72016-08-19 07:29:46 -0700151 uint32_t remoteSSRC)
152 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
danilchap1b1863a2016-09-20 01:39:54 -0700154 void HandleSDES(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700155 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700156 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
danilchap1b1863a2016-09-20 01:39:54 -0700158 void HandleXr(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700159 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700160 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
danilchap92ea6012016-09-23 10:36:03 -0700162 void HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
163 const rtcp::Rrtr& rrtr)
danilchapda161d72016-08-19 07:29:46 -0700164 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000165
danilchap92ea6012016-09-23 10:36:03 -0700166 void HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti)
danilchapda161d72016-08-19 07:29:46 -0700167 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
168
spranga790d832016-12-02 07:29:44 -0800169 void HandleXrTargetBitrate(const rtcp::TargetBitrate& target_bitrate,
170 PacketInformation* packet_information)
171 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
172
danilchap1b1863a2016-09-20 01:39:54 -0700173 void HandleNACK(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700174 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700175 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
176
danilchap1b1863a2016-09-20 01:39:54 -0700177 void HandleBYE(const rtcp::CommonHeader& rtcp_block)
danilchapda161d72016-08-19 07:29:46 -0700178 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
179
danilchap1b1863a2016-09-20 01:39:54 -0700180 void HandlePLI(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700181 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700182 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
183
danilchap1b1863a2016-09-20 01:39:54 -0700184 void HandleSLI(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700185 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700186 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
187
danilchap1b1863a2016-09-20 01:39:54 -0700188 void HandleRPSI(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700189 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700190 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
191
danilchap1b1863a2016-09-20 01:39:54 -0700192 void HandlePsfbApp(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700193 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700194 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
195
danilchap1b1863a2016-09-20 01:39:54 -0700196 void HandleTMMBR(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700197 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700198 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
danilchap1b1863a2016-09-20 01:39:54 -0700200 void HandleTMMBN(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700201 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700202 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000203
danilchap1b1863a2016-09-20 01:39:54 -0700204 void HandleSR_REQ(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700205 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700206 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
danilchap1b1863a2016-09-20 01:39:54 -0700208 void HandleFIR(const rtcp::CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700209 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700210 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
211
danilchap92ea6012016-09-23 10:36:03 -0700212 void HandleTransportFeedback(const rtcp::CommonHeader& rtcp_block,
213 PacketInformation* packet_information)
danilchapda161d72016-08-19 07:29:46 -0700214 EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPReceiver);
Erik Språng6b8d3552015-09-24 15:06:57 +0200215
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200216 Clock* const _clock;
217 const bool receiver_only_;
danilchap59cb2bd2016-08-29 11:08:47 -0700218 ModuleRtpRtcp& _rtpRtcp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
spranga790d832016-12-02 07:29:44 -0800220 rtc::CriticalSection feedbacks_lock_;
221 RtcpBandwidthObserver* const rtcp_bandwidth_observer_;
222 RtcpIntraFrameObserver* const rtcp_intra_frame_observer_;
223 TransportFeedbackObserver* const transport_feedback_observer_;
224 VideoBitrateAllocationObserver* const bitrate_allocation_observer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
danilchap7c9426c2016-04-14 03:05:31 -0700226 rtc::CriticalSection _criticalSectionRTCPReceiver;
sprang7dc39f32015-10-13 09:17:48 -0700227 uint32_t main_ssrc_ GUARDED_BY(_criticalSectionRTCPReceiver);
228 uint32_t _remoteSSRC GUARDED_BY(_criticalSectionRTCPReceiver);
229 std::set<uint32_t> registered_ssrcs_ GUARDED_BY(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000231 // Received send report
232 RTCPSenderInfo _remoteSenderInfo;
danilchap0b4b7272016-10-06 09:24:45 -0700233 // When did we receive the last send report.
234 NtpTime last_received_sr_ntp_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000236 // Received XR receive time report.
danilchap798896a2016-09-28 02:54:25 -0700237 rtcp::ReceiveTimeInfo remote_time_info_;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000238 // Time when the report was received.
danilchap0b4b7272016-10-06 09:24:45 -0700239 NtpTime last_received_xr_ntp_;
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000240 // Estimated rtt, zero when there is no valid estimate.
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100241 bool xr_rrtr_status_ GUARDED_BY(_criticalSectionRTCPReceiver);
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000242 int64_t xr_rr_rtt_ms_;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000243
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000244 // Received report blocks.
danilchap28b03eb2016-10-05 06:59:44 -0700245 ReportBlockMap received_report_blocks_
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000246 GUARDED_BY(_criticalSectionRTCPReceiver);
danilchap7851bda2016-09-29 15:28:07 -0700247 ReceivedInfoMap received_infos_ GUARDED_BY(_criticalSectionRTCPReceiver);
danilchap95321242016-09-27 07:05:32 -0700248 std::map<uint32_t, std::string> received_cnames_
249 GUARDED_BY(_criticalSectionRTCPReceiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000250
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000251 // The last time we received an RTCP RR.
252 int64_t _lastReceivedRrMs;
253
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000254 // The time we last received an RTCP RR telling we have successfully
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000255 // delivered RTP packet to the remote side.
256 int64_t _lastIncreasedSequenceNumberMs;
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +0000257
spranga790d832016-12-02 07:29:44 -0800258 RtcpStatisticsCallback* stats_callback_ GUARDED_BY(feedbacks_lock_);
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000259
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000260 RtcpPacketTypeCounterObserver* const packet_type_counter_observer_;
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000261 RtcpPacketTypeCounter packet_type_counter_;
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000262
263 RTCPUtility::NackStats nack_stats_;
Erik Språng6b8d3552015-09-24 15:06:57 +0200264
265 size_t num_skipped_packets_;
266 int64_t last_skipped_packets_warning_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000268} // namespace webrtc
danilchapda161d72016-08-19 07:29:46 -0700269#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_RECEIVER_H_