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