[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [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] | 542bdfe | 2010-11-30 03:55:47 | [diff] [blame] | 5 | #include <deque> |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 6 | #include <stdlib.h> |
| 7 | |
[email protected] | 1e1cb3b | 2011-11-10 02:07:41 | [diff] [blame] | 8 | #include "base/bind.h" |
| 9 | #include "base/logging.h" |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 11 | #include "media/base/video_frame.h" |
[email protected] | 3860983 | 2012-08-23 01:36:10 | [diff] [blame] | 12 | #include "remoting/codec/codec_test.h" |
| 13 | #include "remoting/codec/video_decoder.h" |
| 14 | #include "remoting/codec/video_encoder.h" |
[email protected] | c3af26f33 | 2010-10-06 22:46:00 | [diff] [blame] | 15 | #include "remoting/base/util.h" |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 17 | #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 18 | |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 19 | using webrtc::BasicDesktopFrame; |
| 20 | using webrtc::DesktopFrame; |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 21 | using webrtc::DesktopRect; |
[email protected] | f301d736 | 2013-06-05 00:02:02 | [diff] [blame] | 22 | using webrtc::DesktopRegion; |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 23 | using webrtc::DesktopSize; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 24 | |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | const int kBytesPerPixel = 4; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 28 | |
| 29 | // Some sample rects for testing. |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 30 | std::vector<std::vector<DesktopRect> > MakeTestRectLists(DesktopSize size) { |
| 31 | std::vector<std::vector<DesktopRect> > rect_lists; |
| 32 | std::vector<DesktopRect> rects; |
| 33 | rects.push_back(DesktopRect::MakeXYWH(0, 0, size.width(), size.height())); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 34 | rect_lists.push_back(rects); |
| 35 | rects.clear(); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 36 | rects.push_back(DesktopRect::MakeXYWH( |
| 37 | 0, 0, size.width() / 2, size.height() / 2)); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 38 | rect_lists.push_back(rects); |
| 39 | rects.clear(); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 40 | rects.push_back(DesktopRect::MakeXYWH( |
| 41 | size.width() / 2, size.height() / 2, |
| 42 | size.width() / 2, size.height() / 2)); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 43 | rect_lists.push_back(rects); |
| 44 | rects.clear(); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 45 | rects.push_back(DesktopRect::MakeXYWH(16, 16, 16, 16)); |
| 46 | rects.push_back(DesktopRect::MakeXYWH(128, 64, 32, 32)); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 47 | rect_lists.push_back(rects); |
| 48 | return rect_lists; |
| 49 | } |
| 50 | |
| 51 | } // namespace |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 52 | |
| 53 | namespace remoting { |
| 54 | |
[email protected] | d9004e4 | 2012-08-24 01:47:37 | [diff] [blame] | 55 | class VideoDecoderTester { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 56 | public: |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 57 | VideoDecoderTester(VideoDecoder* decoder, |
| 58 | const DesktopSize& screen_size, |
| 59 | const DesktopSize& view_size) |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 60 | : screen_size_(screen_size), |
| 61 | view_size_(view_size), |
| 62 | strict_(false), |
[email protected] | a3c1d45 | 2013-08-21 18:51:40 | [diff] [blame] | 63 | decoder_(decoder), |
| 64 | frame_(NULL) { |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 65 | image_data_.reset(new uint8[ |
| 66 | view_size_.width() * view_size_.height() * kBytesPerPixel]); |
[email protected] | 55d3688e | 2012-02-24 23:05:56 | [diff] [blame] | 67 | EXPECT_TRUE(image_data_.get()); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 68 | decoder_->Initialize( |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 69 | DesktopSize(screen_size_.width(), screen_size_.height())); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 72 | void Reset() { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 73 | expected_region_.Clear(); |
[email protected] | e59d659 | 2013-09-25 22:16:21 | [diff] [blame] | 74 | update_region_.Clear(); |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 75 | } |
[email protected] | cee5dba | 2010-12-07 03:19:48 | [diff] [blame] | 76 | |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 77 | void ResetRenderedData() { |
| 78 | memset(image_data_.get(), 0, |
| 79 | view_size_.width() * view_size_.height() * kBytesPerPixel); |
| 80 | } |
| 81 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 82 | void ReceivedPacket(VideoPacket* packet) { |
[email protected] | 41b93ad3 | 2013-09-23 18:53:52 | [diff] [blame] | 83 | ASSERT_TRUE(decoder_->DecodePacket(*packet)); |
[email protected] | cee5dba | 2010-12-07 03:19:48 | [diff] [blame] | 84 | |
[email protected] | 41b93ad3 | 2013-09-23 18:53:52 | [diff] [blame] | 85 | RenderFrame(); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 88 | void RenderFrame() { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 89 | decoder_->RenderFrame( |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 90 | DesktopSize(view_size_.width(), view_size_.height()), |
| 91 | DesktopRect::MakeWH(view_size_.width(), view_size_.height()), |
[email protected] | e59d659 | 2013-09-25 22:16:21 | [diff] [blame] | 92 | image_data_.get(), view_size_.width() * kBytesPerPixel, |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 93 | &update_region_); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 96 | void ReceivedScopedPacket(scoped_ptr<VideoPacket> packet) { |
| 97 | ReceivedPacket(packet.get()); |
| 98 | } |
| 99 | |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 100 | void set_strict(bool strict) { |
| 101 | strict_ = strict; |
| 102 | } |
| 103 | |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 104 | void set_frame(DesktopFrame* frame) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 105 | frame_ = frame; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 108 | void AddRects(const DesktopRect* rects, int count) { |
| 109 | for (int i = 0; i < count; ++i) { |
| 110 | expected_region_.AddRect(rects[i]); |
| 111 | } |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 114 | void AddRegion(const DesktopRegion& region) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 115 | expected_region_.AddRegion(region); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 118 | void VerifyResults() { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 119 | if (!strict_) |
| 120 | return; |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 121 | |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 122 | ASSERT_TRUE(frame_); |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 123 | |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 124 | // Test the content of the update region. |
[email protected] | e59d659 | 2013-09-25 22:16:21 | [diff] [blame] | 125 | EXPECT_TRUE(expected_region_.Equals(update_region_)); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 126 | |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 127 | for (DesktopRegion::Iterator i(update_region_); !i.IsAtEnd(); |
[email protected] | e59d659 | 2013-09-25 22:16:21 | [diff] [blame] | 128 | i.Advance()) { |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 129 | const int stride = view_size_.width() * kBytesPerPixel; |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 130 | EXPECT_EQ(stride, frame_->stride()); |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 131 | const int offset = stride * i.rect().top() + |
| 132 | kBytesPerPixel * i.rect().left(); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 133 | const uint8* original = frame_->data() + offset; |
[email protected] | 55d3688e | 2012-02-24 23:05:56 | [diff] [blame] | 134 | const uint8* decoded = image_data_.get() + offset; |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 135 | const int row_size = kBytesPerPixel * i.rect().width(); |
| 136 | for (int y = 0; y < i.rect().height(); ++y) { |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 137 | EXPECT_EQ(0, memcmp(original, decoded, row_size)) |
| 138 | << "Row " << y << " is different"; |
| 139 | original += stride; |
| 140 | decoded += stride; |
| 141 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 145 | // The error at each pixel is the root mean square of the errors in |
| 146 | // the R, G, and B components, each normalized to [0, 1]. This routine |
| 147 | // checks that the maximum and mean pixel errors do not exceed given limits. |
[email protected] | 8c6d71d | 2012-10-23 21:20:04 | [diff] [blame] | 148 | void VerifyResultsApprox(const uint8* expected_view_data, |
| 149 | double max_error_limit, double mean_error_limit) { |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 150 | double max_error = 0.0; |
| 151 | double sum_error = 0.0; |
| 152 | int error_num = 0; |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 153 | for (DesktopRegion::Iterator i(update_region_); !i.IsAtEnd(); |
[email protected] | e59d659 | 2013-09-25 22:16:21 | [diff] [blame] | 154 | i.Advance()) { |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 155 | const int stride = view_size_.width() * kBytesPerPixel; |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 156 | const int offset = stride * i.rect().top() + |
| 157 | kBytesPerPixel * i.rect().left(); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 158 | const uint8* expected = expected_view_data + offset; |
| 159 | const uint8* actual = image_data_.get() + offset; |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 160 | for (int y = 0; y < i.rect().height(); ++y) { |
| 161 | for (int x = 0; x < i.rect().width(); ++x) { |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 162 | double error = CalculateError(expected, actual); |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 163 | max_error = std::max(max_error, error); |
| 164 | sum_error += error; |
| 165 | ++error_num; |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 166 | expected += 4; |
| 167 | actual += 4; |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | EXPECT_LE(max_error, max_error_limit); |
| 172 | double mean_error = sum_error / error_num; |
| 173 | EXPECT_LE(mean_error, mean_error_limit); |
[email protected] | 5cbe3cf | 2013-11-25 17:05:04 | [diff] [blame] | 174 | VLOG(0) << "Max error: " << max_error; |
| 175 | VLOG(0) << "Mean error: " << mean_error; |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | double CalculateError(const uint8* original, const uint8* decoded) { |
| 179 | double error_sum_squares = 0.0; |
| 180 | for (int i = 0; i < 3; i++) { |
| 181 | double error = static_cast<double>(*original++) - |
| 182 | static_cast<double>(*decoded++); |
| 183 | error /= 255.0; |
| 184 | error_sum_squares += error * error; |
| 185 | } |
| 186 | original++; |
| 187 | decoded++; |
| 188 | return sqrt(error_sum_squares / 3.0); |
| 189 | } |
| 190 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 191 | private: |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 192 | DesktopSize screen_size_; |
| 193 | DesktopSize view_size_; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 194 | bool strict_; |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 195 | DesktopRegion expected_region_; |
| 196 | DesktopRegion update_region_; |
[email protected] | d9004e4 | 2012-08-24 01:47:37 | [diff] [blame] | 197 | VideoDecoder* decoder_; |
[email protected] | c84c53d | 2013-04-08 20:13:23 | [diff] [blame] | 198 | scoped_ptr<uint8[]> image_data_; |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 199 | DesktopFrame* frame_; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 200 | |
[email protected] | d9004e4 | 2012-08-24 01:47:37 | [diff] [blame] | 201 | DISALLOW_COPY_AND_ASSIGN(VideoDecoderTester); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 202 | }; |
| 203 | |
[email protected] | 92b45b8e | 2012-08-24 19:46:47 | [diff] [blame] | 204 | // The VideoEncoderTester provides a hook for retrieving the data, and passing |
| 205 | // the message to other subprograms for validaton. |
| 206 | class VideoEncoderTester { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 207 | public: |
[email protected] | 8b6d3d0 | 2013-09-13 22:43:41 | [diff] [blame] | 208 | VideoEncoderTester() |
| 209 | : decoder_tester_(NULL), |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 210 | data_available_(0) { |
| 211 | } |
| 212 | |
[email protected] | 92b45b8e | 2012-08-24 19:46:47 | [diff] [blame] | 213 | ~VideoEncoderTester() { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 214 | EXPECT_GT(data_available_, 0); |
| 215 | } |
| 216 | |
[email protected] | 3361e1f | 2012-03-20 20:31:44 | [diff] [blame] | 217 | void DataAvailable(scoped_ptr<VideoPacket> packet) { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 218 | ++data_available_; |
[email protected] | d9004e4 | 2012-08-24 01:47:37 | [diff] [blame] | 219 | // Send the message to the VideoDecoderTester. |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 220 | if (decoder_tester_) { |
[email protected] | 3361e1f | 2012-03-20 20:31:44 | [diff] [blame] | 221 | decoder_tester_->ReceivedPacket(packet.get()); |
[email protected] | cee5dba | 2010-12-07 03:19:48 | [diff] [blame] | 222 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | d9004e4 | 2012-08-24 01:47:37 | [diff] [blame] | 225 | void set_decoder_tester(VideoDecoderTester* decoder_tester) { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 226 | decoder_tester_ = decoder_tester; |
| 227 | } |
| 228 | |
| 229 | private: |
[email protected] | d9004e4 | 2012-08-24 01:47:37 | [diff] [blame] | 230 | VideoDecoderTester* decoder_tester_; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 231 | int data_available_; |
| 232 | |
[email protected] | 92b45b8e | 2012-08-24 19:46:47 | [diff] [blame] | 233 | DISALLOW_COPY_AND_ASSIGN(VideoEncoderTester); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 234 | }; |
| 235 | |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 236 | scoped_ptr<DesktopFrame> PrepareFrame(const DesktopSize& size) { |
| 237 | scoped_ptr<DesktopFrame> frame(new BasicDesktopFrame(size)); |
[email protected] | 04499fb | 2012-08-25 00:33:50 | [diff] [blame] | 238 | |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 239 | srand(0); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 240 | int memory_size = size.width() * size.height() * kBytesPerPixel; |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 241 | for (int i = 0; i < memory_size; ++i) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 242 | frame->data()[i] = rand() % 256; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 245 | return frame.Pass(); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 246 | } |
| 247 | |
[email protected] | 92b45b8e | 2012-08-24 19:46:47 | [diff] [blame] | 248 | static void TestEncodingRects(VideoEncoder* encoder, |
| 249 | VideoEncoderTester* tester, |
wez | 43ac266 | 2015-06-10 18:22:29 | [diff] [blame^] | 250 | DesktopFrame* frame, |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 251 | const DesktopRect* rects, |
| 252 | int count) { |
| 253 | frame->mutable_updated_region()->Clear(); |
[email protected] | 88552a9 | 2010-08-06 22:50:00 | [diff] [blame] | 254 | for (int i = 0; i < count; ++i) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 255 | frame->mutable_updated_region()->AddRect(rects[i]); |
[email protected] | 88552a9 | 2010-08-06 22:50:00 | [diff] [blame] | 256 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 257 | |
[email protected] | 8b6d3d0 | 2013-09-13 22:43:41 | [diff] [blame] | 258 | scoped_ptr<VideoPacket> packet = encoder->Encode(*frame); |
| 259 | tester->DataAvailable(packet.Pass()); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 260 | } |
| 261 | |
[email protected] | 92b45b8e | 2012-08-24 19:46:47 | [diff] [blame] | 262 | void TestVideoEncoder(VideoEncoder* encoder, bool strict) { |
[email protected] | 8c6d71d | 2012-10-23 21:20:04 | [diff] [blame] | 263 | const int kSizes[] = {320, 319, 317, 150}; |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 264 | |
[email protected] | 8b6d3d0 | 2013-09-13 22:43:41 | [diff] [blame] | 265 | VideoEncoderTester tester; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 266 | |
[email protected] | 8c6d71d | 2012-10-23 21:20:04 | [diff] [blame] | 267 | for (size_t xi = 0; xi < arraysize(kSizes); ++xi) { |
| 268 | for (size_t yi = 0; yi < arraysize(kSizes); ++yi) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 269 | DesktopSize size = DesktopSize(kSizes[xi], kSizes[yi]); |
wez | 43ac266 | 2015-06-10 18:22:29 | [diff] [blame^] | 270 | scoped_ptr<DesktopFrame> frame = PrepareFrame(size); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 271 | std::vector<std::vector<DesktopRect> > test_rect_lists = |
[email protected] | 8c6d71d | 2012-10-23 21:20:04 | [diff] [blame] | 272 | MakeTestRectLists(size); |
| 273 | for (size_t i = 0; i < test_rect_lists.size(); ++i) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 274 | const std::vector<DesktopRect>& test_rects = test_rect_lists[i]; |
| 275 | TestEncodingRects(encoder, &tester, frame.get(), |
[email protected] | 8c6d71d | 2012-10-23 21:20:04 | [diff] [blame] | 276 | &test_rects[0], test_rects.size()); |
| 277 | } |
wez | 43ac266 | 2015-06-10 18:22:29 | [diff] [blame^] | 278 | |
| 279 | // Pass some empty frames through the encoder. |
| 280 | for (int i = 0; i < 10; ++i) { |
| 281 | TestEncodingRects(encoder, &tester, frame.get(), nullptr, 0); |
| 282 | } |
[email protected] | 8c6d71d | 2012-10-23 21:20:04 | [diff] [blame] | 283 | } |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 284 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 285 | } |
| 286 | |
wez | 43ac266 | 2015-06-10 18:22:29 | [diff] [blame^] | 287 | void TestVideoEncoderEmptyFrames(VideoEncoder* encoder, int topoff_frames) { |
| 288 | const DesktopSize kSize(640, 480); |
| 289 | scoped_ptr<DesktopFrame> frame(PrepareFrame(kSize)); |
| 290 | |
| 291 | frame->mutable_updated_region()->SetRect( |
| 292 | webrtc::DesktopRect::MakeSize(kSize)); |
| 293 | EXPECT_TRUE(encoder->Encode(*frame)); |
| 294 | |
| 295 | frame->mutable_updated_region()->Clear(); |
| 296 | for (int i=0; i < topoff_frames; ++i) { |
| 297 | EXPECT_TRUE(encoder->Encode(*frame)); |
| 298 | } |
| 299 | |
| 300 | EXPECT_FALSE(encoder->Encode(*frame)); |
| 301 | } |
| 302 | |
[email protected] | 92b45b8e | 2012-08-24 19:46:47 | [diff] [blame] | 303 | static void TestEncodeDecodeRects(VideoEncoder* encoder, |
| 304 | VideoEncoderTester* encoder_tester, |
[email protected] | d9004e4 | 2012-08-24 01:47:37 | [diff] [blame] | 305 | VideoDecoderTester* decoder_tester, |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 306 | DesktopFrame* frame, |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 307 | const DesktopRect* rects, int count) { |
| 308 | frame->mutable_updated_region()->Clear(); |
| 309 | for (int i = 0; i < count; ++i) { |
| 310 | frame->mutable_updated_region()->AddRect(rects[i]); |
| 311 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 312 | decoder_tester->AddRects(rects, count); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 313 | |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 314 | // Generate random data for the updated region. |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 315 | srand(0); |
| 316 | for (int i = 0; i < count; ++i) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 317 | const int row_size = |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 318 | DesktopFrame::kBytesPerPixel * rects[i].width(); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 319 | uint8* memory = frame->data() + |
| 320 | frame->stride() * rects[i].top() + |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 321 | DesktopFrame::kBytesPerPixel * rects[i].left(); |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 322 | for (int y = 0; y < rects[i].height(); ++y) { |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 323 | for (int x = 0; x < row_size; ++x) |
| 324 | memory[x] = rand() % 256; |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 325 | memory += frame->stride(); |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
[email protected] | 8b6d3d0 | 2013-09-13 22:43:41 | [diff] [blame] | 329 | scoped_ptr<VideoPacket> packet = encoder->Encode(*frame); |
| 330 | encoder_tester->DataAvailable(packet.Pass()); |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 331 | decoder_tester->VerifyResults(); |
| 332 | decoder_tester->Reset(); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 333 | } |
| 334 | |
[email protected] | 92b45b8e | 2012-08-24 19:46:47 | [diff] [blame] | 335 | void TestVideoEncoderDecoder( |
| 336 | VideoEncoder* encoder, VideoDecoder* decoder, bool strict) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 337 | DesktopSize kSize = DesktopSize(320, 240); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 338 | |
[email protected] | 8b6d3d0 | 2013-09-13 22:43:41 | [diff] [blame] | 339 | VideoEncoderTester encoder_tester; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 340 | |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 341 | scoped_ptr<DesktopFrame> frame = PrepareFrame(kSize); |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 342 | |
[email protected] | d9004e4 | 2012-08-24 01:47:37 | [diff] [blame] | 343 | VideoDecoderTester decoder_tester(decoder, kSize, kSize); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 344 | decoder_tester.set_strict(strict); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 345 | decoder_tester.set_frame(frame.get()); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 346 | encoder_tester.set_decoder_tester(&decoder_tester); |
| 347 | |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 348 | std::vector<std::vector<DesktopRect> > test_rect_lists = |
| 349 | MakeTestRectLists(kSize); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 350 | for (size_t i = 0; i < test_rect_lists.size(); ++i) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 351 | const std::vector<DesktopRect> test_rects = test_rect_lists[i]; |
| 352 | TestEncodeDecodeRects(encoder, &encoder_tester, &decoder_tester, |
| 353 | frame.get(), &test_rects[0], test_rects.size()); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 354 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 355 | } |
| 356 | |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 357 | static void FillWithGradient(DesktopFrame* frame) { |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 358 | for (int j = 0; j < frame->size().height(); ++j) { |
| 359 | uint8* p = frame->data() + j * frame->stride(); |
| 360 | for (int i = 0; i < frame->size().width(); ++i) { |
| 361 | *p++ = (255.0 * i) / frame->size().width(); |
| 362 | *p++ = (164.0 * j) / frame->size().height(); |
| 363 | *p++ = (82.0 * (i + j)) / |
| 364 | (frame->size().width() + frame->size().height()); |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 365 | *p++ = 0; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
[email protected] | 92b45b8e | 2012-08-24 19:46:47 | [diff] [blame] | 370 | void TestVideoEncoderDecoderGradient(VideoEncoder* encoder, |
| 371 | VideoDecoder* decoder, |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 372 | const DesktopSize& screen_size, |
| 373 | const DesktopSize& view_size, |
[email protected] | 92b45b8e | 2012-08-24 19:46:47 | [diff] [blame] | 374 | double max_error_limit, |
| 375 | double mean_error_limit) { |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 376 | scoped_ptr<BasicDesktopFrame> frame( |
| 377 | new BasicDesktopFrame(screen_size)); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 378 | FillWithGradient(frame.get()); |
| 379 | frame->mutable_updated_region()->SetRect(DesktopRect::MakeSize(screen_size)); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 380 | |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 381 | scoped_ptr<BasicDesktopFrame> expected_result( |
| 382 | new BasicDesktopFrame(view_size)); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 383 | FillWithGradient(expected_result.get()); |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 384 | |
[email protected] | d9004e4 | 2012-08-24 01:47:37 | [diff] [blame] | 385 | VideoDecoderTester decoder_tester(decoder, screen_size, view_size); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 386 | decoder_tester.set_frame(frame.get()); |
| 387 | decoder_tester.AddRegion(frame->updated_region()); |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 388 | |
[email protected] | 8b6d3d0 | 2013-09-13 22:43:41 | [diff] [blame] | 389 | scoped_ptr<VideoPacket> packet = encoder->Encode(*frame); |
| 390 | decoder_tester.ReceivedScopedPacket(packet.Pass()); |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 391 | |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 392 | decoder_tester.VerifyResultsApprox(expected_result->data(), |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 393 | max_error_limit, mean_error_limit); |
| 394 | |
| 395 | // Check that the decoder correctly re-renders the frame if its client |
| 396 | // invalidates the frame. |
| 397 | decoder_tester.ResetRenderedData(); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 398 | decoder->Invalidate( |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 399 | DesktopSize(view_size.width(), view_size.height()), |
| 400 | DesktopRegion( |
| 401 | DesktopRect::MakeWH(view_size.width(), view_size.height()))); |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 402 | decoder_tester.RenderFrame(); |
[email protected] | b9ed58f | 2013-05-16 10:45:24 | [diff] [blame] | 403 | decoder_tester.VerifyResultsApprox(expected_result->data(), |
[email protected] | 1d65f48 | 2012-07-28 06:40:46 | [diff] [blame] | 404 | max_error_limit, mean_error_limit); |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame] | 405 | } |
| 406 | |
[email protected] | c5a0f601 | 2014-07-01 06:44:34 | [diff] [blame] | 407 | float MeasureVideoEncoderFpsWithSize(VideoEncoder* encoder, |
| 408 | const DesktopSize& size) { |
| 409 | scoped_ptr<DesktopFrame> frame(PrepareFrame(size)); |
| 410 | frame->mutable_updated_region()->SetRect(DesktopRect::MakeSize(size)); |
| 411 | std::list<DesktopFrame*> frames; |
| 412 | frames.push_back(frame.get()); |
| 413 | return MeasureVideoEncoderFpsWithFrames(encoder, frames); |
| 414 | } |
| 415 | |
| 416 | float MeasureVideoEncoderFpsWithFrames(VideoEncoder* encoder, |
| 417 | const std::list<DesktopFrame*>& frames) { |
| 418 | const base::TimeDelta kTestTime = base::TimeDelta::FromSeconds(1); |
| 419 | |
| 420 | // Encode some frames to "warm up" the encoder (i.e. to let it set up initial |
| 421 | // structures, establish a stable working set, etc), then encode at least |
| 422 | // kMinimumFrameCount frames to measure the encoder's performance. |
| 423 | const int kWarmUpFrameCount = 10; |
| 424 | const int kMinimumFrameCount = 10; |
| 425 | base::TimeTicks start_time; |
| 426 | base::TimeDelta elapsed; |
| 427 | std::list<DesktopFrame*> test_frames; |
| 428 | int frame_count; |
| 429 | for (frame_count = 0; |
| 430 | (frame_count < kMinimumFrameCount + kWarmUpFrameCount || |
| 431 | elapsed < kTestTime); |
| 432 | ++frame_count) { |
| 433 | if (frame_count == kWarmUpFrameCount) { |
| 434 | start_time = base::TimeTicks::Now(); |
| 435 | } |
| 436 | |
| 437 | if (test_frames.empty()) { |
| 438 | test_frames = frames; |
| 439 | } |
| 440 | scoped_ptr<VideoPacket> packet = encoder->Encode(*test_frames.front()); |
| 441 | test_frames.pop_front(); |
| 442 | |
| 443 | if (frame_count >= kWarmUpFrameCount) { |
| 444 | elapsed = base::TimeTicks::Now() - start_time; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | return (frame_count * base::TimeDelta::FromSeconds(1)) / elapsed; |
| 449 | } |
| 450 | |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 451 | } // namespace remoting |