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_AGENT_H_
6 #define REMOTING_HOST_DESKTOP_SESSION_AGENT_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "ipc/ipc_listener.h"
17 #include "ipc/ipc_platform_file.h"
18 #include "remoting/host/client_session_control.h"
19 #include "remoting/protocol/clipboard_stub.h"
20 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
21 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
22 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
33 class AutoThreadTaskRunner
;
34 class DesktopEnvironment
;
35 class DesktopEnvironmentFactory
;
37 class RemoteInputFilter
;
39 class ScreenResolution
;
42 class InputEventTracker
;
43 } // namespace protocol
45 // Provides screen/audio capturing and input injection services for
46 // the network process.
47 class DesktopSessionAgent
48 : public base::RefCountedThreadSafe
<DesktopSessionAgent
>,
50 public webrtc::DesktopCapturer::Callback
,
51 public webrtc::MouseCursorMonitor::Callback
,
52 public ClientSessionControl
{
58 // Returns an instance of desktop environment factory used.
59 virtual DesktopEnvironmentFactory
& desktop_environment_factory() = 0;
61 // Notifies the delegate that the network-to-desktop channel has been
63 virtual void OnNetworkProcessDisconnected() = 0;
67 scoped_refptr
<AutoThreadTaskRunner
> audio_capture_task_runner
,
68 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner
,
69 scoped_refptr
<AutoThreadTaskRunner
> input_task_runner
,
70 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner
,
71 scoped_refptr
<AutoThreadTaskRunner
> video_capture_task_runner
);
73 // IPC::Listener implementation.
74 bool OnMessageReceived(const IPC::Message
& message
) override
;
75 void OnChannelConnected(int32 peer_pid
) override
;
76 void OnChannelError() override
;
78 // webrtc::DesktopCapturer::Callback implementation.
79 webrtc::SharedMemory
* CreateSharedMemory(size_t size
) override
;
80 void OnCaptureCompleted(webrtc::DesktopFrame
* frame
) override
;
82 // webrtc::MouseCursorMonitor::Callback implementation.
83 void OnMouseCursor(webrtc::MouseCursor
* cursor
) override
;
84 void OnMouseCursorPosition(webrtc::MouseCursorMonitor::CursorState state
,
85 const webrtc::DesktopVector
& position
) override
;
87 // Forwards a local clipboard event though the IPC channel to the network
89 void InjectClipboardEvent(const protocol::ClipboardEvent
& event
);
91 // Forwards an audio packet though the IPC channel to the network process.
92 void ProcessAudioPacket(scoped_ptr
<AudioPacket
> packet
);
94 // Creates desktop integration components and a connected IPC channel to be
95 // used to access them. The client end of the channel is returned in
96 // the variable pointed by |desktop_pipe_out|.
97 bool Start(const base::WeakPtr
<Delegate
>& delegate
,
98 IPC::PlatformFileForTransit
* desktop_pipe_out
);
100 // Stops the agent asynchronously.
104 friend class base::RefCountedThreadSafe
<DesktopSessionAgent
>;
106 ~DesktopSessionAgent() override
;
108 // ClientSessionControl interface.
109 const std::string
& client_jid() const override
;
110 void DisconnectSession() override
;
111 void OnLocalMouseMoved(const webrtc::DesktopVector
& position
) override
;
112 void SetDisableInputs(bool disable_inputs
) override
;
113 void ResetVideoPipeline() override
;
115 // Handles StartSessionAgent request from the client.
116 void OnStartSessionAgent(const std::string
& authenticated_jid
,
117 const ScreenResolution
& resolution
,
118 bool virtual_terminal
);
120 // Handles CaptureFrame requests from the client.
121 void OnCaptureFrame();
123 // Handles SharedBufferCreated notification from the client.
124 void OnSharedBufferCreated(int id
);
126 // Handles event executor requests from the client.
127 void OnInjectClipboardEvent(const std::string
& serialized_event
);
128 void OnInjectKeyEvent(const std::string
& serialized_event
);
129 void OnInjectTextEvent(const std::string
& serialized_event
);
130 void OnInjectMouseEvent(const std::string
& serialized_event
);
132 // Handles ChromotingNetworkDesktopMsg_SetScreenResolution request from
134 void SetScreenResolution(const ScreenResolution
& resolution
);
136 // Sends a message to the network process.
137 void SendToNetwork(IPC::Message
* message
);
139 // Posted to |audio_capture_task_runner_| to start the audio capturer.
140 void StartAudioCapturer();
142 // Posted to |audio_capture_task_runner_| to stop the audio capturer.
143 void StopAudioCapturer();
145 // Posted to |video_capture_task_runner_| to start the video capturer and the
146 // mouse cursor monitor.
147 void StartVideoCapturerAndMouseMonitor();
149 // Posted to |video_capture_task_runner_| to stop the video capturer and the
150 // mouse cursor monitor.
151 void StopVideoCapturerAndMouseMonitor();
155 friend class SharedBuffer
;
157 // Called by SharedBuffer when it's destroyed.
158 void OnSharedBufferDeleted(int id
);
160 // Task runner dedicated to running methods of |audio_capturer_|.
161 scoped_refptr
<AutoThreadTaskRunner
> audio_capture_task_runner_
;
163 // Task runner on which public methods of this class should be called.
164 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner_
;
166 // Task runner on which keyboard/mouse input is injected.
167 scoped_refptr
<AutoThreadTaskRunner
> input_task_runner_
;
169 // Task runner used by the IPC channel.
170 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner_
;
172 // Task runner dedicated to running methods of |video_capturer_|.
173 scoped_refptr
<AutoThreadTaskRunner
> video_capture_task_runner_
;
175 // Captures audio output.
176 scoped_ptr
<AudioCapturer
> audio_capturer_
;
178 std::string client_jid_
;
180 base::WeakPtr
<Delegate
> delegate_
;
182 // The DesktopEnvironment instance used by this agent.
183 scoped_ptr
<DesktopEnvironment
> desktop_environment_
;
185 // Executes keyboard, mouse and clipboard events.
186 scoped_ptr
<InputInjector
> input_injector_
;
188 // Tracker used to release pressed keys and buttons when disconnecting.
189 scoped_ptr
<protocol::InputEventTracker
> input_tracker_
;
191 // Filter used to disable remote inputs during local input activity.
192 scoped_ptr
<RemoteInputFilter
> remote_input_filter_
;
194 // Used to apply client-requested changes in screen resolution.
195 scoped_ptr
<ScreenControls
> screen_controls_
;
197 // IPC channel connecting the desktop process with the network process.
198 scoped_ptr
<IPC::ChannelProxy
> network_channel_
;
200 // The client end of the network-to-desktop pipe. It is kept alive until
201 // the network process connects to the pipe.
202 base::File desktop_pipe_
;
204 // Size of the most recent captured video frame.
205 webrtc::DesktopSize current_size_
;
207 // Next shared buffer ID to be used.
208 int next_shared_buffer_id_
;
210 // The number of currently allocated shared buffers.
213 // True if the desktop session agent has been started.
216 // Captures the screen.
217 scoped_ptr
<webrtc::DesktopCapturer
> video_capturer_
;
219 // Captures mouse shapes.
220 scoped_ptr
<webrtc::MouseCursorMonitor
> mouse_cursor_monitor_
;
222 // Keep reference to the last frame sent to make sure shared buffer is alive
223 // before it's received.
224 scoped_ptr
<webrtc::DesktopFrame
> last_frame_
;
226 // Used to disable callbacks to |this|.
227 base::WeakPtrFactory
<DesktopSessionAgent
> weak_factory_
;
229 DISALLOW_COPY_AND_ASSIGN(DesktopSessionAgent
);
232 } // namespace remoting
234 #endif // REMOTING_HOST_DESKTOP_SESSION_AGENT_H_