Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/desktop_capture_device.h" | 5 #include "content/browser/media/capture/desktop_capture_device.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/feature_list.h" | 13 #include "base/feature_list.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
| 21 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
| 22 #include "base/threading/thread_restrictions.h" | 22 #include "base/threading/thread_restrictions.h" |
| 23 #include "base/timer/timer.h" | 23 #include "base/timer/timer.h" |
| 24 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 25 #include "content/browser/media/capture/desktop_capture_device_uma_types.h" | 25 #include "content/browser/media/capture/desktop_capture_device_uma_types.h" |
| 26 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/desktop_media_id.h" | 27 #include "content/public/browser/desktop_media_id.h" |
| 28 #include "content/public/common/content_switches.h" | 28 #include "content/public/common/content_switches.h" |
| 29 #include "device/power_save_blocker/power_save_blocker.h" | 29 #include "content/public/common/service_manager_connection.h" |
| 30 #include "device/wake_lock/public/interfaces/wake_lock_provider.mojom.h" | |
| 31 #include "device/wake_lock/public/interfaces/wake_lock_service.mojom.h" | |
| 30 #include "media/base/video_util.h" | 32 #include "media/base/video_util.h" |
| 31 #include "media/capture/content/capture_resolution_chooser.h" | 33 #include "media/capture/content/capture_resolution_chooser.h" |
| 34 #include "services/device/public/interfaces/constants.mojom.h" | |
| 35 #include "services/service_manager/public/cpp/connector.h" | |
| 32 #include "third_party/libyuv/include/libyuv/scale_argb.h" | 36 #include "third_party/libyuv/include/libyuv/scale_argb.h" |
| 33 #include "third_party/webrtc/modules/desktop_capture/cropping_window_capturer.h" | 37 #include "third_party/webrtc/modules/desktop_capture/cropping_window_capturer.h" |
| 34 #include "third_party/webrtc/modules/desktop_capture/desktop_and_cursor_composer .h" | 38 #include "third_party/webrtc/modules/desktop_capture/desktop_and_cursor_composer .h" |
| 35 #include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h" | 39 #include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h" |
| 36 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" | 40 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" |
| 37 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 41 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 38 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h" | 42 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h" |
| 39 | 43 |
| 40 namespace content { | 44 namespace content { |
| 41 | 45 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 55 gfx::Size(source_size.width(), source_size.height())); | 59 gfx::Size(source_size.width(), source_size.height())); |
| 56 return webrtc::DesktopRect::MakeLTRB( | 60 return webrtc::DesktopRect::MakeLTRB( |
| 57 result.x(), result.y(), result.right(), result.bottom()); | 61 result.x(), result.y(), result.right(), result.bottom()); |
| 58 } | 62 } |
| 59 | 63 |
| 60 bool IsFrameUnpackedOrInverted(webrtc::DesktopFrame* frame) { | 64 bool IsFrameUnpackedOrInverted(webrtc::DesktopFrame* frame) { |
| 61 return frame->stride() != | 65 return frame->stride() != |
| 62 frame->size().width() * webrtc::DesktopFrame::kBytesPerPixel; | 66 frame->size().width() * webrtc::DesktopFrame::kBytesPerPixel; |
| 63 } | 67 } |
| 64 | 68 |
| 69 std::unique_ptr<service_manager::Connector> GetConnector() { | |
|
Do not use (sergeyu)
2017/05/16 19:29:43
Maybe call it GetServiceConnector()?
ke.he
2017/05/17 10:25:52
Done.
| |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 71 | |
| 72 service_manager::Connector* connector = | |
| 73 ServiceManagerConnection::GetForProcess()->GetConnector(); | |
| 74 | |
| 75 DCHECK(connector); | |
| 76 return connector->Clone(); | |
| 77 } | |
| 78 | |
| 65 } // namespace | 79 } // namespace |
| 66 | 80 |
| 67 #if defined(OS_WIN) | 81 #if defined(OS_WIN) |
| 68 const base::Feature kDirectXCapturer{"DirectXCapturer", | 82 const base::Feature kDirectXCapturer{"DirectXCapturer", |
| 69 base::FEATURE_ENABLED_BY_DEFAULT}; | 83 base::FEATURE_ENABLED_BY_DEFAULT}; |
| 70 #endif | 84 #endif |
| 71 | 85 |
| 72 class DesktopCaptureDevice::Core : public webrtc::DesktopCapturer::Callback { | 86 class DesktopCaptureDevice::Core : public webrtc::DesktopCapturer::Callback { |
| 73 public: | 87 public: |
| 74 Core(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 88 Core(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 91 // Method that is scheduled on |task_runner_| to be called on regular interval | 105 // Method that is scheduled on |task_runner_| to be called on regular interval |
| 92 // to capture a frame. | 106 // to capture a frame. |
| 93 void OnCaptureTimer(); | 107 void OnCaptureTimer(); |
| 94 | 108 |
| 95 // Captures a frame and schedules timer for the next one. | 109 // Captures a frame and schedules timer for the next one. |
| 96 void CaptureFrameAndScheduleNext(); | 110 void CaptureFrameAndScheduleNext(); |
| 97 | 111 |
| 98 // Captures a single frame. | 112 // Captures a single frame. |
| 99 void DoCapture(); | 113 void DoCapture(); |
| 100 | 114 |
| 115 // Requests a wake lock. | |
|
Do not use (sergeyu)
2017/05/16 19:29:43
I don't think this comment is useful - it just dup
ke.he
2017/05/17 10:25:52
Done.
| |
| 116 void RequestWakeLock( | |
| 117 const std::unique_ptr<service_manager::Connector>& connector); | |
|
Do not use (sergeyu)
2017/05/16 19:29:43
I don't think it needs to be const reference. Norm
ke.he
2017/05/17 10:25:52
Done.
| |
| 118 | |
| 101 // Task runner used for capturing operations. | 119 // Task runner used for capturing operations. |
| 102 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 120 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 103 | 121 |
| 104 // The underlying DesktopCapturer instance used to capture frames. | 122 // The underlying DesktopCapturer instance used to capture frames. |
| 105 std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer_; | 123 std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer_; |
| 106 | 124 |
| 107 // The device client which proxies device events to the controller. Accessed | 125 // The device client which proxies device events to the controller. Accessed |
| 108 // on the task_runner_ thread. | 126 // on the task_runner_ thread. |
| 109 std::unique_ptr<Client> client_; | 127 std::unique_ptr<Client> client_; |
| 110 | 128 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 133 bool first_capture_returned_; | 151 bool first_capture_returned_; |
| 134 | 152 |
| 135 // The type of the capturer. | 153 // The type of the capturer. |
| 136 DesktopMediaID::Type capturer_type_; | 154 DesktopMediaID::Type capturer_type_; |
| 137 | 155 |
| 138 // The system time when we receive the first frame. | 156 // The system time when we receive the first frame. |
| 139 base::TimeTicks first_ref_time_; | 157 base::TimeTicks first_ref_time_; |
| 140 | 158 |
| 141 std::unique_ptr<webrtc::BasicDesktopFrame> black_frame_; | 159 std::unique_ptr<webrtc::BasicDesktopFrame> black_frame_; |
| 142 | 160 |
| 143 // TODO(jiayl): Remove power_save_blocker_ when there is an API to keep the | 161 device::mojom::WakeLockServicePtr wake_lock_; |
|
Sergey Ulanov
2017/05/17 18:47:55
Please keep this TODO, it's still relevant
ke.he
2017/05/18 05:32:58
Done. and also in "aura_window_capture_machine.h"
| |
| 144 // screen from sleeping for the drive-by web. | |
| 145 std::unique_ptr<device::PowerSaveBlocker> power_save_blocker_; | |
| 146 | 162 |
| 147 DISALLOW_COPY_AND_ASSIGN(Core); | 163 DISALLOW_COPY_AND_ASSIGN(Core); |
| 148 }; | 164 }; |
| 149 | 165 |
| 150 DesktopCaptureDevice::Core::Core( | 166 DesktopCaptureDevice::Core::Core( |
| 151 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 167 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 152 std::unique_ptr<webrtc::DesktopCapturer> capturer, | 168 std::unique_ptr<webrtc::DesktopCapturer> capturer, |
| 153 DesktopMediaID::Type type) | 169 DesktopMediaID::Type type) |
| 154 : task_runner_(task_runner), | 170 : task_runner_(task_runner), |
| 155 desktop_capturer_(std::move(capturer)), | 171 desktop_capturer_(std::move(capturer)), |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 174 DCHECK(desktop_capturer_); | 190 DCHECK(desktop_capturer_); |
| 175 DCHECK(client); | 191 DCHECK(client); |
| 176 DCHECK(!client_); | 192 DCHECK(!client_); |
| 177 | 193 |
| 178 client_ = std::move(client); | 194 client_ = std::move(client); |
| 179 requested_frame_rate_ = params.requested_format.frame_rate; | 195 requested_frame_rate_ = params.requested_format.frame_rate; |
| 180 resolution_chooser_.reset(new media::CaptureResolutionChooser( | 196 resolution_chooser_.reset(new media::CaptureResolutionChooser( |
| 181 params.requested_format.frame_size, | 197 params.requested_format.frame_size, |
| 182 params.resolution_change_policy)); | 198 params.resolution_change_policy)); |
| 183 | 199 |
| 184 power_save_blocker_.reset(new device::PowerSaveBlocker( | 200 // Gets a service_manager::connector first, then request a wake lock. |
|
Do not use (sergeyu)
2017/05/16 19:29:43
s/connector/Connector/
ke.he
2017/05/17 10:25:52
Done.
| |
| 185 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | 201 BrowserThread::PostTaskAndReplyWithResult( |
| 186 device::PowerSaveBlocker::kReasonOther, "DesktopCaptureDevice is running", | 202 BrowserThread::UI, FROM_HERE, base::BindOnce(&GetConnector), |
| 187 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI), | 203 base::BindOnce(&DesktopCaptureDevice::Core::RequestWakeLock, |
| 188 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE))); | 204 base::Unretained(this))); |
|
Do not use (sergeyu)
2017/05/16 19:29:43
I don't think base::Unretained() is safe here. Add
ke.he
2017/05/17 10:25:52
Oh! Yes, should use WeakPtr here. thanks!
Done.
| |
| 189 | 205 |
| 190 desktop_capturer_->Start(this); | 206 desktop_capturer_->Start(this); |
| 191 // Assume it will be always started successfully for now. | 207 // Assume it will be always started successfully for now. |
| 192 client_->OnStarted(); | 208 client_->OnStarted(); |
| 193 | 209 |
| 194 CaptureFrameAndScheduleNext(); | 210 CaptureFrameAndScheduleNext(); |
| 195 } | 211 } |
| 196 | 212 |
| 197 void DesktopCaptureDevice::Core::SetNotificationWindowId( | 213 void DesktopCaptureDevice::Core::SetNotificationWindowId( |
| 198 gfx::NativeViewId window_id) { | 214 gfx::NativeViewId window_id) { |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 357 DCHECK(!capture_in_progress_); | 373 DCHECK(!capture_in_progress_); |
| 358 | 374 |
| 359 capture_in_progress_ = true; | 375 capture_in_progress_ = true; |
| 360 desktop_capturer_->CaptureFrame(); | 376 desktop_capturer_->CaptureFrame(); |
| 361 | 377 |
| 362 // Currently only synchronous implementations of DesktopCapturer are | 378 // Currently only synchronous implementations of DesktopCapturer are |
| 363 // supported. | 379 // supported. |
| 364 DCHECK(!capture_in_progress_); | 380 DCHECK(!capture_in_progress_); |
| 365 } | 381 } |
| 366 | 382 |
| 383 void DesktopCaptureDevice::Core::RequestWakeLock( | |
| 384 const std::unique_ptr<service_manager::Connector>& connector) { | |
| 385 device::mojom::WakeLockProviderPtr wake_lock_provider; | |
| 386 connector->BindInterface(device::mojom::kServiceName, | |
| 387 mojo::MakeRequest(&wake_lock_provider)); | |
| 388 wake_lock_provider->GetWakeLockWithoutContext( | |
| 389 device::mojom::WakeLockType::PreventDisplaySleep, | |
| 390 device::mojom::WakeLockReason::ReasonOther, | |
| 391 "DesktopCaptureDevice is running", mojo::MakeRequest(&wake_lock_)); | |
|
Do not use (sergeyu)
2017/05/16 19:29:43
Please change the description to "Desktop capturer
ke.he
2017/05/17 10:25:52
"is active" or "is running"?
Done with "is running
Sergey Ulanov
2017/05/17 18:47:55
I'm fine with either.
| |
| 392 | |
| 393 wake_lock_->RequestWakeLock(); | |
| 394 } | |
| 395 | |
| 367 // static | 396 // static |
| 368 std::unique_ptr<media::VideoCaptureDevice> DesktopCaptureDevice::Create( | 397 std::unique_ptr<media::VideoCaptureDevice> DesktopCaptureDevice::Create( |
| 369 const DesktopMediaID& source) { | 398 const DesktopMediaID& source) { |
| 370 webrtc::DesktopCaptureOptions options = | 399 webrtc::DesktopCaptureOptions options = |
| 371 webrtc::DesktopCaptureOptions::CreateDefault(); | 400 webrtc::DesktopCaptureOptions::CreateDefault(); |
| 372 // Leave desktop effects enabled during WebRTC captures. | 401 // Leave desktop effects enabled during WebRTC captures. |
| 373 options.set_disable_effects(false); | 402 options.set_disable_effects(false); |
| 374 | 403 |
| 375 #if defined(OS_WIN) | 404 #if defined(OS_WIN) |
| 376 if (!base::FeatureList::IsEnabled(kDirectXCapturer)) { | 405 if (!base::FeatureList::IsEnabled(kDirectXCapturer)) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 442 thread_.Stop(); | 471 thread_.Stop(); |
| 443 } | 472 } |
| 444 } | 473 } |
| 445 | 474 |
| 446 void DesktopCaptureDevice::SetNotificationWindowId( | 475 void DesktopCaptureDevice::SetNotificationWindowId( |
| 447 gfx::NativeViewId window_id) { | 476 gfx::NativeViewId window_id) { |
| 448 // This may be called after the capturer has been stopped. | 477 // This may be called after the capturer has been stopped. |
| 449 if (!core_) | 478 if (!core_) |
| 450 return; | 479 return; |
| 451 thread_.task_runner()->PostTask( | 480 thread_.task_runner()->PostTask( |
| 452 FROM_HERE, | 481 FROM_HERE, base::Bind(&Core::SetNotificationWindowId, |
| 453 base::Bind(&Core::SetNotificationWindowId, | 482 base::Unretained(core_.get()), window_id)); |
| 454 base::Unretained(core_.get()), | |
| 455 window_id)); | |
| 456 } | 483 } |
| 457 | 484 |
| 458 DesktopCaptureDevice::DesktopCaptureDevice( | 485 DesktopCaptureDevice::DesktopCaptureDevice( |
| 459 std::unique_ptr<webrtc::DesktopCapturer> capturer, | 486 std::unique_ptr<webrtc::DesktopCapturer> capturer, |
| 460 DesktopMediaID::Type type) | 487 DesktopMediaID::Type type) |
| 461 : thread_("desktopCaptureThread") { | 488 : thread_("desktopCaptureThread") { |
| 462 #if defined(OS_WIN) | 489 #if defined(OS_WIN) |
| 463 // On Windows the thread must be a UI thread. | 490 // On Windows the thread must be a UI thread. |
| 464 base::MessageLoop::Type thread_type = base::MessageLoop::TYPE_UI; | 491 base::MessageLoop::Type thread_type = base::MessageLoop::TYPE_UI; |
| 465 #else | 492 #else |
| 466 base::MessageLoop::Type thread_type = base::MessageLoop::TYPE_DEFAULT; | 493 base::MessageLoop::Type thread_type = base::MessageLoop::TYPE_DEFAULT; |
| 467 #endif | 494 #endif |
| 468 | 495 |
| 469 thread_.StartWithOptions(base::Thread::Options(thread_type, 0)); | 496 thread_.StartWithOptions(base::Thread::Options(thread_type, 0)); |
| 470 | 497 |
| 471 core_.reset(new Core(thread_.task_runner(), std::move(capturer), type)); | 498 core_.reset(new Core(thread_.task_runner(), std::move(capturer), type)); |
| 472 } | 499 } |
| 473 | 500 |
| 474 } // namespace content | 501 } // namespace content |
| OLD | NEW |