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