Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / capture / content / smooth_event_sampler.h
blobc250eb486e100b83e0b5e99b04adba49a5fe522c
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.
5 #ifndef MEDIA_CAPTURE_SMOOTH_EVENT_SAMPLER_H_
6 #define MEDIA_CAPTURE_SMOOTH_EVENT_SAMPLER_H_
8 #include "base/time/time.h"
9 #include "media/base/media_export.h"
11 namespace media {
13 // Filters a sequence of events to achieve a target frequency.
14 class MEDIA_EXPORT SmoothEventSampler {
15 public:
16 SmoothEventSampler(base::TimeDelta min_capture_period,
17 int redundant_capture_goal);
19 // Get/Set minimum capture period. When setting a new value, the state of the
20 // sampler is retained so that sampling will continue smoothly.
21 base::TimeDelta min_capture_period() const { return min_capture_period_; }
22 void SetMinCapturePeriod(base::TimeDelta p);
24 // Add a new event to the event history, and consider whether it ought to be
25 // sampled. The event is not recorded as a sample until RecordSample() is
26 // called.
27 void ConsiderPresentationEvent(base::TimeTicks event_time);
29 // Returns true if the last event considered should be sampled.
30 bool ShouldSample() const;
32 // Operates on the last event added by ConsiderPresentationEvent(), marking
33 // it as sampled. After this point we are current in the stream of events, as
34 // we have sampled the most recent event.
35 void RecordSample();
37 // Returns true if, at time |event_time|, sampling should occur because too
38 // much time will have passed relative to the last event and/or sample.
39 bool IsOverdueForSamplingAt(base::TimeTicks event_time) const;
41 // Returns true if ConsiderPresentationEvent() has been called since the last
42 // call to RecordSample().
43 bool HasUnrecordedEvent() const;
45 private:
46 base::TimeDelta min_capture_period_;
47 const int redundant_capture_goal_;
48 base::TimeDelta token_bucket_capacity_;
50 base::TimeTicks current_event_;
51 base::TimeTicks last_sample_;
52 int overdue_sample_count_;
53 base::TimeDelta token_bucket_;
55 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler);
58 } // namespace media
60 #endif // MEDIA_CAPTURE_SMOOTH_EVENT_SAMPLER_H_