wez | 59ed1bb4 | 2014-09-03 04:36:33 | [diff] [blame] | 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 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
wez | 59ed1bb4 | 2014-09-03 04:36:33 | [diff] [blame] | 9 | #include "remoting/proto/video.pb.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 12 | |
| 13 | using webrtc::BasicDesktopFrame; |
| 14 | using webrtc::DesktopRect; |
| 15 | using webrtc::DesktopRegion; |
| 16 | using webrtc::DesktopSize; |
| 17 | using webrtc::DesktopVector; |
| 18 | |
| 19 | namespace remoting { |
| 20 | |
| 21 | TEST(VideoEncoderHelperTest, PropagatesCommonFields) { |
| 22 | BasicDesktopFrame frame(DesktopSize(32, 32)); |
| 23 | frame.set_dpi(DesktopVector(96, 97)); |
| 24 | frame.set_capture_time_ms(20); |
| 25 | frame.mutable_updated_region()->SetRect(DesktopRect::MakeLTRB(0, 0, 16, 16)); |
wez | 59ed1bb4 | 2014-09-03 04:36:33 | [diff] [blame] | 26 | |
| 27 | VideoEncoderHelper helper; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 28 | std::unique_ptr<VideoPacket> packet(helper.CreateVideoPacket(frame)); |
wez | 59ed1bb4 | 2014-09-03 04:36:33 | [diff] [blame] | 29 | |
| 30 | ASSERT_TRUE(packet->has_format()); |
| 31 | EXPECT_FALSE(packet->format().has_encoding()); |
| 32 | EXPECT_TRUE(packet->format().has_screen_width()); |
| 33 | EXPECT_TRUE(packet->format().has_screen_height()); |
| 34 | EXPECT_TRUE(packet->format().has_x_dpi()); |
| 35 | EXPECT_TRUE(packet->format().has_y_dpi()); |
| 36 | |
wez | 59ed1bb4 | 2014-09-03 04:36:33 | [diff] [blame] | 37 | EXPECT_EQ(1, packet->dirty_rects().size()); |
wez | 59ed1bb4 | 2014-09-03 04:36:33 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | TEST(VideoEncoderHelperTest, ZeroDpi) { |
| 41 | BasicDesktopFrame frame(DesktopSize(32, 32)); |
| 42 | // DPI is zero unless explicitly set. |
| 43 | |
| 44 | VideoEncoderHelper helper; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 45 | std::unique_ptr<VideoPacket> packet(helper.CreateVideoPacket(frame)); |
wez | 59ed1bb4 | 2014-09-03 04:36:33 | [diff] [blame] | 46 | |
| 47 | // Packet should have a format containing the screen dimensions only. |
| 48 | ASSERT_TRUE(packet->has_format()); |
| 49 | EXPECT_TRUE(packet->format().has_screen_width()); |
| 50 | EXPECT_TRUE(packet->format().has_screen_height()); |
| 51 | EXPECT_FALSE(packet->format().has_x_dpi()); |
| 52 | EXPECT_FALSE(packet->format().has_y_dpi()); |
| 53 | } |
| 54 | |
wez | 59ed1bb4 | 2014-09-03 04:36:33 | [diff] [blame] | 55 | TEST(VideoEncoderHelperTest, NoScreenSizeIfUnchanged) { |
| 56 | BasicDesktopFrame frame(DesktopSize(32, 32)); |
| 57 | // Set DPI so that the packet will have a format, with DPI but no size. |
| 58 | frame.set_dpi(DesktopVector(96, 97)); |
| 59 | |
| 60 | VideoEncoderHelper helper; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 61 | std::unique_ptr<VideoPacket> packet(helper.CreateVideoPacket(frame)); |
wez | 59ed1bb4 | 2014-09-03 04:36:33 | [diff] [blame] | 62 | packet = helper.CreateVideoPacket(frame); |
| 63 | |
| 64 | ASSERT_TRUE(packet->has_format()); |
| 65 | EXPECT_FALSE(packet->format().has_screen_width()); |
| 66 | EXPECT_FALSE(packet->format().has_screen_height()); |
| 67 | EXPECT_TRUE(packet->format().has_x_dpi()); |
| 68 | EXPECT_TRUE(packet->format().has_y_dpi()); |
| 69 | } |
| 70 | |
| 71 | TEST(VideoEncoderHelperTest, ScreenSizeWhenChanged) { |
| 72 | VideoEncoderHelper helper; |
| 73 | |
| 74 | // Process the same frame twice, so the helper knows the current size, and |
| 75 | // to trigger suppression of the size field due to the size not changing. |
| 76 | BasicDesktopFrame frame1(DesktopSize(32, 32)); |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 77 | std::unique_ptr<VideoPacket> packet(helper.CreateVideoPacket(frame1)); |
wez | 59ed1bb4 | 2014-09-03 04:36:33 | [diff] [blame] | 78 | packet = helper.CreateVideoPacket(frame1); |
| 79 | |
| 80 | // Process a different-sized frame to trigger size to be emitted. |
| 81 | BasicDesktopFrame frame2(DesktopSize(48, 48)); |
| 82 | packet = helper.CreateVideoPacket(frame2); |
| 83 | |
| 84 | ASSERT_TRUE(packet->has_format()); |
| 85 | EXPECT_TRUE(packet->format().has_screen_width()); |
| 86 | EXPECT_TRUE(packet->format().has_screen_height()); |
| 87 | } |
| 88 | |
| 89 | } // namespace remoting |