blob: eff143d1c6e1dca9d1fc8c926ccf023eb0b84997 [file] [log] [blame]
[email protected]a9c88d12012-01-29 03:45:171// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]fb13c762010-07-24 02:22:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]542bdfe2010-11-30 03:55:475#include <deque>
[email protected]fb13c762010-07-24 02:22:076#include <stdlib.h>
7
[email protected]1e1cb3b2011-11-10 02:07:418#include "base/bind.h"
9#include "base/logging.h"
[email protected]dc454a7c2011-08-12 22:01:1310#include "base/memory/scoped_ptr.h"
[email protected]fb13c762010-07-24 02:22:0711#include "media/base/video_frame.h"
[email protected]38609832012-08-23 01:36:1012#include "remoting/codec/codec_test.h"
13#include "remoting/codec/video_decoder.h"
14#include "remoting/codec/video_encoder.h"
[email protected]c3af26f332010-10-06 22:46:0015#include "remoting/base/util.h"
[email protected]fb13c762010-07-24 02:22:0716#include "testing/gtest/include/gtest/gtest.h"
[email protected]b9ed58f2013-05-16 10:45:2417#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
18
19using webrtc::DesktopRect;
[email protected]f301d7362013-06-05 00:02:0220using webrtc::DesktopRegion;
[email protected]b9ed58f2013-05-16 10:45:2421using webrtc::DesktopSize;
[email protected]fb13c762010-07-24 02:22:0722
[email protected]1d65f482012-07-28 06:40:4623namespace {
24
25const int kBytesPerPixel = 4;
[email protected]fb13c762010-07-24 02:22:0726
27// Some sample rects for testing.
[email protected]b9ed58f2013-05-16 10:45:2428std::vector<std::vector<DesktopRect> > MakeTestRectLists(DesktopSize size) {
29 std::vector<std::vector<DesktopRect> > rect_lists;
30 std::vector<DesktopRect> rects;
31 rects.push_back(DesktopRect::MakeXYWH(0, 0, size.width(), size.height()));
[email protected]1d65f482012-07-28 06:40:4632 rect_lists.push_back(rects);
33 rects.clear();
[email protected]b9ed58f2013-05-16 10:45:2434 rects.push_back(DesktopRect::MakeXYWH(
35 0, 0, size.width() / 2, size.height() / 2));
[email protected]1d65f482012-07-28 06:40:4636 rect_lists.push_back(rects);
37 rects.clear();
[email protected]b9ed58f2013-05-16 10:45:2438 rects.push_back(DesktopRect::MakeXYWH(
39 size.width() / 2, size.height() / 2,
40 size.width() / 2, size.height() / 2));
[email protected]1d65f482012-07-28 06:40:4641 rect_lists.push_back(rects);
42 rects.clear();
[email protected]b9ed58f2013-05-16 10:45:2443 rects.push_back(DesktopRect::MakeXYWH(16, 16, 16, 16));
44 rects.push_back(DesktopRect::MakeXYWH(128, 64, 32, 32));
[email protected]1d65f482012-07-28 06:40:4645 rect_lists.push_back(rects);
46 return rect_lists;
47}
48
49} // namespace
[email protected]fb13c762010-07-24 02:22:0750
51namespace remoting {
52
[email protected]fb13c762010-07-24 02:22:0753// A class to test the message output of the encoder.
[email protected]92b45b8e2012-08-24 19:46:4754class VideoEncoderMessageTester {
[email protected]fb13c762010-07-24 02:22:0755 public:
[email protected]92b45b8e2012-08-24 19:46:4756 VideoEncoderMessageTester()
[email protected]fb13c762010-07-24 02:22:0757 : begin_rect_(0),
58 rect_data_(0),
59 end_rect_(0),
60 state_(kWaitingForBeginRect),
61 strict_(false) {
62 }
63
[email protected]92b45b8e2012-08-24 19:46:4764 ~VideoEncoderMessageTester() {
[email protected]fb13c762010-07-24 02:22:0765 EXPECT_EQ(begin_rect_, end_rect_);
[email protected]df10bf12010-09-28 22:19:4866 EXPECT_GT(begin_rect_, 0);
[email protected]fb13c762010-07-24 02:22:0767 EXPECT_EQ(kWaitingForBeginRect, state_);
[email protected]df10bf12010-09-28 22:19:4868 if (strict_) {
[email protected]f301d7362013-06-05 00:02:0269 EXPECT_TRUE(region_.Equals(received_region_));
[email protected]66db1af92010-07-27 01:42:4670 }
[email protected]fb13c762010-07-24 02:22:0771 }
72
[email protected]137e58f12010-12-07 19:35:4373 // Test that we received the correct packet.
74 void ReceivedPacket(VideoPacket* packet) {
[email protected]fb13c762010-07-24 02:22:0775 if (state_ == kWaitingForBeginRect) {
[email protected]137e58f12010-12-07 19:35:4376 EXPECT_TRUE((packet->flags() & VideoPacket::FIRST_PACKET) != 0);
[email protected]fb13c762010-07-24 02:22:0777 state_ = kWaitingForRectData;
78 ++begin_rect_;
79
80 if (strict_) {
[email protected]f301d7362013-06-05 00:02:0281 received_region_.AddRect(webrtc::DesktopRect::MakeXYWH(
82 packet->format().x(), packet->format().y(),
83 packet->format().width(), packet->format().height()));
[email protected]fb13c762010-07-24 02:22:0784 }
85 } else {
[email protected]137e58f12010-12-07 19:35:4386 EXPECT_FALSE((packet->flags() & VideoPacket::FIRST_PACKET) != 0);
[email protected]fb13c762010-07-24 02:22:0787 }
88
89 if (state_ == kWaitingForRectData) {
[email protected]137e58f12010-12-07 19:35:4390 if (packet->has_data()) {
[email protected]fb13c762010-07-24 02:22:0791 ++rect_data_;
92 }
93
[email protected]137e58f12010-12-07 19:35:4394 if ((packet->flags() & VideoPacket::LAST_PACKET) != 0) {
[email protected]fb13c762010-07-24 02:22:0795 // Expect that we have received some data.
96 EXPECT_GT(rect_data_, 0);
97 rect_data_ = 0;
98 state_ = kWaitingForBeginRect;
99 ++end_rect_;
100 }
[email protected]5bc71832010-12-09 01:34:08101
102 if ((packet->flags() & VideoPacket::LAST_PARTITION) != 0) {
103 // LAST_PARTITION must always be marked with LAST_PACKET.
104 EXPECT_TRUE((packet->flags() & VideoPacket::LAST_PACKET) != 0);
105 }
[email protected]fb13c762010-07-24 02:22:07106 }
107 }
108
109 void set_strict(bool strict) {
110 strict_ = strict;
111 }
112
[email protected]b9ed58f2013-05-16 10:45:24113 void AddRects(const DesktopRect* rects, int count) {
[email protected]f301d7362013-06-05 00:02:02114 region_.AddRects(rects, count);
[email protected]fb13c762010-07-24 02:22:07115 }
116
117 private:
118 enum State {
119 kWaitingForBeginRect,
120 kWaitingForRectData,
121 };
122
123 int begin_rect_;
124 int rect_data_;
125 int end_rect_;
126 State state_;
127 bool strict_;
128
[email protected]f301d7362013-06-05 00:02:02129 DesktopRegion region_;
130 DesktopRegion received_region_;
[email protected]fb13c762010-07-24 02:22:07131
[email protected]92b45b8e2012-08-24 19:46:47132 DISALLOW_COPY_AND_ASSIGN(VideoEncoderMessageTester);
[email protected]fb13c762010-07-24 02:22:07133};
134
[email protected]d9004e42012-08-24 01:47:37135class VideoDecoderTester {
[email protected]fb13c762010-07-24 02:22:07136 public:
[email protected]b9ed58f2013-05-16 10:45:24137 VideoDecoderTester(VideoDecoder* decoder,
138 const DesktopSize& screen_size,
139 const DesktopSize& view_size)
[email protected]1d65f482012-07-28 06:40:46140 : screen_size_(screen_size),
141 view_size_(view_size),
142 strict_(false),
[email protected]a3c1d452013-08-21 18:51:40143 decoder_(decoder),
144 frame_(NULL) {
[email protected]1d65f482012-07-28 06:40:46145 image_data_.reset(new uint8[
146 view_size_.width() * view_size_.height() * kBytesPerPixel]);
[email protected]55d3688e2012-02-24 23:05:56147 EXPECT_TRUE(image_data_.get());
[email protected]b9ed58f2013-05-16 10:45:24148 decoder_->Initialize(
149 SkISize::Make(screen_size_.width(), screen_size_.height()));
[email protected]fb13c762010-07-24 02:22:07150 }
151
[email protected]137e58f12010-12-07 19:35:43152 void Reset() {
[email protected]b9ed58f2013-05-16 10:45:24153 expected_region_.Clear();
[email protected]a9c88d12012-01-29 03:45:17154 update_region_.setEmpty();
[email protected]137e58f12010-12-07 19:35:43155 }
[email protected]cee5dba2010-12-07 03:19:48156
[email protected]1d65f482012-07-28 06:40:46157 void ResetRenderedData() {
158 memset(image_data_.get(), 0,
159 view_size_.width() * view_size_.height() * kBytesPerPixel);
160 }
161
[email protected]137e58f12010-12-07 19:35:43162 void ReceivedPacket(VideoPacket* packet) {
[email protected]d9004e42012-08-24 01:47:37163 VideoDecoder::DecodeResult result = decoder_->DecodePacket(packet);
[email protected]cee5dba2010-12-07 03:19:48164
[email protected]d9004e42012-08-24 01:47:37165 ASSERT_NE(VideoDecoder::DECODE_ERROR, result);
[email protected]137e58f12010-12-07 19:35:43166
[email protected]d9004e42012-08-24 01:47:37167 if (result == VideoDecoder::DECODE_DONE) {
[email protected]1d65f482012-07-28 06:40:46168 RenderFrame();
[email protected]cee5dba2010-12-07 03:19:48169 }
[email protected]fb13c762010-07-24 02:22:07170 }
171
[email protected]1d65f482012-07-28 06:40:46172 void RenderFrame() {
[email protected]b9ed58f2013-05-16 10:45:24173 decoder_->RenderFrame(
174 SkISize::Make(view_size_.width(), view_size_.height()),
175 SkIRect::MakeWH(view_size_.width(), view_size_.height()),
176 image_data_.get(),
177 view_size_.width() * kBytesPerPixel,
178 &update_region_);
[email protected]1d65f482012-07-28 06:40:46179 }
180
[email protected]36b967f2012-07-27 18:35:32181 void ReceivedScopedPacket(scoped_ptr<VideoPacket> packet) {
182 ReceivedPacket(packet.get());
183 }
184
[email protected]fb13c762010-07-24 02:22:07185 void set_strict(bool strict) {
186 strict_ = strict;
187 }
188
[email protected]b9ed58f2013-05-16 10:45:24189 void set_frame(webrtc::DesktopFrame* frame) {
190 frame_ = frame;
[email protected]fb13c762010-07-24 02:22:07191 }
192
[email protected]b9ed58f2013-05-16 10:45:24193 void AddRects(const DesktopRect* rects, int count) {
194 for (int i = 0; i < count; ++i) {
195 expected_region_.AddRect(rects[i]);
196 }
[email protected]36b967f2012-07-27 18:35:32197 }
198
[email protected]b9ed58f2013-05-16 10:45:24199 void AddRegion(const webrtc::DesktopRegion& region) {
200 expected_region_.AddRegion(region);
[email protected]fb13c762010-07-24 02:22:07201 }
202
[email protected]137e58f12010-12-07 19:35:43203 void VerifyResults() {
[email protected]fb13c762010-07-24 02:22:07204 if (!strict_)
205 return;
[email protected]66db1af92010-07-27 01:42:46206
[email protected]b9ed58f2013-05-16 10:45:24207 ASSERT_TRUE(frame_);
[email protected]137e58f12010-12-07 19:35:43208
[email protected]a9c88d12012-01-29 03:45:17209 // Test the content of the update region.
[email protected]b9ed58f2013-05-16 10:45:24210 //
211 // TODO(sergeyu): Change this to use DesktopRegion when it's capable of
212 // merging the rectangles.
213 SkRegion expected_region;
214 for (webrtc::DesktopRegion::Iterator it(expected_region_);
215 !it.IsAtEnd(); it.Advance()) {
216 expected_region.op(
217 SkIRect::MakeXYWH(it.rect().top(), it.rect().left(),
218 it.rect().width(), it.rect().height()),
219 SkRegion::kUnion_Op);
220 }
221 EXPECT_EQ(expected_region, update_region_);
222
[email protected]a9c88d12012-01-29 03:45:17223 for (SkRegion::Iterator i(update_region_); !i.done(); i.next()) {
[email protected]1d65f482012-07-28 06:40:46224 const int stride = view_size_.width() * kBytesPerPixel;
[email protected]b9ed58f2013-05-16 10:45:24225 EXPECT_EQ(stride, frame_->stride());
[email protected]a9c88d12012-01-29 03:45:17226 const int offset = stride * i.rect().top() +
227 kBytesPerPixel * i.rect().left();
[email protected]b9ed58f2013-05-16 10:45:24228 const uint8* original = frame_->data() + offset;
[email protected]55d3688e2012-02-24 23:05:56229 const uint8* decoded = image_data_.get() + offset;
[email protected]a9c88d12012-01-29 03:45:17230 const int row_size = kBytesPerPixel * i.rect().width();
231 for (int y = 0; y < i.rect().height(); ++y) {
[email protected]66db1af92010-07-27 01:42:46232 EXPECT_EQ(0, memcmp(original, decoded, row_size))
233 << "Row " << y << " is different";
234 original += stride;
235 decoded += stride;
236 }
[email protected]fb13c762010-07-24 02:22:07237 }
238 }
239
[email protected]36b967f2012-07-27 18:35:32240 // The error at each pixel is the root mean square of the errors in
241 // the R, G, and B components, each normalized to [0, 1]. This routine
242 // checks that the maximum and mean pixel errors do not exceed given limits.
[email protected]8c6d71d2012-10-23 21:20:04243 void VerifyResultsApprox(const uint8* expected_view_data,
244 double max_error_limit, double mean_error_limit) {
[email protected]36b967f2012-07-27 18:35:32245 double max_error = 0.0;
246 double sum_error = 0.0;
247 int error_num = 0;
248 for (SkRegion::Iterator i(update_region_); !i.done(); i.next()) {
[email protected]1d65f482012-07-28 06:40:46249 const int stride = view_size_.width() * kBytesPerPixel;
[email protected]36b967f2012-07-27 18:35:32250 const int offset = stride * i.rect().top() +
251 kBytesPerPixel * i.rect().left();
[email protected]1d65f482012-07-28 06:40:46252 const uint8* expected = expected_view_data + offset;
253 const uint8* actual = image_data_.get() + offset;
[email protected]36b967f2012-07-27 18:35:32254 for (int y = 0; y < i.rect().height(); ++y) {
255 for (int x = 0; x < i.rect().width(); ++x) {
[email protected]1d65f482012-07-28 06:40:46256 double error = CalculateError(expected, actual);
[email protected]36b967f2012-07-27 18:35:32257 max_error = std::max(max_error, error);
258 sum_error += error;
259 ++error_num;
[email protected]1d65f482012-07-28 06:40:46260 expected += 4;
261 actual += 4;
[email protected]36b967f2012-07-27 18:35:32262 }
263 }
264 }
265 EXPECT_LE(max_error, max_error_limit);
266 double mean_error = sum_error / error_num;
267 EXPECT_LE(mean_error, mean_error_limit);
268 LOG(INFO) << "Max error: " << max_error;
269 LOG(INFO) << "Mean error: " << mean_error;
270 }
271
272 double CalculateError(const uint8* original, const uint8* decoded) {
273 double error_sum_squares = 0.0;
274 for (int i = 0; i < 3; i++) {
275 double error = static_cast<double>(*original++) -
276 static_cast<double>(*decoded++);
277 error /= 255.0;
278 error_sum_squares += error * error;
279 }
280 original++;
281 decoded++;
282 return sqrt(error_sum_squares / 3.0);
283 }
284
[email protected]137e58f12010-12-07 19:35:43285 private:
[email protected]b9ed58f2013-05-16 10:45:24286 DesktopSize screen_size_;
287 DesktopSize view_size_;
[email protected]fb13c762010-07-24 02:22:07288 bool strict_;
[email protected]b9ed58f2013-05-16 10:45:24289 webrtc::DesktopRegion expected_region_;
[email protected]a9c88d12012-01-29 03:45:17290 SkRegion update_region_;
[email protected]d9004e42012-08-24 01:47:37291 VideoDecoder* decoder_;
[email protected]c84c53d2013-04-08 20:13:23292 scoped_ptr<uint8[]> image_data_;
[email protected]b9ed58f2013-05-16 10:45:24293 webrtc::DesktopFrame* frame_;
[email protected]fb13c762010-07-24 02:22:07294
[email protected]d9004e42012-08-24 01:47:37295 DISALLOW_COPY_AND_ASSIGN(VideoDecoderTester);
[email protected]fb13c762010-07-24 02:22:07296};
297
[email protected]92b45b8e2012-08-24 19:46:47298// The VideoEncoderTester provides a hook for retrieving the data, and passing
299// the message to other subprograms for validaton.
300class VideoEncoderTester {
[email protected]fb13c762010-07-24 02:22:07301 public:
[email protected]92b45b8e2012-08-24 19:46:47302 VideoEncoderTester(VideoEncoderMessageTester* message_tester)
[email protected]fb13c762010-07-24 02:22:07303 : message_tester_(message_tester),
[email protected]fb13c762010-07-24 02:22:07304 decoder_tester_(NULL),
305 data_available_(0) {
306 }
307
[email protected]92b45b8e2012-08-24 19:46:47308 ~VideoEncoderTester() {
[email protected]fb13c762010-07-24 02:22:07309 EXPECT_GT(data_available_, 0);
310 }
311
[email protected]3361e1f2012-03-20 20:31:44312 void DataAvailable(scoped_ptr<VideoPacket> packet) {
[email protected]fb13c762010-07-24 02:22:07313 ++data_available_;
[email protected]3361e1f2012-03-20 20:31:44314 message_tester_->ReceivedPacket(packet.get());
[email protected]fb13c762010-07-24 02:22:07315
[email protected]d9004e42012-08-24 01:47:37316 // Send the message to the VideoDecoderTester.
[email protected]fb13c762010-07-24 02:22:07317 if (decoder_tester_) {
[email protected]3361e1f2012-03-20 20:31:44318 decoder_tester_->ReceivedPacket(packet.get());
[email protected]cee5dba2010-12-07 03:19:48319 }
[email protected]fb13c762010-07-24 02:22:07320 }
321
[email protected]b9ed58f2013-05-16 10:45:24322 void AddRects(const DesktopRect* rects, int count) {
[email protected]fb13c762010-07-24 02:22:07323 message_tester_->AddRects(rects, count);
324 }
325
[email protected]d9004e42012-08-24 01:47:37326 void set_decoder_tester(VideoDecoderTester* decoder_tester) {
[email protected]fb13c762010-07-24 02:22:07327 decoder_tester_ = decoder_tester;
328 }
329
330 private:
[email protected]92b45b8e2012-08-24 19:46:47331 VideoEncoderMessageTester* message_tester_;
[email protected]d9004e42012-08-24 01:47:37332 VideoDecoderTester* decoder_tester_;
[email protected]fb13c762010-07-24 02:22:07333 int data_available_;
334
[email protected]92b45b8e2012-08-24 19:46:47335 DISALLOW_COPY_AND_ASSIGN(VideoEncoderTester);
[email protected]fb13c762010-07-24 02:22:07336};
337
[email protected]b9ed58f2013-05-16 10:45:24338scoped_ptr<webrtc::DesktopFrame> PrepareFrame(const DesktopSize& size) {
339 scoped_ptr<webrtc::DesktopFrame> frame(new webrtc::BasicDesktopFrame(size));
[email protected]04499fb2012-08-25 00:33:50340
[email protected]fb13c762010-07-24 02:22:07341 srand(0);
[email protected]b9ed58f2013-05-16 10:45:24342 int memory_size = size.width() * size.height() * kBytesPerPixel;
[email protected]1d65f482012-07-28 06:40:46343 for (int i = 0; i < memory_size; ++i) {
[email protected]b9ed58f2013-05-16 10:45:24344 frame->data()[i] = rand() % 256;
[email protected]fb13c762010-07-24 02:22:07345 }
346
[email protected]b9ed58f2013-05-16 10:45:24347 return frame.Pass();
[email protected]fb13c762010-07-24 02:22:07348}
349
[email protected]92b45b8e2012-08-24 19:46:47350static void TestEncodingRects(VideoEncoder* encoder,
351 VideoEncoderTester* tester,
[email protected]b9ed58f2013-05-16 10:45:24352 webrtc::DesktopFrame* frame,
353 const DesktopRect* rects,
354 int count) {
355 frame->mutable_updated_region()->Clear();
[email protected]88552a92010-08-06 22:50:00356 for (int i = 0; i < count; ++i) {
[email protected]b9ed58f2013-05-16 10:45:24357 frame->mutable_updated_region()->AddRect(rects[i]);
[email protected]88552a92010-08-06 22:50:00358 }
[email protected]fb13c762010-07-24 02:22:07359 tester->AddRects(rects, count);
360
[email protected]b9ed58f2013-05-16 10:45:24361 encoder->Encode(frame, base::Bind(
[email protected]92b45b8e2012-08-24 19:46:47362 &VideoEncoderTester::DataAvailable, base::Unretained(tester)));
[email protected]fb13c762010-07-24 02:22:07363}
364
[email protected]92b45b8e2012-08-24 19:46:47365void TestVideoEncoder(VideoEncoder* encoder, bool strict) {
[email protected]8c6d71d2012-10-23 21:20:04366 const int kSizes[] = {320, 319, 317, 150};
[email protected]1d65f482012-07-28 06:40:46367
[email protected]92b45b8e2012-08-24 19:46:47368 VideoEncoderMessageTester message_tester;
[email protected]fb13c762010-07-24 02:22:07369 message_tester.set_strict(strict);
370
[email protected]92b45b8e2012-08-24 19:46:47371 VideoEncoderTester tester(&message_tester);
[email protected]fb13c762010-07-24 02:22:07372
[email protected]8c6d71d2012-10-23 21:20:04373 for (size_t xi = 0; xi < arraysize(kSizes); ++xi) {
374 for (size_t yi = 0; yi < arraysize(kSizes); ++yi) {
[email protected]b9ed58f2013-05-16 10:45:24375 DesktopSize size = DesktopSize(kSizes[xi], kSizes[yi]);
376 scoped_ptr<webrtc::DesktopFrame> frame = PrepareFrame(size);
377 std::vector<std::vector<DesktopRect> > test_rect_lists =
[email protected]8c6d71d2012-10-23 21:20:04378 MakeTestRectLists(size);
379 for (size_t i = 0; i < test_rect_lists.size(); ++i) {
[email protected]b9ed58f2013-05-16 10:45:24380 const std::vector<DesktopRect>& test_rects = test_rect_lists[i];
381 TestEncodingRects(encoder, &tester, frame.get(),
[email protected]8c6d71d2012-10-23 21:20:04382 &test_rects[0], test_rects.size());
383 }
384 }
[email protected]1d65f482012-07-28 06:40:46385 }
[email protected]fb13c762010-07-24 02:22:07386}
387
[email protected]92b45b8e2012-08-24 19:46:47388static void TestEncodeDecodeRects(VideoEncoder* encoder,
389 VideoEncoderTester* encoder_tester,
[email protected]d9004e42012-08-24 01:47:37390 VideoDecoderTester* decoder_tester,
[email protected]b9ed58f2013-05-16 10:45:24391 webrtc::DesktopFrame* frame,
392 const DesktopRect* rects, int count) {
393 frame->mutable_updated_region()->Clear();
394 for (int i = 0; i < count; ++i) {
395 frame->mutable_updated_region()->AddRect(rects[i]);
396 }
[email protected]fb13c762010-07-24 02:22:07397 encoder_tester->AddRects(rects, count);
398 decoder_tester->AddRects(rects, count);
[email protected]fb13c762010-07-24 02:22:07399
[email protected]a9c88d12012-01-29 03:45:17400 // Generate random data for the updated region.
[email protected]66db1af92010-07-27 01:42:46401 srand(0);
402 for (int i = 0; i < count; ++i) {
[email protected]b9ed58f2013-05-16 10:45:24403 const int row_size =
404 webrtc::DesktopFrame::kBytesPerPixel * rects[i].width();
405 uint8* memory = frame->data() +
406 frame->stride() * rects[i].top() +
407 webrtc::DesktopFrame::kBytesPerPixel * rects[i].left();
[email protected]a9c88d12012-01-29 03:45:17408 for (int y = 0; y < rects[i].height(); ++y) {
[email protected]66db1af92010-07-27 01:42:46409 for (int x = 0; x < row_size; ++x)
410 memory[x] = rand() % 256;
[email protected]b9ed58f2013-05-16 10:45:24411 memory += frame->stride();
[email protected]66db1af92010-07-27 01:42:46412 }
413 }
414
[email protected]b9ed58f2013-05-16 10:45:24415 encoder->Encode(frame, base::Bind(&VideoEncoderTester::DataAvailable,
416 base::Unretained(encoder_tester)));
[email protected]137e58f12010-12-07 19:35:43417 decoder_tester->VerifyResults();
418 decoder_tester->Reset();
[email protected]fb13c762010-07-24 02:22:07419}
420
[email protected]92b45b8e2012-08-24 19:46:47421void TestVideoEncoderDecoder(
422 VideoEncoder* encoder, VideoDecoder* decoder, bool strict) {
[email protected]b9ed58f2013-05-16 10:45:24423 DesktopSize kSize = DesktopSize(320, 240);
[email protected]1d65f482012-07-28 06:40:46424
[email protected]92b45b8e2012-08-24 19:46:47425 VideoEncoderMessageTester message_tester;
[email protected]fb13c762010-07-24 02:22:07426 message_tester.set_strict(strict);
427
[email protected]92b45b8e2012-08-24 19:46:47428 VideoEncoderTester encoder_tester(&message_tester);
[email protected]fb13c762010-07-24 02:22:07429
[email protected]b9ed58f2013-05-16 10:45:24430 scoped_ptr<webrtc::DesktopFrame> frame = PrepareFrame(kSize);
[email protected]dc454a7c2011-08-12 22:01:13431
[email protected]d9004e42012-08-24 01:47:37432 VideoDecoderTester decoder_tester(decoder, kSize, kSize);
[email protected]fb13c762010-07-24 02:22:07433 decoder_tester.set_strict(strict);
[email protected]b9ed58f2013-05-16 10:45:24434 decoder_tester.set_frame(frame.get());
[email protected]fb13c762010-07-24 02:22:07435 encoder_tester.set_decoder_tester(&decoder_tester);
436
[email protected]b9ed58f2013-05-16 10:45:24437 std::vector<std::vector<DesktopRect> > test_rect_lists =
438 MakeTestRectLists(kSize);
[email protected]1d65f482012-07-28 06:40:46439 for (size_t i = 0; i < test_rect_lists.size(); ++i) {
[email protected]b9ed58f2013-05-16 10:45:24440 const std::vector<DesktopRect> test_rects = test_rect_lists[i];
441 TestEncodeDecodeRects(encoder, &encoder_tester, &decoder_tester,
442 frame.get(), &test_rects[0], test_rects.size());
[email protected]1d65f482012-07-28 06:40:46443 }
[email protected]fb13c762010-07-24 02:22:07444}
445
[email protected]b9ed58f2013-05-16 10:45:24446static void FillWithGradient(webrtc::DesktopFrame* frame) {
447 for (int j = 0; j < frame->size().height(); ++j) {
448 uint8* p = frame->data() + j * frame->stride();
449 for (int i = 0; i < frame->size().width(); ++i) {
450 *p++ = (255.0 * i) / frame->size().width();
451 *p++ = (164.0 * j) / frame->size().height();
452 *p++ = (82.0 * (i + j)) /
453 (frame->size().width() + frame->size().height());
[email protected]36b967f2012-07-27 18:35:32454 *p++ = 0;
455 }
456 }
457}
458
[email protected]92b45b8e2012-08-24 19:46:47459void TestVideoEncoderDecoderGradient(VideoEncoder* encoder,
460 VideoDecoder* decoder,
[email protected]b9ed58f2013-05-16 10:45:24461 const DesktopSize& screen_size,
462 const DesktopSize& view_size,
[email protected]92b45b8e2012-08-24 19:46:47463 double max_error_limit,
464 double mean_error_limit) {
[email protected]b9ed58f2013-05-16 10:45:24465 scoped_ptr<webrtc::BasicDesktopFrame> frame(
466 new webrtc::BasicDesktopFrame(screen_size));
467 FillWithGradient(frame.get());
468 frame->mutable_updated_region()->SetRect(DesktopRect::MakeSize(screen_size));
[email protected]1d65f482012-07-28 06:40:46469
[email protected]b9ed58f2013-05-16 10:45:24470 scoped_ptr<webrtc::BasicDesktopFrame> expected_result(
471 new webrtc::BasicDesktopFrame(view_size));
472 FillWithGradient(expected_result.get());
[email protected]36b967f2012-07-27 18:35:32473
[email protected]d9004e42012-08-24 01:47:37474 VideoDecoderTester decoder_tester(decoder, screen_size, view_size);
[email protected]b9ed58f2013-05-16 10:45:24475 decoder_tester.set_frame(frame.get());
476 decoder_tester.AddRegion(frame->updated_region());
[email protected]36b967f2012-07-27 18:35:32477
[email protected]b9ed58f2013-05-16 10:45:24478 encoder->Encode(frame.get(),
[email protected]d9004e42012-08-24 01:47:37479 base::Bind(&VideoDecoderTester::ReceivedScopedPacket,
[email protected]36b967f2012-07-27 18:35:32480 base::Unretained(&decoder_tester)));
481
[email protected]b9ed58f2013-05-16 10:45:24482 decoder_tester.VerifyResultsApprox(expected_result->data(),
[email protected]1d65f482012-07-28 06:40:46483 max_error_limit, mean_error_limit);
484
485 // Check that the decoder correctly re-renders the frame if its client
486 // invalidates the frame.
487 decoder_tester.ResetRenderedData();
[email protected]b9ed58f2013-05-16 10:45:24488 decoder->Invalidate(
489 SkISize::Make(view_size.width(), view_size.height()),
490 SkRegion(SkIRect::MakeWH(view_size.width(), view_size.height())));
[email protected]1d65f482012-07-28 06:40:46491 decoder_tester.RenderFrame();
[email protected]b9ed58f2013-05-16 10:45:24492 decoder_tester.VerifyResultsApprox(expected_result->data(),
[email protected]1d65f482012-07-28 06:40:46493 max_error_limit, mean_error_limit);
[email protected]36b967f2012-07-27 18:35:32494}
495
[email protected]fb13c762010-07-24 02:22:07496} // namespace remoting