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