ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / browser / media / capture / web_contents_video_capture_device_unittest.cc
blob59dffc48b3ff6207d805c5408ce19a14eddff211
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/gtest/include/gtest/gtest.h"
34 #include "third_party/skia/include/core/SkColor.h"
35 #include "ui/gfx/display.h"
36 #include "ui/gfx/screen.h"
38 namespace content {
39 namespace {
41 const int kTestWidth = 320;
42 const int kTestHeight = 240;
43 const int kTestFramesPerSecond = 20;
44 const float kTestDeviceScaleFactor = 2.0f;
45 const SkColor kNothingYet = 0xdeadbeef;
46 const SkColor kNotInterested = ~kNothingYet;
48 void DeadlineExceeded(base::Closure quit_closure) {
49 if (!base::debug::BeingDebugged()) {
50 quit_closure.Run();
51 FAIL() << "Deadline exceeded while waiting, quitting";
52 } else {
53 LOG(WARNING) << "Deadline exceeded; test would fail if debugger weren't "
54 << "attached.";
58 void RunCurrentLoopWithDeadline() {
59 base::Timer deadline(false, false);
60 deadline.Start(FROM_HERE, TestTimeouts::action_max_timeout(), base::Bind(
61 &DeadlineExceeded, base::MessageLoop::current()->QuitClosure()));
62 base::MessageLoop::current()->Run();
63 deadline.Stop();
66 SkColor ConvertRgbToYuv(SkColor rgb) {
67 uint8 yuv[3];
68 media::ConvertRGB32ToYUV(reinterpret_cast<uint8*>(&rgb),
69 yuv, yuv + 1, yuv + 2, 1, 1, 1, 1, 1);
70 return SkColorSetRGB(yuv[0], yuv[1], yuv[2]);
73 // Thread-safe class that controls the source pattern to be captured by the
74 // system under test. The lifetime of this class is greater than the lifetime
75 // of all objects that reference it, so it does not need to be reference
76 // counted.
77 class CaptureTestSourceController {
78 public:
80 CaptureTestSourceController()
81 : color_(SK_ColorMAGENTA),
82 copy_result_size_(kTestWidth, kTestHeight),
83 can_copy_to_video_frame_(false),
84 use_frame_subscriber_(false) {}
86 void SetSolidColor(SkColor color) {
87 base::AutoLock guard(lock_);
88 color_ = color;
91 SkColor GetSolidColor() {
92 base::AutoLock guard(lock_);
93 return color_;
96 void SetCopyResultSize(int width, int height) {
97 base::AutoLock guard(lock_);
98 copy_result_size_ = gfx::Size(width, height);
101 gfx::Size GetCopyResultSize() {
102 base::AutoLock guard(lock_);
103 return copy_result_size_;
106 void SignalCopy() {
107 // TODO(nick): This actually should always be happening on the UI thread.
108 base::AutoLock guard(lock_);
109 if (!copy_done_.is_null()) {
110 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, copy_done_);
111 copy_done_.Reset();
115 void SetCanCopyToVideoFrame(bool value) {
116 base::AutoLock guard(lock_);
117 can_copy_to_video_frame_ = value;
120 bool CanCopyToVideoFrame() {
121 base::AutoLock guard(lock_);
122 return can_copy_to_video_frame_;
125 void SetUseFrameSubscriber(bool value) {
126 base::AutoLock guard(lock_);
127 use_frame_subscriber_ = value;
130 bool CanUseFrameSubscriber() {
131 base::AutoLock guard(lock_);
132 return use_frame_subscriber_;
135 void WaitForNextCopy() {
137 base::AutoLock guard(lock_);
138 copy_done_ = base::MessageLoop::current()->QuitClosure();
141 RunCurrentLoopWithDeadline();
144 private:
145 base::Lock lock_; // Guards changes to all members.
146 SkColor color_;
147 gfx::Size copy_result_size_;
148 bool can_copy_to_video_frame_;
149 bool use_frame_subscriber_;
150 base::Closure copy_done_;
152 DISALLOW_COPY_AND_ASSIGN(CaptureTestSourceController);
155 // A stub implementation which returns solid-color bitmaps in calls to
156 // CopyFromCompositingSurfaceToVideoFrame(), and which allows the video-frame
157 // readback path to be switched on and off. The behavior is controlled by a
158 // CaptureTestSourceController.
159 class CaptureTestView : public TestRenderWidgetHostView {
160 public:
161 explicit CaptureTestView(RenderWidgetHostImpl* rwh,
162 CaptureTestSourceController* controller)
163 : TestRenderWidgetHostView(rwh),
164 controller_(controller) {}
166 ~CaptureTestView() override {}
168 // TestRenderWidgetHostView overrides.
169 gfx::Rect GetViewBounds() const override {
170 return gfx::Rect(100, 100, 100 + kTestWidth, 100 + kTestHeight);
173 bool CanCopyToVideoFrame() const override {
174 return controller_->CanCopyToVideoFrame();
177 void CopyFromCompositingSurfaceToVideoFrame(
178 const gfx::Rect& src_subrect,
179 const scoped_refptr<media::VideoFrame>& target,
180 const base::Callback<void(bool)>& callback) override {
181 SkColor c = ConvertRgbToYuv(controller_->GetSolidColor());
182 media::FillYUV(
183 target.get(), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
184 callback.Run(true);
185 controller_->SignalCopy();
188 void BeginFrameSubscription(
189 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) override {
190 subscriber_.reset(subscriber.release());
193 void EndFrameSubscription() override { subscriber_.reset(); }
195 // Simulate a compositor paint event for our subscriber.
196 void SimulateUpdate() {
197 const base::TimeTicks present_time = base::TimeTicks::Now();
198 RenderWidgetHostViewFrameSubscriber::DeliverFrameCallback callback;
199 scoped_refptr<media::VideoFrame> target;
200 if (subscriber_ && subscriber_->ShouldCaptureFrame(
201 gfx::Rect(), present_time, &target, &callback)) {
202 SkColor c = ConvertRgbToYuv(controller_->GetSolidColor());
203 media::FillYUV(
204 target.get(), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
205 BrowserThread::PostTask(BrowserThread::UI,
206 FROM_HERE,
207 base::Bind(callback, present_time, true));
208 controller_->SignalCopy();
212 private:
213 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber_;
214 CaptureTestSourceController* const controller_;
216 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestView);
219 #if defined(COMPILER_MSVC)
220 // MSVC warns on diamond inheritance. See comment for same warning on
221 // RenderViewHostImpl.
222 #pragma warning(push)
223 #pragma warning(disable: 4250)
224 #endif
226 // A stub implementation which returns solid-color bitmaps in calls to
227 // CopyFromBackingStore(). The behavior is controlled by a
228 // CaptureTestSourceController.
229 class CaptureTestRenderViewHost : public TestRenderViewHost {
230 public:
231 CaptureTestRenderViewHost(SiteInstance* instance,
232 RenderViewHostDelegate* delegate,
233 RenderWidgetHostDelegate* widget_delegate,
234 int routing_id,
235 int main_frame_routing_id,
236 bool swapped_out,
237 CaptureTestSourceController* controller)
238 : TestRenderViewHost(instance, delegate, widget_delegate, routing_id,
239 main_frame_routing_id, swapped_out),
240 controller_(controller) {
241 // Override the default view installed by TestRenderViewHost; we need
242 // our special subclass which has mocked-out tab capture support.
243 RenderWidgetHostView* old_view = GetView();
244 SetView(new CaptureTestView(this, controller));
245 delete old_view;
248 // TestRenderViewHost overrides.
249 void CopyFromBackingStore(const gfx::Rect& src_rect,
250 const gfx::Size& accelerated_dst_size,
251 ReadbackRequestCallback& callback,
252 const SkColorType color_type) override {
253 gfx::Size size = controller_->GetCopyResultSize();
254 SkColor color = controller_->GetSolidColor();
256 // Although it's not necessary, use a PlatformBitmap here (instead of a
257 // regular SkBitmap) to exercise possible threading issues.
258 skia::PlatformBitmap output;
259 EXPECT_TRUE(output.Allocate(size.width(), size.height(), false));
261 SkAutoLockPixels locker(output.GetBitmap());
262 output.GetBitmap().eraseColor(color);
264 callback.Run(output.GetBitmap(), content::READBACK_SUCCESS);
265 controller_->SignalCopy();
268 private:
269 CaptureTestSourceController* controller_;
271 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHost);
274 #if defined(COMPILER_MSVC)
275 // Re-enable warning 4250
276 #pragma warning(pop)
277 #endif
279 class CaptureTestRenderViewHostFactory : public RenderViewHostFactory {
280 public:
281 explicit CaptureTestRenderViewHostFactory(
282 CaptureTestSourceController* controller) : controller_(controller) {
283 RegisterFactory(this);
286 ~CaptureTestRenderViewHostFactory() override { UnregisterFactory(); }
288 // RenderViewHostFactory implementation.
289 RenderViewHost* CreateRenderViewHost(
290 SiteInstance* instance,
291 RenderViewHostDelegate* delegate,
292 RenderWidgetHostDelegate* widget_delegate,
293 int routing_id,
294 int main_frame_routing_id,
295 bool swapped_out) override {
296 return new CaptureTestRenderViewHost(instance, delegate, widget_delegate,
297 routing_id, main_frame_routing_id,
298 swapped_out, controller_);
300 private:
301 CaptureTestSourceController* controller_;
303 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHostFactory);
306 // A stub consumer of captured video frames, which checks the output of
307 // WebContentsVideoCaptureDevice.
308 class StubClient : public media::VideoCaptureDevice::Client {
309 public:
310 StubClient(const base::Callback<void(SkColor)>& color_callback,
311 const base::Closure& error_callback)
312 : color_callback_(color_callback),
313 error_callback_(error_callback) {
314 buffer_pool_ = new VideoCaptureBufferPool(2);
316 ~StubClient() override {}
318 void OnIncomingCapturedData(const uint8* data,
319 int length,
320 const media::VideoCaptureFormat& frame_format,
321 int rotation,
322 const base::TimeTicks& timestamp) override {
323 FAIL();
326 scoped_refptr<media::VideoCaptureDevice::Client::Buffer> ReserveOutputBuffer(
327 media::VideoFrame::Format format,
328 const gfx::Size& dimensions) override {
329 CHECK_EQ(format, media::VideoFrame::I420);
330 const size_t frame_bytes =
331 media::VideoFrame::AllocationSize(media::VideoFrame::I420, dimensions);
332 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId; // Ignored.
333 int buffer_id =
334 buffer_pool_->ReserveForProducer(frame_bytes, &buffer_id_to_drop);
335 if (buffer_id == VideoCaptureBufferPool::kInvalidId)
336 return NULL;
337 void* data;
338 size_t size;
339 buffer_pool_->GetBufferInfo(buffer_id, &data, &size);
340 return scoped_refptr<media::VideoCaptureDevice::Client::Buffer>(
341 new AutoReleaseBuffer(buffer_pool_, buffer_id, data, size));
344 void OnIncomingCapturedVideoFrame(
345 const scoped_refptr<Buffer>& buffer,
346 const media::VideoCaptureFormat& buffer_format,
347 const scoped_refptr<media::VideoFrame>& frame,
348 const base::TimeTicks& timestamp) override {
349 EXPECT_EQ(gfx::Size(kTestWidth, kTestHeight), buffer_format.frame_size);
350 EXPECT_EQ(media::PIXEL_FORMAT_I420, buffer_format.pixel_format);
351 EXPECT_EQ(media::VideoFrame::I420, frame->format());
352 uint8 yuv[3];
353 for (int plane = 0; plane < 3; ++plane)
354 yuv[plane] = frame->data(plane)[0];
355 // TODO(nick): We just look at the first pixel presently, because if
356 // the analysis is too slow, the backlog of frames will grow without bound
357 // and trouble erupts. http://crbug.com/174519
358 color_callback_.Run((SkColorSetRGB(yuv[0], yuv[1], yuv[2])));
361 void OnError(const std::string& reason) override { error_callback_.Run(); }
363 private:
364 class AutoReleaseBuffer : public media::VideoCaptureDevice::Client::Buffer {
365 public:
366 AutoReleaseBuffer(const scoped_refptr<VideoCaptureBufferPool>& pool,
367 int buffer_id,
368 void* data,
369 size_t size)
370 : pool_(pool),
371 id_(buffer_id),
372 data_(data),
373 size_(size) {
374 DCHECK(pool_.get());
376 int id() const override { return id_; }
377 void* data() const override { return data_; }
378 size_t size() const override { return size_; }
380 private:
381 ~AutoReleaseBuffer() override { pool_->RelinquishProducerReservation(id_); }
383 const scoped_refptr<VideoCaptureBufferPool> pool_;
384 const int id_;
385 void* const data_;
386 const size_t size_;
389 scoped_refptr<VideoCaptureBufferPool> buffer_pool_;
390 base::Callback<void(SkColor)> color_callback_;
391 base::Closure error_callback_;
393 DISALLOW_COPY_AND_ASSIGN(StubClient);
396 class StubClientObserver {
397 public:
398 StubClientObserver()
399 : error_encountered_(false),
400 wait_color_yuv_(0xcafe1950) {
401 client_.reset(new StubClient(
402 base::Bind(&StubClientObserver::OnColor, base::Unretained(this)),
403 base::Bind(&StubClientObserver::OnError, base::Unretained(this))));
406 virtual ~StubClientObserver() {}
408 scoped_ptr<media::VideoCaptureDevice::Client> PassClient() {
409 return client_.Pass();
412 void QuitIfConditionMet(SkColor color) {
413 base::AutoLock guard(lock_);
414 if (wait_color_yuv_ == color || error_encountered_)
415 base::MessageLoop::current()->Quit();
418 void WaitForNextColor(SkColor expected_color) {
420 base::AutoLock guard(lock_);
421 wait_color_yuv_ = ConvertRgbToYuv(expected_color);
422 error_encountered_ = false;
424 RunCurrentLoopWithDeadline();
426 base::AutoLock guard(lock_);
427 ASSERT_FALSE(error_encountered_);
431 void WaitForError() {
433 base::AutoLock guard(lock_);
434 wait_color_yuv_ = kNotInterested;
435 error_encountered_ = false;
437 RunCurrentLoopWithDeadline();
439 base::AutoLock guard(lock_);
440 ASSERT_TRUE(error_encountered_);
444 bool HasError() {
445 base::AutoLock guard(lock_);
446 return error_encountered_;
449 void OnError() {
451 base::AutoLock guard(lock_);
452 error_encountered_ = true;
454 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
455 &StubClientObserver::QuitIfConditionMet,
456 base::Unretained(this),
457 kNothingYet));
460 void OnColor(SkColor color) {
461 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
462 &StubClientObserver::QuitIfConditionMet,
463 base::Unretained(this),
464 color));
467 private:
468 base::Lock lock_;
469 bool error_encountered_;
470 SkColor wait_color_yuv_;
471 scoped_ptr<StubClient> client_;
473 DISALLOW_COPY_AND_ASSIGN(StubClientObserver);
476 // A dummy implementation of gfx::Screen, since WebContentsVideoCaptureDevice
477 // needs access to a gfx::Display's device scale factor.
478 class FakeScreen : public gfx::Screen {
479 public:
480 FakeScreen() : the_one_display_(0x1337, gfx::Rect(0, 0, 2560, 1440)) {
481 the_one_display_.set_device_scale_factor(kTestDeviceScaleFactor);
483 ~FakeScreen() override {}
485 // gfx::Screen implementation (only what's needed for testing).
486 gfx::Point GetCursorScreenPoint() override { return gfx::Point(); }
487 gfx::NativeWindow GetWindowUnderCursor() override { return NULL; }
488 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override {
489 return NULL;
491 int GetNumDisplays() const override { return 1; }
492 std::vector<gfx::Display> GetAllDisplays() const override {
493 return std::vector<gfx::Display>(1, the_one_display_);
495 gfx::Display GetDisplayNearestWindow(gfx::NativeView view) const override {
496 return the_one_display_;
498 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override {
499 return the_one_display_;
501 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override {
502 return the_one_display_;
504 gfx::Display GetPrimaryDisplay() const override { return the_one_display_; }
505 void AddObserver(gfx::DisplayObserver* observer) override {}
506 void RemoveObserver(gfx::DisplayObserver* observer) override {}
508 private:
509 gfx::Display the_one_display_;
511 DISALLOW_COPY_AND_ASSIGN(FakeScreen);
514 // Test harness that sets up a minimal environment with necessary stubs.
515 class WebContentsVideoCaptureDeviceTest : public testing::Test {
516 public:
517 // This is public because C++ method pointer scoping rules are silly and make
518 // this hard to use with Bind().
519 void ResetWebContents() {
520 web_contents_.reset();
523 protected:
524 void SetUp() override {
525 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, &fake_screen_);
526 ASSERT_EQ(&fake_screen_, gfx::Screen::GetNativeScreen());
528 // TODO(nick): Sadness and woe! Much "mock-the-world" boilerplate could be
529 // eliminated here, if only we could use RenderViewHostTestHarness. The
530 // catch is that we need our TestRenderViewHost to support a
531 // CopyFromBackingStore operation that we control. To accomplish that,
532 // either RenderViewHostTestHarness would have to support installing a
533 // custom RenderViewHostFactory, or else we implant some kind of delegated
534 // CopyFromBackingStore functionality into TestRenderViewHost itself.
536 render_process_host_factory_.reset(new MockRenderProcessHostFactory());
537 // Create our (self-registering) RVH factory, so that when we create a
538 // WebContents, it in turn creates CaptureTestRenderViewHosts.
539 render_view_host_factory_.reset(
540 new CaptureTestRenderViewHostFactory(&controller_));
542 browser_context_.reset(new TestBrowserContext());
544 scoped_refptr<SiteInstance> site_instance =
545 SiteInstance::Create(browser_context_.get());
546 SiteInstanceImpl::set_render_process_host_factory(
547 render_process_host_factory_.get());
548 web_contents_.reset(
549 TestWebContents::Create(browser_context_.get(), site_instance.get()));
550 RenderFrameHost* const main_frame = web_contents_->GetMainFrame();
551 device_.reset(WebContentsVideoCaptureDevice::Create(
552 base::StringPrintf("web-contents-media-stream://%d:%d",
553 main_frame->GetProcess()->GetID(),
554 main_frame->GetRoutingID())));
556 base::RunLoop().RunUntilIdle();
559 void TearDown() override {
560 // Tear down in opposite order of set-up.
562 // The device is destroyed asynchronously, and will notify the
563 // CaptureTestSourceController when it finishes destruction.
564 // Trigger this, and wait.
565 if (device_) {
566 device_->StopAndDeAllocate();
567 device_.reset();
570 base::RunLoop().RunUntilIdle();
572 // Destroy the browser objects.
573 web_contents_.reset();
574 browser_context_.reset();
576 base::RunLoop().RunUntilIdle();
578 SiteInstanceImpl::set_render_process_host_factory(NULL);
579 render_view_host_factory_.reset();
580 render_process_host_factory_.reset();
582 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL);
585 // Accessors.
586 CaptureTestSourceController* source() { return &controller_; }
587 WebContents* web_contents() const { return web_contents_.get(); }
588 media::VideoCaptureDevice* device() { return device_.get(); }
590 void SimulateDrawEvent() {
591 if (source()->CanUseFrameSubscriber()) {
592 // Print
593 CaptureTestView* test_view = static_cast<CaptureTestView*>(
594 web_contents_->GetRenderViewHost()->GetView());
595 test_view->SimulateUpdate();
596 } else {
597 // Simulate a non-accelerated paint.
598 NotificationService::current()->Notify(
599 NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE,
600 Source<RenderWidgetHost>(web_contents_->GetRenderViewHost()),
601 NotificationService::NoDetails());
605 void DestroyVideoCaptureDevice() { device_.reset(); }
607 StubClientObserver* client_observer() {
608 return &client_observer_;
611 private:
612 FakeScreen fake_screen_;
614 StubClientObserver client_observer_;
616 // The controller controls which pixel patterns to produce.
617 CaptureTestSourceController controller_;
619 // Self-registering RenderProcessHostFactory.
620 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory_;
622 // Creates capture-capable RenderViewHosts whose pixel content production is
623 // under the control of |controller_|.
624 scoped_ptr<CaptureTestRenderViewHostFactory> render_view_host_factory_;
626 // A mocked-out browser and tab.
627 scoped_ptr<TestBrowserContext> browser_context_;
628 scoped_ptr<WebContents> web_contents_;
630 // Finally, the WebContentsVideoCaptureDevice under test.
631 scoped_ptr<media::VideoCaptureDevice> device_;
633 TestBrowserThreadBundle thread_bundle_;
636 TEST_F(WebContentsVideoCaptureDeviceTest, InvalidInitialWebContentsError) {
637 // Before the installs itself on the UI thread up to start capturing, we'll
638 // delete the web contents. This should trigger an error which can happen in
639 // practice; we should be able to recover gracefully.
640 ResetWebContents();
642 media::VideoCaptureParams capture_params;
643 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
644 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
645 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
646 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
647 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
648 device()->StopAndDeAllocate();
651 TEST_F(WebContentsVideoCaptureDeviceTest, WebContentsDestroyed) {
652 const gfx::Size capture_preferred_size(
653 static_cast<int>(kTestWidth / kTestDeviceScaleFactor),
654 static_cast<int>(kTestHeight / kTestDeviceScaleFactor));
655 ASSERT_NE(capture_preferred_size, web_contents()->GetPreferredSize());
657 // We'll simulate the tab being closed after the capture pipeline is up and
658 // running.
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 // Do one capture to prove
665 source()->SetSolidColor(SK_ColorRED);
666 SimulateDrawEvent();
667 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
669 base::RunLoop().RunUntilIdle();
671 // Check that the preferred size of the WebContents matches the one provided
672 // by WebContentsVideoCaptureDevice.
673 EXPECT_EQ(capture_preferred_size, web_contents()->GetPreferredSize());
675 // Post a task to close the tab. We should see an error reported to the
676 // consumer.
677 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
678 base::Bind(&WebContentsVideoCaptureDeviceTest::ResetWebContents,
679 base::Unretained(this)));
680 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
681 device()->StopAndDeAllocate();
684 TEST_F(WebContentsVideoCaptureDeviceTest,
685 StopDeviceBeforeCaptureMachineCreation) {
686 media::VideoCaptureParams capture_params;
687 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
688 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
689 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
690 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
692 // Make a point of not running the UI messageloop here.
693 device()->StopAndDeAllocate();
694 DestroyVideoCaptureDevice();
696 // Currently, there should be CreateCaptureMachineOnUIThread() and
697 // DestroyCaptureMachineOnUIThread() tasks pending on the current (UI) message
698 // loop. These should both succeed without crashing, and the machine should
699 // wind up in the idle state.
700 base::RunLoop().RunUntilIdle();
703 TEST_F(WebContentsVideoCaptureDeviceTest, StopWithRendererWorkToDo) {
704 // Set up the test to use RGB copies and an normal
705 source()->SetCanCopyToVideoFrame(false);
706 source()->SetUseFrameSubscriber(false);
707 media::VideoCaptureParams capture_params;
708 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
709 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
710 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
711 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
713 base::RunLoop().RunUntilIdle();
715 for (int i = 0; i < 10; ++i)
716 SimulateDrawEvent();
718 ASSERT_FALSE(client_observer()->HasError());
719 device()->StopAndDeAllocate();
720 ASSERT_FALSE(client_observer()->HasError());
721 base::RunLoop().RunUntilIdle();
722 ASSERT_FALSE(client_observer()->HasError());
725 TEST_F(WebContentsVideoCaptureDeviceTest, DeviceRestart) {
726 media::VideoCaptureParams capture_params;
727 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
728 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
729 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
730 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
731 base::RunLoop().RunUntilIdle();
732 source()->SetSolidColor(SK_ColorRED);
733 SimulateDrawEvent();
734 SimulateDrawEvent();
735 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
736 SimulateDrawEvent();
737 SimulateDrawEvent();
738 source()->SetSolidColor(SK_ColorGREEN);
739 SimulateDrawEvent();
740 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN));
741 device()->StopAndDeAllocate();
743 // Device is stopped, but content can still be animating.
744 SimulateDrawEvent();
745 SimulateDrawEvent();
746 base::RunLoop().RunUntilIdle();
748 StubClientObserver observer2;
749 device()->AllocateAndStart(capture_params, observer2.PassClient());
750 source()->SetSolidColor(SK_ColorBLUE);
751 SimulateDrawEvent();
752 ASSERT_NO_FATAL_FAILURE(observer2.WaitForNextColor(SK_ColorBLUE));
753 source()->SetSolidColor(SK_ColorYELLOW);
754 SimulateDrawEvent();
755 ASSERT_NO_FATAL_FAILURE(observer2.WaitForNextColor(SK_ColorYELLOW));
756 device()->StopAndDeAllocate();
759 // The "happy case" test. No scaling is needed, so we should be able to change
760 // the picture emitted from the source and expect to see each delivered to the
761 // consumer. The test will alternate between the three capture paths, simulating
762 // falling in and out of accelerated compositing.
763 TEST_F(WebContentsVideoCaptureDeviceTest, GoesThroughAllTheMotions) {
764 media::VideoCaptureParams capture_params;
765 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
766 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
767 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
768 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
770 for (int i = 0; i < 6; i++) {
771 const char* name = NULL;
772 switch (i % 3) {
773 case 0:
774 source()->SetCanCopyToVideoFrame(true);
775 source()->SetUseFrameSubscriber(false);
776 name = "VideoFrame";
777 break;
778 case 1:
779 source()->SetCanCopyToVideoFrame(false);
780 source()->SetUseFrameSubscriber(true);
781 name = "Subscriber";
782 break;
783 case 2:
784 source()->SetCanCopyToVideoFrame(false);
785 source()->SetUseFrameSubscriber(false);
786 name = "SkBitmap";
787 break;
788 default:
789 FAIL();
792 SCOPED_TRACE(base::StringPrintf("Using %s path, iteration #%d", name, i));
794 source()->SetSolidColor(SK_ColorRED);
795 SimulateDrawEvent();
796 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
798 source()->SetSolidColor(SK_ColorGREEN);
799 SimulateDrawEvent();
800 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN));
802 source()->SetSolidColor(SK_ColorBLUE);
803 SimulateDrawEvent();
804 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLUE));
806 source()->SetSolidColor(SK_ColorBLACK);
807 SimulateDrawEvent();
808 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLACK));
810 device()->StopAndDeAllocate();
813 TEST_F(WebContentsVideoCaptureDeviceTest, RejectsInvalidAllocateParams) {
814 media::VideoCaptureParams capture_params;
815 capture_params.requested_format.frame_size.SetSize(1280, 720);
816 capture_params.requested_format.frame_rate = -2;
817 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
818 BrowserThread::PostTask(
819 BrowserThread::UI,
820 FROM_HERE,
821 base::Bind(&media::VideoCaptureDevice::AllocateAndStart,
822 base::Unretained(device()),
823 capture_params,
824 base::Passed(client_observer()->PassClient())));
825 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForError());
826 BrowserThread::PostTask(
827 BrowserThread::UI,
828 FROM_HERE,
829 base::Bind(&media::VideoCaptureDevice::StopAndDeAllocate,
830 base::Unretained(device())));
831 base::RunLoop().RunUntilIdle();
834 TEST_F(WebContentsVideoCaptureDeviceTest, BadFramesGoodFrames) {
835 media::VideoCaptureParams capture_params;
836 capture_params.requested_format.frame_size.SetSize(kTestWidth, kTestHeight);
837 capture_params.requested_format.frame_rate = kTestFramesPerSecond;
838 capture_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420;
839 // 1x1 is too small to process; we intend for this to result in an error.
840 source()->SetCopyResultSize(1, 1);
841 source()->SetSolidColor(SK_ColorRED);
842 device()->AllocateAndStart(capture_params, client_observer()->PassClient());
844 // These frames ought to be dropped during the Render stage. Let
845 // several captures to happen.
846 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
847 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
848 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
849 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
850 ASSERT_NO_FATAL_FAILURE(source()->WaitForNextCopy());
852 // Now push some good frames through; they should be processed normally.
853 source()->SetCopyResultSize(kTestWidth, kTestHeight);
854 source()->SetSolidColor(SK_ColorGREEN);
855 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN));
856 source()->SetSolidColor(SK_ColorRED);
857 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED));
859 device()->StopAndDeAllocate();
862 } // namespace
863 } // namespace content