Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / remoting / host / capture_scheduler.h
blob42a1657879506e8176bc9bdfc6e0f644c98afe30
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
3 // found in the LICENSE file.
5 #ifndef REMOTING_HOST_CAPTURE_SCHEDULER_H_
6 #define REMOTING_HOST_CAPTURE_SCHEDULER_H_
8 #include "base/callback.h"
9 #include "base/threading/thread_checker.h"
10 #include "base/time/tick_clock.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "remoting/base/running_average.h"
14 #include "remoting/protocol/video_feedback_stub.h"
16 namespace remoting {
18 class VideoPacket;
20 // CaptureScheduler is used by the VideoFramePump to schedule frame capturer,
21 // taking into account capture delay, encoder delay, network bandwidth, etc.
22 // It implements VideoFeedbackStub to receive frame acknowledgments from the
23 // client.
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 {
31 public:
32 explicit CaptureScheduler(const base::Closure& capture_closure);
33 ~CaptureScheduler() override;
35 // Starts the scheduler.
36 void Start();
38 // Pauses or unpauses the stream.
39 void Pause(bool pause);
41 // Notifies the scheduler that a capture has been completed.
42 void OnCaptureCompleted();
44 // Notifies the scheduler that a frame has been encoded. The scheduler can
45 // change |packet| if necessary, e.g. set |frame_id|.
46 void OnFrameEncoded(VideoPacket* packet);
48 // Notifies the scheduler that a frame has been sent.
49 void OnFrameSent();
51 // VideoFeedbackStub interface.
52 void ProcessVideoAck(scoped_ptr<VideoAck> video_ack) override;
54 // Sets minimum interval between frames.
55 void set_minimum_interval(base::TimeDelta minimum_interval) {
56 minimum_interval_ = minimum_interval;
59 // Helper functions for tests.
60 void SetTickClockForTest(scoped_ptr<base::TickClock> tick_clock);
61 void SetTimerForTest(scoped_ptr<base::Timer> timer);
62 void SetNumOfProcessorsForTest(int num_of_processors);
64 private:
65 // Schedules |capture_timer_| to call CaptureNextFrame() at appropriate time.
66 // Doesn't do anything if next frame cannot be captured yet (e.g. because
67 // there are too many frames being processed).
68 void ScheduleNextCapture();
70 // Called by |capture_timer_|. Calls |capture_closure_| to start capturing a
71 // new frame.
72 void CaptureNextFrame();
74 base::Closure capture_closure_;
76 // Set to true if the connection supports video frame acknowledgments.
77 bool acks_supported_;
79 scoped_ptr<base::TickClock> tick_clock_;
81 // Timer used to schedule CaptureNextFrame().
82 scoped_ptr<base::Timer> capture_timer_;
84 // Minimum interval between frames that determines maximum possible framerate.
85 base::TimeDelta minimum_interval_;
87 int num_of_processors_;
89 RunningAverage capture_time_;
90 RunningAverage encode_time_;
92 // Number of frames pending encoding.
93 int num_encoding_frames_;
95 // Number of frames in the sending queue.
96 int num_sending_frames_;
98 // Number of outgoing frames for which we haven't received an acknowledgment.
99 int num_unacknowledged_frames_;
101 // Set to true when capture is pending.
102 bool capture_pending_;
104 // Time at which the last capture started. Used to schedule |capture_timer_|.
105 base::TimeTicks last_capture_started_time_;
107 bool is_paused_;
109 // Frame ID to be assigned to the next outgoing video frame.
110 uint32_t next_frame_id_;
112 base::ThreadChecker thread_checker_;
114 DISALLOW_COPY_AND_ASSIGN(CaptureScheduler);
117 } // namespace remoting
119 #endif // REMOTING_HOST_CAPTURE_SCHEDULER_H_