Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / host / capture_scheduler.h
blobbe8131b5da25ed82ba49cb2a84cb23eb1a7adaeb
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 scoped_ptr<base::TickClock> tick_clock_;
78 // Timer used to schedule CaptureNextFrame().
79 scoped_ptr<base::Timer> capture_timer_;
81 // Minimum interval between frames that determines maximum possible framerate.
82 base::TimeDelta minimum_interval_;
84 int num_of_processors_;
86 RunningAverage capture_time_;
87 RunningAverage encode_time_;
89 // Number of frames pending encoding.
90 int num_encoding_frames_;
92 // Number of outgoing frames for which we haven't received an acknowledgment.
93 int num_unacknowledged_frames_;
95 // Set to true when capture is pending.
96 bool capture_pending_;
98 // Time at which the last capture started. Used to schedule |capture_timer_|.
99 base::TimeTicks last_capture_started_time_;
101 bool is_paused_;
103 // Frame ID to be assigned to the next outgoing video frame.
104 uint32_t next_frame_id_;
106 base::ThreadChecker thread_checker_;
108 DISALLOW_COPY_AND_ASSIGN(CaptureScheduler);
111 } // namespace remoting
113 #endif // REMOTING_HOST_CAPTURE_SCHEDULER_H_