| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "base/bind.h" |
| 6 #include "base/run_loop.h" |
| 7 #include "components/video_capture/mojo_video_frame.h" |
| 8 #include "components/video_capture/video_capture_adapter.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using ::testing::_; |
| 13 using ::testing::InSequence; |
| 14 using ::testing::SaveArg; |
| 15 |
| 16 namespace video_capture { |
| 17 |
| 18 class VideoCaptureAdapterTest : public testing::Test, |
| 19 public VideoCaptureAdapter::Delegate { |
| 20 public: |
| 21 VideoCaptureAdapterTest() : device_client_impl_(this) {} |
| 22 |
| 23 void SetUp() override {} |
| 24 void TearDown() override {} |
| 25 |
| 26 // VideoCaptureAdapter::Delegate |
| 27 MOCK_METHOD2(OnFrame, |
| 28 void(const scoped_refptr<MojoVideoFrame>& frame, |
| 29 const base::TimeTicks& timestamp)); |
| 30 MOCK_METHOD1(OnError, void(const std::string& reason)); |
| 31 |
| 32 void OnIncomingCapturedData(const uint8_t* data, |
| 33 int length, |
| 34 const media::VideoCaptureFormat& frame_format, |
| 35 int clockwise_rotation, |
| 36 const base::TimeTicks& timestamp) { |
| 37 device_client_impl_.OnIncomingCapturedData(data, length, frame_format, |
| 38 clockwise_rotation, timestamp); |
| 39 } |
| 40 void OnError(const tracked_objects::Location& from_here, |
| 41 const std::string& reason) { |
| 42 device_client_impl_.OnError(from_here, reason); |
| 43 } |
| 44 protected: |
| 45 base::MessageLoop loop_; |
| 46 |
| 47 // The mock of the (remote) video capture client. |
| 48 VideoCaptureAdapter device_client_impl_; |
| 49 |
| 50 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(VideoCaptureAdapterTest); |
| 52 }; |
| 53 |
| 54 TEST_F(VideoCaptureAdapterTest, CreateAndDestroy) { |
| 55 } |
| 56 |
| 57 // Tests that OnError is correctly forwarded. |
| 58 TEST_F(VideoCaptureAdapterTest, OnError) { |
| 59 const std::string kBigError("Kaiju-size error"); |
| 60 EXPECT_CALL(*this, OnError(kBigError)).Times(1); |
| 61 OnError(FROM_HERE, kBigError); |
| 62 base::RunLoop().RunUntilIdle(); |
| 63 } |
| 64 |
| 65 // Tests that buffer-based capture API accepts all memory-backed pixel formats. |
| 66 // Same as VideoCaptureDeviceClientTest's homonym test case. |
| 67 TEST_F(VideoCaptureAdapterTest, DataCaptureInEachVideoFormatInSequence) { |
| 68 // The usual ReserveOutputBuffer() -> OnIncomingCapturedVideoFrame() cannot |
| 69 // be used since it does not accept all pixel formats. The memory backed |
| 70 // buffer OnIncomingCapturedData() is used instead, with a dummy scratchpad |
| 71 // buffer. |
| 72 const size_t kScratchpadSizeInBytes = 400; |
| 73 unsigned char data[kScratchpadSizeInBytes] = {}; |
| 74 const gfx::Size capture_resolution(10, 10); |
| 75 ASSERT_GE(kScratchpadSizeInBytes, capture_resolution.GetArea() * 4u) |
| 76 << "Scratchpad is too small to hold the largest pixel format (ARGB)."; |
| 77 |
| 78 for (int format = 0; format < media::PIXEL_FORMAT_MAX; ++format) { |
| 79 // Conversion from some formats are unsupported. |
| 80 if (format == media::PIXEL_FORMAT_UNKNOWN || |
| 81 format == media::PIXEL_FORMAT_YV16 || |
| 82 format == media::PIXEL_FORMAT_YV12A || |
| 83 format == media::PIXEL_FORMAT_YV24 || |
| 84 format == media::PIXEL_FORMAT_ARGB || |
| 85 format == media::PIXEL_FORMAT_XRGB || |
| 86 format == media::PIXEL_FORMAT_MJPEG || |
| 87 format == media::PIXEL_FORMAT_MT21) { |
| 88 continue; |
| 89 } |
| 90 #if !defined(OS_LINUX) && !defined(OS_WIN) |
| 91 if (format == media::PIXEL_FORMAT_RGB24){ |
| 92 continue; |
| 93 } |
| 94 #endif |
| 95 media::VideoCaptureParams params; |
| 96 params.requested_format = media::VideoCaptureFormat( |
| 97 capture_resolution, 30.0f, media::VideoPixelFormat(format)); |
| 98 |
| 99 scoped_refptr<MojoVideoFrame> frame; |
| 100 const base::TimeTicks now = base::TimeTicks::Now(); |
| 101 |
| 102 EXPECT_CALL(*this, OnFrame(_, now)) |
| 103 .Times(1) |
| 104 .WillOnce(SaveArg<0>(&frame)); |
| 105 OnIncomingCapturedData(data, params.requested_format.ImageAllocationSize(), |
| 106 params.requested_format, 0 /* clockwise_rotation */, |
| 107 now); |
| 108 base::RunLoop().RunUntilIdle(); |
| 109 |
| 110 EXPECT_EQ(media::PIXEL_FORMAT_I420, frame->format()); |
| 111 EXPECT_EQ(10, frame->coded_size().width()); |
| 112 EXPECT_EQ(10, frame->coded_size().height()); |
| 113 } |
| 114 } |
| 115 |
| 116 // Test that we receive the expected resolution for a given captured frame |
| 117 // resolution and rotation. Odd resolutions are also cropped. |
| 118 // Same as VideoCaptureDeviceClientTest's homonym test case. |
| 119 TEST_F(VideoCaptureAdapterTest, CheckRotationsAndCrops) { |
| 120 const struct SizeAndRotation { |
| 121 gfx::Size input_resolution; |
| 122 int rotation; |
| 123 gfx::Size output_resolution; |
| 124 } kSizeAndRotations[] = {{{6, 4}, 0, {6, 4}}, |
| 125 {{6, 4}, 90, {4, 6}}, |
| 126 {{6, 4}, 180, {6, 4}}, |
| 127 {{6, 4}, 270, {4, 6}}, |
| 128 {{7, 4}, 0, {6, 4}}, |
| 129 {{7, 4}, 90, {4, 6}}, |
| 130 {{7, 4}, 180, {6, 4}}, |
| 131 {{7, 4}, 270, {4, 6}}}; |
| 132 |
| 133 // The usual ReserveOutputBuffer() -> OnIncomingCapturedVideoFrame() cannot |
| 134 // be used since it does not resolve rotations or crops. The memory backed |
| 135 // buffer OnIncomingCapturedData() is used instead, with a dummy scratchpad |
| 136 // buffer. |
| 137 const size_t kScratchpadSizeInBytes = 400; |
| 138 unsigned char data[kScratchpadSizeInBytes] = {}; |
| 139 |
| 140 media::VideoCaptureParams params; |
| 141 for (const auto& size_and_rotation : kSizeAndRotations) { |
| 142 ASSERT_GE(kScratchpadSizeInBytes, |
| 143 size_and_rotation.input_resolution.GetArea() * 4u) |
| 144 << "Scratchpad is too small to hold the largest pixel format (ARGB)."; |
| 145 params.requested_format = |
| 146 media::VideoCaptureFormat(size_and_rotation.input_resolution, 30.0f, |
| 147 media::PIXEL_FORMAT_ARGB); |
| 148 |
| 149 scoped_refptr<MojoVideoFrame> frame; |
| 150 const base::TimeTicks now = base::TimeTicks::Now(); |
| 151 EXPECT_CALL(*this, OnFrame(_, now)) |
| 152 .Times(1) |
| 153 .WillOnce(SaveArg<0>(&frame)); |
| 154 OnIncomingCapturedData(data, params.requested_format.ImageAllocationSize(), |
| 155 params.requested_format, size_and_rotation.rotation, |
| 156 now); |
| 157 base::RunLoop().RunUntilIdle(); |
| 158 |
| 159 EXPECT_EQ(frame->coded_size().width(), |
| 160 size_and_rotation.output_resolution.width()); |
| 161 EXPECT_EQ(frame->coded_size().height(), |
| 162 size_and_rotation.output_resolution.height()); |
| 163 } |
| 164 } |
| 165 |
| 166 |
| 167 } // namespace video_capture |
| OLD | NEW |