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_geometry.h"
21 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h"
32 class AutoThreadTaskRunner;
33 class DesktopEnvironment;
34 class DesktopEnvironmentFactory;
36 class RemoteInputFilter;
38 class ScreenResolution;
41 class InputEventTracker;
42 } // namespace protocol
44 // Provides screen/audio capturing and input injection services for
45 // the network process.
46 class DesktopSessionAgent
47 : public base::RefCountedThreadSafe<DesktopSessionAgent>,
49 public webrtc::DesktopCapturer::Callback,
50 public webrtc::ScreenCapturer::MouseShapeObserver,
51 public ClientSessionControl {
57 // Returns an instance of desktop environment factory used.
58 virtual DesktopEnvironmentFactory& desktop_environment_factory() = 0;
60 // Notifies the delegate that the network-to-desktop channel has been
62 virtual void OnNetworkProcessDisconnected() = 0;
66 scoped_refptr<AutoThreadTaskRunner> audio_capture_task_runner,
67 scoped_refptr<AutoThreadTaskRunner> caller_task_runner,
68 scoped_refptr<AutoThreadTaskRunner> input_task_runner,
69 scoped_refptr<AutoThreadTaskRunner> io_task_runner,
70 scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner);
72 // IPC::Listener implementation.
73 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
74 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
75 virtual void OnChannelError() OVERRIDE;
77 // webrtc::DesktopCapturer::Callback implementation.
78 virtual webrtc::SharedMemory* CreateSharedMemory(size_t size) OVERRIDE;
79 virtual void OnCaptureCompleted(webrtc::DesktopFrame* frame) OVERRIDE;
81 // webrtc::ScreenCapturer::MouseShapeObserver implementation.
82 virtual void OnCursorShapeChanged(
83 webrtc::MouseCursorShape* cursor_shape) OVERRIDE;
85 // Forwards a local clipboard event though the IPC channel to the network
87 void InjectClipboardEvent(const protocol::ClipboardEvent& event);
89 // Forwards an audio packet though the IPC channel to the network process.
90 void ProcessAudioPacket(scoped_ptr<AudioPacket> packet);
92 // Creates desktop integration components and a connected IPC channel to be
93 // used to access them. The client end of the channel is returned in
94 // the variable pointed by |desktop_pipe_out|.
95 bool Start(const base::WeakPtr<Delegate>& delegate,
96 IPC::PlatformFileForTransit* desktop_pipe_out);
98 // Stops the agent asynchronously.
102 friend class base::RefCountedThreadSafe<DesktopSessionAgent>;
104 virtual ~DesktopSessionAgent();
106 // ClientSessionControl interface.
107 virtual const std::string& client_jid() const OVERRIDE;
108 virtual void DisconnectSession() OVERRIDE;
109 virtual void OnLocalMouseMoved(const SkIPoint& position) OVERRIDE;
110 virtual void SetDisableInputs(bool disable_inputs) OVERRIDE;
112 // Handles StartSessionAgent request from the client.
113 void OnStartSessionAgent(const std::string& authenticated_jid,
114 const ScreenResolution& resolution,
115 bool virtual_terminal);
117 // Handles CaptureFrame requests from the client.
118 void OnCaptureFrame();
120 // Handles SharedBufferCreated notification from the client.
121 void OnSharedBufferCreated(int id);
123 // Handles event executor requests from the client.
124 void OnInjectClipboardEvent(const std::string& serialized_event);
125 void OnInjectKeyEvent(const std::string& serialized_event);
126 void OnInjectMouseEvent(const std::string& serialized_event);
128 // Handles ChromotingNetworkDesktopMsg_SetScreenResolution request from
130 void SetScreenResolution(const ScreenResolution& resolution);
132 // Sends a message to the network process.
133 void SendToNetwork(IPC::Message* message);
135 // Posted to |audio_capture_task_runner_| to start the audio capturer.
136 void StartAudioCapturer();
138 // Posted to |audio_capture_task_runner_| to stop the audio capturer.
139 void StopAudioCapturer();
141 // Posted to |video_capture_task_runner_| to start the video capturer.
142 void StartVideoCapturer();
144 // Posted to |video_capture_task_runner_| to stop the video capturer.
145 void StopVideoCapturer();
149 friend class SharedBuffer;
151 // Called by SharedBuffer when it's destroyed.
152 void OnSharedBufferDeleted(int id);
154 // Closes |desktop_pipe_| if it is open.
155 void CloseDesktopPipeHandle();
157 // Task runner dedicated to running methods of |audio_capturer_|.
158 scoped_refptr<AutoThreadTaskRunner> audio_capture_task_runner_;
160 // Task runner on which public methods of this class should be called.
161 scoped_refptr<AutoThreadTaskRunner> caller_task_runner_;
163 // Task runner on which keyboard/mouse input is injected.
164 scoped_refptr<AutoThreadTaskRunner> input_task_runner_;
166 // Task runner used by the IPC channel.
167 scoped_refptr<AutoThreadTaskRunner> io_task_runner_;
169 // Task runner dedicated to running methods of |video_capturer_|.
170 scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner_;
172 // Captures audio output.
173 scoped_ptr<AudioCapturer> audio_capturer_;
175 std::string client_jid_;
177 // Used to disable callbacks to |this|.
178 base::WeakPtrFactory<ClientSessionControl> control_factory_;
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 IPC::PlatformFileForTransit 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::ScreenCapturer> video_capturer_;
219 // Keep reference to the last frame sent to make sure shared buffer is alive
220 // before it's received.
221 scoped_ptr<webrtc::DesktopFrame> last_frame_;
223 DISALLOW_COPY_AND_ASSIGN(DesktopSessionAgent);
226 } // namespace remoting
228 #endif // REMOTING_HOST_DESKTOP_SESSION_AGENT_H_