diff --git a/api/video/BUILD.gn b/api/video/BUILD.gn index af2b897ea1..0da9c6c86b 100644 --- a/api/video/BUILD.gn +++ b/api/video/BUILD.gn @@ -114,6 +114,7 @@ rtc_source_set("encoded_image") { "..:scoped_refptr", "../..:webrtc_common", "../../rtc_base:checks", + "../../rtc_base:deprecation", "../../rtc_base:rtc_base_approved", "../../rtc_base/system:rtc_export", "//third_party/abseil-cpp/absl/types:optional", diff --git a/api/video/encoded_image.h b/api/video/encoded_image.h index a980ef7ee1..91f2e0f567 100644 --- a/api/video/encoded_image.h +++ b/api/video/encoded_image.h @@ -28,6 +28,7 @@ #include "api/video/video_timing.h" #include "common_types.h" // NOLINT(build/include) #include "rtc_base/checks.h" +#include "rtc_base/deprecation.h" #include "rtc_base/ref_count.h" #include "rtc_base/system/rtc_export.h" @@ -55,6 +56,7 @@ class EncodedImageBufferInterface : public rtc::RefCountInterface { // Basic implementation of EncodedImageBufferInterface. class EncodedImageBuffer : public EncodedImageBufferInterface { public: + static rtc::scoped_refptr Create() { return Create(0); } static rtc::scoped_refptr Create(size_t size); static rtc::scoped_refptr Create(const uint8_t* data, size_t size); @@ -146,6 +148,7 @@ class RTC_EXPORT EncodedImage { // TODO(bugs.webrtc.org/9378): Delete; this method implies realloc, which // should not be generally supported by the EncodedImageBufferInterface. + RTC_DEPRECATED void Allocate(size_t capacity); void SetEncodedData( diff --git a/call/rtp_video_sender_unittest.cc b/call/rtp_video_sender_unittest.cc index 17ee9c6c68..39d25e4d66 100644 --- a/call/rtp_video_sender_unittest.cc +++ b/call/rtp_video_sender_unittest.cc @@ -521,14 +521,13 @@ TEST(RtpVideoSenderTest, EarlyRetransmits) { kPayloadType, {}); test.router()->SetActive(true); - constexpr uint8_t kPayload = 'a'; + const uint8_t kPayload[1] = {'a'}; EncodedImage encoded_image; encoded_image.SetTimestamp(1); encoded_image.capture_time_ms_ = 2; encoded_image._frameType = VideoFrameType::kVideoFrameKey; - encoded_image.Allocate(1); - encoded_image.data()[0] = kPayload; - encoded_image.set_size(1); + encoded_image.SetEncodedData( + EncodedImageBuffer::Create(kPayload, sizeof(kPayload))); encoded_image.SetSpatialIndex(0); CodecSpecificInfo codec_specific; diff --git a/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc b/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc index d4f18e08f5..ee5fd436b2 100644 --- a/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc +++ b/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc @@ -1136,16 +1136,20 @@ int LibvpxVp8Encoder::GetEncodedPartitions(const VideoFrame& input_image, encoded_images_[encoder_idx]._frameType = VideoFrameType::kVideoFrameDelta; CodecSpecificInfo codec_specific; const vpx_codec_cx_pkt_t* pkt = NULL; + + // TODO(nisse): Introduce some buffer cache or buffer pool, to reduce + // allocations and/or copy operations. + auto buffer = EncodedImageBuffer::Create(); + while ((pkt = libvpx_->codec_get_cx_data(&encoders_[encoder_idx], &iter)) != NULL) { switch (pkt->kind) { case VPX_CODEC_CX_FRAME_PKT: { - const size_t size = encoded_images_[encoder_idx].size(); + const size_t size = buffer->size(); const size_t new_size = pkt->data.frame.sz + size; - encoded_images_[encoder_idx].Allocate(new_size); - memcpy(&encoded_images_[encoder_idx].data()[size], - pkt->data.frame.buf, pkt->data.frame.sz); - encoded_images_[encoder_idx].set_size(new_size); + buffer->Realloc(new_size); + memcpy(&buffer->data()[size], pkt->data.frame.buf, + pkt->data.frame.sz); break; } default: @@ -1158,6 +1162,7 @@ int LibvpxVp8Encoder::GetEncodedPartitions(const VideoFrame& input_image, encoded_images_[encoder_idx]._frameType = VideoFrameType::kVideoFrameKey; } + encoded_images_[encoder_idx].SetEncodedData(buffer); encoded_images_[encoder_idx].SetSpatialIndex(stream_idx); PopulateCodecSpecific(&codec_specific, *pkt, stream_idx, encoder_idx, input_image.timestamp()); diff --git a/modules/video_coding/encoded_frame.cc b/modules/video_coding/encoded_frame.cc index bbbd9bce9c..1e9e374c64 100644 --- a/modules/video_coding/encoded_frame.cc +++ b/modules/video_coding/encoded_frame.cc @@ -159,14 +159,4 @@ void VCMEncodedFrame::CopyCodecSpecific(const RTPVideoHeader* header) { } } -void VCMEncodedFrame::VerifyAndAllocate(size_t minimumSize) { - size_t old_capacity = capacity(); - if (minimumSize > old_capacity) { - // TODO(nisse): EncodedImage::Allocate is implemented as a realloc - // operation, and is deprecated. Refactor to use EncodedImageBuffer::Realloc - // instead. - Allocate(minimumSize); - } -} - } // namespace webrtc diff --git a/modules/video_coding/encoded_frame.h b/modules/video_coding/encoded_frame.h index 27ad107850..b29ff632e5 100644 --- a/modules/video_coding/encoded_frame.h +++ b/modules/video_coding/encoded_frame.h @@ -110,16 +110,6 @@ class VCMEncodedFrame : protected EncodedImage { _codecSpecificInfo = *codec_specific; } - /** - * Verifies that current allocated buffer size is larger than or equal to the - * input size. - * If the current buffer size is smaller, a new allocation is made and the old - * buffer data - * is copied to the new buffer. - * Buffer size is updated to minimumSize. - */ - void VerifyAndAllocate(size_t minimumSize); - protected: void Reset(); diff --git a/modules/video_coding/frame_buffer2_unittest.cc b/modules/video_coding/frame_buffer2_unittest.cc index d96960e267..09300fb635 100644 --- a/modules/video_coding/frame_buffer2_unittest.cc +++ b/modules/video_coding/frame_buffer2_unittest.cc @@ -171,8 +171,7 @@ class TestFrameBuffer2 : public ::testing::Test { frame->inter_layer_predicted = inter_layer_predicted; frame->is_last_spatial_layer = last_spatial_layer; // Add some data to buffer. - frame->VerifyAndAllocate(frame_size_bytes); - frame->set_size(frame_size_bytes); + frame->SetEncodedData(EncodedImageBuffer::Create(frame_size_bytes)); for (size_t r = 0; r < references.size(); ++r) frame->references[r] = references[r]; return frame; @@ -585,8 +584,7 @@ TEST_F(TestFrameBuffer2, StatsCallback) { { std::unique_ptr frame(new FrameObjectFake()); - frame->VerifyAndAllocate(kFrameSize); - frame->set_size(kFrameSize); + frame->SetEncodedData(EncodedImageBuffer::Create(kFrameSize)); frame->id.picture_id = pid; frame->id.spatial_layer = 0; frame->SetTimestamp(ts);