| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/capture/content/smooth_event_sampler.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 | |
| 11 #include "base/trace_event/trace_event.h" | |
| 12 | |
| 13 namespace media { | |
| 14 | |
| 15 SmoothEventSampler::SmoothEventSampler(base::TimeDelta min_capture_period) | |
| 16 : token_bucket_(base::TimeDelta::Max()) { | |
| 17 SetMinCapturePeriod(min_capture_period); | |
| 18 } | |
| 19 | |
| 20 void SmoothEventSampler::SetMinCapturePeriod(base::TimeDelta period) { | |
| 21 DCHECK_GT(period, base::TimeDelta()); | |
| 22 min_capture_period_ = period; | |
| 23 token_bucket_capacity_ = period + period / 2; | |
| 24 token_bucket_ = std::min(token_bucket_capacity_, token_bucket_); | |
| 25 } | |
| 26 | |
| 27 void SmoothEventSampler::ConsiderPresentationEvent(base::TimeTicks event_time) { | |
| 28 DCHECK(!event_time.is_null()); | |
| 29 | |
| 30 // Add tokens to the bucket based on advancement in time. Then, re-bound the | |
| 31 // number of tokens in the bucket. Overflow occurs when there is too much | |
| 32 // time between events (a common case), or when RecordSample() is not being | |
| 33 // called often enough (a bug). On the other hand, if RecordSample() is being | |
| 34 // called too often (e.g., as a reaction to IsOverdueForSamplingAt()), the | |
| 35 // bucket will underflow. | |
| 36 if (!current_event_.is_null()) { | |
| 37 if (current_event_ < event_time) { | |
| 38 token_bucket_ += event_time - current_event_; | |
| 39 if (token_bucket_ > token_bucket_capacity_) | |
| 40 token_bucket_ = token_bucket_capacity_; | |
| 41 } | |
| 42 TRACE_COUNTER1("gpu.capture", "MirroringTokenBucketUsec", | |
| 43 std::max<int64_t>(0, token_bucket_.InMicroseconds())); | |
| 44 } | |
| 45 current_event_ = event_time; | |
| 46 } | |
| 47 | |
| 48 bool SmoothEventSampler::ShouldSample() const { | |
| 49 return token_bucket_ >= min_capture_period_; | |
| 50 } | |
| 51 | |
| 52 void SmoothEventSampler::RecordSample() { | |
| 53 token_bucket_ -= min_capture_period_; | |
| 54 if (token_bucket_ < base::TimeDelta()) | |
| 55 token_bucket_ = base::TimeDelta(); | |
| 56 TRACE_COUNTER1("gpu.capture", "MirroringTokenBucketUsec", | |
| 57 std::max<int64_t>(0, token_bucket_.InMicroseconds())); | |
| 58 | |
| 59 if (HasUnrecordedEvent()) | |
| 60 last_sample_ = current_event_; | |
| 61 } | |
| 62 | |
| 63 bool SmoothEventSampler::HasUnrecordedEvent() const { | |
| 64 return !current_event_.is_null() && current_event_ != last_sample_; | |
| 65 } | |
| 66 | |
| 67 } // namespace media | |
| OLD | NEW |