Chromium Code Reviews| 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/non_thread_safe.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 base::NonThreadSafe, |
|
Wez
2015/02/12 21:59:15
ThreadChecker?
Sergey Ulanov
2015/02/13 08:39:44
Done.
| |
| 75 // TODO(sergeyu): Rename this class to VideoFramePump. | 66 public webrtc::DesktopCapturer::Callback { |
| 76 class VideoFramePump : public base::RefCountedThreadSafe<VideoFramePump>, | |
| 77 public webrtc::DesktopCapturer::Callback, | |
| 78 public webrtc::MouseCursorMonitor::Callback { | |
| 79 public: | 67 public: |
| 80 // Enables timestamps for generated frames. Used for testing. | 68 // Enables timestamps for generated frames. Used for testing. |
| 81 static void EnableTimestampsForTests(); | 69 static void EnableTimestampsForTests(); |
| 82 | 70 |
| 83 // Creates a VideoFramePump running capture, encode and network tasks on the | 71 // Creates a VideoFramePump running capture, encode and network tasks on the |
| 84 // supplied TaskRunners. Video and cursor shape updates will be pumped to | 72 // supplied TaskRunners. Video will be pumped to |video_stub|, which must |
| 85 // |video_stub| and |client_stub|, which must remain valid until Stop() is | 73 // outlive the pump.. |
| 86 // called. |capturer| is used to capture frames. | |
| 87 VideoFramePump( | 74 VideoFramePump( |
| 88 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, | |
| 89 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 75 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
| 90 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | |
| 91 scoped_ptr<webrtc::DesktopCapturer> capturer, | 76 scoped_ptr<webrtc::DesktopCapturer> capturer, |
| 92 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, | |
| 93 scoped_ptr<VideoEncoder> encoder, | 77 scoped_ptr<VideoEncoder> encoder, |
| 94 protocol::CursorShapeStub* cursor_stub, | |
| 95 protocol::VideoStub* video_stub); | 78 protocol::VideoStub* video_stub); |
| 96 | 79 ~VideoFramePump(); |
| 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 | 80 |
| 104 // Pauses or resumes scheduling of frame captures. Pausing/resuming captures | 81 // Pauses or resumes scheduling of frame captures. Pausing/resuming captures |
| 105 // only affects capture scheduling and does not stop/start the capturer. | 82 // only affects capture scheduling and does not stop/start the capturer. |
| 106 void Pause(bool pause); | 83 void Pause(bool pause); |
| 107 | 84 |
| 108 // Updates event timestamp from the last event received from the client. This | 85 // Updates event timestamp from the last event received from the client. This |
| 109 // value is sent back to the client for roundtrip latency estimates. | 86 // value is sent back to the client for roundtrip latency estimates. |
| 110 void SetLatestEventTimestamp(int64 latest_event_timestamp); | 87 void SetLatestEventTimestamp(int64 latest_event_timestamp); |
| 111 | 88 |
| 112 // Sets whether the video encoder should be requested to encode losslessly, | 89 // Sets whether the video encoder should be requested to encode losslessly, |
| 113 // or to use a lossless color space (typically requiring higher bandwidth). | 90 // or to use a lossless color space (typically requiring higher bandwidth). |
| 114 void SetLosslessEncode(bool want_lossless); | 91 void SetLosslessEncode(bool want_lossless); |
| 115 void SetLosslessColor(bool want_lossless); | 92 void SetLosslessColor(bool want_lossless); |
| 116 | 93 |
| 117 private: | 94 private: |
| 118 friend class base::RefCountedThreadSafe<VideoFramePump>; | 95 // 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; | 96 webrtc::SharedMemory* CreateSharedMemory(size_t size) override; |
| 128 void OnCaptureCompleted(webrtc::DesktopFrame* frame) override; | 97 void OnCaptureCompleted(webrtc::DesktopFrame* frame) override; |
| 129 | 98 |
| 130 // webrtc::MouseCursorMonitor::Callback implementation. | 99 // 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(); | 100 void CaptureNextFrame(); |
| 148 | 101 |
| 149 // Encodes and sends |frame|. | |
| 150 void EncodeAndSendFrame(scoped_ptr<webrtc::DesktopFrame> frame); | |
| 151 | |
| 152 // Sends encoded frame | 102 // Sends encoded frame |
| 153 void SendEncodedFrame(int64 latest_event_timestamp, | 103 void SendEncodedFrame(int64 latest_event_timestamp, |
| 154 base::TimeTicks timestamp, | 104 base::TimeTicks timestamp, |
| 155 scoped_ptr<VideoPacket> packet); | 105 scoped_ptr<VideoPacket> packet); |
| 156 | 106 |
| 157 // Callback passed to |video_stub_| for the last packet in each frame, to | 107 // Callback passed to |video_stub_| for the last packet in each frame, to |
| 158 // rate-limit frame captures to network throughput. | 108 // rate-limit frame captures to network throughput. |
| 159 void OnVideoPacketSent(); | 109 void OnVideoPacketSent(); |
| 160 | 110 |
| 161 // Called by |keep_alive_timer_|. | 111 // Called by |keep_alive_timer_|. |
| 162 void SendKeepAlivePacket(); | 112 void SendKeepAlivePacket(); |
| 163 | 113 |
| 164 // Callback for |video_stub_| called after a keep-alive packet is sent. | 114 // Callback for |video_stub_| called after a keep-alive packet is sent. |
| 165 void OnKeepAlivePacketSent(); | 115 void OnKeepAlivePacketSent(); |
| 166 | 116 |
| 167 // Send updated cursor shape to client. | 117 // Task runner used to run |encoder_|. |
| 168 void SendCursorShape(scoped_ptr<protocol::CursorShapeInfo> cursor_shape); | 118 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; |
| 169 | 119 |
| 170 // Task runners used by this class. | 120 // Capturer used to capture the screen. |
| 171 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner_; | |
| 172 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; | |
| 173 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | |
| 174 | |
| 175 // Used to capture frames. Always accessed on the capture thread. | |
| 176 scoped_ptr<webrtc::DesktopCapturer> capturer_; | 121 scoped_ptr<webrtc::DesktopCapturer> capturer_; |
| 177 | 122 |
| 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. | 123 // Used to encode captured frames. Always accessed on the encode thread. |
| 182 scoped_ptr<VideoEncoder> encoder_; | 124 scoped_ptr<VideoEncoder> encoder_; |
| 183 | 125 |
| 184 // Interfaces through which video frames and cursor shapes are passed to the | 126 // 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_; | 127 protocol::VideoStub* video_stub_; |
| 188 | 128 |
| 189 // Timer used to ensure that we send empty keep-alive frames to the client | 129 // 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. | 130 // even when the video stream is paused or encoder is busy. |
| 191 scoped_ptr<base::DelayTimer<VideoFramePump> > keep_alive_timer_; | 131 base::Timer keep_alive_timer_; |
| 132 | |
| 133 // CaptureScheduler calls CaptureNextFrame() whenever a new frame needs to be | |
| 134 // captured. | |
| 135 CaptureScheduler capture_scheduler_; | |
| 192 | 136 |
| 193 // Number updated by the caller to trace performance. | 137 // Number updated by the caller to trace performance. |
| 194 int64 latest_event_timestamp_; | 138 int64 latest_event_timestamp_; |
| 195 | 139 |
| 196 scoped_ptr<CaptureScheduler> capture_scheduler_; | 140 base::WeakPtrFactory<VideoFramePump> weak_factory_; |
| 197 | 141 |
| 198 DISALLOW_COPY_AND_ASSIGN(VideoFramePump); | 142 DISALLOW_COPY_AND_ASSIGN(VideoFramePump); |
| 199 }; | 143 }; |
| 200 | 144 |
| 201 } // namespace remoting | 145 } // namespace remoting |
| 202 | 146 |
| 203 #endif // REMOTING_HOST_VIDEO_FRAME_PUMP_H_ | 147 #endif // REMOTING_HOST_VIDEO_FRAME_PUMP_H_ |
| OLD | NEW |