OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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_VIDEO_CAPTURE_ORACLE_H_ |
6 #define CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ | 6 #define CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ |
7 | 7 |
8 #include <deque> | |
9 | |
10 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
11 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
12 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "content/browser/media/capture/animated_content_sampler.h" |
| 12 #include "content/browser/media/capture/smooth_event_sampler.h" |
13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
14 #include "ui/gfx/geometry/rect.h" | 14 #include "ui/gfx/geometry/rect.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
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); | |
23 | |
24 base::TimeDelta min_capture_period() const { return min_capture_period_; } | |
25 | |
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); | |
30 | |
31 // Returns true if the last event considered should be sampled. | |
32 bool ShouldSample() const; | |
33 | |
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(); | |
38 | |
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; | |
42 | |
43 // Returns true if ConsiderPresentationEvent() has been called since the last | |
44 // call to RecordSample(). | |
45 bool HasUnrecordedEvent() const; | |
46 | |
47 private: | |
48 const base::TimeDelta min_capture_period_; | |
49 const int redundant_capture_goal_; | |
50 const base::TimeDelta token_bucket_capacity_; | |
51 | |
52 base::TimeTicks current_event_; | |
53 base::TimeTicks last_sample_; | |
54 int overdue_sample_count_; | |
55 base::TimeDelta token_bucket_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler); | |
58 }; | |
59 | |
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 | 18 // VideoCaptureOracle manages the producer-side throttling of captured frames |
170 // from a video capture device. It is informed of every update by the device; | 19 // 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 | 20 // 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. | 21 // ought to be captured in order to achieve its target frame rate. |
173 class CONTENT_EXPORT VideoCaptureOracle { | 22 class CONTENT_EXPORT VideoCaptureOracle { |
174 public: | 23 public: |
175 enum Event { | 24 enum Event { |
176 kTimerPoll, | 25 kTimerPoll, |
177 kCompositorUpdate, | 26 kCompositorUpdate, |
178 kNumEvents, | 27 kNumEvents, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is | 76 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is |
228 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp() | 77 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp() |
229 // methods. | 78 // methods. |
230 enum { kMaxFrameTimestamps = 16 }; | 79 enum { kMaxFrameTimestamps = 16 }; |
231 base::TimeTicks frame_timestamps_[kMaxFrameTimestamps]; | 80 base::TimeTicks frame_timestamps_[kMaxFrameTimestamps]; |
232 }; | 81 }; |
233 | 82 |
234 } // namespace content | 83 } // namespace content |
235 | 84 |
236 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ | 85 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ |
OLD | NEW |