blob: 5a67ba05d55cf8a425e56464d08dc201cce0bc8a [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]28dc4212011-02-08 20:44:0012#include "remoting/base/base_mock_objects.h"
[email protected]fb13c762010-07-24 02:22:0713#include "remoting/base/codec_test.h"
[email protected]8ea7a1672010-10-04 19:48:4214#include "remoting/base/decoder.h"
[email protected]fb13c762010-07-24 02:22:0715#include "remoting/base/encoder.h"
[email protected]c3af26f332010-10-06 22:46:0016#include "remoting/base/util.h"
[email protected]fb13c762010-07-24 02:22:0717#include "testing/gtest/include/gtest/gtest.h"
18
19static const int kWidth = 320;
20static const int kHeight = 240;
21static const int kBytesPerPixel = 4;
22
23// Some sample rects for testing.
[email protected]dc454a7c2011-08-12 22:01:1324static 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]fb13c762010-07-24 02:22:0730};
31
32namespace remoting {
33
[email protected]fb13c762010-07-24 02:22:0734// A class to test the message output of the encoder.
35class EncoderMessageTester {
36 public:
37 EncoderMessageTester()
38 : begin_rect_(0),
39 rect_data_(0),
40 end_rect_(0),
[email protected]66db1af92010-07-27 01:42:4641 added_rects_(0),
[email protected]fb13c762010-07-24 02:22:0742 state_(kWaitingForBeginRect),
43 strict_(false) {
44 }
45
46 ~EncoderMessageTester() {
47 EXPECT_EQ(begin_rect_, end_rect_);
[email protected]df10bf12010-09-28 22:19:4848 EXPECT_GT(begin_rect_, 0);
[email protected]fb13c762010-07-24 02:22:0749 EXPECT_EQ(kWaitingForBeginRect, state_);
[email protected]df10bf12010-09-28 22:19:4850 if (strict_) {
51 EXPECT_EQ(added_rects_, begin_rect_);
[email protected]66db1af92010-07-27 01:42:4652 }
[email protected]fb13c762010-07-24 02:22:0753 }
54
[email protected]137e58f12010-12-07 19:35:4355 // Test that we received the correct packet.
56 void ReceivedPacket(VideoPacket* packet) {
[email protected]fb13c762010-07-24 02:22:0757 if (state_ == kWaitingForBeginRect) {
[email protected]137e58f12010-12-07 19:35:4358 EXPECT_TRUE((packet->flags() & VideoPacket::FIRST_PACKET) != 0);
[email protected]fb13c762010-07-24 02:22:0759 state_ = kWaitingForRectData;
60 ++begin_rect_;
61
62 if (strict_) {
[email protected]dc454a7c2011-08-12 22:01:1363 SkIRect rect = rects_.front();
[email protected]fb13c762010-07-24 02:22:0764 rects_.pop_front();
[email protected]dc454a7c2011-08-12 22:01:1365 EXPECT_EQ(rect.fLeft, packet->format().x());
66 EXPECT_EQ(rect.fTop, packet->format().y());
[email protected]137e58f12010-12-07 19:35:4367 EXPECT_EQ(rect.width(), packet->format().width());
68 EXPECT_EQ(rect.height(), packet->format().height());
[email protected]fb13c762010-07-24 02:22:0769 }
70 } else {
[email protected]137e58f12010-12-07 19:35:4371 EXPECT_FALSE((packet->flags() & VideoPacket::FIRST_PACKET) != 0);
[email protected]fb13c762010-07-24 02:22:0772 }
73
74 if (state_ == kWaitingForRectData) {
[email protected]137e58f12010-12-07 19:35:4375 if (packet->has_data()) {
[email protected]fb13c762010-07-24 02:22:0776 ++rect_data_;
77 }
78
[email protected]137e58f12010-12-07 19:35:4379 if ((packet->flags() & VideoPacket::LAST_PACKET) != 0) {
[email protected]fb13c762010-07-24 02:22:0780 // 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]5bc71832010-12-09 01:34:0886
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]fb13c762010-07-24 02:22:0791 }
92 }
93
94 void set_strict(bool strict) {
95 strict_ = strict;
96 }
97
[email protected]dc454a7c2011-08-12 22:01:1398 void AddRects(const SkIRect* rects, int count) {
[email protected]fb13c762010-07-24 02:22:0799 rects_.insert(rects_.begin() + rects_.size(), rects, rects + count);
[email protected]66db1af92010-07-27 01:42:46100 added_rects_ += count;
[email protected]fb13c762010-07-24 02:22:07101 }
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]66db1af92010-07-27 01:42:46112 int added_rects_;
[email protected]fb13c762010-07-24 02:22:07113 State state_;
114 bool strict_;
115
[email protected]dc454a7c2011-08-12 22:01:13116 std::deque<SkIRect> rects_;
[email protected]fb13c762010-07-24 02:22:07117
118 DISALLOW_COPY_AND_ASSIGN(EncoderMessageTester);
119};
120
121class DecoderTester {
122 public:
[email protected]542bdfe2010-11-30 03:55:47123 DecoderTester(Decoder* decoder)
[email protected]fb13c762010-07-24 02:22:07124 : strict_(false),
[email protected]137e58f12010-12-07 19:35:43125 decoder_(decoder) {
[email protected]55d3688e2012-02-24 23:05:56126 image_data_.reset(new uint8[kWidth * kHeight * kBytesPerPixel]);
127 EXPECT_TRUE(image_data_.get());
128 decoder_->Initialize(SkISize::Make(kWidth, kHeight));
[email protected]fb13c762010-07-24 02:22:07129 }
130
[email protected]137e58f12010-12-07 19:35:43131 void Reset() {
[email protected]a9c88d12012-01-29 03:45:17132 expected_region_.setEmpty();
133 update_region_.setEmpty();
[email protected]137e58f12010-12-07 19:35:43134 }
[email protected]cee5dba2010-12-07 03:19:48135
[email protected]137e58f12010-12-07 19:35:43136 void ReceivedPacket(VideoPacket* packet) {
137 Decoder::DecodeResult result = decoder_->DecodePacket(packet);
[email protected]cee5dba2010-12-07 03:19:48138
[email protected]137e58f12010-12-07 19:35:43139 ASSERT_NE(Decoder::DECODE_ERROR, result);
140
141 if (result == Decoder::DECODE_DONE) {
[email protected]55d3688e2012-02-24 23:05:56142 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]cee5dba2010-12-07 03:19:48147 }
[email protected]fb13c762010-07-24 02:22:07148 }
149
[email protected]36b967f2012-07-27 18:35:32150 void ReceivedScopedPacket(scoped_ptr<VideoPacket> packet) {
151 ReceivedPacket(packet.get());
152 }
153
[email protected]fb13c762010-07-24 02:22:07154 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]dc454a7c2011-08-12 22:01:13162 void AddRects(const SkIRect* rects, int count) {
[email protected]a9c88d12012-01-29 03:45:17163 SkRegion new_rects;
164 new_rects.setRects(rects, count);
[email protected]36b967f2012-07-27 18:35:32165 AddRegion(new_rects);
166 }
167
168 void AddRegion(const SkRegion& region) {
169 expected_region_.op(region, SkRegion::kUnion_Op);
[email protected]fb13c762010-07-24 02:22:07170 }
171
[email protected]137e58f12010-12-07 19:35:43172 void VerifyResults() {
[email protected]fb13c762010-07-24 02:22:07173 if (!strict_)
174 return;
[email protected]66db1af92010-07-27 01:42:46175
[email protected]137e58f12010-12-07 19:35:43176 ASSERT_TRUE(capture_data_.get());
177
[email protected]a9c88d12012-01-29 03:45:17178 // 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]55d3688e2012-02-24 23:05:56181 const int stride = kWidth * kBytesPerPixel;
182 EXPECT_EQ(stride, capture_data_->data_planes().strides[0]);
[email protected]a9c88d12012-01-29 03:45:17183 const int offset = stride * i.rect().top() +
184 kBytesPerPixel * i.rect().left();
[email protected]66db1af92010-07-27 01:42:46185 const uint8* original = capture_data_->data_planes().data[0] + offset;
[email protected]55d3688e2012-02-24 23:05:56186 const uint8* decoded = image_data_.get() + offset;
[email protected]a9c88d12012-01-29 03:45:17187 const int row_size = kBytesPerPixel * i.rect().width();
188 for (int y = 0; y < i.rect().height(); ++y) {
[email protected]66db1af92010-07-27 01:42:46189 EXPECT_EQ(0, memcmp(original, decoded, row_size))
190 << "Row " << y << " is different";
191 original += stride;
192 decoded += stride;
193 }
[email protected]fb13c762010-07-24 02:22:07194 }
195 }
196
[email protected]36b967f2012-07-27 18:35:32197 // 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]137e58f12010-12-07 19:35:43246 private:
[email protected]fb13c762010-07-24 02:22:07247 bool strict_;
[email protected]a9c88d12012-01-29 03:45:17248 SkRegion expected_region_;
249 SkRegion update_region_;
[email protected]fb13c762010-07-24 02:22:07250 Decoder* decoder_;
[email protected]55d3688e2012-02-24 23:05:56251 scoped_array<uint8> image_data_;
[email protected]fb13c762010-07-24 02:22:07252 scoped_refptr<CaptureData> capture_data_;
[email protected]fb13c762010-07-24 02:22:07253
254 DISALLOW_COPY_AND_ASSIGN(DecoderTester);
255};
256
[email protected]8ea7a1672010-10-04 19:48:42257// The EncoderTester provides a hook for retrieving the data, and passing the
258// message to other subprograms for validaton.
[email protected]fb13c762010-07-24 02:22:07259class EncoderTester {
260 public:
[email protected]137e58f12010-12-07 19:35:43261 EncoderTester(EncoderMessageTester* message_tester)
[email protected]fb13c762010-07-24 02:22:07262 : message_tester_(message_tester),
[email protected]fb13c762010-07-24 02:22:07263 decoder_tester_(NULL),
264 data_available_(0) {
265 }
266
267 ~EncoderTester() {
268 EXPECT_GT(data_available_, 0);
269 }
270
[email protected]3361e1f2012-03-20 20:31:44271 void DataAvailable(scoped_ptr<VideoPacket> packet) {
[email protected]fb13c762010-07-24 02:22:07272 ++data_available_;
[email protected]3361e1f2012-03-20 20:31:44273 message_tester_->ReceivedPacket(packet.get());
[email protected]fb13c762010-07-24 02:22:07274
275 // Send the message to the DecoderTester.
276 if (decoder_tester_) {
[email protected]3361e1f2012-03-20 20:31:44277 decoder_tester_->ReceivedPacket(packet.get());
[email protected]cee5dba2010-12-07 03:19:48278 }
[email protected]fb13c762010-07-24 02:22:07279 }
280
[email protected]dc454a7c2011-08-12 22:01:13281 void AddRects(const SkIRect* rects, int count) {
[email protected]fb13c762010-07-24 02:22:07282 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]fb13c762010-07-24 02:22:07291 DecoderTester* decoder_tester_;
292 int data_available_;
293
294 DISALLOW_COPY_AND_ASSIGN(EncoderTester);
295};
296
[email protected]137e58f12010-12-07 19:35:43297scoped_refptr<CaptureData> PrepareEncodeData(media::VideoFrame::Format format,
[email protected]fb13c762010-07-24 02:22:07298 uint8** memory) {
299 // TODO(hclam): Support also YUV format.
[email protected]137e58f12010-12-07 19:35:43300 CHECK_EQ(format, media::VideoFrame::RGB32);
[email protected]fb13c762010-07-24 02:22:07301 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]bcad2682011-09-30 20:35:26316 new CaptureData(planes, SkISize::Make(kWidth, kHeight), format);
[email protected]fb13c762010-07-24 02:22:07317 return data;
318}
319
320static void TestEncodingRects(Encoder* encoder,
321 EncoderTester* tester,
322 scoped_refptr<CaptureData> data,
[email protected]dc454a7c2011-08-12 22:01:13323 const SkIRect* rects, int count) {
324 data->mutable_dirty_region().setEmpty();
[email protected]88552a92010-08-06 22:50:00325 for (int i = 0; i < count; ++i) {
[email protected]dc454a7c2011-08-12 22:01:13326 data->mutable_dirty_region().op(rects[i], SkRegion::kUnion_Op);
[email protected]88552a92010-08-06 22:50:00327 }
[email protected]fb13c762010-07-24 02:22:07328 tester->AddRects(rects, count);
329
[email protected]1e1cb3b2011-11-10 02:07:41330 encoder->Encode(data, true, base::Bind(
331 &EncoderTester::DataAvailable, base::Unretained(tester)));
[email protected]fb13c762010-07-24 02:22:07332}
333
334void TestEncoder(Encoder* encoder, bool strict) {
335 EncoderMessageTester message_tester;
336 message_tester.set_strict(strict);
337
[email protected]137e58f12010-12-07 19:35:43338 EncoderTester tester(&message_tester);
[email protected]fb13c762010-07-24 02:22:07339
340 uint8* memory;
341 scoped_refptr<CaptureData> data =
[email protected]137e58f12010-12-07 19:35:43342 PrepareEncodeData(media::VideoFrame::RGB32, &memory);
[email protected]dc454a7c2011-08-12 22:01:13343 scoped_array<uint8> memory_wrapper(memory);
[email protected]fb13c762010-07-24 02:22:07344
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]fb13c762010-07-24 02:22:07349}
350
[email protected]a9c88d12012-01-29 03:45:17351static 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]fb13c762010-07-24 02:22:07357 encoder_tester->AddRects(rects, count);
358 decoder_tester->AddRects(rects, count);
[email protected]fb13c762010-07-24 02:22:07359
[email protected]a9c88d12012-01-29 03:45:17360 // Generate random data for the updated region.
[email protected]66db1af92010-07-27 01:42:46361 srand(0);
362 for (int i = 0; i < count; ++i) {
[email protected]f402ba032012-04-11 00:17:16363 CHECK_EQ(data->pixel_format(), media::VideoFrame::RGB32);
364 const int bytes_per_pixel = 4; // Because of RGB32 on previous line.
[email protected]a9c88d12012-01-29 03:45:17365 const int row_size = bytes_per_pixel * rects[i].width();
[email protected]66db1af92010-07-27 01:42:46366 uint8* memory = data->data_planes().data[0] +
[email protected]a9c88d12012-01-29 03:45:17367 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]66db1af92010-07-27 01:42:46370 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]1e1cb3b2011-11-10 02:07:41376 encoder->Encode(data, true, base::Bind(&EncoderTester::DataAvailable,
377 base::Unretained(encoder_tester)));
[email protected]137e58f12010-12-07 19:35:43378 decoder_tester->VerifyResults();
379 decoder_tester->Reset();
[email protected]fb13c762010-07-24 02:22:07380}
381
382void TestEncoderDecoder(Encoder* encoder, Decoder* decoder, bool strict) {
383 EncoderMessageTester message_tester;
384 message_tester.set_strict(strict);
385
[email protected]137e58f12010-12-07 19:35:43386 EncoderTester encoder_tester(&message_tester);
[email protected]fb13c762010-07-24 02:22:07387
388 uint8* memory;
389 scoped_refptr<CaptureData> data =
[email protected]137e58f12010-12-07 19:35:43390 PrepareEncodeData(media::VideoFrame::RGB32, &memory);
[email protected]dc454a7c2011-08-12 22:01:13391 scoped_array<uint8> memory_wrapper(memory);
392
[email protected]fb13c762010-07-24 02:22:07393 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]a9c88d12012-01-29 03:45:17398 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]fb13c762010-07-24 02:22:07406}
407
[email protected]36b967f2012-07-27 18:35:32408static 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
422void 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]fb13c762010-07-24 02:22:07453} // namespace remoting