Add ICU message format support
[chromium-blink-merge.git] / content / browser / media / capture / web_contents_video_capture_device_unittest.cc
blob6067f659b7be7d3302bba0221039b7d71f8dedea
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 int routing_id,
251 int main_frame_routing_id,
252 bool swapped_out,
253 CaptureTestSourceController* controller)
254 : TestRenderViewHost(instance, delegate, widget_delegate, routing_id,
255 main_frame_routing_id, swapped_out),
256 controller_(controller) {
257 // Override the default view installed by TestRenderViewHost; we need
258 // our special subclass which has mocked-out tab capture support.
259 RenderWidgetHostView* old_view = GetView();
260 SetView(new CaptureTestView(this, controller));
261 delete old_view;
264 // TestRenderViewHost overrides.
265 void CopyFromBackingStore(const gfx::Rect& src_rect,
266 const gfx::Size& accelerated_dst_size,
267 ReadbackRequestCallback& callback,
268 const SkColorType color_type) override {
269 gfx::Size size = controller_->GetCopyResultSize();
270 SkColor color = controller_->GetSolidColor();
272 // Although it's not necessary, use a PlatformBitmap here (instead of a
273 // regular SkBitmap) to exercise possible threading issues.
274 skia::PlatformBitmap output;
275 EXPECT_TRUE(output.Allocate(size.width(), size.height(), false));
277 SkAutoLockPixels locker(output.GetBitmap());
278 output.GetBitmap().eraseColor(color);
280 callback.Run(output.GetBitmap(), content::READBACK_SUCCESS);
281 controller_->SignalCopy();
284 private:
285 CaptureTestSourceController* controller_;
287 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHost);
290 #if defined(COMPILER_MSVC)
291 // Re-enable warning 4250
292 #pragma warning(pop)
293 #endif
295 class CaptureTestRenderViewHostFactory : public RenderViewHostFactory {
296 public:
297 explicit CaptureTestRenderViewHostFactory(
298 CaptureTestSourceController* controller) : controller_(controller) {
299 RegisterFactory(this);
302 ~CaptureTestRenderViewHostFactory() override { UnregisterFactory(); }
304 // RenderViewHostFactory implementation.
305 RenderViewHost* CreateRenderViewHost(
306 SiteInstance* instance,
307 RenderViewHostDelegate* delegate,
308 RenderWidgetHostDelegate* widget_delegate,
309 int routing_id,
310 int main_frame_routing_id,
311 bool swapped_out) override {
312 return new CaptureTestRenderViewHost(instance, delegate, widget_delegate,
313 routing_id, main_frame_routing_id,
314 swapped_out, controller_);
316 private:
317 CaptureTestSourceController* controller_;
319 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHostFactory);
322 // A stub consumer of captured video frames, which checks the output of
323 // WebContentsVideoCaptureDevice.
324 class StubClient : public media::VideoCaptureDevice::Client {
325 public:
326 StubClient(
327 const base::Callback<void(SkColor, const gfx::Size&)>& report_callback,
328 const base::Closure& error_callback)
329 : report_callback_(report_callback),
330 error_callback_(error_callback) {
331 buffer_pool_ = new VideoCaptureBufferPool(2);
333 ~StubClient() override {}
335 MOCK_METHOD5(OnIncomingCapturedData,
336 void(const uint8* data,
337 int length,
338 const media::VideoCaptureFormat& frame_format,
339 int rotation,
340 const base::TimeTicks& timestamp));
341 MOCK_METHOD9(OnIncomingCapturedYuvData,
342 void (const uint8* y_data,
343 const uint8* u_data,
344 const uint8* v_data,
345 size_t y_stride,
346 size_t u_stride,
347 size_t v_stride,
348 const media::VideoCaptureFormat& frame_format,
349 int clockwise_rotation,
350 const base::TimeTicks& timestamp));
352 MOCK_METHOD0(DoOnIncomingCapturedBuffer, void(void));
354 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> ReserveOutputBuffer(
355 const gfx::Size& dimensions,
356 media::VideoCapturePixelFormat format,
357 media::VideoPixelStorage storage) override {
358 CHECK_EQ(format, media::VIDEO_CAPTURE_PIXEL_FORMAT_I420);
359 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId; // Ignored.
360 const int buffer_id = buffer_pool_->ReserveForProducer(
361 format, storage, dimensions, &buffer_id_to_drop);
362 if (buffer_id == VideoCaptureBufferPool::kInvalidId)
363 return NULL;
365 return scoped_ptr<media::VideoCaptureDevice::Client::Buffer>(
366 new AutoReleaseBuffer(
367 buffer_pool_, buffer_pool_->GetBufferHandle(buffer_id), buffer_id));
369 // Trampoline method to workaround GMOCK problems with scoped_ptr<>.
370 void OnIncomingCapturedBuffer(scoped_ptr<Buffer> buffer,
371 const media::VideoCaptureFormat& frame_format,
372 const base::TimeTicks& timestamp) override {
373 DoOnIncomingCapturedBuffer();
376 void OnIncomingCapturedVideoFrame(
377 scoped_ptr<Buffer> buffer,
378 const scoped_refptr<media::VideoFrame>& frame,
379 const base::TimeTicks& timestamp) override {
380 EXPECT_FALSE(frame->visible_rect().IsEmpty());
381 EXPECT_EQ(media::PIXEL_FORMAT_I420, frame->format());
382 double frame_rate = 0;
383 EXPECT_TRUE(
384 frame->metadata()->GetDouble(media::VideoFrameMetadata::FRAME_RATE,
385 &frame_rate));
386 EXPECT_EQ(kTestFramesPerSecond, frame_rate);
388 // TODO(miu): We just look at the center pixel presently, because if the
389 // analysis is too slow, the backlog of frames will grow without bound and
390 // trouble erupts. http://crbug.com/174519
391 using media::VideoFrame;
392 const gfx::Point center = frame->visible_rect().CenterPoint();
393 const int center_offset_y =
394 (frame->stride(VideoFrame::kYPlane) * center.y()) + center.x();
395 const int center_offset_uv =
396 (frame->stride(VideoFrame::kUPlane) * (center.y() / 2)) +
397 (center.x() / 2);
398 report_callback_.Run(
399 SkColorSetRGB(frame->data(VideoFrame::kYPlane)[center_offset_y],
400 frame->data(VideoFrame::kUPlane)[center_offset_uv],
401 frame->data(VideoFrame::kVPlane)[center_offset_uv]),
402 frame->visible_rect().size());
405 void OnError(const std::string& reason) override { error_callback_.Run(); }
407 double GetBufferPoolUtilization() const override { return 0.0; }
409 private:
410 class AutoReleaseBuffer : public media::VideoCaptureDevice::Client::Buffer {
411 public:
412 AutoReleaseBuffer(
413 const scoped_refptr<VideoCaptureBufferPool>& pool,
414 scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle,
415 int buffer_id)
416 : id_(buffer_id),
417 pool_(pool),
418 buffer_handle_(buffer_handle.Pass()) {
419 DCHECK(pool_.get());
421 int id() const override { return id_; }
422 size_t size() const override { return buffer_handle_->size(); }
423 void* data() override { return buffer_handle_->data(); }
424 ClientBuffer AsClientBuffer() override { return nullptr; }
425 #if defined(OS_POSIX)
426 base::FileDescriptor AsPlatformFile() override {
427 return base::FileDescriptor();
429 #endif
431 private:
432 ~AutoReleaseBuffer() override { pool_->RelinquishProducerReservation(id_); }
434 const int id_;
435 const scoped_refptr<VideoCaptureBufferPool> pool_;
436 const scoped_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle_;
439 scoped_refptr<VideoCaptureBufferPool> buffer_pool_;
440 base::Callback<void(SkColor, const gfx::Size&)> report_callback_;
441 base::Closure error_callback_;
443 DISALLOW_COPY_AND_ASSIGN(StubClient);
446 class StubClientObserver {
447 public:
448 StubClientObserver()
449 : error_encountered_(false),
450 wait_color_yuv_(0xcafe1950),
451 wait_size_(kTestWidth, kTestHeight) {
452 client_.reset(new StubClient(
453 base::Bind(&StubClientObserver::DidDeliverFrame,
454 base::Unretained(this)),
455 base::Bind(&StubClientObserver::OnError, base::Unretained(this))));
458 virtual ~StubClientObserver() {}
460 scoped_ptr<media::VideoCaptureDevice::Client> PassClient() {
461 return client_.Pass();
464 void QuitIfConditionsMet(SkColor color, const gfx::Size& size) {
465 base::AutoLock guard(lock_);
466 if (error_encountered_)
467 base::MessageLoop::current()->Quit();
468 else if (wait_color_yuv_ == color && wait_size_.IsEmpty())
469 base::MessageLoop::current()->Quit();
470 else if (wait_color_yuv_ == color && wait_size_ == size)
471 base::MessageLoop::current()->Quit();
474 // Run the current loop until a frame is delivered with the |expected_color|
475 // and any non-empty frame size.
476 void WaitForNextColor(SkColor expected_color) {
477 WaitForNextColorAndFrameSize(expected_color, gfx::Size());
480 // Run the current loop until a frame is delivered with the |expected_color|
481 // and is of the |expected_size|.
482 void WaitForNextColorAndFrameSize(SkColor expected_color,
483 const gfx::Size& expected_size) {
485 base::AutoLock guard(lock_);
486 wait_color_yuv_ = ConvertRgbToYuv(expected_color);
487 wait_size_ = expected_size;
488 error_encountered_ = false;
490 RunCurrentLoopWithDeadline();
492 base::AutoLock guard(lock_);
493 ASSERT_FALSE(error_encountered_);
497 void WaitForError() {
499 base::AutoLock guard(lock_);
500 wait_color_yuv_ = kNotInterested;
501 wait_size_ = gfx::Size();
502 error_encountered_ = false;
504 RunCurrentLoopWithDeadline();
506 base::AutoLock guard(lock_);
507 ASSERT_TRUE(error_encountered_);
511 bool HasError() {
512 base::AutoLock guard(lock_);
513 return error_encountered_;
516 void OnError() {
518 base::AutoLock guard(lock_);
519 error_encountered_ = true;
521 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
522 &StubClientObserver::QuitIfConditionsMet,
523 base::Unretained(this),
524 kNothingYet,
525 gfx::Size()));
528 void DidDeliverFrame(SkColor color, const gfx::Size& size) {
529 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
530 &StubClientObserver::QuitIfConditionsMet,
531 base::Unretained(this),
532 color,
533 size));
536 private:
537 base::Lock lock_;
538 bool error_encountered_;
539 SkColor wait_color_yuv_;
540 gfx::Size wait_size_;
541 scoped_ptr<StubClient> client_;
543 DISALLOW_COPY_AND_ASSIGN(StubClientObserver);
546 // Test harness that sets up a minimal environment with necessary stubs.
547 class WebContentsVideoCaptureDeviceTest : public testing::Test {
548 public:
549 // This is public because C++ method pointer scoping rules are silly and make
550 // this hard to use with Bind().
551 void ResetWebContents() {
552 web_contents_.reset();
555 protected:
556 void SetUp() override {
557 test_screen_.display()->set_id(0x1337);
558 test_screen_.display()->set_bounds(gfx::Rect(0, 0, 2560, 1440));
559 test_screen_.display()->set_device_scale_factor(kTestDeviceScaleFactor);
561 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, &test_screen_);
562 ASSERT_EQ(&test_screen_, gfx::Screen::GetNativeScreen());
564 // TODO(nick): Sadness and woe! Much "mock-the-world" boilerplate could be
565 // eliminated here, if only we could use RenderViewHostTestHarness. The
566 // catch is that we need our TestRenderViewHost to support a
567 // CopyFromBackingStore operation that we control. To accomplish that,
568 // either RenderViewHostTestHarness would have to support installing a
569 // custom RenderViewHostFactory, or else we implant some kind of delegated
570 // CopyFromBackingStore functionality into TestRenderViewHost itself.
572 render_process_host_factory_.reset(new MockRenderProcessHostFactory());
573 // Create our (self-registering) RVH factory, so that when we create a
574 // WebContents, it in turn creates CaptureTestRenderViewHosts.
575 render_view_host_factory_.reset(
576 new CaptureTestRenderViewHostFactory(&controller_));
577 render_frame_host_factory_.reset(new TestRenderFrameHostFactory());
579 browser_context_.reset(new TestBrowserContext());
581 scoped_refptr<SiteInstance> site_instance =
582 SiteInstance::Create(browser_context_.get());
583 SiteInstanceImpl::set_render_process_host_factory(
584 render_process_host_factory_.get());
585 web_contents_.reset(
586 TestWebContents::Create(browser_context_.get(), site_instance.get()));
587 RenderFrameHost* const main_frame = web_contents_->GetMainFrame();
588 device_.reset(WebContentsVideoCaptureDevice::Create(
589 base::StringPrintf("web-contents-media-stream://%d:%d",
590 main_frame->GetProcess()->GetID(),
591 main_frame->GetRoutingID())));
593 base::RunLoop().RunUntilIdle();
596 void TearDown() override {
597 // Tear down in opposite order of set-up.
599 // The device is destroyed asynchronously, and will notify the
600 // CaptureTestSourceController when it finishes destruction.
601 // Trigger this, and wait.
602 if (device_) {
603 device_->StopAndDeAllocate();
604 device_.reset();
607 base::RunLoop().RunUntilIdle();
609 // Destroy the browser objects.
610 web_contents_.reset();
611 browser_context_.reset();
613 base::RunLoop().RunUntilIdle();
615 SiteInstanceImpl::set_render_process_host_factory(NULL);
616 render_frame_host_factory_.reset();
617 render_view_host_factory_.reset();
618 render_process_host_factory_.reset();
620 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL);
623 // Accessors.
624 CaptureTestSourceController* source() { return &controller_; }
625 WebContents* web_contents() const { return web_contents_.get(); }
626 media::VideoCaptureDevice* device() { return device_.get(); }
628 // Returns the device scale factor of the capture target's native view. This
629 // is necessary because, architecturally, the TestScreen implementation is
630 // ignored on Mac platforms (when determining the device scale factor for a
631 // particular window).
632 float GetDeviceScaleFactor() const {
633 RenderWidgetHostView* const view =
634 web_contents_->GetRenderViewHost()->GetView();
635 CHECK(view);
636 return ui::GetScaleFactorForNativeView(view->GetNativeView());
639 void SimulateDrawEvent() {
640 if (source()->CanUseFrameSubscriber()) {
641 // Print
642 CaptureTestView* test_view = static_cast<CaptureTestView*>(
643 web_contents_->GetRenderViewHost()->GetView());
644 test_view->SimulateUpdate();
645 } else {
646 // Simulate a non-accelerated paint.
647 NotificationService::current()->Notify(
648 NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE,
649 Source<RenderWidgetHost>(web_contents_->GetRenderViewHost()),
650 NotificationService::NoDetails());
654 void SimulateSourceSizeChange(const gfx::Size& size) {
655 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
656 CaptureTestView* test_view = static_cast<CaptureTestView*>(
657 web_contents_->GetRenderViewHost()->GetView());
658 test_view->SetSize(size);
659 // Normally, RenderWidgetHostImpl would notify WebContentsImpl that the size
660 // has changed. However, in this test setup where there is no render
661 // process, we must notify WebContentsImpl directly.
662 WebContentsImpl* const as_web_contents_impl =
663 static_cast<WebContentsImpl*>(web_contents_.get());
664 RenderWidgetHostDelegate* const as_rwh_delegate =
665 static_cast<RenderWidgetHostDelegate*>(as_web_contents_impl);
666 as_rwh_delegate->RenderWidgetWasResized(
667 as_web_contents_impl->GetMainFrame()->GetRenderWidgetHost(), true);
670 void DestroyVideoCaptureDevice() { device_.reset(); }
672 StubClientObserver* client_observer() {
673 return &client_observer_;
676 private:
677 gfx::test::TestScreen test_screen_;
679 StubClientObserver client_observer_;
681 // The controller controls which pixel patterns to produce.
682 CaptureTestSourceController controller_;
684 // Self-registering RenderProcessHostFactory.
685 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory_;
687 // Creates capture-capable RenderViewHosts whose pixel content production is
688 // under the control of |controller_|.
689 scoped_ptr<CaptureTestRenderViewHostFactory> render_view_host_factory_;
691 // Self-registering RenderFrameHostFactory.
692 scoped_ptr<TestRenderFrameHostFactory> render_frame_host_factory_;
694 // A mocked-out browser and tab.
695 scoped_ptr<TestBrowserContext> browser_context_;
696 scoped_ptr<WebContents> web_contents_;
698 // Finally, the WebContentsVideoCaptureDevice under test.
699 scoped_ptr<media::VideoCaptureDevice> device_;
701 TestBrowserThreadBundle thread_bundle_;
704 TEST_F(WebContentsVideoCaptureDeviceTest, InvalidInitialWebContentsError) {
705 // Before the installs itself on the UI thread up to start capturing, we'll
706 // delete the web contents. This should trigger an error which can happen in
707 // practice; we should be able to recover gracefully.
708 ResetWebContents();
710 media::VideoCaptureParams capture_params;
711 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
712 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
713 capture_params.requested_format.pixel_format =
714 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
715 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
716 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
717 device()->StopAndDeAllocate();
720 TEST_F(WebContentsVideoCaptureDeviceTest, WebContentsDestroyed) {
721 const float device_scale_factor = GetDeviceScaleFactor();
722 const gfx::Size capture_preferred_size(
723 static_cast<int>(kTestWidth / device_scale_factor),
724 static_cast<int>(kTestHeight / device_scale_factor));
725 ASSERT_NE(capture_preferred_size, web_contents()->GetPreferredSize());
727 // We'll simulate the tab being closed after the capture pipeline is up and
728 // running.
729 media::VideoCaptureParams capture_params;
730 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
731 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
732 capture_params.requested_format.pixel_format =
733 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
734 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
735 // Do one capture to prove
736 source()->SetSolidColor(SK_ColorRED);
737 SimulateDrawEvent();
738 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
740 base::RunLoop().RunUntilIdle();
742 // Check that the preferred size of the WebContents matches the one provided
743 // by WebContentsVideoCaptureDevice.
744 EXPECT_EQ(capture_preferred_size, web_contents()->GetPreferredSize());
746 // Post a task to close the tab. We should see an error reported to the
747 // consumer.
748 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
749 base::Bind(&WebContentsVideoCaptureDeviceTest::ResetWebContents,
750 base::Unretained(this)));
751 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
752 device()->StopAndDeAllocate();
755 TEST_F(WebContentsVideoCaptureDeviceTest,
756 StopDeviceBeforeCaptureMachineCreation) {
757 media::VideoCaptureParams capture_params;
758 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
759 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
760 capture_params.requested_format.pixel_format =
761 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
762 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
764 // Make a point of not running the UI messageloop here.
765 device()->StopAndDeAllocate();
766 DestroyVideoCaptureDevice();
768 // Currently, there should be CreateCaptureMachineOnUIThread() and
769 // DestroyCaptureMachineOnUIThread() tasks pending on the current (UI) message
770 // loop. These should both succeed without crashing, and the machine should
771 // wind up in the idle state.
772 base::RunLoop().RunUntilIdle();
775 TEST_F(WebContentsVideoCaptureDeviceTest, StopWithRendererWorkToDo) {
776 // Set up the test to use RGB copies and an normal
777 source()->SetCanCopyToVideoFrame(false);
778 source()->SetUseFrameSubscriber(false);
779 media::VideoCaptureParams capture_params;
780 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
781 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
782 capture_params.requested_format.pixel_format =
783 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
784 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
786 base::RunLoop().RunUntilIdle();
788 for (int i = 0; i < 10; ++i)
789 SimulateDrawEvent();
791 ASSERT_FALSE(client_observer()->HasError());
792 device()->StopAndDeAllocate();
793 ASSERT_FALSE(client_observer()->HasError());
794 base::RunLoop().RunUntilIdle();
795 ASSERT_FALSE(client_observer()->HasError());
798 TEST_F(WebContentsVideoCaptureDeviceTest, DeviceRestart) {
799 media::VideoCaptureParams capture_params;
800 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
801 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
802 capture_params.requested_format.pixel_format =
803 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
804 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
805 base::RunLoop().RunUntilIdle();
806 source()->SetSolidColor(SK_ColorRED);
807 SimulateDrawEvent();
808 SimulateDrawEvent();
809 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
810 SimulateDrawEvent();
811 SimulateDrawEvent();
812 source()->SetSolidColor(SK_ColorGREEN);
813 SimulateDrawEvent();
814 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN));
815 device()->StopAndDeAllocate();
817 // Device is stopped, but content can still be animating.
818 SimulateDrawEvent();
819 SimulateDrawEvent();
820 base::RunLoop().RunUntilIdle();
822 StubClientObserver observer2;
823 device()->AllocateAndStart(capture_params, observer2.PassClient());
824 source()->SetSolidColor(SK_ColorBLUE);
825 SimulateDrawEvent();
826 ASSERT_NO_FATAL_FAILURE(observer2.WaitForNextColor(SK_ColorBLUE));
827 source()->SetSolidColor(SK_ColorYELLOW);
828 SimulateDrawEvent();
829 ASSERT_NO_FATAL_FAILURE(observer2.WaitForNextColor(SK_ColorYELLOW));
830 device()->StopAndDeAllocate();
833 // The "happy case" test. No scaling is needed, so we should be able to change
834 // the picture emitted from the source and expect to see each delivered to the
835 // consumer. The test will alternate between the three capture paths, simulating
836 // falling in and out of accelerated compositing.
837 TEST_F(WebContentsVideoCaptureDeviceTest, GoesThroughAllTheMotions) {
838 media::VideoCaptureParams capture_params;
839 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
840 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
841 capture_params.requested_format.pixel_format =
842 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
843 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
845 for (int i = 0; i < 6; i++) {
846 const char* name = NULL;
847 switch (i % 3) {
848 case 0:
849 source()->SetCanCopyToVideoFrame(true);
850 source()->SetUseFrameSubscriber(false);
851 name = "VideoFrame";
852 break;
853 case 1:
854 source()->SetCanCopyToVideoFrame(false);
855 source()->SetUseFrameSubscriber(true);
856 name = "Subscriber";
857 break;
858 case 2:
859 source()->SetCanCopyToVideoFrame(false);
860 source()->SetUseFrameSubscriber(false);
861 name = "SkBitmap";
862 break;
863 default:
864 FAIL();
867 SCOPED_TRACE(base::StringPrintf("Using %s path, iteration #%d", name, i));
869 source()->SetSolidColor(SK_ColorRED);
870 SimulateDrawEvent();
871 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
873 source()->SetSolidColor(SK_ColorGREEN);
874 SimulateDrawEvent();
875 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN));
877 source()->SetSolidColor(SK_ColorBLUE);
878 SimulateDrawEvent();
879 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLUE));
881 source()->SetSolidColor(SK_ColorBLACK);
882 SimulateDrawEvent();
883 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLACK));
885 device()->StopAndDeAllocate();
888 TEST_F(WebContentsVideoCaptureDeviceTest, BadFramesGoodFrames) {
889 media::VideoCaptureParams capture_params;
890 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
891 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
892 capture_params.requested_format.pixel_format =
893 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
894 // 1x1 is too small to process; we intend for this to result in an error.
895 source()->SetCopyResultSize(1, 1);
896 source()->SetSolidColor(SK_ColorRED);
897 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
899 // These frames ought to be dropped during the Render stage. Let
900 // several captures to happen.
901 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
902 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
903 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
904 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
905 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
907 // Now push some good frames through; they should be processed normally.
908 source()->SetCopyResultSize(kTestWidth, kTestHeight);
909 source()->SetSolidColor(SK_ColorGREEN);
910 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN));
911 source()->SetSolidColor(SK_ColorRED);
912 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
914 device()->StopAndDeAllocate();
917 // Tests that, when configured with the FIXED_ASPECT_RATIO resolution change
918 // policy, the source size changes result in video frames of possibly varying
919 // resolutions, but all with the same aspect ratio.
920 TEST_F(WebContentsVideoCaptureDeviceTest, VariableResolution_FixedAspectRatio) {
921 media::VideoCaptureParams capture_params;
922 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
923 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
924 capture_params.requested_format.pixel_format =
925 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
926 capture_params.resolution_change_policy =
927 media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO;
929 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
931 source()->SetUseFrameSubscriber(true);
933 // Source size equals maximum size. Expect delivered frames to be
934 // kTestWidth by kTestHeight.
935 source()->SetSolidColor(SK_ColorRED);
936 const float device_scale_factor = GetDeviceScaleFactor();
937 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
938 device_scale_factor, gfx::Size(kTestWidth, kTestHeight)));
939 SimulateDrawEvent();
940 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
941 SK_ColorRED, gfx::Size(kTestWidth, kTestHeight)));
943 // Source size is half in both dimensions. Expect delivered frames to be of
944 // the same aspect ratio as kTestWidth by kTestHeight, but larger than the
945 // half size because the minimum height is 180 lines.
946 source()->SetSolidColor(SK_ColorGREEN);
947 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
948 device_scale_factor, gfx::Size(kTestWidth / 2, kTestHeight / 2)));
949 SimulateDrawEvent();
950 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
951 SK_ColorGREEN, gfx::Size(180 * kTestWidth / kTestHeight, 180)));
953 // Source size changes aspect ratio. Expect delivered frames to be padded
954 // in the horizontal dimension to preserve aspect ratio.
955 source()->SetSolidColor(SK_ColorBLUE);
956 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
957 device_scale_factor, gfx::Size(kTestWidth / 2, kTestHeight)));
958 SimulateDrawEvent();
959 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
960 SK_ColorBLUE, gfx::Size(kTestWidth, kTestHeight)));
962 // Source size changes aspect ratio again. Expect delivered frames to be
963 // padded in the vertical dimension to preserve aspect ratio.
964 source()->SetSolidColor(SK_ColorBLACK);
965 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
966 device_scale_factor, gfx::Size(kTestWidth, kTestHeight / 2)));
967 SimulateDrawEvent();
968 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
969 SK_ColorBLACK, gfx::Size(kTestWidth, kTestHeight)));
971 device()->StopAndDeAllocate();
974 // Tests that, when configured with the ANY_WITHIN_LIMIT resolution change
975 // policy, the source size changes result in video frames of possibly varying
976 // resolutions.
977 TEST_F(WebContentsVideoCaptureDeviceTest, VariableResolution_AnyWithinLimits) {
978 media::VideoCaptureParams capture_params;
979 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
980 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
981 capture_params.requested_format.pixel_format =
982 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
983 capture_params.resolution_change_policy =
984 media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT;
986 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
988 source()->SetUseFrameSubscriber(true);
990 // Source size equals maximum size. Expect delivered frames to be
991 // kTestWidth by kTestHeight.
992 source()->SetSolidColor(SK_ColorRED);
993 const float device_scale_factor = GetDeviceScaleFactor();
994 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
995 device_scale_factor, gfx::Size(kTestWidth, kTestHeight)));
996 SimulateDrawEvent();
997 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
998 SK_ColorRED, gfx::Size(kTestWidth, kTestHeight)));
1000 // Source size is half in both dimensions. Expect delivered frames to also
1001 // be half in both dimensions.
1002 source()->SetSolidColor(SK_ColorGREEN);
1003 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(
1004 device_scale_factor, gfx::Size(kTestWidth / 2, kTestHeight / 2)));
1005 SimulateDrawEvent();
1006 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
1007 SK_ColorGREEN, gfx::Size(kTestWidth / 2, kTestHeight / 2)));
1009 // Source size changes to something arbitrary. Since the source size is
1010 // less than the maximum size, expect delivered frames to be the same size
1011 // as the source size.
1012 source()->SetSolidColor(SK_ColorBLUE);
1013 gfx::Size arbitrary_source_size(kTestWidth / 2 + 42, kTestHeight - 10);
1014 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(device_scale_factor,
1015 arbitrary_source_size));
1016 SimulateDrawEvent();
1017 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
1018 SK_ColorBLUE, arbitrary_source_size));
1020 // Source size changes to something arbitrary that exceeds the maximum frame
1021 // size. Since the source size exceeds the maximum size, expect delivered
1022 // frames to be downscaled.
1023 source()->SetSolidColor(SK_ColorBLACK);
1024 arbitrary_source_size = gfx::Size(kTestWidth * 2, kTestHeight / 2);
1025 SimulateSourceSizeChange(gfx::ConvertSizeToDIP(device_scale_factor,
1026 arbitrary_source_size));
1027 SimulateDrawEvent();
1028 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColorAndFrameSize(
1029 SK_ColorBLACK, gfx::Size(kTestWidth,
1030 kTestWidth * arbitrary_source_size.height() /
1031 arbitrary_source_size.width())));
1033 device()->StopAndDeAllocate();
1036 TEST_F(WebContentsVideoCaptureDeviceTest,
1037 ComputesStandardResolutionsForPreferredSize) {
1038 // Helper function to run the same testing procedure for multiple combinations
1039 // of |policy|, |standard_size| and |oddball_size|.
1040 const auto RunTestForPreferredSize =
1041 [=](media::ResolutionChangePolicy policy,
1042 const gfx::Size& oddball_size,
1043 const gfx::Size& standard_size) {
1044 SCOPED_TRACE(::testing::Message()
1045 << "policy=" << policy
1046 << ", oddball_size=" << oddball_size.ToString()
1047 << ", standard_size=" << standard_size.ToString());
1049 // Compute the expected preferred size. For the fixed-resolution use case,
1050 // the |oddball_size| is always the expected size; whereas for the
1051 // variable-resolution cases, the |standard_size| is the expected size.
1052 // Also, adjust to account for the device scale factor.
1053 gfx::Size capture_preferred_size = gfx::ToFlooredSize(gfx::ScaleSize(
1054 policy == media::RESOLUTION_POLICY_FIXED_RESOLUTION ?
1055 oddball_size : standard_size,
1056 1.0f / GetDeviceScaleFactor()));
1057 ASSERT_NE(capture_preferred_size, web_contents()->GetPreferredSize());
1059 // Start the WebContentsVideoCaptureDevice.
1060 media::VideoCaptureParams capture_params;
1061 capture_params.requested_format.frame_size = oddball_size;
1062 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
1063 capture_params.requested_format.pixel_format =
1064 media::VIDEO_CAPTURE_PIXEL_FORMAT_I420;
1065 capture_params.resolution_change_policy = policy;
1066 StubClientObserver unused_observer;
1067 device()->AllocateAndStart(capture_params, unused_observer.PassClient());
1068 base::RunLoop().RunUntilIdle();
1070 // Check that the preferred size of the WebContents matches the one provided
1071 // by WebContentsVideoCaptureDevice.
1072 EXPECT_EQ(capture_preferred_size, web_contents()->GetPreferredSize());
1074 // Stop the WebContentsVideoCaptureDevice.
1075 device()->StopAndDeAllocate();
1076 base::RunLoop().RunUntilIdle();
1079 const media::ResolutionChangePolicy policies[3] = {
1080 media::RESOLUTION_POLICY_FIXED_RESOLUTION,
1081 media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO,
1082 media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT,
1085 for (size_t i = 0; i < arraysize(policies); ++i) {
1086 // A 16:9 standard resolution should be set as the preferred size when the
1087 // source size is almost or exactly 16:9.
1088 for (int delta_w = 0; delta_w <= +5; ++delta_w) {
1089 for (int delta_h = 0; delta_h <= +5; ++delta_h) {
1090 RunTestForPreferredSize(policies[i],
1091 gfx::Size(1280 + delta_w, 720 + delta_h),
1092 gfx::Size(1280, 720));
1095 for (int delta_w = -5; delta_w <= +5; ++delta_w) {
1096 for (int delta_h = -5; delta_h <= +5; ++delta_h) {
1097 RunTestForPreferredSize(policies[i],
1098 gfx::Size(1365 + delta_w, 768 + delta_h),
1099 gfx::Size(1280, 720));
1103 // A 4:3 standard resolution should be set as the preferred size when the
1104 // source size is almost or exactly 4:3.
1105 for (int delta_w = 0; delta_w <= +5; ++delta_w) {
1106 for (int delta_h = 0; delta_h <= +5; ++delta_h) {
1107 RunTestForPreferredSize(policies[i],
1108 gfx::Size(640 + delta_w, 480 + delta_h),
1109 gfx::Size(640, 480));
1112 for (int delta_w = -5; delta_w <= +5; ++delta_w) {
1113 for (int delta_h = -5; delta_h <= +5; ++delta_h) {
1114 RunTestForPreferredSize(policies[i],
1115 gfx::Size(800 + delta_w, 600 + delta_h),
1116 gfx::Size(768, 576));
1120 // When the source size is not a common video aspect ratio, there is no
1121 // adjustment made.
1122 RunTestForPreferredSize(
1123 policies[i], gfx::Size(1000, 1000), gfx::Size(1000, 1000));
1124 RunTestForPreferredSize(
1125 policies[i], gfx::Size(1600, 1000), gfx::Size(1600, 1000));
1126 RunTestForPreferredSize(
1127 policies[i], gfx::Size(837, 999), gfx::Size(837, 999));
1131 } // namespace
1132 } // namespace content