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 StartInputInjector(scoped_ptr
<protocol::ClipboardStub
> client_clipboard
);
130 // API used to implement the SessionController interface.
131 void SetScreenResolution(const ScreenResolution
& resolution
);
134 friend class base::DeleteHelper
<DesktopSessionProxy
>;
135 friend struct DesktopSessionProxyTraits
;
137 class IpcSharedBufferCore
;
138 class IpcSharedBuffer
;
139 typedef std::map
<int, scoped_refptr
<IpcSharedBufferCore
> > SharedBuffers
;
141 ~DesktopSessionProxy() override
;
143 // Returns a shared buffer from the list of known buffers.
144 scoped_refptr
<IpcSharedBufferCore
> GetSharedBufferCore(int id
);
146 // Handles AudioPacket notification from the desktop session agent.
147 void OnAudioPacket(const std::string
& serialized_packet
);
149 // Registers a new shared buffer created by the desktop process.
150 void OnCreateSharedBuffer(int id
,
151 IPC::PlatformFileForTransit handle
,
154 // Drops a cached reference to the shared buffer.
155 void OnReleaseSharedBuffer(int id
);
157 // Handles CaptureCompleted notification from the desktop session agent.
158 void OnCaptureCompleted(const SerializedDesktopFrame
& serialized_frame
);
160 // Handles MouseCursor notification from the desktop session agent.
161 void OnMouseCursor(const webrtc::MouseCursor
& mouse_cursor
);
163 // Handles InjectClipboardEvent request from the desktop integration process.
164 void OnInjectClipboardEvent(const std::string
& serialized_event
);
166 // Posts OnCaptureCompleted() to |video_capturer_| on the video thread,
168 void PostCaptureCompleted(scoped_ptr
<webrtc::DesktopFrame
> frame
);
170 // Posts OnMouseCursor() to |mouse_cursor_monitor_| on the video thread,
171 // passing |mouse_cursor|.
172 void PostMouseCursor(scoped_ptr
<webrtc::MouseCursor
> mouse_cursor
);
174 // Sends a message to the desktop session agent. The message is silently
175 // deleted if the channel is broken.
176 void SendToDesktop(IPC::Message
* message
);
179 // - |audio_capturer_| is called back on |audio_capture_task_runner_|.
180 // - public methods of this class (with some exceptions) are called on
181 // |caller_task_runner_|.
182 // - background I/O is served on |io_task_runner_|.
183 // - |video_capturer_| is called back on |video_capture_task_runner_|.
184 scoped_refptr
<base::SingleThreadTaskRunner
> audio_capture_task_runner_
;
185 scoped_refptr
<base::SingleThreadTaskRunner
> caller_task_runner_
;
186 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
187 scoped_refptr
<base::SingleThreadTaskRunner
> video_capture_task_runner_
;
189 // Points to the audio capturer receiving captured audio packets.
190 base::WeakPtr
<IpcAudioCapturer
> audio_capturer_
;
192 // Points to the client stub passed to StartInputInjector().
193 scoped_ptr
<protocol::ClipboardStub
> client_clipboard_
;
195 // Used to disconnect the client session.
196 base::WeakPtr
<ClientSessionControl
> client_session_control_
;
198 // Used to create a desktop session and receive notifications every time
199 // the desktop process is replaced.
200 base::WeakPtr
<DesktopSessionConnector
> desktop_session_connector_
;
202 // Points to the video capturer receiving captured video frames.
203 base::WeakPtr
<IpcVideoFrameCapturer
> video_capturer_
;
205 // Points to the mouse cursor monitor receiving mouse cursor changes.
206 base::WeakPtr
<IpcMouseCursorMonitor
> mouse_cursor_monitor_
;
208 // IPC channel to the desktop session agent.
209 scoped_ptr
<IPC::ChannelProxy
> desktop_channel_
;
211 // Handle of the desktop process.
212 base::Process desktop_process_
;
214 int pending_capture_frame_requests_
;
216 // Shared memory buffers by Id. Each buffer is owned by the corresponding
218 SharedBuffers shared_buffers_
;
220 // Keeps the desired screen resolution so it can be passed to a newly attached
221 // desktop session agent.
222 ScreenResolution screen_resolution_
;
224 // True if |this| has been connected to the desktop session.
225 bool is_desktop_session_connected_
;
227 bool virtual_terminal_
;
229 DISALLOW_COPY_AND_ASSIGN(DesktopSessionProxy
);
232 // Destroys |DesktopSessionProxy| instances on the caller's thread.
233 struct DesktopSessionProxyTraits
{
234 static void Destruct(const DesktopSessionProxy
* desktop_session_proxy
);
237 } // namespace remoting
239 #endif // REMOTING_HOST_DESKTOP_SESSION_PROXY_H_