Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / media / capture / video_capture_oracle.h
blobdb98b49e571dfe89b0d6d614204aaea9127d47d4
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.
5 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
6 #define CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
8 #include <deque>
10 #include "base/callback_forward.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "content/common/content_export.h"
14 #include "ui/gfx/geometry/rect.h"
16 namespace content {
18 // Filters a sequence of events to achieve a target frequency.
19 class CONTENT_EXPORT SmoothEventSampler {
20 public:
21 SmoothEventSampler(base::TimeDelta min_capture_period,
22 int redundant_capture_goal);
24 base::TimeDelta min_capture_period() const { return min_capture_period_; }
26 // Add a new event to the event history, and consider whether it ought to be
27 // sampled. The event is not recorded as a sample until RecordSample() is
28 // called.
29 void ConsiderPresentationEvent(base::TimeTicks event_time);
31 // Returns true if the last event considered should be sampled.
32 bool ShouldSample() const;
34 // Operates on the last event added by ConsiderPresentationEvent(), marking
35 // it as sampled. After this point we are current in the stream of events, as
36 // we have sampled the most recent event.
37 void RecordSample();
39 // Returns true if, at time |event_time|, sampling should occur because too
40 // much time will have passed relative to the last event and/or sample.
41 bool IsOverdueForSamplingAt(base::TimeTicks event_time) const;
43 // Returns true if ConsiderPresentationEvent() has been called since the last
44 // call to RecordSample().
45 bool HasUnrecordedEvent() const;
47 private:
48 const base::TimeDelta min_capture_period_;
49 const int redundant_capture_goal_;
50 const base::TimeDelta token_bucket_capacity_;
52 base::TimeTicks current_event_;
53 base::TimeTicks last_sample_;
54 int overdue_sample_count_;
55 base::TimeDelta token_bucket_;
57 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler);
60 // Analyzes a sequence of events to detect the presence of constant frame rate
61 // animated content. In the case where there are multiple regions of animated
62 // content, AnimatedContentSampler will propose sampling the one having the
63 // largest "smoothness" impact, according to human perception (e.g., a 24 FPS
64 // video versus a 60 FPS busy spinner).
66 // In addition, AnimatedContentSampler will provide rewritten frame timestamps,
67 // for downstream consumers, that are "truer" to the source content than to the
68 // local presentation hardware.
69 class CONTENT_EXPORT AnimatedContentSampler {
70 public:
71 explicit AnimatedContentSampler(base::TimeDelta min_capture_period);
72 ~AnimatedContentSampler();
74 // Examines the given presentation event metadata, along with recent history,
75 // to detect animated content, updating the state of this sampler.
76 // |damage_rect| is the region of a frame about to be drawn, while
77 // |event_time| refers to the frame's estimated presentation time.
78 void ConsiderPresentationEvent(const gfx::Rect& damage_rect,
79 base::TimeTicks event_time);
81 // Returns true if animated content has been detected and a decision has been
82 // made about whether to sample the last event.
83 bool HasProposal() const;
85 // Returns true if the last event considered should be sampled.
86 bool ShouldSample() const;
88 // Returns a frame timestamp to provide to consumers of the sampled frame.
89 // Only valid when should_sample() returns true.
90 base::TimeTicks frame_timestamp() const { return frame_timestamp_; }
92 // Accessors to currently-detected animating region/period, for logging.
93 const gfx::Rect& detected_region() const { return detected_region_; }
94 base::TimeDelta detected_period() const { return detected_period_; }
96 // Records that a frame with the given |frame_timestamp| was sampled. This
97 // method should be called when *any* sampling is taken, even if it was not
98 // proposed by AnimatedContentSampler.
99 void RecordSample(base::TimeTicks frame_timestamp);
101 private:
102 friend class AnimatedContentSamplerTest;
104 // Data structure for efficient online analysis of recent event history.
105 struct Observation {
106 gfx::Rect damage_rect;
107 base::TimeTicks event_time;
109 Observation(const gfx::Rect& d, base::TimeTicks e)
110 : damage_rect(d), event_time(e) {}
112 typedef std::deque<Observation> ObservationFifo;
114 // Adds an observation to |observations_|, and prunes-out the old ones.
115 void AddObservation(const gfx::Rect& damage_rect, base::TimeTicks event_time);
117 // Returns the damage Rect that is responsible for the majority of the pixel
118 // damage in recent event history, if there is such a Rect. If there isn't,
119 // this method could still return any Rect, so the caller must confirm the
120 // returned Rect really is responsible for the majority of pixel damage.
121 gfx::Rect ElectMajorityDamageRect() const;
123 // Analyzes the observations relative to the current |event_time| to detect
124 // stable animating content. If detected, returns true and sets the output
125 // arguments to the region of the animating content and its mean frame
126 // duration.
127 bool AnalyzeObservations(base::TimeTicks event_time,
128 gfx::Rect* rect,
129 base::TimeDelta* period) const;
131 // Called by ConsiderPresentationEvent() when the current event is part of a
132 // detected animation, to update |frame_timestamp_|.
133 void UpdateFrameTimestamp(base::TimeTicks event_time);
135 // The client expects frame timestamps to be at least this far apart.
136 const base::TimeDelta min_capture_period_;
138 // A recent history of observations in chronological order, maintained by
139 // AddObservation().
140 ObservationFifo observations_;
142 // The region of currently-detected animated content. If empty, that means
143 // "not detected."
144 gfx::Rect detected_region_;
146 // The mean frame duration of currently-detected animated content. If zero,
147 // that means "not detected."
148 base::TimeDelta detected_period_;
150 // The rewritten frame timestamp for the latest event.
151 base::TimeTicks frame_timestamp_;
153 // The frame timestamp provided in the last call to RecordSample(). This
154 // timestamp may or may not have been one proposed by AnimatedContentSampler.
155 base::TimeTicks recorded_frame_timestamp_;
157 // Accumulates all the time advancements since the last call to
158 // RecordSample(). When this is greater than zero, there have been one or
159 // more events proposed for sampling, but not yet recorded. This accounts for
160 // the cases where AnimatedContentSampler indicates a frame should be sampled,
161 // but the client chooses not to do so.
162 base::TimeDelta sequence_offset_;
164 // A token bucket that is used to decide which frames to drop whenever
165 // |detected_period_| is less than |min_capture_period_|.
166 base::TimeDelta borrowed_time_;
169 // VideoCaptureOracle manages the producer-side throttling of captured frames
170 // from a video capture device. It is informed of every update by the device;
171 // this empowers it to look into the future and decide if a particular frame
172 // ought to be captured in order to achieve its target frame rate.
173 class CONTENT_EXPORT VideoCaptureOracle {
174 public:
175 enum Event {
176 kTimerPoll,
177 kCompositorUpdate,
178 kNumEvents,
181 explicit VideoCaptureOracle(base::TimeDelta min_capture_period);
182 virtual ~VideoCaptureOracle();
184 // Record a event of type |event|, and decide whether the caller should do a
185 // frame capture. |damage_rect| is the region of a frame about to be drawn,
186 // and may be an empty Rect, if this is not known. If the caller accepts the
187 // oracle's proposal, it should call RecordCapture() to indicate this.
188 bool ObserveEventAndDecideCapture(Event event,
189 const gfx::Rect& damage_rect,
190 base::TimeTicks event_time);
192 // Record the start of a capture. Returns a frame_number to be used with
193 // CompleteCapture().
194 int RecordCapture();
196 // Notify of the completion of a capture. Returns true iff the captured frame
197 // should be delivered. |frame_timestamp| is set to the timestamp that should
198 // be provided to the consumer of the frame.
199 bool CompleteCapture(int frame_number, base::TimeTicks* frame_timestamp);
201 base::TimeDelta min_capture_period() const {
202 return smoothing_sampler_.min_capture_period();
205 private:
206 // Retrieve/Assign a frame timestamp by capture |frame_number|.
207 base::TimeTicks GetFrameTimestamp(int frame_number) const;
208 void SetFrameTimestamp(int frame_number, base::TimeTicks timestamp);
210 // Incremented every time a paint or update event occurs.
211 int frame_number_;
213 // Stores the last |event_time| from the last observation/decision. Used to
214 // sanity-check that event times are monotonically non-decreasing.
215 base::TimeTicks last_event_time_[kNumEvents];
217 // Stores the frame number from the last delivered frame.
218 int last_delivered_frame_number_;
220 // These track present/paint history and propose whether to sample each event
221 // for capture. |smoothing_sampler_| uses a "works for all" heuristic, while
222 // |content_sampler_| specifically detects animated content (e.g., video
223 // playback) and decides which events to sample to "lock into" that content.
224 SmoothEventSampler smoothing_sampler_;
225 AnimatedContentSampler content_sampler_;
227 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is
228 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp()
229 // methods.
230 enum { kMaxFrameTimestamps = 16 };
231 base::TimeTicks frame_timestamps_[kMaxFrameTimestamps];
234 } // namespace content
236 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_