OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 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.h" | 11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h" |
12 | 12 |
13 #include <algorithm> | |
14 | |
15 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
16 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
17 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
18 | 16 |
19 using webrtc::RTCPUtility::PT_APP; | |
20 using webrtc::RTCPUtility::PT_IJ; | |
21 using webrtc::RTCPUtility::PT_RTPFB; | |
22 using webrtc::RTCPUtility::PT_SR; | |
23 | |
24 using webrtc::RTCPUtility::RTCPPacketAPP; | |
25 using webrtc::RTCPUtility::RTCPPacketReportBlockItem; | |
26 using webrtc::RTCPUtility::RTCPPacketRTPFBNACK; | |
27 using webrtc::RTCPUtility::RTCPPacketRTPFBNACKItem; | |
28 using webrtc::RTCPUtility::RTCPPacketSR; | |
29 | |
30 namespace webrtc { | 17 namespace webrtc { |
31 namespace rtcp { | 18 namespace rtcp { |
32 namespace { | 19 namespace { |
33 void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) { | 20 void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) { |
34 buffer[(*offset)++] = value; | 21 buffer[(*offset)++] = value; |
35 } | 22 } |
36 void AssignUWord16(uint8_t* buffer, size_t* offset, uint16_t value) { | 23 void AssignUWord16(uint8_t* buffer, size_t* offset, uint16_t value) { |
37 ByteWriter<uint16_t>::WriteBigEndian(buffer + *offset, value); | 24 ByteWriter<uint16_t>::WriteBigEndian(buffer + *offset, value); |
38 *offset += 2; | 25 *offset += 2; |
39 } | 26 } |
40 void AssignUWord32(uint8_t* buffer, size_t* offset, uint32_t value) { | |
41 ByteWriter<uint32_t>::WriteBigEndian(buffer + *offset, value); | |
42 *offset += 4; | |
43 } | |
44 | |
45 // Sender report (SR) (RFC 3550). | |
46 // 0 1 2 3 | |
47 // 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 | |
48 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
49 // |V=2|P| RC | PT=SR=200 | length | | |
50 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
51 // | SSRC of sender | | |
52 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | |
53 // | NTP timestamp, most significant word | | |
54 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
55 // | NTP timestamp, least significant word | | |
56 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
57 // | RTP timestamp | | |
58 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
59 // | sender's packet count | | |
60 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
61 // | sender's octet count | | |
62 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | |
63 | |
64 void CreateSenderReport(const RTCPPacketSR& sr, | |
65 uint8_t* buffer, | |
66 size_t* pos) { | |
67 AssignUWord32(buffer, pos, sr.SenderSSRC); | |
68 AssignUWord32(buffer, pos, sr.NTPMostSignificant); | |
69 AssignUWord32(buffer, pos, sr.NTPLeastSignificant); | |
70 AssignUWord32(buffer, pos, sr.RTPTimestamp); | |
71 AssignUWord32(buffer, pos, sr.SenderPacketCount); | |
72 AssignUWord32(buffer, pos, sr.SenderOctetCount); | |
73 } | |
74 | |
75 // Report block (RFC 3550). | |
76 // | |
77 // 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 | |
78 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | |
79 // | SSRC_1 (SSRC of first source) | | |
80 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
81 // | fraction lost | cumulative number of packets lost | | |
82 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
83 // | extended highest sequence number received | | |
84 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
85 // | interarrival jitter | | |
86 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
87 // | last SR (LSR) | | |
88 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
89 // | delay since last SR (DLSR) | | |
90 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | |
91 | |
92 void CreateReportBlocks(const std::vector<ReportBlock>& blocks, | |
93 uint8_t* buffer, | |
94 size_t* pos) { | |
95 for (const ReportBlock& block : blocks) { | |
96 block.Create(buffer + *pos); | |
97 *pos += ReportBlock::kLength; | |
98 } | |
99 } | |
100 } // namespace | 27 } // namespace |
101 | 28 |
102 void RtcpPacket::Append(RtcpPacket* packet) { | 29 void RtcpPacket::Append(RtcpPacket* packet) { |
103 assert(packet); | 30 assert(packet); |
104 appended_packets_.push_back(packet); | 31 appended_packets_.push_back(packet); |
105 } | 32 } |
106 | 33 |
107 rtc::scoped_ptr<RawPacket> RtcpPacket::Build() const { | 34 rtc::scoped_ptr<RawPacket> RtcpPacket::Build() const { |
108 size_t length = 0; | 35 size_t length = 0; |
109 rtc::scoped_ptr<RawPacket> packet(new RawPacket(IP_PACKET_SIZE)); | 36 rtc::scoped_ptr<RawPacket> packet(new RawPacket(IP_PACKET_SIZE)); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 size_t length, | 115 size_t length, |
189 uint8_t* buffer, | 116 uint8_t* buffer, |
190 size_t* pos) { | 117 size_t* pos) { |
191 assert(length <= 0xffff); | 118 assert(length <= 0xffff); |
192 const uint8_t kVersion = 2; | 119 const uint8_t kVersion = 2; |
193 AssignUWord8(buffer, pos, (kVersion << 6) + count_or_format); | 120 AssignUWord8(buffer, pos, (kVersion << 6) + count_or_format); |
194 AssignUWord8(buffer, pos, packet_type); | 121 AssignUWord8(buffer, pos, packet_type); |
195 AssignUWord16(buffer, pos, length); | 122 AssignUWord16(buffer, pos, length); |
196 } | 123 } |
197 | 124 |
198 bool SenderReport::Create(uint8_t* packet, | |
199 size_t* index, | |
200 size_t max_length, | |
201 RtcpPacket::PacketReadyCallback* callback) const { | |
202 while (*index + BlockLength() > max_length) { | |
203 if (!OnBufferFull(packet, index, callback)) | |
204 return false; | |
205 } | |
206 CreateHeader(sr_.NumberOfReportBlocks, PT_SR, HeaderLength(), packet, index); | |
207 CreateSenderReport(sr_, packet, index); | |
208 CreateReportBlocks(report_blocks_, packet, index); | |
209 return true; | |
210 } | |
211 | |
212 bool SenderReport::WithReportBlock(const ReportBlock& block) { | |
213 if (report_blocks_.size() >= kMaxNumberOfReportBlocks) { | |
214 LOG(LS_WARNING) << "Max report blocks reached."; | |
215 return false; | |
216 } | |
217 report_blocks_.push_back(block); | |
218 sr_.NumberOfReportBlocks = report_blocks_.size(); | |
219 return true; | |
220 } | |
221 | |
222 RawPacket::RawPacket(size_t buffer_length) | 125 RawPacket::RawPacket(size_t buffer_length) |
223 : buffer_length_(buffer_length), length_(0) { | 126 : buffer_length_(buffer_length), length_(0) { |
224 buffer_.reset(new uint8_t[buffer_length]); | 127 buffer_.reset(new uint8_t[buffer_length]); |
225 } | 128 } |
226 | 129 |
227 RawPacket::RawPacket(const uint8_t* packet, size_t packet_length) | 130 RawPacket::RawPacket(const uint8_t* packet, size_t packet_length) |
228 : buffer_length_(packet_length), length_(packet_length) { | 131 : buffer_length_(packet_length), length_(packet_length) { |
229 buffer_.reset(new uint8_t[packet_length]); | 132 buffer_.reset(new uint8_t[packet_length]); |
230 memcpy(buffer_.get(), packet, packet_length); | 133 memcpy(buffer_.get(), packet, packet_length); |
231 } | 134 } |
(...skipping 14 matching lines...) Expand all Loading... |
246 return length_; | 149 return length_; |
247 } | 150 } |
248 | 151 |
249 void RawPacket::SetLength(size_t length) { | 152 void RawPacket::SetLength(size_t length) { |
250 assert(length <= buffer_length_); | 153 assert(length <= buffer_length_); |
251 length_ = length; | 154 length_ = length; |
252 } | 155 } |
253 | 156 |
254 } // namespace rtcp | 157 } // namespace rtcp |
255 } // namespace webrtc | 158 } // namespace webrtc |
OLD | NEW |