1 // Copyright 2015 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 "remoting/host/desktop_capturer_proxy.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "remoting/proto/control.pb.h"
12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
13 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
17 class DesktopCapturerProxy::Core
: public webrtc::DesktopCapturer::Callback
{
19 Core(base::WeakPtr
<DesktopCapturerProxy
> proxy
,
20 scoped_refptr
<base::SingleThreadTaskRunner
> capture_task_runner
,
21 scoped_ptr
<webrtc::DesktopCapturer
> capturer
);
25 void Capture(const webrtc::DesktopRegion
& rect
);
28 // webrtc::DesktopCapturer::Callback implementation.
29 webrtc::SharedMemory
* CreateSharedMemory(size_t size
) override
;
30 void OnCaptureCompleted(webrtc::DesktopFrame
* frame
) override
;
32 base::ThreadChecker thread_checker_
;
34 base::WeakPtr
<DesktopCapturerProxy
> proxy_
;
35 scoped_refptr
<base::SingleThreadTaskRunner
> caller_task_runner_
;
36 scoped_ptr
<webrtc::DesktopCapturer
> capturer_
;
38 DISALLOW_COPY_AND_ASSIGN(Core
);
41 DesktopCapturerProxy::Core::Core(
42 base::WeakPtr
<DesktopCapturerProxy
> proxy
,
43 scoped_refptr
<base::SingleThreadTaskRunner
> capture_task_runner
,
44 scoped_ptr
<webrtc::DesktopCapturer
> capturer
)
46 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()),
47 capturer_(capturer
.Pass()) {
48 thread_checker_
.DetachFromThread();
51 DesktopCapturerProxy::Core::~Core() {
52 DCHECK(thread_checker_
.CalledOnValidThread());
55 void DesktopCapturerProxy::Core::Start() {
56 DCHECK(thread_checker_
.CalledOnValidThread());
57 capturer_
->Start(this);
60 void DesktopCapturerProxy::Core::Capture(const webrtc::DesktopRegion
& rect
) {
61 DCHECK(thread_checker_
.CalledOnValidThread());
63 capturer_
->Capture(rect
);
66 webrtc::SharedMemory
* DesktopCapturerProxy::Core::CreateSharedMemory(
68 DCHECK(thread_checker_
.CalledOnValidThread());
70 // CreateSharedMemory() call is synchronous and cannot be proxied to another
75 void DesktopCapturerProxy::Core::OnCaptureCompleted(
76 webrtc::DesktopFrame
* frame
) {
77 DCHECK(thread_checker_
.CalledOnValidThread());
79 caller_task_runner_
->PostTask(
80 FROM_HERE
, base::Bind(&DesktopCapturerProxy::OnFrameCaptured
, proxy_
,
81 base::Passed(make_scoped_ptr(frame
))));
84 DesktopCapturerProxy::DesktopCapturerProxy(
85 scoped_refptr
<base::SingleThreadTaskRunner
> capture_task_runner
,
86 scoped_ptr
<webrtc::DesktopCapturer
> capturer
)
87 : capture_task_runner_(capture_task_runner
),
89 core_
.reset(new Core(weak_factory_
.GetWeakPtr(), capture_task_runner
,
93 void DesktopCapturerProxy::Start(Callback
* callback
) {
94 DCHECK(thread_checker_
.CalledOnValidThread());
98 capture_task_runner_
->PostTask(
99 FROM_HERE
, base::Bind(&Core::Start
, base::Unretained(core_
.get())));
102 DesktopCapturerProxy::~DesktopCapturerProxy() {
103 DCHECK(thread_checker_
.CalledOnValidThread());
105 capture_task_runner_
->DeleteSoon(FROM_HERE
, core_
.release());
108 void DesktopCapturerProxy::Capture(const webrtc::DesktopRegion
& rect
) {
109 DCHECK(thread_checker_
.CalledOnValidThread());
111 // Start() must be called before Capture().
114 capture_task_runner_
->PostTask(
116 base::Bind(&Core::Capture
, base::Unretained(core_
.get()), rect
));
119 void DesktopCapturerProxy::OnFrameCaptured(
120 scoped_ptr
<webrtc::DesktopFrame
> frame
) {
121 DCHECK(thread_checker_
.CalledOnValidThread());
123 callback_
->OnCaptureCompleted(frame
.release());
126 } // namespace remoting