asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h" |
| 12 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 13 | #include "webrtc/base/checks.h" |
Peter Boström | ebc0b4e | 2015-10-28 16:39:33 +0100 | [diff] [blame] | 14 | #include "webrtc/base/logging.h" |
sprang@webrtc.org | 779c3d1 | 2015-03-17 16:42:49 +0000 | [diff] [blame] | 15 | #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace rtcp { |
| 19 | namespace { |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 20 | void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) { |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 21 | buffer[(*offset)++] = value; |
| 22 | } |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 23 | void AssignUWord16(uint8_t* buffer, size_t* offset, uint16_t value) { |
sprang@webrtc.org | 779c3d1 | 2015-03-17 16:42:49 +0000 | [diff] [blame] | 24 | ByteWriter<uint16_t>::WriteBigEndian(buffer + *offset, value); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 25 | *offset += 2; |
| 26 | } |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 27 | } // namespace |
| 28 | |
| 29 | void RtcpPacket::Append(RtcpPacket* packet) { |
| 30 | assert(packet); |
| 31 | appended_packets_.push_back(packet); |
| 32 | } |
| 33 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 34 | rtc::scoped_ptr<RawPacket> RtcpPacket::Build() const { |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 35 | size_t length = 0; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 36 | rtc::scoped_ptr<RawPacket> packet(new RawPacket(IP_PACKET_SIZE)); |
| 37 | |
| 38 | class PacketVerifier : public PacketReadyCallback { |
| 39 | public: |
| 40 | explicit PacketVerifier(RawPacket* packet) |
| 41 | : called_(false), packet_(packet) {} |
| 42 | virtual ~PacketVerifier() {} |
| 43 | void OnPacketReady(uint8_t* data, size_t length) override { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 44 | RTC_CHECK(!called_) << "Fragmentation not supported."; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 45 | called_ = true; |
| 46 | packet_->SetLength(length); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | bool called_; |
| 51 | RawPacket* const packet_; |
| 52 | } verifier(packet.get()); |
| 53 | CreateAndAddAppended(packet->MutableBuffer(), &length, packet->BufferLength(), |
| 54 | &verifier); |
| 55 | OnBufferFull(packet->MutableBuffer(), &length, &verifier); |
| 56 | return packet; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 59 | bool RtcpPacket::Build(PacketReadyCallback* callback) const { |
| 60 | uint8_t buffer[IP_PACKET_SIZE]; |
| 61 | return BuildExternalBuffer(buffer, IP_PACKET_SIZE, callback); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 64 | bool RtcpPacket::BuildExternalBuffer(uint8_t* buffer, |
| 65 | size_t max_length, |
| 66 | PacketReadyCallback* callback) const { |
| 67 | size_t index = 0; |
| 68 | if (!CreateAndAddAppended(buffer, &index, max_length, callback)) |
| 69 | return false; |
| 70 | return OnBufferFull(buffer, &index, callback); |
| 71 | } |
| 72 | |
| 73 | bool RtcpPacket::CreateAndAddAppended(uint8_t* packet, |
| 74 | size_t* index, |
| 75 | size_t max_length, |
| 76 | PacketReadyCallback* callback) const { |
| 77 | if (!Create(packet, index, max_length, callback)) |
| 78 | return false; |
| 79 | for (RtcpPacket* appended : appended_packets_) { |
| 80 | if (!appended->CreateAndAddAppended(packet, index, max_length, callback)) |
| 81 | return false; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 82 | } |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 83 | return true; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 86 | bool RtcpPacket::OnBufferFull(uint8_t* packet, |
| 87 | size_t* index, |
| 88 | RtcpPacket::PacketReadyCallback* callback) const { |
| 89 | if (*index == 0) |
| 90 | return false; |
| 91 | callback->OnPacketReady(packet, *index); |
| 92 | *index = 0; |
| 93 | return true; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 96 | size_t RtcpPacket::HeaderLength() const { |
| 97 | size_t length_in_bytes = BlockLength(); |
| 98 | // Length in 32-bit words minus 1. |
| 99 | assert(length_in_bytes > 0); |
| 100 | return ((length_in_bytes + 3) / 4) - 1; |
| 101 | } |
| 102 | |
| 103 | // From RFC 3550, RTP: A Transport Protocol for Real-Time Applications. |
| 104 | // |
| 105 | // RTP header format. |
| 106 | // 0 1 2 3 |
| 107 | // 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 |
| 108 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 109 | // |V=2|P| RC/FMT | PT | length | |
| 110 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 111 | |
| 112 | void RtcpPacket::CreateHeader( |
| 113 | uint8_t count_or_format, // Depends on packet type. |
| 114 | uint8_t packet_type, |
| 115 | size_t length, |
| 116 | uint8_t* buffer, |
sprang | 73a93e8 | 2015-09-14 12:50:39 -0700 | [diff] [blame] | 117 | size_t* pos) { |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 118 | assert(length <= 0xffff); |
| 119 | const uint8_t kVersion = 2; |
| 120 | AssignUWord8(buffer, pos, (kVersion << 6) + count_or_format); |
| 121 | AssignUWord8(buffer, pos, packet_type); |
| 122 | AssignUWord16(buffer, pos, length); |
| 123 | } |
| 124 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 125 | RawPacket::RawPacket(size_t buffer_length) |
| 126 | : buffer_length_(buffer_length), length_(0) { |
| 127 | buffer_.reset(new uint8_t[buffer_length]); |
| 128 | } |
| 129 | |
| 130 | RawPacket::RawPacket(const uint8_t* packet, size_t packet_length) |
| 131 | : buffer_length_(packet_length), length_(packet_length) { |
| 132 | buffer_.reset(new uint8_t[packet_length]); |
| 133 | memcpy(buffer_.get(), packet, packet_length); |
| 134 | } |
| 135 | |
| 136 | const uint8_t* RawPacket::Buffer() const { |
| 137 | return buffer_.get(); |
| 138 | } |
| 139 | |
| 140 | uint8_t* RawPacket::MutableBuffer() { |
| 141 | return buffer_.get(); |
| 142 | } |
| 143 | |
| 144 | size_t RawPacket::BufferLength() const { |
| 145 | return buffer_length_; |
| 146 | } |
| 147 | |
| 148 | size_t RawPacket::Length() const { |
| 149 | return length_; |
| 150 | } |
| 151 | |
| 152 | void RawPacket::SetLength(size_t length) { |
| 153 | assert(length <= buffer_length_); |
| 154 | length_ = length; |
asapersson@webrtc.org | 3b84b3a | 2014-06-25 12:22:17 +0000 | [diff] [blame] | 155 | } |
| 156 | |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 157 | } // namespace rtcp |
| 158 | } // namespace webrtc |