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(
110 const webrtc::DesktopVector
& position
) OVERRIDE
;
111 virtual void SetDisableInputs(bool disable_inputs
) OVERRIDE
;
113 // Handles StartSessionAgent request from the client.
114 void OnStartSessionAgent(const std::string
& authenticated_jid
,
115 const ScreenResolution
& resolution
,
116 bool virtual_terminal
);
118 // Handles CaptureFrame requests from the client.
119 void OnCaptureFrame();
121 // Handles SharedBufferCreated notification from the client.
122 void OnSharedBufferCreated(int id
);
124 // Handles event executor requests from the client.
125 void OnInjectClipboardEvent(const std::string
& serialized_event
);
126 void OnInjectKeyEvent(const std::string
& serialized_event
);
127 void OnInjectTextEvent(const std::string
& serialized_event
);
128 void OnInjectMouseEvent(const std::string
& serialized_event
);
130 // Handles ChromotingNetworkDesktopMsg_SetScreenResolution request from
132 void SetScreenResolution(const ScreenResolution
& resolution
);
134 // Sends a message to the network process.
135 void SendToNetwork(IPC::Message
* message
);
137 // Posted to |audio_capture_task_runner_| to start the audio capturer.
138 void StartAudioCapturer();
140 // Posted to |audio_capture_task_runner_| to stop the audio capturer.
141 void StopAudioCapturer();
143 // Posted to |video_capture_task_runner_| to start the video capturer.
144 void StartVideoCapturer();
146 // Posted to |video_capture_task_runner_| to stop the video capturer.
147 void StopVideoCapturer();
151 friend class SharedBuffer
;
153 // Called by SharedBuffer when it's destroyed.
154 void OnSharedBufferDeleted(int id
);
156 // Task runner dedicated to running methods of |audio_capturer_|.
157 scoped_refptr
<AutoThreadTaskRunner
> audio_capture_task_runner_
;
159 // Task runner on which public methods of this class should be called.
160 scoped_refptr
<AutoThreadTaskRunner
> caller_task_runner_
;
162 // Task runner on which keyboard/mouse input is injected.
163 scoped_refptr
<AutoThreadTaskRunner
> input_task_runner_
;
165 // Task runner used by the IPC channel.
166 scoped_refptr
<AutoThreadTaskRunner
> io_task_runner_
;
168 // Task runner dedicated to running methods of |video_capturer_|.
169 scoped_refptr
<AutoThreadTaskRunner
> video_capture_task_runner_
;
171 // Captures audio output.
172 scoped_ptr
<AudioCapturer
> audio_capturer_
;
174 std::string client_jid_
;
176 // Used to disable callbacks to |this|.
177 base::WeakPtrFactory
<ClientSessionControl
> control_factory_
;
179 base::WeakPtr
<Delegate
> delegate_
;
181 // The DesktopEnvironment instance used by this agent.
182 scoped_ptr
<DesktopEnvironment
> desktop_environment_
;
184 // Executes keyboard, mouse and clipboard events.
185 scoped_ptr
<InputInjector
> input_injector_
;
187 // Tracker used to release pressed keys and buttons when disconnecting.
188 scoped_ptr
<protocol::InputEventTracker
> input_tracker_
;
190 // Filter used to disable remote inputs during local input activity.
191 scoped_ptr
<RemoteInputFilter
> remote_input_filter_
;
193 // Used to apply client-requested changes in screen resolution.
194 scoped_ptr
<ScreenControls
> screen_controls_
;
196 // IPC channel connecting the desktop process with the network process.
197 scoped_ptr
<IPC::ChannelProxy
> network_channel_
;
199 // The client end of the network-to-desktop pipe. It is kept alive until
200 // the network process connects to the pipe.
201 base::File desktop_pipe_
;
203 // Size of the most recent captured video frame.
204 webrtc::DesktopSize current_size_
;
206 // Next shared buffer ID to be used.
207 int next_shared_buffer_id_
;
209 // The number of currently allocated shared buffers.
212 // True if the desktop session agent has been started.
215 // Captures the screen.
216 scoped_ptr
<webrtc::ScreenCapturer
> video_capturer_
;
218 // Keep reference to the last frame sent to make sure shared buffer is alive
219 // before it's received.
220 scoped_ptr
<webrtc::DesktopFrame
> last_frame_
;
222 DISALLOW_COPY_AND_ASSIGN(DesktopSessionAgent
);
225 } // namespace remoting
227 #endif // REMOTING_HOST_DESKTOP_SESSION_AGENT_H_