| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "media/base/video_frame.h" | 7 #include "media/base/video_frame.h" |
| 8 #include "media/mojo/common/media_type_converters.h" | 8 #include "media/mojo/common/media_type_converters.h" |
| 9 #include "services/video_capture/device_media_to_mojo_adapter.h" | 9 #include "services/video_capture/device_media_to_mojo_adapter.h" |
| 10 #include "services/video_capture/public/interfaces/device_factory.mojom.h" | 10 #include "services/video_capture/public/interfaces/device_factory.mojom.h" |
| 11 #include "services/video_capture/test/fake_device_test.h" | 11 #include "services/video_capture/test/fake_device_test.h" |
| 12 #include "services/video_capture/test/mock_receiver.h" | 12 #include "services/video_capture/test/mock_receiver.h" |
| 13 | 13 |
| 14 using testing::_; | 14 using testing::_; |
| 15 using testing::AtLeast; |
| 15 using testing::Invoke; | 16 using testing::Invoke; |
| 16 using testing::InvokeWithoutArgs; | 17 using testing::InvokeWithoutArgs; |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 struct FrameInfo { | 21 struct FrameInfo { |
| 21 gfx::Size size; | 22 gfx::Size size; |
| 22 media::VideoPixelFormat pixel_format; | 23 media::VideoPixelFormat pixel_format; |
| 23 media::VideoFrame::StorageType storage_type; | 24 media::VideoPixelStorage storage_type; |
| 24 base::TimeDelta timestamp; | 25 base::TimeDelta timestamp; |
| 25 }; | 26 }; |
| 26 | 27 |
| 27 } // anonymous namespace | 28 } // anonymous namespace |
| 28 | 29 |
| 29 namespace video_capture { | 30 namespace video_capture { |
| 30 | 31 |
| 31 // This alias ensures test output is easily attributed to this service's tests. | 32 // This alias ensures test output is easily attributed to this service's tests. |
| 32 // TODO(rockot/chfremer): Consider just renaming the type. | 33 // TODO(rockot/chfremer): Consider just renaming the type. |
| 33 using FakeVideoCaptureDeviceTest = FakeDeviceTest; | 34 using FakeVideoCaptureDeviceTest = FakeDeviceTest; |
| 34 | 35 |
| 35 TEST_F(FakeVideoCaptureDeviceTest, DISABLED_FrameCallbacksArrive) { | 36 TEST_F(FakeVideoCaptureDeviceTest, FrameCallbacksArrive) { |
| 36 base::RunLoop wait_loop; | 37 base::RunLoop wait_loop; |
| 37 // These two constants must be static as a workaround | 38 // Constants must be static as a workaround |
| 38 // for a MSVC++ bug about lambda captures, see the discussion at | 39 // for a MSVC++ bug about lambda captures, see the discussion at |
| 39 // https://social.msdn.microsoft.com/Forums/SqlServer/4abf18bd-4ae4-4c72-ba3e-
3b13e7909d5f | 40 // https://social.msdn.microsoft.com/Forums/SqlServer/4abf18bd-4ae4-4c72-ba3e-
3b13e7909d5f |
| 40 static const int kNumFramesToWaitFor = 3; | 41 static const int kNumFramesToWaitFor = 3; |
| 41 int num_frames_arrived = 0; | 42 int num_frames_arrived = 0; |
| 42 mojom::ReceiverPtr receiver_proxy; | 43 mojom::ReceiverPtr receiver_proxy; |
| 43 MockReceiver receiver(mojo::MakeRequest(&receiver_proxy)); | 44 MockReceiver receiver(mojo::MakeRequest(&receiver_proxy)); |
| 44 EXPECT_CALL(receiver, OnIncomingCapturedVideoFramePtr(_)) | 45 EXPECT_CALL(receiver, DoOnNewBufferHandle(_, _)).Times(AtLeast(1)); |
| 45 .WillRepeatedly(InvokeWithoutArgs( | 46 EXPECT_CALL(receiver, DoOnFrameReadyInBuffer(_, _, _, _)) |
| 46 [&wait_loop, &num_frames_arrived]() { | 47 .WillRepeatedly(InvokeWithoutArgs([&wait_loop, &num_frames_arrived]() { |
| 47 num_frames_arrived += 1; | 48 num_frames_arrived += 1; |
| 48 if (num_frames_arrived >= kNumFramesToWaitFor) { | 49 if (num_frames_arrived >= kNumFramesToWaitFor) { |
| 49 wait_loop.Quit(); | 50 wait_loop.Quit(); |
| 50 } | 51 } |
| 51 })); | 52 })); |
| 52 | 53 |
| 53 fake_device_proxy_->Start(requestable_settings_, std::move(receiver_proxy)); | 54 fake_device_proxy_->Start(requestable_settings_, std::move(receiver_proxy)); |
| 54 wait_loop.Run(); | 55 wait_loop.Run(); |
| 55 } | 56 } |
| 56 | 57 |
| 57 // Tests that frames received from a fake capture device match the requested | 58 // Tests that frames received from a fake capture device match the requested |
| 58 // format and have increasing timestamps. | 59 // format and have increasing timestamps. |
| 59 TEST_F(FakeVideoCaptureDeviceTest, | 60 TEST_F(FakeVideoCaptureDeviceTest, ReceiveFramesFromFakeCaptureDevice) { |
| 60 DISABLED_ReceiveFramesFromFakeCaptureDevice) { | |
| 61 base::RunLoop wait_loop; | 61 base::RunLoop wait_loop; |
| 62 mojom::ReceiverPtr receiver_proxy; | 62 mojom::ReceiverPtr receiver_proxy; |
| 63 // These two constants must be static as a workaround | 63 // Constants must be static as a workaround |
| 64 // for a MSVC++ bug about lambda captures, see the discussion at | 64 // for a MSVC++ bug about lambda captures, see the discussion at |
| 65 // https://social.msdn.microsoft.com/Forums/SqlServer/4abf18bd-4ae4-4c72-ba3e-
3b13e7909d5f | 65 // https://social.msdn.microsoft.com/Forums/SqlServer/4abf18bd-4ae4-4c72-ba3e-
3b13e7909d5f |
| 66 static const int num_frames_to_receive = 2; | 66 static const int num_frames_to_receive = 2; |
| 67 FrameInfo received_frame_infos[num_frames_to_receive]; | 67 FrameInfo received_frame_infos[num_frames_to_receive]; |
| 68 int received_frame_count = 0; | 68 int received_frame_count = 0; |
| 69 MockReceiver receiver(mojo::MakeRequest(&receiver_proxy)); | 69 MockReceiver receiver(mojo::MakeRequest(&receiver_proxy)); |
| 70 EXPECT_CALL(receiver, OnIncomingCapturedVideoFramePtr(_)) | 70 EXPECT_CALL(receiver, DoOnNewBufferHandle(_, _)).Times(AtLeast(1)); |
| 71 .WillRepeatedly(Invoke( | 71 EXPECT_CALL(receiver, DoOnFrameReadyInBuffer(_, _, _, _)) |
| 72 [&received_frame_infos, &received_frame_count, &wait_loop] | 72 .WillRepeatedly( |
| 73 (const media::mojom::VideoFramePtr* frame) { | 73 Invoke([&received_frame_infos, &received_frame_count, &wait_loop]( |
| 74 int32_t buffer_id, int32_t frame_feedback_id, |
| 75 mojom::ScopedAccessPermissionPtr* access_permission, |
| 76 media::mojom::VideoFrameInfoPtr* frame_info) { |
| 74 if (received_frame_count >= num_frames_to_receive) | 77 if (received_frame_count >= num_frames_to_receive) |
| 75 return; | 78 return; |
| 76 auto video_frame = frame->To<scoped_refptr<media::VideoFrame>>(); | 79 auto& received_frame_info = |
| 77 auto& frame_info = received_frame_infos[received_frame_count]; | 80 received_frame_infos[received_frame_count]; |
| 78 frame_info.pixel_format = video_frame->format(); | 81 received_frame_info.pixel_format = (*frame_info)->pixel_format; |
| 79 frame_info.storage_type = video_frame->storage_type(); | 82 received_frame_info.storage_type = (*frame_info)->storage_type; |
| 80 frame_info.size = video_frame->natural_size(); | 83 received_frame_info.size = (*frame_info)->coded_size; |
| 81 frame_info.timestamp = video_frame->timestamp(); | 84 received_frame_info.timestamp = (*frame_info)->timestamp; |
| 82 received_frame_count += 1; | 85 received_frame_count += 1; |
| 83 if (received_frame_count == num_frames_to_receive) | 86 if (received_frame_count == num_frames_to_receive) |
| 84 wait_loop.Quit(); | 87 wait_loop.Quit(); |
| 85 })); | 88 })); |
| 86 | 89 |
| 87 fake_device_proxy_->Start(requestable_settings_, std::move(receiver_proxy)); | 90 fake_device_proxy_->Start(requestable_settings_, std::move(receiver_proxy)); |
| 88 | 91 |
| 89 wait_loop.Run(); | 92 wait_loop.Run(); |
| 90 | 93 |
| 91 base::TimeDelta previous_timestamp; | 94 base::TimeDelta previous_timestamp; |
| 92 for (int i = 0; i < num_frames_to_receive; i++) { | 95 for (int i = 0; i < num_frames_to_receive; i++) { |
| 93 auto& frame_info = received_frame_infos[i]; | 96 auto& frame_info = received_frame_infos[i]; |
| 94 // Service is expected to always output I420 | 97 // Service is expected to always output I420 |
| 95 EXPECT_EQ(media::PIXEL_FORMAT_I420, frame_info.pixel_format); | 98 EXPECT_EQ(media::PIXEL_FORMAT_I420, frame_info.pixel_format); |
| 96 // Service is expected to always use STORAGE_MOJO_SHARED_BUFFER | 99 // Service is expected to always use PIXEL_STORAGE_CPU |
| 97 EXPECT_EQ(media::VideoFrame::STORAGE_MOJO_SHARED_BUFFER, | 100 EXPECT_EQ(media::PIXEL_STORAGE_CPU, frame_info.storage_type); |
| 98 frame_info.storage_type); | 101 EXPECT_EQ(requestable_settings_.requested_format.frame_size, |
| 102 frame_info.size); |
| 99 // Timestamps are expected to increase | 103 // Timestamps are expected to increase |
| 100 if (i > 0) | 104 if (i > 0) |
| 101 EXPECT_GT(frame_info.timestamp, previous_timestamp); | 105 EXPECT_GT(frame_info.timestamp, previous_timestamp); |
| 102 previous_timestamp = frame_info.timestamp; | 106 previous_timestamp = frame_info.timestamp; |
| 103 } | 107 } |
| 104 } | 108 } |
| 105 | 109 |
| 110 // Tests that buffers get reused when receiving more frames than the maximum |
| 111 // number of buffers in the pool. |
| 112 TEST_F(FakeVideoCaptureDeviceTest, BuffersGetReused) { |
| 113 base::RunLoop wait_loop; |
| 114 const int kMaxBufferPoolBuffers = |
| 115 DeviceMediaToMojoAdapter::max_buffer_pool_buffer_count(); |
| 116 // Constants must be static as a workaround |
| 117 // for a MSVC++ bug about lambda captures, see the discussion at |
| 118 // https://social.msdn.microsoft.com/Forums/SqlServer/4abf18bd-4ae4-4c72-ba3e-
3b13e7909d5f |
| 119 static const int kNumFramesToWaitFor = kMaxBufferPoolBuffers + 3; |
| 120 int num_buffers_created = 0; |
| 121 int num_frames_arrived = 0; |
| 122 mojom::ReceiverPtr receiver_proxy; |
| 123 MockReceiver receiver(mojo::MakeRequest(&receiver_proxy)); |
| 124 EXPECT_CALL(receiver, DoOnNewBufferHandle(_, _)) |
| 125 .WillRepeatedly(InvokeWithoutArgs( |
| 126 [&num_buffers_created]() { num_buffers_created++; })); |
| 127 EXPECT_CALL(receiver, DoOnFrameReadyInBuffer(_, _, _, _)) |
| 128 .WillRepeatedly(InvokeWithoutArgs([&wait_loop, &num_frames_arrived]() { |
| 129 if (++num_frames_arrived >= kNumFramesToWaitFor) { |
| 130 wait_loop.Quit(); |
| 131 } |
| 132 })); |
| 133 |
| 134 fake_device_proxy_->Start(requestable_settings_, std::move(receiver_proxy)); |
| 135 wait_loop.Run(); |
| 136 |
| 137 ASSERT_LT(num_buffers_created, num_frames_arrived); |
| 138 ASSERT_LE(num_buffers_created, kMaxBufferPoolBuffers); |
| 139 } |
| 140 |
| 106 } // namespace video_capture | 141 } // namespace video_capture |
| OLD | NEW |