| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
| 8 #include "content/child/child_process.h" | 8 #include "content/child/child_process.h" |
| 9 #include "content/renderer/media/canvas_capture_handler.h" | 9 #include "content/renderer/media/canvas_capture_handler.h" |
| 10 #include "content/renderer/media/media_stream_video_capturer_source.h" | 10 #include "content/renderer/media/media_stream_video_capturer_source.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 using ::testing::TestWithParam; | 24 using ::testing::TestWithParam; |
| 25 | 25 |
| 26 namespace content { | 26 namespace content { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 static const int kTestCanvasCaptureWidth = 320; | 30 static const int kTestCanvasCaptureWidth = 320; |
| 31 static const int kTestCanvasCaptureHeight = 240; | 31 static const int kTestCanvasCaptureHeight = 240; |
| 32 static const double kTestCanvasCaptureFramesPerSecond = 55.5; | 32 static const double kTestCanvasCaptureFramesPerSecond = 55.5; |
| 33 | 33 |
| 34 static const int kTestCanvasCaptureFrameWidth = 2; | 34 static const int kTestCanvasCaptureFrameEvenSize = 2; |
| 35 static const int kTestCanvasCaptureFrameHeight = 2; | 35 static const int kTestCanvasCaptureFrameOddSize = 3; |
| 36 static const int kTestCanvasCaptureFrameErrorTolerance = 2; | 36 static const int kTestCanvasCaptureFrameColorErrorTolerance = 2; |
| 37 static const int kTestAlphaValue = 175; | 37 static const int kTestAlphaValue = 175; |
| 38 | 38 |
| 39 ACTION_P(RunClosure, closure) { | 39 ACTION_P(RunClosure, closure) { |
| 40 closure.Run(); | 40 closure.Run(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 class CanvasCaptureHandlerTest : public TestWithParam<bool> { | 45 class CanvasCaptureHandlerTest |
| 46 : public TestWithParam<testing::tuple<bool, int, int>> { |
| 46 public: | 47 public: |
| 47 CanvasCaptureHandlerTest() {} | 48 CanvasCaptureHandlerTest() {} |
| 48 | 49 |
| 49 void SetUp() override { | 50 void SetUp() override { |
| 50 canvas_capture_handler_.reset( | 51 canvas_capture_handler_.reset( |
| 51 CanvasCaptureHandler::CreateCanvasCaptureHandler( | 52 CanvasCaptureHandler::CreateCanvasCaptureHandler( |
| 52 blink::WebSize(kTestCanvasCaptureWidth, kTestCanvasCaptureHeight), | 53 blink::WebSize(kTestCanvasCaptureWidth, kTestCanvasCaptureHeight), |
| 53 kTestCanvasCaptureFramesPerSecond, message_loop_.task_runner(), | 54 kTestCanvasCaptureFramesPerSecond, message_loop_.task_runner(), |
| 54 &track_)); | 55 &track_)); |
| 55 } | 56 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 74 MOCK_METHOD1(DoOnVideoCaptureDeviceFormats, | 75 MOCK_METHOD1(DoOnVideoCaptureDeviceFormats, |
| 75 void(const media::VideoCaptureFormats&)); | 76 void(const media::VideoCaptureFormats&)); |
| 76 void OnVideoCaptureDeviceFormats(const media::VideoCaptureFormats& formats) { | 77 void OnVideoCaptureDeviceFormats(const media::VideoCaptureFormats& formats) { |
| 77 DoOnVideoCaptureDeviceFormats(formats); | 78 DoOnVideoCaptureDeviceFormats(formats); |
| 78 } | 79 } |
| 79 | 80 |
| 80 MOCK_METHOD1(DoOnRunning, void(bool)); | 81 MOCK_METHOD1(DoOnRunning, void(bool)); |
| 81 void OnRunning(bool state) { DoOnRunning(state); } | 82 void OnRunning(bool state) { DoOnRunning(state); } |
| 82 | 83 |
| 83 // Verify returned frames. | 84 // Verify returned frames. |
| 84 static sk_sp<SkImage> GenerateTestImage(bool opaque) { | 85 static sk_sp<SkImage> GenerateTestImage(bool opaque, int width, int height) { |
| 85 | |
| 86 SkBitmap testBitmap; | 86 SkBitmap testBitmap; |
| 87 testBitmap.allocN32Pixels(kTestCanvasCaptureFrameWidth, | 87 testBitmap.allocN32Pixels(width, height, opaque); |
| 88 kTestCanvasCaptureFrameHeight, opaque); | |
| 89 testBitmap.eraseARGB(kTestAlphaValue, 30, 60, 200); | 88 testBitmap.eraseARGB(kTestAlphaValue, 30, 60, 200); |
| 90 return SkImage::MakeFromBitmap(testBitmap); | 89 return SkImage::MakeFromBitmap(testBitmap); |
| 91 } | 90 } |
| 92 | 91 |
| 93 void OnVerifyDeliveredFrame( | 92 void OnVerifyDeliveredFrame( |
| 94 bool opaque, | 93 bool opaque, |
| 94 int expected_width, |
| 95 int expected_height, |
| 95 const scoped_refptr<media::VideoFrame>& video_frame, | 96 const scoped_refptr<media::VideoFrame>& video_frame, |
| 96 base::TimeTicks estimated_capture_time) { | 97 base::TimeTicks estimated_capture_time) { |
| 97 if (opaque) | 98 if (opaque) |
| 98 EXPECT_EQ(media::PIXEL_FORMAT_I420, video_frame->format()); | 99 EXPECT_EQ(media::PIXEL_FORMAT_I420, video_frame->format()); |
| 99 else | 100 else |
| 100 EXPECT_EQ(media::PIXEL_FORMAT_YV12A, video_frame->format()); | 101 EXPECT_EQ(media::PIXEL_FORMAT_YV12A, video_frame->format()); |
| 101 | 102 |
| 102 EXPECT_EQ(video_frame->timestamp().InMilliseconds(), | 103 EXPECT_EQ(video_frame->timestamp().InMilliseconds(), |
| 103 (estimated_capture_time - base::TimeTicks()).InMilliseconds()); | 104 (estimated_capture_time - base::TimeTicks()).InMilliseconds()); |
| 104 const gfx::Size& size = video_frame->coded_size(); | 105 const gfx::Size& size = video_frame->visible_rect().size(); |
| 105 EXPECT_EQ(kTestCanvasCaptureFrameWidth, size.width()); | 106 EXPECT_EQ(expected_width, size.width()); |
| 106 EXPECT_EQ(kTestCanvasCaptureFrameHeight, size.height()); | 107 EXPECT_EQ(expected_height, size.height()); |
| 107 const uint8_t* y_plane = video_frame->data(media::VideoFrame::kYPlane); | 108 const uint8_t* y_plane = |
| 108 EXPECT_NEAR(74, y_plane[0], kTestCanvasCaptureFrameErrorTolerance); | 109 video_frame->visible_data(media::VideoFrame::kYPlane); |
| 109 const uint8_t* u_plane = video_frame->data(media::VideoFrame::kUPlane); | 110 EXPECT_NEAR(74, y_plane[0], kTestCanvasCaptureFrameColorErrorTolerance); |
| 110 EXPECT_NEAR(193, u_plane[0], kTestCanvasCaptureFrameErrorTolerance); | 111 const uint8_t* u_plane = |
| 111 const uint8_t* v_plane = video_frame->data(media::VideoFrame::kVPlane); | 112 video_frame->visible_data(media::VideoFrame::kUPlane); |
| 112 EXPECT_NEAR(105, v_plane[0], kTestCanvasCaptureFrameErrorTolerance); | 113 EXPECT_NEAR(193, u_plane[0], kTestCanvasCaptureFrameColorErrorTolerance); |
| 114 const uint8_t* v_plane = |
| 115 video_frame->visible_data(media::VideoFrame::kVPlane); |
| 116 EXPECT_NEAR(105, v_plane[0], kTestCanvasCaptureFrameColorErrorTolerance); |
| 113 if (!opaque) { | 117 if (!opaque) { |
| 114 const uint8_t* a_plane = video_frame->data(media::VideoFrame::kAPlane); | 118 const uint8_t* a_plane = |
| 119 video_frame->visible_data(media::VideoFrame::kAPlane); |
| 115 EXPECT_EQ(kTestAlphaValue, a_plane[0]); | 120 EXPECT_EQ(kTestAlphaValue, a_plane[0]); |
| 116 } | 121 } |
| 117 } | 122 } |
| 118 | 123 |
| 119 blink::WebMediaStreamTrack track_; | 124 blink::WebMediaStreamTrack track_; |
| 120 // The Class under test. Needs to be scoped_ptr to force its destruction. | 125 // The Class under test. Needs to be scoped_ptr to force its destruction. |
| 121 std::unique_ptr<CanvasCaptureHandler> canvas_capture_handler_; | 126 std::unique_ptr<CanvasCaptureHandler> canvas_capture_handler_; |
| 122 | 127 |
| 123 protected: | 128 protected: |
| 124 media::VideoCapturerSource* GetVideoCapturerSource( | 129 media::VideoCapturerSource* GetVideoCapturerSource( |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 base::RunLoop run_loop; | 191 base::RunLoop run_loop; |
| 187 base::Closure quit_closure = run_loop.QuitClosure(); | 192 base::Closure quit_closure = run_loop.QuitClosure(); |
| 188 EXPECT_CALL(*this, DoOnRunning(true)).Times(1); | 193 EXPECT_CALL(*this, DoOnRunning(true)).Times(1); |
| 189 EXPECT_CALL(*this, DoOnDeliverFrame(_, _)) | 194 EXPECT_CALL(*this, DoOnDeliverFrame(_, _)) |
| 190 .Times(1) | 195 .Times(1) |
| 191 .WillOnce(RunClosure(quit_closure)); | 196 .WillOnce(RunClosure(quit_closure)); |
| 192 source->StartCapture( | 197 source->StartCapture( |
| 193 params, base::Bind(&CanvasCaptureHandlerTest::OnDeliverFrame, | 198 params, base::Bind(&CanvasCaptureHandlerTest::OnDeliverFrame, |
| 194 base::Unretained(this)), | 199 base::Unretained(this)), |
| 195 base::Bind(&CanvasCaptureHandlerTest::OnRunning, base::Unretained(this))); | 200 base::Bind(&CanvasCaptureHandlerTest::OnRunning, base::Unretained(this))); |
| 196 canvas_capture_handler_->sendNewFrame(GenerateTestImage(GetParam()).get()); | 201 canvas_capture_handler_->sendNewFrame( |
| 202 GenerateTestImage(testing::get<0>(GetParam()), |
| 203 testing::get<1>(GetParam()), |
| 204 testing::get<2>(GetParam())) |
| 205 .get()); |
| 197 run_loop.Run(); | 206 run_loop.Run(); |
| 198 | 207 |
| 199 source->StopCapture(); | 208 source->StopCapture(); |
| 200 } | 209 } |
| 201 | 210 |
| 202 // Verifies that SkImage is processed and produces VideoFrame as expected. | 211 // Verifies that SkImage is processed and produces VideoFrame as expected. |
| 203 TEST_P(CanvasCaptureHandlerTest, VerifyOpaqueFrame) { | 212 TEST_P(CanvasCaptureHandlerTest, VerifyFrame) { |
| 204 const bool isOpaque = GetParam(); | 213 const bool opaque_frame = testing::get<0>(GetParam()); |
| 214 const bool width = testing::get<1>(GetParam()); |
| 215 const bool height = testing::get<1>(GetParam()); |
| 205 InSequence s; | 216 InSequence s; |
| 206 media::VideoCapturerSource* const source = | 217 media::VideoCapturerSource* const source = |
| 207 GetVideoCapturerSource(static_cast<MediaStreamVideoCapturerSource*>( | 218 GetVideoCapturerSource(static_cast<MediaStreamVideoCapturerSource*>( |
| 208 track_.source().getExtraData())); | 219 track_.source().getExtraData())); |
| 209 EXPECT_TRUE(source != nullptr); | 220 EXPECT_TRUE(source != nullptr); |
| 210 | 221 |
| 211 base::RunLoop run_loop; | 222 base::RunLoop run_loop; |
| 212 EXPECT_CALL(*this, DoOnRunning(true)).Times(1); | 223 EXPECT_CALL(*this, DoOnRunning(true)).Times(1); |
| 213 media::VideoCaptureParams params; | 224 media::VideoCaptureParams params; |
| 214 source->StartCapture( | 225 source->StartCapture( |
| 215 params, base::Bind(&CanvasCaptureHandlerTest::OnVerifyDeliveredFrame, | 226 params, base::Bind(&CanvasCaptureHandlerTest::OnVerifyDeliveredFrame, |
| 216 base::Unretained(this), isOpaque), | 227 base::Unretained(this), opaque_frame, width, height), |
| 217 base::Bind(&CanvasCaptureHandlerTest::OnRunning, base::Unretained(this))); | 228 base::Bind(&CanvasCaptureHandlerTest::OnRunning, base::Unretained(this))); |
| 218 canvas_capture_handler_->sendNewFrame(GenerateTestImage(isOpaque).get()); | 229 canvas_capture_handler_->sendNewFrame( |
| 230 GenerateTestImage(opaque_frame, width, height).get()); |
| 219 run_loop.RunUntilIdle(); | 231 run_loop.RunUntilIdle(); |
| 220 } | 232 } |
| 221 | 233 |
| 222 // Checks that needsNewFrame() works as expected. | 234 // Checks that needsNewFrame() works as expected. |
| 223 TEST_F(CanvasCaptureHandlerTest, CheckNeedsNewFrame) { | 235 TEST_F(CanvasCaptureHandlerTest, CheckNeedsNewFrame) { |
| 224 InSequence s; | 236 InSequence s; |
| 225 media::VideoCapturerSource* source = | 237 media::VideoCapturerSource* source = |
| 226 GetVideoCapturerSource(static_cast<MediaStreamVideoCapturerSource*>( | 238 GetVideoCapturerSource(static_cast<MediaStreamVideoCapturerSource*>( |
| 227 track_.source().getExtraData())); | 239 track_.source().getExtraData())); |
| 228 EXPECT_TRUE(source != nullptr); | 240 EXPECT_TRUE(source != nullptr); |
| 229 EXPECT_TRUE(canvas_capture_handler_->needsNewFrame()); | 241 EXPECT_TRUE(canvas_capture_handler_->needsNewFrame()); |
| 230 source->StopCapture(); | 242 source->StopCapture(); |
| 231 EXPECT_FALSE(canvas_capture_handler_->needsNewFrame()); | 243 EXPECT_FALSE(canvas_capture_handler_->needsNewFrame()); |
| 232 } | 244 } |
| 233 | 245 |
| 234 INSTANTIATE_TEST_CASE_P(, CanvasCaptureHandlerTest, ::testing::Bool()); | 246 INSTANTIATE_TEST_CASE_P( |
| 247 , |
| 248 CanvasCaptureHandlerTest, |
| 249 ::testing::Combine(::testing::Bool(), |
| 250 ::testing::Values(kTestCanvasCaptureFrameEvenSize, |
| 251 kTestCanvasCaptureFrameOddSize), |
| 252 ::testing::Values(kTestCanvasCaptureFrameEvenSize, |
| 253 kTestCanvasCaptureFrameOddSize))); |
| 235 | 254 |
| 236 } // namespace content | 255 } // namespace content |
| OLD | NEW |