| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/browser/renderer_host/media/web_contents_video_capture_device.
h" | |
| 6 | |
| 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" | |
| 34 | |
| 35 namespace content { | |
| 36 namespace { | |
| 37 | |
| 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; | |
| 43 | |
| 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."; | |
| 51 } | |
| 52 } | |
| 53 | |
| 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(); | |
| 60 } | |
| 61 | |
| 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]); | |
| 67 } | |
| 68 | |
| 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: | |
| 75 | |
| 76 CaptureTestSourceController() | |
| 77 : color_(SK_ColorMAGENTA), | |
| 78 copy_result_size_(kTestWidth, kTestHeight), | |
| 79 can_copy_to_video_frame_(false), | |
| 80 use_frame_subscriber_(false) {} | |
| 81 | |
| 82 void SetSolidColor(SkColor color) { | |
| 83 base::AutoLock guard(lock_); | |
| 84 color_ = color; | |
| 85 } | |
| 86 | |
| 87 SkColor GetSolidColor() { | |
| 88 base::AutoLock guard(lock_); | |
| 89 return color_; | |
| 90 } | |
| 91 | |
| 92 void SetCopyResultSize(int width, int height) { | |
| 93 base::AutoLock guard(lock_); | |
| 94 copy_result_size_ = gfx::Size(width, height); | |
| 95 } | |
| 96 | |
| 97 gfx::Size GetCopyResultSize() { | |
| 98 base::AutoLock guard(lock_); | |
| 99 return copy_result_size_; | |
| 100 } | |
| 101 | |
| 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(); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 void SetCanCopyToVideoFrame(bool value) { | |
| 112 base::AutoLock guard(lock_); | |
| 113 can_copy_to_video_frame_ = value; | |
| 114 } | |
| 115 | |
| 116 bool CanCopyToVideoFrame() { | |
| 117 base::AutoLock guard(lock_); | |
| 118 return can_copy_to_video_frame_; | |
| 119 } | |
| 120 | |
| 121 void SetUseFrameSubscriber(bool value) { | |
| 122 base::AutoLock guard(lock_); | |
| 123 use_frame_subscriber_ = value; | |
| 124 } | |
| 125 | |
| 126 bool CanUseFrameSubscriber() { | |
| 127 base::AutoLock guard(lock_); | |
| 128 return use_frame_subscriber_; | |
| 129 } | |
| 130 | |
| 131 void WaitForNextCopy() { | |
| 132 { | |
| 133 base::AutoLock guard(lock_); | |
| 134 copy_done_ = base::MessageLoop::current()->QuitClosure(); | |
| 135 } | |
| 136 | |
| 137 RunCurrentLoopWithDeadline(); | |
| 138 } | |
| 139 | |
| 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_; | |
| 147 | |
| 148 DISALLOW_COPY_AND_ASSIGN(CaptureTestSourceController); | |
| 149 }; | |
| 150 | |
| 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) {} | |
| 161 | |
| 162 virtual ~CaptureTestView() {} | |
| 163 | |
| 164 // TestRenderWidgetHostView overrides. | |
| 165 virtual gfx::Rect GetViewBounds() const OVERRIDE { | |
| 166 return gfx::Rect(100, 100, 100 + kTestWidth, 100 + kTestHeight); | |
| 167 } | |
| 168 | |
| 169 virtual bool CanCopyToVideoFrame() const OVERRIDE { | |
| 170 return controller_->CanCopyToVideoFrame(); | |
| 171 } | |
| 172 | |
| 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(); | |
| 182 } | |
| 183 | |
| 184 virtual void BeginFrameSubscription( | |
| 185 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) OVERRIDE { | |
| 186 subscriber_.reset(subscriber.release()); | |
| 187 } | |
| 188 | |
| 189 virtual void EndFrameSubscription() OVERRIDE { | |
| 190 subscriber_.reset(); | |
| 191 } | |
| 192 | |
| 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(); | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 private: | |
| 211 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber_; | |
| 212 CaptureTestSourceController* const controller_; | |
| 213 | |
| 214 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestView); | |
| 215 }; | |
| 216 | |
| 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 | |
| 223 | |
| 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; | |
| 244 } | |
| 245 | |
| 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(); | |
| 253 | |
| 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)); | |
| 258 { | |
| 259 SkAutoLockPixels locker(output.GetBitmap()); | |
| 260 output.GetBitmap().eraseColor(color); | |
| 261 } | |
| 262 callback.Run(true, output.GetBitmap()); | |
| 263 controller_->SignalCopy(); | |
| 264 } | |
| 265 | |
| 266 private: | |
| 267 CaptureTestSourceController* controller_; | |
| 268 | |
| 269 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHost); | |
| 270 }; | |
| 271 | |
| 272 #if defined(COMPILER_MSVC) | |
| 273 // Re-enable warning 4250 | |
| 274 #pragma warning(pop) | |
| 275 #endif | |
| 276 | |
| 277 class CaptureTestRenderViewHostFactory : public RenderViewHostFactory { | |
| 278 public: | |
| 279 explicit CaptureTestRenderViewHostFactory( | |
| 280 CaptureTestSourceController* controller) : controller_(controller) { | |
| 281 RegisterFactory(this); | |
| 282 } | |
| 283 | |
| 284 virtual ~CaptureTestRenderViewHostFactory() { | |
| 285 UnregisterFactory(); | |
| 286 } | |
| 287 | |
| 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_); | |
| 299 } | |
| 300 private: | |
| 301 CaptureTestSourceController* controller_; | |
| 302 | |
| 303 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHostFactory); | |
| 304 }; | |
| 305 | |
| 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); | |
| 315 } | |
| 316 virtual ~StubClient() {} | |
| 317 | |
| 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)); | |
| 333 } | |
| 334 | |
| 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(); | |
| 342 } | |
| 343 | |
| 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); | |
| 357 } | |
| 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]))); | |
| 362 } | |
| 363 | |
| 364 virtual void OnError(const std::string& reason) OVERRIDE { | |
| 365 error_callback_.Run(); | |
| 366 } | |
| 367 | |
| 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) {} | |
| 376 | |
| 377 private: | |
| 378 virtual ~PoolBuffer() { pool_->RelinquishProducerReservation(id()); } | |
| 379 const scoped_refptr<VideoCaptureBufferPool> pool_; | |
| 380 }; | |
| 381 | |
| 382 scoped_refptr<VideoCaptureBufferPool> buffer_pool_; | |
| 383 base::Callback<void(SkColor)> color_callback_; | |
| 384 base::Closure error_callback_; | |
| 385 | |
| 386 DISALLOW_COPY_AND_ASSIGN(StubClient); | |
| 387 }; | |
| 388 | |
| 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)))); | |
| 397 } | |
| 398 | |
| 399 virtual ~StubClientObserver() {} | |
| 400 | |
| 401 scoped_ptr<media::VideoCaptureDevice::Client> PassClient() { | |
| 402 return client_.PassAs<media::VideoCaptureDevice::Client>(); | |
| 403 } | |
| 404 | |
| 405 void QuitIfConditionMet(SkColor color) { | |
| 406 base::AutoLock guard(lock_); | |
| 407 if (wait_color_yuv_ == color || error_encountered_) | |
| 408 base::MessageLoop::current()->Quit(); | |
| 409 } | |
| 410 | |
| 411 void WaitForNextColor(SkColor expected_color) { | |
| 412 { | |
| 413 base::AutoLock guard(lock_); | |
| 414 wait_color_yuv_ = ConvertRgbToYuv(expected_color); | |
| 415 error_encountered_ = false; | |
| 416 } | |
| 417 RunCurrentLoopWithDeadline(); | |
| 418 { | |
| 419 base::AutoLock guard(lock_); | |
| 420 ASSERT_FALSE(error_encountered_); | |
| 421 } | |
| 422 } | |
| 423 | |
| 424 void WaitForError() { | |
| 425 { | |
| 426 base::AutoLock guard(lock_); | |
| 427 wait_color_yuv_ = kNotInterested; | |
| 428 error_encountered_ = false; | |
| 429 } | |
| 430 RunCurrentLoopWithDeadline(); | |
| 431 { | |
| 432 base::AutoLock guard(lock_); | |
| 433 ASSERT_TRUE(error_encountered_); | |
| 434 } | |
| 435 } | |
| 436 | |
| 437 bool HasError() { | |
| 438 base::AutoLock guard(lock_); | |
| 439 return error_encountered_; | |
| 440 } | |
| 441 | |
| 442 void OnError() { | |
| 443 { | |
| 444 base::AutoLock guard(lock_); | |
| 445 error_encountered_ = true; | |
| 446 } | |
| 447 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 448 &StubClientObserver::QuitIfConditionMet, | |
| 449 base::Unretained(this), | |
| 450 kNothingYet)); | |
| 451 } | |
| 452 | |
| 453 void OnColor(SkColor color) { | |
| 454 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 455 &StubClientObserver::QuitIfConditionMet, | |
| 456 base::Unretained(this), | |
| 457 color)); | |
| 458 } | |
| 459 | |
| 460 private: | |
| 461 base::Lock lock_; | |
| 462 bool error_encountered_; | |
| 463 SkColor wait_color_yuv_; | |
| 464 scoped_ptr<StubClient> client_; | |
| 465 | |
| 466 DISALLOW_COPY_AND_ASSIGN(StubClientObserver); | |
| 467 }; | |
| 468 | |
| 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(); | |
| 476 } | |
| 477 | |
| 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. | |
| 487 | |
| 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_)); | |
| 493 | |
| 494 browser_context_.reset(new TestBrowserContext()); | |
| 495 | |
| 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())); | |
| 502 | |
| 503 // This is actually a CaptureTestRenderViewHost. | |
| 504 RenderWidgetHostImpl* rwh = | |
| 505 RenderWidgetHostImpl::From(web_contents_->GetRenderViewHost()); | |
| 506 | |
| 507 std::string device_id = | |
| 508 WebContentsCaptureUtil::AppendWebContentsDeviceScheme( | |
| 509 base::StringPrintf("%d:%d", rwh->GetProcess()->GetID(), | |
| 510 rwh->GetRoutingID())); | |
| 511 | |
| 512 device_.reset(WebContentsVideoCaptureDevice::Create(device_id)); | |
| 513 | |
| 514 base::RunLoop().RunUntilIdle(); | |
| 515 } | |
| 516 | |
| 517 virtual void TearDown() { | |
| 518 // Tear down in opposite order of set-up. | |
| 519 | |
| 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(); | |
| 526 } | |
| 527 | |
| 528 base::RunLoop().RunUntilIdle(); | |
| 529 | |
| 530 // Destroy the browser objects. | |
| 531 web_contents_.reset(); | |
| 532 browser_context_.reset(); | |
| 533 | |
| 534 base::RunLoop().RunUntilIdle(); | |
| 535 | |
| 536 SiteInstanceImpl::set_render_process_host_factory(NULL); | |
| 537 render_view_host_factory_.reset(); | |
| 538 render_process_host_factory_.reset(); | |
| 539 } | |
| 540 | |
| 541 // Accessors. | |
| 542 CaptureTestSourceController* source() { return &controller_; } | |
| 543 media::VideoCaptureDevice* device() { return device_.get(); } | |
| 544 | |
| 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()); | |
| 557 } | |
| 558 } | |
| 559 | |
| 560 void DestroyVideoCaptureDevice() { device_.reset(); } | |
| 561 | |
| 562 StubClientObserver* client_observer() { | |
| 563 return &client_observer_; | |
| 564 } | |
| 565 | |
| 566 private: | |
| 567 StubClientObserver client_observer_; | |
| 568 | |
| 569 // The controller controls which pixel patterns to produce. | |
| 570 CaptureTestSourceController controller_; | |
| 571 | |
| 572 // Self-registering RenderProcessHostFactory. | |
| 573 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory_; | |
| 574 | |
| 575 // Creates capture-capable RenderViewHosts whose pixel content production is | |
| 576 // under the control of |controller_|. | |
| 577 scoped_ptr<CaptureTestRenderViewHostFactory> render_view_host_factory_; | |
| 578 | |
| 579 // A mocked-out browser and tab. | |
| 580 scoped_ptr<TestBrowserContext> browser_context_; | |
| 581 scoped_ptr<WebContents> web_contents_; | |
| 582 | |
| 583 // Finally, the WebContentsVideoCaptureDevice under test. | |
| 584 scoped_ptr<media::VideoCaptureDevice> device_; | |
| 585 | |
| 586 TestBrowserThreadBundle thread_bundle_; | |
| 587 }; | |
| 588 | |
| 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(); | |
| 594 | |
| 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(); | |
| 603 } | |
| 604 | |
| 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)); | |
| 618 | |
| 619 base::RunLoop().RunUntilIdle(); | |
| 620 | |
| 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(); | |
| 628 } | |
| 629 | |
| 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()); | |
| 638 | |
| 639 // Make a point of not running the UI messageloop here. | |
| 640 device()->StopAndDeAllocate(); | |
| 641 DestroyVideoCaptureDevice(); | |
| 642 | |
| 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(); | |
| 648 } | |
| 649 | |
| 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()); | |
| 660 | |
| 661 base::RunLoop().RunUntilIdle(); | |
| 662 | |
| 663 for (int i = 0; i < 10; ++i) | |
| 664 SimulateDrawEvent(); | |
| 665 | |
| 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()); | |
| 671 } | |
| 672 | |
| 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(); | |
| 691 | |
| 692 // Device is stopped, but content can still be animating. | |
| 693 SimulateDrawEvent(); | |
| 694 SimulateDrawEvent(); | |
| 695 base::RunLoop().RunUntilIdle(); | |
| 696 | |
| 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(); | |
| 706 } | |
| 707 | |
| 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()); | |
| 719 | |
| 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(); | |
| 740 } | |
| 741 | |
| 742 SCOPED_TRACE(base::StringPrintf("Using %s path, iteration #%d", name, i)); | |
| 743 | |
| 744 source()->SetSolidColor(SK_ColorRED); | |
| 745 SimulateDrawEvent(); | |
| 746 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorRED)); | |
| 747 | |
| 748 source()->SetSolidColor(SK_ColorGREEN); | |
| 749 SimulateDrawEvent(); | |
| 750 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN)); | |
| 751 | |
| 752 source()->SetSolidColor(SK_ColorBLUE); | |
| 753 SimulateDrawEvent(); | |
| 754 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLUE)); | |
| 755 | |
| 756 source()->SetSolidColor(SK_ColorBLACK); | |
| 757 SimulateDrawEvent(); | |
| 758 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorBLACK)); | |
| 759 } | |
| 760 device()->StopAndDeAllocate(); | |
| 761 } | |
| 762 | |
| 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(); | |
| 783 } | |
| 784 | |
| 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()); | |
| 795 | |
| 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()); | |
| 803 | |
| 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)); | |
| 810 | |
| 811 device()->StopAndDeAllocate(); | |
| 812 } | |
| 813 | |
| 814 } // namespace | |
| 815 } // namespace content | |
| OLD | NEW |