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::AtLeast; |
16 using testing::Invoke; | 16 using testing::Invoke; |
17 using testing::InvokeWithoutArgs; | 17 using testing::InvokeWithoutArgs; |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 struct FrameInfo { | 21 struct FrameInfo { |
22 gfx::Size size; | 22 gfx::Size size; |
23 media::VideoPixelFormat pixel_format; | 23 media::VideoPixelFormat pixel_format; |
24 media::VideoFrame::StorageType storage_type; | 24 media::VideoPixelStorage storage_type; |
25 base::TimeDelta timestamp; | 25 base::TimeDelta timestamp; |
26 }; | 26 }; |
27 | 27 |
28 } // anonymous namespace | 28 } // anonymous namespace |
29 | 29 |
30 namespace video_capture { | 30 namespace video_capture { |
31 | 31 |
32 // 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. |
33 // TODO(rockot/chfremer): Consider just renaming the type. | 33 // TODO(rockot/chfremer): Consider just renaming the type. |
34 using FakeVideoCaptureDeviceTest = FakeDeviceTest; | 34 using FakeVideoCaptureDeviceTest = FakeDeviceTest; |
35 | 35 |
36 TEST_F(FakeVideoCaptureDeviceTest, DISABLED_FrameCallbacksArrive) { | 36 TEST_F(FakeVideoCaptureDeviceTest, FrameCallbacksArrive) { |
37 base::RunLoop wait_loop; | 37 base::RunLoop wait_loop; |
38 // These two constants must be static as a workaround | 38 // Constants must be static as a workaround |
39 // for a MSVC++ bug about lambda captures, see the discussion at | 39 // for a MSVC++ bug about lambda captures, see the discussion at |
40 // 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 |
41 static const int kNumFramesToWaitFor = 3; | 41 static const int kNumFramesToWaitFor = 3; |
42 int num_frames_arrived = 0; | 42 int num_frames_arrived = 0; |
43 mojom::ReceiverPtr receiver_proxy; | 43 mojom::ReceiverPtr receiver_proxy; |
44 MockReceiver receiver(mojo::MakeRequest(&receiver_proxy)); | 44 MockReceiver receiver(mojo::MakeRequest(&receiver_proxy)); |
45 EXPECT_CALL(receiver, OnIncomingCapturedVideoFramePtr(_)) | 45 EXPECT_CALL(receiver, DoOnNewBufferHandle(_, _)).Times(AtLeast(1)); |
46 .WillRepeatedly(InvokeWithoutArgs( | 46 EXPECT_CALL(receiver, DoOnFrameReadyInBuffer(_, _, _, _)) |
47 [&wait_loop, &num_frames_arrived]() { | 47 .WillRepeatedly(InvokeWithoutArgs([&wait_loop, &num_frames_arrived]() { |
48 num_frames_arrived += 1; | 48 num_frames_arrived += 1; |
49 if (num_frames_arrived >= kNumFramesToWaitFor) { | 49 if (num_frames_arrived >= kNumFramesToWaitFor) { |
50 wait_loop.Quit(); | 50 wait_loop.Quit(); |
51 } | 51 } |
52 })); | 52 })); |
53 | 53 |
54 fake_device_proxy_->Start(requestable_settings_, std::move(receiver_proxy)); | 54 fake_device_proxy_->Start(requestable_settings_, std::move(receiver_proxy)); |
55 wait_loop.Run(); | 55 wait_loop.Run(); |
56 } | 56 } |
57 | 57 |
58 // 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 |
59 // format and have increasing timestamps. | 59 // format and have increasing timestamps. |
60 TEST_F(FakeVideoCaptureDeviceTest, | 60 TEST_F(FakeVideoCaptureDeviceTest, ReceiveFramesFromFakeCaptureDevice) { |
61 DISABLED_ReceiveFramesFromFakeCaptureDevice) { | |
62 base::RunLoop wait_loop; | 61 base::RunLoop wait_loop; |
63 mojom::ReceiverPtr receiver_proxy; | 62 mojom::ReceiverPtr receiver_proxy; |
64 // These two constants must be static as a workaround | 63 // Constants must be static as a workaround |
65 // for a MSVC++ bug about lambda captures, see the discussion at | 64 // for a MSVC++ bug about lambda captures, see the discussion at |
66 // 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 |
67 static const int num_frames_to_receive = 2; | 66 static const int num_frames_to_receive = 2; |
68 FrameInfo received_frame_infos[num_frames_to_receive]; | 67 FrameInfo received_frame_infos[num_frames_to_receive]; |
69 int received_frame_count = 0; | 68 int received_frame_count = 0; |
70 MockReceiver receiver(mojo::MakeRequest(&receiver_proxy)); | 69 MockReceiver receiver(mojo::MakeRequest(&receiver_proxy)); |
71 EXPECT_CALL(receiver, OnIncomingCapturedVideoFramePtr(_)) | 70 EXPECT_CALL(receiver, DoOnNewBufferHandle(_, _)).Times(AtLeast(1)); |
72 .WillRepeatedly(Invoke( | 71 EXPECT_CALL(receiver, DoOnFrameReadyInBuffer(_, _, _, _)) |
73 [&received_frame_infos, &received_frame_count, &wait_loop] | 72 .WillRepeatedly( |
74 (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) { | |
75 if (received_frame_count >= num_frames_to_receive) | 77 if (received_frame_count >= num_frames_to_receive) |
76 return; | 78 return; |
77 auto video_frame = frame->To<scoped_refptr<media::VideoFrame>>(); | 79 auto& received_frame_info = |
78 auto& frame_info = received_frame_infos[received_frame_count]; | 80 received_frame_infos[received_frame_count]; |
79 frame_info.pixel_format = video_frame->format(); | 81 received_frame_info.pixel_format = (*frame_info)->pixel_format; |
80 frame_info.storage_type = video_frame->storage_type(); | 82 received_frame_info.storage_type = (*frame_info)->storage_type; |
81 frame_info.size = video_frame->natural_size(); | 83 received_frame_info.size = (*frame_info)->coded_size; |
82 frame_info.timestamp = video_frame->timestamp(); | 84 received_frame_info.timestamp = (*frame_info)->timestamp; |
83 received_frame_count += 1; | 85 received_frame_count += 1; |
84 if (received_frame_count == num_frames_to_receive) | 86 if (received_frame_count == num_frames_to_receive) |
85 wait_loop.Quit(); | 87 wait_loop.Quit(); |
86 })); | 88 })); |
87 | 89 |
88 fake_device_proxy_->Start(requestable_settings_, std::move(receiver_proxy)); | 90 fake_device_proxy_->Start(requestable_settings_, std::move(receiver_proxy)); |
89 | 91 |
90 wait_loop.Run(); | 92 wait_loop.Run(); |
91 | 93 |
92 base::TimeDelta previous_timestamp; | 94 base::TimeDelta previous_timestamp; |
93 for (int i = 0; i < num_frames_to_receive; i++) { | 95 for (int i = 0; i < num_frames_to_receive; i++) { |
94 auto& frame_info = received_frame_infos[i]; | 96 auto& frame_info = received_frame_infos[i]; |
95 // Service is expected to always output I420 | 97 // Service is expected to always output I420 |
96 EXPECT_EQ(media::PIXEL_FORMAT_I420, frame_info.pixel_format); | 98 EXPECT_EQ(media::PIXEL_FORMAT_I420, frame_info.pixel_format); |
97 // Service is expected to always use STORAGE_MOJO_SHARED_BUFFER | 99 // Service is expected to always use PIXEL_STORAGE_CPU |
98 EXPECT_EQ(media::VideoFrame::STORAGE_MOJO_SHARED_BUFFER, | 100 EXPECT_EQ(media::PIXEL_STORAGE_CPU, frame_info.storage_type); |
99 frame_info.storage_type); | 101 EXPECT_EQ(requestable_settings_.requested_format.frame_size, |
102 frame_info.size); | |
100 // Timestamps are expected to increase | 103 // Timestamps are expected to increase |
101 if (i > 0) | 104 if (i > 0) |
102 EXPECT_GT(frame_info.timestamp, previous_timestamp); | 105 EXPECT_GT(frame_info.timestamp, previous_timestamp); |
103 previous_timestamp = frame_info.timestamp; | 106 previous_timestamp = frame_info.timestamp; |
104 } | 107 } |
105 } | 108 } |
106 | 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 num_frames_arrived++; | |
130 if (num_frames_arrived >= kNumFramesToWaitFor) { | |
emircan
2017/04/19 18:16:48
++num_frames_arrived
chfremer
2017/04/19 23:03:38
Done.
| |
131 wait_loop.Quit(); | |
132 } | |
133 })); | |
134 | |
135 fake_device_proxy_->Start(requestable_settings_, std::move(receiver_proxy)); | |
136 wait_loop.Run(); | |
137 | |
138 ASSERT_LT(num_buffers_created, num_frames_arrived); | |
emircan
2017/04/19 18:16:48
ASSERT_LE(num_buffers_created, kMaxBufferPoolBuffe
chfremer
2017/04/19 23:03:38
Done.
| |
139 } | |
140 | |
107 } // namespace video_capture | 141 } // namespace video_capture |
OLD | NEW |