OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h" | 11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h" |
12 | 12 |
13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
16 | 16 |
17 using webrtc::RTCPUtility::PT_XR; | 17 using webrtc::RTCPUtility::RtcpCommonHeader; |
18 using webrtc::RTCPUtility::RTCPPacketXR; | |
19 | 18 |
20 namespace webrtc { | 19 namespace webrtc { |
21 namespace rtcp { | 20 namespace rtcp { |
22 namespace { | |
23 void AssignUWord32(uint8_t* buffer, size_t* offset, uint32_t value) { | |
24 ByteWriter<uint32_t>::WriteBigEndian(buffer + *offset, value); | |
25 *offset += 4; | |
26 } | |
27 // From RFC 3611: RTP Control Protocol Extended Reports (RTCP XR). | 21 // From RFC 3611: RTP Control Protocol Extended Reports (RTCP XR). |
28 // | 22 // |
29 // Format for XR packets: | 23 // Format for XR packets: |
30 // | 24 // |
31 // 0 1 2 3 | 25 // 0 1 2 3 |
32 // 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 | 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 |
33 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 27 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
34 // |V=2|P|reserved | PT=XR=207 | length | | 28 // |V=2|P|reserved | PT=XR=207 | length | |
35 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 29 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
36 // | SSRC | | 30 // | SSRC | |
37 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 31 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
38 // : report blocks : | 32 // : report blocks : |
39 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 33 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
40 void CreateXrHeader(const RTCPPacketXR& header, | 34 // |
41 uint8_t* buffer, | 35 // Report header: |
åsapersson
2016/01/29 12:46:09
maybe Report block header:
danilchap
2016/01/29 13:49:30
To match specification included full report block
| |
42 size_t* pos) { | 36 // 0 1 2 3 |
43 AssignUWord32(buffer, pos, header.OriginatorSSRC); | 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 ExtendedReports::ExtendedReports() : sender_ssrc_(0) {} | |
42 ExtendedReports::~ExtendedReports() {} | |
43 | |
44 bool ExtendedReports::Parse(const RtcpCommonHeader& header, | |
45 const uint8_t* payload) { | |
46 RTC_CHECK(header.packet_type == kPacketType); | |
47 | |
48 if (header.payload_size_bytes < kXrHeaderLength) { | |
49 LOG(LS_WARNING) << "Packet is too small to be an ExtendedReports packet."; | |
50 return false; | |
51 } | |
52 | |
53 sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(payload); | |
54 | |
åsapersson
2016/01/29 12:46:09
should the blocks be resized to zero here?
danilchap
2016/01/29 13:49:30
Done.
| |
55 const uint8_t* current_block = payload + kXrHeaderLength; | |
56 const uint8_t* const packet_end = payload + header.payload_size_bytes; | |
57 const size_t kBlockHeaderSizeBytes = 4; | |
58 while (current_block + kBlockHeaderSizeBytes <= packet_end) { | |
59 uint8_t block_type = ByteReader<uint8_t>::ReadBigEndian(current_block); | |
60 uint16_t block_size = | |
åsapersson
2016/01/29 12:46:09
perhaps call block_length (as in figure)
danilchap
2016/01/29 13:49:30
Done. Word 'size' may also give false impression i
| |
61 ByteReader<uint16_t>::ReadBigEndian(current_block + 2); | |
62 const uint8_t* next_block = | |
63 current_block + kBlockHeaderSizeBytes + block_size * 4; | |
64 if (next_block > packet_end) { | |
65 LOG(LS_WARNING) << "Report block in extended report packet is too big."; | |
66 return false; | |
67 } | |
68 switch (block_type) { | |
69 case Rrtr::kBlockType: | |
70 ParseRrtrBlock(current_block, block_size); | |
71 break; | |
72 case Dlrr::kBlockType: | |
73 ParseDlrrBlock(current_block, block_size); | |
74 break; | |
75 case VoipMetric::kBlockType: | |
76 ParseVoipMetricBlock(current_block, block_size); | |
77 break; | |
78 default: | |
79 // Unknown block, ignore. | |
80 LOG(LS_WARNING) << "Unknown extended report block type " << block_type; | |
81 break; | |
82 } | |
83 current_block = next_block; | |
84 } | |
85 | |
86 return true; | |
44 } | 87 } |
45 } // namespace | 88 |
89 bool ExtendedReports::WithRrtr(const Rrtr& rrtr) { | |
90 if (rrtr_blocks_.size() >= kMaxNumberOfRrtrBlocks) { | |
91 LOG(LS_WARNING) << "Max RRTR blocks reached."; | |
92 return false; | |
93 } | |
94 rrtr_blocks_.push_back(rrtr); | |
95 return true; | |
96 } | |
97 | |
98 bool ExtendedReports::WithDlrr(const Dlrr& dlrr) { | |
99 if (dlrr_blocks_.size() >= kMaxNumberOfDlrrBlocks) { | |
100 LOG(LS_WARNING) << "Max DLRR blocks reached."; | |
101 return false; | |
102 } | |
103 dlrr_blocks_.push_back(dlrr); | |
104 return true; | |
105 } | |
106 | |
107 bool ExtendedReports::WithVoipMetric(const VoipMetric& voip_metric) { | |
108 if (voip_metric_blocks_.size() >= kMaxNumberOfVoipMetricBlocks) { | |
109 LOG(LS_WARNING) << "Max Voip Metric blocks reached."; | |
110 return false; | |
111 } | |
112 voip_metric_blocks_.push_back(voip_metric); | |
113 return true; | |
114 } | |
46 | 115 |
47 bool ExtendedReports::Create(uint8_t* packet, | 116 bool ExtendedReports::Create(uint8_t* packet, |
48 size_t* index, | 117 size_t* index, |
49 size_t max_length, | 118 size_t max_length, |
50 RtcpPacket::PacketReadyCallback* callback) const { | 119 RtcpPacket::PacketReadyCallback* callback) const { |
51 while (*index + BlockLength() > max_length) { | 120 while (*index + BlockLength() > max_length) { |
52 if (!OnBufferFull(packet, index, callback)) | 121 if (!OnBufferFull(packet, index, callback)) |
53 return false; | 122 return false; |
54 } | 123 } |
55 CreateHeader(0U, PT_XR, HeaderLength(), packet, index); | 124 size_t index_end = *index + BlockLength(); |
56 CreateXrHeader(xr_header_, packet, index); | 125 const uint8_t kReserved = 0; |
126 CreateHeader(kReserved, kPacketType, HeaderLength(), packet, index); | |
127 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, sender_ssrc_); | |
128 *index += sizeof(uint32_t); | |
57 for (const Rrtr& block : rrtr_blocks_) { | 129 for (const Rrtr& block : rrtr_blocks_) { |
58 block.Create(packet + *index); | 130 block.Create(packet + *index); |
59 *index += Rrtr::kLength; | 131 *index += Rrtr::kLength; |
60 } | 132 } |
61 for (const Dlrr& block : dlrr_blocks_) { | 133 for (const Dlrr& block : dlrr_blocks_) { |
62 block.Create(packet + *index); | 134 block.Create(packet + *index); |
63 *index += block.BlockLength(); | 135 *index += block.BlockLength(); |
64 } | 136 } |
65 for (const VoipMetric& block : voip_metric_blocks_) { | 137 for (const VoipMetric& block : voip_metric_blocks_) { |
66 block.Create(packet + *index); | 138 block.Create(packet + *index); |
67 *index += VoipMetric::kLength; | 139 *index += VoipMetric::kLength; |
68 } | 140 } |
141 RTC_CHECK_EQ(*index, index_end); | |
69 return true; | 142 return true; |
70 } | 143 } |
71 | 144 |
72 bool ExtendedReports::WithRrtr(Rrtr* rrtr) { | |
73 RTC_DCHECK(rrtr); | |
74 if (rrtr_blocks_.size() >= kMaxNumberOfRrtrBlocks) { | |
75 LOG(LS_WARNING) << "Max RRTR blocks reached."; | |
76 return false; | |
77 } | |
78 rrtr_blocks_.push_back(*rrtr); | |
79 return true; | |
80 } | |
81 | |
82 bool ExtendedReports::WithDlrr(Dlrr* dlrr) { | |
83 RTC_DCHECK(dlrr); | |
84 if (dlrr_blocks_.size() >= kMaxNumberOfDlrrBlocks) { | |
85 LOG(LS_WARNING) << "Max DLRR blocks reached."; | |
86 return false; | |
87 } | |
88 dlrr_blocks_.push_back(*dlrr); | |
89 return true; | |
90 } | |
91 | |
92 bool ExtendedReports::WithVoipMetric(VoipMetric* voip_metric) { | |
93 assert(voip_metric); | |
94 if (voip_metric_blocks_.size() >= kMaxNumberOfVoipMetricBlocks) { | |
95 LOG(LS_WARNING) << "Max Voip Metric blocks reached."; | |
96 return false; | |
97 } | |
98 voip_metric_blocks_.push_back(*voip_metric); | |
99 return true; | |
100 } | |
101 | |
102 size_t ExtendedReports::DlrrLength() const { | 145 size_t ExtendedReports::DlrrLength() const { |
103 size_t length = 0; | 146 size_t length = 0; |
104 for (const Dlrr& block : dlrr_blocks_) { | 147 for (const Dlrr& block : dlrr_blocks_) { |
105 length += block.BlockLength(); | 148 length += block.BlockLength(); |
106 } | 149 } |
107 return length; | 150 return length; |
108 } | 151 } |
109 | 152 |
153 void ExtendedReports::ParseRrtrBlock(const uint8_t* block, | |
154 uint16_t block_size) { | |
155 if (block_size != Rrtr::kBlockLength) { | |
156 LOG(LS_WARNING) << "Incorrect rrtr block size " << block_size | |
157 << " Should be " << Rrtr::kBlockLength; | |
158 return; | |
159 } | |
160 rrtr_blocks_.push_back(Rrtr()); | |
161 rrtr_blocks_.back().Parse(block); | |
162 } | |
163 | |
164 void ExtendedReports::ParseDlrrBlock(const uint8_t* block, | |
165 uint16_t block_size) { | |
166 dlrr_blocks_.push_back(Dlrr()); | |
167 if (!dlrr_blocks_.back().Parse(block, block_size)) { | |
168 dlrr_blocks_.pop_back(); | |
169 } | |
170 } | |
171 | |
172 void ExtendedReports::ParseVoipMetricBlock(const uint8_t* block, | |
173 uint16_t block_size) { | |
174 if (block_size != VoipMetric::kBlockLength) { | |
175 LOG(LS_WARNING) << "Incorrect voip metric block size " << block_size | |
176 << " Should be " << VoipMetric::kBlockLength; | |
177 return; | |
178 } | |
179 voip_metric_blocks_.push_back(VoipMetric()); | |
180 voip_metric_blocks_.back().Parse(block); | |
181 } | |
110 } // namespace rtcp | 182 } // namespace rtcp |
111 } // namespace webrtc | 183 } // namespace webrtc |
OLD | NEW |