Remove RtcpPacket dependency on rtcp_utility

and thus IP_PACKET_SIZE constant:
Build() use BlockLength() instead of constant IP_PACKET_SIZE for packet
capacity, adding extra checks about packet generation in tests.
Build(callback) removed as unused.
definitions reordered to follow style.

BUG=webrtc:5260

Review-Url: https://codereview.webrtc.org/2270753002
Cr-Commit-Position: refs/heads/master@{#14647}
This commit is contained in:
danilchap
2016-10-17 01:44:44 -07:00
committed by Commit bot
parent 27c3d5b652
commit c1f40b7bae
2 changed files with 35 additions and 61 deletions

View File

@ -11,50 +11,23 @@
#include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h" #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h"
#include "webrtc/base/checks.h" #include "webrtc/base/checks.h"
#include "webrtc/base/logging.h"
#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
namespace webrtc { namespace webrtc {
namespace rtcp { namespace rtcp {
namespace { constexpr size_t RtcpPacket::kHeaderLength;
void AssignUWord8(uint8_t* buffer, size_t* offset, uint8_t value) {
buffer[(*offset)++] = value;
}
void AssignUWord16(uint8_t* buffer, size_t* offset, uint16_t value) {
ByteWriter<uint16_t>::WriteBigEndian(buffer + *offset, value);
*offset += 2;
}
} // namespace
rtc::Buffer RtcpPacket::Build() const { rtc::Buffer RtcpPacket::Build() const {
rtc::Buffer packet(BlockLength());
size_t length = 0; size_t length = 0;
rtc::Buffer packet(IP_PACKET_SIZE); bool created = Create(packet.data(), &length, packet.capacity(), nullptr);
RTC_DCHECK(created) << "Invalid packet is not supported.";
RTC_DCHECK_EQ(length, packet.size())
<< "BlockLength mispredicted size used by Create";
class PacketVerifier : public PacketReadyCallback {
public:
explicit PacketVerifier(rtc::Buffer* packet)
: called_(false), packet_(packet) {}
virtual ~PacketVerifier() {}
void OnPacketReady(uint8_t* data, size_t length) override {
RTC_CHECK(!called_) << "Fragmentation not supported.";
called_ = true;
packet_->SetSize(length);
}
private:
bool called_;
rtc::Buffer* const packet_;
} verifier(&packet);
Create(packet.data(), &length, packet.capacity(), &verifier);
OnBufferFull(packet.data(), &length, &verifier);
return packet; return packet;
} }
bool RtcpPacket::Build(PacketReadyCallback* callback) const {
uint8_t buffer[IP_PACKET_SIZE];
return BuildExternalBuffer(buffer, IP_PACKET_SIZE, callback);
}
bool RtcpPacket::BuildExternalBuffer(uint8_t* buffer, bool RtcpPacket::BuildExternalBuffer(uint8_t* buffer,
size_t max_length, size_t max_length,
PacketReadyCallback* callback) const { PacketReadyCallback* callback) const {
@ -66,9 +39,10 @@ bool RtcpPacket::BuildExternalBuffer(uint8_t* buffer,
bool RtcpPacket::OnBufferFull(uint8_t* packet, bool RtcpPacket::OnBufferFull(uint8_t* packet,
size_t* index, size_t* index,
RtcpPacket::PacketReadyCallback* callback) const { PacketReadyCallback* callback) const {
if (*index == 0) if (*index == 0)
return false; return false;
RTC_DCHECK(callback) << "Fragmentation not supported.";
callback->OnPacketReady(packet, *index); callback->OnPacketReady(packet, *index);
*index = 0; *index = 0;
return true; return true;
@ -76,9 +50,10 @@ bool RtcpPacket::OnBufferFull(uint8_t* packet,
size_t RtcpPacket::HeaderLength() const { size_t RtcpPacket::HeaderLength() const {
size_t length_in_bytes = BlockLength(); size_t length_in_bytes = BlockLength();
// Length in 32-bit words minus 1. RTC_DCHECK_GT(length_in_bytes, 0u);
assert(length_in_bytes > 0); RTC_DCHECK_EQ(length_in_bytes % 4, 0u) << "Padding not supported";
return ((length_in_bytes + 3) / 4) - 1; // Length in 32-bit words without common header.
return (length_in_bytes - kHeaderLength) / 4;
} }
// From RFC 3550, RTP: A Transport Protocol for Real-Time Applications. // From RFC 3550, RTP: A Transport Protocol for Real-Time Applications.
@ -89,18 +64,21 @@ size_t RtcpPacket::HeaderLength() const {
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |V=2|P| RC/FMT | PT | length | // |V=2|P| RC/FMT | PT | length |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
void RtcpPacket::CreateHeader( void RtcpPacket::CreateHeader(
uint8_t count_or_format, // Depends on packet type. uint8_t count_or_format, // Depends on packet type.
uint8_t packet_type, uint8_t packet_type,
size_t length, size_t length,
uint8_t* buffer, uint8_t* buffer,
size_t* pos) { size_t* pos) {
assert(length <= 0xffff); RTC_DCHECK_LE(length, 0xffffU);
const uint8_t kVersion = 2; RTC_DCHECK_LE(count_or_format, 0x1f);
AssignUWord8(buffer, pos, (kVersion << 6) + count_or_format); constexpr uint8_t kVersionBits = 2 << 6;
AssignUWord8(buffer, pos, packet_type); constexpr uint8_t kNoPaddingBit = 0 << 5;
AssignUWord16(buffer, pos, length); buffer[*pos + 0] = kVersionBits | kNoPaddingBit | count_or_format;
buffer[*pos + 1] = packet_type;
buffer[*pos + 2] = (length >> 8) & 0xff;
buffer[*pos + 3] = length & 0xff;
*pos += kHeaderLength;
} }
} // namespace rtcp } // namespace rtcp

View File

@ -8,12 +8,11 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
* *
*/ */
#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
#include "webrtc/base/basictypes.h"
#include "webrtc/base/buffer.h" #include "webrtc/base/buffer.h"
#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
namespace webrtc { namespace webrtc {
namespace rtcp { namespace rtcp {
@ -46,27 +45,24 @@ namespace rtcp {
class RtcpPacket { class RtcpPacket {
public: public:
virtual ~RtcpPacket() {}
// Callback used to signal that an RTCP packet is ready. Note that this may // Callback used to signal that an RTCP packet is ready. Note that this may
// not contain all data in this RtcpPacket; if a packet cannot fit in // not contain all data in this RtcpPacket; if a packet cannot fit in
// max_length bytes, it will be fragmented and multiple calls to this // max_length bytes, it will be fragmented and multiple calls to this
// callback will be made. // callback will be made.
class PacketReadyCallback { class PacketReadyCallback {
public: public:
virtual void OnPacketReady(uint8_t* data, size_t length) = 0;
protected:
PacketReadyCallback() {} PacketReadyCallback() {}
virtual ~PacketReadyCallback() {} virtual ~PacketReadyCallback() {}
virtual void OnPacketReady(uint8_t* data, size_t length) = 0;
}; };
// Convenience method mostly used for test. Max length of IP_PACKET_SIZE is virtual ~RtcpPacket() {}
// used, will cause assertion error if fragmentation occurs.
rtc::Buffer Build() const;
// Returns true if call to Create succeeded. A buffer of size // Convenience method mostly used for test. Creates packet without
// IP_PACKET_SIZE will be allocated and reused between calls to callback. // fragmentation using BlockLength() to allocate big enough buffer.
bool Build(PacketReadyCallback* callback) const; rtc::Buffer Build() const;
// Returns true if call to Create succeeded. Provided buffer reference // Returns true if call to Create succeeded. Provided buffer reference
// will be used for all calls to callback. // will be used for all calls to callback.
@ -86,21 +82,21 @@ class RtcpPacket {
PacketReadyCallback* callback) const = 0; PacketReadyCallback* callback) const = 0;
protected: protected:
// Size of the rtcp common header.
static constexpr size_t kHeaderLength = 4;
RtcpPacket() {} RtcpPacket() {}
static void CreateHeader(uint8_t count_or_format, static void CreateHeader(uint8_t count_or_format,
uint8_t packet_type, uint8_t packet_type,
size_t block_length, // Size in 32bit words - 1. size_t block_length, // Payload size in 32bit words.
uint8_t* buffer, uint8_t* buffer,
size_t* pos); size_t* pos);
bool OnBufferFull(uint8_t* packet, bool OnBufferFull(uint8_t* packet,
size_t* index, size_t* index,
RtcpPacket::PacketReadyCallback* callback) const; PacketReadyCallback* callback) const;
// Size of the rtcp packet as written in header.
size_t HeaderLength() const; size_t HeaderLength() const;
static const size_t kHeaderLength = 4;
}; };
} // namespace rtcp } // namespace rtcp
} // namespace webrtc } // namespace webrtc