1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "remoting/codec/video_encoder_helper.h"
6
7#include "base/memory/scoped_ptr.h"
8#include "remoting/proto/video.pb.h"
9#include "testing/gtest/include/gtest/gtest.h"
10#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
11
12using webrtc::BasicDesktopFrame;
13using webrtc::DesktopRect;
14using webrtc::DesktopRegion;
15using webrtc::DesktopSize;
16using webrtc::DesktopVector;
17
18namespace remoting {
19
20TEST(VideoEncoderHelperTest, PropagatesCommonFields) {
21  BasicDesktopFrame frame(DesktopSize(32, 32));
22  frame.set_dpi(DesktopVector(96, 97));
23  frame.set_capture_time_ms(20);
24  frame.mutable_updated_region()->SetRect(DesktopRect::MakeLTRB(0, 0, 16, 16));
25  scoped_ptr<DesktopRegion> shape(
26      new DesktopRegion(DesktopRect::MakeLTRB(16, 0, 32, 16)));
27  frame.set_shape(shape.release());
28
29  VideoEncoderHelper helper;
30  scoped_ptr<VideoPacket> packet(helper.CreateVideoPacket(frame));
31
32  ASSERT_TRUE(packet->has_format());
33  EXPECT_FALSE(packet->format().has_encoding());
34  EXPECT_TRUE(packet->format().has_screen_width());
35  EXPECT_TRUE(packet->format().has_screen_height());
36  EXPECT_TRUE(packet->format().has_x_dpi());
37  EXPECT_TRUE(packet->format().has_y_dpi());
38
39  EXPECT_TRUE(packet->has_capture_time_ms());
40  EXPECT_EQ(1, packet->dirty_rects().size());
41
42  ASSERT_TRUE(packet->has_use_desktop_shape());
43  EXPECT_TRUE(packet->use_desktop_shape());
44
45  EXPECT_EQ(1, packet->desktop_shape_rects().size());
46}
47
48TEST(VideoEncoderHelperTest, ZeroDpi) {
49  BasicDesktopFrame frame(DesktopSize(32, 32));
50  // DPI is zero unless explicitly set.
51
52  VideoEncoderHelper helper;
53  scoped_ptr<VideoPacket> packet(helper.CreateVideoPacket(frame));
54
55  // Packet should have a format containing the screen dimensions only.
56  ASSERT_TRUE(packet->has_format());
57  EXPECT_TRUE(packet->format().has_screen_width());
58  EXPECT_TRUE(packet->format().has_screen_height());
59  EXPECT_FALSE(packet->format().has_x_dpi());
60  EXPECT_FALSE(packet->format().has_y_dpi());
61}
62
63TEST(VideoEncoderHelperTest, NoShape) {
64  BasicDesktopFrame frame(DesktopSize(32, 32));
65
66  VideoEncoderHelper helper;
67  scoped_ptr<VideoPacket> packet(helper.CreateVideoPacket(frame));
68
69  EXPECT_FALSE(packet->use_desktop_shape());
70  EXPECT_EQ(0, packet->desktop_shape_rects().size());
71}
72
73TEST(VideoEncoderHelperTest, NoScreenSizeIfUnchanged) {
74  BasicDesktopFrame frame(DesktopSize(32, 32));
75  // Set DPI so that the packet will have a format, with DPI but no size.
76  frame.set_dpi(DesktopVector(96, 97));
77
78  VideoEncoderHelper helper;
79  scoped_ptr<VideoPacket> packet(helper.CreateVideoPacket(frame));
80  packet = helper.CreateVideoPacket(frame);
81
82  ASSERT_TRUE(packet->has_format());
83  EXPECT_FALSE(packet->format().has_screen_width());
84  EXPECT_FALSE(packet->format().has_screen_height());
85  EXPECT_TRUE(packet->format().has_x_dpi());
86  EXPECT_TRUE(packet->format().has_y_dpi());
87}
88
89TEST(VideoEncoderHelperTest, ScreenSizeWhenChanged) {
90  VideoEncoderHelper helper;
91
92  // Process the same frame twice, so the helper knows the current size, and
93  // to trigger suppression of the size field due to the size not changing.
94  BasicDesktopFrame frame1(DesktopSize(32, 32));
95  scoped_ptr<VideoPacket> packet(helper.CreateVideoPacket(frame1));
96  packet = helper.CreateVideoPacket(frame1);
97
98  // Process a different-sized frame to trigger size to be emitted.
99  BasicDesktopFrame frame2(DesktopSize(48, 48));
100  packet = helper.CreateVideoPacket(frame2);
101
102  ASSERT_TRUE(packet->has_format());
103  EXPECT_TRUE(packet->format().has_screen_width());
104  EXPECT_TRUE(packet->format().has_screen_height());
105}
106
107}  // namespace remoting
108