OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "media/video/capture/fake_video_capture_device.h" | 10 #include "media/video/capture/fake_video_capture_device.h" |
11 #include "media/video/capture/fake_video_capture_device_factory.h" | 11 #include "media/video/capture/fake_video_capture_device_factory.h" |
12 #include "media/video/capture/video_capture_device.h" | 12 #include "media/video/capture/video_capture_device.h" |
13 #include "media/video/capture/video_capture_types.h" | 13 #include "media/video/capture/video_capture_types.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 using ::testing::_; | 17 using ::testing::_; |
18 using ::testing::SaveArg; | 18 using ::testing::SaveArg; |
19 | 19 |
20 namespace media { | 20 namespace media { |
21 | 21 |
22 class MockClient : public media::VideoCaptureDevice::Client { | 22 namespace { |
| 23 |
| 24 class MockClient : public VideoCaptureDevice::Client { |
23 public: | 25 public: |
24 MOCK_METHOD2(ReserveOutputBuffer, | 26 MOCK_METHOD2(ReserveOutputBuffer, |
25 scoped_refptr<Buffer>(media::VideoFrame::Format format, | 27 scoped_refptr<Buffer>(VideoFrame::Format format, |
26 const gfx::Size& dimensions)); | 28 const gfx::Size& dimensions)); |
27 MOCK_METHOD0(OnErr, void()); | 29 MOCK_METHOD0(OnErr, void()); |
28 | 30 |
29 explicit MockClient(base::Callback<void(const VideoCaptureFormat&)> frame_cb) | 31 explicit MockClient(base::Callback<void(const VideoCaptureFormat&)> frame_cb) |
30 : main_thread_(base::MessageLoopProxy::current()), frame_cb_(frame_cb) {} | 32 : main_thread_(base::MessageLoopProxy::current()), frame_cb_(frame_cb) {} |
31 | 33 |
32 virtual void OnError(const std::string& error_message) override { | 34 void OnError(const std::string& error_message) override { |
33 OnErr(); | 35 OnErr(); |
34 } | 36 } |
35 | 37 |
36 virtual void OnIncomingCapturedData(const uint8* data, | 38 void OnIncomingCapturedData(const uint8* data, |
37 int length, | 39 int length, |
38 const VideoCaptureFormat& format, | 40 const VideoCaptureFormat& format, |
39 int rotation, | 41 int rotation, |
40 base::TimeTicks timestamp) override { | 42 base::TimeTicks timestamp) override { |
41 main_thread_->PostTask(FROM_HERE, base::Bind(frame_cb_, format)); | 43 main_thread_->PostTask(FROM_HERE, base::Bind(frame_cb_, format)); |
42 } | 44 } |
43 | 45 |
44 virtual void OnIncomingCapturedVideoFrame( | 46 void OnIncomingCapturedVideoFrame(const scoped_refptr<Buffer>& buffer, |
45 const scoped_refptr<Buffer>& buffer, | 47 const VideoCaptureFormat& buffer_format, |
46 const media::VideoCaptureFormat& buffer_format, | 48 const scoped_refptr<VideoFrame>& frame, |
47 const scoped_refptr<media::VideoFrame>& frame, | 49 base::TimeTicks timestamp) override { |
48 base::TimeTicks timestamp) override { | |
49 NOTREACHED(); | 50 NOTREACHED(); |
50 } | 51 } |
51 | 52 |
52 private: | 53 private: |
53 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; | 54 scoped_refptr<base::SingleThreadTaskRunner> main_thread_; |
54 base::Callback<void(const VideoCaptureFormat&)> frame_cb_; | 55 base::Callback<void(const VideoCaptureFormat&)> frame_cb_; |
55 }; | 56 }; |
56 | 57 |
57 class DeviceEnumerationListener : | 58 class DeviceEnumerationListener : |
58 public base::RefCounted<DeviceEnumerationListener> { | 59 public base::RefCounted<DeviceEnumerationListener> { |
59 public: | 60 public: |
60 MOCK_METHOD1(OnEnumeratedDevicesCallbackPtr, | 61 MOCK_METHOD1(OnEnumeratedDevicesCallbackPtr, |
61 void(media::VideoCaptureDevice::Names* names)); | 62 void(VideoCaptureDevice::Names* names)); |
62 // GMock doesn't support move-only arguments, so we use this forward method. | 63 // GMock doesn't support move-only arguments, so we use this forward method. |
63 void OnEnumeratedDevicesCallback( | 64 void OnEnumeratedDevicesCallback( |
64 scoped_ptr<media::VideoCaptureDevice::Names> names) { | 65 scoped_ptr<VideoCaptureDevice::Names> names) { |
65 OnEnumeratedDevicesCallbackPtr(names.release()); | 66 OnEnumeratedDevicesCallbackPtr(names.release()); |
66 } | 67 } |
67 | 68 |
68 private: | 69 private: |
69 friend class base::RefCounted<DeviceEnumerationListener>; | 70 friend class base::RefCounted<DeviceEnumerationListener>; |
70 virtual ~DeviceEnumerationListener() {} | 71 virtual ~DeviceEnumerationListener() {} |
71 }; | 72 }; |
72 | 73 |
| 74 } // namespace |
| 75 |
73 class FakeVideoCaptureDeviceTest : public testing::Test { | 76 class FakeVideoCaptureDeviceTest : public testing::Test { |
74 protected: | 77 protected: |
75 typedef media::VideoCaptureDevice::Client Client; | 78 typedef VideoCaptureDevice::Client Client; |
76 | 79 |
77 FakeVideoCaptureDeviceTest() | 80 FakeVideoCaptureDeviceTest() |
78 : loop_(new base::MessageLoop()), | 81 : loop_(new base::MessageLoop()), |
79 client_(new MockClient( | 82 client_(new MockClient( |
80 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured, | 83 base::Bind(&FakeVideoCaptureDeviceTest::OnFrameCaptured, |
81 base::Unretained(this)))), | 84 base::Unretained(this)))), |
82 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) { | 85 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) { |
83 device_enumeration_listener_ = new DeviceEnumerationListener(); | 86 device_enumeration_listener_ = new DeviceEnumerationListener(); |
84 } | 87 } |
85 | 88 |
86 void SetUp() override {} | |
87 | |
88 void OnFrameCaptured(const VideoCaptureFormat& format) { | 89 void OnFrameCaptured(const VideoCaptureFormat& format) { |
89 last_format_ = format; | 90 last_format_ = format; |
90 run_loop_->QuitClosure().Run(); | 91 run_loop_->QuitClosure().Run(); |
91 } | 92 } |
92 | 93 |
93 void WaitForCapturedFrame() { | 94 void WaitForCapturedFrame() { |
94 run_loop_.reset(new base::RunLoop()); | 95 run_loop_.reset(new base::RunLoop()); |
95 run_loop_->Run(); | 96 run_loop_->Run(); |
96 } | 97 } |
97 | 98 |
98 scoped_ptr<media::VideoCaptureDevice::Names> EnumerateDevices() { | 99 scoped_ptr<VideoCaptureDevice::Names> EnumerateDevices() { |
99 media::VideoCaptureDevice::Names* names; | 100 VideoCaptureDevice::Names* names; |
100 EXPECT_CALL(*device_enumeration_listener_.get(), | 101 EXPECT_CALL(*device_enumeration_listener_.get(), |
101 OnEnumeratedDevicesCallbackPtr(_)).WillOnce(SaveArg<0>(&names)); | 102 OnEnumeratedDevicesCallbackPtr(_)).WillOnce(SaveArg<0>(&names)); |
102 | 103 |
103 video_capture_device_factory_->EnumerateDeviceNames( | 104 video_capture_device_factory_->EnumerateDeviceNames( |
104 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback, | 105 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback, |
105 device_enumeration_listener_)); | 106 device_enumeration_listener_)); |
106 base::MessageLoop::current()->RunUntilIdle(); | 107 base::MessageLoop::current()->RunUntilIdle(); |
107 return scoped_ptr<media::VideoCaptureDevice::Names>(names); | 108 return scoped_ptr<VideoCaptureDevice::Names>(names); |
108 } | 109 } |
109 | 110 |
110 const VideoCaptureFormat& last_format() const { return last_format_; } | 111 const VideoCaptureFormat& last_format() const { return last_format_; } |
111 | 112 |
112 VideoCaptureDevice::Names names_; | 113 VideoCaptureDevice::Names names_; |
113 scoped_ptr<base::MessageLoop> loop_; | 114 scoped_ptr<base::MessageLoop> loop_; |
114 scoped_ptr<base::RunLoop> run_loop_; | 115 scoped_ptr<base::RunLoop> run_loop_; |
115 scoped_ptr<MockClient> client_; | 116 scoped_ptr<MockClient> client_; |
116 scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; | 117 scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; |
117 VideoCaptureFormat last_format_; | 118 VideoCaptureFormat last_format_; |
118 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_; | 119 scoped_ptr<VideoCaptureDeviceFactory> video_capture_device_factory_; |
119 }; | 120 }; |
120 | 121 |
121 TEST_F(FakeVideoCaptureDeviceTest, Capture) { | 122 TEST_F(FakeVideoCaptureDeviceTest, Capture) { |
122 scoped_ptr<media::VideoCaptureDevice::Names> names(EnumerateDevices()); | 123 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); |
123 | 124 |
124 ASSERT_GT(static_cast<int>(names->size()), 0); | 125 ASSERT_FALSE(names->empty()); |
125 | 126 |
126 scoped_ptr<VideoCaptureDevice> device( | 127 scoped_ptr<VideoCaptureDevice> device( |
127 video_capture_device_factory_->Create(names->front())); | 128 video_capture_device_factory_->Create(names->front())); |
128 ASSERT_TRUE(device); | 129 ASSERT_TRUE(device); |
129 | 130 |
130 EXPECT_CALL(*client_, OnErr()).Times(0); | 131 EXPECT_CALL(*client_, OnErr()).Times(0); |
131 | 132 |
132 VideoCaptureParams capture_params; | 133 VideoCaptureParams capture_params; |
133 capture_params.requested_format.frame_size.SetSize(640, 480); | 134 capture_params.requested_format.frame_size.SetSize(640, 480); |
134 capture_params.requested_format.frame_rate = 30; | 135 capture_params.requested_format.frame_rate = 30; |
135 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 136 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
136 device->AllocateAndStart(capture_params, client_.Pass()); | 137 device->AllocateAndStart(capture_params, client_.Pass()); |
137 WaitForCapturedFrame(); | 138 WaitForCapturedFrame(); |
138 EXPECT_EQ(last_format().frame_size.width(), 640); | 139 EXPECT_EQ(last_format().frame_size.width(), 640); |
139 EXPECT_EQ(last_format().frame_size.height(), 480); | 140 EXPECT_EQ(last_format().frame_size.height(), 480); |
140 EXPECT_EQ(last_format().frame_rate, 30); | 141 EXPECT_EQ(last_format().frame_rate, 30); |
141 device->StopAndDeAllocate(); | 142 device->StopAndDeAllocate(); |
142 } | 143 } |
143 | 144 |
144 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { | 145 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { |
145 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); | 146 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); |
146 | 147 |
147 VideoCaptureFormats supported_formats; | 148 VideoCaptureFormats supported_formats; |
148 VideoCaptureDevice::Names::iterator names_iterator; | |
149 | 149 |
150 for (names_iterator = names->begin(); names_iterator != names->end(); | 150 for (const auto& names_iterator : *names) { |
151 ++names_iterator) { | |
152 video_capture_device_factory_->GetDeviceSupportedFormats( | 151 video_capture_device_factory_->GetDeviceSupportedFormats( |
153 *names_iterator, &supported_formats); | 152 names_iterator, &supported_formats); |
154 EXPECT_EQ(supported_formats.size(), 4u); | 153 ASSERT_EQ(supported_formats.size(), 4u); |
155 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); | 154 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); |
156 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); | 155 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); |
157 EXPECT_EQ(supported_formats[0].pixel_format, media::PIXEL_FORMAT_I420); | 156 EXPECT_EQ(supported_formats[0].pixel_format, PIXEL_FORMAT_I420); |
158 EXPECT_GE(supported_formats[0].frame_rate, 20); | 157 EXPECT_GE(supported_formats[0].frame_rate, 20); |
159 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); | 158 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); |
160 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); | 159 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); |
161 EXPECT_EQ(supported_formats[1].pixel_format, media::PIXEL_FORMAT_I420); | 160 EXPECT_EQ(supported_formats[1].pixel_format, PIXEL_FORMAT_I420); |
162 EXPECT_GE(supported_formats[1].frame_rate, 20); | 161 EXPECT_GE(supported_formats[1].frame_rate, 20); |
163 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); | 162 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); |
164 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); | 163 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); |
165 EXPECT_EQ(supported_formats[2].pixel_format, media::PIXEL_FORMAT_I420); | 164 EXPECT_EQ(supported_formats[2].pixel_format, PIXEL_FORMAT_I420); |
166 EXPECT_GE(supported_formats[2].frame_rate, 20); | 165 EXPECT_GE(supported_formats[2].frame_rate, 20); |
167 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); | 166 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); |
168 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); | 167 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); |
169 EXPECT_EQ(supported_formats[3].pixel_format, media::PIXEL_FORMAT_I420); | 168 EXPECT_EQ(supported_formats[3].pixel_format, PIXEL_FORMAT_I420); |
170 EXPECT_GE(supported_formats[3].frame_rate, 20); | 169 EXPECT_GE(supported_formats[3].frame_rate, 20); |
171 } | 170 } |
172 } | 171 } |
173 | 172 |
174 // Disabled, http://crbug.com/407061 . | 173 // Disabled, http://crbug.com/407061 . |
175 TEST_F(FakeVideoCaptureDeviceTest, DISABLED_CaptureVariableResolution) { | 174 TEST_F(FakeVideoCaptureDeviceTest, DISABLED_CaptureVariableResolution) { |
176 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); | 175 scoped_ptr<VideoCaptureDevice::Names> names(EnumerateDevices()); |
177 | 176 |
178 VideoCaptureParams capture_params; | 177 VideoCaptureParams capture_params; |
179 capture_params.requested_format.frame_size.SetSize(640, 480); | 178 capture_params.requested_format.frame_size.SetSize(640, 480); |
180 capture_params.requested_format.frame_rate = 30; | 179 capture_params.requested_format.frame_rate = 30; |
181 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; | 180 capture_params.requested_format.pixel_format = PIXEL_FORMAT_I420; |
182 capture_params.resolution_change_policy = | 181 capture_params.resolution_change_policy = |
183 RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT; | 182 RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT; |
184 | 183 |
185 ASSERT_GT(static_cast<int>(names->size()), 0); | 184 ASSERT_FALSE(names->empty()); |
186 | 185 |
187 scoped_ptr<VideoCaptureDevice> device( | 186 scoped_ptr<VideoCaptureDevice> device( |
188 video_capture_device_factory_->Create(names->front())); | 187 video_capture_device_factory_->Create(names->front())); |
189 ASSERT_TRUE(device); | 188 ASSERT_TRUE(device); |
190 | 189 |
191 // Configure the FakeVideoCaptureDevice to use all its formats as roster. | 190 // Configure the FakeVideoCaptureDevice to use all its formats as roster. |
192 VideoCaptureFormats formats; | 191 VideoCaptureFormats formats; |
193 video_capture_device_factory_->GetDeviceSupportedFormats(names->front(), | 192 video_capture_device_factory_->GetDeviceSupportedFormats(names->front(), |
194 &formats); | 193 &formats); |
195 static_cast<FakeVideoCaptureDevice*>(device.get())-> | 194 static_cast<FakeVideoCaptureDevice*>(device.get())-> |
196 PopulateVariableFormatsRoster(formats); | 195 PopulateVariableFormatsRoster(formats); |
197 | 196 |
198 EXPECT_CALL(*client_, OnErr()) | 197 EXPECT_CALL(*client_, OnErr()).Times(0); |
199 .Times(0); | |
200 int action_count = 200; | 198 int action_count = 200; |
201 | 199 |
202 device->AllocateAndStart(capture_params, client_.Pass()); | 200 device->AllocateAndStart(capture_params, client_.Pass()); |
203 | 201 |
204 // We set TimeWait to 200 action timeouts and this should be enough for at | 202 // We set TimeWait to 200 action timeouts and this should be enough for at |
205 // least action_count/kFakeCaptureCapabilityChangePeriod calls. | 203 // least action_count/kFakeCaptureCapabilityChangePeriod calls. |
206 for (int i = 0; i < action_count; ++i) { | 204 for (int i = 0; i < action_count; ++i) { |
207 WaitForCapturedFrame(); | 205 WaitForCapturedFrame(); |
208 } | 206 } |
209 device->StopAndDeAllocate(); | 207 device->StopAndDeAllocate(); |
210 } | 208 } |
211 | 209 |
212 }; // namespace media | 210 }; // namespace media |
OLD | NEW |