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 "media/capture/video/fake_video_capture_device.h" | 5 #include "media/capture/video/fake_video_capture_device.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 170 |
171 mojom::PhotoCapabilitiesPtr capabilities_; | 171 mojom::PhotoCapabilitiesPtr capabilities_; |
172 }; | 172 }; |
173 | 173 |
174 } // namespace | 174 } // namespace |
175 | 175 |
176 class FakeVideoCaptureDeviceBase : public ::testing::Test { | 176 class FakeVideoCaptureDeviceBase : public ::testing::Test { |
177 protected: | 177 protected: |
178 FakeVideoCaptureDeviceBase() | 178 FakeVideoCaptureDeviceBase() |
179 : loop_(new base::MessageLoop()), | 179 : loop_(new base::MessageLoop()), |
180 client_(new MockClient( | |
181 base::Bind(&FakeVideoCaptureDeviceBase::OnFrameCaptured, | |
182 base::Unretained(this)))), | |
183 device_enumeration_listener_(new DeviceEnumerationListener()), | 180 device_enumeration_listener_(new DeviceEnumerationListener()), |
184 image_capture_client_(new ImageCaptureClient()), | 181 image_capture_client_(new ImageCaptureClient()), |
185 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {} | 182 video_capture_device_factory_(new FakeVideoCaptureDeviceFactory()) {} |
186 | 183 |
187 void SetUp() override { EXPECT_CALL(*client_, OnError(_, _)).Times(0); } | 184 std::unique_ptr<MockClient> CreateClient() { |
| 185 std::unique_ptr<MockClient> client = std::unique_ptr<MockClient>( |
| 186 new MockClient(base::Bind(&FakeVideoCaptureDeviceBase::OnFrameCaptured, |
| 187 base::Unretained(this)))); |
| 188 EXPECT_CALL(*client, OnError(_, _)).Times(0); |
| 189 return client; |
| 190 } |
188 | 191 |
189 void OnFrameCaptured(const VideoCaptureFormat& format) { | 192 void OnFrameCaptured(const VideoCaptureFormat& format) { |
190 last_format_ = format; | 193 last_format_ = format; |
191 run_loop_->QuitClosure().Run(); | 194 run_loop_->QuitClosure().Run(); |
192 } | 195 } |
193 | 196 |
194 void WaitForCapturedFrame() { | 197 void WaitForCapturedFrame() { |
195 run_loop_.reset(new base::RunLoop()); | 198 run_loop_.reset(new base::RunLoop()); |
196 run_loop_->Run(); | 199 run_loop_->Run(); |
197 } | 200 } |
198 | 201 |
199 std::unique_ptr<VideoCaptureDeviceDescriptors> EnumerateDevices() { | 202 std::unique_ptr<VideoCaptureDeviceDescriptors> EnumerateDevices() { |
200 VideoCaptureDeviceDescriptors* descriptors; | 203 VideoCaptureDeviceDescriptors* descriptors; |
201 EXPECT_CALL(*device_enumeration_listener_.get(), | 204 EXPECT_CALL(*device_enumeration_listener_.get(), |
202 OnEnumeratedDevicesCallbackPtr(_)) | 205 OnEnumeratedDevicesCallbackPtr(_)) |
203 .WillOnce(SaveArg<0>(&descriptors)); | 206 .WillOnce(SaveArg<0>(&descriptors)); |
204 | 207 |
205 video_capture_device_factory_->EnumerateDeviceDescriptors( | 208 video_capture_device_factory_->EnumerateDeviceDescriptors( |
206 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback, | 209 base::Bind(&DeviceEnumerationListener::OnEnumeratedDevicesCallback, |
207 device_enumeration_listener_)); | 210 device_enumeration_listener_)); |
208 base::RunLoop().RunUntilIdle(); | 211 base::RunLoop().RunUntilIdle(); |
209 return std::unique_ptr<VideoCaptureDeviceDescriptors>(descriptors); | 212 return std::unique_ptr<VideoCaptureDeviceDescriptors>(descriptors); |
210 } | 213 } |
211 | 214 |
212 const VideoCaptureFormat& last_format() const { return last_format_; } | 215 const VideoCaptureFormat& last_format() const { return last_format_; } |
213 | 216 |
214 VideoCaptureDeviceDescriptors descriptors_; | 217 VideoCaptureDeviceDescriptors descriptors_; |
215 const std::unique_ptr<base::MessageLoop> loop_; | 218 const std::unique_ptr<base::MessageLoop> loop_; |
216 std::unique_ptr<base::RunLoop> run_loop_; | 219 std::unique_ptr<base::RunLoop> run_loop_; |
217 std::unique_ptr<MockClient> client_; | |
218 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; | 220 const scoped_refptr<DeviceEnumerationListener> device_enumeration_listener_; |
219 const scoped_refptr<ImageCaptureClient> image_capture_client_; | 221 const scoped_refptr<ImageCaptureClient> image_capture_client_; |
220 VideoCaptureFormat last_format_; | 222 VideoCaptureFormat last_format_; |
221 const std::unique_ptr<VideoCaptureDeviceFactory> | 223 const std::unique_ptr<VideoCaptureDeviceFactory> |
222 video_capture_device_factory_; | 224 video_capture_device_factory_; |
223 }; | 225 }; |
224 | 226 |
225 class FakeVideoCaptureDeviceTest | 227 class FakeVideoCaptureDeviceTest |
226 : public FakeVideoCaptureDeviceBase, | 228 : public FakeVideoCaptureDeviceBase, |
227 public ::testing::WithParamInterface< | 229 public ::testing::WithParamInterface< |
(...skipping 15 matching lines...) Expand all Loading... |
243 EnumerateDevices()); | 245 EnumerateDevices()); |
244 ASSERT_FALSE(descriptors->empty()); | 246 ASSERT_FALSE(descriptors->empty()); |
245 | 247 |
246 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | 248 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( |
247 testing::get<0>(GetParam()), testing::get<1>(GetParam()))); | 249 testing::get<0>(GetParam()), testing::get<1>(GetParam()))); |
248 ASSERT_TRUE(device); | 250 ASSERT_TRUE(device); |
249 | 251 |
250 VideoCaptureParams capture_params; | 252 VideoCaptureParams capture_params; |
251 capture_params.requested_format.frame_size.SetSize(640, 480); | 253 capture_params.requested_format.frame_size.SetSize(640, 480); |
252 capture_params.requested_format.frame_rate = testing::get<1>(GetParam()); | 254 capture_params.requested_format.frame_rate = testing::get<1>(GetParam()); |
253 device->AllocateAndStart(capture_params, std::move(client_)); | 255 device->AllocateAndStart(capture_params, CreateClient()); |
254 | 256 |
255 WaitForCapturedFrame(); | 257 WaitForCapturedFrame(); |
256 EXPECT_EQ(last_format().frame_size.width(), 640); | 258 EXPECT_EQ(last_format().frame_size.width(), 640); |
257 EXPECT_EQ(last_format().frame_size.height(), 480); | 259 EXPECT_EQ(last_format().frame_size.height(), 480); |
258 EXPECT_EQ(last_format().frame_rate, testing::get<1>(GetParam())); | 260 EXPECT_EQ(last_format().frame_rate, testing::get<1>(GetParam())); |
259 device->StopAndDeAllocate(); | 261 device->StopAndDeAllocate(); |
260 } | 262 } |
261 | 263 |
262 INSTANTIATE_TEST_CASE_P( | 264 INSTANTIATE_TEST_CASE_P( |
263 , | 265 , |
264 FakeVideoCaptureDeviceTest, | 266 FakeVideoCaptureDeviceTest, |
265 Combine(Values(FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, | 267 Combine(Values(FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, |
266 FakeVideoCaptureDevice::BufferOwnership::CLIENT_BUFFERS), | 268 FakeVideoCaptureDevice::BufferOwnership::CLIENT_BUFFERS), |
267 Values(20, 29.97, 30, 50, 60))); | 269 Values(20, 29.97, 30, 50, 60))); |
268 | 270 |
269 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { | 271 TEST_F(FakeVideoCaptureDeviceTest, GetDeviceSupportedFormats) { |
270 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( | 272 std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( |
271 EnumerateDevices()); | 273 EnumerateDevices()); |
272 | 274 |
273 for (const auto& descriptors_iterator : *descriptors) { | 275 for (const auto& descriptors_iterator : *descriptors) { |
274 VideoCaptureFormats supported_formats; | 276 VideoCaptureFormats supported_formats; |
275 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator, | 277 video_capture_device_factory_->GetSupportedFormats(descriptors_iterator, |
276 &supported_formats); | 278 &supported_formats); |
277 ASSERT_EQ(supported_formats.size(), 4u); | 279 ASSERT_EQ(supported_formats.size(), 5u); |
278 EXPECT_EQ(supported_formats[0].frame_size.width(), 320); | 280 const std::string device_id = descriptors_iterator.device_id; |
279 EXPECT_EQ(supported_formats[0].frame_size.height(), 240); | 281 VideoPixelFormat expected_format = |
280 EXPECT_EQ(supported_formats[0].pixel_format, PIXEL_FORMAT_I420); | 282 (device_id == "/dev/video1") ? PIXEL_FORMAT_Y16 : PIXEL_FORMAT_I420; |
| 283 EXPECT_EQ(supported_formats[0].frame_size.width(), 96); |
| 284 EXPECT_EQ(supported_formats[0].frame_size.height(), 96); |
| 285 EXPECT_EQ(supported_formats[0].pixel_format, expected_format); |
281 EXPECT_GE(supported_formats[0].frame_rate, 20.0); | 286 EXPECT_GE(supported_formats[0].frame_rate, 20.0); |
282 EXPECT_EQ(supported_formats[1].frame_size.width(), 640); | 287 EXPECT_EQ(supported_formats[1].frame_size.width(), 320); |
283 EXPECT_EQ(supported_formats[1].frame_size.height(), 480); | 288 EXPECT_EQ(supported_formats[1].frame_size.height(), 240); |
284 EXPECT_EQ(supported_formats[1].pixel_format, PIXEL_FORMAT_I420); | 289 EXPECT_EQ(supported_formats[1].pixel_format, expected_format); |
285 EXPECT_GE(supported_formats[1].frame_rate, 20.0); | 290 EXPECT_GE(supported_formats[1].frame_rate, 20.0); |
286 EXPECT_EQ(supported_formats[2].frame_size.width(), 1280); | 291 EXPECT_EQ(supported_formats[2].frame_size.width(), 640); |
287 EXPECT_EQ(supported_formats[2].frame_size.height(), 720); | 292 EXPECT_EQ(supported_formats[2].frame_size.height(), 480); |
288 EXPECT_EQ(supported_formats[2].pixel_format, PIXEL_FORMAT_I420); | 293 EXPECT_EQ(supported_formats[2].pixel_format, expected_format); |
289 EXPECT_GE(supported_formats[2].frame_rate, 20.0); | 294 EXPECT_GE(supported_formats[2].frame_rate, 20.0); |
290 EXPECT_EQ(supported_formats[3].frame_size.width(), 1920); | 295 EXPECT_EQ(supported_formats[3].frame_size.width(), 1280); |
291 EXPECT_EQ(supported_formats[3].frame_size.height(), 1080); | 296 EXPECT_EQ(supported_formats[3].frame_size.height(), 720); |
292 EXPECT_EQ(supported_formats[3].pixel_format, PIXEL_FORMAT_I420); | 297 EXPECT_EQ(supported_formats[3].pixel_format, expected_format); |
293 EXPECT_GE(supported_formats[3].frame_rate, 20.0); | 298 EXPECT_GE(supported_formats[3].frame_rate, 20.0); |
| 299 EXPECT_EQ(supported_formats[4].frame_size.width(), 1920); |
| 300 EXPECT_EQ(supported_formats[4].frame_size.height(), 1080); |
| 301 EXPECT_EQ(supported_formats[4].pixel_format, expected_format); |
| 302 EXPECT_GE(supported_formats[4].frame_rate, 20.0); |
294 } | 303 } |
295 } | 304 } |
296 | 305 |
297 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) { | 306 TEST_F(FakeVideoCaptureDeviceTest, GetAndSetCapabilities) { |
298 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | 307 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( |
299 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); | 308 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); |
300 ASSERT_TRUE(device); | 309 ASSERT_TRUE(device); |
301 | 310 |
302 VideoCaptureParams capture_params; | 311 VideoCaptureParams capture_params; |
303 capture_params.requested_format.frame_size.SetSize(640, 480); | 312 capture_params.requested_format.frame_size.SetSize(640, 480); |
304 capture_params.requested_format.frame_rate = 30.0; | 313 capture_params.requested_format.frame_rate = 30.0; |
305 device->AllocateAndStart(capture_params, std::move(client_)); | 314 device->AllocateAndStart(capture_params, CreateClient()); |
306 | 315 |
307 VideoCaptureDevice::GetPhotoCapabilitiesCallback scoped_get_callback( | 316 VideoCaptureDevice::GetPhotoCapabilitiesCallback scoped_get_callback( |
308 base::Bind(&ImageCaptureClient::DoOnGetPhotoCapabilities, | 317 base::Bind(&ImageCaptureClient::DoOnGetPhotoCapabilities, |
309 image_capture_client_), | 318 image_capture_client_), |
310 base::Bind(&ImageCaptureClient::OnGetPhotoCapabilitiesFailure, | 319 base::Bind(&ImageCaptureClient::OnGetPhotoCapabilitiesFailure, |
311 image_capture_client_)); | 320 image_capture_client_)); |
312 | 321 |
313 EXPECT_CALL(*image_capture_client_.get(), OnCorrectGetPhotoCapabilities()) | 322 EXPECT_CALL(*image_capture_client_.get(), OnCorrectGetPhotoCapabilities()) |
314 .Times(1); | 323 .Times(1); |
315 device->GetPhotoCapabilities(std::move(scoped_get_callback)); | 324 device->GetPhotoCapabilities(std::move(scoped_get_callback)); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 } | 413 } |
405 | 414 |
406 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) { | 415 TEST_F(FakeVideoCaptureDeviceTest, TakePhoto) { |
407 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( | 416 std::unique_ptr<VideoCaptureDevice> device(new FakeVideoCaptureDevice( |
408 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); | 417 FakeVideoCaptureDevice::BufferOwnership::OWN_BUFFERS, 30.0)); |
409 ASSERT_TRUE(device); | 418 ASSERT_TRUE(device); |
410 | 419 |
411 VideoCaptureParams capture_params; | 420 VideoCaptureParams capture_params; |
412 capture_params.requested_format.frame_size.SetSize(640, 480); | 421 capture_params.requested_format.frame_size.SetSize(640, 480); |
413 capture_params.requested_format.frame_rate = 30.0; | 422 capture_params.requested_format.frame_rate = 30.0; |
414 device->AllocateAndStart(capture_params, std::move(client_)); | 423 device->AllocateAndStart(capture_params, CreateClient()); |
415 | 424 |
416 VideoCaptureDevice::TakePhotoCallback scoped_callback( | 425 VideoCaptureDevice::TakePhotoCallback scoped_callback( |
417 base::Bind(&ImageCaptureClient::DoOnPhotoTaken, image_capture_client_), | 426 base::Bind(&ImageCaptureClient::DoOnPhotoTaken, image_capture_client_), |
418 base::Bind(&ImageCaptureClient::OnTakePhotoFailure, | 427 base::Bind(&ImageCaptureClient::OnTakePhotoFailure, |
419 image_capture_client_)); | 428 image_capture_client_)); |
420 | 429 |
421 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); | 430 EXPECT_CALL(*image_capture_client_.get(), OnCorrectPhotoTaken()).Times(1); |
422 device->TakePhoto(std::move(scoped_callback)); | 431 device->TakePhoto(std::move(scoped_callback)); |
423 | 432 |
424 run_loop_.reset(new base::RunLoop()); | 433 run_loop_.reset(new base::RunLoop()); |
425 run_loop_->Run(); | 434 run_loop_->Run(); |
426 device->StopAndDeAllocate(); | 435 device->StopAndDeAllocate(); |
427 } | 436 } |
428 | 437 |
429 TEST_P(FakeVideoCaptureDeviceCommandLineTest, FrameRate) { | 438 TEST_P(FakeVideoCaptureDeviceCommandLineTest, FrameRate) { |
430 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 439 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
431 switches::kUseFakeDeviceForMediaStream, GetParam().argument); | 440 switches::kUseFakeDeviceForMediaStream, GetParam().argument); |
432 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( | 441 const std::unique_ptr<VideoCaptureDeviceDescriptors> descriptors( |
433 EnumerateDevices()); | 442 EnumerateDevices()); |
434 ASSERT_FALSE(descriptors->empty()); | 443 ASSERT_FALSE(descriptors->empty()); |
435 | 444 |
436 for (const auto& descriptors_iterator : *descriptors) { | 445 for (const auto& descriptors_iterator : *descriptors) { |
437 std::unique_ptr<VideoCaptureDevice> device = | 446 std::unique_ptr<VideoCaptureDevice> device = |
438 video_capture_device_factory_->CreateDevice(descriptors_iterator); | 447 video_capture_device_factory_->CreateDevice(descriptors_iterator); |
439 ASSERT_TRUE(device); | 448 ASSERT_TRUE(device); |
440 | 449 |
441 VideoCaptureParams capture_params; | 450 VideoCaptureParams capture_params; |
442 capture_params.requested_format.frame_size.SetSize(1280, 720); | 451 capture_params.requested_format.frame_size.SetSize(1280, 720); |
443 capture_params.requested_format.frame_rate = GetParam().fps; | 452 capture_params.requested_format.frame_rate = GetParam().fps; |
444 device->AllocateAndStart(capture_params, std::move(client_)); | 453 device->AllocateAndStart(capture_params, CreateClient()); |
445 | 454 |
446 WaitForCapturedFrame(); | 455 WaitForCapturedFrame(); |
447 EXPECT_EQ(last_format().frame_size.width(), 1280); | 456 EXPECT_EQ(last_format().frame_size.width(), 1280); |
448 EXPECT_EQ(last_format().frame_size.height(), 720); | 457 EXPECT_EQ(last_format().frame_size.height(), 720); |
449 EXPECT_EQ(last_format().frame_rate, GetParam().fps); | 458 EXPECT_EQ(last_format().frame_rate, GetParam().fps); |
450 device->StopAndDeAllocate(); | 459 device->StopAndDeAllocate(); |
451 } | 460 } |
452 } | 461 } |
453 | 462 |
454 INSTANTIATE_TEST_CASE_P(, | 463 INSTANTIATE_TEST_CASE_P(, |
455 FakeVideoCaptureDeviceCommandLineTest, | 464 FakeVideoCaptureDeviceCommandLineTest, |
456 Values(CommandLineTestData{"fps=-1", 5}, | 465 Values(CommandLineTestData{"fps=-1", 5}, |
457 CommandLineTestData{"fps=29.97", 29.97f}, | 466 CommandLineTestData{"fps=29.97", 29.97f}, |
458 CommandLineTestData{"fps=60", 60}, | 467 CommandLineTestData{"fps=60", 60}, |
459 CommandLineTestData{"fps=1000", 60})); | 468 CommandLineTestData{"fps=1000", 60})); |
460 }; // namespace media | 469 }; // namespace media |
OLD | NEW |