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
);
131 void OnInjectTouchEvent(const std::string
& serialized_event
);
133 // Handles ChromotingNetworkDesktopMsg_SetScreenResolution request from
135 void SetScreenResolution(const ScreenResolution
& resolution
);
137 // Sends a message to the network process.
138 void SendToNetwork(IPC::Message
* message
);
140 // Posted to |audio_capture_task_runner_| to start the audio capturer.
141 void StartAudioCapturer();
143 // Posted to |audio_capture_task_runner_| to stop the audio capturer.
144 void StopAudioCapturer();
146 // Posted to |video_capture_task_runner_| to start the video capturer and the
147 // mouse cursor monitor.
148 void StartVideoCapturerAndMouseMonitor();
150 // Posted to |video_capture_task_runner_| to stop the video capturer and the
151 // mouse cursor monitor.
152 void StopVideoCapturerAndMouseMonitor();
156 friend class SharedBuffer
;
158 // Called by SharedBuffer when it's destroyed.
159 void OnSharedBufferDeleted(int id
);
161 // Task runner dedicated to running methods of |audio_capturer_|.
162 scoped_refptr
<AutoThreadTaskRunner
> audio_capture_task_runner_
;
164 // Task runner on which public methods of this class should be called.
165 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner_
;
167 // Task runner on which keyboard/mouse input is injected.
168 scoped_refptr
<AutoThreadTaskRunner
> input_task_runner_
;
170 // Task runner used by the IPC channel.
171 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner_
;
173 // Task runner dedicated to running methods of |video_capturer_|.
174 scoped_refptr
<AutoThreadTaskRunner
> video_capture_task_runner_
;
176 // Captures audio output.
177 scoped_ptr
<AudioCapturer
> audio_capturer_
;
179 std::string client_jid_
;
181 base::WeakPtr
<Delegate
> delegate_
;
183 // The DesktopEnvironment instance used by this agent.
184 scoped_ptr
<DesktopEnvironment
> desktop_environment_
;
186 // Executes keyboard, mouse and clipboard events.
187 scoped_ptr
<InputInjector
> input_injector_
;
189 // Tracker used to release pressed keys and buttons when disconnecting.
190 scoped_ptr
<protocol::InputEventTracker
> input_tracker_
;
192 // Filter used to disable remote inputs during local input activity.
193 scoped_ptr
<RemoteInputFilter
> remote_input_filter_
;
195 // Used to apply client-requested changes in screen resolution.
196 scoped_ptr
<ScreenControls
> screen_controls_
;
198 // IPC channel connecting the desktop process with the network process.
199 scoped_ptr
<IPC::ChannelProxy
> network_channel_
;
201 // The client end of the network-to-desktop pipe. It is kept alive until
202 // the network process connects to the pipe.
203 base::File desktop_pipe_
;
205 // Size of the most recent captured video frame.
206 webrtc::DesktopSize current_size_
;
208 // Next shared buffer ID to be used.
209 int next_shared_buffer_id_
;
211 // The number of currently allocated shared buffers.
214 // True if the desktop session agent has been started.
217 // Captures the screen.
218 scoped_ptr
<webrtc::DesktopCapturer
> video_capturer_
;
220 // Captures mouse shapes.
221 scoped_ptr
<webrtc::MouseCursorMonitor
> mouse_cursor_monitor_
;
223 // Keep reference to the last frame sent to make sure shared buffer is alive
224 // before it's received.
225 scoped_ptr
<webrtc::DesktopFrame
> last_frame_
;
227 // Used to disable callbacks to |this|.
228 base::WeakPtrFactory
<DesktopSessionAgent
> weak_factory_
;
230 DISALLOW_COPY_AND_ASSIGN(DesktopSessionAgent
);
233 } // namespace remoting
235 #endif // REMOTING_HOST_DESKTOP_SESSION_AGENT_H_