| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/codec/video_encoder_vpx.h" | 5 #include "remoting/codec/video_encoder_vpx.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "remoting/codec/codec_test.h" | 11 #include "remoting/codec/codec_test.h" |
| 12 #include "remoting/proto/video.pb.h" | 12 #include "remoting/proto/video.pb.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 14 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 15 | 15 |
| 16 namespace remoting { | 16 namespace remoting { |
| 17 | 17 |
| 18 // xRGB pixel colors for use by tests. | 18 // xRGB pixel colors for use by tests. |
| 19 const uint32 kBlueColor = 0x0000ff; | 19 const uint32 kBlueColor = 0x0000ff; |
| 20 const uint32 kGreenColor = 0x00ff00; | 20 const uint32 kGreenColor = 0x00ff00; |
| 21 | 21 |
| 22 // Creates a frame stippled between blue and red pixels, which is useful for | 22 // Creates a frame stippled between blue and red pixels, which is useful for |
| 23 // lossy/lossless encode and color tests. | 23 // lossy/lossless encode and color tests. By default all pixels in the frame |
| 24 // are included in the updated_region(). |
| 24 static scoped_ptr<webrtc::DesktopFrame> CreateTestFrame( | 25 static scoped_ptr<webrtc::DesktopFrame> CreateTestFrame( |
| 25 const webrtc::DesktopSize& frame_size) { | 26 const webrtc::DesktopSize& frame_size) { |
| 26 scoped_ptr<webrtc::DesktopFrame> frame( | 27 scoped_ptr<webrtc::DesktopFrame> frame( |
| 27 new webrtc::BasicDesktopFrame(frame_size)); | 28 new webrtc::BasicDesktopFrame(frame_size)); |
| 28 for (int x = 0; x < frame_size.width(); ++x) { | 29 for (int x = 0; x < frame_size.width(); ++x) { |
| 29 for (int y = 0; y < frame_size.height(); ++y) { | 30 for (int y = 0; y < frame_size.height(); ++y) { |
| 30 uint8* pixel_u8 = frame->data() + (y * frame->stride()) + | 31 uint8* pixel_u8 = frame->data() + (y * frame->stride()) + |
| 31 (x * webrtc::DesktopFrame::kBytesPerPixel); | 32 (x * webrtc::DesktopFrame::kBytesPerPixel); |
| 32 *(reinterpret_cast<uint32*>(pixel_u8)) = | 33 *(reinterpret_cast<uint32*>(pixel_u8)) = |
| 33 ((x + y) & 1) ? kGreenColor : kBlueColor; | 34 ((x + y) & 1) ? kGreenColor : kBlueColor; |
| 34 } | 35 } |
| 35 } | 36 } |
| 37 frame->mutable_updated_region()->SetRect( |
| 38 webrtc::DesktopRect::MakeSize(frame_size)); |
| 36 return frame.Pass(); | 39 return frame.Pass(); |
| 37 } | 40 } |
| 38 | 41 |
| 39 TEST(VideoEncoderVpxTest, TestVp8VideoEncoder) { | 42 TEST(VideoEncoderVpxTest, TestVp8VideoEncoder) { |
| 40 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP8()); | 43 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP8()); |
| 41 TestVideoEncoder(encoder.get(), false); | 44 TestVideoEncoder(encoder.get(), false); |
| 42 } | 45 } |
| 43 | 46 |
| 44 TEST(VideoEncoderVpxTest, TestVp9VideoEncoder) { | 47 TEST(VideoEncoderVpxTest, TestVp9VideoEncoder) { |
| 45 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP9()); | 48 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP9()); |
| 46 // VP9 encoder defaults to lossless encode and lossy (I420) color. | 49 // VP9 encoder defaults to lossless encode and lossy (I420) color. |
| 47 TestVideoEncoder(encoder.get(), false); | 50 TestVideoEncoder(encoder.get(), false); |
| 48 } | 51 } |
| 49 | 52 |
| 50 // Test that the VP9 encoder can switch between lossy & lossless encode. | 53 // Test that the VP9 encoder can switch between lossy & lossless encode. |
| 51 TEST(VideoEncoderVpxTest, TestVp9VideoEncoderLossyEncode) { | 54 TEST(VideoEncoderVpxTest, TestVp9VideoEncoderLossyEncode) { |
| 52 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP9()); | 55 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP9()); |
| 53 | 56 |
| 54 webrtc::DesktopSize frame_size(1024, 768); | 57 webrtc::DesktopSize frame_size(1024, 768); |
| 55 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); | 58 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); |
| 56 frame->mutable_updated_region()->SetRect( | |
| 57 webrtc::DesktopRect::MakeSize(frame_size)); | |
| 58 | 59 |
| 59 // Lossy encode the first frame. | 60 // Lossy encode the first frame. |
| 60 encoder->SetLosslessEncode(false); | 61 encoder->SetLosslessEncode(false); |
| 61 scoped_ptr<VideoPacket> lossy_packet = encoder->Encode(*frame); | 62 scoped_ptr<VideoPacket> lossy_packet = encoder->Encode(*frame); |
| 62 | 63 |
| 63 // Lossless encode the second frame. | 64 // Lossless encode the second frame. |
| 64 encoder->SetLosslessEncode(true); | 65 encoder->SetLosslessEncode(true); |
| 65 scoped_ptr<VideoPacket> lossless_packet = encoder->Encode(*frame); | 66 scoped_ptr<VideoPacket> lossless_packet = encoder->Encode(*frame); |
| 66 // Lossless encode is so good that the frames are smaller than the lossy | 67 // Lossless encode is so good that the frames are smaller than the lossy |
| 67 // encodes. This comparison needs to be revisited. | 68 // encodes. This comparison needs to be revisited. |
| 68 // http://crbug.com/439166 | 69 // http://crbug.com/439166 |
| 69 // EXPECT_GT(lossless_packet->data().size(), lossy_packet->data().size()); | 70 // EXPECT_GT(lossless_packet->data().size(), lossy_packet->data().size()); |
| 70 | 71 |
| 71 // Lossy encode one more frame. | 72 // Lossy encode one more frame. |
| 72 encoder->SetLosslessEncode(false); | 73 encoder->SetLosslessEncode(false); |
| 73 lossy_packet = encoder->Encode(*frame); | 74 lossy_packet = encoder->Encode(*frame); |
| 74 // Same bug as above. | 75 // Same bug as above. |
| 75 // EXPECT_LT(lossy_packet->data().size(), lossless_packet->data().size()); | 76 // EXPECT_LT(lossy_packet->data().size(), lossless_packet->data().size()); |
| 76 } | 77 } |
| 77 | 78 |
| 78 // Test that the VP9 encoder can switch between lossy & lossless color. | 79 // Test that the VP9 encoder can switch between lossy & lossless color. |
| 79 TEST(VideoEncoderVpxTest, TestVp9VideoEncoderLossyColor) { | 80 TEST(VideoEncoderVpxTest, TestVp9VideoEncoderLossyColor) { |
| 80 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP9()); | 81 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP9()); |
| 81 | 82 |
| 82 webrtc::DesktopSize frame_size(1024, 768); | 83 webrtc::DesktopSize frame_size(1024, 768); |
| 83 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); | 84 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); |
| 84 frame->mutable_updated_region()->SetRect( | |
| 85 webrtc::DesktopRect::MakeSize(frame_size)); | |
| 86 | 85 |
| 87 // Lossy encode the first frame. | 86 // Lossy encode the first frame. |
| 88 encoder->SetLosslessColor(false); | 87 encoder->SetLosslessColor(false); |
| 89 scoped_ptr<VideoPacket> lossy_packet = encoder->Encode(*frame); | 88 scoped_ptr<VideoPacket> lossy_packet = encoder->Encode(*frame); |
| 90 | 89 |
| 91 // Lossless encode the second frame. | 90 // Lossless encode the second frame. |
| 92 encoder->SetLosslessColor(true); | 91 encoder->SetLosslessColor(true); |
| 93 scoped_ptr<VideoPacket> lossless_packet = encoder->Encode(*frame); | 92 scoped_ptr<VideoPacket> lossless_packet = encoder->Encode(*frame); |
| 94 | 93 |
| 95 // Lossy encode one more frame. | 94 // Lossy encode one more frame. |
| 96 encoder->SetLosslessColor(false); | 95 encoder->SetLosslessColor(false); |
| 97 lossy_packet = encoder->Encode(*frame); | 96 lossy_packet = encoder->Encode(*frame); |
| 98 } | 97 } |
| 99 | 98 |
| 100 // Test that the VP8 encoder ignores lossless modes without crashing. | 99 // Test that the VP8 encoder ignores lossless modes without crashing. |
| 101 TEST(VideoEncoderVpxTest, TestVp8VideoEncoderIgnoreLossy) { | 100 TEST(VideoEncoderVpxTest, TestVp8VideoEncoderIgnoreLossy) { |
| 102 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP8()); | 101 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP8()); |
| 103 | 102 |
| 104 webrtc::DesktopSize frame_size(1024, 768); | 103 webrtc::DesktopSize frame_size(1024, 768); |
| 105 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); | 104 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); |
| 106 frame->mutable_updated_region()->SetRect( | |
| 107 webrtc::DesktopRect::MakeSize(frame_size)); | |
| 108 | 105 |
| 109 // Encode a frame, to give the encoder a chance to crash if misconfigured. | 106 // Encode a frame, to give the encoder a chance to crash if misconfigured. |
| 110 encoder->SetLosslessEncode(true); | 107 encoder->SetLosslessEncode(true); |
| 111 encoder->SetLosslessColor(true); | 108 encoder->SetLosslessColor(true); |
| 112 scoped_ptr<VideoPacket> packet = encoder->Encode(*frame); | 109 scoped_ptr<VideoPacket> packet = encoder->Encode(*frame); |
| 113 EXPECT_TRUE(packet); | 110 EXPECT_TRUE(packet); |
| 114 } | 111 } |
| 115 | 112 |
| 116 // Test that calling Encode with a differently-sized media::ScreenCaptureData | 113 // Test that calling Encode with a larger frame size than the initial one |
| 117 // does not leak memory. | 114 // does not cause VP8 to crash. |
| 118 TEST(VideoEncoderVpxTest, TestSizeChangeNoLeak) { | 115 TEST(VideoEncoderVpxTest, TestVp8SizeChangeNoCrash) { |
| 119 webrtc::DesktopSize frame_size(1000, 1000); | 116 webrtc::DesktopSize frame_size(1000, 1000); |
| 120 | 117 |
| 121 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP8()); | 118 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP8()); |
| 122 | 119 |
| 123 // Create first frame & encode it. | 120 // Create first frame & encode it. |
| 124 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); | 121 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); |
| 125 frame->mutable_updated_region()->SetRect( | |
| 126 webrtc::DesktopRect::MakeSize(frame_size)); | |
| 127 scoped_ptr<VideoPacket> packet = encoder->Encode(*frame); | 122 scoped_ptr<VideoPacket> packet = encoder->Encode(*frame); |
| 128 EXPECT_TRUE(packet); | 123 EXPECT_TRUE(packet); |
| 129 | 124 |
| 130 // Halve the size of the frame, and updated region, and encode again. | 125 // Double the size of the frame, and updated region, and encode again. |
| 131 frame_size.set(frame_size.width(), frame_size.height() / 2); | 126 frame_size.set(frame_size.width() * 2, frame_size.height() * 2); |
| 132 frame = CreateTestFrame(frame_size); | 127 frame = CreateTestFrame(frame_size); |
| 133 frame->mutable_updated_region()->SetRect( | |
| 134 webrtc::DesktopRect::MakeSize(frame_size)); | |
| 135 packet = encoder->Encode(*frame); | 128 packet = encoder->Encode(*frame); |
| 136 EXPECT_TRUE(packet); | 129 EXPECT_TRUE(packet); |
| 137 } | 130 } |
| 131 |
| 132 // Test that calling Encode with a larger frame size than the initial one |
| 133 // does not cause VP9 to crash. |
| 134 TEST(VideoEncoderVpxTest, TestVp9SizeChangeNoCrash) { |
| 135 webrtc::DesktopSize frame_size(1000, 1000); |
| 136 |
| 137 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP9()); |
| 138 |
| 139 // Create first frame & encode it. |
| 140 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); |
| 141 scoped_ptr<VideoPacket> packet = encoder->Encode(*frame); |
| 142 EXPECT_TRUE(packet); |
| 143 |
| 144 // Double the size of the frame, and updated region, and encode again. |
| 145 frame_size.set(frame_size.width() * 2, frame_size.height() * 2); |
| 146 frame = CreateTestFrame(frame_size); |
| 147 packet = encoder->Encode(*frame); |
| 148 EXPECT_TRUE(packet); |
| 149 } |
| 138 | 150 |
| 139 // Test that the DPI information is correctly propagated from the | 151 // Test that the DPI information is correctly propagated from the |
| 140 // media::ScreenCaptureData to the VideoPacket. | 152 // media::ScreenCaptureData to the VideoPacket. |
| 141 TEST(VideoEncoderVpxTest, TestDpiPropagation) { | 153 TEST(VideoEncoderVpxTest, TestDpiPropagation) { |
| 142 webrtc::DesktopSize frame_size(32, 32); | 154 webrtc::DesktopSize frame_size(32, 32); |
| 143 | 155 |
| 144 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP8()); | 156 scoped_ptr<VideoEncoderVpx> encoder(VideoEncoderVpx::CreateForVP8()); |
| 145 | 157 |
| 146 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); | 158 scoped_ptr<webrtc::DesktopFrame> frame(CreateTestFrame(frame_size)); |
| 147 frame->set_dpi(webrtc::DesktopVector(96, 97)); | 159 frame->set_dpi(webrtc::DesktopVector(96, 97)); |
| 148 scoped_ptr<VideoPacket> packet = encoder->Encode(*frame); | 160 scoped_ptr<VideoPacket> packet = encoder->Encode(*frame); |
| 149 EXPECT_EQ(packet->format().x_dpi(), 96); | 161 EXPECT_EQ(packet->format().x_dpi(), 96); |
| 150 EXPECT_EQ(packet->format().y_dpi(), 97); | 162 EXPECT_EQ(packet->format().y_dpi(), 97); |
| 151 } | 163 } |
| 152 | 164 |
| 153 } // namespace remoting | 165 } // namespace remoting |
| OLD | NEW |