OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_SCHEDULER_H_ | 5 #ifndef REMOTING_HOST_VIDEO_SCHEDULER_H_ |
6 #define REMOTING_HOST_VIDEO_SCHEDULER_H_ | 6 #define REMOTING_HOST_VIDEO_SCHEDULER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
15 #include "remoting/codec/video_encoder.h" | 15 #include "remoting/codec/video_encoder.h" |
16 #include "remoting/host/capture_scheduler.h" | |
17 #include "remoting/proto/video.pb.h" | 16 #include "remoting/proto/video.pb.h" |
18 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" | 17 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" |
19 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h" | 18 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h" |
20 | 19 |
21 namespace base { | 20 namespace base { |
22 class SingleThreadTaskRunner; | 21 class SingleThreadTaskRunner; |
23 } // namespace base | 22 } // namespace base |
24 | 23 |
25 namespace media { | 24 namespace media { |
26 class DesktopCapturer; | 25 class DesktopCapturer; |
27 } // namespace media | 26 } // namespace media |
28 | 27 |
29 namespace remoting { | 28 namespace remoting { |
30 | 29 |
| 30 class CaptureScheduler; |
31 class CursorShapeInfo; | 31 class CursorShapeInfo; |
32 | 32 |
33 namespace protocol { | 33 namespace protocol { |
34 class CursorShapeInfo; | 34 class CursorShapeInfo; |
35 class CursorShapeStub; | 35 class CursorShapeStub; |
36 class VideoStub; | 36 class VideoStub; |
37 } // namespace protocol | 37 } // namespace protocol |
38 | 38 |
39 // Class responsible for scheduling frame captures from a | 39 // Class responsible for scheduling frame captures from a |
40 // webrtc::DesktopCapturer, delivering them to a VideoEncoder to encode, and | 40 // webrtc::DesktopCapturer, delivering them to a VideoEncoder to encode, and |
(...skipping 24 matching lines...) Expand all Loading... |
65 // | . . | 65 // | . . |
66 // | . . | 66 // | . . |
67 // | ............ | 67 // | ............ |
68 // | Time | 68 // | Time |
69 // v | 69 // v |
70 // | 70 // |
71 // VideoScheduler would ideally schedule captures so as to saturate the slowest | 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 | 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 | 73 // rate-limit captures to avoid overloading the host system, either by consuming |
74 // too much CPU, or hogging the host's graphics subsystem. | 74 // too much CPU, or hogging the host's graphics subsystem. |
75 | 75 // |
| 76 // TODO(sergeyu): Rename this class to VideoFramePipe. |
76 class VideoScheduler : public base::RefCountedThreadSafe<VideoScheduler>, | 77 class VideoScheduler : public base::RefCountedThreadSafe<VideoScheduler>, |
77 public webrtc::DesktopCapturer::Callback, | 78 public webrtc::DesktopCapturer::Callback, |
78 public webrtc::MouseCursorMonitor::Callback { | 79 public webrtc::MouseCursorMonitor::Callback { |
79 public: | 80 public: |
80 // Enables timestamps for generated frames. Used for testing. | 81 // Enables timestamps for generated frames. Used for testing. |
81 static void EnableTimestampsForTests(); | 82 static void EnableTimestampsForTests(); |
82 | 83 |
83 // Creates a VideoScheduler running capture, encode and network tasks on the | 84 // Creates a VideoScheduler running capture, encode and network tasks on the |
84 // supplied TaskRunners. Video and cursor shape updates will be pumped to | 85 // supplied TaskRunners. Video and cursor shape updates will be pumped to |
85 // |video_stub| and |client_stub|, which must remain valid until Stop() is | 86 // |video_stub| and |client_stub|, which must remain valid until Stop() is |
86 // called. |capturer| is used to capture frames. | 87 // called. |capturer| is used to capture frames. |
87 VideoScheduler( | 88 VideoScheduler( |
88 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, | 89 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, |
89 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 90 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
90 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | 91 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
91 scoped_ptr<webrtc::DesktopCapturer> capturer, | 92 scoped_ptr<webrtc::DesktopCapturer> capturer, |
92 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, | 93 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, |
93 scoped_ptr<VideoEncoder> encoder, | 94 scoped_ptr<VideoEncoder> encoder, |
94 protocol::CursorShapeStub* cursor_stub, | 95 protocol::CursorShapeStub* cursor_stub, |
95 protocol::VideoStub* video_stub); | 96 protocol::VideoStub* video_stub); |
96 | 97 |
97 // webrtc::DesktopCapturer::Callback implementation. | |
98 webrtc::SharedMemory* CreateSharedMemory(size_t size) override; | |
99 void OnCaptureCompleted(webrtc::DesktopFrame* frame) override; | |
100 | |
101 // webrtc::MouseCursorMonitor::Callback implementation. | |
102 void OnMouseCursor(webrtc::MouseCursor* mouse_cursor) override; | |
103 void OnMouseCursorPosition(webrtc::MouseCursorMonitor::CursorState state, | |
104 const webrtc::DesktopVector& position) override; | |
105 | |
106 // Starts scheduling frame captures. | 98 // Starts scheduling frame captures. |
107 void Start(); | 99 void Start(); |
108 | 100 |
109 // Stop scheduling frame captures. This object cannot be re-used once | 101 // Stop scheduling frame captures. This object cannot be re-used once |
110 // it has been stopped. | 102 // it has been stopped. |
111 void Stop(); | 103 void Stop(); |
112 | 104 |
113 // Pauses or resumes scheduling of frame captures. Pausing/resuming captures | 105 // Pauses or resumes scheduling of frame captures. Pausing/resuming captures |
114 // only affects capture scheduling and does not stop/start the capturer. | 106 // only affects capture scheduling and does not stop/start the capturer. |
115 void Pause(bool pause); | 107 void Pause(bool pause); |
116 | 108 |
117 // Updates event timestamp from the last event received from the client. This | 109 // Updates event timestamp from the last event received from the client. This |
118 // value is sent back to the client for roundtrip latency estimates. | 110 // value is sent back to the client for roundtrip latency estimates. |
119 void SetLatestEventTimestamp(int64 latest_event_timestamp); | 111 void SetLatestEventTimestamp(int64 latest_event_timestamp); |
120 | 112 |
121 // Sets whether the video encoder should be requested to encode losslessly, | 113 // Sets whether the video encoder should be requested to encode losslessly, |
122 // or to use a lossless color space (typically requiring higher bandwidth). | 114 // or to use a lossless color space (typically requiring higher bandwidth). |
123 void SetLosslessEncode(bool want_lossless); | 115 void SetLosslessEncode(bool want_lossless); |
124 void SetLosslessColor(bool want_lossless); | 116 void SetLosslessColor(bool want_lossless); |
125 | 117 |
126 private: | 118 private: |
127 friend class base::RefCountedThreadSafe<VideoScheduler>; | 119 friend class base::RefCountedThreadSafe<VideoScheduler>; |
128 ~VideoScheduler() override; | 120 ~VideoScheduler() override; |
129 | 121 |
130 // Capturer thread ---------------------------------------------------------- | 122 // Capturer thread ---------------------------------------------------------- |
131 | 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 |
132 // Starts the capturer on the capture thread. | 136 // Starts the capturer on the capture thread. |
133 void StartOnCaptureThread(); | 137 void StartOnCaptureThread(); |
134 | 138 |
135 // Stops scheduling frame captures on the capture thread. | 139 // Stops scheduling frame captures on the capture thread. |
136 void StopOnCaptureThread(); | 140 void StopOnCaptureThread(); |
137 | 141 |
138 // Schedules the next call to CaptureNextFrame. | 142 // Captures next frame on the capture thread. |
139 void ScheduleNextCapture(); | 143 void CaptureNextFrameOnCaptureThread(); |
140 | |
141 // Starts the next frame capture, unless there are already too many pending. | |
142 void CaptureNextFrame(); | |
143 | |
144 // Called when a frame capture has been encoded & sent to the client. | |
145 void FrameCaptureCompleted(); | |
146 | 144 |
147 // Network thread ----------------------------------------------------------- | 145 // Network thread ----------------------------------------------------------- |
148 | 146 |
149 // Send |packet| to the client, unless we are in the process of stopping. | 147 // Captures a new frame. Called by CaptureScheduler. |
150 void SendVideoPacket(scoped_ptr<VideoPacket> packet); | 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); |
151 | 157 |
152 // Callback passed to |video_stub_| for the last packet in each frame, to | 158 // Callback passed to |video_stub_| for the last packet in each frame, to |
153 // rate-limit frame captures to network throughput. | 159 // rate-limit frame captures to network throughput. |
154 void OnVideoPacketSent(); | 160 void OnVideoPacketSent(); |
155 | 161 |
156 // Called by |keep_alive_timer_|. | 162 // Called by |keep_alive_timer_|. |
157 void SendKeepAlivePacket(); | 163 void SendKeepAlivePacket(); |
158 | 164 |
159 // Callback for |video_stub_| called after a keep-alive packet is sent. | 165 // Callback for |video_stub_| called after a keep-alive packet is sent. |
160 void OnKeepAlivePacketSent(); | 166 void OnKeepAlivePacketSent(); |
161 | 167 |
162 // Send updated cursor shape to client. | 168 // Send updated cursor shape to client. |
163 void SendCursorShape(scoped_ptr<protocol::CursorShapeInfo> cursor_shape); | 169 void SendCursorShape(scoped_ptr<protocol::CursorShapeInfo> cursor_shape); |
164 | 170 |
165 // Encoder thread ----------------------------------------------------------- | |
166 | |
167 // Encode a frame, passing generated VideoPackets to SendVideoPacket(). | |
168 void EncodeFrame(scoped_ptr<webrtc::DesktopFrame> frame, | |
169 int64 latest_event_timestamp, | |
170 base::TimeTicks timestamp); | |
171 | |
172 void EncodedDataAvailableCallback(int64 latest_event_timestamp, | |
173 scoped_ptr<VideoPacket> packet); | |
174 | |
175 // Task runners used by this class. | 171 // Task runners used by this class. |
176 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner_; | 172 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner_; |
177 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; | 173 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; |
178 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | 174 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
179 | 175 |
180 // Used to capture frames. Always accessed on the capture thread. | 176 // Used to capture frames. Always accessed on the capture thread. |
181 scoped_ptr<webrtc::DesktopCapturer> capturer_; | 177 scoped_ptr<webrtc::DesktopCapturer> capturer_; |
182 | 178 |
183 // Used to capture mouse cursor shapes. Always accessed on the capture thread. | 179 // Used to capture mouse cursor shapes. Always accessed on the capture thread. |
184 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor_; | 180 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor_; |
185 | 181 |
186 // Used to encode captured frames. Always accessed on the encode thread. | 182 // Used to encode captured frames. Always accessed on the encode thread. |
187 scoped_ptr<VideoEncoder> encoder_; | 183 scoped_ptr<VideoEncoder> encoder_; |
188 | 184 |
189 // Interfaces through which video frames and cursor shapes are passed to the | 185 // Interfaces through which video frames and cursor shapes are passed to the |
190 // client. These members are always accessed on the network thread. | 186 // client. These members are always accessed on the network thread. |
191 protocol::CursorShapeStub* cursor_stub_; | 187 protocol::CursorShapeStub* cursor_stub_; |
192 protocol::VideoStub* video_stub_; | 188 protocol::VideoStub* video_stub_; |
193 | 189 |
194 // Timer used to schedule CaptureNextFrame(). | |
195 scoped_ptr<base::OneShotTimer<VideoScheduler> > capture_timer_; | |
196 | |
197 // Timer used to ensure that we send empty keep-alive frames to the client | 190 // Timer used to ensure that we send empty keep-alive frames to the client |
198 // even when the video stream is paused or encoder is busy. | 191 // even when the video stream is paused or encoder is busy. |
199 scoped_ptr<base::DelayTimer<VideoScheduler> > keep_alive_timer_; | 192 scoped_ptr<base::DelayTimer<VideoScheduler> > keep_alive_timer_; |
200 | 193 |
201 // The number of frames being processed, i.e. frames that we are currently | |
202 // capturing, encoding or sending. The value is capped at 2 to minimize | |
203 // latency. | |
204 int pending_frames_; | |
205 | |
206 // Set when the capturer is capturing a frame. | |
207 bool capture_pending_; | |
208 | |
209 // True if the previous scheduled capture was skipped. | |
210 bool did_skip_frame_; | |
211 | |
212 // True if capture of video frames is paused. | |
213 bool is_paused_; | |
214 | |
215 // Number updated by the caller to trace performance. | 194 // Number updated by the caller to trace performance. |
216 int64 latest_event_timestamp_; | 195 int64 latest_event_timestamp_; |
217 | 196 |
218 // An object to schedule capturing. | 197 scoped_ptr<CaptureScheduler> capture_scheduler_; |
219 CaptureScheduler scheduler_; | |
220 | 198 |
221 DISALLOW_COPY_AND_ASSIGN(VideoScheduler); | 199 DISALLOW_COPY_AND_ASSIGN(VideoScheduler); |
222 }; | 200 }; |
223 | 201 |
224 } // namespace remoting | 202 } // namespace remoting |
225 | 203 |
226 #endif // REMOTING_HOST_VIDEO_SCHEDULER_H_ | 204 #endif // REMOTING_HOST_VIDEO_SCHEDULER_H_ |
OLD | NEW |