OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // This class chooses a capture interval so as to limit CPU usage to not exceed | |
6 // a specified %age. It bases this on the CPU usage of recent capture and encode | |
7 // operations, and on the number of available CPUs. | |
8 | |
9 #ifndef REMOTING_HOST_CAPTURE_SCHEDULER_H_ | 5 #ifndef REMOTING_HOST_CAPTURE_SCHEDULER_H_ |
10 #define REMOTING_HOST_CAPTURE_SCHEDULER_H_ | 6 #define REMOTING_HOST_CAPTURE_SCHEDULER_H_ |
11 | 7 |
12 #include "base/callback.h" | 8 #include "base/callback.h" |
13 #include "base/threading/non_thread_safe.h" | 9 #include "base/threading/thread_checker.h" |
14 #include "base/time/tick_clock.h" | 10 #include "base/time/tick_clock.h" |
15 #include "base/time/time.h" | 11 #include "base/time/time.h" |
16 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
17 #include "remoting/base/running_average.h" | 13 #include "remoting/base/running_average.h" |
| 14 #include "remoting/protocol/video_feedback_stub.h" |
18 | 15 |
19 namespace remoting { | 16 namespace remoting { |
20 | 17 |
| 18 class VideoPacket; |
| 19 |
21 // CaptureScheduler is used by the VideoFramePump to schedule frame capturer, | 20 // CaptureScheduler is used by the VideoFramePump to schedule frame capturer, |
22 // taking into account capture delay, encoder delay, network bandwidth, etc. | 21 // taking into account capture delay, encoder delay, network bandwidth, etc. |
23 class CaptureScheduler : public base::NonThreadSafe { | 22 // It implements VideoFeedbackStub to receive frame acknowledgments from the |
| 23 // client. |
| 24 // |
| 25 // It attempts to achieve the following goals when scheduling frames: |
| 26 // - Keep round-trip latency as low a possible. |
| 27 // - Parallelize capture, encode and transmission, to achieve frame rate as |
| 28 // close to the target of 30fps as possible. |
| 29 // - Limit CPU usage to 50%. |
| 30 class CaptureScheduler : public protocol::VideoFeedbackStub { |
24 public: | 31 public: |
25 // |capture_closure| is called every time a new frame needs to be captured. | |
26 explicit CaptureScheduler(const base::Closure& capture_closure); | 32 explicit CaptureScheduler(const base::Closure& capture_closure); |
27 ~CaptureScheduler(); | 33 ~CaptureScheduler() override; |
28 | 34 |
29 // Starts the scheduler. | 35 // Starts the scheduler. |
30 void Start(); | 36 void Start(); |
31 | 37 |
32 // Pauses or unpauses the stream. | 38 // Pauses or unpauses the stream. |
33 void Pause(bool pause); | 39 void Pause(bool pause); |
34 | 40 |
35 // Notifies the scheduler that a capture has been completed. | 41 // Notifies the scheduler that a capture has been completed. |
36 void OnCaptureCompleted(); | 42 void OnCaptureCompleted(); |
37 | 43 |
38 // Notifies the scheduler that a frame has been encoded. | 44 // Notifies the scheduler that a frame has been encoded. The scheduler can |
39 void OnFrameEncoded(base::TimeDelta encode_time); | 45 // change |packet| if necessary, e.g. set |frame_id|. |
| 46 void OnFrameEncoded(VideoPacket* packet); |
40 | 47 |
41 // Notifies the scheduler that a frame has been sent. | 48 // Notifies the scheduler that a frame has been sent. |
42 void OnFrameSent(); | 49 void OnFrameSent(); |
43 | 50 |
| 51 // VideoFeedbackStub interface. |
| 52 void ProcessVideoAck(scoped_ptr<VideoAck> video_ack) override; |
| 53 |
44 // Sets minimum interval between frames. | 54 // Sets minimum interval between frames. |
45 void set_minimum_interval(base::TimeDelta minimum_interval) { | 55 void set_minimum_interval(base::TimeDelta minimum_interval) { |
46 minimum_interval_ = minimum_interval; | 56 minimum_interval_ = minimum_interval; |
47 } | 57 } |
48 | 58 |
49 // Helper functions for tests. | 59 // Helper functions for tests. |
50 void SetTickClockForTest(scoped_ptr<base::TickClock> tick_clock); | 60 void SetTickClockForTest(scoped_ptr<base::TickClock> tick_clock); |
51 void SetTimerForTest(scoped_ptr<base::Timer> timer); | 61 void SetTimerForTest(scoped_ptr<base::Timer> timer); |
52 void SetNumOfProcessorsForTest(int num_of_processors); | 62 void SetNumOfProcessorsForTest(int num_of_processors); |
53 | 63 |
54 private: | 64 private: |
55 // Schedules |capture_timer_| to call CaptureNextFrame() at appropriate time. | 65 // Schedules |capture_timer_| to call CaptureNextFrame() at appropriate time. |
56 // Doesn't do anything if next frame cannot be captured yet (e.g. because | 66 // Doesn't do anything if next frame cannot be captured yet (e.g. because |
57 // there are too many frames being processed). | 67 // there are too many frames being processed). |
58 void ScheduleNextCapture(); | 68 void ScheduleNextCapture(); |
59 | 69 |
60 // Called by |capture_timer_|. Calls |capture_closure_| to start capturing a | 70 // Called by |capture_timer_|. Calls |capture_closure_| to start capturing a |
61 // new frame. | 71 // new frame. |
62 void CaptureNextFrame(); | 72 void CaptureNextFrame(); |
63 | 73 |
64 base::Closure capture_closure_; | 74 base::Closure capture_closure_; |
65 | 75 |
| 76 // Set to true if the connection supports video frame acknowledgments. |
| 77 bool acks_supported_; |
| 78 |
66 scoped_ptr<base::TickClock> tick_clock_; | 79 scoped_ptr<base::TickClock> tick_clock_; |
67 | 80 |
68 // Timer used to schedule CaptureNextFrame(). | 81 // Timer used to schedule CaptureNextFrame(). |
69 scoped_ptr<base::Timer> capture_timer_; | 82 scoped_ptr<base::Timer> capture_timer_; |
70 | 83 |
71 // Minimum interval between frames that determines maximum possible framerate. | 84 // Minimum interval between frames that determines maximum possible framerate. |
72 base::TimeDelta minimum_interval_; | 85 base::TimeDelta minimum_interval_; |
73 | 86 |
74 int num_of_processors_; | 87 int num_of_processors_; |
75 | 88 |
76 RunningAverage capture_time_; | 89 RunningAverage capture_time_; |
77 RunningAverage encode_time_; | 90 RunningAverage encode_time_; |
78 | 91 |
79 // Total number of pending frames that are being captured, encoded or sent. | 92 // Number of frames pending encoding. |
80 int pending_frames_; | 93 int num_encoding_frames_; |
| 94 |
| 95 // Number of frames in the sending queue. |
| 96 int num_sending_frames_; |
| 97 |
| 98 // Number of outgoing frames for which we haven't received an acknowledgment. |
| 99 int num_unacknowledged_frames_; |
81 | 100 |
82 // Set to true when capture is pending. | 101 // Set to true when capture is pending. |
83 bool capture_pending_; | 102 bool capture_pending_; |
84 | 103 |
85 // Time at which the last capture started. Used to schedule |capture_timer_|. | 104 // Time at which the last capture started. Used to schedule |capture_timer_|. |
86 base::TimeTicks last_capture_started_time_; | 105 base::TimeTicks last_capture_started_time_; |
87 | 106 |
88 bool is_paused_; | 107 bool is_paused_; |
89 | 108 |
| 109 // Frame ID to be assigned to the next outgoing video frame. |
| 110 uint32_t next_frame_id_; |
| 111 |
| 112 base::ThreadChecker thread_checker_; |
| 113 |
90 DISALLOW_COPY_AND_ASSIGN(CaptureScheduler); | 114 DISALLOW_COPY_AND_ASSIGN(CaptureScheduler); |
91 }; | 115 }; |
92 | 116 |
93 } // namespace remoting | 117 } // namespace remoting |
94 | 118 |
95 #endif // REMOTING_HOST_CAPTURE_SCHEDULER_H_ | 119 #endif // REMOTING_HOST_CAPTURE_SCHEDULER_H_ |
OLD | NEW |