Chromium Code Reviews| 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 #include "content/browser/media/capture/video_capture_oracle.h" | 5 #include "content/browser/media/capture/video_capture_oracle.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 | |
| 7 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/format_macros.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 8 | 12 |
| 9 namespace content { | 13 namespace content { |
| 10 | 14 |
| 11 namespace { | 15 namespace { |
| 12 | 16 |
| 13 // This value controls how many redundant, timer-base captures occur when the | 17 // This value controls how many redundant, timer-base captures occur when the |
| 14 // content is static. Redundantly capturing the same frame allows iterative | 18 // content is static. Redundantly capturing the same frame allows iterative |
| 15 // quality enhancement, and also allows the buffer to fill in "buffered mode". | 19 // quality enhancement, and also allows the buffer to fill in "buffered mode". |
| 16 // | 20 // |
| 17 // TODO(nick): Controlling this here is a hack and a layering violation, since | 21 // TODO(nick): Controlling this here is a hack and a layering violation, since |
| 18 // it's a strategy specific to the WebRTC consumer, and probably just papers | 22 // it's a strategy specific to the WebRTC consumer, and probably just papers |
| 19 // over some frame dropping and quality bugs. It should either be controlled at | 23 // over some frame dropping and quality bugs. It should either be controlled at |
| 20 // a higher level, or else redundant frame generation should be pushed down | 24 // a higher level, or else redundant frame generation should be pushed down |
| 21 // further into the WebRTC encoding stack. | 25 // further into the WebRTC encoding stack. |
| 22 const int kNumRedundantCapturesOfStaticContent = 200; | 26 const int kNumRedundantCapturesOfStaticContent = 200; |
| 23 | 27 |
| 28 // These specify the minimum/maximum amount of recent event history to examine | |
| 29 // to detect animated content. If the values are too low, there is a greater | |
| 30 // risk of false-positive detections and low accuracy. If they are too high, | |
| 31 // the the implementation will be slow to lock-in/out, and also will not react | |
| 32 // well to mildly-variable frame rate content (e.g., 25 +/- 1 FPS). | |
| 33 // | |
| 34 // These values were established by experimenting with a wide variety of | |
| 35 // scenarios, including 24/25/30 FPS videos, 60 FPS WebGL demos, and the | |
| 36 // transitions between static and animated content. | |
| 37 const int kMinObservationWindowMillis = 1000; | |
| 38 const int kMaxObservationWindowMillis = 2000; | |
| 39 | |
| 40 // The maximum amount of time that can elapse before declaring two subsequent | |
| 41 // events as "not animating." This is the same value found in | |
| 42 // cc::FrameRateCounter. | |
| 43 const int kNonAnimatingThresholdMillis = 250; // 4 FPS | |
| 44 | |
| 45 // The slowest that content can be animating in order for AnimatedContentSampler | |
| 46 // to lock-in. This is the threshold at which the "smoothness" problem is no | |
| 47 // longer relevant. | |
| 48 const int kMaxLockInPeriodMicros = 83333; // 12 FPS | |
| 49 | |
| 50 // The amount of time over which to fully correct the drift of the rewritten | |
| 51 // frame timestamps from the presentation event timestamps. The lower the | |
| 52 // value, the higher the variance in frame timestamps. | |
| 53 const int kDriftCorrectionMillis = 6000; | |
| 54 | |
| 55 // Given the amount of time between frames, compare to the expected amount of | |
| 56 // time between frames at |frame_rate| and return the fractional difference. | |
| 57 double FractionFromExpectedFrameRate(base::TimeDelta delta, int frame_rate) { | |
| 58 DCHECK_GT(frame_rate, 0); | |
| 59 const base::TimeDelta expected_delta = | |
| 60 base::TimeDelta::FromSeconds(1) / frame_rate; | |
| 61 return (delta - expected_delta).InMillisecondsF() / | |
| 62 expected_delta.InMillisecondsF(); | |
| 63 } | |
| 64 | |
| 24 } // anonymous namespace | 65 } // anonymous namespace |
| 25 | 66 |
| 26 VideoCaptureOracle::VideoCaptureOracle(base::TimeDelta capture_period, | 67 VideoCaptureOracle::VideoCaptureOracle(base::TimeDelta min_capture_period, |
| 27 bool events_are_reliable) | 68 bool events_are_reliable) |
| 28 : capture_period_(capture_period), | 69 : frame_number_(0), |
| 29 frame_number_(0), | 70 last_delivered_frame_number_(-1), |
| 30 last_delivered_frame_number_(0), | 71 smoothing_sampler_(min_capture_period, |
| 31 sampler_(capture_period_, | 72 events_are_reliable, |
| 32 events_are_reliable, | 73 kNumRedundantCapturesOfStaticContent), |
| 33 kNumRedundantCapturesOfStaticContent) {} | 74 content_sampler_(min_capture_period) { |
| 75 } | |
| 76 | |
| 77 VideoCaptureOracle::~VideoCaptureOracle() {} | |
| 34 | 78 |
| 35 bool VideoCaptureOracle::ObserveEventAndDecideCapture( | 79 bool VideoCaptureOracle::ObserveEventAndDecideCapture( |
| 36 Event event, | 80 Event event, |
| 81 const gfx::Rect& damage_rect, | |
| 37 base::TimeTicks event_time) { | 82 base::TimeTicks event_time) { |
| 38 // Record |event| and decide whether it's a good time to capture. | 83 DCHECK_GE(event, 0); |
| 39 const bool content_is_dirty = (event == kCompositorUpdate || | 84 DCHECK_LT(event, kNumEvents); |
| 40 event == kSoftwarePaint); | 85 if (event_time < last_event_time_[event]) { |
| 86 LOG(WARNING) << "Event time is not monotonically non-decreasing. " | |
| 87 << "Deciding not to capture this frame."; | |
| 88 return false; | |
| 89 } | |
| 90 last_event_time_[event] = event_time; | |
| 91 | |
| 41 bool should_sample; | 92 bool should_sample; |
| 42 if (content_is_dirty) { | 93 switch (event) { |
| 43 frame_number_++; | 94 case kCompositorUpdate: |
| 44 should_sample = sampler_.AddEventAndConsiderSampling(event_time); | 95 case kSoftwarePaint: |
| 45 } else { | 96 smoothing_sampler_.ConsiderPresentationEvent(event_time); |
| 46 should_sample = sampler_.IsOverdueForSamplingAt(event_time); | 97 content_sampler_.ConsiderPresentationEvent(damage_rect, event_time); |
| 98 if (content_sampler_.has_proposal()) { | |
| 99 should_sample = content_sampler_.should_sample(); | |
| 100 if (should_sample) | |
| 101 event_time = content_sampler_.frame_timestamp(); | |
| 102 } else { | |
| 103 should_sample = smoothing_sampler_.should_sample(); | |
|
ncarter (slow)
2014/08/01 23:36:39
Much better, thanks!
| |
| 104 } | |
| 105 break; | |
| 106 default: | |
| 107 should_sample = smoothing_sampler_.IsOverdueForSamplingAt(event_time); | |
| 108 break; | |
| 47 } | 109 } |
| 110 | |
| 111 SetFrameTimestamp(frame_number_, event_time); | |
| 48 return should_sample; | 112 return should_sample; |
| 49 } | 113 } |
| 50 | 114 |
| 51 int VideoCaptureOracle::RecordCapture() { | 115 int VideoCaptureOracle::RecordCapture() { |
| 52 sampler_.RecordSample(); | 116 smoothing_sampler_.RecordSample(); |
| 53 return frame_number_; | 117 content_sampler_.RecordSample(GetFrameTimestamp(frame_number_)); |
| 118 return frame_number_++; | |
| 54 } | 119 } |
| 55 | 120 |
| 56 bool VideoCaptureOracle::CompleteCapture(int frame_number, | 121 bool VideoCaptureOracle::CompleteCapture(int frame_number, |
| 57 base::TimeTicks timestamp) { | 122 base::TimeTicks* frame_timestamp) { |
| 58 // Drop frame if previous frame number is higher or we're trying to deliver | 123 // Drop frame if previous frame number is higher. |
| 59 // a frame with the same timestamp. | 124 if (last_delivered_frame_number_ > frame_number) { |
| 60 if (last_delivered_frame_number_ > frame_number || | 125 LOG(WARNING) << "Out of order frame delivery detected. Dropping frame."; |
| 61 last_delivered_frame_timestamp_ == timestamp) { | |
| 62 LOG(ERROR) << "Frame with same timestamp or out of order delivery. " | |
| 63 << "Dropping frame."; | |
| 64 return false; | 126 return false; |
| 65 } | 127 } |
| 128 last_delivered_frame_number_ = frame_number; | |
| 66 | 129 |
| 67 if (last_delivered_frame_timestamp_ > timestamp) { | 130 *frame_timestamp = GetFrameTimestamp(frame_number); |
| 68 // We should not get here unless time was adjusted backwards. | 131 |
| 69 LOG(ERROR) << "Frame with past timestamp (" << timestamp.ToInternalValue() | 132 // If enabled, log a measurement of how this frame timestamp has incremented |
| 70 << ") was delivered"; | 133 // in relation to an ideal increment. |
| 134 if (VLOG_IS_ON(2) && frame_number > 0) { | |
| 135 const base::TimeDelta delta = | |
| 136 *frame_timestamp - GetFrameTimestamp(frame_number - 1); | |
| 137 if (content_sampler_.has_proposal()) { | |
| 138 const double estimated_frame_rate = | |
| 139 1000000.0 / content_sampler_.detected_period().InMicroseconds(); | |
| 140 const int rounded_frame_rate = | |
| 141 static_cast<int>(estimated_frame_rate + 0.5); | |
| 142 VLOG(2) << base::StringPrintf( | |
| 143 "Captured #%d: delta=%" PRId64 " usec" | |
| 144 ", now locked into {%s}, %+0.1f%% slower than %d FPS", | |
| 145 frame_number, | |
| 146 delta.InMicroseconds(), | |
| 147 content_sampler_.detected_region().ToString().c_str(), | |
| 148 100.0 * FractionFromExpectedFrameRate(delta, rounded_frame_rate), | |
| 149 rounded_frame_rate); | |
| 150 } else { | |
| 151 VLOG(2) << base::StringPrintf( | |
| 152 "Captured #%d: delta=%" PRId64 " usec" | |
| 153 ", d/30fps=%+0.1f%%, d/25fps=%+0.1f%%, d/24fps=%+0.1f%%", | |
| 154 frame_number, | |
| 155 delta.InMicroseconds(), | |
| 156 100.0 * FractionFromExpectedFrameRate(delta, 30), | |
| 157 100.0 * FractionFromExpectedFrameRate(delta, 25), | |
| 158 100.0 * FractionFromExpectedFrameRate(delta, 24)); | |
| 159 } | |
| 71 } | 160 } |
| 72 | 161 |
| 73 last_delivered_frame_number_ = frame_number; | 162 return !frame_timestamp->is_null(); |
| 74 last_delivered_frame_timestamp_ = timestamp; | |
| 75 | |
| 76 return true; | |
| 77 } | 163 } |
| 78 | 164 |
| 79 SmoothEventSampler::SmoothEventSampler(base::TimeDelta capture_period, | 165 base::TimeTicks VideoCaptureOracle::GetFrameTimestamp(int frame_number) const { |
| 166 DCHECK_LE(frame_number, frame_number_); | |
| 167 DCHECK_LT(frame_number_ - frame_number, kMaxFrameTimestamps); | |
| 168 return frame_timestamps_[frame_number % kMaxFrameTimestamps]; | |
| 169 } | |
| 170 | |
| 171 void VideoCaptureOracle::SetFrameTimestamp(int frame_number, | |
| 172 base::TimeTicks timestamp) { | |
| 173 frame_timestamps_[frame_number % kMaxFrameTimestamps] = timestamp; | |
| 174 } | |
| 175 | |
| 176 SmoothEventSampler::SmoothEventSampler(base::TimeDelta min_capture_period, | |
| 80 bool events_are_reliable, | 177 bool events_are_reliable, |
| 81 int redundant_capture_goal) | 178 int redundant_capture_goal) |
| 82 : events_are_reliable_(events_are_reliable), | 179 : events_are_reliable_(events_are_reliable), |
| 83 capture_period_(capture_period), | 180 min_capture_period_(min_capture_period), |
| 84 redundant_capture_goal_(redundant_capture_goal), | 181 redundant_capture_goal_(redundant_capture_goal), |
| 85 token_bucket_capacity_(capture_period + capture_period / 2), | 182 token_bucket_capacity_(min_capture_period + min_capture_period / 2), |
| 86 overdue_sample_count_(0), | 183 overdue_sample_count_(0), |
| 87 token_bucket_(token_bucket_capacity_) { | 184 token_bucket_(token_bucket_capacity_) { |
| 88 DCHECK_GT(capture_period_.InMicroseconds(), 0); | 185 DCHECK_GT(min_capture_period_.InMicroseconds(), 0); |
| 89 } | 186 } |
| 90 | 187 |
| 91 bool SmoothEventSampler::AddEventAndConsiderSampling( | 188 void SmoothEventSampler::ConsiderPresentationEvent(base::TimeTicks event_time) { |
| 92 base::TimeTicks event_time) { | |
| 93 DCHECK(!event_time.is_null()); | 189 DCHECK(!event_time.is_null()); |
| 94 | 190 |
| 95 // Add tokens to the bucket based on advancement in time. Then, re-bound the | 191 // Add tokens to the bucket based on advancement in time. Then, re-bound the |
| 96 // number of tokens in the bucket. Overflow occurs when there is too much | 192 // number of tokens in the bucket. Overflow occurs when there is too much |
| 97 // time between events (a common case), or when RecordSample() is not being | 193 // time between events (a common case), or when RecordSample() is not being |
| 98 // called often enough (a bug). On the other hand, if RecordSample() is being | 194 // called often enough (a bug). On the other hand, if RecordSample() is being |
| 99 // called too often (e.g., as a reaction to IsOverdueForSamplingAt()), the | 195 // called too often (e.g., as a reaction to IsOverdueForSamplingAt()), the |
| 100 // bucket will underflow. | 196 // bucket will underflow. |
| 101 if (!current_event_.is_null()) { | 197 if (!current_event_.is_null()) { |
| 102 if (current_event_ < event_time) { | 198 if (current_event_ < event_time) { |
| 103 token_bucket_ += event_time - current_event_; | 199 token_bucket_ += event_time - current_event_; |
| 104 if (token_bucket_ > token_bucket_capacity_) | 200 if (token_bucket_ > token_bucket_capacity_) |
| 105 token_bucket_ = token_bucket_capacity_; | 201 token_bucket_ = token_bucket_capacity_; |
| 106 } | 202 } |
| 107 // Side note: If the system clock is reset, causing |current_event_| to be | |
| 108 // greater than |event_time|, everything here will simply gracefully adjust. | |
| 109 if (token_bucket_ < base::TimeDelta()) | |
| 110 token_bucket_ = base::TimeDelta(); | |
| 111 TRACE_COUNTER1("mirroring", | 203 TRACE_COUNTER1("mirroring", |
| 112 "MirroringTokenBucketUsec", | 204 "MirroringTokenBucketUsec", |
| 113 std::max<int64>(0, token_bucket_.InMicroseconds())); | 205 std::max<int64>(0, token_bucket_.InMicroseconds())); |
| 114 } | 206 } |
| 115 current_event_ = event_time; | 207 current_event_ = event_time; |
| 116 | |
| 117 // Return true if one capture period's worth of tokens are in the bucket. | |
| 118 return token_bucket_ >= capture_period_; | |
| 119 } | 208 } |
| 120 | 209 |
| 121 void SmoothEventSampler::RecordSample() { | 210 void SmoothEventSampler::RecordSample() { |
| 122 token_bucket_ -= capture_period_; | 211 token_bucket_ -= min_capture_period_; |
| 212 if (token_bucket_ < base::TimeDelta()) | |
| 213 token_bucket_ = base::TimeDelta(); | |
| 123 TRACE_COUNTER1("mirroring", | 214 TRACE_COUNTER1("mirroring", |
| 124 "MirroringTokenBucketUsec", | 215 "MirroringTokenBucketUsec", |
| 125 std::max<int64>(0, token_bucket_.InMicroseconds())); | 216 std::max<int64>(0, token_bucket_.InMicroseconds())); |
| 126 | 217 |
| 127 bool was_paused = overdue_sample_count_ == redundant_capture_goal_; | 218 bool was_paused = overdue_sample_count_ >= redundant_capture_goal_; |
| 128 if (HasUnrecordedEvent()) { | 219 if (HasUnrecordedEvent()) { |
| 129 last_sample_ = current_event_; | 220 last_sample_ = current_event_; |
| 130 overdue_sample_count_ = 0; | 221 overdue_sample_count_ = 0; |
| 131 } else { | 222 } else { |
| 132 ++overdue_sample_count_; | 223 ++overdue_sample_count_; |
| 133 } | 224 } |
| 134 bool is_paused = overdue_sample_count_ == redundant_capture_goal_; | 225 bool is_paused = overdue_sample_count_ >= redundant_capture_goal_; |
| 135 | 226 |
| 136 VLOG_IF(0, !was_paused && is_paused) | 227 VLOG_IF(0, !was_paused && is_paused) |
| 137 << "Tab content unchanged for " << redundant_capture_goal_ | 228 << "Tab content unchanged for " << redundant_capture_goal_ |
| 138 << " frames; capture will halt until content changes."; | 229 << " frames; capture will halt until content changes."; |
| 139 VLOG_IF(0, was_paused && !is_paused) | 230 VLOG_IF(0, was_paused && !is_paused) |
| 140 << "Content changed; capture will resume."; | 231 << "Content changed; capture will resume."; |
|
ncarter (slow)
2014/08/01 23:36:38
I think these VLOGS should probably either be upda
miu
2014/08/04 18:46:05
Done. Yeah, even as debugging aids, they're no lo
| |
| 141 } | 232 } |
| 142 | 233 |
| 143 bool SmoothEventSampler::IsOverdueForSamplingAt(base::TimeTicks event_time) | 234 bool SmoothEventSampler::IsOverdueForSamplingAt(base::TimeTicks event_time) |
| 144 const { | 235 const { |
| 145 DCHECK(!event_time.is_null()); | 236 DCHECK(!event_time.is_null()); |
| 146 | 237 |
| 147 // If we don't get events on compositor updates on this platform, then we | 238 // If we don't get events on compositor updates on this platform, then we |
| 148 // don't reliably know whether we're dirty. | 239 // don't reliably know whether we're dirty. |
| 149 if (events_are_reliable_) { | 240 if (events_are_reliable_) { |
| 150 if (!HasUnrecordedEvent() && | 241 if (!HasUnrecordedEvent() && |
| 151 overdue_sample_count_ >= redundant_capture_goal_) { | 242 overdue_sample_count_ >= redundant_capture_goal_) { |
| 152 return false; // Not dirty. | 243 return false; // Not dirty. |
| 153 } | 244 } |
| 154 } | 245 } |
| 155 | 246 |
| 156 if (last_sample_.is_null()) | 247 if (last_sample_.is_null()) |
| 157 return true; | 248 return true; |
| 158 | 249 |
| 159 // If we're dirty but not yet old, then we've recently gotten updates, so we | 250 // If we're dirty but not yet old, then we've recently gotten updates, so we |
| 160 // won't request a sample just yet. | 251 // won't request a sample just yet. |
| 161 base::TimeDelta dirty_interval = event_time - last_sample_; | 252 base::TimeDelta dirty_interval = event_time - last_sample_; |
| 162 if (dirty_interval < capture_period_ * 4) | 253 return dirty_interval >= |
| 163 return false; | 254 base::TimeDelta::FromMilliseconds(kNonAnimatingThresholdMillis); |
| 164 else | |
| 165 return true; | |
| 166 } | 255 } |
| 167 | 256 |
| 168 bool SmoothEventSampler::HasUnrecordedEvent() const { | 257 bool SmoothEventSampler::HasUnrecordedEvent() const { |
| 169 return !current_event_.is_null() && current_event_ != last_sample_; | 258 return !current_event_.is_null() && current_event_ != last_sample_; |
| 170 } | 259 } |
| 171 | 260 |
| 261 AnimatedContentSampler::AnimatedContentSampler( | |
| 262 base::TimeDelta min_capture_period) | |
| 263 : min_capture_period_(min_capture_period) {} | |
| 264 | |
| 265 AnimatedContentSampler::~AnimatedContentSampler() {} | |
| 266 | |
| 267 void AnimatedContentSampler::ConsiderPresentationEvent( | |
| 268 const gfx::Rect& damage_rect, base::TimeTicks event_time) { | |
| 269 AddObservation(damage_rect, event_time); | |
| 270 | |
| 271 if (AnalyzeObservations(event_time, &detected_region_, &detected_period_) && | |
| 272 detected_period_ > base::TimeDelta() && | |
| 273 detected_period_ <= | |
| 274 base::TimeDelta::FromMicroseconds(kMaxLockInPeriodMicros)) { | |
| 275 if (damage_rect == detected_region_) | |
| 276 UpdateFrameTimestamp(event_time); | |
| 277 else | |
| 278 frame_timestamp_ = base::TimeTicks(); | |
| 279 } else { | |
| 280 detected_region_ = gfx::Rect(); | |
| 281 detected_period_ = base::TimeDelta(); | |
| 282 frame_timestamp_ = base::TimeTicks(); | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 void AnimatedContentSampler::RecordSample(base::TimeTicks frame_timestamp) { | |
| 287 recorded_frame_timestamp_ = frame_timestamp; | |
| 288 sequence_offset_ = base::TimeDelta(); | |
| 289 } | |
| 290 | |
| 291 void AnimatedContentSampler::AddObservation(const gfx::Rect& damage_rect, | |
| 292 base::TimeTicks event_time) { | |
| 293 if (damage_rect.IsEmpty()) | |
| 294 return; // Useless observation. | |
| 295 | |
| 296 // Add the observation to the FIFO queue. | |
| 297 if (!observations_.empty() && observations_.back().event_time > event_time) | |
| 298 return; // The implementation assumes chronological order. | |
| 299 observations_.push_back(Observation(damage_rect, event_time)); | |
| 300 | |
| 301 // Prune-out old observations. | |
| 302 const base::TimeDelta threshold = | |
| 303 base::TimeDelta::FromMilliseconds(kMaxObservationWindowMillis); | |
| 304 while ((event_time - observations_.front().event_time) > threshold) | |
| 305 observations_.pop_front(); | |
| 306 } | |
| 307 | |
| 308 gfx::Rect AnimatedContentSampler::ElectMajorityDamageRect() const { | |
| 309 // This is an derivative of the Boyer-Moore Majority Vote Algorithm where each | |
| 310 // pixel in a candidate gets one vote, as opposed to each candidate getting | |
| 311 // one vote. | |
| 312 const gfx::Rect* candidate = NULL; | |
| 313 int64 votes = 0; | |
| 314 for (ObservationFifo::const_iterator i = observations_.begin(); | |
| 315 i != observations_.end(); ++i) { | |
| 316 DCHECK_GT(i->damage_rect.size().GetArea(), 0); | |
| 317 if (votes == 0) { | |
| 318 candidate = &(i->damage_rect); | |
| 319 votes = candidate->size().GetArea(); | |
| 320 } else if (i->damage_rect == *candidate) { | |
| 321 votes += i->damage_rect.size().GetArea(); | |
| 322 } else { | |
| 323 votes -= i->damage_rect.size().GetArea(); | |
| 324 if (votes < 0) { | |
| 325 candidate = &(i->damage_rect); | |
| 326 votes = -votes; | |
| 327 } | |
| 328 } | |
| 329 } | |
| 330 return (votes > 0) ? *candidate : gfx::Rect(); | |
| 331 } | |
| 332 | |
| 333 bool AnimatedContentSampler::AnalyzeObservations( | |
| 334 base::TimeTicks event_time, | |
| 335 gfx::Rect* rect, | |
| 336 base::TimeDelta* period) const { | |
| 337 const gfx::Rect elected_rect = ElectMajorityDamageRect(); | |
| 338 if (elected_rect.IsEmpty()) | |
| 339 return false; // There is no regular animation present. | |
| 340 | |
| 341 // Scan |observations_|, gathering metrics about the ones having a damage Rect | |
| 342 // equivalent to the |elected_rect|. Along the way, break early whenever the | |
| 343 // event times reveal a non-animating period. | |
| 344 int64 num_pixels_damaged_in_all = 0; | |
| 345 int64 num_pixels_damaged_in_chosen = 0; | |
| 346 base::TimeDelta sum_frame_durations; | |
| 347 size_t count_frame_durations = 0; | |
| 348 base::TimeTicks first_event_time; | |
| 349 base::TimeTicks last_event_time; | |
| 350 for (ObservationFifo::const_reverse_iterator i = observations_.rbegin(); | |
| 351 i != observations_.rend(); ++i) { | |
| 352 const int area = i->damage_rect.size().GetArea(); | |
| 353 num_pixels_damaged_in_all += area; | |
| 354 if (i->damage_rect != elected_rect) | |
| 355 continue; | |
| 356 num_pixels_damaged_in_chosen += area; | |
| 357 if (last_event_time.is_null()) { | |
| 358 last_event_time = i->event_time; | |
| 359 if ((event_time - last_event_time) >= | |
| 360 base::TimeDelta::FromMilliseconds(kNonAnimatingThresholdMillis)) { | |
| 361 return false; // Content animation has recently ended. | |
| 362 } | |
| 363 } else { | |
| 364 const base::TimeDelta frame_duration = first_event_time - i->event_time; | |
| 365 if (frame_duration >= | |
| 366 base::TimeDelta::FromMilliseconds(kNonAnimatingThresholdMillis)) { | |
| 367 break; // Content not animating before this point. | |
| 368 } | |
| 369 sum_frame_durations += frame_duration; | |
| 370 ++count_frame_durations; | |
| 371 } | |
| 372 first_event_time = i->event_time; | |
| 373 } | |
| 374 | |
| 375 if ((last_event_time - first_event_time) < | |
| 376 base::TimeDelta::FromMilliseconds(kMinObservationWindowMillis)) { | |
| 377 return false; // Content has not animated for long enough for accuracy. | |
| 378 } | |
| 379 if (num_pixels_damaged_in_chosen <= (num_pixels_damaged_in_all * 2 / 3)) | |
| 380 return false; // Animation is not damaging a supermajority of pixels. | |
| 381 | |
| 382 *rect = elected_rect; | |
| 383 DCHECK_GT(count_frame_durations, 0u); | |
| 384 *period = sum_frame_durations / count_frame_durations; | |
| 385 return true; | |
| 386 } | |
| 387 | |
| 388 void AnimatedContentSampler::UpdateFrameTimestamp(base::TimeTicks event_time) { | |
| 389 // This is how much time to advance from the last frame timestamp. Never | |
| 390 // advance by less than |min_capture_period_| because the downstream consumer | |
| 391 // cannot handle the higher frame rate. If |detected_period_| is less than | |
| 392 // |min_capture_period_|, excess frames should be dropped. | |
| 393 const base::TimeDelta advancement = | |
| 394 std::max(detected_period_, min_capture_period_); | |
| 395 | |
| 396 // Compute the |timebase| upon which to determine the |frame_timestamp_|. | |
| 397 // Ideally, this would always equal the timestamp of the last recorded frame | |
| 398 // sampling. Determine how much drift from the ideal is present, then adjust | |
| 399 // the timebase by a small amount to spread out the entire correction over | |
| 400 // many frame timestamps. | |
| 401 // | |
| 402 // This accounts for two main sources of drift: 1) The clock drift of the | |
| 403 // system clock relative to the video hardware, which affects the event times; | |
| 404 // and 2) The small error introduced by this frame timestamp rewriting, as it | |
| 405 // is based on averaging over recent events. | |
| 406 base::TimeTicks timebase = event_time - sequence_offset_ - advancement; | |
| 407 if (!recorded_frame_timestamp_.is_null()) { | |
| 408 const base::TimeDelta drift = recorded_frame_timestamp_ - timebase; | |
| 409 const int64 correct_over_num_frames = | |
| 410 base::TimeDelta::FromMilliseconds(kDriftCorrectionMillis) / | |
| 411 detected_period_; | |
| 412 DCHECK_GT(correct_over_num_frames, 0); | |
| 413 timebase = recorded_frame_timestamp_ - (drift / correct_over_num_frames); | |
| 414 } | |
| 415 | |
| 416 // Compute |frame_timestamp_|. Whenever |detected_period_| is less than | |
| 417 // |min_capture_period_|, some extra time is "borrowed" to be able to advance | |
| 418 // by the full |min_capture_period_|. Then, whenever the total amount of | |
| 419 // borrowed time reaches a full |min_capture_period_|, drop a frame. Note | |
| 420 // that when |detected_period_| is greater or equal to |min_capture_period_|, | |
| 421 // this logic is effectively disabled. | |
| 422 borrowed_time_ += advancement - detected_period_; | |
| 423 if (borrowed_time_ >= min_capture_period_) { | |
| 424 borrowed_time_ -= min_capture_period_; | |
| 425 frame_timestamp_ = base::TimeTicks(); | |
| 426 } else { | |
| 427 sequence_offset_ += advancement; | |
| 428 frame_timestamp_ = timebase + sequence_offset_; | |
| 429 } | |
| 430 } | |
| 431 | |
| 172 } // namespace content | 432 } // namespace content |
| OLD | NEW |