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