OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ | 5 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_SMOOTH_EVENT_SAMPLER_H_ |
6 #define CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ | 6 #define CONTENT_BROWSER_MEDIA_CAPTURE_SMOOTH_EVENT_SAMPLER_H_ |
7 | 7 |
8 #include <deque> | |
9 | |
10 #include "base/callback_forward.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/time/time.h" | 8 #include "base/time/time.h" |
13 #include "content/common/content_export.h" | 9 #include "content/common/content_export.h" |
14 #include "ui/gfx/geometry/rect.h" | |
15 | 10 |
16 namespace content { | 11 namespace content { |
17 | 12 |
18 // Filters a sequence of events to achieve a target frequency. | 13 // Filters a sequence of events to achieve a target frequency. |
19 class CONTENT_EXPORT SmoothEventSampler { | 14 class CONTENT_EXPORT SmoothEventSampler { |
20 public: | 15 public: |
21 SmoothEventSampler(base::TimeDelta min_capture_period, | 16 SmoothEventSampler(base::TimeDelta min_capture_period, |
22 int redundant_capture_goal); | 17 int redundant_capture_goal); |
23 | 18 |
24 base::TimeDelta min_capture_period() const { return min_capture_period_; } | 19 base::TimeDelta min_capture_period() const { return min_capture_period_; } |
(...skipping 25 matching lines...) Expand all Loading... |
50 const base::TimeDelta token_bucket_capacity_; | 45 const base::TimeDelta token_bucket_capacity_; |
51 | 46 |
52 base::TimeTicks current_event_; | 47 base::TimeTicks current_event_; |
53 base::TimeTicks last_sample_; | 48 base::TimeTicks last_sample_; |
54 int overdue_sample_count_; | 49 int overdue_sample_count_; |
55 base::TimeDelta token_bucket_; | 50 base::TimeDelta token_bucket_; |
56 | 51 |
57 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler); | 52 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler); |
58 }; | 53 }; |
59 | 54 |
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). | |
65 // | |
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(); | |
73 | |
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); | |
80 | |
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; | |
84 | |
85 // Returns true if the last event considered should be sampled. | |
86 bool ShouldSample() const; | |
87 | |
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_; } | |
91 | |
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_; } | |
95 | |
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); | |
100 | |
101 private: | |
102 friend class AnimatedContentSamplerTest; | |
103 | |
104 // Data structure for efficient online analysis of recent event history. | |
105 struct Observation { | |
106 gfx::Rect damage_rect; | |
107 base::TimeTicks event_time; | |
108 | |
109 Observation(const gfx::Rect& d, base::TimeTicks e) | |
110 : damage_rect(d), event_time(e) {} | |
111 }; | |
112 typedef std::deque<Observation> ObservationFifo; | |
113 | |
114 // Adds an observation to |observations_|, and prunes-out the old ones. | |
115 void AddObservation(const gfx::Rect& damage_rect, base::TimeTicks event_time); | |
116 | |
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; | |
122 | |
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; | |
130 | |
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); | |
134 | |
135 // The client expects frame timestamps to be at least this far apart. | |
136 const base::TimeDelta min_capture_period_; | |
137 | |
138 // A recent history of observations in chronological order, maintained by | |
139 // AddObservation(). | |
140 ObservationFifo observations_; | |
141 | |
142 // The region of currently-detected animated content. If empty, that means | |
143 // "not detected." | |
144 gfx::Rect detected_region_; | |
145 | |
146 // The mean frame duration of currently-detected animated content. If zero, | |
147 // that means "not detected." | |
148 base::TimeDelta detected_period_; | |
149 | |
150 // The rewritten frame timestamp for the latest event. | |
151 base::TimeTicks frame_timestamp_; | |
152 | |
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_; | |
156 | |
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_; | |
163 | |
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_; | |
167 }; | |
168 | |
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, | |
179 }; | |
180 | |
181 explicit VideoCaptureOracle(base::TimeDelta min_capture_period); | |
182 virtual ~VideoCaptureOracle(); | |
183 | |
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); | |
191 | |
192 // Record the start of a capture. Returns a frame_number to be used with | |
193 // CompleteCapture(). | |
194 int RecordCapture(); | |
195 | |
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); | |
200 | |
201 base::TimeDelta min_capture_period() const { | |
202 return smoothing_sampler_.min_capture_period(); | |
203 } | |
204 | |
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); | |
209 | |
210 // Incremented every time a paint or update event occurs. | |
211 int frame_number_; | |
212 | |
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]; | |
216 | |
217 // Stores the frame number from the last delivered frame. | |
218 int last_delivered_frame_number_; | |
219 | |
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_; | |
226 | |
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]; | |
232 }; | |
233 | |
234 } // namespace content | 55 } // namespace content |
235 | 56 |
236 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ | 57 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_SMOOTH_EVENT_SAMPLER_H_ |
OLD | NEW |