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/public/browser/notification_service.h"
20 #include "content/public/browser/notification_types.h"
21 #include "content/public/browser/render_widget_host_view_frame_subscriber.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_capture_types.h"
29 #include "media/base/video_frame.h"
30 #include "media/base/video_util.h"
31 #include "media/base/yuv_convert.h"
32 #include "skia/ext/platform_canvas.h"
33 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "third_party/skia/include/core/SkColor.h"
36 #include "ui/gfx/display.h"
37 #include "ui/gfx/screen.h"
42 const int kTestWidth
= 320;
43 const int kTestHeight
= 240;
44 const int kTestFramesPerSecond
= 20;
45 const float kTestDeviceScaleFactor
= 2.0f
;
46 const SkColor kNothingYet
= 0xdeadbeef;
47 const SkColor kNotInterested
= ~kNothingYet
;
49 void DeadlineExceeded(base::Closure quit_closure
) {
50 if (!base::debug::BeingDebugged()) {
52 FAIL() << "Deadline exceeded while waiting, quitting";
54 LOG(WARNING
) << "Deadline exceeded; test would fail if debugger weren't "
59 void RunCurrentLoopWithDeadline() {
60 base::Timer
deadline(false, false);
61 deadline
.Start(FROM_HERE
, TestTimeouts::action_max_timeout(), base::Bind(
62 &DeadlineExceeded
, base::MessageLoop::current()->QuitClosure()));
63 base::MessageLoop::current()->Run();
67 SkColor
ConvertRgbToYuv(SkColor rgb
) {
69 media::ConvertRGB32ToYUV(reinterpret_cast<uint8
*>(&rgb
),
70 yuv
, yuv
+ 1, yuv
+ 2, 1, 1, 1, 1, 1);
71 return SkColorSetRGB(yuv
[0], yuv
[1], yuv
[2]);
74 // Thread-safe class that controls the source pattern to be captured by the
75 // system under test. The lifetime of this class is greater than the lifetime
76 // of all objects that reference it, so it does not need to be reference
78 class CaptureTestSourceController
{
81 CaptureTestSourceController()
82 : color_(SK_ColorMAGENTA
),
83 copy_result_size_(kTestWidth
, kTestHeight
),
84 can_copy_to_video_frame_(false),
85 use_frame_subscriber_(false) {}
87 void SetSolidColor(SkColor color
) {
88 base::AutoLock
guard(lock_
);
92 SkColor
GetSolidColor() {
93 base::AutoLock
guard(lock_
);
97 void SetCopyResultSize(int width
, int height
) {
98 base::AutoLock
guard(lock_
);
99 copy_result_size_
= gfx::Size(width
, height
);
102 gfx::Size
GetCopyResultSize() {
103 base::AutoLock
guard(lock_
);
104 return copy_result_size_
;
108 // TODO(nick): This actually should always be happening on the UI thread.
109 base::AutoLock
guard(lock_
);
110 if (!copy_done_
.is_null()) {
111 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, copy_done_
);
116 void SetCanCopyToVideoFrame(bool value
) {
117 base::AutoLock
guard(lock_
);
118 can_copy_to_video_frame_
= value
;
121 bool CanCopyToVideoFrame() {
122 base::AutoLock
guard(lock_
);
123 return can_copy_to_video_frame_
;
126 void SetUseFrameSubscriber(bool value
) {
127 base::AutoLock
guard(lock_
);
128 use_frame_subscriber_
= value
;
131 bool CanUseFrameSubscriber() {
132 base::AutoLock
guard(lock_
);
133 return use_frame_subscriber_
;
136 void WaitForNextCopy() {
138 base::AutoLock
guard(lock_
);
139 copy_done_
= base::MessageLoop::current()->QuitClosure();
142 RunCurrentLoopWithDeadline();
146 base::Lock lock_
; // Guards changes to all members.
148 gfx::Size copy_result_size_
;
149 bool can_copy_to_video_frame_
;
150 bool use_frame_subscriber_
;
151 base::Closure copy_done_
;
153 DISALLOW_COPY_AND_ASSIGN(CaptureTestSourceController
);
156 // A stub implementation which returns solid-color bitmaps in calls to
157 // CopyFromCompositingSurfaceToVideoFrame(), and which allows the video-frame
158 // readback path to be switched on and off. The behavior is controlled by a
159 // CaptureTestSourceController.
160 class CaptureTestView
: public TestRenderWidgetHostView
{
162 explicit CaptureTestView(RenderWidgetHostImpl
* rwh
,
163 CaptureTestSourceController
* controller
)
164 : TestRenderWidgetHostView(rwh
),
165 controller_(controller
) {}
167 ~CaptureTestView() override
{}
169 // TestRenderWidgetHostView overrides.
170 gfx::Rect
GetViewBounds() const override
{
171 return gfx::Rect(100, 100, 100 + kTestWidth
, 100 + kTestHeight
);
174 bool CanCopyToVideoFrame() const override
{
175 return controller_
->CanCopyToVideoFrame();
178 void CopyFromCompositingSurfaceToVideoFrame(
179 const gfx::Rect
& src_subrect
,
180 const scoped_refptr
<media::VideoFrame
>& target
,
181 const base::Callback
<void(bool)>& callback
) override
{
182 SkColor c
= ConvertRgbToYuv(controller_
->GetSolidColor());
184 target
.get(), SkColorGetR(c
), SkColorGetG(c
), SkColorGetB(c
));
186 controller_
->SignalCopy();
189 void BeginFrameSubscription(
190 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
) override
{
191 subscriber_
.reset(subscriber
.release());
194 void EndFrameSubscription() override
{ subscriber_
.reset(); }
196 // Simulate a compositor paint event for our subscriber.
197 void SimulateUpdate() {
198 const base::TimeTicks present_time
= base::TimeTicks::Now();
199 RenderWidgetHostViewFrameSubscriber::DeliverFrameCallback callback
;
200 scoped_refptr
<media::VideoFrame
> target
;
201 if (subscriber_
&& subscriber_
->ShouldCaptureFrame(
202 gfx::Rect(), present_time
, &target
, &callback
)) {
203 SkColor c
= ConvertRgbToYuv(controller_
->GetSolidColor());
205 target
.get(), SkColorGetR(c
), SkColorGetG(c
), SkColorGetB(c
));
206 BrowserThread::PostTask(BrowserThread::UI
,
208 base::Bind(callback
, present_time
, true));
209 controller_
->SignalCopy();
214 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber_
;
215 CaptureTestSourceController
* const controller_
;
217 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestView
);
220 #if defined(COMPILER_MSVC)
221 // MSVC warns on diamond inheritance. See comment for same warning on
222 // RenderViewHostImpl.
223 #pragma warning(push)
224 #pragma warning(disable: 4250)
227 // A stub implementation which returns solid-color bitmaps in calls to
228 // CopyFromBackingStore(). The behavior is controlled by a
229 // CaptureTestSourceController.
230 class CaptureTestRenderViewHost
: public TestRenderViewHost
{
232 CaptureTestRenderViewHost(SiteInstance
* instance
,
233 RenderViewHostDelegate
* delegate
,
234 RenderWidgetHostDelegate
* widget_delegate
,
236 int main_frame_routing_id
,
238 CaptureTestSourceController
* controller
)
239 : TestRenderViewHost(instance
, delegate
, widget_delegate
, routing_id
,
240 main_frame_routing_id
, swapped_out
),
241 controller_(controller
) {
242 // Override the default view installed by TestRenderViewHost; we need
243 // our special subclass which has mocked-out tab capture support.
244 RenderWidgetHostView
* old_view
= GetView();
245 SetView(new CaptureTestView(this, controller
));
249 // TestRenderViewHost overrides.
250 void CopyFromBackingStore(const gfx::Rect
& src_rect
,
251 const gfx::Size
& accelerated_dst_size
,
252 ReadbackRequestCallback
& callback
,
253 const SkColorType color_type
) override
{
254 gfx::Size size
= controller_
->GetCopyResultSize();
255 SkColor color
= controller_
->GetSolidColor();
257 // Although it's not necessary, use a PlatformBitmap here (instead of a
258 // regular SkBitmap) to exercise possible threading issues.
259 skia::PlatformBitmap output
;
260 EXPECT_TRUE(output
.Allocate(size
.width(), size
.height(), false));
262 SkAutoLockPixels
locker(output
.GetBitmap());
263 output
.GetBitmap().eraseColor(color
);
265 callback
.Run(output
.GetBitmap(), content::READBACK_SUCCESS
);
266 controller_
->SignalCopy();
270 CaptureTestSourceController
* controller_
;
272 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHost
);
275 #if defined(COMPILER_MSVC)
276 // Re-enable warning 4250
280 class CaptureTestRenderViewHostFactory
: public RenderViewHostFactory
{
282 explicit CaptureTestRenderViewHostFactory(
283 CaptureTestSourceController
* controller
) : controller_(controller
) {
284 RegisterFactory(this);
287 ~CaptureTestRenderViewHostFactory() override
{ UnregisterFactory(); }
289 // RenderViewHostFactory implementation.
290 RenderViewHost
* CreateRenderViewHost(
291 SiteInstance
* instance
,
292 RenderViewHostDelegate
* delegate
,
293 RenderWidgetHostDelegate
* widget_delegate
,
295 int main_frame_routing_id
,
296 bool swapped_out
) override
{
297 return new CaptureTestRenderViewHost(instance
, delegate
, widget_delegate
,
298 routing_id
, main_frame_routing_id
,
299 swapped_out
, controller_
);
302 CaptureTestSourceController
* controller_
;
304 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHostFactory
);
307 // A stub consumer of captured video frames, which checks the output of
308 // WebContentsVideoCaptureDevice.
309 class StubClient
: public media::VideoCaptureDevice::Client
{
311 StubClient(const base::Callback
<void(SkColor
)>& color_callback
,
312 const base::Closure
& error_callback
)
313 : color_callback_(color_callback
),
314 error_callback_(error_callback
) {
315 buffer_pool_
= new VideoCaptureBufferPool(2);
317 ~StubClient() override
{}
319 MOCK_METHOD5(OnIncomingCapturedData
,
320 void(const uint8
* data
,
322 const media::VideoCaptureFormat
& frame_format
,
324 const base::TimeTicks
& timestamp
));
325 MOCK_METHOD9(OnIncomingCapturedYuvData
,
326 void (const uint8
* y_data
,
332 const media::VideoCaptureFormat
& frame_format
,
333 int clockwise_rotation
,
334 const base::TimeTicks
& timestamp
));
336 MOCK_METHOD0(DoOnIncomingCapturedBuffer
, void(void));
338 scoped_ptr
<media::VideoCaptureDevice::Client::Buffer
> ReserveOutputBuffer(
339 media::VideoPixelFormat format
,
340 const gfx::Size
& dimensions
) override
{
341 CHECK_EQ(format
, media::PIXEL_FORMAT_I420
);
342 int buffer_id_to_drop
= VideoCaptureBufferPool::kInvalidId
; // Ignored.
343 int buffer_id
= buffer_pool_
->ReserveForProducer(format
, dimensions
,
345 if (buffer_id
== VideoCaptureBufferPool::kInvalidId
)
348 return scoped_ptr
<media::VideoCaptureDevice::Client::Buffer
>(
349 new AutoReleaseBuffer(
350 buffer_pool_
, buffer_pool_
->GetBufferHandle(buffer_id
), buffer_id
));
352 // Trampoline method to workaround GMOCK problems with scoped_ptr<>.
353 void OnIncomingCapturedBuffer(scoped_ptr
<Buffer
> buffer
,
354 const media::VideoCaptureFormat
& frame_format
,
355 const base::TimeTicks
& timestamp
) override
{
356 DoOnIncomingCapturedBuffer();
359 void OnIncomingCapturedVideoFrame(
360 scoped_ptr
<Buffer
> buffer
,
361 const scoped_refptr
<media::VideoFrame
>& frame
,
362 const base::TimeTicks
& timestamp
) override
{
363 EXPECT_EQ(gfx::Size(kTestWidth
, kTestHeight
), frame
->visible_rect().size());
364 EXPECT_EQ(media::VideoFrame::I420
, frame
->format());
365 double frame_rate
= 0;
367 frame
->metadata()->GetDouble(media::VideoFrameMetadata::FRAME_RATE
,
369 EXPECT_EQ(kTestFramesPerSecond
, frame_rate
);
371 for (int plane
= 0; plane
< 3; ++plane
)
372 yuv
[plane
] = frame
->visible_data(plane
)[0];
373 // TODO(nick): We just look at the first pixel presently, because if
374 // the analysis is too slow, the backlog of frames will grow without bound
375 // and trouble erupts. http://crbug.com/174519
376 color_callback_
.Run((SkColorSetRGB(yuv
[0], yuv
[1], yuv
[2])));
379 void OnError(const std::string
& reason
) override
{ error_callback_
.Run(); }
382 class AutoReleaseBuffer
: public media::VideoCaptureDevice::Client::Buffer
{
385 const scoped_refptr
<VideoCaptureBufferPool
>& pool
,
386 scoped_ptr
<VideoCaptureBufferPool::BufferHandle
> buffer_handle
,
390 buffer_handle_(buffer_handle
.Pass()) {
393 int id() const override
{ return id_
; }
394 size_t size() const override
{ return buffer_handle_
->size(); }
395 void* data() override
{ return buffer_handle_
->data(); }
396 ClientBuffer
AsClientBuffer() override
{ return nullptr; }
399 ~AutoReleaseBuffer() override
{ pool_
->RelinquishProducerReservation(id_
); }
402 const scoped_refptr
<VideoCaptureBufferPool
> pool_
;
403 const scoped_ptr
<VideoCaptureBufferPool::BufferHandle
> buffer_handle_
;
406 scoped_refptr
<VideoCaptureBufferPool
> buffer_pool_
;
407 base::Callback
<void(SkColor
)> color_callback_
;
408 base::Closure error_callback_
;
410 DISALLOW_COPY_AND_ASSIGN(StubClient
);
413 class StubClientObserver
{
416 : error_encountered_(false),
417 wait_color_yuv_(0xcafe1950) {
418 client_
.reset(new StubClient(
419 base::Bind(&StubClientObserver::OnColor
, base::Unretained(this)),
420 base::Bind(&StubClientObserver::OnError
, base::Unretained(this))));
423 virtual ~StubClientObserver() {}
425 scoped_ptr
<media::VideoCaptureDevice::Client
> PassClient() {
426 return client_
.Pass();
429 void QuitIfConditionMet(SkColor color
) {
430 base::AutoLock
guard(lock_
);
431 if (wait_color_yuv_
== color
|| error_encountered_
)
432 base::MessageLoop::current()->Quit();
435 void WaitForNextColor(SkColor expected_color
) {
437 base::AutoLock
guard(lock_
);
438 wait_color_yuv_
= ConvertRgbToYuv(expected_color
);
439 error_encountered_
= false;
441 RunCurrentLoopWithDeadline();
443 base::AutoLock
guard(lock_
);
444 ASSERT_FALSE(error_encountered_
);
448 void WaitForError() {
450 base::AutoLock
guard(lock_
);
451 wait_color_yuv_
= kNotInterested
;
452 error_encountered_
= false;
454 RunCurrentLoopWithDeadline();
456 base::AutoLock
guard(lock_
);
457 ASSERT_TRUE(error_encountered_
);
462 base::AutoLock
guard(lock_
);
463 return error_encountered_
;
468 base::AutoLock
guard(lock_
);
469 error_encountered_
= true;
471 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, base::Bind(
472 &StubClientObserver::QuitIfConditionMet
,
473 base::Unretained(this),
477 void OnColor(SkColor color
) {
478 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, base::Bind(
479 &StubClientObserver::QuitIfConditionMet
,
480 base::Unretained(this),
486 bool error_encountered_
;
487 SkColor wait_color_yuv_
;
488 scoped_ptr
<StubClient
> client_
;
490 DISALLOW_COPY_AND_ASSIGN(StubClientObserver
);
493 // A dummy implementation of gfx::Screen, since WebContentsVideoCaptureDevice
494 // needs access to a gfx::Display's device scale factor.
495 class FakeScreen
: public gfx::Screen
{
497 FakeScreen() : the_one_display_(0x1337, gfx::Rect(0, 0, 2560, 1440)) {
498 the_one_display_
.set_device_scale_factor(kTestDeviceScaleFactor
);
500 ~FakeScreen() override
{}
502 // gfx::Screen implementation (only what's needed for testing).
503 gfx::Point
GetCursorScreenPoint() override
{ return gfx::Point(); }
504 gfx::NativeWindow
GetWindowUnderCursor() override
{ return NULL
; }
505 gfx::NativeWindow
GetWindowAtScreenPoint(const gfx::Point
& point
) override
{
508 int GetNumDisplays() const override
{ return 1; }
509 std::vector
<gfx::Display
> GetAllDisplays() const override
{
510 return std::vector
<gfx::Display
>(1, the_one_display_
);
512 gfx::Display
GetDisplayNearestWindow(gfx::NativeView view
) const override
{
513 return the_one_display_
;
515 gfx::Display
GetDisplayNearestPoint(const gfx::Point
& point
) const override
{
516 return the_one_display_
;
518 gfx::Display
GetDisplayMatching(const gfx::Rect
& match_rect
) const override
{
519 return the_one_display_
;
521 gfx::Display
GetPrimaryDisplay() const override
{ return the_one_display_
; }
522 void AddObserver(gfx::DisplayObserver
* observer
) override
{}
523 void RemoveObserver(gfx::DisplayObserver
* observer
) override
{}
526 gfx::Display the_one_display_
;
528 DISALLOW_COPY_AND_ASSIGN(FakeScreen
);
531 // Test harness that sets up a minimal environment with necessary stubs.
532 class WebContentsVideoCaptureDeviceTest
: public testing::Test
{
534 // This is public because C++ method pointer scoping rules are silly and make
535 // this hard to use with Bind().
536 void ResetWebContents() {
537 web_contents_
.reset();
541 void SetUp() override
{
542 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, &fake_screen_
);
543 ASSERT_EQ(&fake_screen_
, gfx::Screen::GetNativeScreen());
545 // TODO(nick): Sadness and woe! Much "mock-the-world" boilerplate could be
546 // eliminated here, if only we could use RenderViewHostTestHarness. The
547 // catch is that we need our TestRenderViewHost to support a
548 // CopyFromBackingStore operation that we control. To accomplish that,
549 // either RenderViewHostTestHarness would have to support installing a
550 // custom RenderViewHostFactory, or else we implant some kind of delegated
551 // CopyFromBackingStore functionality into TestRenderViewHost itself.
553 render_process_host_factory_
.reset(new MockRenderProcessHostFactory());
554 // Create our (self-registering) RVH factory, so that when we create a
555 // WebContents, it in turn creates CaptureTestRenderViewHosts.
556 render_view_host_factory_
.reset(
557 new CaptureTestRenderViewHostFactory(&controller_
));
559 browser_context_
.reset(new TestBrowserContext());
561 scoped_refptr
<SiteInstance
> site_instance
=
562 SiteInstance::Create(browser_context_
.get());
563 SiteInstanceImpl::set_render_process_host_factory(
564 render_process_host_factory_
.get());
566 TestWebContents::Create(browser_context_
.get(), site_instance
.get()));
567 RenderFrameHost
* const main_frame
= web_contents_
->GetMainFrame();
568 device_
.reset(WebContentsVideoCaptureDevice::Create(
569 base::StringPrintf("web-contents-media-stream://%d:%d",
570 main_frame
->GetProcess()->GetID(),
571 main_frame
->GetRoutingID())));
573 base::RunLoop().RunUntilIdle();
576 void TearDown() override
{
577 // Tear down in opposite order of set-up.
579 // The device is destroyed asynchronously, and will notify the
580 // CaptureTestSourceController when it finishes destruction.
581 // Trigger this, and wait.
583 device_
->StopAndDeAllocate();
587 base::RunLoop().RunUntilIdle();
589 // Destroy the browser objects.
590 web_contents_
.reset();
591 browser_context_
.reset();
593 base::RunLoop().RunUntilIdle();
595 SiteInstanceImpl::set_render_process_host_factory(NULL
);
596 render_view_host_factory_
.reset();
597 render_process_host_factory_
.reset();
599 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, NULL
);
603 CaptureTestSourceController
* source() { return &controller_
; }
604 WebContents
* web_contents() const { return web_contents_
.get(); }
605 media::VideoCaptureDevice
* device() { return device_
.get(); }
607 void SimulateDrawEvent() {
608 if (source()->CanUseFrameSubscriber()) {
610 CaptureTestView
* test_view
= static_cast<CaptureTestView
*>(
611 web_contents_
->GetRenderViewHost()->GetView());
612 test_view
->SimulateUpdate();
614 // Simulate a non-accelerated paint.
615 NotificationService::current()->Notify(
616 NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE
,
617 Source
<RenderWidgetHost
>(web_contents_
->GetRenderViewHost()),
618 NotificationService::NoDetails());
622 void DestroyVideoCaptureDevice() { device_
.reset(); }
624 StubClientObserver
* client_observer() {
625 return &client_observer_
;
629 FakeScreen fake_screen_
;
631 StubClientObserver client_observer_
;
633 // The controller controls which pixel patterns to produce.
634 CaptureTestSourceController controller_
;
636 // Self-registering RenderProcessHostFactory.
637 scoped_ptr
<MockRenderProcessHostFactory
> render_process_host_factory_
;
639 // Creates capture-capable RenderViewHosts whose pixel content production is
640 // under the control of |controller_|.
641 scoped_ptr
<CaptureTestRenderViewHostFactory
> render_view_host_factory_
;
643 // A mocked-out browser and tab.
644 scoped_ptr
<TestBrowserContext
> browser_context_
;
645 scoped_ptr
<WebContents
> web_contents_
;
647 // Finally, the WebContentsVideoCaptureDevice under test.
648 scoped_ptr
<media::VideoCaptureDevice
> device_
;
650 TestBrowserThreadBundle thread_bundle_
;
653 TEST_F(WebContentsVideoCaptureDeviceTest
, InvalidInitialWebContentsError
) {
654 // Before the installs itself on the UI thread up to start capturing, we'll
655 // delete the web contents. This should trigger an error which can happen in
656 // practice; we should be able to recover gracefully.
659 media::VideoCaptureParams capture_params
;
660 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
661 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
662 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
663 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
664 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
665 device()->StopAndDeAllocate();
668 TEST_F(WebContentsVideoCaptureDeviceTest
, WebContentsDestroyed
) {
669 const gfx::Size
capture_preferred_size(
670 static_cast<int>(kTestWidth
/ kTestDeviceScaleFactor
),
671 static_cast<int>(kTestHeight
/ kTestDeviceScaleFactor
));
672 ASSERT_NE(capture_preferred_size
, web_contents()->GetPreferredSize());
674 // We'll simulate the tab being closed after the capture pipeline is up and
676 media::VideoCaptureParams capture_params
;
677 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
678 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
679 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
680 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
681 // Do one capture to prove
682 source()->SetSolidColor(SK_ColorRED
);
684 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED
));
686 base::RunLoop().RunUntilIdle();
688 // Check that the preferred size of the WebContents matches the one provided
689 // by WebContentsVideoCaptureDevice.
690 EXPECT_EQ(capture_preferred_size
, web_contents()->GetPreferredSize());
692 // Post a task to close the tab. We should see an error reported to the
694 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
695 base::Bind(&WebContentsVideoCaptureDeviceTest::ResetWebContents
,
696 base::Unretained(this)));
697 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
698 device()->StopAndDeAllocate();
701 TEST_F(WebContentsVideoCaptureDeviceTest
,
702 StopDeviceBeforeCaptureMachineCreation
) {
703 media::VideoCaptureParams capture_params
;
704 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
705 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
706 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
707 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
709 // Make a point of not running the UI messageloop here.
710 device()->StopAndDeAllocate();
711 DestroyVideoCaptureDevice();
713 // Currently, there should be CreateCaptureMachineOnUIThread() and
714 // DestroyCaptureMachineOnUIThread() tasks pending on the current (UI) message
715 // loop. These should both succeed without crashing, and the machine should
716 // wind up in the idle state.
717 base::RunLoop().RunUntilIdle();
720 TEST_F(WebContentsVideoCaptureDeviceTest
, StopWithRendererWorkToDo
) {
721 // Set up the test to use RGB copies and an normal
722 source()->SetCanCopyToVideoFrame(false);
723 source()->SetUseFrameSubscriber(false);
724 media::VideoCaptureParams capture_params
;
725 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
726 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
727 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
728 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
730 base::RunLoop().RunUntilIdle();
732 for (int i
= 0; i
< 10; ++i
)
735 ASSERT_FALSE(client_observer()->HasError());
736 device()->StopAndDeAllocate();
737 ASSERT_FALSE(client_observer()->HasError());
738 base::RunLoop().RunUntilIdle();
739 ASSERT_FALSE(client_observer()->HasError());
742 TEST_F(WebContentsVideoCaptureDeviceTest
, DeviceRestart
) {
743 media::VideoCaptureParams capture_params
;
744 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
745 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
746 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
747 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
748 base::RunLoop().RunUntilIdle();
749 source()->SetSolidColor(SK_ColorRED
);
752 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED
));
755 source()->SetSolidColor(SK_ColorGREEN
);
757 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN
));
758 device()->StopAndDeAllocate();
760 // Device is stopped, but content can still be animating.
763 base::RunLoop().RunUntilIdle();
765 StubClientObserver observer2
;
766 device()->AllocateAndStart(capture_params
, observer2
.PassClient());
767 source()->SetSolidColor(SK_ColorBLUE
);
769 ASSERT_NO_FATAL_FAILURE(observer2
.WaitForNextColor(SK_ColorBLUE
));
770 source()->SetSolidColor(SK_ColorYELLOW
);
772 ASSERT_NO_FATAL_FAILURE(observer2
.WaitForNextColor(SK_ColorYELLOW
));
773 device()->StopAndDeAllocate();
776 // The "happy case" test. No scaling is needed, so we should be able to change
777 // the picture emitted from the source and expect to see each delivered to the
778 // consumer. The test will alternate between the three capture paths, simulating
779 // falling in and out of accelerated compositing.
780 TEST_F(WebContentsVideoCaptureDeviceTest
, GoesThroughAllTheMotions
) {
781 media::VideoCaptureParams capture_params
;
782 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
783 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
784 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
785 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
787 for (int i
= 0; i
< 6; i
++) {
788 const char* name
= NULL
;
791 source()->SetCanCopyToVideoFrame(true);
792 source()->SetUseFrameSubscriber(false);
796 source()->SetCanCopyToVideoFrame(false);
797 source()->SetUseFrameSubscriber(true);
801 source()->SetCanCopyToVideoFrame(false);
802 source()->SetUseFrameSubscriber(false);
809 SCOPED_TRACE(base::StringPrintf("Using %s path, iteration #%d", name
, i
));
811 source()->SetSolidColor(SK_ColorRED
);
813 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED
));
815 source()->SetSolidColor(SK_ColorGREEN
);
817 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN
));
819 source()->SetSolidColor(SK_ColorBLUE
);
821 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLUE
));
823 source()->SetSolidColor(SK_ColorBLACK
);
825 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLACK
));
827 device()->StopAndDeAllocate();
830 TEST_F(WebContentsVideoCaptureDeviceTest
, RejectsInvalidAllocateParams
) {
831 media::VideoCaptureParams capture_params
;
832 capture_params
.requested_format
.frame_size
.SetSize(1280, 720);
833 capture_params
.requested_format
.frame_rate
= -2;
834 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
835 BrowserThread::PostTask(
838 base::Bind(&media::VideoCaptureDevice::AllocateAndStart
,
839 base::Unretained(device()),
841 base::Passed(client_observer()->PassClient())));
842 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
843 BrowserThread::PostTask(
846 base::Bind(&media::VideoCaptureDevice::StopAndDeAllocate
,
847 base::Unretained(device())));
848 base::RunLoop().RunUntilIdle();
851 TEST_F(WebContentsVideoCaptureDeviceTest
, BadFramesGoodFrames
) {
852 media::VideoCaptureParams capture_params
;
853 capture_params
.requested_format
.frame_size
.SetSize(kTestWidth
, kTestHeight
);
854 capture_params
.requested_format
.frame_rate
= kTestFramesPerSecond
;
855 capture_params
.requested_format
.pixel_format
= media::PIXEL_FORMAT_I420
;
856 // 1x1 is too small to process; we intend for this to result in an error.
857 source()->SetCopyResultSize(1, 1);
858 source()->SetSolidColor(SK_ColorRED
);
859 device()->AllocateAndStart(capture_params
, client_observer()->PassClient());
861 // These frames ought to be dropped during the Render stage. Let
862 // several captures to happen.
863 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
864 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
865 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
866 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
867 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
869 // Now push some good frames through; they should be processed normally.
870 source()->SetCopyResultSize(kTestWidth
, kTestHeight
);
871 source()->SetSolidColor(SK_ColorGREEN
);
872 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN
));
873 source()->SetSolidColor(SK_ColorRED
);
874 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED
));
876 device()->StopAndDeAllocate();
880 } // namespace content