Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: content/browser/media/capture/desktop_capture_device.cc

Issue 2881143003: Convert //content/browser/media to be clients of WakeLock mojo service. (Closed)
Patch Set: wake lock bindinterface should only be executed once. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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> GetServiceConnector() {
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
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 void BindAndRequestWakeLock(
116 std::unique_ptr<service_manager::Connector> connector);
117
101 // Task runner used for capturing operations. 118 // Task runner used for capturing operations.
102 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 119 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
103 120
104 // The underlying DesktopCapturer instance used to capture frames. 121 // The underlying DesktopCapturer instance used to capture frames.
105 std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer_; 122 std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer_;
106 123
107 // The device client which proxies device events to the controller. Accessed 124 // The device client which proxies device events to the controller. Accessed
108 // on the task_runner_ thread. 125 // on the task_runner_ thread.
109 std::unique_ptr<Client> client_; 126 std::unique_ptr<Client> client_;
110 127
(...skipping 22 matching lines...) Expand all
133 bool first_capture_returned_; 150 bool first_capture_returned_;
134 151
135 // The type of the capturer. 152 // The type of the capturer.
136 DesktopMediaID::Type capturer_type_; 153 DesktopMediaID::Type capturer_type_;
137 154
138 // The system time when we receive the first frame. 155 // The system time when we receive the first frame.
139 base::TimeTicks first_ref_time_; 156 base::TimeTicks first_ref_time_;
140 157
141 std::unique_ptr<webrtc::BasicDesktopFrame> black_frame_; 158 std::unique_ptr<webrtc::BasicDesktopFrame> black_frame_;
142 159
143 // TODO(jiayl): Remove power_save_blocker_ when there is an API to keep the 160 device::mojom::WakeLockServicePtr wake_lock_;
144 // screen from sleeping for the drive-by web. 161
145 std::unique_ptr<device::PowerSaveBlocker> power_save_blocker_; 162 base::WeakPtrFactory<Core> weak_factory_;
146 163
147 DISALLOW_COPY_AND_ASSIGN(Core); 164 DISALLOW_COPY_AND_ASSIGN(Core);
148 }; 165 };
149 166
150 DesktopCaptureDevice::Core::Core( 167 DesktopCaptureDevice::Core::Core(
151 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 168 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
152 std::unique_ptr<webrtc::DesktopCapturer> capturer, 169 std::unique_ptr<webrtc::DesktopCapturer> capturer,
153 DesktopMediaID::Type type) 170 DesktopMediaID::Type type)
154 : task_runner_(task_runner), 171 : task_runner_(task_runner),
155 desktop_capturer_(std::move(capturer)), 172 desktop_capturer_(std::move(capturer)),
156 capture_in_progress_(false), 173 capture_in_progress_(false),
157 first_capture_returned_(false), 174 first_capture_returned_(false),
158 capturer_type_(type) {} 175 capturer_type_(type),
176 weak_factory_(this) {}
159 177
160 DesktopCaptureDevice::Core::~Core() { 178 DesktopCaptureDevice::Core::~Core() {
161 DCHECK(task_runner_->BelongsToCurrentThread()); 179 DCHECK(task_runner_->BelongsToCurrentThread());
162 client_.reset(); 180 client_.reset();
163 output_frame_.reset(); 181 output_frame_.reset();
164 previous_frame_size_.set(0, 0); 182 previous_frame_size_.set(0, 0);
165 desktop_capturer_.reset(); 183 desktop_capturer_.reset();
166 } 184 }
167 185
168 void DesktopCaptureDevice::Core::AllocateAndStart( 186 void DesktopCaptureDevice::Core::AllocateAndStart(
169 const media::VideoCaptureParams& params, 187 const media::VideoCaptureParams& params,
170 std::unique_ptr<Client> client) { 188 std::unique_ptr<Client> client) {
171 DCHECK(task_runner_->BelongsToCurrentThread()); 189 DCHECK(task_runner_->BelongsToCurrentThread());
172 DCHECK_GT(params.requested_format.frame_size.GetArea(), 0); 190 DCHECK_GT(params.requested_format.frame_size.GetArea(), 0);
173 DCHECK_GT(params.requested_format.frame_rate, 0); 191 DCHECK_GT(params.requested_format.frame_rate, 0);
174 DCHECK(desktop_capturer_); 192 DCHECK(desktop_capturer_);
175 DCHECK(client); 193 DCHECK(client);
176 DCHECK(!client_); 194 DCHECK(!client_);
177 195
178 client_ = std::move(client); 196 client_ = std::move(client);
179 requested_frame_rate_ = params.requested_format.frame_rate; 197 requested_frame_rate_ = params.requested_format.frame_rate;
180 resolution_chooser_.reset(new media::CaptureResolutionChooser( 198 resolution_chooser_.reset(new media::CaptureResolutionChooser(
181 params.requested_format.frame_size, 199 params.requested_format.frame_size,
182 params.resolution_change_policy)); 200 params.resolution_change_policy));
183 201
184 power_save_blocker_.reset(new device::PowerSaveBlocker( 202 if (!wake_lock_) {
Sergey Ulanov 2017/05/17 18:47:55 AllocateAndStart() is never called more than once,
ke.he 2017/05/18 05:32:58 Done.
185 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, 203 // Gets a service_manager::Connector first, then binds the |wake_lock_|
186 device::PowerSaveBlocker::kReasonOther, "DesktopCaptureDevice is running", 204 // and requests a wake lock.
187 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI), 205 BrowserThread::PostTaskAndReplyWithResult(
188 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE))); 206 BrowserThread::UI, FROM_HERE, base::BindOnce(&GetServiceConnector),
207 base::BindOnce(&DesktopCaptureDevice::Core::BindAndRequestWakeLock,
208 weak_factory_.GetWeakPtr()));
209 } else {
210 // If |wake_lock_| is in bound already, request a wake lock directly.
211 wake_lock_->RequestWakeLock();
212 }
189 213
190 desktop_capturer_->Start(this); 214 desktop_capturer_->Start(this);
191 // Assume it will be always started successfully for now. 215 // Assume it will be always started successfully for now.
192 client_->OnStarted(); 216 client_->OnStarted();
193 217
194 CaptureFrameAndScheduleNext(); 218 CaptureFrameAndScheduleNext();
195 } 219 }
196 220
197 void DesktopCaptureDevice::Core::SetNotificationWindowId( 221 void DesktopCaptureDevice::Core::SetNotificationWindowId(
198 gfx::NativeViewId window_id) { 222 gfx::NativeViewId window_id) {
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 DCHECK(!capture_in_progress_); 381 DCHECK(!capture_in_progress_);
358 382
359 capture_in_progress_ = true; 383 capture_in_progress_ = true;
360 desktop_capturer_->CaptureFrame(); 384 desktop_capturer_->CaptureFrame();
361 385
362 // Currently only synchronous implementations of DesktopCapturer are 386 // Currently only synchronous implementations of DesktopCapturer are
363 // supported. 387 // supported.
364 DCHECK(!capture_in_progress_); 388 DCHECK(!capture_in_progress_);
365 } 389 }
366 390
391 void DesktopCaptureDevice::Core::BindAndRequestWakeLock(
392 std::unique_ptr<service_manager::Connector> connector) {
393 device::mojom::WakeLockProviderPtr wake_lock_provider;
394 connector->BindInterface(device::mojom::kServiceName,
395 mojo::MakeRequest(&wake_lock_provider));
396 wake_lock_provider->GetWakeLockWithoutContext(
397 device::mojom::WakeLockType::PreventDisplaySleep,
398 device::mojom::WakeLockReason::ReasonOther, "Desktop capture is running",
399 mojo::MakeRequest(&wake_lock_));
400
401 wake_lock_->RequestWakeLock();
402 }
403
367 // static 404 // static
368 std::unique_ptr<media::VideoCaptureDevice> DesktopCaptureDevice::Create( 405 std::unique_ptr<media::VideoCaptureDevice> DesktopCaptureDevice::Create(
369 const DesktopMediaID& source) { 406 const DesktopMediaID& source) {
370 webrtc::DesktopCaptureOptions options = 407 webrtc::DesktopCaptureOptions options =
371 webrtc::DesktopCaptureOptions::CreateDefault(); 408 webrtc::DesktopCaptureOptions::CreateDefault();
372 // Leave desktop effects enabled during WebRTC captures. 409 // Leave desktop effects enabled during WebRTC captures.
373 options.set_disable_effects(false); 410 options.set_disable_effects(false);
374 411
375 #if defined(OS_WIN) 412 #if defined(OS_WIN)
376 if (!base::FeatureList::IsEnabled(kDirectXCapturer)) { 413 if (!base::FeatureList::IsEnabled(kDirectXCapturer)) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 thread_.Stop(); 479 thread_.Stop();
443 } 480 }
444 } 481 }
445 482
446 void DesktopCaptureDevice::SetNotificationWindowId( 483 void DesktopCaptureDevice::SetNotificationWindowId(
447 gfx::NativeViewId window_id) { 484 gfx::NativeViewId window_id) {
448 // This may be called after the capturer has been stopped. 485 // This may be called after the capturer has been stopped.
449 if (!core_) 486 if (!core_)
450 return; 487 return;
451 thread_.task_runner()->PostTask( 488 thread_.task_runner()->PostTask(
452 FROM_HERE, 489 FROM_HERE, base::Bind(&Core::SetNotificationWindowId,
453 base::Bind(&Core::SetNotificationWindowId, 490 base::Unretained(core_.get()), window_id));
454 base::Unretained(core_.get()),
455 window_id));
456 } 491 }
457 492
458 DesktopCaptureDevice::DesktopCaptureDevice( 493 DesktopCaptureDevice::DesktopCaptureDevice(
459 std::unique_ptr<webrtc::DesktopCapturer> capturer, 494 std::unique_ptr<webrtc::DesktopCapturer> capturer,
460 DesktopMediaID::Type type) 495 DesktopMediaID::Type type)
461 : thread_("desktopCaptureThread") { 496 : thread_("desktopCaptureThread") {
462 #if defined(OS_WIN) 497 #if defined(OS_WIN)
463 // On Windows the thread must be a UI thread. 498 // On Windows the thread must be a UI thread.
464 base::MessageLoop::Type thread_type = base::MessageLoop::TYPE_UI; 499 base::MessageLoop::Type thread_type = base::MessageLoop::TYPE_UI;
465 #else 500 #else
466 base::MessageLoop::Type thread_type = base::MessageLoop::TYPE_DEFAULT; 501 base::MessageLoop::Type thread_type = base::MessageLoop::TYPE_DEFAULT;
467 #endif 502 #endif
468 503
469 thread_.StartWithOptions(base::Thread::Options(thread_type, 0)); 504 thread_.StartWithOptions(base::Thread::Options(thread_type, 0));
470 505
471 core_.reset(new Core(thread_.task_runner(), std::move(capturer), type)); 506 core_.reset(new Core(thread_.task_runner(), std::move(capturer), type));
472 } 507 }
473 508
474 } // namespace content 509 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698