OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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" |
| 6 #include "base/bind_helpers.h" |
5 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
6 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/run_loop.h" |
7 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
8 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
9 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
10 #include "media/video/capture/fake_video_capture_device.h" | 14 #include "media/video/capture/fake_video_capture_device.h" |
11 #include "media/video/capture/video_capture_device.h" | 15 #include "media/video/capture/video_capture_device.h" |
12 #include "media/video/capture/video_capture_types.h" | 16 #include "media/video/capture/video_capture_types.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
15 | 19 |
16 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 62 |
59 namespace media { | 63 namespace media { |
60 | 64 |
61 class MockFrameObserver : public media::VideoCaptureDevice::EventHandler { | 65 class MockFrameObserver : public media::VideoCaptureDevice::EventHandler { |
62 public: | 66 public: |
63 MOCK_METHOD0(ReserveOutputBuffer, scoped_refptr<media::VideoFrame>()); | 67 MOCK_METHOD0(ReserveOutputBuffer, scoped_refptr<media::VideoFrame>()); |
64 MOCK_METHOD0(OnErr, void()); | 68 MOCK_METHOD0(OnErr, void()); |
65 MOCK_METHOD1(OnFrameInfo, void(const VideoCaptureCapability&)); | 69 MOCK_METHOD1(OnFrameInfo, void(const VideoCaptureCapability&)); |
66 MOCK_METHOD1(OnFrameInfoChanged, void(const VideoCaptureCapability&)); | 70 MOCK_METHOD1(OnFrameInfoChanged, void(const VideoCaptureCapability&)); |
67 | 71 |
68 explicit MockFrameObserver(base::WaitableEvent* wait_event) | 72 explicit MockFrameObserver( |
69 : wait_event_(wait_event) {} | 73 base::Closure frame_cb) |
| 74 : main_thread_(base::MessageLoopProxy::current()), |
| 75 frame_cb_(frame_cb) {} |
70 | 76 |
71 virtual void OnError() OVERRIDE { | 77 virtual void OnError() OVERRIDE { |
72 OnErr(); | 78 OnErr(); |
73 } | 79 } |
74 | 80 |
75 virtual void OnIncomingCapturedFrame( | 81 virtual void OnIncomingCapturedFrame( |
76 const uint8* data, | 82 const uint8* data, |
77 int length, | 83 int length, |
78 base::Time timestamp, | 84 base::Time timestamp, |
79 int rotation, | 85 int rotation, |
80 bool flip_vert, | 86 bool flip_vert, |
81 bool flip_horiz) OVERRIDE { | 87 bool flip_horiz) OVERRIDE { |
82 wait_event_->Signal(); | 88 main_thread_->PostTask(FROM_HERE, frame_cb_); |
83 } | 89 } |
84 | 90 |
85 virtual void OnIncomingCapturedVideoFrame( | 91 virtual void OnIncomingCapturedVideoFrame( |
86 const scoped_refptr<media::VideoFrame>& frame, | 92 const scoped_refptr<media::VideoFrame>& frame, |
87 base::Time timestamp) OVERRIDE { | 93 base::Time timestamp) OVERRIDE { |
88 wait_event_->Signal(); | 94 main_thread_->PostTask(FROM_HERE, frame_cb_); |
89 } | 95 } |
90 | 96 |
91 private: | 97 private: |
92 base::WaitableEvent* wait_event_; | 98 scoped_refptr<base::MessageLoopProxy> main_thread_; |
| 99 base::Closure frame_cb_; |
93 }; | 100 }; |
94 | 101 |
95 class VideoCaptureDeviceTest : public testing::Test { | 102 class VideoCaptureDeviceTest : public testing::Test { |
96 public: | 103 protected: |
97 VideoCaptureDeviceTest(): wait_event_(false, false) { } | 104 typedef media::VideoCaptureDevice::EventHandler EventHandler; |
98 | 105 |
99 void PostQuitTask() { | |
100 loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | |
101 loop_->Run(); | |
102 } | |
103 | |
104 protected: | |
105 virtual void SetUp() { | 106 virtual void SetUp() { |
106 frame_observer_.reset(new MockFrameObserver(&wait_event_)); | |
107 loop_.reset(new base::MessageLoopForUI()); | 107 loop_.reset(new base::MessageLoopForUI()); |
| 108 frame_observer_.reset(new MockFrameObserver(loop_->QuitClosure())); |
108 #if defined(OS_ANDROID) | 109 #if defined(OS_ANDROID) |
109 media::VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice( | 110 media::VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice( |
110 base::android::AttachCurrentThread()); | 111 base::android::AttachCurrentThread()); |
111 #endif | 112 #endif |
112 } | 113 } |
113 | 114 |
114 virtual void TearDown() { | 115 void WaitForCapturedFrame() { |
| 116 loop_->Run(); |
115 } | 117 } |
116 | 118 |
117 #if defined(OS_WIN) | 119 #if defined(OS_WIN) |
118 base::win::ScopedCOMInitializer initialize_com_; | 120 base::win::ScopedCOMInitializer initialize_com_; |
119 #endif | 121 #endif |
120 base::WaitableEvent wait_event_; | |
121 scoped_ptr<MockFrameObserver> frame_observer_; | 122 scoped_ptr<MockFrameObserver> frame_observer_; |
122 VideoCaptureDevice::Names names_; | 123 VideoCaptureDevice::Names names_; |
123 scoped_ptr<base::MessageLoop> loop_; | 124 scoped_ptr<base::MessageLoop> loop_; |
124 }; | 125 }; |
125 | 126 |
126 TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) { | 127 TEST_F(VideoCaptureDeviceTest, OpenInvalidDevice) { |
127 #if defined(OS_WIN) | 128 #if defined(OS_WIN) |
128 VideoCaptureDevice::Name::CaptureApiType api_type = | 129 VideoCaptureDevice::Name::CaptureApiType api_type = |
129 VideoCaptureDeviceMFWin::PlatformSupported() | 130 VideoCaptureDeviceMFWin::PlatformSupported() |
130 ? VideoCaptureDevice::Name::MEDIA_FOUNDATION | 131 ? VideoCaptureDevice::Name::MEDIA_FOUNDATION |
(...skipping 25 matching lines...) Expand all Loading... |
156 EXPECT_CALL(*frame_observer_, OnErr()) | 157 EXPECT_CALL(*frame_observer_, OnErr()) |
157 .Times(0); | 158 .Times(0); |
158 | 159 |
159 VideoCaptureCapability capture_format(640, | 160 VideoCaptureCapability capture_format(640, |
160 480, | 161 480, |
161 30, | 162 30, |
162 PIXEL_FORMAT_I420, | 163 PIXEL_FORMAT_I420, |
163 0, | 164 0, |
164 false, | 165 false, |
165 ConstantResolutionVideoCaptureDevice); | 166 ConstantResolutionVideoCaptureDevice); |
166 device->Allocate(capture_format, frame_observer_.get()); | 167 device->AllocateAndStart(capture_format, |
167 device->Start(); | 168 frame_observer_.PassAs<EventHandler>()); |
168 // Get captured video frames. | 169 // Get captured video frames. |
169 PostQuitTask(); | 170 loop_->Run(); |
170 EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout())); | |
171 EXPECT_EQ(rx_capability.width, 640); | 171 EXPECT_EQ(rx_capability.width, 640); |
172 EXPECT_EQ(rx_capability.height, 480); | 172 EXPECT_EQ(rx_capability.height, 480); |
173 device->Stop(); | 173 device->StopAndDeAllocate(); |
174 device->DeAllocate(); | |
175 } | 174 } |
176 | 175 |
177 TEST_F(VideoCaptureDeviceTest, Capture720p) { | 176 TEST_F(VideoCaptureDeviceTest, Capture720p) { |
178 VideoCaptureDevice::GetDeviceNames(&names_); | 177 VideoCaptureDevice::GetDeviceNames(&names_); |
179 if (!names_.size()) { | 178 if (!names_.size()) { |
180 DVLOG(1) << "No camera available. Exiting test."; | 179 DVLOG(1) << "No camera available. Exiting test."; |
181 return; | 180 return; |
182 } | 181 } |
183 | 182 |
184 scoped_ptr<VideoCaptureDevice> device( | 183 scoped_ptr<VideoCaptureDevice> device( |
185 VideoCaptureDevice::Create(names_.front())); | 184 VideoCaptureDevice::Create(names_.front())); |
186 ASSERT_FALSE(device.get() == NULL); | 185 ASSERT_FALSE(device.get() == NULL); |
187 | 186 |
188 // Get info about the new resolution. | 187 // Get info about the new resolution. |
189 // We don't care about the resulting resolution or frame rate as it might | 188 // We don't care about the resulting resolution or frame rate as it might |
190 // be different from one machine to the next. | 189 // be different from one machine to the next. |
191 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) | 190 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) |
192 .Times(1); | 191 .Times(1); |
193 | 192 |
194 EXPECT_CALL(*frame_observer_, OnErr()) | 193 EXPECT_CALL(*frame_observer_, OnErr()) |
195 .Times(0); | 194 .Times(0); |
196 | 195 |
197 VideoCaptureCapability capture_format(1280, | 196 VideoCaptureCapability capture_format(1280, |
198 720, | 197 720, |
199 30, | 198 30, |
200 PIXEL_FORMAT_I420, | 199 PIXEL_FORMAT_I420, |
201 0, | 200 0, |
202 false, | 201 false, |
203 ConstantResolutionVideoCaptureDevice); | 202 ConstantResolutionVideoCaptureDevice); |
204 device->Allocate(capture_format, frame_observer_.get()); | 203 device->AllocateAndStart(capture_format, |
205 device->Start(); | 204 frame_observer_.PassAs<EventHandler>()); |
206 // Get captured video frames. | 205 // Get captured video frames. |
207 PostQuitTask(); | 206 WaitForCapturedFrame(); |
208 EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout())); | 207 device->StopAndDeAllocate(); |
209 device->Stop(); | |
210 device->DeAllocate(); | |
211 } | 208 } |
212 | 209 |
213 TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateBadSize) { | 210 TEST_F(VideoCaptureDeviceTest, MAYBE_AllocateBadSize) { |
214 VideoCaptureDevice::GetDeviceNames(&names_); | 211 VideoCaptureDevice::GetDeviceNames(&names_); |
215 if (!names_.size()) { | 212 if (!names_.size()) { |
216 DVLOG(1) << "No camera available. Exiting test."; | 213 DVLOG(1) << "No camera available. Exiting test."; |
217 return; | 214 return; |
218 } | 215 } |
219 scoped_ptr<VideoCaptureDevice> device( | 216 scoped_ptr<VideoCaptureDevice> device( |
220 VideoCaptureDevice::Create(names_.front())); | 217 VideoCaptureDevice::Create(names_.front())); |
221 ASSERT_TRUE(device.get() != NULL); | 218 ASSERT_TRUE(device.get() != NULL); |
222 | 219 |
223 EXPECT_CALL(*frame_observer_, OnErr()) | 220 EXPECT_CALL(*frame_observer_, OnErr()) |
224 .Times(0); | 221 .Times(0); |
225 | 222 |
226 // Get info about the new resolution. | 223 // Get info about the new resolution. |
227 VideoCaptureCapability rx_capability; | 224 VideoCaptureCapability rx_capability; |
228 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) | 225 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) |
229 .Times(AtLeast(1)).WillOnce(SaveArg<0>(&rx_capability)); | 226 .Times(AtLeast(1)).WillOnce(SaveArg<0>(&rx_capability)); |
230 | 227 |
231 VideoCaptureCapability capture_format(637, | 228 VideoCaptureCapability capture_format(637, |
232 472, | 229 472, |
233 35, | 230 35, |
234 PIXEL_FORMAT_I420, | 231 PIXEL_FORMAT_I420, |
235 0, | 232 0, |
236 false, | 233 false, |
237 ConstantResolutionVideoCaptureDevice); | 234 ConstantResolutionVideoCaptureDevice); |
238 device->Allocate(capture_format, frame_observer_.get()); | 235 device->AllocateAndStart(capture_format, |
239 device->DeAllocate(); | 236 frame_observer_.PassAs<EventHandler>()); |
| 237 device->StopAndDeAllocate(); |
240 EXPECT_EQ(rx_capability.width, 640); | 238 EXPECT_EQ(rx_capability.width, 640); |
241 EXPECT_EQ(rx_capability.height, 480); | 239 EXPECT_EQ(rx_capability.height, 480); |
242 } | 240 } |
243 | 241 |
244 TEST_F(VideoCaptureDeviceTest, ReAllocateCamera) { | 242 TEST_F(VideoCaptureDeviceTest, ReAllocateCamera) { |
245 VideoCaptureDevice::GetDeviceNames(&names_); | 243 VideoCaptureDevice::GetDeviceNames(&names_); |
246 if (!names_.size()) { | 244 if (!names_.size()) { |
247 DVLOG(1) << "No camera available. Exiting test."; | 245 DVLOG(1) << "No camera available. Exiting test."; |
248 return; | 246 return; |
249 } | 247 } |
| 248 |
| 249 // First, do a number of very fast device start/stops. |
| 250 for (int i = 0; i <= 5; i++) { |
| 251 scoped_ptr<MockFrameObserver> frame_observer( |
| 252 new MockFrameObserver(base::Bind(&base::DoNothing))); |
| 253 scoped_ptr<VideoCaptureDevice> device( |
| 254 VideoCaptureDevice::Create(names_.front())); |
| 255 gfx::Size resolution; |
| 256 if (i % 2) { |
| 257 resolution = gfx::Size(640, 480); |
| 258 } else { |
| 259 resolution = gfx::Size(1280, 1024); |
| 260 } |
| 261 VideoCaptureCapability requested_format( |
| 262 resolution.width(), |
| 263 resolution.height(), |
| 264 30, |
| 265 PIXEL_FORMAT_I420, |
| 266 0, |
| 267 false, |
| 268 ConstantResolutionVideoCaptureDevice); |
| 269 |
| 270 // The device (if it is an async implementation) may or may not get as far |
| 271 // as the OnFrameInfo() step; we're intentionally not going to wait for it |
| 272 // to get that far. |
| 273 ON_CALL(*frame_observer, OnFrameInfo(_)); |
| 274 device->AllocateAndStart(requested_format, |
| 275 frame_observer.PassAs<EventHandler>()); |
| 276 device->StopAndDeAllocate(); |
| 277 } |
| 278 |
| 279 // Finally, do a device start and wait for it to finish. |
| 280 gfx::Size resolution; |
| 281 VideoCaptureCapability requested_format( |
| 282 320, |
| 283 240, |
| 284 30, |
| 285 PIXEL_FORMAT_I420, |
| 286 0, |
| 287 false, |
| 288 ConstantResolutionVideoCaptureDevice); |
| 289 |
| 290 base::RunLoop run_loop; |
| 291 scoped_ptr<MockFrameObserver> frame_observer( |
| 292 new MockFrameObserver(base::Bind(run_loop.QuitClosure()))); |
250 scoped_ptr<VideoCaptureDevice> device( | 293 scoped_ptr<VideoCaptureDevice> device( |
251 VideoCaptureDevice::Create(names_.front())); | 294 VideoCaptureDevice::Create(names_.front())); |
252 ASSERT_TRUE(device.get() != NULL); | |
253 EXPECT_CALL(*frame_observer_, OnErr()) | |
254 .Times(0); | |
255 // Get info about the new resolution. | |
256 VideoCaptureCapability rx_capability_1; | |
257 VideoCaptureCapability rx_capability_2; | |
258 VideoCaptureCapability capture_format_1(640, | |
259 480, | |
260 30, | |
261 PIXEL_FORMAT_I420, | |
262 0, | |
263 false, | |
264 ConstantResolutionVideoCaptureDevice); | |
265 VideoCaptureCapability capture_format_2(1280, | |
266 1024, | |
267 30, | |
268 PIXEL_FORMAT_I420, | |
269 0, | |
270 false, | |
271 ConstantResolutionVideoCaptureDevice); | |
272 VideoCaptureCapability capture_format_3(320, | |
273 240, | |
274 30, | |
275 PIXEL_FORMAT_I420, | |
276 0, | |
277 false, | |
278 ConstantResolutionVideoCaptureDevice); | |
279 | 295 |
280 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) | 296 // The device (if it is an async implementation) may or may not get as far |
281 .WillOnce(SaveArg<0>(&rx_capability_1)); | 297 // as the OnFrameInfo() step; we're intentionally not going to wait for it |
282 device->Allocate(capture_format_1, frame_observer_.get()); | 298 // to get that far. |
283 device->Start(); | 299 VideoCaptureCapability final_format; |
284 // Nothing shall happen. | 300 EXPECT_CALL(*frame_observer, OnFrameInfo(_)) |
285 device->Allocate(capture_format_2, frame_observer_.get()); | 301 .Times(1).WillOnce(SaveArg<0>(&final_format)); |
286 device->DeAllocate(); | 302 device->AllocateAndStart(requested_format, |
287 // Allocate new size 320, 240 | 303 frame_observer.PassAs<EventHandler>()); |
288 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) | 304 run_loop.Run(); // Waits for a frame. |
289 .WillOnce(SaveArg<0>(&rx_capability_2)); | 305 device->StopAndDeAllocate(); |
290 device->Allocate(capture_format_3, frame_observer_.get()); | 306 device.reset(); |
291 | 307 EXPECT_EQ(final_format.width, 320); |
292 device->Start(); | 308 EXPECT_EQ(final_format.height, 240); |
293 // Get captured video frames. | |
294 PostQuitTask(); | |
295 EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout())); | |
296 EXPECT_EQ(rx_capability_1.width, 640); | |
297 EXPECT_EQ(rx_capability_1.height, 480); | |
298 EXPECT_EQ(rx_capability_2.width, 320); | |
299 EXPECT_EQ(rx_capability_2.height, 240); | |
300 device->Stop(); | |
301 device->DeAllocate(); | |
302 } | 309 } |
303 | 310 |
304 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { | 311 TEST_F(VideoCaptureDeviceTest, DeAllocateCameraWhileRunning) { |
305 VideoCaptureDevice::GetDeviceNames(&names_); | 312 VideoCaptureDevice::GetDeviceNames(&names_); |
306 if (!names_.size()) { | 313 if (!names_.size()) { |
307 DVLOG(1) << "No camera available. Exiting test."; | 314 DVLOG(1) << "No camera available. Exiting test."; |
308 return; | 315 return; |
309 } | 316 } |
310 scoped_ptr<VideoCaptureDevice> device( | 317 scoped_ptr<VideoCaptureDevice> device( |
311 VideoCaptureDevice::Create(names_.front())); | 318 VideoCaptureDevice::Create(names_.front())); |
312 ASSERT_TRUE(device.get() != NULL); | 319 ASSERT_TRUE(device.get() != NULL); |
313 | 320 |
314 EXPECT_CALL(*frame_observer_, OnErr()) | 321 EXPECT_CALL(*frame_observer_, OnErr()) |
315 .Times(0); | 322 .Times(0); |
316 // Get info about the new resolution. | 323 // Get info about the new resolution. |
317 VideoCaptureCapability rx_capability; | 324 VideoCaptureCapability rx_capability; |
318 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) | 325 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) |
319 .WillOnce(SaveArg<0>(&rx_capability)); | 326 .WillOnce(SaveArg<0>(&rx_capability)); |
320 | 327 |
321 VideoCaptureCapability capture_format(640, | 328 VideoCaptureCapability capture_format(640, |
322 480, | 329 480, |
323 30, | 330 30, |
324 PIXEL_FORMAT_I420, | 331 PIXEL_FORMAT_I420, |
325 0, | 332 0, |
326 false, | 333 false, |
327 ConstantResolutionVideoCaptureDevice); | 334 ConstantResolutionVideoCaptureDevice); |
328 device->Allocate(capture_format, frame_observer_.get()); | 335 device->AllocateAndStart(capture_format, |
329 | 336 frame_observer_.PassAs<EventHandler>()); |
330 device->Start(); | |
331 // Get captured video frames. | 337 // Get captured video frames. |
332 PostQuitTask(); | 338 WaitForCapturedFrame(); |
333 EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout())); | |
334 EXPECT_EQ(rx_capability.width, 640); | 339 EXPECT_EQ(rx_capability.width, 640); |
335 EXPECT_EQ(rx_capability.height, 480); | 340 EXPECT_EQ(rx_capability.height, 480); |
336 EXPECT_EQ(rx_capability.frame_rate, 30); | 341 EXPECT_EQ(rx_capability.frame_rate, 30); |
337 device->DeAllocate(); | 342 device->StopAndDeAllocate(); |
338 } | 343 } |
339 | 344 |
340 TEST_F(VideoCaptureDeviceTest, FakeCapture) { | 345 TEST_F(VideoCaptureDeviceTest, FakeCapture) { |
341 VideoCaptureDevice::Names names; | 346 VideoCaptureDevice::Names names; |
342 | 347 |
343 FakeVideoCaptureDevice::GetDeviceNames(&names); | 348 FakeVideoCaptureDevice::GetDeviceNames(&names); |
344 | 349 |
345 ASSERT_GT(static_cast<int>(names.size()), 0); | 350 ASSERT_GT(static_cast<int>(names.size()), 0); |
346 | 351 |
347 scoped_ptr<VideoCaptureDevice> device( | 352 scoped_ptr<VideoCaptureDevice> device( |
348 FakeVideoCaptureDevice::Create(names.front())); | 353 FakeVideoCaptureDevice::Create(names.front())); |
349 ASSERT_TRUE(device.get() != NULL); | 354 ASSERT_TRUE(device.get() != NULL); |
350 | 355 |
351 // Get info about the new resolution. | 356 // Get info about the new resolution. |
352 VideoCaptureCapability rx_capability; | 357 VideoCaptureCapability rx_capability; |
353 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) | 358 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) |
354 .Times(1).WillOnce(SaveArg<0>(&rx_capability)); | 359 .Times(1).WillOnce(SaveArg<0>(&rx_capability)); |
355 | 360 |
356 EXPECT_CALL(*frame_observer_, OnErr()) | 361 EXPECT_CALL(*frame_observer_, OnErr()) |
357 .Times(0); | 362 .Times(0); |
358 | 363 |
359 VideoCaptureCapability capture_format(640, | 364 VideoCaptureCapability capture_format(640, |
360 480, | 365 480, |
361 30, | 366 30, |
362 PIXEL_FORMAT_I420, | 367 PIXEL_FORMAT_I420, |
363 0, | 368 0, |
364 false, | 369 false, |
365 ConstantResolutionVideoCaptureDevice); | 370 ConstantResolutionVideoCaptureDevice); |
366 device->Allocate(capture_format, frame_observer_.get()); | 371 device->AllocateAndStart(capture_format, |
367 | 372 frame_observer_.PassAs<EventHandler>()); |
368 device->Start(); | 373 WaitForCapturedFrame(); |
369 EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout())); | |
370 EXPECT_EQ(rx_capability.width, 640); | 374 EXPECT_EQ(rx_capability.width, 640); |
371 EXPECT_EQ(rx_capability.height, 480); | 375 EXPECT_EQ(rx_capability.height, 480); |
372 EXPECT_EQ(rx_capability.frame_rate, 30); | 376 EXPECT_EQ(rx_capability.frame_rate, 30); |
373 device->Stop(); | 377 device->StopAndDeAllocate(); |
374 device->DeAllocate(); | |
375 } | 378 } |
376 | 379 |
377 // Start the camera in 720p to capture MJPEG instead of a raw format. | 380 // Start the camera in 720p to capture MJPEG instead of a raw format. |
378 TEST_F(VideoCaptureDeviceTest, MAYBE_CaptureMjpeg) { | 381 TEST_F(VideoCaptureDeviceTest, MAYBE_CaptureMjpeg) { |
379 VideoCaptureDevice::GetDeviceNames(&names_); | 382 VideoCaptureDevice::GetDeviceNames(&names_); |
380 if (!names_.size()) { | 383 if (!names_.size()) { |
381 DVLOG(1) << "No camera available. Exiting test."; | 384 DVLOG(1) << "No camera available. Exiting test."; |
382 return; | 385 return; |
383 } | 386 } |
384 scoped_ptr<VideoCaptureDevice> device( | 387 scoped_ptr<VideoCaptureDevice> device( |
385 VideoCaptureDevice::Create(names_.front())); | 388 VideoCaptureDevice::Create(names_.front())); |
386 ASSERT_TRUE(device.get() != NULL); | 389 ASSERT_TRUE(device.get() != NULL); |
387 | 390 |
388 EXPECT_CALL(*frame_observer_, OnErr()) | 391 EXPECT_CALL(*frame_observer_, OnErr()) |
389 .Times(0); | 392 .Times(0); |
390 // Verify we get MJPEG from the device. Not all devices can capture 1280x720 | 393 // Verify we get MJPEG from the device. Not all devices can capture 1280x720 |
391 // @ 30 fps, so we don't care about the exact resolution we get. | 394 // @ 30 fps, so we don't care about the exact resolution we get. |
392 VideoCaptureCapability rx_capability; | 395 VideoCaptureCapability rx_capability; |
393 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) | 396 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) |
394 .WillOnce(SaveArg<0>(&rx_capability)); | 397 .WillOnce(SaveArg<0>(&rx_capability)); |
395 | 398 |
396 VideoCaptureCapability capture_format(1280, | 399 VideoCaptureCapability capture_format(1280, |
397 720, | 400 720, |
398 30, | 401 30, |
399 PIXEL_FORMAT_MJPEG, | 402 PIXEL_FORMAT_MJPEG, |
400 0, | 403 0, |
401 false, | 404 false, |
402 ConstantResolutionVideoCaptureDevice); | 405 ConstantResolutionVideoCaptureDevice); |
403 device->Allocate(capture_format, frame_observer_.get()); | 406 device->AllocateAndStart(capture_format, |
404 | 407 frame_observer_.PassAs<EventHandler>()); |
405 device->Start(); | |
406 // Get captured video frames. | 408 // Get captured video frames. |
407 PostQuitTask(); | 409 WaitForCapturedFrame(); |
408 EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_max_timeout())); | |
409 EXPECT_EQ(rx_capability.color, PIXEL_FORMAT_MJPEG); | 410 EXPECT_EQ(rx_capability.color, PIXEL_FORMAT_MJPEG); |
410 device->DeAllocate(); | 411 device->StopAndDeAllocate(); |
411 } | 412 } |
412 | 413 |
413 TEST_F(VideoCaptureDeviceTest, FakeCaptureVariableResolution) { | 414 TEST_F(VideoCaptureDeviceTest, FakeCaptureVariableResolution) { |
414 VideoCaptureDevice::Names names; | 415 VideoCaptureDevice::Names names; |
415 | 416 |
416 FakeVideoCaptureDevice::GetDeviceNames(&names); | 417 FakeVideoCaptureDevice::GetDeviceNames(&names); |
417 media::VideoCaptureCapability capture_format; | 418 media::VideoCaptureCapability capture_format; |
418 capture_format.width = 640; | 419 capture_format.width = 640; |
419 capture_format.height = 480; | 420 capture_format.height = 480; |
420 capture_format.frame_rate = 30; | 421 capture_format.frame_rate = 30; |
421 capture_format.frame_size_type = media::VariableResolutionVideoCaptureDevice; | 422 capture_format.frame_size_type = media::VariableResolutionVideoCaptureDevice; |
422 | 423 |
423 ASSERT_GT(static_cast<int>(names.size()), 0); | 424 ASSERT_GT(static_cast<int>(names.size()), 0); |
424 | 425 |
425 scoped_ptr<VideoCaptureDevice> device( | 426 scoped_ptr<VideoCaptureDevice> device( |
426 FakeVideoCaptureDevice::Create(names.front())); | 427 FakeVideoCaptureDevice::Create(names.front())); |
427 ASSERT_TRUE(device.get() != NULL); | 428 ASSERT_TRUE(device.get() != NULL); |
428 | 429 |
429 // Get info about the new resolution. | 430 // Get info about the new resolution. |
430 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) | 431 EXPECT_CALL(*frame_observer_, OnFrameInfo(_)) |
431 .Times(1); | 432 .Times(1); |
432 | 433 |
433 EXPECT_CALL(*frame_observer_, OnErr()) | 434 EXPECT_CALL(*frame_observer_, OnErr()) |
434 .Times(0); | 435 .Times(0); |
| 436 int action_count = 200; |
| 437 EXPECT_CALL(*frame_observer_, OnFrameInfoChanged(_)) |
| 438 .Times(AtLeast(action_count / 30)); |
435 | 439 |
436 device->Allocate(capture_format, frame_observer_.get()); | 440 device->AllocateAndStart(capture_format, |
| 441 frame_observer_.PassAs<EventHandler>()); |
437 | 442 |
438 // The amount of times the OnFrameInfoChanged gets called depends on how often | 443 // The amount of times the OnFrameInfoChanged gets called depends on how often |
439 // FakeDevice is supposed to change and what is its actual frame rate. | 444 // FakeDevice is supposed to change and what is its actual frame rate. |
440 // We set TimeWait to 200 action timeouts and this should be enough for at | 445 // We set TimeWait to 200 action timeouts and this should be enough for at |
441 // least action_count/kFakeCaptureCapabilityChangePeriod calls. | 446 // least action_count/kFakeCaptureCapabilityChangePeriod calls. |
442 int action_count = 200; | |
443 EXPECT_CALL(*frame_observer_, OnFrameInfoChanged(_)) | |
444 .Times(AtLeast(action_count / 30)); | |
445 device->Start(); | |
446 for (int i = 0; i < action_count; ++i) { | 447 for (int i = 0; i < action_count; ++i) { |
447 EXPECT_TRUE(wait_event_.TimedWait(TestTimeouts::action_timeout())); | 448 WaitForCapturedFrame(); |
448 } | 449 } |
449 device->Stop(); | 450 device->StopAndDeAllocate(); |
450 device->DeAllocate(); | |
451 } | 451 } |
452 | 452 |
453 }; // namespace media | 453 }; // namespace media |
OLD | NEW |