| 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" |
| 27 #include "ui/compositor/test/in_process_context_factory.h" |
| 28 |
| 29 #if defined(OS_ANDROID) |
| 30 #include "content/browser/renderer_host/image_transport_factory_android.h" |
| 31 #else |
| 32 #include "content/browser/compositor/image_transport_factory.h" |
| 33 #endif |
| 26 | 34 |
| 27 using ::testing::InSequence; | 35 using ::testing::InSequence; |
| 28 using ::testing::Mock; | 36 using ::testing::Mock; |
| 29 | 37 |
| 30 namespace content { | 38 namespace content { |
| 31 | 39 |
| 32 class MockVideoCaptureControllerEventHandler | 40 class MockVideoCaptureControllerEventHandler |
| 33 : public VideoCaptureControllerEventHandler { | 41 : public VideoCaptureControllerEventHandler { |
| 34 public: | 42 public: |
| 35 explicit MockVideoCaptureControllerEventHandler( | 43 explicit MockVideoCaptureControllerEventHandler( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 63 const media::VideoCaptureFormat& format, | 71 const media::VideoCaptureFormat& format, |
| 64 base::TimeTicks timestamp) OVERRIDE { | 72 base::TimeTicks timestamp) OVERRIDE { |
| 65 DoBufferReady(id); | 73 DoBufferReady(id); |
| 66 base::MessageLoop::current()->PostTask( | 74 base::MessageLoop::current()->PostTask( |
| 67 FROM_HERE, | 75 FROM_HERE, |
| 68 base::Bind(&VideoCaptureController::ReturnBuffer, | 76 base::Bind(&VideoCaptureController::ReturnBuffer, |
| 69 base::Unretained(controller_), | 77 base::Unretained(controller_), |
| 70 id, | 78 id, |
| 71 this, | 79 this, |
| 72 buffer_id, | 80 buffer_id, |
| 73 std::vector<uint32>())); | 81 0)); |
| 74 } | 82 } |
| 75 virtual void OnMailboxBufferReady(const VideoCaptureControllerID& id, | 83 virtual void OnMailboxBufferReady(const VideoCaptureControllerID& id, |
| 76 int buffer_id, | 84 int buffer_id, |
| 77 const gpu::MailboxHolder& mailbox_holder, | 85 const gpu::MailboxHolder& mailbox_holder, |
| 78 const media::VideoCaptureFormat& format, | 86 const media::VideoCaptureFormat& format, |
| 79 base::TimeTicks timestamp) OVERRIDE { | 87 base::TimeTicks timestamp) OVERRIDE { |
| 80 DoMailboxBufferReady(id); | 88 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( | 89 base::MessageLoop::current()->PostTask( |
| 85 FROM_HERE, | 90 FROM_HERE, |
| 86 base::Bind(&VideoCaptureController::ReturnBuffer, | 91 base::Bind(&VideoCaptureController::ReturnBuffer, |
| 87 base::Unretained(controller_), | 92 base::Unretained(controller_), |
| 88 id, | 93 id, |
| 89 this, | 94 this, |
| 90 buffer_id, | 95 buffer_id, |
| 91 release_sync_points)); | 96 mailbox_holder.sync_point)); |
| 92 } | 97 } |
| 93 virtual void OnEnded(const VideoCaptureControllerID& id) OVERRIDE { | 98 virtual void OnEnded(const VideoCaptureControllerID& id) OVERRIDE { |
| 94 DoEnded(id); | 99 DoEnded(id); |
| 95 // OnEnded() must respond by (eventually) unregistering the client. | 100 // OnEnded() must respond by (eventually) unregistering the client. |
| 96 base::MessageLoop::current()->PostTask(FROM_HERE, | 101 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 97 base::Bind(base::IgnoreResult(&VideoCaptureController::RemoveClient), | 102 base::Bind(base::IgnoreResult(&VideoCaptureController::RemoveClient), |
| 98 base::Unretained(controller_), id, this)); | 103 base::Unretained(controller_), id, this)); |
| 99 } | 104 } |
| 100 | 105 |
| 101 VideoCaptureController* controller_; | 106 VideoCaptureController* controller_; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 ASSERT_EQ(1, controller_->GetClientCount()) | 261 ASSERT_EQ(1, controller_->GetClientCount()) |
| 257 << "Removing non-existant session 200 should be a no-op."; | 262 << "Removing non-existant session 200 should be a no-op."; |
| 258 ASSERT_EQ(400, | 263 ASSERT_EQ(400, |
| 259 controller_->RemoveClient(client_b_route_2, client_b_.get())) | 264 controller_->RemoveClient(client_b_route_2, client_b_.get())) |
| 260 << "Removing client B/2 should return its session_id."; | 265 << "Removing client B/2 should return its session_id."; |
| 261 // Clients in controller: [] | 266 // Clients in controller: [] |
| 262 ASSERT_EQ(0, controller_->GetClientCount()) | 267 ASSERT_EQ(0, controller_->GetClientCount()) |
| 263 << "Client count should return to zero after all clients are gone."; | 268 << "Client count should return to zero after all clients are gone."; |
| 264 } | 269 } |
| 265 | 270 |
| 266 static void CacheSyncPoint(std::vector<uint32>* called_release_sync_points, | 271 static void CacheSyncPoint(uint32* called_release_sync_point, |
| 267 const std::vector<uint32>& release_sync_points) { | 272 uint32 release_sync_point) { |
| 268 DCHECK(called_release_sync_points->empty()); | 273 *called_release_sync_point = release_sync_point; |
| 269 called_release_sync_points->assign(release_sync_points.begin(), | |
| 270 release_sync_points.end()); | |
| 271 } | 274 } |
| 272 | 275 |
| 273 // This test will connect and disconnect several clients while simulating an | 276 // This test will connect and disconnect several clients while simulating an |
| 274 // active capture device being started and generating frames. It runs on one | 277 // active capture device being started and generating frames. It runs on one |
| 275 // thread and is intended to behave deterministically. | 278 // thread and is intended to behave deterministically. |
| 276 TEST_F(VideoCaptureControllerTest, NormalCaptureMultipleClients) { | 279 TEST_F(VideoCaptureControllerTest, NormalCaptureMultipleClients) { |
| 280 // VideoCaptureController::ReturnBuffer() uses ImageTransportFactory. |
| 281 #if defined(OS_ANDROID) |
| 282 ImageTransportFactoryAndroid::InitializeForUnitTests( |
| 283 scoped_ptr<ui::ContextFactory>(new ui::InProcessContextFactory)); |
| 284 #else |
| 285 ImageTransportFactory::InitializeForUnitTests( |
| 286 scoped_ptr<ui::ContextFactory>(new ui::InProcessContextFactory)); |
| 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_syncpoint_vectors(mailbox_buffers); |
| 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( |
| 499 buffer, | 517 buffer, |
| 500 make_scoped_ptr(new gpu::MailboxHolder( | 518 make_scoped_ptr(new gpu::MailboxHolder( |
| 501 gpu::Mailbox(), 0, mailbox_syncpoints[i])), | 519 gpu::Mailbox(), 0, mailbox_syncpoints[i])), |
| 502 base::Bind(&CacheSyncPoint, &release_syncpoint_vectors[i]), | 520 base::Bind(&CacheSyncPoint, &release_syncpoint_vectors[i]), |
| 503 capture_resolution), | 521 capture_resolution), |
| 504 base::TimeTicks()); | 522 base::TimeTicks()); |
| 505 buffer = NULL; | 523 buffer = NULL; |
| 506 } | 524 } |
| 507 // ReserveOutputBuffers ought to fail now regardless of buffer format, because | 525 // ReserveOutputBuffers ought to fail now regardless of buffer format, because |
| 508 // the pool is depleted. | 526 // the pool is depleted. |
| 509 ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::I420, | 527 ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::I420, |
| 510 capture_resolution)); | 528 capture_resolution)); |
| 511 ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::NATIVE_TEXTURE, | 529 ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::NATIVE_TEXTURE, |
| 512 gfx::Size(0, 0))); | 530 gfx::Size(0, 0))); |
| 513 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(shm_buffers); | 531 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(shm_buffers); |
| 514 EXPECT_CALL(*client_b_, DoMailboxBufferReady(client_b_route_2)) | 532 EXPECT_CALL(*client_b_, DoMailboxBufferReady(client_b_route_2)) |
| 515 .Times(mailbox_buffers); | 533 .Times(mailbox_buffers); |
| 516 base::RunLoop().RunUntilIdle(); | 534 base::RunLoop().RunUntilIdle(); |
| 517 for (size_t i = 0; i < mailbox_syncpoints.size(); ++i) { | 535 for (size_t i = 0; i < mailbox_syncpoints.size(); ++i) { |
| 518 // See: MockVideoCaptureControllerEventHandler::OnMailboxBufferReady() | 536 // A new release sync point must be inserted when the video frame is |
| 519 ASSERT_EQ(1u, release_syncpoint_vectors[i].size()); | 537 // returned to the Browser process. |
| 520 ASSERT_EQ(mailbox_syncpoints[i], ~release_syncpoint_vectors[i][0]); | 538 // See: MockVideoCaptureControllerEventHandler::OnMailboxBufferReady() and |
| 539 // VideoCaptureController::ReturnBuffer() |
| 540 ASSERT_NE(mailbox_syncpoints[i], release_syncpoint_vectors[i]); |
| 521 } | 541 } |
| 522 Mock::VerifyAndClearExpectations(client_b_.get()); | 542 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 523 } | 543 } |
| 524 | 544 |
| 525 // Exercises the OnError() codepath of VideoCaptureController, and tests the | 545 // Exercises the OnError() codepath of VideoCaptureController, and tests the |
| 526 // behavior of various operations after the error state has been signalled. | 546 // behavior of various operations after the error state has been signalled. |
| 527 TEST_F(VideoCaptureControllerTest, ErrorBeforeDeviceCreation) { | 547 TEST_F(VideoCaptureControllerTest, ErrorBeforeDeviceCreation) { |
| 528 media::VideoCaptureParams session_100; | 548 media::VideoCaptureParams session_100; |
| 529 session_100.requested_format = media::VideoCaptureFormat( | 549 session_100.requested_format = media::VideoCaptureFormat( |
| 530 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); | 550 gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 | 629 |
| 610 // Second client connects after the error state. It also should get told of | 630 // Second client connects after the error state. It also should get told of |
| 611 // the error. | 631 // the error. |
| 612 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); | 632 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); |
| 613 controller_->AddClient( | 633 controller_->AddClient( |
| 614 route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200); | 634 route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200); |
| 615 Mock::VerifyAndClearExpectations(client_b_.get()); | 635 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 616 } | 636 } |
| 617 | 637 |
| 618 } // namespace content | 638 } // namespace content |
| OLD | NEW |