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 "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 #include "webrtc/base/logging.h" | |
15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | |
16 | 14 |
17 namespace webrtc { | 15 namespace webrtc { |
18 namespace rtcp { | 16 namespace rtcp { |
19 namespace { | 17 constexpr size_t RtcpPacket::kHeaderLength; |
20 void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) { | |
21 buffer[(*offset)++] = value; | |
22 } | |
23 void AssignUWord16(uint8_t* buffer, size_t* offset, uint16_t value) { | |
24 ByteWriter<uint16_t>::WriteBigEndian(buffer + *offset, value); | |
25 *offset += 2; | |
26 } | |
27 } // namespace | |
28 | 18 |
29 rtc::Buffer RtcpPacket::Build() const { | 19 rtc::Buffer RtcpPacket::Build() const { |
| 20 rtc::Buffer packet(BlockLength()); |
| 21 |
30 size_t length = 0; | 22 size_t length = 0; |
31 rtc::Buffer packet(IP_PACKET_SIZE); | 23 bool created = Create(packet.data(), &length, packet.capacity(), nullptr); |
| 24 RTC_DCHECK(created) << "Invalid packet is not supported."; |
| 25 RTC_DCHECK_EQ(length, packet.size()) |
| 26 << "BlockLength mispredicted size used by Create"; |
32 | 27 |
33 class PacketVerifier : public PacketReadyCallback { | |
34 public: | |
35 explicit PacketVerifier(rtc::Buffer* packet) | |
36 : called_(false), packet_(packet) {} | |
37 virtual ~PacketVerifier() {} | |
38 void OnPacketReady(uint8_t* data, size_t length) override { | |
39 RTC_CHECK(!called_) << "Fragmentation not supported."; | |
40 called_ = true; | |
41 packet_->SetSize(length); | |
42 } | |
43 | |
44 private: | |
45 bool called_; | |
46 rtc::Buffer* const packet_; | |
47 } verifier(&packet); | |
48 Create(packet.data(), &length, packet.capacity(), &verifier); | |
49 OnBufferFull(packet.data(), &length, &verifier); | |
50 return packet; | 28 return packet; |
51 } | 29 } |
52 | 30 |
53 bool RtcpPacket::Build(PacketReadyCallback* callback) const { | |
54 uint8_t buffer[IP_PACKET_SIZE]; | |
55 return BuildExternalBuffer(buffer, IP_PACKET_SIZE, callback); | |
56 } | |
57 | |
58 bool RtcpPacket::BuildExternalBuffer(uint8_t* buffer, | 31 bool RtcpPacket::BuildExternalBuffer(uint8_t* buffer, |
59 size_t max_length, | 32 size_t max_length, |
60 PacketReadyCallback* callback) const { | 33 PacketReadyCallback* callback) const { |
61 size_t index = 0; | 34 size_t index = 0; |
62 if (!Create(buffer, &index, max_length, callback)) | 35 if (!Create(buffer, &index, max_length, callback)) |
63 return false; | 36 return false; |
64 return OnBufferFull(buffer, &index, callback); | 37 return OnBufferFull(buffer, &index, callback); |
65 } | 38 } |
66 | 39 |
67 bool RtcpPacket::OnBufferFull(uint8_t* packet, | 40 bool RtcpPacket::OnBufferFull(uint8_t* packet, |
68 size_t* index, | 41 size_t* index, |
69 RtcpPacket::PacketReadyCallback* callback) const { | 42 PacketReadyCallback* callback) const { |
70 if (*index == 0) | 43 if (*index == 0) |
71 return false; | 44 return false; |
| 45 RTC_DCHECK(callback) << "Fragmentation not supported."; |
72 callback->OnPacketReady(packet, *index); | 46 callback->OnPacketReady(packet, *index); |
73 *index = 0; | 47 *index = 0; |
74 return true; | 48 return true; |
75 } | 49 } |
76 | 50 |
77 size_t RtcpPacket::HeaderLength() const { | 51 size_t RtcpPacket::HeaderLength() const { |
78 size_t length_in_bytes = BlockLength(); | 52 size_t length_in_bytes = BlockLength(); |
79 // Length in 32-bit words minus 1. | 53 RTC_DCHECK_GT(length_in_bytes, 0u); |
80 assert(length_in_bytes > 0); | 54 RTC_DCHECK_EQ(length_in_bytes % 4, 0u) << "Padding not supported"; |
81 return ((length_in_bytes + 3) / 4) - 1; | 55 // Length in 32-bit words without common header. |
| 56 return (length_in_bytes - kHeaderLength) / 4; |
82 } | 57 } |
83 | 58 |
84 // From RFC 3550, RTP: A Transport Protocol for Real-Time Applications. | 59 // From RFC 3550, RTP: A Transport Protocol for Real-Time Applications. |
85 // | 60 // |
86 // RTP header format. | 61 // RTP header format. |
87 // 0 1 2 3 | 62 // 0 1 2 3 |
88 // 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 | 63 // 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 |
89 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 64 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
90 // |V=2|P| RC/FMT | PT | length | | 65 // |V=2|P| RC/FMT | PT | length | |
91 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 66 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
92 | |
93 void RtcpPacket::CreateHeader( | 67 void RtcpPacket::CreateHeader( |
94 uint8_t count_or_format, // Depends on packet type. | 68 uint8_t count_or_format, // Depends on packet type. |
95 uint8_t packet_type, | 69 uint8_t packet_type, |
96 size_t length, | 70 size_t length, |
97 uint8_t* buffer, | 71 uint8_t* buffer, |
98 size_t* pos) { | 72 size_t* pos) { |
99 assert(length <= 0xffff); | 73 RTC_DCHECK_LE(length, 0xffffU); |
100 const uint8_t kVersion = 2; | 74 RTC_DCHECK_LE(count_or_format, 0x1f); |
101 AssignUWord8(buffer, pos, (kVersion << 6) + count_or_format); | 75 constexpr uint8_t kVersionBits = 2 << 6; |
102 AssignUWord8(buffer, pos, packet_type); | 76 constexpr uint8_t kNoPaddingBit = 0 << 5; |
103 AssignUWord16(buffer, pos, length); | 77 buffer[*pos + 0] = kVersionBits | kNoPaddingBit | count_or_format; |
| 78 buffer[*pos + 1] = packet_type; |
| 79 buffer[*pos + 2] = (length >> 8) & 0xff; |
| 80 buffer[*pos + 3] = length & 0xff; |
| 81 *pos += kHeaderLength; |
104 } | 82 } |
105 | 83 |
106 } // namespace rtcp | 84 } // namespace rtcp |
107 } // namespace webrtc | 85 } // namespace webrtc |
OLD | NEW |