From 07a01b3357b220558310e85d460298bb6f5516b9 Mon Sep 17 00:00:00 2001 From: danilchap Date: Wed, 29 Mar 2017 07:33:13 -0700 Subject: [PATCH] Allow RtpPacket::SetPayloadSize to increase payload size Make SetPayloadSize return buffer to write to so that it can replace AllocatePayload function. BUG=None Review-Url: https://codereview.webrtc.org/2785713002 Cr-Commit-Position: refs/heads/master@{#17450} --- webrtc/modules/rtp_rtcp/source/rtp_packet.cc | 17 +++++++---------- webrtc/modules/rtp_rtcp/source/rtp_packet.h | 3 ++- .../rtp_rtcp/source/rtp_packet_unittest.cc | 4 ++-- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/webrtc/modules/rtp_rtcp/source/rtp_packet.cc b/webrtc/modules/rtp_rtcp/source/rtp_packet.cc index e720eebc4a..ec240a8635 100644 --- a/webrtc/modules/rtp_rtcp/source/rtp_packet.cc +++ b/webrtc/modules/rtp_rtcp/source/rtp_packet.cc @@ -274,26 +274,23 @@ void Packet::SetCsrcs(const std::vector& csrcs) { } uint8_t* Packet::AllocatePayload(size_t size_bytes) { + // Reset payload size to 0. If CopyOnWrite buffer_ was shared, this will cause + // reallocation and memcpy. Keeping just header reduces memcpy size. + SetPayloadSize(0); + return SetPayloadSize(size_bytes); +} + +uint8_t* Packet::SetPayloadSize(size_t size_bytes) { RTC_DCHECK_EQ(padding_size_, 0); if (payload_offset_ + size_bytes > capacity()) { LOG(LS_WARNING) << "Cannot set payload, not enough space in buffer."; return nullptr; } - // Reset payload size to 0. If CopyOnWrite buffer_ was shared, this will cause - // reallocation and memcpy. Setting size to just headers reduces memcpy size. - buffer_.SetSize(payload_offset_); payload_size_ = size_bytes; buffer_.SetSize(payload_offset_ + payload_size_); return WriteAt(payload_offset_); } -void Packet::SetPayloadSize(size_t size_bytes) { - RTC_DCHECK_EQ(padding_size_, 0); - RTC_DCHECK_LE(size_bytes, payload_size_); - payload_size_ = size_bytes; - buffer_.SetSize(payload_offset_ + payload_size_); -} - bool Packet::SetPadding(uint8_t size_bytes, Random* random) { RTC_DCHECK(random); if (payload_offset_ + payload_size_ + size_bytes > capacity()) { diff --git a/webrtc/modules/rtp_rtcp/source/rtp_packet.h b/webrtc/modules/rtp_rtcp/source/rtp_packet.h index 3c02125e08..75c73cfbcc 100644 --- a/webrtc/modules/rtp_rtcp/source/rtp_packet.h +++ b/webrtc/modules/rtp_rtcp/source/rtp_packet.h @@ -96,8 +96,9 @@ class Packet { bool ReserveExtension(); // Reserve size_bytes for payload. Returns nullptr on failure. + uint8_t* SetPayloadSize(size_t size_bytes); + // Same as SetPayloadSize but doesn't guarantee to keep current payload. uint8_t* AllocatePayload(size_t size_bytes); - void SetPayloadSize(size_t size_bytes); bool SetPadding(uint8_t size_bytes, Random* random); protected: diff --git a/webrtc/modules/rtp_rtcp/source/rtp_packet_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtp_packet_unittest.cc index fc88497b19..2f1259e1bc 100644 --- a/webrtc/modules/rtp_rtcp/source/rtp_packet_unittest.cc +++ b/webrtc/modules/rtp_rtcp/source/rtp_packet_unittest.cc @@ -125,7 +125,7 @@ TEST(RtpPacketTest, SetReservedExtensionsAfterPayload) { RtpPacketToSend packet(&extensions); EXPECT_TRUE(packet.ReserveExtension()); - packet.AllocatePayload(kPayloadSize); + packet.SetPayloadSize(kPayloadSize); // Can't set extension after payload. EXPECT_FALSE(packet.SetExtension(kVoiceActive, kAudioLevel)); // Unless reserved. @@ -154,7 +154,7 @@ TEST(RtpPacketTest, CreateUnalignedPadding) { packet.SetSequenceNumber(kSeqNum); packet.SetTimestamp(kTimestamp); packet.SetSsrc(kSsrc); - packet.AllocatePayload(kPayloadSize); + packet.SetPayloadSize(kPayloadSize); Random r(0x123456789); EXPECT_LT(packet.size(), packet.capacity());