Chromium Code Reviews| 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 size_t ChunkSize(const Sdes::Chunk& chunk) { |
| 56 size_t* pos) { | 48 // Chunk: |
| 57 const uint8_t kSdesItemType = 1; | 49 // SSRC/CSRC (4 bytes) | CNAME=1 (1 byte) | length (1 byte) | cname | padding. |
| 58 for (std::vector<Sdes::Chunk>::const_iterator it = chunks.begin(); | 50 size_t chunk_payload_size = 4 + 1 + 1 + chunk.cname.size(); |
| 59 it != chunks.end(); ++it) { | 51 size_t padding_size = 4 - (chunk_payload_size % 4); // Minimum 1. |
| 60 AssignUWord32(buffer, pos, (*it).ssrc); | 52 return chunk_payload_size + padding_size; |
| 61 AssignUWord8(buffer, pos, kSdesItemType); | |
| 62 AssignUWord8(buffer, pos, (*it).name.length()); | |
| 63 memcpy(buffer + *pos, (*it).name.data(), (*it).name.length()); | |
| 64 *pos += (*it).name.length(); | |
| 65 memset(buffer + *pos, 0, (*it).null_octets); | |
| 66 *pos += (*it).null_octets; | |
| 67 } | |
| 68 } | 53 } |
| 69 } // namespace | 54 } // namespace |
| 70 | 55 |
| 56 Sdes::Sdes() : block_length_(RtcpPacket::kHeaderLength) {} | |
| 57 | |
| 58 Sdes::~Sdes() {} | |
| 59 | |
| 60 bool Sdes::Parse(const RtcpCommonHeader& header, const uint8_t* payload) { | |
| 61 RTC_CHECK(header.packet_type == kPacketType); | |
| 62 | |
| 63 const uint8_t number_of_chunks = header.count_or_format; | |
| 64 std::vector<Chunk> chunks; // Read chunk into temporary array, so that in | |
| 65 // case of an error original array would stay | |
| 66 // unchanged. | |
| 67 size_t block_length = kHeaderLength; | |
| 68 | |
| 69 if (header.payload_size_bytes % 4 != 0) { | |
| 70 LOG(LS_WARNING) << "Invalid payload size " << header.payload_size_bytes | |
| 71 << " bytes for a valid Sdes packet. Size should be" | |
| 72 " multiple of 4 bytes"; | |
| 73 } | |
| 74 const uint8_t* const payload_end = payload + header.payload_size_bytes; | |
| 75 const uint8_t* looking_at = payload; | |
| 76 chunks.resize(number_of_chunks); | |
| 77 for (size_t i = 0; i < number_of_chunks; ++i) { | |
| 78 // Each chunk consumes at least 8 bytes. | |
| 79 if (payload_end - looking_at < 8) { | |
| 80 LOG(LS_WARNING) << "Not enough space left for chunk #" << (i + 1); | |
| 81 return false; | |
| 82 } | |
| 83 chunks[i].ssrc = ByteReader<uint32_t>::ReadBigEndian(looking_at); | |
| 84 looking_at += sizeof(uint32_t); | |
| 85 bool cname_found = false; | |
| 86 | |
| 87 const uint8_t kTerminateItemType = 0; | |
| 88 const uint8_t kCnameItemType = 1; | |
|
åsapersson
2016/02/11 10:20:32
use same name here and in Create (kCnameId)
danilchap
2016/02/11 11:13:37
Using now same name here, in Create and in unittes
| |
| 89 uint8_t item_type; | |
| 90 while ((item_type = *(looking_at++)) != kTerminateItemType) { | |
| 91 if (looking_at >= payload_end) { | |
| 92 LOG(LS_WARNING) << "Unexpected end of packet while reading chunk #" | |
| 93 << (i + 1) << ". Expected to find size of the text."; | |
| 94 return false; | |
| 95 } | |
| 96 uint8_t size = *(looking_at++); | |
|
åsapersson
2016/02/11 10:20:32
maybe call length as in figure
danilchap
2016/02/11 11:13:37
Done.
| |
| 97 if (looking_at + size + 1 > payload_end) { | |
|
åsapersson
2016/02/11 10:20:32
maybe looking_at + size >= payload_end
danilchap
2016/02/11 11:13:37
+1 has a different meaning here. Explained with a
| |
| 98 LOG(LS_WARNING) << "Unexpected end of packet while reading chunk #" | |
| 99 << (i + 1) << ". Expected to find text of size " | |
| 100 << size; | |
| 101 return false; | |
| 102 } | |
| 103 if (item_type == kCnameItemType) { | |
| 104 if (cname_found) { | |
| 105 LOG(LS_WARNING) << "Found extra CNAME for same ssrc in chunk #" | |
| 106 << (i + 1); | |
| 107 return false; | |
| 108 } | |
| 109 cname_found = true; | |
| 110 chunks[i].cname.assign(reinterpret_cast<const char*>(looking_at), size); | |
| 111 } | |
| 112 looking_at += size; | |
| 113 } | |
|
åsapersson
2016/02/11 10:20:32
is a chuck with zero items valid?
danilchap
2016/02/11 11:13:37
Chunk need to have a cname. chunk with zero items
| |
| 114 if (!cname_found) { | |
| 115 LOG(LS_WARNING) << "CNAME not found for chunk #" << (i + 1); | |
| 116 return false; | |
| 117 } | |
| 118 block_length += ChunkSize(chunks[i]); | |
|
åsapersson
2016/02/11 10:20:33
is block_length supposed to not include non-cname
danilchap
2016/02/11 11:13:37
block_length is for packing Sdes back to byte arra
åsapersson
2016/02/11 12:28:45
Maybe add a comment.
danilchap
2016/02/11 12:48:47
Done.
| |
| 119 // Adjust to 32bit boundary. | |
| 120 looking_at += (payload_end - looking_at) % 4; | |
| 121 } | |
| 122 | |
| 123 chunks_ = std::move(chunks); | |
| 124 block_length_ = block_length; | |
| 125 return true; | |
| 126 } | |
| 127 | |
| 128 bool Sdes::WithCName(uint32_t ssrc, const std::string& cname) { | |
| 129 RTC_DCHECK_LE(cname.length(), 0xffu); | |
| 130 if (chunks_.size() >= kMaxNumberOfChunks) { | |
| 131 LOG(LS_WARNING) << "Max SDES chunks reached."; | |
| 132 return false; | |
| 133 } | |
| 134 Chunk chunk; | |
| 135 chunk.ssrc = ssrc; | |
| 136 chunk.cname = cname; | |
| 137 chunks_.push_back(chunk); | |
| 138 block_length_ += ChunkSize(chunk); | |
| 139 return true; | |
| 140 } | |
| 141 | |
| 71 bool Sdes::Create(uint8_t* packet, | 142 bool Sdes::Create(uint8_t* packet, |
| 72 size_t* index, | 143 size_t* index, |
| 73 size_t max_length, | 144 size_t max_length, |
| 74 RtcpPacket::PacketReadyCallback* callback) const { | 145 RtcpPacket::PacketReadyCallback* callback) const { |
| 75 assert(!chunks_.empty()); | 146 RTC_DCHECK(!chunks_.empty()); |
|
åsapersson
2016/02/11 10:20:32
zero is valid right? maybe remove
danilchap
2016/02/11 11:13:37
Done.
| |
| 76 while (*index + BlockLength() > max_length) { | 147 while (*index + BlockLength() > max_length) { |
| 77 if (!OnBufferFull(packet, index, callback)) | 148 if (!OnBufferFull(packet, index, callback)) |
| 78 return false; | 149 return false; |
| 79 } | 150 } |
| 80 CreateHeader(chunks_.size(), PT_SDES, HeaderLength(), packet, index); | 151 const size_t index_end = *index + BlockLength(); |
| 81 CreateSdes(chunks_, packet, index); | 152 CreateHeader(chunks_.size(), kPacketType, HeaderLength(), packet, index); |
| 153 | |
| 154 const uint8_t kCnameId = 1; | |
| 155 for (const Sdes::Chunk& chunk : chunks_) { | |
| 156 ByteWriter<uint32_t>::WriteBigEndian(&packet[*index + 0], chunk.ssrc); | |
| 157 ByteWriter<uint8_t>::WriteBigEndian(&packet[*index + 4], kCnameId); | |
| 158 ByteWriter<uint8_t>::WriteBigEndian(&packet[*index + 5], | |
| 159 chunk.cname.size()); | |
| 160 memcpy(&packet[*index + 6], chunk.cname.data(), chunk.cname.size()); | |
| 161 *index += (6 + chunk.cname.size()); | |
| 162 | |
| 163 // In each chunk, the list of items must be terminated by one or more null | |
| 164 // octets. The next chunk must start on a 32-bit boundary. | |
| 165 // CNAME (1 byte) | length (1 byte) | name | padding. | |
| 166 size_t padding_size = 4 - ((6 + chunk.cname.size()) % 4); | |
| 167 const int kPadding = 0; | |
| 168 memset(packet + *index, kPadding, padding_size); | |
| 169 *index += padding_size; | |
| 170 } | |
| 171 | |
| 172 RTC_CHECK_EQ(*index, index_end); | |
| 82 return true; | 173 return true; |
| 83 } | 174 } |
| 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 | 175 } // namespace rtcp |
| 114 } // namespace webrtc | 176 } // namespace webrtc |
| OLD | NEW |