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