• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef REMOTING_HOST_DESKTOP_SESSION_AGENT_H_
6 #define REMOTING_HOST_DESKTOP_SESSION_AGENT_H_
7 
8 #include <map>
9 
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"
22 
23 namespace IPC {
24 class ChannelProxy;
25 class Message;
26 }  // namespace IPC
27 
28 namespace remoting {
29 
30 class AudioCapturer;
31 class AudioPacket;
32 class AutoThreadTaskRunner;
33 class DesktopEnvironment;
34 class DesktopEnvironmentFactory;
35 class InputInjector;
36 class RemoteInputFilter;
37 class ScreenControls;
38 class ScreenResolution;
39 
40 namespace protocol {
41 class InputEventTracker;
42 }  // namespace protocol
43 
44 // Provides screen/audio capturing and input injection services for
45 // the network process.
46 class DesktopSessionAgent
47     : public base::RefCountedThreadSafe<DesktopSessionAgent>,
48       public IPC::Listener,
49       public webrtc::DesktopCapturer::Callback,
50       public webrtc::ScreenCapturer::MouseShapeObserver,
51       public ClientSessionControl {
52  public:
53   class Delegate {
54    public:
55     virtual ~Delegate();
56 
57     // Returns an instance of desktop environment factory used.
58     virtual DesktopEnvironmentFactory& desktop_environment_factory() = 0;
59 
60     // Notifies the delegate that the network-to-desktop channel has been
61     // disconnected.
62     virtual void OnNetworkProcessDisconnected() = 0;
63   };
64 
65   DesktopSessionAgent(
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);
71 
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;
76 
77   // webrtc::DesktopCapturer::Callback implementation.
78   virtual webrtc::SharedMemory* CreateSharedMemory(size_t size) OVERRIDE;
79   virtual void OnCaptureCompleted(webrtc::DesktopFrame* frame) OVERRIDE;
80 
81   // webrtc::ScreenCapturer::MouseShapeObserver implementation.
82   virtual void OnCursorShapeChanged(
83       webrtc::MouseCursorShape* cursor_shape) OVERRIDE;
84 
85   // Forwards a local clipboard event though the IPC channel to the network
86   // process.
87   void InjectClipboardEvent(const protocol::ClipboardEvent& event);
88 
89   // Forwards an audio packet though the IPC channel to the network process.
90   void ProcessAudioPacket(scoped_ptr<AudioPacket> packet);
91 
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);
97 
98   // Stops the agent asynchronously.
99   void Stop();
100 
101  protected:
102   friend class base::RefCountedThreadSafe<DesktopSessionAgent>;
103 
104   virtual ~DesktopSessionAgent();
105 
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;
112 
113   // Handles StartSessionAgent request from the client.
114   void OnStartSessionAgent(const std::string& authenticated_jid,
115                            const ScreenResolution& resolution,
116                            bool virtual_terminal);
117 
118   // Handles CaptureFrame requests from the client.
119   void OnCaptureFrame();
120 
121   // Handles SharedBufferCreated notification from the client.
122   void OnSharedBufferCreated(int id);
123 
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 OnInjectMouseEvent(const std::string& serialized_event);
128 
129   // Handles ChromotingNetworkDesktopMsg_SetScreenResolution request from
130   // the client.
131   void SetScreenResolution(const ScreenResolution& resolution);
132 
133   // Sends a message to the network process.
134   void SendToNetwork(IPC::Message* message);
135 
136   // Posted to |audio_capture_task_runner_| to start the audio capturer.
137   void StartAudioCapturer();
138 
139   // Posted to |audio_capture_task_runner_| to stop the audio capturer.
140   void StopAudioCapturer();
141 
142   // Posted to |video_capture_task_runner_| to start the video capturer.
143   void StartVideoCapturer();
144 
145   // Posted to |video_capture_task_runner_| to stop the video capturer.
146   void StopVideoCapturer();
147 
148  private:
149   class SharedBuffer;
150   friend class SharedBuffer;
151 
152   // Called by SharedBuffer when it's destroyed.
153   void OnSharedBufferDeleted(int id);
154 
155   // Closes |desktop_pipe_| if it is open.
156   void CloseDesktopPipeHandle();
157 
158   // Task runner dedicated to running methods of |audio_capturer_|.
159   scoped_refptr<AutoThreadTaskRunner> audio_capture_task_runner_;
160 
161   // Task runner on which public methods of this class should be called.
162   scoped_refptr<AutoThreadTaskRunner> caller_task_runner_;
163 
164   // Task runner on which keyboard/mouse input is injected.
165   scoped_refptr<AutoThreadTaskRunner> input_task_runner_;
166 
167   // Task runner used by the IPC channel.
168   scoped_refptr<AutoThreadTaskRunner> io_task_runner_;
169 
170   // Task runner dedicated to running methods of |video_capturer_|.
171   scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner_;
172 
173   // Captures audio output.
174   scoped_ptr<AudioCapturer> audio_capturer_;
175 
176   std::string client_jid_;
177 
178   // Used to disable callbacks to |this|.
179   base::WeakPtrFactory<ClientSessionControl> control_factory_;
180 
181   base::WeakPtr<Delegate> delegate_;
182 
183   // The DesktopEnvironment instance used by this agent.
184   scoped_ptr<DesktopEnvironment> desktop_environment_;
185 
186   // Executes keyboard, mouse and clipboard events.
187   scoped_ptr<InputInjector> input_injector_;
188 
189   // Tracker used to release pressed keys and buttons when disconnecting.
190   scoped_ptr<protocol::InputEventTracker> input_tracker_;
191 
192   // Filter used to disable remote inputs during local input activity.
193   scoped_ptr<RemoteInputFilter> remote_input_filter_;
194 
195   // Used to apply client-requested changes in screen resolution.
196   scoped_ptr<ScreenControls> screen_controls_;
197 
198   // IPC channel connecting the desktop process with the network process.
199   scoped_ptr<IPC::ChannelProxy> network_channel_;
200 
201   // The client end of the network-to-desktop pipe. It is kept alive until
202   // the network process connects to the pipe.
203   IPC::PlatformFileForTransit desktop_pipe_;
204 
205   // Size of the most recent captured video frame.
206   webrtc::DesktopSize current_size_;
207 
208   // Next shared buffer ID to be used.
209   int next_shared_buffer_id_;
210 
211   // The number of currently allocated shared buffers.
212   int shared_buffers_;
213 
214   // True if the desktop session agent has been started.
215   bool started_;
216 
217   // Captures the screen.
218   scoped_ptr<webrtc::ScreenCapturer> video_capturer_;
219 
220   // Keep reference to the last frame sent to make sure shared buffer is alive
221   // before it's received.
222   scoped_ptr<webrtc::DesktopFrame> last_frame_;
223 
224   DISALLOW_COPY_AND_ASSIGN(DesktopSessionAgent);
225 };
226 
227 }  // namespace remoting
228 
229 #endif  // REMOTING_HOST_DESKTOP_SESSION_AGENT_H_
230