• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 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.
4 
5 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
6 #define CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
7 
8 #include "base/callback_forward.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "content/common/content_export.h"
12 
13 namespace content {
14 
15 // Filters a sequence of events to achieve a target frequency.
16 class CONTENT_EXPORT SmoothEventSampler {
17  public:
18   explicit SmoothEventSampler(base::TimeDelta capture_period,
19                               bool events_are_reliable,
20                               int redundant_capture_goal);
21 
22   // Add a new event to the event history, and return whether it ought to be
23   // sampled based on the desired |capture_period|. The event is not recorded as
24   // a sample until RecordSample() is called.
25   bool AddEventAndConsiderSampling(base::TimeTicks event_time);
26 
27   // Operates on the last event added by AddEventAndConsiderSampling(), marking
28   // it as sampled. After this point we are current in the stream of events, as
29   // we have sampled the most recent event.
30   void RecordSample();
31 
32   // Returns true if, at time |event_time|, sampling should occur because too
33   // much time will have passed relative to the last event and/or sample.
34   bool IsOverdueForSamplingAt(base::TimeTicks event_time) const;
35 
36   // Returns true if AddEventAndConsiderSampling() has been called since the
37   // last call to RecordSample().
38   bool HasUnrecordedEvent() const;
39 
40  private:
41   const bool events_are_reliable_;
42   const base::TimeDelta capture_period_;
43   const int redundant_capture_goal_;
44   const base::TimeDelta token_bucket_capacity_;
45 
46   base::TimeTicks current_event_;
47   base::TimeTicks last_sample_;
48   int overdue_sample_count_;
49   base::TimeDelta token_bucket_;
50 
51   DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler);
52 };
53 
54 // VideoCaptureOracle manages the producer-side throttling of captured frames
55 // from a video capture device.  It is informed of every update by the device;
56 // this empowers it to look into the future and decide if a particular frame
57 // ought to be captured in order to achieve its target frame rate.
58 class CONTENT_EXPORT VideoCaptureOracle {
59  public:
60   enum Event {
61     kTimerPoll,
62     kCompositorUpdate,
63     kSoftwarePaint,
64   };
65 
66   VideoCaptureOracle(base::TimeDelta capture_period,
67                      bool events_are_reliable);
~VideoCaptureOracle()68   virtual ~VideoCaptureOracle() {}
69 
70   // Record an event of type |event|, and decide whether the caller should do a
71   // frame capture immediately. Decisions of the oracle are final: the caller
72   // must do what it is told.
73   bool ObserveEventAndDecideCapture(Event event, base::TimeTicks event_time);
74 
75   // Record the start of a capture.  Returns a frame_number to be used with
76   // CompleteCapture().
77   int RecordCapture();
78 
79   // Record the completion of a capture.  Returns true iff the captured frame
80   // should be delivered.
81   bool CompleteCapture(int frame_number, base::TimeTicks timestamp);
82 
capture_period()83   base::TimeDelta capture_period() const { return capture_period_; }
84 
85  private:
86 
87   // Time between frames.
88   const base::TimeDelta capture_period_;
89 
90   // Incremented every time a paint or update event occurs.
91   int frame_number_;
92 
93   // Stores the frame number from the last delivered frame.
94   int last_delivered_frame_number_;
95 
96   // Stores the timestamp of the last delivered frame.
97   base::TimeTicks last_delivered_frame_timestamp_;
98 
99   // Tracks present/paint history.
100   SmoothEventSampler sampler_;
101 };
102 
103 }  // namespace content
104 
105 #endif  // CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
106