summaryrefslogtreecommitdiffstats
path: root/remoting
diff options
context:
space:
mode:
authorlambroslambrou@chromium.org <lambroslambrou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-09 17:13:52 +0000
committerlambroslambrou@chromium.org <lambroslambrou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-09 17:13:52 +0000
commit770284aab3daac2f3982df211355662453d92e2c (patch)
tree019d9e8520cc08cbafe15e46bb33dd1574b57d57 /remoting
parent2dfcd62c749a9414670c8ddcab54516b21f964cc (diff)
downloadchromium_src-770284aab3daac2f3982df211355662453d92e2c.zip
chromium_src-770284aab3daac2f3982df211355662453d92e2c.tar.gz
chromium_src-770284aab3daac2f3982df211355662453d92e2c.tar.bz2
Fix memory leak in EncoderVp8, and add unit-test for it.
Leak occurred when changing host's screen resolution. BUG=None TEST=Valgrind bots stay green Review URL: http://codereview.chromium.org/6951010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@84633 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r--remoting/base/encoder_vp8.cc6
-rw-r--r--remoting/base/encoder_vp8.h3
-rw-r--r--remoting/base/encoder_vp8_unittest.cc41
3 files changed, 49 insertions, 1 deletions
diff --git a/remoting/base/encoder_vp8.cc b/remoting/base/encoder_vp8.cc
index 84a0bc6..0bba179 100644
--- a/remoting/base/encoder_vp8.cc
+++ b/remoting/base/encoder_vp8.cc
@@ -37,13 +37,19 @@ EncoderVp8::EncoderVp8()
}
EncoderVp8::~EncoderVp8() {
+ Destroy();
+}
+
+void EncoderVp8::Destroy() {
if (initialized_) {
vpx_codec_err_t ret = vpx_codec_destroy(codec_.get());
DCHECK(ret == VPX_CODEC_OK) << "Failed to destroy codec";
+ initialized_ = false;
}
}
bool EncoderVp8::Init(const gfx::Size& size) {
+ Destroy();
size_ = size;
codec_.reset(new vpx_codec_ctx_t());
image_.reset(new vpx_image_t());
diff --git a/remoting/base/encoder_vp8.h b/remoting/base/encoder_vp8.h
index 6ded488..a74d9f9 100644
--- a/remoting/base/encoder_vp8.h
+++ b/remoting/base/encoder_vp8.h
@@ -32,6 +32,9 @@ class EncoderVp8 : public Encoder {
// Initialize the encoder. Returns true if successful.
bool Init(const gfx::Size& size);
+ // Destroy the encoder.
+ void Destroy();
+
// Prepare |image_| for encoding. Write updated rectangles into
// |updated_rects|. Returns true if successful.
bool PrepareImage(scoped_refptr<CaptureData> capture_data,
diff --git a/remoting/base/encoder_vp8_unittest.cc b/remoting/base/encoder_vp8_unittest.cc
index 370fd08..9435cd1 100644
--- a/remoting/base/encoder_vp8_unittest.cc
+++ b/remoting/base/encoder_vp8_unittest.cc
@@ -1,11 +1,16 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <limits>
+#include <vector>
+#include "base/callback.h"
+#include "base/scoped_ptr.h"
+#include "remoting/base/capture_data.h"
#include "remoting/base/codec_test.h"
#include "remoting/base/encoder_vp8.h"
+#include "remoting/proto/video.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
@@ -21,6 +26,40 @@ TEST(EncoderVp8Test, TestEncoder) {
TestEncoder(&encoder, false);
}
+class EncoderCallback {
+ public:
+ void DataAvailable(VideoPacket *packet) {
+ delete packet;
+ }
+};
+
+// Test that calling Encode with a differently-sized CaptureData does not
+// leak memory.
+TEST(EncoderVp8Test, TestSizeChangeNoLeak) {
+ int height = 1000;
+ int width = 1000;
+ const int kBytesPerPixel = 4;
+
+ EncoderVp8 encoder;
+ EncoderCallback callback;
+
+ std::vector<uint8> buffer(width * height * kBytesPerPixel);
+ DataPlanes planes;
+ planes.data[0] = &buffer.front();
+ planes.strides[0] = width;
+
+ scoped_refptr<CaptureData> capture_data(new CaptureData(
+ planes, gfx::Size(width, height), media::VideoFrame::RGB32));
+ encoder.Encode(capture_data, false,
+ NewCallback(&callback, &EncoderCallback::DataAvailable));
+
+ height /= 2;
+ capture_data = new CaptureData(planes, gfx::Size(width, height),
+ media::VideoFrame::RGB32);
+ encoder.Encode(capture_data, false,
+ NewCallback(&callback, &EncoderCallback::DataAvailable));
+}
+
TEST(EncoderVp8Test, AlignAndClipRect) {
// Simple test case (no clipping).
gfx::Rect r1(100, 200, 300, 400);