[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] | 28dc421 | 2011-02-08 20:44:00 | [diff] [blame] | 12 | #include "remoting/base/base_mock_objects.h" |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 13 | #include "remoting/base/codec_test.h" |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 14 | #include "remoting/base/decoder.h" |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 15 | #include "remoting/base/encoder.h" |
[email protected] | c3af26f33 | 2010-10-06 22:46:00 | [diff] [blame] | 16 | #include "remoting/base/util.h" |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | static const int kWidth = 320; |
| 20 | static const int kHeight = 240; |
| 21 | static const int kBytesPerPixel = 4; |
| 22 | |
| 23 | // Some sample rects for testing. |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 24 | static const SkIRect kTestRects[] = { |
| 25 | SkIRect::MakeXYWH(0, 0, kWidth, kHeight), |
| 26 | SkIRect::MakeXYWH(0, 0, kWidth / 2, kHeight / 2), |
| 27 | SkIRect::MakeXYWH(kWidth / 2, kHeight / 2, kWidth / 2, kHeight / 2), |
| 28 | SkIRect::MakeXYWH(16, 16, 16, 16), |
| 29 | SkIRect::MakeXYWH(128, 64, 32, 32), |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | namespace remoting { |
| 33 | |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 34 | // A class to test the message output of the encoder. |
| 35 | class EncoderMessageTester { |
| 36 | public: |
| 37 | EncoderMessageTester() |
| 38 | : begin_rect_(0), |
| 39 | rect_data_(0), |
| 40 | end_rect_(0), |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 41 | added_rects_(0), |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 42 | state_(kWaitingForBeginRect), |
| 43 | strict_(false) { |
| 44 | } |
| 45 | |
| 46 | ~EncoderMessageTester() { |
| 47 | EXPECT_EQ(begin_rect_, end_rect_); |
[email protected] | df10bf1 | 2010-09-28 22:19:48 | [diff] [blame] | 48 | EXPECT_GT(begin_rect_, 0); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 49 | EXPECT_EQ(kWaitingForBeginRect, state_); |
[email protected] | df10bf1 | 2010-09-28 22:19:48 | [diff] [blame] | 50 | if (strict_) { |
| 51 | EXPECT_EQ(added_rects_, begin_rect_); |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 52 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 55 | // Test that we received the correct packet. |
| 56 | void ReceivedPacket(VideoPacket* packet) { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 57 | if (state_ == kWaitingForBeginRect) { |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 58 | EXPECT_TRUE((packet->flags() & VideoPacket::FIRST_PACKET) != 0); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 59 | state_ = kWaitingForRectData; |
| 60 | ++begin_rect_; |
| 61 | |
| 62 | if (strict_) { |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 63 | SkIRect rect = rects_.front(); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 64 | rects_.pop_front(); |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 65 | EXPECT_EQ(rect.fLeft, packet->format().x()); |
| 66 | EXPECT_EQ(rect.fTop, packet->format().y()); |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 67 | EXPECT_EQ(rect.width(), packet->format().width()); |
| 68 | EXPECT_EQ(rect.height(), packet->format().height()); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 69 | } |
| 70 | } else { |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 71 | EXPECT_FALSE((packet->flags() & VideoPacket::FIRST_PACKET) != 0); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (state_ == kWaitingForRectData) { |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 75 | if (packet->has_data()) { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 76 | ++rect_data_; |
| 77 | } |
| 78 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 79 | if ((packet->flags() & VideoPacket::LAST_PACKET) != 0) { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 80 | // Expect that we have received some data. |
| 81 | EXPECT_GT(rect_data_, 0); |
| 82 | rect_data_ = 0; |
| 83 | state_ = kWaitingForBeginRect; |
| 84 | ++end_rect_; |
| 85 | } |
[email protected] | 5bc7183 | 2010-12-09 01:34:08 | [diff] [blame] | 86 | |
| 87 | if ((packet->flags() & VideoPacket::LAST_PARTITION) != 0) { |
| 88 | // LAST_PARTITION must always be marked with LAST_PACKET. |
| 89 | EXPECT_TRUE((packet->flags() & VideoPacket::LAST_PACKET) != 0); |
| 90 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | void set_strict(bool strict) { |
| 95 | strict_ = strict; |
| 96 | } |
| 97 | |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 98 | void AddRects(const SkIRect* rects, int count) { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 99 | rects_.insert(rects_.begin() + rects_.size(), rects, rects + count); |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 100 | added_rects_ += count; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private: |
| 104 | enum State { |
| 105 | kWaitingForBeginRect, |
| 106 | kWaitingForRectData, |
| 107 | }; |
| 108 | |
| 109 | int begin_rect_; |
| 110 | int rect_data_; |
| 111 | int end_rect_; |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 112 | int added_rects_; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 113 | State state_; |
| 114 | bool strict_; |
| 115 | |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 116 | std::deque<SkIRect> rects_; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 117 | |
| 118 | DISALLOW_COPY_AND_ASSIGN(EncoderMessageTester); |
| 119 | }; |
| 120 | |
| 121 | class DecoderTester { |
| 122 | public: |
[email protected] | 542bdfe | 2010-11-30 03:55:47 | [diff] [blame] | 123 | DecoderTester(Decoder* decoder) |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 124 | : strict_(false), |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 125 | decoder_(decoder) { |
[email protected] | 55d3688e | 2012-02-24 23:05:56 | [diff] [blame] | 126 | image_data_.reset(new uint8[kWidth * kHeight * kBytesPerPixel]); |
| 127 | EXPECT_TRUE(image_data_.get()); |
| 128 | decoder_->Initialize(SkISize::Make(kWidth, kHeight)); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 129 | } |
| 130 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 131 | void Reset() { |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 132 | expected_region_.setEmpty(); |
| 133 | update_region_.setEmpty(); |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 134 | } |
[email protected] | cee5dba | 2010-12-07 03:19:48 | [diff] [blame] | 135 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 136 | void ReceivedPacket(VideoPacket* packet) { |
| 137 | Decoder::DecodeResult result = decoder_->DecodePacket(packet); |
[email protected] | cee5dba | 2010-12-07 03:19:48 | [diff] [blame] | 138 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 139 | ASSERT_NE(Decoder::DECODE_ERROR, result); |
| 140 | |
| 141 | if (result == Decoder::DECODE_DONE) { |
[email protected] | 55d3688e | 2012-02-24 23:05:56 | [diff] [blame] | 142 | decoder_->RenderFrame(SkISize::Make(kWidth, kHeight), |
| 143 | SkIRect::MakeXYWH(0, 0, kWidth, kHeight), |
| 144 | image_data_.get(), |
| 145 | kWidth * kBytesPerPixel, |
| 146 | &update_region_); |
[email protected] | cee5dba | 2010-12-07 03:19:48 | [diff] [blame] | 147 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame^] | 150 | void ReceivedScopedPacket(scoped_ptr<VideoPacket> packet) { |
| 151 | ReceivedPacket(packet.get()); |
| 152 | } |
| 153 | |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 154 | void set_strict(bool strict) { |
| 155 | strict_ = strict; |
| 156 | } |
| 157 | |
| 158 | void set_capture_data(scoped_refptr<CaptureData> data) { |
| 159 | capture_data_ = data; |
| 160 | } |
| 161 | |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 162 | void AddRects(const SkIRect* rects, int count) { |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 163 | SkRegion new_rects; |
| 164 | new_rects.setRects(rects, count); |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame^] | 165 | AddRegion(new_rects); |
| 166 | } |
| 167 | |
| 168 | void AddRegion(const SkRegion& region) { |
| 169 | expected_region_.op(region, SkRegion::kUnion_Op); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 172 | void VerifyResults() { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 173 | if (!strict_) |
| 174 | return; |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 175 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 176 | ASSERT_TRUE(capture_data_.get()); |
| 177 | |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 178 | // Test the content of the update region. |
| 179 | EXPECT_EQ(expected_region_, update_region_); |
| 180 | for (SkRegion::Iterator i(update_region_); !i.done(); i.next()) { |
[email protected] | 55d3688e | 2012-02-24 23:05:56 | [diff] [blame] | 181 | const int stride = kWidth * kBytesPerPixel; |
| 182 | EXPECT_EQ(stride, capture_data_->data_planes().strides[0]); |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 183 | const int offset = stride * i.rect().top() + |
| 184 | kBytesPerPixel * i.rect().left(); |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 185 | const uint8* original = capture_data_->data_planes().data[0] + offset; |
[email protected] | 55d3688e | 2012-02-24 23:05:56 | [diff] [blame] | 186 | const uint8* decoded = image_data_.get() + offset; |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 187 | const int row_size = kBytesPerPixel * i.rect().width(); |
| 188 | for (int y = 0; y < i.rect().height(); ++y) { |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 189 | EXPECT_EQ(0, memcmp(original, decoded, row_size)) |
| 190 | << "Row " << y << " is different"; |
| 191 | original += stride; |
| 192 | decoded += stride; |
| 193 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame^] | 197 | // The error at each pixel is the root mean square of the errors in |
| 198 | // the R, G, and B components, each normalized to [0, 1]. This routine |
| 199 | // checks that the maximum and mean pixel errors do not exceed given limits. |
| 200 | void VerifyResultsApprox(double max_error_limit, double mean_error_limit) { |
| 201 | ASSERT_TRUE(capture_data_.get()); |
| 202 | |
| 203 | // Test the content of the update region. |
| 204 | EXPECT_EQ(expected_region_, update_region_); |
| 205 | double max_error = 0.0; |
| 206 | double sum_error = 0.0; |
| 207 | int error_num = 0; |
| 208 | for (SkRegion::Iterator i(update_region_); !i.done(); i.next()) { |
| 209 | const int stride = kWidth * kBytesPerPixel; |
| 210 | EXPECT_EQ(stride, capture_data_->data_planes().strides[0]); |
| 211 | const int offset = stride * i.rect().top() + |
| 212 | kBytesPerPixel * i.rect().left(); |
| 213 | const uint8* original = capture_data_->data_planes().data[0] + offset; |
| 214 | const uint8* decoded = image_data_.get() + offset; |
| 215 | for (int y = 0; y < i.rect().height(); ++y) { |
| 216 | for (int x = 0; x < i.rect().width(); ++x) { |
| 217 | double error = CalculateError(original, decoded); |
| 218 | max_error = std::max(max_error, error); |
| 219 | sum_error += error; |
| 220 | ++error_num; |
| 221 | original += 4; |
| 222 | decoded += 4; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | EXPECT_LE(max_error, max_error_limit); |
| 227 | double mean_error = sum_error / error_num; |
| 228 | EXPECT_LE(mean_error, mean_error_limit); |
| 229 | LOG(INFO) << "Max error: " << max_error; |
| 230 | LOG(INFO) << "Mean error: " << mean_error; |
| 231 | } |
| 232 | |
| 233 | double CalculateError(const uint8* original, const uint8* decoded) { |
| 234 | double error_sum_squares = 0.0; |
| 235 | for (int i = 0; i < 3; i++) { |
| 236 | double error = static_cast<double>(*original++) - |
| 237 | static_cast<double>(*decoded++); |
| 238 | error /= 255.0; |
| 239 | error_sum_squares += error * error; |
| 240 | } |
| 241 | original++; |
| 242 | decoded++; |
| 243 | return sqrt(error_sum_squares / 3.0); |
| 244 | } |
| 245 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 246 | private: |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 247 | bool strict_; |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 248 | SkRegion expected_region_; |
| 249 | SkRegion update_region_; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 250 | Decoder* decoder_; |
[email protected] | 55d3688e | 2012-02-24 23:05:56 | [diff] [blame] | 251 | scoped_array<uint8> image_data_; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 252 | scoped_refptr<CaptureData> capture_data_; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 253 | |
| 254 | DISALLOW_COPY_AND_ASSIGN(DecoderTester); |
| 255 | }; |
| 256 | |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 257 | // The EncoderTester provides a hook for retrieving the data, and passing the |
| 258 | // message to other subprograms for validaton. |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 259 | class EncoderTester { |
| 260 | public: |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 261 | EncoderTester(EncoderMessageTester* message_tester) |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 262 | : message_tester_(message_tester), |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 263 | decoder_tester_(NULL), |
| 264 | data_available_(0) { |
| 265 | } |
| 266 | |
| 267 | ~EncoderTester() { |
| 268 | EXPECT_GT(data_available_, 0); |
| 269 | } |
| 270 | |
[email protected] | 3361e1f | 2012-03-20 20:31:44 | [diff] [blame] | 271 | void DataAvailable(scoped_ptr<VideoPacket> packet) { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 272 | ++data_available_; |
[email protected] | 3361e1f | 2012-03-20 20:31:44 | [diff] [blame] | 273 | message_tester_->ReceivedPacket(packet.get()); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 274 | |
| 275 | // Send the message to the DecoderTester. |
| 276 | if (decoder_tester_) { |
[email protected] | 3361e1f | 2012-03-20 20:31:44 | [diff] [blame] | 277 | decoder_tester_->ReceivedPacket(packet.get()); |
[email protected] | cee5dba | 2010-12-07 03:19:48 | [diff] [blame] | 278 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 281 | void AddRects(const SkIRect* rects, int count) { |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 282 | message_tester_->AddRects(rects, count); |
| 283 | } |
| 284 | |
| 285 | void set_decoder_tester(DecoderTester* decoder_tester) { |
| 286 | decoder_tester_ = decoder_tester; |
| 287 | } |
| 288 | |
| 289 | private: |
| 290 | EncoderMessageTester* message_tester_; |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 291 | DecoderTester* decoder_tester_; |
| 292 | int data_available_; |
| 293 | |
| 294 | DISALLOW_COPY_AND_ASSIGN(EncoderTester); |
| 295 | }; |
| 296 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 297 | scoped_refptr<CaptureData> PrepareEncodeData(media::VideoFrame::Format format, |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 298 | uint8** memory) { |
| 299 | // TODO(hclam): Support also YUV format. |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 300 | CHECK_EQ(format, media::VideoFrame::RGB32); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 301 | int size = kWidth * kHeight * kBytesPerPixel; |
| 302 | |
| 303 | *memory = new uint8[size]; |
| 304 | srand(0); |
| 305 | for (int i = 0; i < size; ++i) { |
| 306 | (*memory)[i] = rand() % 256; |
| 307 | } |
| 308 | |
| 309 | DataPlanes planes; |
| 310 | memset(planes.data, 0, sizeof(planes.data)); |
| 311 | memset(planes.strides, 0, sizeof(planes.strides)); |
| 312 | planes.data[0] = *memory; |
| 313 | planes.strides[0] = kWidth * kBytesPerPixel; |
| 314 | |
| 315 | scoped_refptr<CaptureData> data = |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 316 | new CaptureData(planes, SkISize::Make(kWidth, kHeight), format); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 317 | return data; |
| 318 | } |
| 319 | |
| 320 | static void TestEncodingRects(Encoder* encoder, |
| 321 | EncoderTester* tester, |
| 322 | scoped_refptr<CaptureData> data, |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 323 | const SkIRect* rects, int count) { |
| 324 | data->mutable_dirty_region().setEmpty(); |
[email protected] | 88552a9 | 2010-08-06 22:50:00 | [diff] [blame] | 325 | for (int i = 0; i < count; ++i) { |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 326 | data->mutable_dirty_region().op(rects[i], SkRegion::kUnion_Op); |
[email protected] | 88552a9 | 2010-08-06 22:50:00 | [diff] [blame] | 327 | } |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 328 | tester->AddRects(rects, count); |
| 329 | |
[email protected] | 1e1cb3b | 2011-11-10 02:07:41 | [diff] [blame] | 330 | encoder->Encode(data, true, base::Bind( |
| 331 | &EncoderTester::DataAvailable, base::Unretained(tester))); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void TestEncoder(Encoder* encoder, bool strict) { |
| 335 | EncoderMessageTester message_tester; |
| 336 | message_tester.set_strict(strict); |
| 337 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 338 | EncoderTester tester(&message_tester); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 339 | |
| 340 | uint8* memory; |
| 341 | scoped_refptr<CaptureData> data = |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 342 | PrepareEncodeData(media::VideoFrame::RGB32, &memory); |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 343 | scoped_array<uint8> memory_wrapper(memory); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 344 | |
| 345 | TestEncodingRects(encoder, &tester, data, kTestRects, 1); |
| 346 | TestEncodingRects(encoder, &tester, data, kTestRects + 1, 1); |
| 347 | TestEncodingRects(encoder, &tester, data, kTestRects + 2, 1); |
| 348 | TestEncodingRects(encoder, &tester, data, kTestRects + 3, 2); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 349 | } |
| 350 | |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 351 | static void TestEncodeDecodeRects(Encoder* encoder, |
| 352 | EncoderTester* encoder_tester, |
| 353 | DecoderTester* decoder_tester, |
| 354 | scoped_refptr<CaptureData> data, |
| 355 | const SkIRect* rects, int count) { |
| 356 | data->mutable_dirty_region().setRects(rects, count); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 357 | encoder_tester->AddRects(rects, count); |
| 358 | decoder_tester->AddRects(rects, count); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 359 | |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 360 | // Generate random data for the updated region. |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 361 | srand(0); |
| 362 | for (int i = 0; i < count; ++i) { |
[email protected] | f402ba03 | 2012-04-11 00:17:16 | [diff] [blame] | 363 | CHECK_EQ(data->pixel_format(), media::VideoFrame::RGB32); |
| 364 | const int bytes_per_pixel = 4; // Because of RGB32 on previous line. |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 365 | const int row_size = bytes_per_pixel * rects[i].width(); |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 366 | uint8* memory = data->data_planes().data[0] + |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 367 | data->data_planes().strides[0] * rects[i].top() + |
| 368 | bytes_per_pixel * rects[i].left(); |
| 369 | for (int y = 0; y < rects[i].height(); ++y) { |
[email protected] | 66db1af9 | 2010-07-27 01:42:46 | [diff] [blame] | 370 | for (int x = 0; x < row_size; ++x) |
| 371 | memory[x] = rand() % 256; |
| 372 | memory += data->data_planes().strides[0]; |
| 373 | } |
| 374 | } |
| 375 | |
[email protected] | 1e1cb3b | 2011-11-10 02:07:41 | [diff] [blame] | 376 | encoder->Encode(data, true, base::Bind(&EncoderTester::DataAvailable, |
| 377 | base::Unretained(encoder_tester))); |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 378 | decoder_tester->VerifyResults(); |
| 379 | decoder_tester->Reset(); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void TestEncoderDecoder(Encoder* encoder, Decoder* decoder, bool strict) { |
| 383 | EncoderMessageTester message_tester; |
| 384 | message_tester.set_strict(strict); |
| 385 | |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 386 | EncoderTester encoder_tester(&message_tester); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 387 | |
| 388 | uint8* memory; |
| 389 | scoped_refptr<CaptureData> data = |
[email protected] | 137e58f1 | 2010-12-07 19:35:43 | [diff] [blame] | 390 | PrepareEncodeData(media::VideoFrame::RGB32, &memory); |
[email protected] | dc454a7c | 2011-08-12 22:01:13 | [diff] [blame] | 391 | scoped_array<uint8> memory_wrapper(memory); |
| 392 | |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 393 | DecoderTester decoder_tester(decoder); |
| 394 | decoder_tester.set_strict(strict); |
| 395 | decoder_tester.set_capture_data(data); |
| 396 | encoder_tester.set_decoder_tester(&decoder_tester); |
| 397 | |
[email protected] | a9c88d1 | 2012-01-29 03:45:17 | [diff] [blame] | 398 | TestEncodeDecodeRects(encoder, &encoder_tester, &decoder_tester, data, |
| 399 | kTestRects, 1); |
| 400 | TestEncodeDecodeRects(encoder, &encoder_tester, &decoder_tester, data, |
| 401 | kTestRects + 1, 1); |
| 402 | TestEncodeDecodeRects(encoder, &encoder_tester, &decoder_tester, data, |
| 403 | kTestRects + 2, 1); |
| 404 | TestEncodeDecodeRects(encoder, &encoder_tester, &decoder_tester, data, |
| 405 | kTestRects + 3, 2); |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 406 | } |
| 407 | |
[email protected] | 36b967f | 2012-07-27 18:35:32 | [diff] [blame^] | 408 | static void FillWithGradient(uint8* memory, const SkISize& frame_size, |
| 409 | const SkIRect& rect) { |
| 410 | for (int j = rect.top(); j < rect.bottom(); ++j) { |
| 411 | uint8* p = memory + ((j * frame_size.width()) + rect.left()) * 4; |
| 412 | for (int i = rect.left(); i < rect.right(); ++i) { |
| 413 | *p++ = static_cast<uint8>((255.0 * i) / frame_size.width()); |
| 414 | *p++ = static_cast<uint8>((164.0 * j) / frame_size.height()); |
| 415 | *p++ = static_cast<uint8>((82.0 * (i + j)) / |
| 416 | (frame_size.width() + frame_size.height())); |
| 417 | *p++ = 0; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void TestEncoderDecoderGradient(Encoder* encoder, |
| 423 | Decoder* decoder, |
| 424 | double max_error_limit, |
| 425 | double mean_error_limit) { |
| 426 | SkIRect full_frame = SkIRect::MakeWH(kWidth, kHeight); |
| 427 | scoped_array<uint8> frame_data(new uint8[kWidth * kHeight * kBytesPerPixel]); |
| 428 | FillWithGradient(frame_data.get(), SkISize::Make(kWidth, kHeight), |
| 429 | full_frame); |
| 430 | |
| 431 | DataPlanes planes; |
| 432 | memset(planes.data, 0, sizeof(planes.data)); |
| 433 | memset(planes.strides, 0, sizeof(planes.strides)); |
| 434 | planes.data[0] = frame_data.get(); |
| 435 | planes.strides[0] = kWidth * kBytesPerPixel; |
| 436 | |
| 437 | scoped_refptr<CaptureData> capture_data = |
| 438 | new CaptureData(planes, SkISize::Make(kWidth, kHeight), |
| 439 | media::VideoFrame::RGB32); |
| 440 | capture_data->mutable_dirty_region().op(full_frame, SkRegion::kUnion_Op); |
| 441 | |
| 442 | DecoderTester decoder_tester(decoder); |
| 443 | decoder_tester.set_capture_data(capture_data); |
| 444 | decoder_tester.AddRegion(capture_data->dirty_region()); |
| 445 | |
| 446 | encoder->Encode(capture_data, true, |
| 447 | base::Bind(&DecoderTester::ReceivedScopedPacket, |
| 448 | base::Unretained(&decoder_tester))); |
| 449 | |
| 450 | decoder_tester.VerifyResultsApprox(max_error_limit, mean_error_limit); |
| 451 | } |
| 452 | |
[email protected] | fb13c76 | 2010-07-24 02:22:07 | [diff] [blame] | 453 | } // namespace remoting |