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/sdes.h" | 11 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h" |
12 | 12 |
| 13 #include "webrtc/base/checks.h" |
13 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
14 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 15 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
15 | 16 |
16 using webrtc::RTCPUtility::PT_SDES; | 17 using webrtc::RTCPUtility::RtcpCommonHeader; |
17 | 18 |
18 namespace webrtc { | 19 namespace webrtc { |
19 namespace rtcp { | 20 namespace rtcp { |
20 namespace { | |
21 void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) { | |
22 buffer[(*offset)++] = value; | |
23 } | |
24 | |
25 void AssignUWord32(uint8_t* buffer, size_t* offset, uint32_t value) { | |
26 ByteWriter<uint32_t>::WriteBigEndian(buffer + *offset, value); | |
27 *offset += 4; | |
28 } | |
29 // Source Description (SDES) (RFC 3550). | 21 // Source Description (SDES) (RFC 3550). |
30 // | 22 // |
31 // 0 1 2 3 | 23 // 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 | 24 // 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 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 25 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
34 // header |V=2|P| SC | PT=SDES=202 | length | | 26 // header |V=2|P| SC | PT=SDES=202 | length | |
35 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | 27 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
36 // chunk | SSRC/CSRC_1 | | 28 // chunk | SSRC/CSRC_1 | |
37 // 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 29 // 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
38 // | SDES items | | 30 // | SDES items | |
39 // | ... | | 31 // | ... | |
40 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | 32 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
41 // chunk | SSRC/CSRC_2 | | 33 // chunk | SSRC/CSRC_2 | |
42 // 2 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 34 // 2 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
43 // | SDES items | | 35 // | SDES items | |
44 // | ... | | 36 // | ... | |
45 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | 37 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
46 // | 38 // |
47 // Canonical End-Point Identifier SDES Item (CNAME) | 39 // Canonical End-Point Identifier SDES Item (CNAME) |
48 // | 40 // |
49 // 0 1 2 3 | 41 // 0 1 2 3 |
50 // 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 | 42 // 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 |
51 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 43 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
52 // | CNAME=1 | length | user and domain name ... | 44 // | CNAME=1 | length | user and domain name ... |
53 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 45 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
54 void CreateSdes(const std::vector<Sdes::Chunk>& chunks, | 46 namespace { |
55 uint8_t* buffer, | 47 const uint8_t kTerminatorTag = 0; |
56 size_t* pos) { | 48 const uint8_t kCnameTag = 1; |
57 const uint8_t kSdesItemType = 1; | 49 |
58 for (std::vector<Sdes::Chunk>::const_iterator it = chunks.begin(); | 50 size_t ChunkSize(const Sdes::Chunk& chunk) { |
59 it != chunks.end(); ++it) { | 51 // Chunk: |
60 AssignUWord32(buffer, pos, (*it).ssrc); | 52 // SSRC/CSRC (4 bytes) | CNAME=1 (1 byte) | length (1 byte) | cname | padding. |
61 AssignUWord8(buffer, pos, kSdesItemType); | 53 size_t chunk_payload_size = 4 + 1 + 1 + chunk.cname.size(); |
62 AssignUWord8(buffer, pos, (*it).name.length()); | 54 size_t padding_size = 4 - (chunk_payload_size % 4); // Minimum 1. |
63 memcpy(buffer + *pos, (*it).name.data(), (*it).name.length()); | 55 return chunk_payload_size + padding_size; |
64 *pos += (*it).name.length(); | |
65 memset(buffer + *pos, 0, (*it).null_octets); | |
66 *pos += (*it).null_octets; | |
67 } | |
68 } | 56 } |
69 } // namespace | 57 } // namespace |
70 | 58 |
| 59 Sdes::Sdes() : block_length_(RtcpPacket::kHeaderLength) {} |
| 60 |
| 61 Sdes::~Sdes() {} |
| 62 |
| 63 bool Sdes::Parse(const RtcpCommonHeader& header, const uint8_t* payload) { |
| 64 RTC_CHECK(header.packet_type == kPacketType); |
| 65 |
| 66 uint8_t number_of_chunks = header.count_or_format; |
| 67 std::vector<Chunk> chunks; // Read chunk into temporary array, so that in |
| 68 // case of an error original array would stay |
| 69 // unchanged. |
| 70 size_t block_length = kHeaderLength; |
| 71 |
| 72 if (header.payload_size_bytes % 4 != 0) { |
| 73 LOG(LS_WARNING) << "Invalid payload size " << header.payload_size_bytes |
| 74 << " bytes for a valid Sdes packet. Size should be" |
| 75 " multiple of 4 bytes"; |
| 76 } |
| 77 const uint8_t* const payload_end = payload + header.payload_size_bytes; |
| 78 const uint8_t* looking_at = payload; |
| 79 chunks.resize(number_of_chunks); |
| 80 for (size_t i = 0; i < number_of_chunks;) { |
| 81 // Each chunk consumes at least 8 bytes. |
| 82 if (payload_end - looking_at < 8) { |
| 83 LOG(LS_WARNING) << "Not enough space left for chunk #" << (i + 1); |
| 84 return false; |
| 85 } |
| 86 chunks[i].ssrc = ByteReader<uint32_t>::ReadBigEndian(looking_at); |
| 87 looking_at += sizeof(uint32_t); |
| 88 bool cname_found = false; |
| 89 |
| 90 uint8_t item_type; |
| 91 while ((item_type = *(looking_at++)) != kTerminatorTag) { |
| 92 if (looking_at >= payload_end) { |
| 93 LOG(LS_WARNING) << "Unexpected end of packet while reading chunk #" |
| 94 << (i + 1) << ". Expected to find size of the text."; |
| 95 return false; |
| 96 } |
| 97 uint8_t item_length = *(looking_at++); |
| 98 const size_t kTerminatorSize = 1; |
| 99 if (looking_at + item_length + kTerminatorSize > payload_end) { |
| 100 LOG(LS_WARNING) << "Unexpected end of packet while reading chunk #" |
| 101 << (i + 1) << ". Expected to find text of size " |
| 102 << item_length; |
| 103 return false; |
| 104 } |
| 105 if (item_type == kCnameTag) { |
| 106 if (cname_found) { |
| 107 LOG(LS_WARNING) << "Found extra CNAME for same ssrc in chunk #" |
| 108 << (i + 1); |
| 109 return false; |
| 110 } |
| 111 cname_found = true; |
| 112 chunks[i].cname.assign(reinterpret_cast<const char*>(looking_at), |
| 113 item_length); |
| 114 } |
| 115 looking_at += item_length; |
| 116 } |
| 117 if (cname_found) { |
| 118 // block_length calculates length of the packet that would be generated by |
| 119 // Build/Create functions. Adjust it same way WithCName function does. |
| 120 block_length += ChunkSize(chunks[i]); |
| 121 ++i; |
| 122 } else { |
| 123 // RFC states CNAME item is mandatory. |
| 124 // But same time it allows chunk without items. |
| 125 // So while parsing, ignore all chunks without cname, |
| 126 // but do not fail the parse. |
| 127 LOG(LS_WARNING) << "CNAME not found for ssrc " << chunks[i].ssrc; |
| 128 --number_of_chunks; |
| 129 chunks.resize(number_of_chunks); |
| 130 } |
| 131 // Adjust to 32bit boundary. |
| 132 looking_at += (payload_end - looking_at) % 4; |
| 133 } |
| 134 |
| 135 chunks_ = std::move(chunks); |
| 136 block_length_ = block_length; |
| 137 return true; |
| 138 } |
| 139 |
| 140 bool Sdes::WithCName(uint32_t ssrc, const std::string& cname) { |
| 141 RTC_DCHECK_LE(cname.length(), 0xffu); |
| 142 if (chunks_.size() >= kMaxNumberOfChunks) { |
| 143 LOG(LS_WARNING) << "Max SDES chunks reached."; |
| 144 return false; |
| 145 } |
| 146 Chunk chunk; |
| 147 chunk.ssrc = ssrc; |
| 148 chunk.cname = cname; |
| 149 chunks_.push_back(chunk); |
| 150 block_length_ += ChunkSize(chunk); |
| 151 return true; |
| 152 } |
| 153 |
71 bool Sdes::Create(uint8_t* packet, | 154 bool Sdes::Create(uint8_t* packet, |
72 size_t* index, | 155 size_t* index, |
73 size_t max_length, | 156 size_t max_length, |
74 RtcpPacket::PacketReadyCallback* callback) const { | 157 RtcpPacket::PacketReadyCallback* callback) const { |
75 assert(!chunks_.empty()); | |
76 while (*index + BlockLength() > max_length) { | 158 while (*index + BlockLength() > max_length) { |
77 if (!OnBufferFull(packet, index, callback)) | 159 if (!OnBufferFull(packet, index, callback)) |
78 return false; | 160 return false; |
79 } | 161 } |
80 CreateHeader(chunks_.size(), PT_SDES, HeaderLength(), packet, index); | 162 const size_t index_end = *index + BlockLength(); |
81 CreateSdes(chunks_, packet, index); | 163 CreateHeader(chunks_.size(), kPacketType, HeaderLength(), packet, index); |
| 164 |
| 165 for (const Sdes::Chunk& chunk : chunks_) { |
| 166 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], chunk.ssrc); |
| 167 ByteWriter<uint8_t>::WriteBigEndian(&packet[*index + 4], kCnameTag); |
| 168 ByteWriter<uint8_t>::WriteBigEndian(&packet[*index + 5], |
| 169 chunk.cname.size()); |
| 170 memcpy(&packet[*index + 6], chunk.cname.data(), chunk.cname.size()); |
| 171 *index += (6 + chunk.cname.size()); |
| 172 |
| 173 // In each chunk, the list of items must be terminated by one or more null |
| 174 // octets. The next chunk must start on a 32-bit boundary. |
| 175 // CNAME (1 byte) | length (1 byte) | name | padding. |
| 176 size_t padding_size = 4 - ((6 + chunk.cname.size()) % 4); |
| 177 const int kPadding = 0; |
| 178 memset(packet + *index, kPadding, padding_size); |
| 179 *index += padding_size; |
| 180 } |
| 181 |
| 182 RTC_CHECK_EQ(*index, index_end); |
82 return true; | 183 return true; |
83 } | 184 } |
84 | |
85 bool Sdes::WithCName(uint32_t ssrc, const std::string& cname) { | |
86 assert(cname.length() <= 0xff); | |
87 if (chunks_.size() >= kMaxNumberOfChunks) { | |
88 LOG(LS_WARNING) << "Max SDES chunks reached."; | |
89 return false; | |
90 } | |
91 // In each chunk, the list of items must be terminated by one or more null | |
92 // octets. The next chunk must start on a 32-bit boundary. | |
93 // CNAME (1 byte) | length (1 byte) | name | padding. | |
94 int null_octets = 4 - ((2 + cname.length()) % 4); | |
95 Chunk chunk; | |
96 chunk.ssrc = ssrc; | |
97 chunk.name = cname; | |
98 chunk.null_octets = null_octets; | |
99 chunks_.push_back(chunk); | |
100 return true; | |
101 } | |
102 | |
103 size_t Sdes::BlockLength() const { | |
104 // Header (4 bytes). | |
105 // Chunk: | |
106 // SSRC/CSRC (4 bytes) | CNAME (1 byte) | length (1 byte) | name | padding. | |
107 size_t length = kHeaderLength; | |
108 for (const Chunk& chunk : chunks_) | |
109 length += 6 + chunk.name.length() + chunk.null_octets; | |
110 assert(length % 4 == 0); | |
111 return length; | |
112 } | |
113 } // namespace rtcp | 185 } // namespace rtcp |
114 } // namespace webrtc | 186 } // namespace webrtc |
OLD | NEW |