| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/media/capture/web_contents_video_capture_device.h" | 5 #include "content/browser/media/capture/web_contents_video_capture_device.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 } | 318 } |
| 319 | 319 |
| 320 private: | 320 private: |
| 321 CaptureTestSourceController* controller_; | 321 CaptureTestSourceController* controller_; |
| 322 | 322 |
| 323 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHostFactory); | 323 DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTestRenderViewHostFactory); |
| 324 }; | 324 }; |
| 325 | 325 |
| 326 // A stub consumer of captured video frames, which checks the output of | 326 // A stub consumer of captured video frames, which checks the output of |
| 327 // WebContentsVideoCaptureDevice. | 327 // WebContentsVideoCaptureDevice. |
| 328 class StubClient : public media::VideoCaptureDevice::Client { | 328 class StubClient : public device::VideoCaptureDevice::Client { |
| 329 public: | 329 public: |
| 330 StubClient( | 330 StubClient( |
| 331 const base::Callback<void(SkColor, const gfx::Size&)>& report_callback, | 331 const base::Callback<void(SkColor, const gfx::Size&)>& report_callback, |
| 332 const base::Closure& error_callback) | 332 const base::Closure& error_callback) |
| 333 : report_callback_(report_callback), | 333 : report_callback_(report_callback), |
| 334 error_callback_(error_callback) { | 334 error_callback_(error_callback) { |
| 335 buffer_pool_ = new VideoCaptureBufferPool(2); | 335 buffer_pool_ = new VideoCaptureBufferPool(2); |
| 336 } | 336 } |
| 337 ~StubClient() override {} | 337 ~StubClient() override {} |
| 338 | 338 |
| 339 MOCK_METHOD6(OnIncomingCapturedData, | 339 MOCK_METHOD6(OnIncomingCapturedData, |
| 340 void(const uint8_t* data, | 340 void(const uint8_t* data, |
| 341 int length, | 341 int length, |
| 342 const media::VideoCaptureFormat& frame_format, | 342 const media::VideoCaptureFormat& frame_format, |
| 343 int rotation, | 343 int rotation, |
| 344 base::TimeTicks reference_time, | 344 base::TimeTicks reference_time, |
| 345 base::TimeDelta timestamp)); | 345 base::TimeDelta timestamp)); |
| 346 | 346 |
| 347 MOCK_METHOD0(DoOnIncomingCapturedBuffer, void(void)); | 347 MOCK_METHOD0(DoOnIncomingCapturedBuffer, void(void)); |
| 348 | 348 |
| 349 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> | 349 std::unique_ptr<device::VideoCaptureDevice::Client::Buffer> |
| 350 ReserveOutputBuffer(const gfx::Size& dimensions, | 350 ReserveOutputBuffer(const gfx::Size& dimensions, |
| 351 media::VideoPixelFormat format, | 351 media::VideoPixelFormat format, |
| 352 media::VideoPixelStorage storage) override { | 352 media::VideoPixelStorage storage) override { |
| 353 CHECK_EQ(format, media::PIXEL_FORMAT_I420); | 353 CHECK_EQ(format, media::PIXEL_FORMAT_I420); |
| 354 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId; // Ignored. | 354 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId; // Ignored. |
| 355 const int buffer_id = buffer_pool_->ReserveForProducer( | 355 const int buffer_id = buffer_pool_->ReserveForProducer( |
| 356 dimensions, format, storage, &buffer_id_to_drop); | 356 dimensions, format, storage, &buffer_id_to_drop); |
| 357 if (buffer_id == VideoCaptureBufferPool::kInvalidId) | 357 if (buffer_id == VideoCaptureBufferPool::kInvalidId) |
| 358 return NULL; | 358 return NULL; |
| 359 | 359 |
| 360 return std::unique_ptr<media::VideoCaptureDevice::Client::Buffer>( | 360 return std::unique_ptr<device::VideoCaptureDevice::Client::Buffer>( |
| 361 new AutoReleaseBuffer( | 361 new AutoReleaseBuffer( |
| 362 buffer_pool_, buffer_pool_->GetBufferHandle(buffer_id), buffer_id)); | 362 buffer_pool_, buffer_pool_->GetBufferHandle(buffer_id), buffer_id)); |
| 363 } | 363 } |
| 364 | 364 |
| 365 // Trampoline method to workaround GMOCK problems with std::unique_ptr<>. | 365 // Trampoline method to workaround GMOCK problems with std::unique_ptr<>. |
| 366 void OnIncomingCapturedBuffer(std::unique_ptr<Buffer> buffer, | 366 void OnIncomingCapturedBuffer(std::unique_ptr<Buffer> buffer, |
| 367 const media::VideoCaptureFormat& frame_format, | 367 const media::VideoCaptureFormat& frame_format, |
| 368 base::TimeTicks reference_time, | 368 base::TimeTicks reference_time, |
| 369 base::TimeDelta timestamp) override { | 369 base::TimeDelta timestamp) override { |
| 370 DoOnIncomingCapturedBuffer(); | 370 DoOnIncomingCapturedBuffer(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 391 const int center_offset_uv = | 391 const int center_offset_uv = |
| 392 (frame->stride(VideoFrame::kUPlane) * (center.y() / 2)) + | 392 (frame->stride(VideoFrame::kUPlane) * (center.y() / 2)) + |
| 393 (center.x() / 2); | 393 (center.x() / 2); |
| 394 report_callback_.Run( | 394 report_callback_.Run( |
| 395 SkColorSetRGB(frame->data(VideoFrame::kYPlane)[center_offset_y], | 395 SkColorSetRGB(frame->data(VideoFrame::kYPlane)[center_offset_y], |
| 396 frame->data(VideoFrame::kUPlane)[center_offset_uv], | 396 frame->data(VideoFrame::kUPlane)[center_offset_uv], |
| 397 frame->data(VideoFrame::kVPlane)[center_offset_uv]), | 397 frame->data(VideoFrame::kVPlane)[center_offset_uv]), |
| 398 frame->visible_rect().size()); | 398 frame->visible_rect().size()); |
| 399 } | 399 } |
| 400 | 400 |
| 401 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> | 401 std::unique_ptr<device::VideoCaptureDevice::Client::Buffer> |
| 402 ResurrectLastOutputBuffer(const gfx::Size& dimensions, | 402 ResurrectLastOutputBuffer(const gfx::Size& dimensions, |
| 403 media::VideoPixelFormat format, | 403 media::VideoPixelFormat format, |
| 404 media::VideoPixelStorage storage) override { | 404 media::VideoPixelStorage storage) override { |
| 405 CHECK_EQ(format, media::PIXEL_FORMAT_I420); | 405 CHECK_EQ(format, media::PIXEL_FORMAT_I420); |
| 406 const int buffer_id = | 406 const int buffer_id = |
| 407 buffer_pool_->ResurrectLastForProducer(dimensions, format, storage); | 407 buffer_pool_->ResurrectLastForProducer(dimensions, format, storage); |
| 408 if (buffer_id == VideoCaptureBufferPool::kInvalidId) | 408 if (buffer_id == VideoCaptureBufferPool::kInvalidId) |
| 409 return nullptr; | 409 return nullptr; |
| 410 return std::unique_ptr<media::VideoCaptureDevice::Client::Buffer>( | 410 return std::unique_ptr<device::VideoCaptureDevice::Client::Buffer>( |
| 411 new AutoReleaseBuffer( | 411 new AutoReleaseBuffer( |
| 412 buffer_pool_, buffer_pool_->GetBufferHandle(buffer_id), buffer_id)); | 412 buffer_pool_, buffer_pool_->GetBufferHandle(buffer_id), buffer_id)); |
| 413 } | 413 } |
| 414 | 414 |
| 415 void OnError(const tracked_objects::Location& from_here, | 415 void OnError(const tracked_objects::Location& from_here, |
| 416 const std::string& reason) override { | 416 const std::string& reason) override { |
| 417 error_callback_.Run(); | 417 error_callback_.Run(); |
| 418 } | 418 } |
| 419 | 419 |
| 420 double GetBufferPoolUtilization() const override { return 0.0; } | 420 double GetBufferPoolUtilization() const override { return 0.0; } |
| 421 | 421 |
| 422 private: | 422 private: |
| 423 class AutoReleaseBuffer : public media::VideoCaptureDevice::Client::Buffer { | 423 class AutoReleaseBuffer : public device::VideoCaptureDevice::Client::Buffer { |
| 424 public: | 424 public: |
| 425 AutoReleaseBuffer( | 425 AutoReleaseBuffer( |
| 426 const scoped_refptr<VideoCaptureBufferPool>& pool, | 426 const scoped_refptr<VideoCaptureBufferPool>& pool, |
| 427 std::unique_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle, | 427 std::unique_ptr<VideoCaptureBufferPool::BufferHandle> buffer_handle, |
| 428 int buffer_id) | 428 int buffer_id) |
| 429 : id_(buffer_id), | 429 : id_(buffer_id), |
| 430 pool_(pool), | 430 pool_(pool), |
| 431 buffer_handle_(std::move(buffer_handle)) { | 431 buffer_handle_(std::move(buffer_handle)) { |
| 432 DCHECK(pool_); | 432 DCHECK(pool_); |
| 433 } | 433 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 : error_encountered_(false), | 467 : error_encountered_(false), |
| 468 wait_color_yuv_(0xcafe1950) { | 468 wait_color_yuv_(0xcafe1950) { |
| 469 client_.reset(new StubClient( | 469 client_.reset(new StubClient( |
| 470 base::Bind(&StubClientObserver::DidDeliverFrame, | 470 base::Bind(&StubClientObserver::DidDeliverFrame, |
| 471 base::Unretained(this)), | 471 base::Unretained(this)), |
| 472 base::Bind(&StubClientObserver::OnError, base::Unretained(this)))); | 472 base::Bind(&StubClientObserver::OnError, base::Unretained(this)))); |
| 473 } | 473 } |
| 474 | 474 |
| 475 virtual ~StubClientObserver() {} | 475 virtual ~StubClientObserver() {} |
| 476 | 476 |
| 477 std::unique_ptr<media::VideoCaptureDevice::Client> PassClient() { | 477 std::unique_ptr<device::VideoCaptureDevice::Client> PassClient() { |
| 478 return std::move(client_); | 478 return std::move(client_); |
| 479 } | 479 } |
| 480 | 480 |
| 481 void QuitIfConditionsMet(SkColor color, const gfx::Size& size) { | 481 void QuitIfConditionsMet(SkColor color, const gfx::Size& size) { |
| 482 base::AutoLock guard(lock_); | 482 base::AutoLock guard(lock_); |
| 483 if (error_encountered_ || wait_color_yuv_ == kNotInterested || | 483 if (error_encountered_ || wait_color_yuv_ == kNotInterested || |
| 484 wait_color_yuv_ == color) { | 484 wait_color_yuv_ == color) { |
| 485 last_frame_color_yuv_ = color; | 485 last_frame_color_yuv_ = color; |
| 486 last_frame_size_ = size; | 486 last_frame_size_ = size; |
| 487 base::MessageLoop::current()->QuitWhenIdle(); | 487 base::MessageLoop::current()->QuitWhenIdle(); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 render_frame_host_factory_.reset(); | 649 render_frame_host_factory_.reset(); |
| 650 render_view_host_factory_.reset(); | 650 render_view_host_factory_.reset(); |
| 651 render_process_host_factory_.reset(); | 651 render_process_host_factory_.reset(); |
| 652 | 652 |
| 653 display::Screen::SetScreenInstance(nullptr); | 653 display::Screen::SetScreenInstance(nullptr); |
| 654 } | 654 } |
| 655 | 655 |
| 656 // Accessors. | 656 // Accessors. |
| 657 CaptureTestSourceController* source() { return &controller_; } | 657 CaptureTestSourceController* source() { return &controller_; } |
| 658 WebContents* web_contents() const { return web_contents_.get(); } | 658 WebContents* web_contents() const { return web_contents_.get(); } |
| 659 media::VideoCaptureDevice* device() { return device_.get(); } | 659 device::VideoCaptureDevice* device() { return device_.get(); } |
| 660 | 660 |
| 661 // Returns the device scale factor of the capture target's native view. This | 661 // Returns the device scale factor of the capture target's native view. This |
| 662 // is necessary because, architecturally, the TestScreen implementation is | 662 // is necessary because, architecturally, the TestScreen implementation is |
| 663 // ignored on Mac platforms (when determining the device scale factor for a | 663 // ignored on Mac platforms (when determining the device scale factor for a |
| 664 // particular window). | 664 // particular window). |
| 665 float GetDeviceScaleFactor() const { | 665 float GetDeviceScaleFactor() const { |
| 666 RenderWidgetHostView* const view = | 666 RenderWidgetHostView* const view = |
| 667 web_contents_->GetRenderViewHost()->GetWidget()->GetView(); | 667 web_contents_->GetRenderViewHost()->GetWidget()->GetView(); |
| 668 CHECK(view); | 668 CHECK(view); |
| 669 return ui::GetScaleFactorForNativeView(view->GetNativeView()); | 669 return ui::GetScaleFactorForNativeView(view->GetNativeView()); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 base::RunLoop run_loop; | 740 base::RunLoop run_loop; |
| 741 BrowserThread::PostDelayedTask( | 741 BrowserThread::PostDelayedTask( |
| 742 BrowserThread::UI, FROM_HERE, | 742 BrowserThread::UI, FROM_HERE, |
| 743 run_loop.QuitClosure(), | 743 run_loop.QuitClosure(), |
| 744 base::TimeDelta::FromMicroseconds( | 744 base::TimeDelta::FromMicroseconds( |
| 745 3 * base::Time::kMicrosecondsPerSecond / kTestFramesPerSecond)); | 745 3 * base::Time::kMicrosecondsPerSecond / kTestFramesPerSecond)); |
| 746 run_loop.Run(); | 746 run_loop.Run(); |
| 747 | 747 |
| 748 BrowserThread::PostTask( | 748 BrowserThread::PostTask( |
| 749 BrowserThread::UI, FROM_HERE, | 749 BrowserThread::UI, FROM_HERE, |
| 750 base::Bind(&media::VideoCaptureDevice::RequestRefreshFrame, | 750 base::Bind(&device::VideoCaptureDevice::RequestRefreshFrame, |
| 751 base::Unretained(device_.get()))); | 751 base::Unretained(device_.get()))); |
| 752 } | 752 } |
| 753 | 753 |
| 754 void DestroyVideoCaptureDevice() { device_.reset(); } | 754 void DestroyVideoCaptureDevice() { device_.reset(); } |
| 755 | 755 |
| 756 StubClientObserver* client_observer() { | 756 StubClientObserver* client_observer() { |
| 757 return &client_observer_; | 757 return &client_observer_; |
| 758 } | 758 } |
| 759 | 759 |
| 760 private: | 760 private: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 773 std::unique_ptr<CaptureTestRenderViewHostFactory> render_view_host_factory_; | 773 std::unique_ptr<CaptureTestRenderViewHostFactory> render_view_host_factory_; |
| 774 | 774 |
| 775 // Self-registering RenderFrameHostFactory. | 775 // Self-registering RenderFrameHostFactory. |
| 776 std::unique_ptr<TestRenderFrameHostFactory> render_frame_host_factory_; | 776 std::unique_ptr<TestRenderFrameHostFactory> render_frame_host_factory_; |
| 777 | 777 |
| 778 // A mocked-out browser and tab. | 778 // A mocked-out browser and tab. |
| 779 std::unique_ptr<TestBrowserContext> browser_context_; | 779 std::unique_ptr<TestBrowserContext> browser_context_; |
| 780 std::unique_ptr<WebContents> web_contents_; | 780 std::unique_ptr<WebContents> web_contents_; |
| 781 | 781 |
| 782 // Finally, the WebContentsVideoCaptureDevice under test. | 782 // Finally, the WebContentsVideoCaptureDevice under test. |
| 783 std::unique_ptr<media::VideoCaptureDevice> device_; | 783 std::unique_ptr<device::VideoCaptureDevice> device_; |
| 784 | 784 |
| 785 TestBrowserThreadBundle thread_bundle_; | 785 TestBrowserThreadBundle thread_bundle_; |
| 786 }; | 786 }; |
| 787 | 787 |
| 788 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest, | 788 TEST_F(MAYBE_WebContentsVideoCaptureDeviceTest, |
| 789 InvalidInitialWebContentsError) { | 789 InvalidInitialWebContentsError) { |
| 790 // Before the installs itself on the UI thread up to start capturing, we'll | 790 // Before the installs itself on the UI thread up to start capturing, we'll |
| 791 // delete the web contents. This should trigger an error which can happen in | 791 // delete the web contents. This should trigger an error which can happen in |
| 792 // practice; we should be able to recover gracefully. | 792 // practice; we should be able to recover gracefully. |
| 793 ResetWebContents(); | 793 ResetWebContents(); |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1209 for (int i = 0; i < 3; ++i) { | 1209 for (int i = 0; i < 3; ++i) { |
| 1210 SimulateRefreshFrameRequest(); | 1210 SimulateRefreshFrameRequest(); |
| 1211 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN)); | 1211 ASSERT_NO_FATAL_FAILURE(client_observer()->WaitForNextColor(SK_ColorGREEN)); |
| 1212 } | 1212 } |
| 1213 | 1213 |
| 1214 device()->StopAndDeAllocate(); | 1214 device()->StopAndDeAllocate(); |
| 1215 } | 1215 } |
| 1216 | 1216 |
| 1217 } // namespace | 1217 } // namespace |
| 1218 } // namespace content | 1218 } // namespace content |
| OLD | NEW |