[email protected] | 770284a | 2011-05-09 17:13:52 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f752400 | 2011-03-17 14:22:14 | [diff] [blame] | 5 | #include <limits> |
[email protected] | 770284a | 2011-05-09 17:13:52 | [diff] [blame] | 6 | #include <vector> |
[email protected] | f752400 | 2011-03-17 14:22:14 | [diff] [blame] | 7 | |
[email protected] | 1e1cb3b | 2011-11-10 02:07:41 | [diff] [blame^] | 8 | #include "base/bind.h" |
[email protected] | 770284a | 2011-05-09 17:13:52 | [diff] [blame] | 9 | #include "base/callback.h" |
[email protected] | e57a716 | 2011-06-15 04:14:23 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 770284a | 2011-05-09 17:13:52 | [diff] [blame] | 11 | #include "remoting/base/capture_data.h" |
[email protected] | df10bf17 | 2010-09-28 22:19:48 | [diff] [blame] | 12 | #include "remoting/base/codec_test.h" |
| 13 | #include "remoting/base/encoder_vp8.h" |
[email protected] | 770284a | 2011-05-09 17:13:52 | [diff] [blame] | 14 | #include "remoting/proto/video.pb.h" |
[email protected] | df10bf17 | 2010-09-28 22:19:48 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 16 | |
[email protected] | f752400 | 2011-03-17 14:22:14 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | const int kIntMax = std::numeric_limits<int>::max(); |
| 20 | |
| 21 | } // namespace |
| 22 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 23 | namespace remoting { |
| 24 | |
[email protected] | df10bf17 | 2010-09-28 22:19:48 | [diff] [blame] | 25 | TEST(EncoderVp8Test, TestEncoder) { |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 26 | EncoderVp8 encoder; |
[email protected] | df10bf17 | 2010-09-28 22:19:48 | [diff] [blame] | 27 | TestEncoder(&encoder, false); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 28 | } |
| 29 | |
[email protected] | 770284a | 2011-05-09 17:13:52 | [diff] [blame] | 30 | class EncoderCallback { |
| 31 | public: |
| 32 | void DataAvailable(VideoPacket *packet) { |
| 33 | delete packet; |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | // Test that calling Encode with a differently-sized CaptureData does not |
| 38 | // leak memory. |
| 39 | TEST(EncoderVp8Test, TestSizeChangeNoLeak) { |
| 40 | int height = 1000; |
| 41 | int width = 1000; |
| 42 | const int kBytesPerPixel = 4; |
| 43 | |
| 44 | EncoderVp8 encoder; |
| 45 | EncoderCallback callback; |
| 46 | |
| 47 | std::vector<uint8> buffer(width * height * kBytesPerPixel); |
| 48 | DataPlanes planes; |
| 49 | planes.data[0] = &buffer.front(); |
| 50 | planes.strides[0] = width; |
| 51 | |
| 52 | scoped_refptr<CaptureData> capture_data(new CaptureData( |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 53 | planes, SkISize::Make(width, height), media::VideoFrame::RGB32)); |
[email protected] | 770284a | 2011-05-09 17:13:52 | [diff] [blame] | 54 | encoder.Encode(capture_data, false, |
[email protected] | 1e1cb3b | 2011-11-10 02:07:41 | [diff] [blame^] | 55 | base::Bind(&EncoderCallback::DataAvailable, |
| 56 | base::Unretained(&callback))); |
[email protected] | 770284a | 2011-05-09 17:13:52 | [diff] [blame] | 57 | |
| 58 | height /= 2; |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 59 | capture_data = new CaptureData(planes, SkISize::Make(width, height), |
[email protected] | 770284a | 2011-05-09 17:13:52 | [diff] [blame] | 60 | media::VideoFrame::RGB32); |
| 61 | encoder.Encode(capture_data, false, |
[email protected] | 1e1cb3b | 2011-11-10 02:07:41 | [diff] [blame^] | 62 | base::Bind(&EncoderCallback::DataAvailable, |
| 63 | base::Unretained(&callback))); |
[email protected] | 770284a | 2011-05-09 17:13:52 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | f752400 | 2011-03-17 14:22:14 | [diff] [blame] | 66 | TEST(EncoderVp8Test, AlignAndClipRect) { |
| 67 | // Simple test case (no clipping). |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 68 | SkIRect r1(SkIRect::MakeXYWH(100, 200, 300, 400)); |
[email protected] | f752400 | 2011-03-17 14:22:14 | [diff] [blame] | 69 | EXPECT_EQ(EncoderVp8::AlignAndClipRect(r1, kIntMax, kIntMax), r1); |
| 70 | |
| 71 | // Should expand outward to r1. |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 72 | SkIRect r2(SkIRect::MakeXYWH(101, 201, 298, 398)); |
[email protected] | f752400 | 2011-03-17 14:22:14 | [diff] [blame] | 73 | EXPECT_EQ(EncoderVp8::AlignAndClipRect(r2, kIntMax, kIntMax), r1); |
| 74 | |
| 75 | // Test clipping to screen size. |
| 76 | EXPECT_EQ(EncoderVp8::AlignAndClipRect(r1, 110, 220), |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 77 | SkIRect::MakeXYWH(100, 200, 10, 20)); |
[email protected] | f752400 | 2011-03-17 14:22:14 | [diff] [blame] | 78 | |
| 79 | // Rectangle completely off-screen. |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 80 | EXPECT_TRUE(EncoderVp8::AlignAndClipRect(r1, 50, 50).isEmpty()); |
[email protected] | f752400 | 2011-03-17 14:22:14 | [diff] [blame] | 81 | |
| 82 | // Clipping to odd-sized screen. An unlikely case, and we might not deal |
| 83 | // with it cleanly in the encoder (we possibly lose 1px at right & bottom |
| 84 | // of screen). |
| 85 | EXPECT_EQ(EncoderVp8::AlignAndClipRect(r1, 199, 299), |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 86 | SkIRect::MakeXYWH(100, 200, 98, 98)); |
[email protected] | f752400 | 2011-03-17 14:22:14 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 89 | } // namespace remoting |