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.
5 #ifndef REMOTING_HOST_DESKTOP_SESSION_PROXY_H_
6 #define REMOTING_HOST_DESKTOP_SESSION_PROXY_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/process/process.h"
15 #include "base/sequenced_task_runner_helpers.h"
16 #include "ipc/ipc_listener.h"
17 #include "ipc/ipc_platform_file.h"
18 #include "remoting/host/audio_capturer.h"
19 #include "remoting/host/desktop_environment.h"
20 #include "remoting/host/screen_resolution.h"
21 #include "remoting/proto/event.pb.h"
22 #include "remoting/protocol/clipboard_stub.h"
23 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h"
26 class SingleThreadTaskRunner
;
38 struct SerializedDesktopFrame
;
44 class ClientSessionControl
;
45 class DesktopSessionConnector
;
46 struct DesktopSessionProxyTraits
;
47 class IpcAudioCapturer
;
48 class IpcMouseCursorMonitor
;
49 class IpcVideoFrameCapturer
;
52 // DesktopSessionProxy is created by an owning DesktopEnvironment to route
53 // requests from stubs to the DesktopSessionAgent instance through
54 // the IPC channel. DesktopSessionProxy is owned both by the DesktopEnvironment
55 // and the stubs, since stubs can out-live their DesktopEnvironment.
57 // DesktopSessionProxy objects are ref-counted but are always deleted on
58 // the |caller_tast_runner_| thread. This makes it possible to continue
59 // to receive IPC messages after the ref-count has dropped to zero, until
60 // the proxy is deleted. DesktopSessionProxy must therefore avoid creating new
61 // references to the itself while handling IPC messages and desktop
62 // attach/detach notifications.
64 // All public methods of DesktopSessionProxy are called on
65 // the |caller_task_runner_| thread unless it is specified otherwise.
66 class DesktopSessionProxy
67 : public base::RefCountedThreadSafe
<DesktopSessionProxy
,
68 DesktopSessionProxyTraits
>,
69 public IPC::Listener
{
72 scoped_refptr
<base::SingleThreadTaskRunner
> audio_capture_task_runner
,
73 scoped_refptr
<base::SingleThreadTaskRunner
> caller_task_runner
,
74 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
75 scoped_refptr
<base::SingleThreadTaskRunner
> video_capture_task_runner
,
76 base::WeakPtr
<ClientSessionControl
> client_session_control
,
77 base::WeakPtr
<DesktopSessionConnector
> desktop_session_connector
,
78 bool virtual_terminal
);
80 // Mirrors DesktopEnvironment.
81 scoped_ptr
<AudioCapturer
> CreateAudioCapturer();
82 scoped_ptr
<InputInjector
> CreateInputInjector();
83 scoped_ptr
<ScreenControls
> CreateScreenControls();
84 scoped_ptr
<webrtc::DesktopCapturer
> CreateVideoCapturer();
85 scoped_ptr
<webrtc::MouseCursorMonitor
> CreateMouseCursorMonitor();
86 std::string
GetCapabilities() const;
87 void SetCapabilities(const std::string
& capabilities
);
89 // IPC::Listener implementation.
90 bool OnMessageReceived(const IPC::Message
& message
) override
;
91 void OnChannelConnected(int32 peer_pid
) override
;
92 void OnChannelError() override
;
94 // Connects to the desktop session agent.
95 bool AttachToDesktop(base::Process desktop_process
,
96 IPC::PlatformFileForTransit desktop_pipe
);
98 // Closes the connection to the desktop session agent and cleans up
99 // the associated resources.
100 void DetachFromDesktop();
102 // Disconnects the client session that owns |this|.
103 void DisconnectSession();
105 // Stores |audio_capturer| to be used to post captured audio packets. Called
106 // on the |audio_capture_task_runner_| thread.
107 void SetAudioCapturer(const base::WeakPtr
<IpcAudioCapturer
>& audio_capturer
);
109 // APIs used to implement the webrtc::DesktopCapturer interface. These must be
110 // called on the |video_capture_task_runner_| thread.
113 // Stores |video_capturer| to be used to post captured video frames. Called on
114 // the |video_capture_task_runner_| thread.
115 void SetVideoCapturer(
116 const base::WeakPtr
<IpcVideoFrameCapturer
> video_capturer
);
118 // Stores |mouse_cursor_monitor| to be used to post mouse cursor changes.
119 // Called on the |video_capture_task_runner_| thread.
120 void SetMouseCursorMonitor(
121 const base::WeakPtr
<IpcMouseCursorMonitor
>& mouse_cursor_monitor
);
123 // APIs used to implement the InputInjector interface.
124 void InjectClipboardEvent(const protocol::ClipboardEvent
& event
);
125 void InjectKeyEvent(const protocol::KeyEvent
& event
);
126 void InjectTextEvent(const protocol::TextEvent
& event
);
127 void InjectMouseEvent(const protocol::MouseEvent
& event
);
128 void InjectTouchEvent(const protocol::TouchEvent
& event
);
129 void StartInputInjector(scoped_ptr
<protocol::ClipboardStub
> client_clipboard
);
131 // API used to implement the SessionController interface.
132 void SetScreenResolution(const ScreenResolution
& resolution
);
135 friend class base::DeleteHelper
<DesktopSessionProxy
>;
136 friend struct DesktopSessionProxyTraits
;
138 class IpcSharedBufferCore
;
139 class IpcSharedBuffer
;
140 typedef std::map
<int, scoped_refptr
<IpcSharedBufferCore
> > SharedBuffers
;
142 ~DesktopSessionProxy() override
;
144 // Returns a shared buffer from the list of known buffers.
145 scoped_refptr
<IpcSharedBufferCore
> GetSharedBufferCore(int id
);
147 // Handles AudioPacket notification from the desktop session agent.
148 void OnAudioPacket(const std::string
& serialized_packet
);
150 // Registers a new shared buffer created by the desktop process.
151 void OnCreateSharedBuffer(int id
,
152 IPC::PlatformFileForTransit handle
,
155 // Drops a cached reference to the shared buffer.
156 void OnReleaseSharedBuffer(int id
);
158 // Handles CaptureCompleted notification from the desktop session agent.
159 void OnCaptureCompleted(const SerializedDesktopFrame
& serialized_frame
);
161 // Handles MouseCursor notification from the desktop session agent.
162 void OnMouseCursor(const webrtc::MouseCursor
& mouse_cursor
);
164 // Handles InjectClipboardEvent request from the desktop integration process.
165 void OnInjectClipboardEvent(const std::string
& serialized_event
);
167 // Posts OnCaptureCompleted() to |video_capturer_| on the video thread,
169 void PostCaptureCompleted(scoped_ptr
<webrtc::DesktopFrame
> frame
);
171 // Posts OnMouseCursor() to |mouse_cursor_monitor_| on the video thread,
172 // passing |mouse_cursor|.
173 void PostMouseCursor(scoped_ptr
<webrtc::MouseCursor
> mouse_cursor
);
175 // Sends a message to the desktop session agent. The message is silently
176 // deleted if the channel is broken.
177 void SendToDesktop(IPC::Message
* message
);
180 // - |audio_capturer_| is called back on |audio_capture_task_runner_|.
181 // - public methods of this class (with some exceptions) are called on
182 // |caller_task_runner_|.
183 // - background I/O is served on |io_task_runner_|.
184 // - |video_capturer_| is called back on |video_capture_task_runner_|.
185 scoped_refptr
<base::SingleThreadTaskRunner
> audio_capture_task_runner_
;
186 scoped_refptr
<base::SingleThreadTaskRunner
> caller_task_runner_
;
187 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
188 scoped_refptr
<base::SingleThreadTaskRunner
> video_capture_task_runner_
;
190 // Points to the audio capturer receiving captured audio packets.
191 base::WeakPtr
<IpcAudioCapturer
> audio_capturer_
;
193 // Points to the client stub passed to StartInputInjector().
194 scoped_ptr
<protocol::ClipboardStub
> client_clipboard_
;
196 // Used to disconnect the client session.
197 base::WeakPtr
<ClientSessionControl
> client_session_control_
;
199 // Used to create a desktop session and receive notifications every time
200 // the desktop process is replaced.
201 base::WeakPtr
<DesktopSessionConnector
> desktop_session_connector_
;
203 // Points to the video capturer receiving captured video frames.
204 base::WeakPtr
<IpcVideoFrameCapturer
> video_capturer_
;
206 // Points to the mouse cursor monitor receiving mouse cursor changes.
207 base::WeakPtr
<IpcMouseCursorMonitor
> mouse_cursor_monitor_
;
209 // IPC channel to the desktop session agent.
210 scoped_ptr
<IPC::ChannelProxy
> desktop_channel_
;
212 // Handle of the desktop process.
213 base::Process desktop_process_
;
215 int pending_capture_frame_requests_
;
217 // Shared memory buffers by Id. Each buffer is owned by the corresponding
219 SharedBuffers shared_buffers_
;
221 // Keeps the desired screen resolution so it can be passed to a newly attached
222 // desktop session agent.
223 ScreenResolution screen_resolution_
;
225 // True if |this| has been connected to the desktop session.
226 bool is_desktop_session_connected_
;
228 bool virtual_terminal_
;
230 DISALLOW_COPY_AND_ASSIGN(DesktopSessionProxy
);
233 // Destroys |DesktopSessionProxy| instances on the caller's thread.
234 struct DesktopSessionProxyTraits
{
235 static void Destruct(const DesktopSessionProxy
* desktop_session_proxy
);
238 } // namespace remoting
240 #endif // REMOTING_HOST_DESKTOP_SESSION_PROXY_H_