Line data Source code
1 : /*
2 : * Copyright (c) 2016 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 : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
12 :
13 : #include "webrtc/base/checks.h"
14 : #include "webrtc/base/logging.h"
15 : #include "webrtc/modules/rtp_rtcp/source/byte_io.h"
16 : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
17 :
18 : namespace webrtc {
19 : namespace rtcp {
20 : constexpr uint8_t ExtendedReports::kPacketType;
21 : // From RFC 3611: RTP Control Protocol Extended Reports (RTCP XR).
22 : //
23 : // Format for XR packets:
24 : //
25 : // 0 1 2 3
26 : // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
27 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28 : // |V=2|P|reserved | PT=XR=207 | length |
29 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 : // | SSRC |
31 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 : // : report blocks :
33 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 : //
35 : // Extended report block:
36 : // 0 1 2 3
37 : // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
38 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 : // | Block Type | reserved | block length |
40 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 : // : type-specific block contents :
42 : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 0 : ExtendedReports::ExtendedReports() : sender_ssrc_(0) {}
44 0 : ExtendedReports::~ExtendedReports() {}
45 :
46 0 : bool ExtendedReports::Parse(const CommonHeader& packet) {
47 0 : RTC_DCHECK_EQ(packet.type(), kPacketType);
48 :
49 0 : if (packet.payload_size_bytes() < kXrBaseLength) {
50 0 : LOG(LS_WARNING) << "Packet is too small to be an ExtendedReports packet.";
51 0 : return false;
52 : }
53 :
54 0 : sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(packet.payload());
55 0 : rrtr_block_.reset();
56 0 : dlrr_block_.ClearItems();
57 0 : voip_metric_block_.reset();
58 0 : target_bitrate_ = rtc::Optional<TargetBitrate>();
59 :
60 0 : const uint8_t* current_block = packet.payload() + kXrBaseLength;
61 : const uint8_t* const packet_end =
62 0 : packet.payload() + packet.payload_size_bytes();
63 0 : constexpr size_t kBlockHeaderSizeBytes = 4;
64 0 : while (current_block + kBlockHeaderSizeBytes <= packet_end) {
65 0 : uint8_t block_type = ByteReader<uint8_t>::ReadBigEndian(current_block);
66 : uint16_t block_length =
67 0 : ByteReader<uint16_t>::ReadBigEndian(current_block + 2);
68 : const uint8_t* next_block =
69 0 : current_block + kBlockHeaderSizeBytes + block_length * 4;
70 0 : if (next_block > packet_end) {
71 0 : LOG(LS_WARNING) << "Report block in extended report packet is too big.";
72 0 : return false;
73 : }
74 0 : switch (block_type) {
75 : case Rrtr::kBlockType:
76 0 : ParseRrtrBlock(current_block, block_length);
77 0 : break;
78 : case Dlrr::kBlockType:
79 0 : ParseDlrrBlock(current_block, block_length);
80 0 : break;
81 : case VoipMetric::kBlockType:
82 0 : ParseVoipMetricBlock(current_block, block_length);
83 0 : break;
84 : case TargetBitrate::kBlockType:
85 0 : ParseTargetBitrateBlock(current_block, block_length);
86 0 : break;
87 : default:
88 : // Unknown block, ignore.
89 0 : LOG(LS_WARNING) << "Unknown extended report block type " << block_type;
90 0 : break;
91 : }
92 0 : current_block = next_block;
93 : }
94 :
95 0 : return true;
96 : }
97 :
98 0 : void ExtendedReports::SetRrtr(const Rrtr& rrtr) {
99 0 : if (rrtr_block_)
100 0 : LOG(LS_WARNING) << "Rrtr already set, overwriting.";
101 0 : rrtr_block_.emplace(rrtr);
102 0 : }
103 :
104 0 : void ExtendedReports::AddDlrrItem(const ReceiveTimeInfo& time_info) {
105 0 : dlrr_block_.AddDlrrItem(time_info);
106 0 : }
107 :
108 0 : void ExtendedReports::SetVoipMetric(const VoipMetric& voip_metric) {
109 0 : if (voip_metric_block_)
110 0 : LOG(LS_WARNING) << "Voip metric already set, overwriting.";
111 0 : voip_metric_block_.emplace(voip_metric);
112 0 : }
113 :
114 0 : void ExtendedReports::SetTargetBitrate(const TargetBitrate& bitrate) {
115 0 : if (target_bitrate_)
116 0 : LOG(LS_WARNING) << "TargetBitrate already set, overwriting.";
117 :
118 0 : target_bitrate_ = rtc::Optional<TargetBitrate>(bitrate);
119 0 : }
120 :
121 0 : bool ExtendedReports::Create(uint8_t* packet,
122 : size_t* index,
123 : size_t max_length,
124 : RtcpPacket::PacketReadyCallback* callback) const {
125 0 : while (*index + BlockLength() > max_length) {
126 0 : if (!OnBufferFull(packet, index, callback))
127 0 : return false;
128 : }
129 0 : size_t index_end = *index + BlockLength();
130 0 : const uint8_t kReserved = 0;
131 0 : CreateHeader(kReserved, kPacketType, HeaderLength(), packet, index);
132 0 : ByteWriter<uint32_t>::WriteBigEndian(packet + *index, sender_ssrc_);
133 0 : *index += sizeof(uint32_t);
134 0 : if (rrtr_block_) {
135 0 : rrtr_block_->Create(packet + *index);
136 0 : *index += Rrtr::kLength;
137 : }
138 0 : if (dlrr_block_) {
139 0 : dlrr_block_.Create(packet + *index);
140 0 : *index += dlrr_block_.BlockLength();
141 : }
142 0 : if (voip_metric_block_) {
143 0 : voip_metric_block_->Create(packet + *index);
144 0 : *index += VoipMetric::kLength;
145 : }
146 0 : if (target_bitrate_) {
147 0 : target_bitrate_->Create(packet + *index);
148 0 : *index += target_bitrate_->BlockLength();
149 : }
150 0 : RTC_CHECK_EQ(*index, index_end);
151 0 : return true;
152 : }
153 :
154 0 : size_t ExtendedReports::TargetBitrateLength() const {
155 0 : if (target_bitrate_)
156 0 : return target_bitrate_->BlockLength();
157 0 : return 0;
158 : }
159 :
160 0 : void ExtendedReports::ParseRrtrBlock(const uint8_t* block,
161 : uint16_t block_length) {
162 0 : if (block_length != Rrtr::kBlockLength) {
163 0 : LOG(LS_WARNING) << "Incorrect rrtr block size " << block_length
164 0 : << " Should be " << Rrtr::kBlockLength;
165 0 : return;
166 : }
167 0 : if (rrtr_block_) {
168 0 : LOG(LS_WARNING) << "Two rrtr blocks found in same Extended Report packet";
169 0 : return;
170 : }
171 0 : rrtr_block_.emplace();
172 0 : rrtr_block_->Parse(block);
173 : }
174 :
175 0 : void ExtendedReports::ParseDlrrBlock(const uint8_t* block,
176 : uint16_t block_length) {
177 0 : if (dlrr_block_) {
178 0 : LOG(LS_WARNING) << "Two Dlrr blocks found in same Extended Report packet";
179 0 : return;
180 : }
181 0 : dlrr_block_.Parse(block, block_length);
182 : }
183 :
184 0 : void ExtendedReports::ParseVoipMetricBlock(const uint8_t* block,
185 : uint16_t block_length) {
186 0 : if (block_length != VoipMetric::kBlockLength) {
187 0 : LOG(LS_WARNING) << "Incorrect voip metric block size " << block_length
188 0 : << " Should be " << VoipMetric::kBlockLength;
189 0 : return;
190 : }
191 0 : if (voip_metric_block_) {
192 0 : LOG(LS_WARNING)
193 0 : << "Two Voip Metric blocks found in same Extended Report packet";
194 0 : return;
195 : }
196 0 : voip_metric_block_.emplace();
197 0 : voip_metric_block_->Parse(block);
198 : }
199 :
200 0 : void ExtendedReports::ParseTargetBitrateBlock(const uint8_t* block,
201 : uint16_t block_length) {
202 0 : target_bitrate_ = rtc::Optional<TargetBitrate>(TargetBitrate());
203 0 : if (!target_bitrate_->Parse(block, block_length))
204 0 : target_bitrate_ = rtc::Optional<TargetBitrate>();
205 0 : }
206 : } // namespace rtcp
207 : } // namespace webrtc
|