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 | |
8 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
10 #include "base/time/time.h" | 12 #include "base/time/time.h" |
11 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
14 #include "ui/gfx/geometry/rect.h" | |
12 | 15 |
13 namespace content { | 16 namespace content { |
14 | 17 |
15 // Filters a sequence of events to achieve a target frequency. | 18 // Filters a sequence of events to achieve a target frequency. |
16 class CONTENT_EXPORT SmoothEventSampler { | 19 class CONTENT_EXPORT SmoothEventSampler { |
17 public: | 20 public: |
18 explicit SmoothEventSampler(base::TimeDelta capture_period, | 21 SmoothEventSampler(base::TimeDelta capture_period, |
19 bool events_are_reliable, | 22 bool events_are_reliable, |
20 int redundant_capture_goal); | 23 int redundant_capture_goal); |
21 | 24 |
22 // Add a new event to the event history, and return whether it ought to be | 25 // 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 | 26 // sampled based on the desired |capture_period|. The event is not recorded as |
24 // a sample until RecordSample() is called. | 27 // a sample until RecordSample() is called. |
25 bool AddEventAndConsiderSampling(base::TimeTicks event_time); | 28 bool AddEventAndConsiderSampling(base::TimeTicks event_time); |
26 | 29 |
27 // Operates on the last event added by AddEventAndConsiderSampling(), marking | 30 // 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 | 31 // it as sampled. After this point we are current in the stream of events, as |
29 // we have sampled the most recent event. | 32 // we have sampled the most recent event. |
30 void RecordSample(); | 33 void RecordSample(); |
(...skipping 13 matching lines...) Expand all Loading... | |
44 const base::TimeDelta token_bucket_capacity_; | 47 const base::TimeDelta token_bucket_capacity_; |
45 | 48 |
46 base::TimeTicks current_event_; | 49 base::TimeTicks current_event_; |
47 base::TimeTicks last_sample_; | 50 base::TimeTicks last_sample_; |
48 int overdue_sample_count_; | 51 int overdue_sample_count_; |
49 base::TimeDelta token_bucket_; | 52 base::TimeDelta token_bucket_; |
50 | 53 |
51 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler); | 54 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler); |
52 }; | 55 }; |
53 | 56 |
57 // Analyzes a sequence of events to determine whether a subset of the events | |
58 // looks like constant frame rate animated content. Events are grouped into | |
59 // subsets by their reported damage Rect. Then, the largest of the subsets, if | |
ncarter (slow)
2014/07/26 00:57:58
Could you say "most frequently occurring rect" ins
miu
2014/07/31 01:25:32
Done. I rewrote much of the comment to make the w
| |
60 // it is a majority of the events, is examined to estimate the content's frame | |
61 // rate. | |
62 class CONTENT_EXPORT AnimatedContentSampler { | |
63 public: | |
64 explicit AnimatedContentSampler(base::TimeDelta min_capture_period); | |
65 ~AnimatedContentSampler(); | |
66 | |
67 // Examines the given presentation event metadata, along with recent history, | |
68 // to detect animated content, updating the state of this sampler. Returns | |
69 // true if animated content is detected. |damage_rect| is the region of a | |
70 // frame about to be drawn, while |event_time| refers to the frame's estimated | |
71 // presentation time. | |
72 bool ConsiderPresentationEvent(const gfx::Rect& damage_rect, | |
73 base::TimeTicks event_time); | |
74 | |
75 // Returns a "null" TimeTicks if the last event considered should not be | |
76 // sampled. Otherwise, returns a frame timestamp to provide to consumers of | |
77 // the sampled frame. | |
78 base::TimeTicks next_frame_timestamp() const { | |
79 return next_frame_timestamp_; | |
80 } | |
81 | |
82 // Accessors to currently-detected animating region/period, for logging. | |
83 const gfx::Rect& detected_region() const { return detected_region_; } | |
84 base::TimeDelta detected_period() const { return detected_period_; } | |
85 | |
86 // Records that a frame with the given |frame_timestamp| was sampled. This | |
87 // method should be called when *any* sampling is taken, even if it was not | |
88 // proposed by AnimatedContentSampler. | |
89 void RecordSample(base::TimeTicks frame_timestamp); | |
90 | |
91 private: | |
92 // Data structure for efficient online analysis of recent event history. | |
93 typedef std::pair<gfx::Rect, base::TimeTicks> Observation; | |
94 typedef std::deque<Observation> ObservationFifo; | |
95 | |
96 // Adds an observation to |observations_|, and prunes-out the old ones. | |
97 void AddObservation(const gfx::Rect& damage_rect, base::TimeTicks event_time); | |
98 | |
99 // Analyzes the observations relative to the current |event_time| to detect | |
100 // stable animating content. If detected, returns true and sets the output | |
101 // arguments to the region of the animating content and its mean frame | |
102 // duration. | |
103 bool AnalyzeObservations(base::TimeTicks event_time, | |
104 gfx::Rect* rect, | |
105 base::TimeDelta* period) const; | |
106 | |
107 // Updates |next_frame_timestamp_|. This is called by | |
108 // ConsiderPresentationEvent() when the current event is part of a detected | |
109 // animation. | |
110 void UpdateNextFrameTimestamp(base::TimeTicks event_time); | |
111 | |
112 // The client expects frame timestamps to be at least this far apart. | |
113 const base::TimeDelta min_capture_period_; | |
114 | |
115 // A recent history of observations in chronological order, maintained by | |
116 // AddObservation(). | |
117 ObservationFifo observations_; | |
118 | |
119 // The region of currently-detected animated content. If empty, that means | |
120 // "not detected." | |
121 gfx::Rect detected_region_; | |
122 | |
123 // The mean frame duration of currently-detected animated content. If zero, | |
124 // that means "not detected." | |
125 base::TimeDelta detected_period_; | |
126 | |
127 // The timestamp to use for the next frame, updated as each event is | |
128 // considered. If the last event should not be sampled, this is set to a | |
129 // "null" value. | |
130 base::TimeTicks next_frame_timestamp_; | |
131 | |
132 // The frame timestamp provided in the last call to RecordSample(). This | |
133 // timestamp may or may not have been one proposed by AnimatedContentSampler. | |
134 base::TimeTicks recorded_frame_timestamp_; | |
135 | |
136 // Accumulates all the time advancements since the last call to | |
137 // RecordSample(). When this is greater than zero, there have been one or | |
138 // more events proposed for sampling, but not yet recorded. This accounts for | |
139 // the cases where AnimatedContentSampler indicates a frame should be sampled, | |
140 // but the client chooses not to do so. | |
141 base::TimeDelta sequence_offset_; | |
142 | |
143 // A token bucket that is used to decide which frames to drop whenever | |
144 // |detected_period_| is less than |min_capture_period_|. | |
145 base::TimeDelta borrowed_time_; | |
146 }; | |
147 | |
54 // VideoCaptureOracle manages the producer-side throttling of captured frames | 148 // VideoCaptureOracle manages the producer-side throttling of captured frames |
55 // from a video capture device. It is informed of every update by the device; | 149 // 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 | 150 // 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. | 151 // ought to be captured in order to achieve its target frame rate. |
58 class CONTENT_EXPORT VideoCaptureOracle { | 152 class CONTENT_EXPORT VideoCaptureOracle { |
59 public: | 153 public: |
60 enum Event { | 154 enum Event { |
61 kTimerPoll, | 155 kTimerPoll, |
62 kCompositorUpdate, | 156 kCompositorUpdate, |
63 kSoftwarePaint, | 157 kSoftwarePaint, |
158 kNumEvents, | |
64 }; | 159 }; |
65 | 160 |
66 VideoCaptureOracle(base::TimeDelta capture_period, | 161 VideoCaptureOracle(base::TimeDelta min_capture_period, |
67 bool events_are_reliable); | 162 bool events_are_reliable); |
68 virtual ~VideoCaptureOracle() {} | 163 virtual ~VideoCaptureOracle(); |
69 | 164 |
70 // Record an event of type |event|, and decide whether the caller should do a | 165 // Record a event of type |event|, and decide whether the caller should do a |
71 // frame capture immediately. Decisions of the oracle are final: the caller | 166 // frame capture. |damage_rect| is the region of a frame about to be drawn, |
72 // must do what it is told. | 167 // and may be an empty Rect, if this is not known. If the caller accepts the |
73 bool ObserveEventAndDecideCapture(Event event, base::TimeTicks event_time); | 168 // oracle's proposal, it should call RecordCapture() to indicate this. |
169 bool ObserveEventAndDecideCapture(Event event, | |
170 const gfx::Rect& damage_rect, | |
171 base::TimeTicks event_time); | |
74 | 172 |
75 // Record the start of a capture. Returns a frame_number to be used with | 173 // Record the start of a capture. Returns a frame_number to be used with |
76 // CompleteCapture(). | 174 // CompleteCapture(). |
77 int RecordCapture(); | 175 int RecordCapture(); |
78 | 176 |
79 // Record the completion of a capture. Returns true iff the captured frame | 177 // Notify of the completion of a capture. Returns true iff the captured frame |
80 // should be delivered. | 178 // should be delivered. |frame_timestamp| is set to the timestamp that should |
81 bool CompleteCapture(int frame_number, base::TimeTicks timestamp); | 179 // be provided to the consumer of the frame. |
180 bool CompleteCapture(int frame_number, base::TimeTicks* frame_timestamp); | |
82 | 181 |
83 base::TimeDelta capture_period() const { return capture_period_; } | 182 base::TimeDelta min_capture_period() const { return min_capture_period_; } |
84 | 183 |
85 private: | 184 private: |
185 // Retrieve/Assign a frame timestamp by capture |frame_number|. | |
186 base::TimeTicks GetFrameTimestamp(int frame_number) const; | |
187 void SetFrameTimestamp(int frame_number, base::TimeTicks timestamp); | |
86 | 188 |
87 // Time between frames. | 189 // Hard boundary: Minimum time between frames. |
88 const base::TimeDelta capture_period_; | 190 const base::TimeDelta min_capture_period_; |
89 | 191 |
90 // Incremented every time a paint or update event occurs. | 192 // Incremented every time a paint or update event occurs. |
91 int frame_number_; | 193 int frame_number_; |
92 | 194 |
195 // Stores the last |event_time| from the last observation/decision. Used to | |
196 // sanity-check that event times are monotonically non-decreasing. | |
197 base::TimeTicks last_event_time_[kNumEvents]; | |
198 | |
93 // Stores the frame number from the last delivered frame. | 199 // Stores the frame number from the last delivered frame. |
94 int last_delivered_frame_number_; | 200 int last_delivered_frame_number_; |
95 | 201 |
96 // Stores the timestamp of the last delivered frame. | 202 // These track present/paint history and propose whether to sample each event |
97 base::TimeTicks last_delivered_frame_timestamp_; | 203 // for capture. |smoothing_sampler_| uses a "works for all" heuristic, while |
204 // |content_sampler_| specifically detects animated content (e.g., video | |
205 // playback) and decides which events to sample to "lock into" that content. | |
206 SmoothEventSampler smoothing_sampler_; | |
207 AnimatedContentSampler content_sampler_; | |
98 | 208 |
99 // Tracks present/paint history. | 209 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is |
100 SmoothEventSampler sampler_; | 210 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp() |
211 // methods. | |
212 enum { kMaxFrameTimestamps = 16 }; | |
213 base::TimeTicks frame_timestamps_[kMaxFrameTimestamps]; | |
101 }; | 214 }; |
102 | 215 |
103 } // namespace content | 216 } // namespace content |
104 | 217 |
105 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ | 218 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ |
OLD | NEW |