OLD | NEW |
1 // Copyright (c) 2015 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_ANIMATED_CONTENT_SAMPLER_H_ | 5 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_ANIMATED_CONTENT_SAMPLER_H_ |
6 #define CONTENT_BROWSER_MEDIA_CAPTURE_ANIMATED_CONTENT_SAMPLER_H_ | 6 #define CONTENT_BROWSER_MEDIA_CAPTURE_ANIMATED_CONTENT_SAMPLER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
12 #include "ui/gfx/geometry/rect.h" | 12 #include "ui/gfx/geometry/rect.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 // Analyzes a sequence of events to detect the presence of constant frame rate | 16 // Analyzes a sequence of events to detect the presence of constant frame rate |
17 // animated content. In the case where there are multiple regions of animated | 17 // animated content. In the case where there are multiple regions of animated |
18 // content, AnimatedContentSampler will propose sampling the one having the | 18 // content, AnimatedContentSampler will propose sampling the one having the |
19 // largest "smoothness" impact, according to human perception (e.g., a 24 FPS | 19 // largest "smoothness" impact, according to human perception (e.g., a 24 FPS |
20 // video versus a 60 FPS busy spinner). | 20 // video versus a 60 FPS busy spinner). |
21 // | 21 // |
22 // In addition, AnimatedContentSampler will provide rewritten frame timestamps, | 22 // In addition, AnimatedContentSampler will provide rewritten frame timestamps, |
23 // for downstream consumers, that are "truer" to the source content than to the | 23 // for downstream consumers, that are "truer" to the source content than to the |
24 // local presentation hardware. | 24 // local presentation hardware. |
25 class CONTENT_EXPORT AnimatedContentSampler { | 25 class CONTENT_EXPORT AnimatedContentSampler { |
26 public: | 26 public: |
27 explicit AnimatedContentSampler(base::TimeDelta min_capture_period); | 27 explicit AnimatedContentSampler(base::TimeDelta min_capture_period); |
28 ~AnimatedContentSampler(); | 28 ~AnimatedContentSampler(); |
29 | 29 |
| 30 // Get/Set the target sampling period. This is used to determine whether to |
| 31 // subsample the frames of animated content. |
| 32 base::TimeDelta target_sampling_period() const { |
| 33 return target_sampling_period_; |
| 34 } |
| 35 void SetTargetSamplingPeriod(base::TimeDelta period); |
| 36 |
30 // Examines the given presentation event metadata, along with recent history, | 37 // Examines the given presentation event metadata, along with recent history, |
31 // to detect animated content, updating the state of this sampler. | 38 // to detect animated content, updating the state of this sampler. |
32 // |damage_rect| is the region of a frame about to be drawn, while | 39 // |damage_rect| is the region of a frame about to be drawn, while |
33 // |event_time| refers to the frame's estimated presentation time. | 40 // |event_time| refers to the frame's estimated presentation time. |
34 void ConsiderPresentationEvent(const gfx::Rect& damage_rect, | 41 void ConsiderPresentationEvent(const gfx::Rect& damage_rect, |
35 base::TimeTicks event_time); | 42 base::TimeTicks event_time); |
36 | 43 |
37 // Returns true if animated content has been detected and a decision has been | 44 // Returns true if animated content has been detected and a decision has been |
38 // made about whether to sample the last event. | 45 // made about whether to sample the last event. |
39 bool HasProposal() const; | 46 bool HasProposal() const; |
40 | 47 |
41 // Returns true if the last event considered should be sampled. | 48 // Returns true if the last event considered should be sampled. |
42 bool ShouldSample() const; | 49 bool ShouldSample() const; |
43 | 50 |
44 // Returns a frame timestamp to provide to consumers of the sampled frame. | 51 // Returns a frame timestamp to provide to consumers of the sampled frame. |
45 // Only valid when should_sample() returns true. | 52 // Only valid when ShouldSample() returns true. |
46 base::TimeTicks frame_timestamp() const { return frame_timestamp_; } | 53 base::TimeTicks frame_timestamp() const { return frame_timestamp_; } |
47 | 54 |
| 55 // Returns the current sampling period. This can be treated as the estimated |
| 56 // duration of the frame to be sampled. Only valid when HasProposal() |
| 57 // returns true. |
| 58 base::TimeDelta sampling_period() const { return sampling_period_; } |
| 59 |
48 // Accessors to currently-detected animating region/period, for logging. | 60 // Accessors to currently-detected animating region/period, for logging. |
49 const gfx::Rect& detected_region() const { return detected_region_; } | 61 const gfx::Rect& detected_region() const { return detected_region_; } |
50 base::TimeDelta detected_period() const { return detected_period_; } | 62 base::TimeDelta detected_period() const { return detected_period_; } |
51 | 63 |
52 // Records that a frame with the given |frame_timestamp| was sampled. This | 64 // Records that a frame with the given |frame_timestamp| was sampled. This |
53 // method should be called when *any* sampling is taken, even if it was not | 65 // method should be called when *any* sampling is taken, even if it was not |
54 // proposed by AnimatedContentSampler. | 66 // proposed by AnimatedContentSampler. |
55 void RecordSample(base::TimeTicks frame_timestamp); | 67 void RecordSample(base::TimeTicks frame_timestamp); |
56 | 68 |
57 private: | 69 private: |
(...skipping 21 matching lines...) Expand all Loading... |
79 // Analyzes the observations relative to the current |event_time| to detect | 91 // Analyzes the observations relative to the current |event_time| to detect |
80 // stable animating content. If detected, returns true and sets the output | 92 // stable animating content. If detected, returns true and sets the output |
81 // arguments to the region of the animating content and its mean frame | 93 // arguments to the region of the animating content and its mean frame |
82 // duration. | 94 // duration. |
83 bool AnalyzeObservations(base::TimeTicks event_time, | 95 bool AnalyzeObservations(base::TimeTicks event_time, |
84 gfx::Rect* rect, | 96 gfx::Rect* rect, |
85 base::TimeDelta* period) const; | 97 base::TimeDelta* period) const; |
86 | 98 |
87 // Called by ConsiderPresentationEvent() when the current event is part of a | 99 // Called by ConsiderPresentationEvent() when the current event is part of a |
88 // detected animation, to update |frame_timestamp_|. | 100 // detected animation, to update |frame_timestamp_|. |
89 void UpdateFrameTimestamp(base::TimeTicks event_time); | 101 base::TimeTicks ComputeNextFrameTimestamp(base::TimeTicks event_time) const; |
| 102 |
| 103 // When the animation frame rate is greater than the target sampling rate, |
| 104 // this function determines an integer division of the animation frame rate |
| 105 // that is closest to the target sampling rate. Returns the inverse of that |
| 106 // result (the period). If the animation frame rate is slower or the same as |
| 107 // the target sampling rate, this function just returns |animation_period|. |
| 108 static base::TimeDelta ComputeSamplingPeriod( |
| 109 base::TimeDelta animation_period, |
| 110 base::TimeDelta target_sampling_period, |
| 111 base::TimeDelta min_capture_period); |
90 | 112 |
91 // The client expects frame timestamps to be at least this far apart. | 113 // The client expects frame timestamps to be at least this far apart. |
92 const base::TimeDelta min_capture_period_; | 114 const base::TimeDelta min_capture_period_; |
93 | 115 |
94 // A recent history of observations in chronological order, maintained by | 116 // A recent history of observations in chronological order, maintained by |
95 // AddObservation(). | 117 // AddObservation(). |
96 ObservationFifo observations_; | 118 ObservationFifo observations_; |
97 | 119 |
98 // The region of currently-detected animated content. If empty, that means | 120 // The region of currently-detected animated content. If empty, that means |
99 // "not detected." | 121 // "not detected." |
100 gfx::Rect detected_region_; | 122 gfx::Rect detected_region_; |
101 | 123 |
102 // The mean frame duration of currently-detected animated content. If zero, | 124 // The mean frame duration of currently-detected animated content. If zero, |
103 // that means "not detected." | 125 // that means "not detected." |
104 base::TimeDelta detected_period_; | 126 base::TimeDelta detected_period_; |
105 | 127 |
| 128 // Target period between sampled frames. This can be changed by the client at |
| 129 // any time (e.g., to sample high frame rate content at a lower rate). |
| 130 base::TimeDelta target_sampling_period_; |
| 131 |
| 132 // The sampling period computed during the last call to |
| 133 // ConsiderPresentationEvent(). |
| 134 base::TimeDelta sampling_period_; |
| 135 |
| 136 // Indicates whether the last event caused animated content to be detected and |
| 137 // whether the current event should be sampled. |
| 138 enum { |
| 139 NOT_SAMPLING, |
| 140 START_SAMPLING, |
| 141 SHOULD_NOT_SAMPLE, |
| 142 SHOULD_SAMPLE |
| 143 } sampling_state_; |
| 144 |
| 145 // A token bucket that is used to decide which subset of the frames containing |
| 146 // the animated content should be sampled. Here, the smallest discrete unit |
| 147 // of time (one microsecond) equals one token; and, tokens are only taken from |
| 148 // the bucket when at least a full sampling period's worth are present. |
| 149 base::TimeDelta token_bucket_; |
| 150 |
106 // The rewritten frame timestamp for the latest event. | 151 // The rewritten frame timestamp for the latest event. |
107 base::TimeTicks frame_timestamp_; | 152 base::TimeTicks frame_timestamp_; |
108 | |
109 // The frame timestamp provided in the last call to RecordSample(). This | |
110 // timestamp may or may not have been one proposed by AnimatedContentSampler. | |
111 base::TimeTicks recorded_frame_timestamp_; | |
112 | |
113 // Accumulates all the time advancements since the last call to | |
114 // RecordSample(). When this is greater than zero, there have been one or | |
115 // more events proposed for sampling, but not yet recorded. This accounts for | |
116 // the cases where AnimatedContentSampler indicates a frame should be sampled, | |
117 // but the client chooses not to do so. | |
118 base::TimeDelta sequence_offset_; | |
119 | |
120 // A token bucket that is used to decide which frames to drop whenever | |
121 // |detected_period_| is less than |min_capture_period_|. | |
122 base::TimeDelta borrowed_time_; | |
123 }; | 153 }; |
124 | 154 |
125 } // namespace content | 155 } // namespace content |
126 | 156 |
127 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_ANIMATED_CONTENT_SAMPLER_H_ | 157 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_ANIMATED_CONTENT_SAMPLER_H_ |
OLD | NEW |