Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / content / browser / media / capture / web_contents_video_capture_device_unittest.cc
blobd0c7a8859b48c260d65e60594013822826f50013
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/frame_host/render_frame_host_impl.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/browser/web_contents/web_contents_impl.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/browser/render_widget_host_view_frame_subscriber.h"
23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_context.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "content/public/test/test_utils.h"
27 #include "content/test/test_render_frame_host_factory.h"
28 #include "content/test/test_render_view_host.h"
29 #include "content/test/test_web_contents.h"
30 #include "media/base/video_capture_types.h"
31 #include "media/base/video_frame.h"
32 #include "media/base/video_util.h"
33 #include "media/base/yuv_convert.h"
34 #include "skia/ext/platform_canvas.h"
35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h"
37 #include "third_party/skia/include/core/SkColor.h"
38 #include "ui/base/layout.h"
39 #include "ui/gfx/display.h"
40 #include "ui/gfx/geometry/dip_util.h"
41 #include "ui/gfx/geometry/size_conversions.h"
42 #include "ui/gfx/screen.h"
43 #include "ui/gfx/test/test_screen.h"
45 namespace content {
46 namespace {
48 const int kTestWidth = 320;
49 const int kTestHeight = 240;
50 const int kTestFramesPerSecond = 20;
51 const float kTestDeviceScaleFactor = 2.0f;
52 const SkColor kNothingYet = 0xdeadbeef;
53 const SkColor kNotInterested = ~kNothingYet;
55 void DeadlineExceeded(base::Closure quit_closure) {
56 if (!base::debug::BeingDebugged()) {
57 quit_closure.Run();
58 FAIL() << "Deadline exceeded while waiting, quitting";
59 } else {
60 LOG(WARNING) << "Deadline exceeded; test would fail if debugger weren't "
61 << "attached.";
65 void RunCurrentLoopWithDeadline() {
66 base::Timer deadline(false, false);
67 deadline.Start(FROM_HERE, TestTimeouts::action_max_timeout(), base::Bind(
68 &DeadlineExceeded, base::MessageLoop::current()->QuitClosure()));
69 base::MessageLoop::current()->Run();
70 deadline.Stop();
73 SkColor ConvertRgbToYuv(SkColor rgb) {
74 uint8 yuv[3];
75 media::ConvertRGB32ToYUV(reinterpret_cast<uint8*>(&rgb),
76 yuv, yuv + 1, yuv + 2, 1, 1, 1, 1, 1);
77 return SkColorSetRGB(yuv[0], yuv[1], yuv[2]);
80 // Thread-safe class that controls the source pattern to be captured by the
81 // system under test. The lifetime of this class is greater than the lifetime
82 // of all objects that reference it, so it does not need to be reference
83 // counted.
84 class CaptureTestSourceController {
85 public:
86 CaptureTestSourceController()
87 : color_(SK_ColorMAGENTA),
88 copy_result_size_(kTestWidth, kTestHeight),
89 can_copy_to_video_frame_(false),
90 use_frame_subscriber_(false) {}
92 void SetSolidColor(SkColor color) {
93 base::AutoLock guard(lock_);
94 color_ = color;
97 SkColor GetSolidColor() {
98 base::AutoLock guard(lock_);
99 return color_;
102 void SetCopyResultSize(int width, int height) {
103 base::AutoLock guard(lock_);
104 copy_result_size_ = gfx::Size(width, height);
107 gfx::Size GetCopyResultSize() {
108 base::AutoLock guard(lock_);
109 return copy_result_size_;
112 void SignalCopy() {
113 // TODO(nick): This actually should always be happening on the UI thread.
114 base::AutoLock guard(lock_);
115 if (!copy_done_.is_null()) {
116 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, copy_done_);
117 copy_done_.Reset();
121 void SetCanCopyToVideoFrame(bool value) {
122 base::AutoLock guard(lock_);
123 can_copy_to_video_frame_ = value;
126 bool CanCopyToVideoFrame() {
127 base::AutoLock guard(lock_);
128 return can_copy_to_video_frame_;
131 void SetUseFrameSubscriber(bool value) {
132 base::AutoLock guard(lock_);
133 use_frame_subscriber_ = value;
136 bool CanUseFrameSubscriber() {
137 base::AutoLock guard(lock_);
138 return use_frame_subscriber_;
141 void WaitForNextCopy() {
143 base::AutoLock guard(lock_);
144 copy_done_ = base::MessageLoop::current()->QuitClosure();
147 RunCurrentLoopWithDeadline();
150 private:
151 base::Lock lock_; // Guards changes to all members.
152 SkColor color_;
153 gfx::Size copy_result_size_;
154 bool can_copy_to_video_frame_;
155 bool use_frame_subscriber_;
156 base::Closure copy_done_;
158 DISALLOW_COPY_AND_ASSIGN(CaptureTestSourceController);
161 // A stub implementation which returns solid-color bitmaps in calls to
162 // CopyFromCompositingSurfaceToVideoFrame(), and which allows the video-frame
163 // readback path to be switched on and off. The behavior is controlled by a
164 // CaptureTestSourceController.
165 class CaptureTestView : public TestRenderWidgetHostView {
166 public:
167 explicit CaptureTestView(RenderWidgetHostImpl* rwh,
168 CaptureTestSourceController* controller)
169 : TestRenderWidgetHostView(rwh),
170 controller_(controller),
171 fake_bounds_(100, 100, 100 + kTestWidth, 100 + kTestHeight) {}
173 ~CaptureTestView() override {}
175 // TestRenderWidgetHostView overrides.
176 gfx::Rect GetViewBounds() const override {
177 return fake_bounds_;
180 void SetSize(const gfx::Size& size) override {
181 SetBounds(gfx::Rect(fake_bounds_.origin(), size));
184 void SetBounds(const gfx::Rect& rect) override {
185 fake_bounds_ = rect;
188 bool CanCopyToVideoFrame() const override {
189 return controller_->CanCopyToVideoFrame();
192 void CopyFromCompositingSurfaceToVideoFrame(
193 const gfx::Rect& src_subrect,
194 const scoped_refptr<media::VideoFrame>& target,
195 const base::Callback<void(bool)>& callback) override {
196 SkColor c = ConvertRgbToYuv(controller_->GetSolidColor());
197 media::FillYUV(
198 target.get(), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
199 callback.Run(true);
200 controller_->SignalCopy();
203 void BeginFrameSubscription(
204 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) override {
205 subscriber_.reset(subscriber.release());
208 void EndFrameSubscription() override { subscriber_.reset(); }
210 // Simulate a compositor paint event for our subscriber.
211 void SimulateUpdate() {
212 const base::TimeTicks present_time = base::TimeTicks::Now();
213 RenderWidgetHostViewFrameSubscriber::DeliverFrameCallback callback;
214 scoped_refptr<media::VideoFrame> target;
215 if (subscriber_ && subscriber_->ShouldCaptureFrame(
216 gfx::Rect(), present_time, &target, &callback)) {
217 SkColor c = ConvertRgbToYuv(controller_->GetSolidColor());
218 media::FillYUV(
219 target.get(), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
220 BrowserThread::PostTask(BrowserThread::UI,
221 FROM_HERE,
222 base::Bind(callback, present_time, true));
223 controller_->SignalCopy();
227 private:
228 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber_;
229 CaptureTestSourceController* const controller_;
230 gfx::Rect fake_bounds_;
232 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestView);
235 #if defined(COMPILER_MSVC)
236 // MSVC warns on diamond inheritance. See comment for same warning on
237 // RenderViewHostImpl.
238 #pragma warning(push)
239 #pragma warning(disable: 4250)
240 #endif
242 // A stub implementation which returns solid-color bitmaps in calls to
243 // CopyFromBackingStore(). The behavior is controlled by a
244 // CaptureTestSourceController.
245 class CaptureTestRenderViewHost : public TestRenderViewHost {
246 public:
247 CaptureTestRenderViewHost(SiteInstance* instance,
248 RenderViewHostDelegate* delegate,
249 RenderWidgetHostDelegate* widget_delegate,
250 int32 routing_id,
251 int32 surface_id,
252 int32 main_frame_routing_id,
253 bool swapped_out,
254 CaptureTestSourceController* controller)
255 : TestRenderViewHost(instance,
256 delegate,
257 widget_delegate,
258 routing_id,
259 surface_id,
260 main_frame_routing_id,
261 swapped_out),
262 controller_(controller) {
263 // Override the default view installed by TestRenderViewHost; we need
264 // our special subclass which has mocked-out tab capture support.
265 RenderWidgetHostView* old_view = GetView();
266 SetView(new CaptureTestView(this, controller));
267 delete old_view;
270 // TestRenderViewHost overrides.
271 void CopyFromBackingStore(const gfx::Rect& src_rect,
272 const gfx::Size& accelerated_dst_size,
273 ReadbackRequestCallback& callback,
274 const SkColorType color_type) override {
275 gfx::Size size = controller_->GetCopyResultSize();
276 SkColor color = controller_->GetSolidColor();
278 // Although it's not necessary, use a PlatformBitmap here (instead of a
279 // regular SkBitmap) to exercise possible threading issues.
280 skia::PlatformBitmap output;
281 EXPECT_TRUE(output.Allocate(size.width(), size.height(), false));
283 SkAutoLockPixels locker(output.GetBitmap());
284 output.GetBitmap().eraseColor(color);
286 callback.Run(output.GetBitmap(), content::READBACK_SUCCESS);
287 controller_->SignalCopy();
290 private:
291 CaptureTestSourceController* controller_;
293 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHost);
296 #if defined(COMPILER_MSVC)
297 // Re-enable warning 4250
298 #pragma warning(pop)
299 #endif
301 class CaptureTestRenderViewHostFactory : public RenderViewHostFactory {
302 public:
303 explicit CaptureTestRenderViewHostFactory(
304 CaptureTestSourceController* controller) : controller_(controller) {
305 RegisterFactory(this);
308 ~CaptureTestRenderViewHostFactory() override { UnregisterFactory(); }
310 // RenderViewHostFactory implementation.
311 RenderViewHost* CreateRenderViewHost(
312 SiteInstance* instance,
313 RenderViewHostDelegate* delegate,
314 RenderWidgetHostDelegate* widget_delegate,
315 int32 routing_id,
316 int32 surface_id,
317 int32 main_frame_routing_id,
318 bool swapped_out) override {
319 return new CaptureTestRenderViewHost(
320 instance, delegate, widget_delegate, routing_id, surface_id,
321 main_frame_routing_id, swapped_out, controller_);
323 private:
324 CaptureTestSourceController* controller_;
326 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHostFactory);
329 // A stub consumer of captured video frames, which checks the output of
330 // WebContentsVideoCaptureDevice.
331 class StubClient : public media::VideoCaptureDevice::Client {
332 public:
333 StubClient(
334 const base::Callback<void(SkColor, const gfx::Size&)>& report_callback,
335 const base::Closure& error_callback)
336 : report_callback_(report_callback),
337 error_callback_(error_callback) {
338 buffer_pool_ = new VideoCaptureBufferPool(2);
340 ~StubClient() override {}
342 MOCK_METHOD5(OnIncomingCapturedData,
343 void(const uint8* data,
344 int length,
345 const media::VideoCaptureFormat& frame_format,
346 int rotation,
347 const base::TimeTicks& timestamp));
348 MOCK_METHOD9(OnIncomingCapturedYuvData,
349 void (const uint8* y_data,
350 const uint8* u_data,
351 const uint8* v_data,
352 size_t y_stride,
353 size_t u_stride,
354 size_t v_stride,
355 const media::VideoCaptureFormat& frame_format,
356 int clockwise_rotation,
357 const base::TimeTicks& timestamp));
359 MOCK_METHOD0(DoOnIncomingCapturedBuffer, void(void));
361 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> ReserveOutputBuffer(
362 const gfx::Size& dimensions,
363 media::VideoPixelFormat format,
364 media::VideoPixelStorage storage) override {
365 CHECK_EQ(format, media::PIXEL_FORMAT_I420);
366 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId; // Ignored.
367 const int buffer_id = buffer_pool_->ReserveForProducer(
368 format, storage, dimensions, &buffer_id_to_drop);
369 if (buffer_id == VideoCaptureBufferPool::kInvalidId)
370 return NULL;
372 return scoped_ptr<media::VideoCaptureDevice::Client::Buffer>(
373 new AutoReleaseBuffer(
374 buffer_pool_, buffer_pool_->GetBufferHandle(buffer_id), buffer_id));
376 // Trampoline method to workaround GMOCK problems with scoped_ptr<>.
377 void OnIncomingCapturedBuffer(scoped_ptr<Buffer> buffer,
378 const media::VideoCaptureFormat& frame_format,
379 const base::TimeTicks& timestamp) override {
380 DoOnIncomingCapturedBuffer();
383 void OnIncomingCapturedVideoFrame(
384 scoped_ptr<Buffer> buffer,
385 const scoped_refptr<media::VideoFrame>& frame,
386 const base::TimeTicks& timestamp) override {
387 EXPECT_FALSE(frame->visible_rect().IsEmpty());
388 EXPECT_EQ(media::PIXEL_FORMAT_I420, frame->format());
389 double frame_rate = 0;
390 EXPECT_TRUE(
391 frame->metadata()->GetDouble(media::VideoFrameMetadata::FRAME_RATE,
392 &frame_rate));
393 EXPECT_EQ(kTestFramesPerSecond, frame_rate);
395 // TODO(miu): We just look at the center pixel presently, because if the
396 // analysis is too slow, the backlog of frames will grow without bound and
397 // trouble erupts. http://crbug.com/174519
398 using media::VideoFrame;
399 const gfx::Point center = frame->visible_rect().CenterPoint();
400 const int center_offset_y =
401 (frame->stride(VideoFrame::kYPlane) * center.y()) + center.x();
402 const int center_offset_uv =
403 (frame->stride(VideoFrame::kUPlane) * (center.y() / 2)) +
404 (center.x() / 2);
405 report_callback_.Run(
406 SkColorSetRGB(frame->data(VideoFrame::kYPlane)[center_offset_y],
407 frame->data(VideoFrame::kUPlane)[center_offset_uv],
408 frame->data(VideoFrame::kVPlane)[center_offset_uv]),
409 frame->visible_rect().size());
412 void OnError(const std::string& reason) override { error_callback_.Run(); }
414 double GetBufferPoolUtilization() const override { return 0.0; }
416 private:
417 class AutoReleaseBuffer : public media::VideoCaptureDevice::Client::Buffer {
418 public:
419 AutoReleaseBuffer(
420 const scoped_refptr<VideoCaptureBufferPool>& pool,
421 scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle,
422 int buffer_id)
423 : id_(buffer_id),
424 pool_(pool),
425 buffer_handle_(buffer_handle.Pass()) {
426 DCHECK(pool_.get());
428 int id() const override { return id_; }
429 gfx::Size dimensions() const override { return gfx::Size(); }
430 size_t mapped_size() const override {
431 return buffer_handle_->mapped_size();
433 void* data(int plane) override { return buffer_handle_->data(plane); }
434 ClientBuffer AsClientBuffer(int plane) override { return nullptr; }
435 #if defined(OS_POSIX)
436 base::FileDescriptor AsPlatformFile() override {
437 return base::FileDescriptor();
439 #endif
441 private:
442 ~AutoReleaseBuffer() override { pool_->RelinquishProducerReservation(id_); }
444 const int id_;
445 const scoped_refptr<VideoCaptureBufferPool> pool_;
446 const scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle_;
449 scoped_refptr<VideoCaptureBufferPool> buffer_pool_;
450 base::Callback<void(SkColor, const gfx::Size&)> report_callback_;
451 base::Closure error_callback_;
453 DISALLOW_COPY_AND_ASSIGN(StubClient);
456 class StubClientObserver {
457 public:
458 StubClientObserver()
459 : error_encountered_(false),
460 wait_color_yuv_(0xcafe1950),
461 wait_size_(kTestWidth, kTestHeight) {
462 client_.reset(new StubClient(
463 base::Bind(&StubClientObserver::DidDeliverFrame,
464 base::Unretained(this)),
465 base::Bind(&StubClientObserver::OnError, base::Unretained(this))));
468 virtual ~StubClientObserver() {}
470 scoped_ptr<media::VideoCaptureDevice::Client> PassClient() {
471 return client_.Pass();
474 void QuitIfConditionsMet(SkColor color, const gfx::Size& size) {
475 base::AutoLock guard(lock_);
476 if (error_encountered_)
477 base::MessageLoop::current()->Quit();
478 else if (wait_color_yuv_ == color && wait_size_.IsEmpty())
479 base::MessageLoop::current()->Quit();
480 else if (wait_color_yuv_ == color && wait_size_ == size)
481 base::MessageLoop::current()->Quit();
484 // Run the current loop until a frame is delivered with the |expected_color|
485 // and any non-empty frame size.
486 void WaitForNextColor(SkColor expected_color) {
487 WaitForNextColorAndFrameSize(expected_color, gfx::Size());
490 // Run the current loop until a frame is delivered with the |expected_color|
491 // and is of the |expected_size|.
492 void WaitForNextColorAndFrameSize(SkColor expected_color,
493 const gfx::Size& expected_size) {
495 base::AutoLock guard(lock_);
496 wait_color_yuv_ = ConvertRgbToYuv(expected_color);
497 wait_size_ = expected_size;
498 error_encountered_ = false;
500 RunCurrentLoopWithDeadline();
502 base::AutoLock guard(lock_);
503 ASSERT_FALSE(error_encountered_);
507 void WaitForError() {
509 base::AutoLock guard(lock_);
510 wait_color_yuv_ = kNotInterested;
511 wait_size_ = gfx::Size();
512 error_encountered_ = false;
514 RunCurrentLoopWithDeadline();
516 base::AutoLock guard(lock_);
517 ASSERT_TRUE(error_encountered_);
521 bool HasError() {
522 base::AutoLock guard(lock_);
523 return error_encountered_;
526 void OnError() {
528 base::AutoLock guard(lock_);
529 error_encountered_ = true;
531 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
532 &StubClientObserver::QuitIfConditionsMet,
533 base::Unretained(this),
534 kNothingYet,
535 gfx::Size()));
538 void DidDeliverFrame(SkColor color, const gfx::Size& size) {
539 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
540 &StubClientObserver::QuitIfConditionsMet,
541 base::Unretained(this),
542 color,
543 size));
546 private:
547 base::Lock lock_;
548 bool error_encountered_;
549 SkColor wait_color_yuv_;
550 gfx::Size wait_size_;
551 scoped_ptr<StubClient> client_;
553 DISALLOW_COPY_AND_ASSIGN(StubClientObserver);
556 // crbug.com/159234
557 #if defined(OS_ANDROID)
558 #define MAYBE_WebContentsVideoCaptureDeviceTest \
559 DISABLED_WebContentsVideoCaptureDeviceTest
560 #else
561 #define MAYBE_WebContentsVideoCaptureDeviceTest \
562 WebContentsVideoCaptureDeviceTest
563 #endif // defined(OS_ANDROID)
565 // Test harness that sets up a minimal environment with necessary stubs.
566 class MAYBE_WebContentsVideoCaptureDeviceTest : public testing::Test {
567 public:
568 // This is public because C++ method pointer scoping rules are silly and make
569 // this hard to use with Bind().
570 void ResetWebContents() {
571 web_contents_.reset();
574 protected:
575 void SetUp() override {
576 test_screen_.display()->set_id(0x1337);
577 test_screen_.display()->set_bounds(gfx::Rect(0, 0, 2560, 1440));
578 test_screen_.display()->set_device_scale_factor(kTestDeviceScaleFactor);
580 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, &test_screen_);
581 ASSERT_EQ(&test_screen_, gfx::Screen::GetNativeScreen());
583 // TODO(nick): Sadness and woe! Much "mock-the-world" boilerplate could be
584 // eliminated here, if only we could use RenderViewHostTestHarness. The
585 // catch is that we need our TestRenderViewHost to support a
586 // CopyFromBackingStore operation that we control. To accomplish that,
587 // either RenderViewHostTestHarness would have to support installing a
588 // custom RenderViewHostFactory, or else we implant some kind of delegated
589 // CopyFromBackingStore functionality into TestRenderViewHost itself.
591 render_process_host_factory_.reset(new MockRenderProcessHostFactory());
592 // Create our (self-registering) RVH factory, so that when we create a
593 // WebContents, it in turn creates CaptureTestRenderViewHosts.
594 render_view_host_factory_.reset(
595 new CaptureTestRenderViewHostFactory(&controller_));
596 render_frame_host_factory_.reset(new TestRenderFrameHostFactory());
598 browser_context_.reset(new TestBrowserContext());
600 scoped_refptr<SiteInstance> site_instance =
601 SiteInstance::Create(browser_context_.get());
602 SiteInstanceImpl::set_render_process_host_factory(
603 render_process_host_factory_.get());
604 web_contents_.reset(
605 TestWebContents::Create(browser_context_.get(), site_instance.get()));
606 RenderFrameHost* const main_frame = web_contents_->GetMainFrame();
607 device_.reset(WebContentsVideoCaptureDevice::Create(
608 base::StringPrintf("web-contents-media-stream://%d:%d",
609 main_frame->GetProcess()->GetID(),
610 main_frame->GetRoutingID())));
612 base::RunLoop().RunUntilIdle();
615 void TearDown() override {
616 // Tear down in opposite order of set-up.
618 // The device is destroyed asynchronously, and will notify the
619 // CaptureTestSourceController when it finishes destruction.
620 // Trigger this, and wait.
621 if (device_) {
622 device_->StopAndDeAllocate();
623 device_.reset();
626 base::RunLoop().RunUntilIdle();
628 // Destroy the browser objects.
629 web_contents_.reset();
630 browser_context_.reset();
632 base::RunLoop().RunUntilIdle();
634 SiteInstanceImpl::set_render_process_host_factory(NULL);
635 render_frame_host_factory_.reset();
636 render_view_host_factory_.reset();
637 render_process_host_factory_.reset();
639 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL);
642 // Accessors.
643 CaptureTestSourceController* source() { return &controller_; }
644 WebContents* web_contents() const { return web_contents_.get(); }
645 media::VideoCaptureDevice* device() { return device_.get(); }
647 // Returns the device scale factor of the capture target's native view. This
648 // is necessary because, architecturally, the TestScreen implementation is
649 // ignored on Mac platforms (when determining the device scale factor for a
650 // particular window).
651 float GetDeviceScaleFactor() const {
652 RenderWidgetHostView* const view =
653 web_contents_->GetRenderViewHost()->GetView();
654 CHECK(view);
655 return ui::GetScaleFactorForNativeView(view->GetNativeView());
658 void SimulateDrawEvent() {
659 if (source()->CanUseFrameSubscriber()) {
660 // Print
661 CaptureTestView* test_view = static_cast<CaptureTestView*>(
662 web_contents_->GetRenderViewHost()->GetView());
663 test_view->SimulateUpdate();
664 } else {
665 // Simulate a non-accelerated paint.
666 NotificationService::current()->Notify(
667 NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE,
668 Source<RenderWidgetHost>(web_contents_->GetRenderViewHost()),
669 NotificationService::NoDetails());
673 void SimulateSourceSizeChange(const gfx::Size& size) {
674 DCHECK_CURRENTLY_ON(BrowserThread::UI);
675 CaptureTestView* test_view = static_cast<CaptureTestView*>(
676 web_contents_->GetRenderViewHost()->GetView());
677 test_view->SetSize(size);
678 // Normally, RenderWidgetHostImpl would notify WebContentsImpl that the size
679 // has changed. However, in this test setup where there is no render
680 // process, we must notify WebContentsImpl directly.
681 WebContentsImpl* const as_web_contents_impl =
682 static_cast<WebContentsImpl*>(web_contents_.get());
683 RenderWidgetHostDelegate* const as_rwh_delegate =
684 static_cast<RenderWidgetHostDelegate*>(as_web_contents_impl);
685 as_rwh_delegate->RenderWidgetWasResized(
686 as_web_contents_impl->GetMainFrame()->GetRenderWidgetHost(), true);
689 void DestroyVideoCaptureDevice() { device_.reset(); }
691 StubClientObserver* client_observer() {
692 return &client_observer_;
695 private:
696 gfx::test::TestScreen test_screen_;
698 StubClientObserver client_observer_;
700 // The controller controls which pixel patterns to produce.
701 CaptureTestSourceController controller_;
703 // Self-registering RenderProcessHostFactory.
704 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory_;
706 // Creates capture-capable RenderViewHosts whose pixel content production is
707 // under the control of |controller_|.
708 scoped_ptr<CaptureTestRenderViewHostFactory> render_view_host_factory_;
710 // Self-registering RenderFrameHostFactory.
711 scoped_ptr<TestRenderFrameHostFactory> render_frame_host_factory_;
713 // A mocked-out browser and tab.
714 scoped_ptr<TestBrowserContext> browser_context_;
715 scoped_ptr<WebContents> web_contents_;
717 // Finally, the WebContentsVideoCaptureDevice under test.
718 scoped_ptr<media::VideoCaptureDevice> device_;
720 TestBrowserThreadBundle thread_bundle_;
723 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest,
724 InvalidInitialWebContentsError) {
725 // Before the installs itself on the UI thread up to start capturing, we'll
726 // delete the web contents. This should trigger an error which can happen in
727 // practice; we should be able to recover gracefully.
728 ResetWebContents();
730 media::VideoCaptureParams capture_params;
731 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
732 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
733 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
734 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
735 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
736 device()->StopAndDeAllocate();
739 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest, WebContentsDestroyed) {
740 const float device_scale_factor = GetDeviceScaleFactor();
741 const gfx::Size capture_preferred_size(
742 static_cast<int>(kTestWidth / device_scale_factor),
743 static_cast<int>(kTestHeight / device_scale_factor));
744 ASSERT_NE(capture_preferred_size, web_contents()->GetPreferredSize());
746 // We'll simulate the tab being closed after the capture pipeline is up and
747 // running.
748 media::VideoCaptureParams capture_params;
749 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
750 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
751 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
752 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
753 // Do one capture to prove
754 source()->SetSolidColor(SK_ColorRED);
755 SimulateDrawEvent();
756 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
758 base::RunLoop().RunUntilIdle();
760 // Check that the preferred size of the WebContents matches the one provided
761 // by WebContentsVideoCaptureDevice.
762 EXPECT_EQ(capture_preferred_size, web_contents()->GetPreferredSize());
764 // Post a task to close the tab. We should see an error reported to the
765 // consumer.
766 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
767 base::Bind(&MAYBE_WebContentsVideoCaptureDeviceTest::ResetWebContents,
768 base::Unretained(this)));
769 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
770 device()->StopAndDeAllocate();
773 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest,
774 StopDeviceBeforeCaptureMachineCreation) {
775 media::VideoCaptureParams capture_params;
776 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
777 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
778 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
779 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
781 // Make a point of not running the UI messageloop here.
782 device()->StopAndDeAllocate();
783 DestroyVideoCaptureDevice();
785 // Currently, there should be CreateCaptureMachineOnUIThread() and
786 // DestroyCaptureMachineOnUIThread() tasks pending on the current (UI) message
787 // loop. These should both succeed without crashing, and the machine should
788 // wind up in the idle state.
789 base::RunLoop().RunUntilIdle();
792 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest, StopWithRendererWorkToDo) {
793 // Set up the test to use RGB copies and an normal
794 source()->SetCanCopyToVideoFrame(false);
795 source()->SetUseFrameSubscriber(false);
796 media::VideoCaptureParams capture_params;
797 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
798 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
799 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
800 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
802 base::RunLoop().RunUntilIdle();
804 for (int i = 0; i < 10; ++i)
805 SimulateDrawEvent();
807 ASSERT_FALSE(client_observer()->HasError());
808 device()->StopAndDeAllocate();
809 ASSERT_FALSE(client_observer()->HasError());
810 base::RunLoop().RunUntilIdle();
811 ASSERT_FALSE(client_observer()->HasError());
814 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest, DeviceRestart) {
815 media::VideoCaptureParams capture_params;
816 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
817 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
818 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
819 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
820 base::RunLoop().RunUntilIdle();
821 source()->SetSolidColor(SK_ColorRED);
822 SimulateDrawEvent();
823 SimulateDrawEvent();
824 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
825 SimulateDrawEvent();
826 SimulateDrawEvent();
827 source()->SetSolidColor(SK_ColorGREEN);
828 SimulateDrawEvent();
829 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN));
830 device()->StopAndDeAllocate();
832 // Device is stopped, but content can still be animating.
833 SimulateDrawEvent();
834 SimulateDrawEvent();
835 base::RunLoop().RunUntilIdle();
837 StubClientObserver observer2;
838 device()->AllocateAndStart(capture_params, observer2.PassClient());
839 source()->SetSolidColor(SK_ColorBLUE);
840 SimulateDrawEvent();
841 ASSERT_NO_FATAL_FAILURE(observer2.WaitForNextColor(SK_ColorBLUE));
842 source()->SetSolidColor(SK_ColorYELLOW);
843 SimulateDrawEvent();
844 ASSERT_NO_FATAL_FAILURE(observer2.WaitForNextColor(SK_ColorYELLOW));
845 device()->StopAndDeAllocate();
848 // The "happy case" test. No scaling is needed, so we should be able to change
849 // the picture emitted from the source and expect to see each delivered to the
850 // consumer. The test will alternate between the three capture paths, simulating
851 // falling in and out of accelerated compositing.
852 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest, GoesThroughAllTheMotions) {
853 media::VideoCaptureParams capture_params;
854 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
855 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
856 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
857 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
859 for (int i = 0; i < 6; i++) {
860 const char* name = NULL;
861 switch (i % 3) {
862 case 0:
863 source()->SetCanCopyToVideoFrame(true);
864 source()->SetUseFrameSubscriber(false);
865 name = "VideoFrame";
866 break;
867 case 1:
868 source()->SetCanCopyToVideoFrame(false);
869 source()->SetUseFrameSubscriber(true);
870 name = "Subscriber";
871 break;
872 case 2:
873 source()->SetCanCopyToVideoFrame(false);
874 source()->SetUseFrameSubscriber(false);
875 name = "SkBitmap";
876 break;
877 default:
878 FAIL();
881 SCOPED_TRACE(base::StringPrintf("Using %s path, iteration #%d", name, i));
883 source()->SetSolidColor(SK_ColorRED);
884 SimulateDrawEvent();
885 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
887 source()->SetSolidColor(SK_ColorGREEN);
888 SimulateDrawEvent();
889 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN));
891 source()->SetSolidColor(SK_ColorBLUE);
892 SimulateDrawEvent();
893 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLUE));
895 source()->SetSolidColor(SK_ColorBLACK);
896 SimulateDrawEvent();
897 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLACK));
899 device()->StopAndDeAllocate();
902 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest, BadFramesGoodFrames) {
903 media::VideoCaptureParams capture_params;
904 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
905 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
906 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
907 // 1x1 is too small to process; we intend for this to result in an error.
908 source()->SetCopyResultSize(1, 1);
909 source()->SetSolidColor(SK_ColorRED);
910 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
912 // These frames ought to be dropped during the Render stage. Let
913 // several captures to happen.
914 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
915 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
916 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
917 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
918 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
920 // Now push some good frames through; they should be processed normally.
921 source()->SetCopyResultSize(kTestWidth, kTestHeight);
922 source()->SetSolidColor(SK_ColorGREEN);
923 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN));
924 source()->SetSolidColor(SK_ColorRED);
925 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
927 device()->StopAndDeAllocate();
930 // Tests that, when configured with the FIXED_ASPECT_RATIO resolution change
931 // policy, the source size changes result in video frames of possibly varying
932 // resolutions, but all with the same aspect ratio.
933 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest,
934 VariableResolution_FixedAspectRatio) {
935 media::VideoCaptureParams capture_params;
936 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
937 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
938 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
939 capture_params.resolution_change_policy =
940 media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO;
942 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
944 source()->SetUseFrameSubscriber(true);
946 // Source size equals maximum size. Expect delivered frames to be
947 // kTestWidth by kTestHeight.
948 source()->SetSolidColor(SK_ColorRED);
949 const float device_scale_factor = GetDeviceScaleFactor();
950 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
951 device_scale_factor, gfx::Size(kTestWidth, kTestHeight)));
952 SimulateDrawEvent();
953 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
954 SK_ColorRED, gfx::Size(kTestWidth, kTestHeight)));
956 // Source size is half in both dimensions. Expect delivered frames to be of
957 // the same aspect ratio as kTestWidth by kTestHeight, but larger than the
958 // half size because the minimum height is 180 lines.
959 source()->SetSolidColor(SK_ColorGREEN);
960 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
961 device_scale_factor, gfx::Size(kTestWidth / 2, kTestHeight / 2)));
962 SimulateDrawEvent();
963 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
964 SK_ColorGREEN, gfx::Size(180 * kTestWidth / kTestHeight, 180)));
966 // Source size changes aspect ratio. Expect delivered frames to be padded
967 // in the horizontal dimension to preserve aspect ratio.
968 source()->SetSolidColor(SK_ColorBLUE);
969 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
970 device_scale_factor, gfx::Size(kTestWidth / 2, kTestHeight)));
971 SimulateDrawEvent();
972 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
973 SK_ColorBLUE, gfx::Size(kTestWidth, kTestHeight)));
975 // Source size changes aspect ratio again. Expect delivered frames to be
976 // padded in the vertical dimension to preserve aspect ratio.
977 source()->SetSolidColor(SK_ColorBLACK);
978 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
979 device_scale_factor, gfx::Size(kTestWidth, kTestHeight / 2)));
980 SimulateDrawEvent();
981 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
982 SK_ColorBLACK, gfx::Size(kTestWidth, kTestHeight)));
984 device()->StopAndDeAllocate();
987 // Tests that, when configured with the ANY_WITHIN_LIMIT resolution change
988 // policy, the source size changes result in video frames of possibly varying
989 // resolutions.
990 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest,
991 VariableResolution_AnyWithinLimits) {
992 media::VideoCaptureParams capture_params;
993 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
994 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
995 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
996 capture_params.resolution_change_policy =
997 media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT;
999 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
1001 source()->SetUseFrameSubscriber(true);
1003 // Source size equals maximum size. Expect delivered frames to be
1004 // kTestWidth by kTestHeight.
1005 source()->SetSolidColor(SK_ColorRED);
1006 const float device_scale_factor = GetDeviceScaleFactor();
1007 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
1008 device_scale_factor, gfx::Size(kTestWidth, kTestHeight)));
1009 SimulateDrawEvent();
1010 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
1011 SK_ColorRED, gfx::Size(kTestWidth, kTestHeight)));
1013 // Source size is half in both dimensions. Expect delivered frames to also
1014 // be half in both dimensions.
1015 source()->SetSolidColor(SK_ColorGREEN);
1016 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
1017 device_scale_factor, gfx::Size(kTestWidth / 2, kTestHeight / 2)));
1018 SimulateDrawEvent();
1019 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
1020 SK_ColorGREEN, gfx::Size(kTestWidth / 2, kTestHeight / 2)));
1022 // Source size changes to something arbitrary. Since the source size is
1023 // less than the maximum size, expect delivered frames to be the same size
1024 // as the source size.
1025 source()->SetSolidColor(SK_ColorBLUE);
1026 gfx::Size arbitrary_source_size(kTestWidth / 2 + 42, kTestHeight - 10);
1027 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(device_scale_factor,
1028 arbitrary_source_size));
1029 SimulateDrawEvent();
1030 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
1031 SK_ColorBLUE, arbitrary_source_size));
1033 // Source size changes to something arbitrary that exceeds the maximum frame
1034 // size. Since the source size exceeds the maximum size, expect delivered
1035 // frames to be downscaled.
1036 source()->SetSolidColor(SK_ColorBLACK);
1037 arbitrary_source_size = gfx::Size(kTestWidth * 2, kTestHeight / 2);
1038 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(device_scale_factor,
1039 arbitrary_source_size));
1040 SimulateDrawEvent();
1041 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
1042 SK_ColorBLACK, gfx::Size(kTestWidth,
1043 kTestWidth * arbitrary_source_size.height() /
1044 arbitrary_source_size.width())));
1046 device()->StopAndDeAllocate();
1049 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest,
1050 ComputesStandardResolutionsForPreferredSize) {
1051 // Helper function to run the same testing procedure for multiple combinations
1052 // of |policy|, |standard_size| and |oddball_size|.
1053 const auto RunTestForPreferredSize =
1054 [=](media::ResolutionChangePolicy policy,
1055 const gfx::Size& oddball_size,
1056 const gfx::Size& standard_size) {
1057 SCOPED_TRACE(::testing::Message()
1058 << "policy=" << policy
1059 << ", oddball_size=" << oddball_size.ToString()
1060 << ", standard_size=" << standard_size.ToString());
1062 // Compute the expected preferred size. For the fixed-resolution use case,
1063 // the |oddball_size| is always the expected size; whereas for the
1064 // variable-resolution cases, the |standard_size| is the expected size.
1065 // Also, adjust to account for the device scale factor.
1066 gfx::Size capture_preferred_size = gfx::ToFlooredSize(gfx::ScaleSize(
1067 policy == media::RESOLUTION_POLICY_FIXED_RESOLUTION ?
1068 oddball_size : standard_size,
1069 1.0f / GetDeviceScaleFactor()));
1070 ASSERT_NE(capture_preferred_size, web_contents()->GetPreferredSize());
1072 // Start the WebContentsVideoCaptureDevice.
1073 media::VideoCaptureParams capture_params;
1074 capture_params.requested_format.frame_size = oddball_size;
1075 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
1076 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
1077 capture_params.resolution_change_policy = policy;
1078 StubClientObserver unused_observer;
1079 device()->AllocateAndStart(capture_params, unused_observer.PassClient());
1080 base::RunLoop().RunUntilIdle();
1082 // Check that the preferred size of the WebContents matches the one provided
1083 // by WebContentsVideoCaptureDevice.
1084 EXPECT_EQ(capture_preferred_size, web_contents()->GetPreferredSize());
1086 // Stop the WebContentsVideoCaptureDevice.
1087 device()->StopAndDeAllocate();
1088 base::RunLoop().RunUntilIdle();
1091 const media::ResolutionChangePolicy policies[3] = {
1092 media::RESOLUTION_POLICY_FIXED_RESOLUTION,
1093 media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO,
1094 media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT,
1097 for (size_t i = 0; i < arraysize(policies); ++i) {
1098 // A 16:9 standard resolution should be set as the preferred size when the
1099 // source size is almost or exactly 16:9.
1100 for (int delta_w = 0; delta_w <= +5; ++delta_w) {
1101 for (int delta_h = 0; delta_h <= +5; ++delta_h) {
1102 RunTestForPreferredSize(policies[i],
1103 gfx::Size(1280 + delta_w, 720 + delta_h),
1104 gfx::Size(1280, 720));
1107 for (int delta_w = -5; delta_w <= +5; ++delta_w) {
1108 for (int delta_h = -5; delta_h <= +5; ++delta_h) {
1109 RunTestForPreferredSize(policies[i],
1110 gfx::Size(1365 + delta_w, 768 + delta_h),
1111 gfx::Size(1280, 720));
1115 // A 4:3 standard resolution should be set as the preferred size when the
1116 // source size is almost or exactly 4:3.
1117 for (int delta_w = 0; delta_w <= +5; ++delta_w) {
1118 for (int delta_h = 0; delta_h <= +5; ++delta_h) {
1119 RunTestForPreferredSize(policies[i],
1120 gfx::Size(640 + delta_w, 480 + delta_h),
1121 gfx::Size(640, 480));
1124 for (int delta_w = -5; delta_w <= +5; ++delta_w) {
1125 for (int delta_h = -5; delta_h <= +5; ++delta_h) {
1126 RunTestForPreferredSize(policies[i],
1127 gfx::Size(800 + delta_w, 600 + delta_h),
1128 gfx::Size(768, 576));
1132 // When the source size is not a common video aspect ratio, there is no
1133 // adjustment made.
1134 RunTestForPreferredSize(
1135 policies[i], gfx::Size(1000, 1000), gfx::Size(1000, 1000));
1136 RunTestForPreferredSize(
1137 policies[i], gfx::Size(1600, 1000), gfx::Size(1600, 1000));
1138 RunTestForPreferredSize(
1139 policies[i], gfx::Size(837, 999), gfx::Size(837, 999));
1143 } // namespace
1144 } // namespace content