Chromium Code Reviews

Side by Side Diff: webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.cc

Issue 1557593002: [rtp_rtcp] rtcp::ExtenededReports packet got Parse function (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: rebase & fix win compile errors Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
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 // Extended 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 // : type-specific block contents :
42 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 ExtendedReports::ExtendedReports() : sender_ssrc_(0) {}
44 ExtendedReports::~ExtendedReports() {}
45
46 bool ExtendedReports::Parse(const RtcpCommonHeader& header,
47 const uint8_t* payload) {
48 RTC_CHECK(header.packet_type == kPacketType);
49
50 if (header.payload_size_bytes < kXrBaseLength) {
51 LOG(LS_WARNING) << "Packet is too small to be an ExtendedReports packet.";
52 return false;
53 }
54
55 sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(payload);
56 rrtr_blocks_.clear();
57 dlrr_blocks_.clear();
58 voip_metric_blocks_.clear();
59
60 const uint8_t* current_block = payload + kXrBaseLength;
61 const uint8_t* const packet_end = payload + header.payload_size_bytes;
62 const size_t kBlockHeaderSizeBytes = 4;
63 while (current_block + kBlockHeaderSizeBytes <= packet_end) {
64 uint8_t block_type = ByteReader<uint8_t>::ReadBigEndian(current_block);
65 uint16_t block_length =
66 ByteReader<uint16_t>::ReadBigEndian(current_block + 2);
67 const uint8_t* next_block =
68 current_block + kBlockHeaderSizeBytes + block_length * 4;
69 if (next_block > packet_end) {
70 LOG(LS_WARNING) << "Report block in extended report packet is too big.";
71 return false;
72 }
73 switch (block_type) {
74 case Rrtr::kBlockType:
75 ParseRrtrBlock(current_block, block_length);
76 break;
77 case Dlrr::kBlockType:
78 ParseDlrrBlock(current_block, block_length);
79 break;
80 case VoipMetric::kBlockType:
81 ParseVoipMetricBlock(current_block, block_length);
82 break;
83 default:
84 // Unknown block, ignore.
85 LOG(LS_WARNING) << "Unknown extended report block type " << block_type;
86 break;
87 }
88 current_block = next_block;
89 }
90
91 return true;
44 } 92 }
45 } // namespace 93
94 bool ExtendedReports::WithRrtr(const Rrtr& rrtr) {
95 if (rrtr_blocks_.size() >= kMaxNumberOfRrtrBlocks) {
96 LOG(LS_WARNING) << "Max RRTR blocks reached.";
97 return false;
98 }
99 rrtr_blocks_.push_back(rrtr);
100 return true;
101 }
102
103 bool ExtendedReports::WithDlrr(const Dlrr& dlrr) {
104 if (dlrr_blocks_.size() >= kMaxNumberOfDlrrBlocks) {
105 LOG(LS_WARNING) << "Max DLRR blocks reached.";
106 return false;
107 }
108 dlrr_blocks_.push_back(dlrr);
109 return true;
110 }
111
112 bool ExtendedReports::WithVoipMetric(const VoipMetric& voip_metric) {
113 if (voip_metric_blocks_.size() >= kMaxNumberOfVoipMetricBlocks) {
114 LOG(LS_WARNING) << "Max Voip Metric blocks reached.";
115 return false;
116 }
117 voip_metric_blocks_.push_back(voip_metric);
118 return true;
119 }
46 120
47 bool ExtendedReports::Create(uint8_t* packet, 121 bool ExtendedReports::Create(uint8_t* packet,
48 size_t* index, 122 size_t* index,
49 size_t max_length, 123 size_t max_length,
50 RtcpPacket::PacketReadyCallback* callback) const { 124 RtcpPacket::PacketReadyCallback* callback) const {
51 while (*index + BlockLength() > max_length) { 125 while (*index + BlockLength() > max_length) {
52 if (!OnBufferFull(packet, index, callback)) 126 if (!OnBufferFull(packet, index, callback))
53 return false; 127 return false;
54 } 128 }
55 CreateHeader(0U, PT_XR, HeaderLength(), packet, index); 129 size_t index_end = *index + BlockLength();
56 CreateXrHeader(xr_header_, packet, index); 130 const uint8_t kReserved = 0;
131 CreateHeader(kReserved, kPacketType, HeaderLength(), packet, index);
132 ByteWriter<uint32_t>::WriteBigEndian(packet + *index, sender_ssrc_);
133 *index += sizeof(uint32_t);
57 for (const Rrtr& block : rrtr_blocks_) { 134 for (const Rrtr& block : rrtr_blocks_) {
58 block.Create(packet + *index); 135 block.Create(packet + *index);
59 *index += Rrtr::kLength; 136 *index += Rrtr::kLength;
60 } 137 }
61 for (const Dlrr& block : dlrr_blocks_) { 138 for (const Dlrr& block : dlrr_blocks_) {
62 block.Create(packet + *index); 139 block.Create(packet + *index);
63 *index += block.BlockLength(); 140 *index += block.BlockLength();
64 } 141 }
65 for (const VoipMetric& block : voip_metric_blocks_) { 142 for (const VoipMetric& block : voip_metric_blocks_) {
66 block.Create(packet + *index); 143 block.Create(packet + *index);
67 *index += VoipMetric::kLength; 144 *index += VoipMetric::kLength;
68 } 145 }
146 RTC_CHECK_EQ(*index, index_end);
69 return true; 147 return true;
70 } 148 }
71 149
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 { 150 size_t ExtendedReports::DlrrLength() const {
103 size_t length = 0; 151 size_t length = 0;
104 for (const Dlrr& block : dlrr_blocks_) { 152 for (const Dlrr& block : dlrr_blocks_) {
105 length += block.BlockLength(); 153 length += block.BlockLength();
106 } 154 }
107 return length; 155 return length;
108 } 156 }
109 157
158 void ExtendedReports::ParseRrtrBlock(const uint8_t* block,
159 uint16_t block_length) {
160 if (block_length != Rrtr::kBlockLength) {
161 LOG(LS_WARNING) << "Incorrect rrtr block size " << block_length
162 << " Should be " << Rrtr::kBlockLength;
163 return;
164 }
165 rrtr_blocks_.push_back(Rrtr());
166 rrtr_blocks_.back().Parse(block);
167 }
168
169 void ExtendedReports::ParseDlrrBlock(const uint8_t* block,
170 uint16_t block_length) {
171 dlrr_blocks_.push_back(Dlrr());
172 if (!dlrr_blocks_.back().Parse(block, block_length)) {
173 dlrr_blocks_.pop_back();
174 }
175 }
176
177 void ExtendedReports::ParseVoipMetricBlock(const uint8_t* block,
178 uint16_t block_length) {
179 if (block_length != VoipMetric::kBlockLength) {
180 LOG(LS_WARNING) << "Incorrect voip metric block size " << block_length
181 << " Should be " << VoipMetric::kBlockLength;
182 return;
183 }
184 voip_metric_blocks_.push_back(VoipMetric());
185 voip_metric_blocks_.back().Parse(block);
186 }
110 } // namespace rtcp 187 } // namespace rtcp
111 } // namespace webrtc 188 } // namespace webrtc
OLDNEW

Powered by Google App Engine