Chromium Code Reviews| 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 // - Maximize frame-rate. | |
| 28 // - Limit CPU usage. | |
|
Wez
2015/02/23 21:52:17
nit: Specificy that it limits to less-than some ta
Sergey Ulanov
2015/02/23 22:22:33
Done.
| |
| 29 class CaptureScheduler : public protocol::VideoFeedbackStub { | |
| 24 public: | 30 public: |
| 25 // |capture_closure| is called every time a new frame needs to be captured. | |
| 26 explicit CaptureScheduler(const base::Closure& capture_closure); | 31 explicit CaptureScheduler(const base::Closure& capture_closure); |
| 27 ~CaptureScheduler(); | 32 ~CaptureScheduler(); |
| 28 | 33 |
| 29 // Starts the scheduler. | 34 // Starts the scheduler. |
| 30 void Start(); | 35 void Start(); |
| 31 | 36 |
| 32 // Pauses or unpauses the stream. | 37 // Pauses or unpauses the stream. |
| 33 void Pause(bool pause); | 38 void Pause(bool pause); |
| 34 | 39 |
| 35 // Notifies the scheduler that a capture has been completed. | 40 // Notifies the scheduler that a capture has been completed. |
| 36 void OnCaptureCompleted(); | 41 void OnCaptureCompleted(); |
| 37 | 42 |
| 38 // Notifies the scheduler that a frame has been encoded. | 43 // Notifies the scheduler that a frame has been encoded. The scheduler can |
| 39 void OnFrameEncoded(base::TimeDelta encode_time); | 44 // change |packet| if necessary, e.g. set |frame_id|. |
| 45 void OnFrameEncoded(VideoPacket* packet); | |
| 40 | 46 |
| 41 // Notifies the scheduler that a frame has been sent. | 47 // Notifies the scheduler that a frame has been sent. |
| 42 void OnFrameSent(); | 48 void OnFrameSent(); |
| 43 | 49 |
| 50 // VideoFeedbackStub interface. | |
| 51 void ProcessVideoAck(scoped_ptr<VideoAck> video_ack) override; | |
| 52 | |
| 44 // Sets minimum interval between frames. | 53 // Sets minimum interval between frames. |
| 45 void set_minimum_interval(base::TimeDelta minimum_interval) { | 54 void set_minimum_interval(base::TimeDelta minimum_interval) { |
| 46 minimum_interval_ = minimum_interval; | 55 minimum_interval_ = minimum_interval; |
| 47 } | 56 } |
| 48 | 57 |
| 49 // Helper functions for tests. | 58 // Helper functions for tests. |
| 50 void SetTickClockForTest(scoped_ptr<base::TickClock> tick_clock); | 59 void SetTickClockForTest(scoped_ptr<base::TickClock> tick_clock); |
| 51 void SetTimerForTest(scoped_ptr<base::Timer> timer); | 60 void SetTimerForTest(scoped_ptr<base::Timer> timer); |
| 52 void SetNumOfProcessorsForTest(int num_of_processors); | 61 void SetNumOfProcessorsForTest(int num_of_processors); |
| 53 | 62 |
| 54 private: | 63 private: |
| 55 // Schedules |capture_timer_| to call CaptureNextFrame() at appropriate time. | 64 // Schedules |capture_timer_| to call CaptureNextFrame() at appropriate time. |
| 56 // Doesn't do anything if next frame cannot be captured yet (e.g. because | 65 // Doesn't do anything if next frame cannot be captured yet (e.g. because |
| 57 // there are too many frames being processed). | 66 // there are too many frames being processed). |
| 58 void ScheduleNextCapture(); | 67 void ScheduleNextCapture(); |
| 59 | 68 |
| 60 // Called by |capture_timer_|. Calls |capture_closure_| to start capturing a | 69 // Called by |capture_timer_|. Calls |capture_closure_| to start capturing a |
| 61 // new frame. | 70 // new frame. |
| 62 void CaptureNextFrame(); | 71 void CaptureNextFrame(); |
| 63 | 72 |
| 64 base::Closure capture_closure_; | 73 base::Closure capture_closure_; |
| 65 | 74 |
| 75 // Set to true if the connection supports video frame acknowledgments. | |
| 76 bool acks_supported_; | |
| 77 | |
| 66 scoped_ptr<base::TickClock> tick_clock_; | 78 scoped_ptr<base::TickClock> tick_clock_; |
| 67 | 79 |
| 68 // Timer used to schedule CaptureNextFrame(). | 80 // Timer used to schedule CaptureNextFrame(). |
| 69 scoped_ptr<base::Timer> capture_timer_; | 81 scoped_ptr<base::Timer> capture_timer_; |
| 70 | 82 |
| 71 // Minimum interval between frames that determines maximum possible framerate. | 83 // Minimum interval between frames that determines maximum possible framerate. |
| 72 base::TimeDelta minimum_interval_; | 84 base::TimeDelta minimum_interval_; |
| 73 | 85 |
| 74 int num_of_processors_; | 86 int num_of_processors_; |
| 75 | 87 |
| 76 RunningAverage capture_time_; | 88 RunningAverage capture_time_; |
| 77 RunningAverage encode_time_; | 89 RunningAverage encode_time_; |
| 78 | 90 |
| 79 // Total number of pending frames that are being captured, encoded or sent. | 91 // Number of frames pending encoding. |
| 80 int pending_frames_; | 92 int num_encoding_frames_; |
| 93 | |
| 94 // Number of frames in the sending queue. | |
| 95 int num_sending_frames_; | |
| 96 | |
| 97 // Number of outgoing frames for which we haven't received an acknowledgment. | |
| 98 int num_unacknowledged_frames_; | |
| 81 | 99 |
| 82 // Set to true when capture is pending. | 100 // Set to true when capture is pending. |
| 83 bool capture_pending_; | 101 bool capture_pending_; |
| 84 | 102 |
| 85 // Time at which the last capture started. Used to schedule |capture_timer_|. | 103 // Time at which the last capture started. Used to schedule |capture_timer_|. |
| 86 base::TimeTicks last_capture_started_time_; | 104 base::TimeTicks last_capture_started_time_; |
| 87 | 105 |
| 88 bool is_paused_; | 106 bool is_paused_; |
| 89 | 107 |
| 108 // Frame ID to be assigned to the next outgoing video frame. | |
| 109 uint32_t next_frame_id_; | |
| 110 | |
| 111 base::ThreadChecker thread_checker_; | |
| 112 | |
| 90 DISALLOW_COPY_AND_ASSIGN(CaptureScheduler); | 113 DISALLOW_COPY_AND_ASSIGN(CaptureScheduler); |
| 91 }; | 114 }; |
| 92 | 115 |
| 93 } // namespace remoting | 116 } // namespace remoting |
| 94 | 117 |
| 95 #endif // REMOTING_HOST_CAPTURE_SCHEDULER_H_ | 118 #endif // REMOTING_HOST_CAPTURE_SCHEDULER_H_ |
| OLD | NEW |