Home | History | Annotate | Download | only in host
      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 OnInjectTextEvent(const std::string& serialized_event);
    128   void OnInjectMouseEvent(const std::string& serialized_event);
    129 
    130   // Handles ChromotingNetworkDesktopMsg_SetScreenResolution request from
    131   // the client.
    132   void SetScreenResolution(const ScreenResolution& resolution);
    133 
    134   // Sends a message to the network process.
    135   void SendToNetwork(IPC::Message* message);
    136 
    137   // Posted to |audio_capture_task_runner_| to start the audio capturer.
    138   void StartAudioCapturer();
    139 
    140   // Posted to |audio_capture_task_runner_| to stop the audio capturer.
    141   void StopAudioCapturer();
    142 
    143   // Posted to |video_capture_task_runner_| to start the video capturer.
    144   void StartVideoCapturer();
    145 
    146   // Posted to |video_capture_task_runner_| to stop the video capturer.
    147   void StopVideoCapturer();
    148 
    149  private:
    150   class SharedBuffer;
    151   friend class SharedBuffer;
    152 
    153   // Called by SharedBuffer when it's destroyed.
    154   void OnSharedBufferDeleted(int id);
    155 
    156   // Task runner dedicated to running methods of |audio_capturer_|.
    157   scoped_refptr<AutoThreadTaskRunner> audio_capture_task_runner_;
    158 
    159   // Task runner on which public methods of this class should be called.
    160   scoped_refptr<AutoThreadTaskRunner> caller_task_runner_;
    161 
    162   // Task runner on which keyboard/mouse input is injected.
    163   scoped_refptr<AutoThreadTaskRunner> input_task_runner_;
    164 
    165   // Task runner used by the IPC channel.
    166   scoped_refptr<AutoThreadTaskRunner> io_task_runner_;
    167 
    168   // Task runner dedicated to running methods of |video_capturer_|.
    169   scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner_;
    170 
    171   // Captures audio output.
    172   scoped_ptr<AudioCapturer> audio_capturer_;
    173 
    174   std::string client_jid_;
    175 
    176   // Used to disable callbacks to |this|.
    177   base::WeakPtrFactory<ClientSessionControl> control_factory_;
    178 
    179   base::WeakPtr<Delegate> delegate_;
    180 
    181   // The DesktopEnvironment instance used by this agent.
    182   scoped_ptr<DesktopEnvironment> desktop_environment_;
    183 
    184   // Executes keyboard, mouse and clipboard events.
    185   scoped_ptr<InputInjector> input_injector_;
    186 
    187   // Tracker used to release pressed keys and buttons when disconnecting.
    188   scoped_ptr<protocol::InputEventTracker> input_tracker_;
    189 
    190   // Filter used to disable remote inputs during local input activity.
    191   scoped_ptr<RemoteInputFilter> remote_input_filter_;
    192 
    193   // Used to apply client-requested changes in screen resolution.
    194   scoped_ptr<ScreenControls> screen_controls_;
    195 
    196   // IPC channel connecting the desktop process with the network process.
    197   scoped_ptr<IPC::ChannelProxy> network_channel_;
    198 
    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_;
    202 
    203   // Size of the most recent captured video frame.
    204   webrtc::DesktopSize current_size_;
    205 
    206   // Next shared buffer ID to be used.
    207   int next_shared_buffer_id_;
    208 
    209   // The number of currently allocated shared buffers.
    210   int shared_buffers_;
    211 
    212   // True if the desktop session agent has been started.
    213   bool started_;
    214 
    215   // Captures the screen.
    216   scoped_ptr<webrtc::ScreenCapturer> video_capturer_;
    217 
    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_;
    221 
    222   DISALLOW_COPY_AND_ASSIGN(DesktopSessionAgent);
    223 };
    224 
    225 }  // namespace remoting
    226 
    227 #endif  // REMOTING_HOST_DESKTOP_SESSION_AGENT_H_
    228