| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef REMOTING_HOST_VIDEO_FRAME_PUMP_H_ | 5 #ifndef REMOTING_HOST_VIDEO_FRAME_PUMP_H_ |
| 6 #define REMOTING_HOST_VIDEO_FRAME_PUMP_H_ | 6 #define REMOTING_HOST_VIDEO_FRAME_PUMP_H_ |
| 7 | 7 |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 11 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/threading/thread_checker.h" |
| 13 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 14 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 15 #include "remoting/codec/video_encoder.h" | 14 #include "remoting/codec/video_encoder.h" |
| 15 #include "remoting/host/capture_scheduler.h" |
| 16 #include "remoting/proto/video.pb.h" | 16 #include "remoting/proto/video.pb.h" |
| 17 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" | 17 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" |
| 18 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h" | |
| 19 | 18 |
| 20 namespace base { | 19 namespace base { |
| 21 class SingleThreadTaskRunner; | 20 class SingleThreadTaskRunner; |
| 22 } // namespace base | 21 } // namespace base |
| 23 | 22 |
| 24 namespace media { | |
| 25 class DesktopCapturer; | |
| 26 } // namespace media | |
| 27 | |
| 28 namespace remoting { | 23 namespace remoting { |
| 29 | 24 |
| 30 class CaptureScheduler; | |
| 31 | |
| 32 namespace protocol { | 25 namespace protocol { |
| 33 class CursorShapeInfo; | |
| 34 class CursorShapeStub; | |
| 35 class VideoStub; | 26 class VideoStub; |
| 36 } // namespace protocol | 27 } // namespace protocol |
| 37 | 28 |
| 38 // Class responsible for scheduling frame captures from a | 29 // Class responsible for scheduling frame captures from a screen capturer., |
| 39 // webrtc::DesktopCapturer, delivering them to a VideoEncoder to encode, and | 30 // delivering them to a VideoEncoder to encode, and |
| 40 // finally passing the encoded video packets to the specified VideoStub to send | 31 // finally passing the encoded video packets to the specified VideoStub to send |
| 41 // on the network. | 32 // on the network. |
| 42 // | 33 // |
| 43 // THREADING | 34 // THREADING |
| 44 // | 35 // |
| 45 // This class is supplied TaskRunners to use for capture, encode and network | 36 // This class is supplied TaskRunners to use for capture, encode and network |
| 46 // operations. Capture, encode and network transmission tasks are interleaved | 37 // operations. Capture, encode and network transmission tasks are interleaved |
| 47 // as illustrated below: | 38 // as illustrated below: |
| 48 // | 39 // |
| 49 // | CAPTURE ENCODE NETWORK | 40 // | CAPTURE ENCODE NETWORK |
| (...skipping 14 matching lines...) Expand all Loading... |
| 64 // | . . | 55 // | . . |
| 65 // | . . | 56 // | . . |
| 66 // | ............ | 57 // | ............ |
| 67 // | Time | 58 // | Time |
| 68 // v | 59 // v |
| 69 // | 60 // |
| 70 // VideoFramePump would ideally schedule captures so as to saturate the slowest | 61 // VideoFramePump would ideally schedule captures so as to saturate the slowest |
| 71 // of the capture, encode and network processes. However, it also needs to | 62 // of the capture, encode and network processes. However, it also needs to |
| 72 // rate-limit captures to avoid overloading the host system, either by consuming | 63 // rate-limit captures to avoid overloading the host system, either by consuming |
| 73 // too much CPU, or hogging the host's graphics subsystem. | 64 // too much CPU, or hogging the host's graphics subsystem. |
| 74 // | 65 class VideoFramePump : public webrtc::DesktopCapturer::Callback { |
| 75 // TODO(sergeyu): Rename this class to VideoFramePump. | |
| 76 class VideoFramePump : public base::RefCountedThreadSafe<VideoFramePump>, | |
| 77 public webrtc::DesktopCapturer::Callback, | |
| 78 public webrtc::MouseCursorMonitor::Callback { | |
| 79 public: | 66 public: |
| 80 // Enables timestamps for generated frames. Used for testing. | 67 // Enables timestamps for generated frames. Used for testing. |
| 81 static void EnableTimestampsForTests(); | 68 static void EnableTimestampsForTests(); |
| 82 | 69 |
| 83 // Creates a VideoFramePump running capture, encode and network tasks on the | 70 // Creates a VideoFramePump running capture, encode and network tasks on the |
| 84 // supplied TaskRunners. Video and cursor shape updates will be pumped to | 71 // supplied TaskRunners. Video will be pumped to |video_stub|, which must |
| 85 // |video_stub| and |client_stub|, which must remain valid until Stop() is | 72 // outlive the pump.. |
| 86 // called. |capturer| is used to capture frames. | |
| 87 VideoFramePump( | 73 VideoFramePump( |
| 88 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, | |
| 89 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 74 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
| 90 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | |
| 91 scoped_ptr<webrtc::DesktopCapturer> capturer, | 75 scoped_ptr<webrtc::DesktopCapturer> capturer, |
| 92 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, | |
| 93 scoped_ptr<VideoEncoder> encoder, | 76 scoped_ptr<VideoEncoder> encoder, |
| 94 protocol::CursorShapeStub* cursor_stub, | |
| 95 protocol::VideoStub* video_stub); | 77 protocol::VideoStub* video_stub); |
| 96 | 78 ~VideoFramePump() override; |
| 97 // Starts scheduling frame captures. | |
| 98 void Start(); | |
| 99 | |
| 100 // Stop scheduling frame captures. This object cannot be re-used once | |
| 101 // it has been stopped. | |
| 102 void Stop(); | |
| 103 | 79 |
| 104 // Pauses or resumes scheduling of frame captures. Pausing/resuming captures | 80 // Pauses or resumes scheduling of frame captures. Pausing/resuming captures |
| 105 // only affects capture scheduling and does not stop/start the capturer. | 81 // only affects capture scheduling and does not stop/start the capturer. |
| 106 void Pause(bool pause); | 82 void Pause(bool pause); |
| 107 | 83 |
| 108 // Updates event timestamp from the last event received from the client. This | 84 // Updates event timestamp from the last event received from the client. This |
| 109 // value is sent back to the client for roundtrip latency estimates. | 85 // value is sent back to the client for roundtrip latency estimates. |
| 110 void SetLatestEventTimestamp(int64 latest_event_timestamp); | 86 void SetLatestEventTimestamp(int64 latest_event_timestamp); |
| 111 | 87 |
| 112 // Sets whether the video encoder should be requested to encode losslessly, | 88 // Sets whether the video encoder should be requested to encode losslessly, |
| 113 // or to use a lossless color space (typically requiring higher bandwidth). | 89 // or to use a lossless color space (typically requiring higher bandwidth). |
| 114 void SetLosslessEncode(bool want_lossless); | 90 void SetLosslessEncode(bool want_lossless); |
| 115 void SetLosslessColor(bool want_lossless); | 91 void SetLosslessColor(bool want_lossless); |
| 116 | 92 |
| 117 private: | 93 private: |
| 118 friend class base::RefCountedThreadSafe<VideoFramePump>; | 94 // webrtc::DesktopCapturer::Callback interface. |
| 119 ~VideoFramePump() override; | |
| 120 | |
| 121 // Capturer thread ---------------------------------------------------------- | |
| 122 | |
| 123 // TODO(sergeyu): Move all methods that run on the capture thread to a | |
| 124 // separate class and make VideoFramePump not ref-counted. | |
| 125 | |
| 126 // webrtc::DesktopCapturer::Callback implementation. | |
| 127 webrtc::SharedMemory* CreateSharedMemory(size_t size) override; | 95 webrtc::SharedMemory* CreateSharedMemory(size_t size) override; |
| 128 void OnCaptureCompleted(webrtc::DesktopFrame* frame) override; | 96 void OnCaptureCompleted(webrtc::DesktopFrame* frame) override; |
| 129 | 97 |
| 130 // webrtc::MouseCursorMonitor::Callback implementation. | 98 // Callback for CaptureScheduler. |
| 131 void OnMouseCursor(webrtc::MouseCursor* mouse_cursor) override; | |
| 132 void OnMouseCursorPosition(webrtc::MouseCursorMonitor::CursorState state, | |
| 133 const webrtc::DesktopVector& position) override; | |
| 134 | |
| 135 // Starts the capturer on the capture thread. | |
| 136 void StartOnCaptureThread(); | |
| 137 | |
| 138 // Stops scheduling frame captures on the capture thread. | |
| 139 void StopOnCaptureThread(); | |
| 140 | |
| 141 // Captures next frame on the capture thread. | |
| 142 void CaptureNextFrameOnCaptureThread(); | |
| 143 | |
| 144 // Network thread ----------------------------------------------------------- | |
| 145 | |
| 146 // Captures a new frame. Called by CaptureScheduler. | |
| 147 void CaptureNextFrame(); | 99 void CaptureNextFrame(); |
| 148 | 100 |
| 149 // Encodes and sends |frame|. | |
| 150 void EncodeAndSendFrame(scoped_ptr<webrtc::DesktopFrame> frame); | |
| 151 | |
| 152 // Sends encoded frame | 101 // Sends encoded frame |
| 153 void SendEncodedFrame(int64 latest_event_timestamp, | 102 void SendEncodedFrame(int64 latest_event_timestamp, |
| 154 base::TimeTicks timestamp, | 103 base::TimeTicks timestamp, |
| 155 scoped_ptr<VideoPacket> packet); | 104 scoped_ptr<VideoPacket> packet); |
| 156 | 105 |
| 157 // Callback passed to |video_stub_| for the last packet in each frame, to | 106 // Callback passed to |video_stub_| for the last packet in each frame, to |
| 158 // rate-limit frame captures to network throughput. | 107 // rate-limit frame captures to network throughput. |
| 159 void OnVideoPacketSent(); | 108 void OnVideoPacketSent(); |
| 160 | 109 |
| 161 // Called by |keep_alive_timer_|. | 110 // Called by |keep_alive_timer_|. |
| 162 void SendKeepAlivePacket(); | 111 void SendKeepAlivePacket(); |
| 163 | 112 |
| 164 // Callback for |video_stub_| called after a keep-alive packet is sent. | 113 // Callback for |video_stub_| called after a keep-alive packet is sent. |
| 165 void OnKeepAlivePacketSent(); | 114 void OnKeepAlivePacketSent(); |
| 166 | 115 |
| 167 // Send updated cursor shape to client. | 116 base::ThreadChecker thread_checker_; |
| 168 void SendCursorShape(scoped_ptr<protocol::CursorShapeInfo> cursor_shape); | |
| 169 | 117 |
| 170 // Task runners used by this class. | 118 // Task runner used to run |encoder_|. |
| 171 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner_; | |
| 172 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; | 119 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; |
| 173 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | |
| 174 | 120 |
| 175 // Used to capture frames. Always accessed on the capture thread. | 121 // Capturer used to capture the screen. |
| 176 scoped_ptr<webrtc::DesktopCapturer> capturer_; | 122 scoped_ptr<webrtc::DesktopCapturer> capturer_; |
| 177 | 123 |
| 178 // Used to capture mouse cursor shapes. Always accessed on the capture thread. | |
| 179 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor_; | |
| 180 | |
| 181 // Used to encode captured frames. Always accessed on the encode thread. | 124 // Used to encode captured frames. Always accessed on the encode thread. |
| 182 scoped_ptr<VideoEncoder> encoder_; | 125 scoped_ptr<VideoEncoder> encoder_; |
| 183 | 126 |
| 184 // Interfaces through which video frames and cursor shapes are passed to the | 127 // Interface through which video frames are passed to the client. |
| 185 // client. These members are always accessed on the network thread. | |
| 186 protocol::CursorShapeStub* cursor_stub_; | |
| 187 protocol::VideoStub* video_stub_; | 128 protocol::VideoStub* video_stub_; |
| 188 | 129 |
| 189 // Timer used to ensure that we send empty keep-alive frames to the client | 130 // Timer used to ensure that we send empty keep-alive frames to the client |
| 190 // even when the video stream is paused or encoder is busy. | 131 // even when the video stream is paused or encoder is busy. |
| 191 scoped_ptr<base::DelayTimer<VideoFramePump> > keep_alive_timer_; | 132 base::Timer keep_alive_timer_; |
| 133 |
| 134 // CaptureScheduler calls CaptureNextFrame() whenever a new frame needs to be |
| 135 // captured. |
| 136 CaptureScheduler capture_scheduler_; |
| 192 | 137 |
| 193 // Number updated by the caller to trace performance. | 138 // Number updated by the caller to trace performance. |
| 194 int64 latest_event_timestamp_; | 139 int64 latest_event_timestamp_; |
| 195 | 140 |
| 196 scoped_ptr<CaptureScheduler> capture_scheduler_; | 141 base::WeakPtrFactory<VideoFramePump> weak_factory_; |
| 197 | 142 |
| 198 DISALLOW_COPY_AND_ASSIGN(VideoFramePump); | 143 DISALLOW_COPY_AND_ASSIGN(VideoFramePump); |
| 199 }; | 144 }; |
| 200 | 145 |
| 201 } // namespace remoting | 146 } // namespace remoting |
| 202 | 147 |
| 203 #endif // REMOTING_HOST_VIDEO_FRAME_PUMP_H_ | 148 #endif // REMOTING_HOST_VIDEO_FRAME_PUMP_H_ |
| OLD | NEW |