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 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
75 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
76 virtual void OnChannelError() OVERRIDE;
78 // webrtc::DesktopCapturer::Callback implementation.
79 virtual webrtc::SharedMemory* CreateSharedMemory(size_t size) OVERRIDE;
80 virtual void OnCaptureCompleted(webrtc::DesktopFrame* frame) OVERRIDE;
82 // webrtc::MouseCursorMonitor::Callback implementation.
83 virtual void OnMouseCursor(webrtc::MouseCursor* cursor) OVERRIDE;
84 virtual void OnMouseCursorPosition(
85 webrtc::MouseCursorMonitor::CursorState state,
86 const webrtc::DesktopVector& position) OVERRIDE;
88 // Forwards a local clipboard event though the IPC channel to the network
90 void InjectClipboardEvent(const protocol::ClipboardEvent& event);
92 // Forwards an audio packet though the IPC channel to the network process.
93 void ProcessAudioPacket(scoped_ptr<AudioPacket> packet);
95 // Creates desktop integration components and a connected IPC channel to be
96 // used to access them. The client end of the channel is returned in
97 // the variable pointed by |desktop_pipe_out|.
98 bool Start(const base::WeakPtr<Delegate>& delegate,
99 IPC::PlatformFileForTransit* desktop_pipe_out);
101 // Stops the agent asynchronously.
105 friend class base::RefCountedThreadSafe<DesktopSessionAgent>;
107 virtual ~DesktopSessionAgent();
109 // ClientSessionControl interface.
110 virtual const std::string& client_jid() const OVERRIDE;
111 virtual void DisconnectSession() OVERRIDE;
112 virtual void OnLocalMouseMoved(
113 const webrtc::DesktopVector& position) OVERRIDE;
114 virtual void SetDisableInputs(bool disable_inputs) OVERRIDE;
115 virtual void ResetVideoPipeline() OVERRIDE;
117 // Handles StartSessionAgent request from the client.
118 void OnStartSessionAgent(const std::string& authenticated_jid,
119 const ScreenResolution& resolution,
120 bool virtual_terminal);
122 // Handles CaptureFrame requests from the client.
123 void OnCaptureFrame();
125 // Handles SharedBufferCreated notification from the client.
126 void OnSharedBufferCreated(int id);
128 // Handles event executor requests from the client.
129 void OnInjectClipboardEvent(const std::string& serialized_event);
130 void OnInjectKeyEvent(const std::string& serialized_event);
131 void OnInjectTextEvent(const std::string& serialized_event);
132 void OnInjectMouseEvent(const std::string& serialized_event);
134 // Handles ChromotingNetworkDesktopMsg_SetScreenResolution request from
136 void SetScreenResolution(const ScreenResolution& resolution);
138 // Sends a message to the network process.
139 void SendToNetwork(IPC::Message* message);
141 // Posted to |audio_capture_task_runner_| to start the audio capturer.
142 void StartAudioCapturer();
144 // Posted to |audio_capture_task_runner_| to stop the audio capturer.
145 void StopAudioCapturer();
147 // Posted to |video_capture_task_runner_| to start the video capturer and the
148 // mouse cursor monitor.
149 void StartVideoCapturerAndMouseMonitor();
151 // Posted to |video_capture_task_runner_| to stop the video capturer and the
152 // mouse cursor monitor.
153 void StopVideoCapturerAndMouseMonitor();
157 friend class SharedBuffer;
159 // Called by SharedBuffer when it's destroyed.
160 void OnSharedBufferDeleted(int id);
162 // Task runner dedicated to running methods of |audio_capturer_|.
163 scoped_refptr<AutoThreadTaskRunner> audio_capture_task_runner_;
165 // Task runner on which public methods of this class should be called.
166 scoped_refptr<AutoThreadTaskRunner> caller_task_runner_;
168 // Task runner on which keyboard/mouse input is injected.
169 scoped_refptr<AutoThreadTaskRunner> input_task_runner_;
171 // Task runner used by the IPC channel.
172 scoped_refptr<AutoThreadTaskRunner> io_task_runner_;
174 // Task runner dedicated to running methods of |video_capturer_|.
175 scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner_;
177 // Captures audio output.
178 scoped_ptr<AudioCapturer> audio_capturer_;
180 std::string client_jid_;
182 base::WeakPtr<Delegate> delegate_;
184 // The DesktopEnvironment instance used by this agent.
185 scoped_ptr<DesktopEnvironment> desktop_environment_;
187 // Executes keyboard, mouse and clipboard events.
188 scoped_ptr<InputInjector> input_injector_;
190 // Tracker used to release pressed keys and buttons when disconnecting.
191 scoped_ptr<protocol::InputEventTracker> input_tracker_;
193 // Filter used to disable remote inputs during local input activity.
194 scoped_ptr<RemoteInputFilter> remote_input_filter_;
196 // Used to apply client-requested changes in screen resolution.
197 scoped_ptr<ScreenControls> screen_controls_;
199 // IPC channel connecting the desktop process with the network process.
200 scoped_ptr<IPC::ChannelProxy> network_channel_;
202 // The client end of the network-to-desktop pipe. It is kept alive until
203 // the network process connects to the pipe.
204 base::File desktop_pipe_;
206 // Size of the most recent captured video frame.
207 webrtc::DesktopSize current_size_;
209 // Next shared buffer ID to be used.
210 int next_shared_buffer_id_;
212 // The number of currently allocated shared buffers.
215 // True if the desktop session agent has been started.
218 // Captures the screen.
219 scoped_ptr<webrtc::DesktopCapturer> video_capturer_;
221 // Captures mouse shapes.
222 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor_;
224 // Keep reference to the last frame sent to make sure shared buffer is alive
225 // before it's received.
226 scoped_ptr<webrtc::DesktopFrame> last_frame_;
228 // Used to disable callbacks to |this|.
229 base::WeakPtrFactory<DesktopSessionAgent> weak_factory_;
231 DISALLOW_COPY_AND_ASSIGN(DesktopSessionAgent);
234 } // namespace remoting
236 #endif // REMOTING_HOST_DESKTOP_SESSION_AGENT_H_