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
3 // found in the LICENSE file.
5 #include "content/browser/media/capture/web_contents_video_capture_device.h"
7 #include "base/bind_helpers.h"
8 #include "base/debug/debugger.h"
9 #include "base/run_loop.h"
10 #include "base/test/test_timeouts.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/media/capture/video_capture_oracle.h"
15 #include "content/browser/media/capture/web_contents_capture_util.h"
16 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h"
17 #include "content/browser/renderer_host/render_view_host_factory.h"
18 #include "content/browser/renderer_host/render_widget_host_impl.h"
19 #include "content/port/browser/render_widget_host_view_frame_subscriber.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/test/mock_render_process_host.h"
23 #include "content/public/test/test_browser_context.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "content/public/test/test_utils.h"
26 #include "content/test/test_render_view_host.h"
27 #include "content/test/test_web_contents.h"
28 #include "media/base/video_frame.h"
29 #include "media/base/video_util.h"
30 #include "media/base/yuv_convert.h"
31 #include "media/video/capture/video_capture_types.h"
32 #include "skia/ext/platform_canvas.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "third_party/skia/include/core/SkColor.h"
39 const int kTestWidth
= 320;
40 const int kTestHeight
= 240;
41 const int kTestFramesPerSecond
= 20;
42 const SkColor kNothingYet
= 0xdeadbeef;
43 const SkColor kNotInterested
= ~kNothingYet
;
45 void DeadlineExceeded(base::Closure quit_closure
) {
46 if (!base::debug::BeingDebugged()) {
48 FAIL() << "Deadline exceeded while waiting, quitting";
50 LOG(WARNING
) << "Deadline exceeded; test would fail if debugger weren't "
55 void RunCurrentLoopWithDeadline() {
56 base::Timer
deadline(false, false);
57 deadline
.Start(FROM_HERE
, TestTimeouts::action_max_timeout(), base::Bind(
58 &DeadlineExceeded
, base::MessageLoop::current()->QuitClosure()));
59 base::MessageLoop::current()->Run();
63 SkColor
ConvertRgbToYuv(SkColor rgb
) {
65 media::ConvertRGB32ToYUV(reinterpret_cast<uint8
*>(&rgb
),
66 yuv
, yuv
+ 1, yuv
+ 2, 1, 1, 1, 1, 1);
67 return SkColorSetRGB(yuv
[0], yuv
[1], yuv
[2]);
70 // Thread-safe class that controls the source pattern to be captured by the
71 // system under test. The lifetime of this class is greater than the lifetime
72 // of all objects that reference it, so it does not need to be reference
74 class CaptureTestSourceController
{
77 CaptureTestSourceController()
78 : color_(SK_ColorMAGENTA
),
79 copy_result_size_(kTestWidth
, kTestHeight
),
80 can_copy_to_video_frame_(false),
81 use_frame_subscriber_(false) {}
83 void SetSolidColor(SkColor color
) {
84 base::AutoLock
guard(lock_
);
88 SkColor
GetSolidColor() {
89 base::AutoLock
guard(lock_
);
93 void SetCopyResultSize(int width
, int height
) {
94 base::AutoLock
guard(lock_
);
95 copy_result_size_
= gfx::Size(width
, height
);
98 gfx::Size
GetCopyResultSize() {
99 base::AutoLock
guard(lock_
);
100 return copy_result_size_
;
104 // TODO(nick): This actually should always be happening on the UI thread.
105 base::AutoLock
guard(lock_
);
106 if (!copy_done_
.is_null()) {
107 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, copy_done_
);
112 void SetCanCopyToVideoFrame(bool value
) {
113 base::AutoLock
guard(lock_
);
114 can_copy_to_video_frame_
= value
;
117 bool CanCopyToVideoFrame() {
118 base::AutoLock
guard(lock_
);
119 return can_copy_to_video_frame_
;
122 void SetUseFrameSubscriber(bool value
) {
123 base::AutoLock
guard(lock_
);
124 use_frame_subscriber_
= value
;
127 bool CanUseFrameSubscriber() {
128 base::AutoLock
guard(lock_
);
129 return use_frame_subscriber_
;
132 void WaitForNextCopy() {
134 base::AutoLock
guard(lock_
);
135 copy_done_
= base::MessageLoop::current()->QuitClosure();
138 RunCurrentLoopWithDeadline();
142 base::Lock lock_
; // Guards changes to all members.
144 gfx::Size copy_result_size_
;
145 bool can_copy_to_video_frame_
;
146 bool use_frame_subscriber_
;
147 base::Closure copy_done_
;
149 DISALLOW_COPY_AND_ASSIGN(CaptureTestSourceController
);
152 // A stub implementation which returns solid-color bitmaps in calls to
153 // CopyFromCompositingSurfaceToVideoFrame(), and which allows the video-frame
154 // readback path to be switched on and off. The behavior is controlled by a
155 // CaptureTestSourceController.
156 class CaptureTestView
: public TestRenderWidgetHostView
{
158 explicit CaptureTestView(RenderWidgetHostImpl
* rwh
,
159 CaptureTestSourceController
* controller
)
160 : TestRenderWidgetHostView(rwh
),
161 controller_(controller
) {}
163 virtual ~CaptureTestView() {}
165 // TestRenderWidgetHostView overrides.
166 virtual gfx::Rect
GetViewBounds() const OVERRIDE
{
167 return gfx::Rect(100, 100, 100 + kTestWidth
, 100 + kTestHeight
);
170 virtual bool CanCopyToVideoFrame() const OVERRIDE
{
171 return controller_
->CanCopyToVideoFrame();
174 virtual void CopyFromCompositingSurfaceToVideoFrame(
175 const gfx::Rect
& src_subrect
,
176 const scoped_refptr
<media::VideoFrame
>& target
,
177 const base::Callback
<void(bool)>& callback
) OVERRIDE
{
178 SkColor c
= ConvertRgbToYuv(controller_
->GetSolidColor());
180 target
.get(), SkColorGetR(c
), SkColorGetG(c
), SkColorGetB(c
));
182 controller_
->SignalCopy();
185 virtual void BeginFrameSubscription(
186 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
) OVERRIDE
{
187 subscriber_
.reset(subscriber
.release());
190 virtual void EndFrameSubscription() OVERRIDE
{
194 // Simulate a compositor paint event for our subscriber.
195 void SimulateUpdate() {
196 const base::TimeTicks present_time
= base::TimeTicks::Now();
197 RenderWidgetHostViewFrameSubscriber::DeliverFrameCallback callback
;
198 scoped_refptr
<media::VideoFrame
> target
;
199 if (subscriber_
&& subscriber_
->ShouldCaptureFrame(present_time
,
200 &target
, &callback
)) {
201 SkColor c
= ConvertRgbToYuv(controller_
->GetSolidColor());
203 target
.get(), SkColorGetR(c
), SkColorGetG(c
), SkColorGetB(c
));
204 BrowserThread::PostTask(BrowserThread::UI
,
206 base::Bind(callback
, present_time
, true));
207 controller_
->SignalCopy();
212 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber_
;
213 CaptureTestSourceController
* const controller_
;
215 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestView
);
218 #if defined(COMPILER_MSVC)
219 // MSVC warns on diamond inheritance. See comment for same warning on
220 // RenderViewHostImpl.
221 #pragma warning(push)
222 #pragma warning(disable: 4250)
225 // A stub implementation which returns solid-color bitmaps in calls to
226 // CopyFromBackingStore(). The behavior is controlled by a
227 // CaptureTestSourceController.
228 class CaptureTestRenderViewHost
: public TestRenderViewHost
{
230 CaptureTestRenderViewHost(SiteInstance
* instance
,
231 RenderViewHostDelegate
* delegate
,
232 RenderWidgetHostDelegate
* widget_delegate
,
234 int main_frame_routing_id
,
236 CaptureTestSourceController
* controller
)
237 : TestRenderViewHost(instance
, delegate
, widget_delegate
, routing_id
,
238 main_frame_routing_id
, swapped_out
),
239 controller_(controller
) {
240 // Override the default view installed by TestRenderViewHost; we need
241 // our special subclass which has mocked-out tab capture support.
242 RenderWidgetHostView
* old_view
= GetView();
243 SetView(new CaptureTestView(this, controller
));
247 // TestRenderViewHost overrides.
248 virtual void CopyFromBackingStore(
249 const gfx::Rect
& src_rect
,
250 const gfx::Size
& accelerated_dst_size
,
251 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
252 const SkBitmap::Config
& bitmap_config
) OVERRIDE
{
253 gfx::Size size
= controller_
->GetCopyResultSize();
254 SkColor color
= controller_
->GetSolidColor();
256 // Although it's not necessary, use a PlatformBitmap here (instead of a
257 // regular SkBitmap) to exercise possible threading issues.
258 skia::PlatformBitmap output
;
259 EXPECT_TRUE(output
.Allocate(size
.width(), size
.height(), false));
261 SkAutoLockPixels
locker(output
.GetBitmap());
262 output
.GetBitmap().eraseColor(color
);
264 callback
.Run(true, output
.GetBitmap());
265 controller_
->SignalCopy();
269 CaptureTestSourceController
* controller_
;
271 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHost
);
274 #if defined(COMPILER_MSVC)
275 // Re-enable warning 4250
279 class CaptureTestRenderViewHostFactory
: public RenderViewHostFactory
{
281 explicit CaptureTestRenderViewHostFactory(
282 CaptureTestSourceController
* controller
) : controller_(controller
) {
283 RegisterFactory(this);
286 virtual ~CaptureTestRenderViewHostFactory() {
290 // RenderViewHostFactory implementation.
291 virtual RenderViewHost
* CreateRenderViewHost(
292 SiteInstance
* instance
,
293 RenderViewHostDelegate
* delegate
,
294 RenderWidgetHostDelegate
* widget_delegate
,
296 int main_frame_routing_id
,
297 bool swapped_out
) OVERRIDE
{
298 return new CaptureTestRenderViewHost(instance
, delegate
, widget_delegate
,
299 routing_id
, main_frame_routing_id
,
300 swapped_out
, controller_
);
303 CaptureTestSourceController
* controller_
;
305 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHostFactory
);
308 // A stub consumer of captured video frames, which checks the output of
309 // WebContentsVideoCaptureDevice.
310 class StubClient
: public media::VideoCaptureDevice::Client
{
312 StubClient(const base::Callback
<void(SkColor
)>& color_callback
,
313 const base::Closure
& error_callback
)
314 : color_callback_(color_callback
),
315 error_callback_(error_callback
) {
316 buffer_pool_
= new VideoCaptureBufferPool(2);
318 virtual ~StubClient() {}
320 virtual scoped_refptr
<media::VideoCaptureDevice::Client::Buffer
>
321 ReserveOutputBuffer(media::VideoFrame::Format format
,
322 const gfx::Size
& dimensions
) OVERRIDE
{
323 CHECK_EQ(format
, media::VideoFrame::I420
);
324 const size_t frame_bytes
=
325 media::VideoFrame::AllocationSize(media::VideoFrame::I420
, dimensions
);
326 int buffer_id_to_drop
= VideoCaptureBufferPool::kInvalidId
; // Ignored.
328 buffer_pool_
->ReserveForProducer(frame_bytes
, &buffer_id_to_drop
);
329 if (buffer_id
== VideoCaptureBufferPool::kInvalidId
)
333 buffer_pool_
->GetBufferInfo(buffer_id
, &data
, &size
);
334 return scoped_refptr
<media::VideoCaptureDevice::Client::Buffer
>(
335 new PoolBuffer(buffer_pool_
, buffer_id
, data
, size
));
338 virtual void OnIncomingCapturedData(
341 const media::VideoCaptureFormat
& frame_format
,
343 base::TimeTicks timestamp
) OVERRIDE
{
347 virtual void OnIncomingCapturedVideoFrame(
348 const scoped_refptr
<Buffer
>& buffer
,
349 const media::VideoCaptureFormat
& buffer_format
,
350 const scoped_refptr
<media::VideoFrame
>& frame
,
351 base::TimeTicks timestamp
) OVERRIDE
{
352 EXPECT_EQ(gfx::Size(kTestWidth
, kTestHeight
), buffer_format
.frame_size
);
353 EXPECT_EQ(media::PIXEL_FORMAT_I420
, buffer_format
.pixel_format
);
354 EXPECT_EQ(media::VideoFrame::I420
, frame
->format());
356 for (int plane
= 0; plane
< 3; ++plane
)
357 yuv
[plane
] = frame
->data(plane
)[0];
358 // TODO(nick): We just look at the first pixel presently, because if
359 // the analysis is too slow, the backlog of frames will grow without bound
360 // and trouble erupts. http://crbug.com/174519
361 color_callback_
.Run((SkColorSetRGB(yuv
[0], yuv
[1], yuv
[2])));
364 virtual void OnError(const std::string
& reason
) OVERRIDE
{
365 error_callback_
.Run();
369 class PoolBuffer
: public media::VideoCaptureDevice::Client::Buffer
{
371 PoolBuffer(const scoped_refptr
<VideoCaptureBufferPool
>& pool
,
375 : Buffer(buffer_id
, data
, size
), pool_(pool
) {}
378 virtual ~PoolBuffer() { pool_
->RelinquishProducerReservation(id()); }
379 const scoped_refptr
<VideoCaptureBufferPool
> pool_
;
382 scoped_refptr
<VideoCaptureBufferPool
> buffer_pool_
;
383 base::Callback
<void(SkColor
)> color_callback_
;
384 base::Closure error_callback_
;
386 DISALLOW_COPY_AND_ASSIGN(StubClient
);
389 class StubClientObserver
{
392 : error_encountered_(false),
393 wait_color_yuv_(0xcafe1950) {
394 client_
.reset(new StubClient(
395 base::Bind(&StubClientObserver::OnColor
, base::Unretained(this)),
396 base::Bind(&StubClientObserver::OnError
, base::Unretained(this))));
399 virtual ~StubClientObserver() {}
401 scoped_ptr
<media::VideoCaptureDevice::Client
> PassClient() {
402 return client_
.PassAs
<media::VideoCaptureDevice::Client
>();
405 void QuitIfConditionMet(SkColor color
) {
406 base::AutoLock
guard(lock_
);
407 if (wait_color_yuv_
== color
|| error_encountered_
)
408 base::MessageLoop::current()->Quit();
411 void WaitForNextColor(SkColor expected_color
) {
413 base::AutoLock
guard(lock_
);
414 wait_color_yuv_
= ConvertRgbToYuv(expected_color
);
415 error_encountered_
= false;
417 RunCurrentLoopWithDeadline();
419 base::AutoLock
guard(lock_
);
420 ASSERT_FALSE(error_encountered_
);
424 void WaitForError() {
426 base::AutoLock
guard(lock_
);
427 wait_color_yuv_
= kNotInterested
;
428 error_encountered_
= false;
430 RunCurrentLoopWithDeadline();
432 base::AutoLock
guard(lock_
);
433 ASSERT_TRUE(error_encountered_
);
438 base::AutoLock
guard(lock_
);
439 return error_encountered_
;
444 base::AutoLock
guard(lock_
);
445 error_encountered_
= true;
447 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, base::Bind(
448 &StubClientObserver::QuitIfConditionMet
,
449 base::Unretained(this),
453 void OnColor(SkColor color
) {
454 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, base::Bind(
455 &StubClientObserver::QuitIfConditionMet
,
456 base::Unretained(this),
462 bool error_encountered_
;
463 SkColor wait_color_yuv_
;
464 scoped_ptr
<StubClient
> client_
;
466 DISALLOW_COPY_AND_ASSIGN(StubClientObserver
);
469 // Test harness that sets up a minimal environment with necessary stubs.
470 class WebContentsVideoCaptureDeviceTest
: public testing::Test
{
472 // This is public because C++ method pointer scoping rules are silly and make
473 // this hard to use with Bind().
474 void ResetWebContents() {
475 web_contents_
.reset();
479 virtual void SetUp() {
480 // TODO(nick): Sadness and woe! Much "mock-the-world" boilerplate could be
481 // eliminated here, if only we could use RenderViewHostTestHarness. The
482 // catch is that we need our TestRenderViewHost to support a
483 // CopyFromBackingStore operation that we control. To accomplish that,
484 // either RenderViewHostTestHarness would have to support installing a
485 // custom RenderViewHostFactory, or else we implant some kind of delegated
486 // CopyFromBackingStore functionality into TestRenderViewHost itself.
488 render_process_host_factory_
.reset(new MockRenderProcessHostFactory());
489 // Create our (self-registering) RVH factory, so that when we create a
490 // WebContents, it in turn creates CaptureTestRenderViewHosts.
491 render_view_host_factory_
.reset(
492 new CaptureTestRenderViewHostFactory(&controller_
));
494 browser_context_
.reset(new TestBrowserContext());
496 scoped_refptr
<SiteInstance
> site_instance
=
497 SiteInstance::Create(browser_context_
.get());
498 SiteInstanceImpl::set_render_process_host_factory(
499 render_process_host_factory_
.get());
501 TestWebContents::Create(browser_context_
.get(), site_instance
.get()));
503 // This is actually a CaptureTestRenderViewHost.
504 RenderWidgetHostImpl
* rwh
=
505 RenderWidgetHostImpl::From(web_contents_
->GetRenderViewHost());
507 std::string device_id
=
508 WebContentsCaptureUtil::AppendWebContentsDeviceScheme(
509 base::StringPrintf("%d:%d", rwh
->GetProcess()->GetID(),
510 rwh
->GetRoutingID()));
512 device_
.reset(WebContentsVideoCaptureDevice::Create(device_id
));
514 base::RunLoop().RunUntilIdle();
517 virtual void TearDown() {
518 // Tear down in opposite order of set-up.
520 // The device is destroyed asynchronously, and will notify the
521 // CaptureTestSourceController when it finishes destruction.
522 // Trigger this, and wait.
524 device_
->StopAndDeAllocate();
528 base::RunLoop().RunUntilIdle();
530 // Destroy the browser objects.
531 web_contents_
.reset();
532 browser_context_
.reset();
534 base::RunLoop().RunUntilIdle();
536 SiteInstanceImpl::set_render_process_host_factory(NULL
);
537 render_view_host_factory_
.reset();
538 render_process_host_factory_
.reset();
542 CaptureTestSourceController
* source() { return &controller_
; }
543 media::VideoCaptureDevice
* device() { return device_
.get(); }
545 void SimulateDrawEvent() {
546 if (source()->CanUseFrameSubscriber()) {
548 CaptureTestView
* test_view
= static_cast<CaptureTestView
*>(
549 web_contents_
->GetRenderViewHost()->GetView());
550 test_view
->SimulateUpdate();
552 // Simulate a non-accelerated paint.
553 NotificationService::current()->Notify(
554 NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE
,
555 Source
<RenderWidgetHost
>(web_contents_
->GetRenderViewHost()),
556 NotificationService::NoDetails());
560 void DestroyVideoCaptureDevice() { device_
.reset(); }
562 StubClientObserver
* client_observer() {
563 return &client_observer_
;
567 StubClientObserver client_observer_
;
569 // The controller controls which pixel patterns to produce.
570 CaptureTestSourceController controller_
;
572 // Self-registering RenderProcessHostFactory.
573 scoped_ptr
<MockRenderProcessHostFactory
> render_process_host_factory_
;
575 // Creates capture-capable RenderViewHosts whose pixel content production is
576 // under the control of |controller_|.
577 scoped_ptr
<CaptureTestRenderViewHostFactory
> render_view_host_factory_
;
579 // A mocked-out browser and tab.
580 scoped_ptr
<TestBrowserContext
> browser_context_
;
581 scoped_ptr
<WebContents
> web_contents_
;
583 // Finally, the WebContentsVideoCaptureDevice under test.
584 scoped_ptr
<media::VideoCaptureDevice
> device_
;
586 TestBrowserThreadBundle thread_bundle_
;
589 TEST_F(WebContentsVideoCaptureDeviceTest
, InvalidInitialWebContentsError
) {
590 // Before the installs itself on the UI thread up to start capturing, we'll
591 // delete the web contents. This should trigger an error which can happen in
592 // practice; we should be able to recover gracefully.
595 media::VideoCaptureParams capture_params
;
596 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
597 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
598 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
599 capture_params
.allow_resolution_change
= false;
600 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
601 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
602 device()->StopAndDeAllocate();
605 TEST_F(WebContentsVideoCaptureDeviceTest
, WebContentsDestroyed
) {
606 // We'll simulate the tab being closed after the capture pipeline is up and
608 media::VideoCaptureParams capture_params
;
609 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
610 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
611 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
612 capture_params
.allow_resolution_change
= false;
613 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
614 // Do one capture to prove
615 source()->SetSolidColor(SK_ColorRED
);
617 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED
));
619 base::RunLoop().RunUntilIdle();
621 // Post a task to close the tab. We should see an error reported to the
623 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
624 base::Bind(&WebContentsVideoCaptureDeviceTest::ResetWebContents
,
625 base::Unretained(this)));
626 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
627 device()->StopAndDeAllocate();
630 TEST_F(WebContentsVideoCaptureDeviceTest
,
631 StopDeviceBeforeCaptureMachineCreation
) {
632 media::VideoCaptureParams capture_params
;
633 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
634 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
635 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
636 capture_params
.allow_resolution_change
= false;
637 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
639 // Make a point of not running the UI messageloop here.
640 device()->StopAndDeAllocate();
641 DestroyVideoCaptureDevice();
643 // Currently, there should be CreateCaptureMachineOnUIThread() and
644 // DestroyCaptureMachineOnUIThread() tasks pending on the current (UI) message
645 // loop. These should both succeed without crashing, and the machine should
646 // wind up in the idle state.
647 base::RunLoop().RunUntilIdle();
650 TEST_F(WebContentsVideoCaptureDeviceTest
, StopWithRendererWorkToDo
) {
651 // Set up the test to use RGB copies and an normal
652 source()->SetCanCopyToVideoFrame(false);
653 source()->SetUseFrameSubscriber(false);
654 media::VideoCaptureParams capture_params
;
655 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
656 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
657 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
658 capture_params
.allow_resolution_change
= false;
659 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
661 base::RunLoop().RunUntilIdle();
663 for (int i
= 0; i
< 10; ++i
)
666 ASSERT_FALSE(client_observer()->HasError());
667 device()->StopAndDeAllocate();
668 ASSERT_FALSE(client_observer()->HasError());
669 base::RunLoop().RunUntilIdle();
670 ASSERT_FALSE(client_observer()->HasError());
673 TEST_F(WebContentsVideoCaptureDeviceTest
, DeviceRestart
) {
674 media::VideoCaptureParams capture_params
;
675 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
676 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
677 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
678 capture_params
.allow_resolution_change
= false;
679 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
680 base::RunLoop().RunUntilIdle();
681 source()->SetSolidColor(SK_ColorRED
);
684 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED
));
687 source()->SetSolidColor(SK_ColorGREEN
);
689 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN
));
690 device()->StopAndDeAllocate();
692 // Device is stopped, but content can still be animating.
695 base::RunLoop().RunUntilIdle();
697 StubClientObserver observer2
;
698 device()->AllocateAndStart(capture_params
, observer2
.PassClient());
699 source()->SetSolidColor(SK_ColorBLUE
);
701 ASSERT_NO_FATAL_FAILURE(observer2
.WaitForNextColor(SK_ColorBLUE
));
702 source()->SetSolidColor(SK_ColorYELLOW
);
704 ASSERT_NO_FATAL_FAILURE(observer2
.WaitForNextColor(SK_ColorYELLOW
));
705 device()->StopAndDeAllocate();
708 // The "happy case" test. No scaling is needed, so we should be able to change
709 // the picture emitted from the source and expect to see each delivered to the
710 // consumer. The test will alternate between the three capture paths, simulating
711 // falling in and out of accelerated compositing.
712 TEST_F(WebContentsVideoCaptureDeviceTest
, GoesThroughAllTheMotions
) {
713 media::VideoCaptureParams capture_params
;
714 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
715 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
716 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
717 capture_params
.allow_resolution_change
= false;
718 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
720 for (int i
= 0; i
< 6; i
++) {
721 const char* name
= NULL
;
724 source()->SetCanCopyToVideoFrame(true);
725 source()->SetUseFrameSubscriber(false);
729 source()->SetCanCopyToVideoFrame(false);
730 source()->SetUseFrameSubscriber(true);
734 source()->SetCanCopyToVideoFrame(false);
735 source()->SetUseFrameSubscriber(false);
742 SCOPED_TRACE(base::StringPrintf("Using %s path, iteration #%d", name
, i
));
744 source()->SetSolidColor(SK_ColorRED
);
746 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED
));
748 source()->SetSolidColor(SK_ColorGREEN
);
750 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN
));
752 source()->SetSolidColor(SK_ColorBLUE
);
754 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLUE
));
756 source()->SetSolidColor(SK_ColorBLACK
);
758 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLACK
));
760 device()->StopAndDeAllocate();
763 TEST_F(WebContentsVideoCaptureDeviceTest
, RejectsInvalidAllocateParams
) {
764 media::VideoCaptureParams capture_params
;
765 capture_params
.requested_format
.frame_size
.SetSize(1280, 720);
766 capture_params
.requested_format
.frame_rate
= -2;
767 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
768 capture_params
.allow_resolution_change
= false;
769 BrowserThread::PostTask(
772 base::Bind(&media::VideoCaptureDevice::AllocateAndStart
,
773 base::Unretained(device()),
775 base::Passed(client_observer()->PassClient())));
776 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
777 BrowserThread::PostTask(
780 base::Bind(&media::VideoCaptureDevice::StopAndDeAllocate
,
781 base::Unretained(device())));
782 base::RunLoop().RunUntilIdle();
785 TEST_F(WebContentsVideoCaptureDeviceTest
, BadFramesGoodFrames
) {
786 media::VideoCaptureParams capture_params
;
787 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
788 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
789 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
790 capture_params
.allow_resolution_change
= false;
791 // 1x1 is too small to process; we intend for this to result in an error.
792 source()->SetCopyResultSize(1, 1);
793 source()->SetSolidColor(SK_ColorRED
);
794 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
796 // These frames ought to be dropped during the Render stage. Let
797 // several captures to happen.
798 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
799 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
800 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
801 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
802 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
804 // Now push some good frames through; they should be processed normally.
805 source()->SetCopyResultSize(kTestWidth
, kTestHeight
);
806 source()->SetSolidColor(SK_ColorGREEN
);
807 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN
));
808 source()->SetSolidColor(SK_ColorRED
);
809 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED
));
811 device()->StopAndDeAllocate();
815 } // namespace content