smooth_event_sampler.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #ifndef MEDIA_CAPTURE_CONTENT_SMOOTH_EVENT_SAMPLER_H_
  5. #define MEDIA_CAPTURE_CONTENT_SMOOTH_EVENT_SAMPLER_H_
  6. #include "base/time/time.h"
  7. #include "media/capture/capture_export.h"
  8. namespace media {
  9. // Filters a sequence of events to achieve a target frequency.
  10. class CAPTURE_EXPORT SmoothEventSampler {
  11. public:
  12. explicit SmoothEventSampler(base::TimeDelta min_capture_period);
  13. SmoothEventSampler(const SmoothEventSampler&) = delete;
  14. SmoothEventSampler& operator=(const SmoothEventSampler&) = delete;
  15. // Get/Set minimum capture period. When setting a new value, the state of the
  16. // sampler is retained so that sampling will continue smoothly.
  17. base::TimeDelta min_capture_period() const { return min_capture_period_; }
  18. void SetMinCapturePeriod(base::TimeDelta p);
  19. // Add a new event to the event history, and consider whether it ought to be
  20. // sampled. The event is not recorded as a sample until RecordSample() is
  21. // called.
  22. void ConsiderPresentationEvent(base::TimeTicks event_time);
  23. // Returns true if the last event considered should be sampled.
  24. bool ShouldSample() const;
  25. // Operates on the last event added by ConsiderPresentationEvent(), marking
  26. // it as sampled. After this point we are current in the stream of events, as
  27. // we have sampled the most recent event.
  28. void RecordSample();
  29. // Returns true if ConsiderPresentationEvent() has been called since the last
  30. // call to RecordSample().
  31. bool HasUnrecordedEvent() const;
  32. private:
  33. base::TimeDelta min_capture_period_;
  34. base::TimeDelta token_bucket_capacity_;
  35. base::TimeTicks current_event_;
  36. base::TimeTicks last_sample_;
  37. base::TimeDelta token_bucket_;
  38. };
  39. } // namespace media
  40. #endif // MEDIA_CAPTURE_CONTENT_SMOOTH_EVENT_SAMPLER_H_