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 // Unit test for VideoCaptureController. | 5 // Unit test for VideoCaptureController. |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "content/browser/renderer_host/media/media_stream_provider.h" | 15 #include "content/browser/renderer_host/media/media_stream_provider.h" |
16 #include "content/browser/renderer_host/media/video_capture_controller.h" | 16 #include "content/browser/renderer_host/media/video_capture_controller.h" |
17 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" | 17 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" |
18 #include "content/browser/renderer_host/media/video_capture_manager.h" | 18 #include "content/browser/renderer_host/media/video_capture_manager.h" |
19 #include "content/common/gpu/client/gl_helper.h" | |
19 #include "content/common/media/media_stream_options.h" | 20 #include "content/common/media/media_stream_options.h" |
20 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
21 #include "gpu/command_buffer/common/mailbox_holder.h" | 22 #include "gpu/command_buffer/common/mailbox_holder.h" |
22 #include "media/base/video_util.h" | 23 #include "media/base/video_util.h" |
23 #include "media/video/capture/video_capture_types.h" | 24 #include "media/video/capture/video_capture_types.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
28 #if defined(OS_ANDROID) | |
29 #include "content/browser/renderer_host/no_image_transport_factory_android.h" | |
30 #else | |
31 #include "content/browser/compositor/test/no_transport_image_transport_factory.h " | |
32 #endif | |
33 | |
27 using ::testing::InSequence; | 34 using ::testing::InSequence; |
28 using ::testing::Mock; | 35 using ::testing::Mock; |
29 | 36 |
30 namespace content { | 37 namespace content { |
31 | 38 |
32 class MockVideoCaptureControllerEventHandler | 39 class MockVideoCaptureControllerEventHandler |
33 : public VideoCaptureControllerEventHandler { | 40 : public VideoCaptureControllerEventHandler { |
34 public: | 41 public: |
35 explicit MockVideoCaptureControllerEventHandler( | 42 explicit MockVideoCaptureControllerEventHandler( |
36 VideoCaptureController* controller) | 43 VideoCaptureController* controller) |
(...skipping 26 matching lines...) Expand all Loading... | |
63 const media::VideoCaptureFormat& format, | 70 const media::VideoCaptureFormat& format, |
64 base::TimeTicks timestamp) OVERRIDE { | 71 base::TimeTicks timestamp) OVERRIDE { |
65 DoBufferReady(id); | 72 DoBufferReady(id); |
66 base::MessageLoop::current()->PostTask( | 73 base::MessageLoop::current()->PostTask( |
67 FROM_HERE, | 74 FROM_HERE, |
68 base::Bind(&VideoCaptureController::ReturnBuffer, | 75 base::Bind(&VideoCaptureController::ReturnBuffer, |
69 base::Unretained(controller_), | 76 base::Unretained(controller_), |
70 id, | 77 id, |
71 this, | 78 this, |
72 buffer_id, | 79 buffer_id, |
73 std::vector<uint32>())); | 80 0)); |
74 } | 81 } |
75 virtual void OnMailboxBufferReady(const VideoCaptureControllerID& id, | 82 virtual void OnMailboxBufferReady(const VideoCaptureControllerID& id, |
76 int buffer_id, | 83 int buffer_id, |
77 const gpu::MailboxHolder& mailbox_holder, | 84 const gpu::MailboxHolder& mailbox_holder, |
78 const media::VideoCaptureFormat& format, | 85 const media::VideoCaptureFormat& format, |
79 base::TimeTicks timestamp) OVERRIDE { | 86 base::TimeTicks timestamp) OVERRIDE { |
80 DoMailboxBufferReady(id); | 87 DoMailboxBufferReady(id); |
81 // Use a very different syncpoint value when returning a new syncpoint. | |
82 std::vector<uint32> release_sync_points; | |
83 release_sync_points.push_back(~mailbox_holder.sync_point); | |
84 base::MessageLoop::current()->PostTask( | 88 base::MessageLoop::current()->PostTask( |
85 FROM_HERE, | 89 FROM_HERE, |
86 base::Bind(&VideoCaptureController::ReturnBuffer, | 90 base::Bind(&VideoCaptureController::ReturnBuffer, |
87 base::Unretained(controller_), | 91 base::Unretained(controller_), |
88 id, | 92 id, |
89 this, | 93 this, |
90 buffer_id, | 94 buffer_id, |
91 release_sync_points)); | 95 mailbox_holder.sync_point)); |
92 } | 96 } |
93 virtual void OnEnded(const VideoCaptureControllerID& id) OVERRIDE { | 97 virtual void OnEnded(const VideoCaptureControllerID& id) OVERRIDE { |
94 DoEnded(id); | 98 DoEnded(id); |
95 // OnEnded() must respond by (eventually) unregistering the client. | 99 // OnEnded() must respond by (eventually) unregistering the client. |
96 base::MessageLoop::current()->PostTask(FROM_HERE, | 100 base::MessageLoop::current()->PostTask(FROM_HERE, |
97 base::Bind(base::IgnoreResult(&VideoCaptureController::RemoveClient), | 101 base::Bind(base::IgnoreResult(&VideoCaptureController::RemoveClient), |
98 base::Unretained(controller_), id, this)); | 102 base::Unretained(controller_), id, this)); |
99 } | 103 } |
100 | 104 |
101 VideoCaptureController* controller_; | 105 VideoCaptureController* controller_; |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 ASSERT_EQ(1, controller_->GetClientCount()) | 260 ASSERT_EQ(1, controller_->GetClientCount()) |
257 << "Removing non-existant session 200 should be a no-op."; | 261 << "Removing non-existant session 200 should be a no-op."; |
258 ASSERT_EQ(400, | 262 ASSERT_EQ(400, |
259 controller_->RemoveClient(client_b_route_2, client_b_.get())) | 263 controller_->RemoveClient(client_b_route_2, client_b_.get())) |
260 << "Removing client B/2 should return its session_id."; | 264 << "Removing client B/2 should return its session_id."; |
261 // Clients in controller: [] | 265 // Clients in controller: [] |
262 ASSERT_EQ(0, controller_->GetClientCount()) | 266 ASSERT_EQ(0, controller_->GetClientCount()) |
263 << "Client count should return to zero after all clients are gone."; | 267 << "Client count should return to zero after all clients are gone."; |
264 } | 268 } |
265 | 269 |
266 static void CacheSyncPoint(std::vector<uint32>* called_release_sync_points, | 270 static void CacheSyncPoint(uint32* called_release_sync_point, |
267 const std::vector<uint32>& release_sync_points) { | 271 uint32 release_sync_point) { |
268 DCHECK(called_release_sync_points->empty()); | 272 *called_release_sync_point = release_sync_point; |
269 called_release_sync_points->assign(release_sync_points.begin(), | |
270 release_sync_points.end()); | |
271 } | 273 } |
272 | 274 |
273 // This test will connect and disconnect several clients while simulating an | 275 // This test will connect and disconnect several clients while simulating an |
274 // active capture device being started and generating frames. It runs on one | 276 // active capture device being started and generating frames. It runs on one |
275 // thread and is intended to behave deterministically. | 277 // thread and is intended to behave deterministically. |
276 TEST_F(VideoCaptureControllerTest, NormalCaptureMultipleClients) { | 278 TEST_F(VideoCaptureControllerTest, NormalCaptureMultipleClients) { |
279 // VideoCaptureController::ReturnBuffer() uses ImageTransportFactory. | |
280 #if defined(OS_ANDROID) | |
281 ImageTransportFactoryAndroid::InitializeForUnitTests( | |
282 scoped_ptr<ImageTransportFactoryAndroid>( | |
283 new NoTransportImageTransportFactoryAndroid)); | |
284 #else | |
285 ImageTransportFactory::InitializeForUnitTests( | |
286 scoped_ptr<ImageTransportFactory>(new NoTransportImageTransportFactory)); | |
287 #endif | |
288 | |
277 media::VideoCaptureParams session_100; | 289 media::VideoCaptureParams session_100; |
278 session_100.requested_format = media::VideoCaptureFormat( | 290 session_100.requested_format = media::VideoCaptureFormat( |
279 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); | 291 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); |
280 | 292 |
281 media::VideoCaptureParams session_200 = session_100; | 293 media::VideoCaptureParams session_200 = session_100; |
282 | 294 |
283 media::VideoCaptureParams session_300 = session_100; | 295 media::VideoCaptureParams session_300 = session_100; |
284 | 296 |
285 media::VideoCaptureParams session_1 = session_100; | 297 media::VideoCaptureParams session_1 = session_100; |
286 | 298 |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 device_->OnIncomingCapturedVideoFrame( | 489 device_->OnIncomingCapturedVideoFrame( |
478 buffer, | 490 buffer, |
479 media::VideoCaptureFormat(capture_resolution, | 491 media::VideoCaptureFormat(capture_resolution, |
480 device_format.frame_rate, | 492 device_format.frame_rate, |
481 media::PIXEL_FORMAT_I420), | 493 media::PIXEL_FORMAT_I420), |
482 WrapI420Buffer(buffer, capture_resolution), | 494 WrapI420Buffer(buffer, capture_resolution), |
483 base::TimeTicks()); | 495 base::TimeTicks()); |
484 buffer = NULL; | 496 buffer = NULL; |
485 } | 497 } |
486 std::vector<uint32> mailbox_syncpoints(mailbox_buffers); | 498 std::vector<uint32> mailbox_syncpoints(mailbox_buffers); |
487 std::vector<std::vector<uint32> > release_syncpoint_vectors(mailbox_buffers); | 499 std::vector<uint32> release_syncpoints(mailbox_buffers); |
dshwang
2014/07/10 21:44:25
sievers@, nits done.
| |
500 #if defined(OS_ANDROID) | |
501 GLHelper* gl_helper = | |
502 ImageTransportFactoryAndroid::GetInstance()->GetGLHelper(); | |
503 #else | |
504 GLHelper* gl_helper = ImageTransportFactory::GetInstance()->GetGLHelper(); | |
505 #endif | |
488 for (int i = 0; i < mailbox_buffers; ++i) { | 506 for (int i = 0; i < mailbox_buffers; ++i) { |
489 buffer = device_->ReserveOutputBuffer(media::VideoFrame::NATIVE_TEXTURE, | 507 buffer = device_->ReserveOutputBuffer(media::VideoFrame::NATIVE_TEXTURE, |
490 gfx::Size(0, 0)); | 508 gfx::Size(0, 0)); |
491 ASSERT_TRUE(buffer); | 509 ASSERT_TRUE(buffer); |
492 mailbox_syncpoints[i] = i; | 510 mailbox_syncpoints[i] = gl_helper->InsertSyncPoint(); |
493 device_->OnIncomingCapturedVideoFrame( | 511 device_->OnIncomingCapturedVideoFrame( |
494 buffer, | 512 buffer, |
495 media::VideoCaptureFormat(capture_resolution, | 513 media::VideoCaptureFormat(capture_resolution, |
496 device_format.frame_rate, | 514 device_format.frame_rate, |
497 media::PIXEL_FORMAT_TEXTURE), | 515 media::PIXEL_FORMAT_TEXTURE), |
498 WrapMailboxBuffer( | 516 WrapMailboxBuffer(buffer, |
499 buffer, | 517 make_scoped_ptr(new gpu::MailboxHolder( |
500 make_scoped_ptr(new gpu::MailboxHolder( | 518 gpu::Mailbox(), 0, mailbox_syncpoints[i])), |
501 gpu::Mailbox(), 0, mailbox_syncpoints[i])), | 519 base::Bind(&CacheSyncPoint, &release_syncpoints[i]), |
502 base::Bind(&CacheSyncPoint, &release_syncpoint_vectors[i]), | 520 capture_resolution), |
503 capture_resolution), | |
504 base::TimeTicks()); | 521 base::TimeTicks()); |
505 buffer = NULL; | 522 buffer = NULL; |
506 } | 523 } |
507 // ReserveOutputBuffers ought to fail now regardless of buffer format, because | 524 // ReserveOutputBuffers ought to fail now regardless of buffer format, because |
508 // the pool is depleted. | 525 // the pool is depleted. |
509 ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::I420, | 526 ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::I420, |
510 capture_resolution)); | 527 capture_resolution)); |
511 ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::NATIVE_TEXTURE, | 528 ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::NATIVE_TEXTURE, |
512 gfx::Size(0, 0))); | 529 gfx::Size(0, 0))); |
513 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(shm_buffers); | 530 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(shm_buffers); |
514 EXPECT_CALL(*client_b_, DoMailboxBufferReady(client_b_route_2)) | 531 EXPECT_CALL(*client_b_, DoMailboxBufferReady(client_b_route_2)) |
515 .Times(mailbox_buffers); | 532 .Times(mailbox_buffers); |
516 base::RunLoop().RunUntilIdle(); | 533 base::RunLoop().RunUntilIdle(); |
517 for (size_t i = 0; i < mailbox_syncpoints.size(); ++i) { | 534 for (size_t i = 0; i < mailbox_syncpoints.size(); ++i) { |
518 // See: MockVideoCaptureControllerEventHandler::OnMailboxBufferReady() | 535 // A new release sync point must be inserted when the video frame is |
519 ASSERT_EQ(1u, release_syncpoint_vectors[i].size()); | 536 // returned to the Browser process. |
520 ASSERT_EQ(mailbox_syncpoints[i], ~release_syncpoint_vectors[i][0]); | 537 // See: MockVideoCaptureControllerEventHandler::OnMailboxBufferReady() and |
538 // VideoCaptureController::ReturnBuffer() | |
539 ASSERT_NE(mailbox_syncpoints[i], release_syncpoints[i]); | |
521 } | 540 } |
522 Mock::VerifyAndClearExpectations(client_b_.get()); | 541 Mock::VerifyAndClearExpectations(client_b_.get()); |
542 | |
543 #if defined(OS_ANDROID) | |
544 ImageTransportFactoryAndroid::TerminateForUnitTests(); | |
545 #else | |
546 ImageTransportFactory::Terminate(); | |
547 #endif | |
523 } | 548 } |
524 | 549 |
525 // Exercises the OnError() codepath of VideoCaptureController, and tests the | 550 // Exercises the OnError() codepath of VideoCaptureController, and tests the |
526 // behavior of various operations after the error state has been signalled. | 551 // behavior of various operations after the error state has been signalled. |
527 TEST_F(VideoCaptureControllerTest, ErrorBeforeDeviceCreation) { | 552 TEST_F(VideoCaptureControllerTest, ErrorBeforeDeviceCreation) { |
528 media::VideoCaptureParams session_100; | 553 media::VideoCaptureParams session_100; |
529 session_100.requested_format = media::VideoCaptureFormat( | 554 session_100.requested_format = media::VideoCaptureFormat( |
530 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); | 555 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); |
531 | 556 |
532 media::VideoCaptureParams session_200 = session_100; | 557 media::VideoCaptureParams session_200 = session_100; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
609 | 634 |
610 // Second client connects after the error state. It also should get told of | 635 // Second client connects after the error state. It also should get told of |
611 // the error. | 636 // the error. |
612 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); | 637 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); |
613 controller_->AddClient( | 638 controller_->AddClient( |
614 route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200); | 639 route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200); |
615 Mock::VerifyAndClearExpectations(client_b_.get()); | 640 Mock::VerifyAndClearExpectations(client_b_.get()); |
616 } | 641 } |
617 | 642 |
618 } // namespace content | 643 } // namespace content |
OLD | NEW |