1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Unit test for VideoCaptureController.
#include <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "content/browser/renderer_host/media/media_stream_provider.h"
#include "content/browser/renderer_host/media/video_capture_controller.h"
#include "content/browser/renderer_host/media/video_capture_controller_event_handler.h"
#include "content/browser/renderer_host/media/video_capture_manager.h"
#include "content/common/gpu/client/gl_helper.h"
#include "content/common/media/media_stream_options.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "gpu/command_buffer/common/mailbox_holder.h"
#include "media/base/video_util.h"
#include "media/video/capture/video_capture_types.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if !defined(OS_ANDROID)
#include "content/browser/compositor/test/no_transport_image_transport_factory.h"
#endif
using ::testing::InSequence;
using ::testing::Mock;
namespace content {
class MockVideoCaptureControllerEventHandler
: public VideoCaptureControllerEventHandler {
public:
explicit MockVideoCaptureControllerEventHandler(
VideoCaptureController* controller)
: controller_(controller) {}
virtual ~MockVideoCaptureControllerEventHandler() {}
// These mock methods are delegated to by our fake implementation of
// VideoCaptureControllerEventHandler, to be used in EXPECT_CALL().
MOCK_METHOD1(DoBufferCreated, void(const VideoCaptureControllerID&));
MOCK_METHOD1(DoBufferDestroyed, void(const VideoCaptureControllerID&));
MOCK_METHOD1(DoBufferReady, void(const VideoCaptureControllerID&));
MOCK_METHOD1(DoMailboxBufferReady, void(const VideoCaptureControllerID&));
MOCK_METHOD1(DoEnded, void(const VideoCaptureControllerID&));
MOCK_METHOD1(DoError, void(const VideoCaptureControllerID&));
virtual void OnError(const VideoCaptureControllerID& id) override {
DoError(id);
}
virtual void OnBufferCreated(const VideoCaptureControllerID& id,
base::SharedMemoryHandle handle,
int length, int buffer_id) override {
DoBufferCreated(id);
}
virtual void OnBufferDestroyed(const VideoCaptureControllerID& id,
int buffer_id) override {
DoBufferDestroyed(id);
}
virtual void OnBufferReady(const VideoCaptureControllerID& id,
int buffer_id,
const media::VideoCaptureFormat& format,
const gfx::Rect& visible_rect,
base::TimeTicks timestamp) override {
DoBufferReady(id);
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&VideoCaptureController::ReturnBuffer,
base::Unretained(controller_),
id,
this,
buffer_id,
0));
}
virtual void OnMailboxBufferReady(const VideoCaptureControllerID& id,
int buffer_id,
const gpu::MailboxHolder& mailbox_holder,
const media::VideoCaptureFormat& format,
base::TimeTicks timestamp) override {
DoMailboxBufferReady(id);
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&VideoCaptureController::ReturnBuffer,
base::Unretained(controller_),
id,
this,
buffer_id,
mailbox_holder.sync_point));
}
virtual void OnEnded(const VideoCaptureControllerID& id) override {
DoEnded(id);
// OnEnded() must respond by (eventually) unregistering the client.
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(base::IgnoreResult(&VideoCaptureController::RemoveClient),
base::Unretained(controller_), id, this));
}
VideoCaptureController* controller_;
};
// Test class.
class VideoCaptureControllerTest : public testing::Test {
public:
VideoCaptureControllerTest() {}
~VideoCaptureControllerTest() override {}
protected:
static const int kPoolSize = 3;
void SetUp() override {
controller_.reset(new VideoCaptureController(kPoolSize));
device_ = controller_->NewDeviceClient().Pass();
client_a_.reset(new MockVideoCaptureControllerEventHandler(
controller_.get()));
client_b_.reset(new MockVideoCaptureControllerEventHandler(
controller_.get()));
}
void TearDown() override { base::RunLoop().RunUntilIdle(); }
scoped_refptr<media::VideoFrame> WrapI420Buffer(
const scoped_refptr<media::VideoCaptureDevice::Client::Buffer>& buffer,
gfx::Size dimensions) {
return media::VideoFrame::WrapExternalPackedMemory(
media::VideoFrame::I420,
dimensions,
gfx::Rect(dimensions),
dimensions,
reinterpret_cast<uint8*>(buffer->data()),
media::VideoFrame::AllocationSize(media::VideoFrame::I420, dimensions),
base::SharedMemory::NULLHandle(),
base::TimeDelta(),
base::Closure());
}
scoped_refptr<media::VideoFrame> WrapMailboxBuffer(
const scoped_refptr<media::VideoCaptureDevice::Client::Buffer>& buffer,
scoped_ptr<gpu::MailboxHolder> holder,
const media::VideoFrame::ReleaseMailboxCB& release_cb,
gfx::Size dimensions) {
return media::VideoFrame::WrapNativeTexture(
holder.Pass(),
release_cb,
dimensions,
gfx::Rect(dimensions),
dimensions,
base::TimeDelta(),
media::VideoFrame::ReadPixelsCB());
}
TestBrowserThreadBundle bundle_;
scoped_ptr<MockVideoCaptureControllerEventHandler> client_a_;
scoped_ptr<MockVideoCaptureControllerEventHandler> client_b_;
scoped_ptr<VideoCaptureController> controller_;
scoped_ptr<media::VideoCaptureDevice::Client> device_;
private:
DISALLOW_COPY_AND_ASSIGN(VideoCaptureControllerTest);
};
// A simple test of VideoCaptureController's ability to add, remove, and keep
// track of clients.
TEST_F(VideoCaptureControllerTest, AddAndRemoveClients) {
media::VideoCaptureParams session_100;
session_100.requested_format = media::VideoCaptureFormat(
gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420);
media::VideoCaptureParams session_200 = session_100;
media::VideoCaptureParams session_300 = session_100;
media::VideoCaptureParams session_400 = session_100;
// Intentionally use the same route ID for two of the clients: the device_ids
// are a per-VideoCaptureHost namespace, and can overlap across hosts.
const VideoCaptureControllerID client_a_route_1(44);
const VideoCaptureControllerID client_a_route_2(30);
const VideoCaptureControllerID client_b_route_1(30);
const VideoCaptureControllerID client_b_route_2(1);
// Clients in controller: []
ASSERT_EQ(0, controller_->GetClientCount())
<< "Client count should initially be zero.";
controller_->AddClient(client_a_route_1,
client_a_.get(),
base::kNullProcessHandle,
100,
session_100);
// Clients in controller: [A/1]
ASSERT_EQ(1, controller_->GetClientCount())
<< "Adding client A/1 should bump client count.";
controller_->AddClient(client_a_route_2,
client_a_.get(),
base::kNullProcessHandle,
200,
session_200);
// Clients in controller: [A/1, A/2]
ASSERT_EQ(2, controller_->GetClientCount())
<< "Adding client A/2 should bump client count.";
controller_->AddClient(client_b_route_1,
client_b_.get(),
base::kNullProcessHandle,
300,
session_300);
// Clients in controller: [A/1, A/2, B/1]
ASSERT_EQ(3, controller_->GetClientCount())
<< "Adding client B/1 should bump client count.";
ASSERT_EQ(200,
controller_->RemoveClient(client_a_route_2, client_a_.get()))
<< "Removing client A/1 should return its session_id.";
// Clients in controller: [A/1, B/1]
ASSERT_EQ(2, controller_->GetClientCount());
ASSERT_EQ(static_cast<int>(kInvalidMediaCaptureSessionId),
controller_->RemoveClient(client_a_route_2, client_a_.get()))
<< "Removing a nonexistant client should fail.";
// Clients in controller: [A/1, B/1]
ASSERT_EQ(2, controller_->GetClientCount());
ASSERT_EQ(300,
controller_->RemoveClient(client_b_route_1, client_b_.get()))
<< "Removing client B/1 should return its session_id.";
// Clients in controller: [A/1]
ASSERT_EQ(1, controller_->GetClientCount());
controller_->AddClient(client_b_route_2,
client_b_.get(),
base::kNullProcessHandle,
400,
session_400);
// Clients in controller: [A/1, B/2]
EXPECT_CALL(*client_a_, DoEnded(client_a_route_1)).Times(1);
controller_->StopSession(100); // Session 100 == client A/1
Mock::VerifyAndClearExpectations(client_a_.get());
ASSERT_EQ(2, controller_->GetClientCount())
<< "Client should be closed but still exist after StopSession.";
// Clients in controller: [A/1 (closed, removal pending), B/2]
base::RunLoop().RunUntilIdle();
// Clients in controller: [B/2]
ASSERT_EQ(1, controller_->GetClientCount())
<< "Client A/1 should be deleted by now.";
controller_->StopSession(200); // Session 200 does not exist anymore
// Clients in controller: [B/2]
ASSERT_EQ(1, controller_->GetClientCount())
<< "Stopping non-existant session 200 should be a no-op.";
controller_->StopSession(256); // Session 256 never existed.
// Clients in controller: [B/2]
ASSERT_EQ(1, controller_->GetClientCount())
<< "Stopping non-existant session 256 should be a no-op.";
ASSERT_EQ(static_cast<int>(kInvalidMediaCaptureSessionId),
controller_->RemoveClient(client_a_route_1, client_a_.get()))
<< "Removing already-removed client A/1 should fail.";
// Clients in controller: [B/2]
ASSERT_EQ(1, controller_->GetClientCount())
<< "Removing non-existant session 200 should be a no-op.";
ASSERT_EQ(400,
controller_->RemoveClient(client_b_route_2, client_b_.get()))
<< "Removing client B/2 should return its session_id.";
// Clients in controller: []
ASSERT_EQ(0, controller_->GetClientCount())
<< "Client count should return to zero after all clients are gone.";
}
static void CacheSyncPoint(uint32* called_release_sync_point,
uint32 release_sync_point) {
*called_release_sync_point = release_sync_point;
}
// This test will connect and disconnect several clients while simulating an
// active capture device being started and generating frames. It runs on one
// thread and is intended to behave deterministically.
TEST_F(VideoCaptureControllerTest, NormalCaptureMultipleClients) {
// VideoCaptureController::ReturnBuffer() uses ImageTransportFactory.
#if !defined(OS_ANDROID)
ImageTransportFactory::InitializeForUnitTests(
scoped_ptr<ImageTransportFactory>(new NoTransportImageTransportFactory));
#endif
media::VideoCaptureParams session_100;
session_100.requested_format = media::VideoCaptureFormat(
gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420);
media::VideoCaptureParams session_200 = session_100;
media::VideoCaptureParams session_300 = session_100;
media::VideoCaptureParams session_1 = session_100;
gfx::Size capture_resolution(444, 200);
// The device format needn't match the VideoCaptureParams (the camera can do
// what it wants). Pick something random.
media::VideoCaptureFormat device_format(
gfx::Size(10, 10), 25, media::PIXEL_FORMAT_RGB24);
const VideoCaptureControllerID client_a_route_1(0xa1a1a1a1);
const VideoCaptureControllerID client_a_route_2(0xa2a2a2a2);
const VideoCaptureControllerID client_b_route_1(0xb1b1b1b1);
const VideoCaptureControllerID client_b_route_2(0xb2b2b2b2);
// Start with two clients.
controller_->AddClient(client_a_route_1,
client_a_.get(),
base::kNullProcessHandle,
100,
session_100);
controller_->AddClient(client_b_route_1,
client_b_.get(),
base::kNullProcessHandle,
300,
session_300);
controller_->AddClient(client_a_route_2,
client_a_.get(),
base::kNullProcessHandle,
200,
session_200);
ASSERT_EQ(3, controller_->GetClientCount());
// Now, simulate an incoming captured buffer from the capture device. As a
// side effect this will cause the first buffer to be shared with clients.
uint8 buffer_no = 1;
scoped_refptr<media::VideoCaptureDevice::Client::Buffer> buffer;
buffer =
device_->ReserveOutputBuffer(media::VideoFrame::I420, capture_resolution);
ASSERT_TRUE(buffer.get());
memset(buffer->data(), buffer_no++, buffer->size());
{
InSequence s;
EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)).Times(1);
EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(1);
}
{
InSequence s;
EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_1)).Times(1);
EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(1);
}
{
InSequence s;
EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1);
EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(1);
}
device_->OnIncomingCapturedVideoFrame(
buffer,
media::VideoCaptureFormat(capture_resolution,
device_format.frame_rate,
media::PIXEL_FORMAT_I420),
WrapI420Buffer(buffer, capture_resolution),
base::TimeTicks());
buffer = NULL;
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(client_a_.get());
Mock::VerifyAndClearExpectations(client_b_.get());
// Second buffer which ought to use the same shared memory buffer. In this
// case pretend that the Buffer pointer is held by the device for a long
// delay. This shouldn't affect anything.
buffer =
device_->ReserveOutputBuffer(media::VideoFrame::I420, capture_resolution);
ASSERT_TRUE(buffer.get());
memset(buffer->data(), buffer_no++, buffer->size());
device_->OnIncomingCapturedVideoFrame(
buffer,
media::VideoCaptureFormat(capture_resolution,
device_format.frame_rate,
media::PIXEL_FORMAT_I420),
WrapI420Buffer(buffer, capture_resolution),
base::TimeTicks());
buffer = NULL;
// The buffer should be delivered to the clients in any order.
EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(1);
EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(1);
EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(1);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(client_a_.get());
Mock::VerifyAndClearExpectations(client_b_.get());
// Add a fourth client now that some buffers have come through.
controller_->AddClient(client_b_route_2,
client_b_.get(),
base::kNullProcessHandle,
1,
session_1);
Mock::VerifyAndClearExpectations(client_b_.get());
// Third, fourth, and fifth buffers. Pretend they all arrive at the same time.
for (int i = 0; i < kPoolSize; i++) {
buffer = device_->ReserveOutputBuffer(media::VideoFrame::I420,
capture_resolution);
ASSERT_TRUE(buffer.get());
memset(buffer->data(), buffer_no++, buffer->size());
device_->OnIncomingCapturedVideoFrame(
buffer,
media::VideoCaptureFormat(capture_resolution,
device_format.frame_rate,
media::PIXEL_FORMAT_I420),
WrapI420Buffer(buffer, capture_resolution),
base::TimeTicks());
buffer = NULL;
}
// ReserveOutputBuffer ought to fail now, because the pool is depleted.
ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::I420,
capture_resolution).get());
// The new client needs to be told of 3 buffers; the old clients only 2.
EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_2)).Times(kPoolSize);
EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(kPoolSize);
EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1))
.Times(kPoolSize - 1);
EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(kPoolSize);
EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2))
.Times(kPoolSize - 1);
EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(kPoolSize);
EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_1))
.Times(kPoolSize - 1);
EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(kPoolSize);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(client_a_.get());
Mock::VerifyAndClearExpectations(client_b_.get());
// Now test the interaction of client shutdown and buffer delivery.
// Kill A1 via renderer disconnect (synchronous).
controller_->RemoveClient(client_a_route_1, client_a_.get());
// Kill B1 via session close (posts a task to disconnect).
EXPECT_CALL(*client_b_, DoEnded(client_b_route_1)).Times(1);
controller_->StopSession(300);
// Queue up another buffer.
buffer =
device_->ReserveOutputBuffer(media::VideoFrame::I420, capture_resolution);
ASSERT_TRUE(buffer.get());
memset(buffer->data(), buffer_no++, buffer->size());
device_->OnIncomingCapturedVideoFrame(
buffer,
media::VideoCaptureFormat(capture_resolution,
device_format.frame_rate,
media::PIXEL_FORMAT_I420),
WrapI420Buffer(buffer, capture_resolution),
base::TimeTicks());
buffer = NULL;
buffer =
device_->ReserveOutputBuffer(media::VideoFrame::I420, capture_resolution);
{
// Kill A2 via session close (posts a task to disconnect, but A2 must not
// be sent either of these two buffers).
EXPECT_CALL(*client_a_, DoEnded(client_a_route_2)).Times(1);
controller_->StopSession(200);
}
ASSERT_TRUE(buffer.get());
memset(buffer->data(), buffer_no++, buffer->size());
device_->OnIncomingCapturedVideoFrame(
buffer,
media::VideoCaptureFormat(capture_resolution,
device_format.frame_rate,
media::PIXEL_FORMAT_I420),
WrapI420Buffer(buffer, capture_resolution),
base::TimeTicks());
buffer = NULL;
// B2 is the only client left, and is the only one that should
// get the buffer.
EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(2);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(client_a_.get());
Mock::VerifyAndClearExpectations(client_b_.get());
// Allocate all buffers from the buffer pool, half as SHM buffer and half as
// mailbox buffers. Make sure of different counts though.
#if defined(OS_ANDROID)
int mailbox_buffers = 0;
#else
int mailbox_buffers = kPoolSize / 2;
GLHelper* gl_helper = ImageTransportFactory::GetInstance()->GetGLHelper();
#endif
int shm_buffers = kPoolSize - mailbox_buffers;
if (shm_buffers == mailbox_buffers) {
shm_buffers--;
mailbox_buffers++;
}
for (int i = 0; i < shm_buffers; ++i) {
buffer = device_->ReserveOutputBuffer(media::VideoFrame::I420,
capture_resolution);
ASSERT_TRUE(buffer.get());
device_->OnIncomingCapturedVideoFrame(
buffer,
media::VideoCaptureFormat(capture_resolution,
device_format.frame_rate,
media::PIXEL_FORMAT_I420),
WrapI420Buffer(buffer, capture_resolution),
base::TimeTicks());
buffer = NULL;
}
std::vector<uint32> mailbox_syncpoints(mailbox_buffers);
std::vector<uint32> release_syncpoints(mailbox_buffers);
for (int i = 0; i < mailbox_buffers; ++i) {
buffer = device_->ReserveOutputBuffer(media::VideoFrame::NATIVE_TEXTURE,
gfx::Size(0, 0));
ASSERT_TRUE(buffer.get());
#if !defined(OS_ANDROID)
mailbox_syncpoints[i] = gl_helper->InsertSyncPoint();
#endif
device_->OnIncomingCapturedVideoFrame(
buffer,
media::VideoCaptureFormat(capture_resolution,
device_format.frame_rate,
media::PIXEL_FORMAT_TEXTURE),
WrapMailboxBuffer(buffer,
make_scoped_ptr(new gpu::MailboxHolder(
gpu::Mailbox(), 0, mailbox_syncpoints[i])),
base::Bind(&CacheSyncPoint, &release_syncpoints[i]),
capture_resolution),
base::TimeTicks());
buffer = NULL;
}
// ReserveOutputBuffers ought to fail now regardless of buffer format, because
// the pool is depleted.
ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::I420,
capture_resolution).get());
ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::NATIVE_TEXTURE,
gfx::Size(0, 0)).get());
EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(shm_buffers);
EXPECT_CALL(*client_b_, DoMailboxBufferReady(client_b_route_2))
.Times(mailbox_buffers);
base::RunLoop().RunUntilIdle();
for (size_t i = 0; i < mailbox_syncpoints.size(); ++i) {
// A new release sync point must be inserted when the video frame is
// returned to the Browser process.
// See: MockVideoCaptureControllerEventHandler::OnMailboxBufferReady() and
// VideoCaptureController::ReturnBuffer()
ASSERT_NE(mailbox_syncpoints[i], release_syncpoints[i]);
}
Mock::VerifyAndClearExpectations(client_b_.get());
#if !defined(OS_ANDROID)
ImageTransportFactory::Terminate();
#endif
}
// Exercises the OnError() codepath of VideoCaptureController, and tests the
// behavior of various operations after the error state has been signalled.
TEST_F(VideoCaptureControllerTest, ErrorBeforeDeviceCreation) {
media::VideoCaptureParams session_100;
session_100.requested_format = media::VideoCaptureFormat(
gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420);
media::VideoCaptureParams session_200 = session_100;
const gfx::Size capture_resolution(320, 240);
const VideoCaptureControllerID route_id(0x99);
// Start with one client.
controller_->AddClient(
route_id, client_a_.get(), base::kNullProcessHandle, 100, session_100);
device_->OnError("Test Error");
EXPECT_CALL(*client_a_, DoError(route_id)).Times(1);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(client_a_.get());
// Second client connects after the error state. It also should get told of
// the error.
EXPECT_CALL(*client_b_, DoError(route_id)).Times(1);
controller_->AddClient(
route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(client_b_.get());
scoped_refptr<media::VideoCaptureDevice::Client::Buffer> buffer =
device_->ReserveOutputBuffer(media::VideoFrame::I420, capture_resolution);
ASSERT_TRUE(buffer.get());
device_->OnIncomingCapturedVideoFrame(
buffer,
media::VideoCaptureFormat(
capture_resolution, 30, media::PIXEL_FORMAT_I420),
WrapI420Buffer(buffer, capture_resolution),
base::TimeTicks());
buffer = NULL;
base::RunLoop().RunUntilIdle();
}
// Exercises the OnError() codepath of VideoCaptureController, and tests the
// behavior of various operations after the error state has been signalled.
TEST_F(VideoCaptureControllerTest, ErrorAfterDeviceCreation) {
media::VideoCaptureParams session_100;
session_100.requested_format = media::VideoCaptureFormat(
gfx::Size(320, 240), 30, media::PIXEL_FORMAT_I420);
media::VideoCaptureParams session_200 = session_100;
const VideoCaptureControllerID route_id(0x99);
// Start with one client.
controller_->AddClient(
route_id, client_a_.get(), base::kNullProcessHandle, 100, session_100);
media::VideoCaptureFormat device_format(
gfx::Size(10, 10), 25, media::PIXEL_FORMAT_ARGB);
// Start the device. Then, before the first buffer, signal an error and
// deliver the buffer. The error should be propagated to clients; the buffer
// should not be.
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(client_a_.get());
const gfx::Size dims(320, 240);
scoped_refptr<media::VideoCaptureDevice::Client::Buffer> buffer =
device_->ReserveOutputBuffer(media::VideoFrame::I420, dims);
ASSERT_TRUE(buffer.get());
device_->OnError("Test error");
device_->OnIncomingCapturedVideoFrame(
buffer,
media::VideoCaptureFormat(
dims, device_format.frame_rate, media::PIXEL_FORMAT_I420),
WrapI420Buffer(buffer, dims),
base::TimeTicks());
buffer = NULL;
EXPECT_CALL(*client_a_, DoError(route_id)).Times(1);
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(client_a_.get());
// Second client connects after the error state. It also should get told of
// the error.
EXPECT_CALL(*client_b_, DoError(route_id)).Times(1);
controller_->AddClient(
route_id, client_b_.get(), base::kNullProcessHandle, 200, session_200);
Mock::VerifyAndClearExpectations(client_b_.get());
}
TEST_F(VideoCaptureControllerTest, DataCaptureInEachVideoFormatInSequence) {
// This Test will skip PIXEL_FORMAT_TEXTURE and PIXEL_FORMAT_UNKNOWN
for (int format = 0; format < media::PIXEL_FORMAT_TEXTURE; ++format) {
media::VideoCaptureParams params;
params.requested_format = media::VideoCaptureFormat(
gfx::Size(320, 240), 30, media::VideoPixelFormat(format));
const gfx::Size capture_resolution(320, 240);
const VideoCaptureControllerID route(0x99);
// Start with one client.
controller_->AddClient(route,
client_a_.get(),
base::kNullProcessHandle,
100,
params);
ASSERT_EQ(1, controller_->GetClientCount());
// Now, simulate an incoming captured buffer from the capture device.
scoped_refptr<media::VideoCaptureDevice::Client::Buffer> buffer;
buffer =
device_->ReserveOutputBuffer(media::VideoFrame::I420,
capture_resolution);
ASSERT_TRUE(buffer.get());
// Captured a new video frame.
device_->OnIncomingCapturedData(
static_cast<unsigned char*>(buffer.get()->data()),
buffer.get()->size(),
params.requested_format,
0,
base::TimeTicks());
}
}
} // namespace content
|