simple_watch_timer.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 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_BASE_SIMPLE_WATCH_TIMER_H_
  5. #define MEDIA_BASE_SIMPLE_WATCH_TIMER_H_
  6. #include "base/callback_forward.h"
  7. #include "base/time/time.h"
  8. #include "base/timer/timer.h"
  9. #include "media/base/media_export.h"
  10. namespace media {
  11. // SimpleWatchTimer aids in recording UMA counts that accumulate media watch
  12. // time in seconds. It will fire its callback about once per second during
  13. // active playback.
  14. //
  15. // Active playback is a duration after Start() and before Stop() in
  16. // which current time progresses. Large jumps in current time are not considered
  17. // to be progress; they are assumed to be seeks or media errors.
  18. //
  19. // Start() and Stop() may be called repeatedly. It is recommended to call Stop()
  20. // before destructing a SimpleWatchTimer so that |tick_cb| can be fired at an
  21. // opportune time.
  22. //
  23. // Note: SimpleWatchTimer does not understand playbackRate and will discard
  24. // durations with high rates.
  25. class MEDIA_EXPORT SimpleWatchTimer {
  26. public:
  27. using TickCB = base::RepeatingClosure;
  28. using GetCurrentTimeCB = base::RepeatingCallback<base::TimeDelta()>;
  29. SimpleWatchTimer(TickCB tick_cb, GetCurrentTimeCB get_current_time_cb);
  30. SimpleWatchTimer(const SimpleWatchTimer&) = delete;
  31. SimpleWatchTimer& operator=(const SimpleWatchTimer&) = delete;
  32. ~SimpleWatchTimer();
  33. void Start();
  34. void Stop();
  35. private:
  36. void Tick();
  37. TickCB tick_cb_;
  38. GetCurrentTimeCB get_current_time_cb_;
  39. int unreported_ms_ = 0;
  40. base::TimeDelta last_current_time_;
  41. base::RepeatingTimer timer_;
  42. };
  43. } // namespace media
  44. #endif // MEDIA_BASE_SIMPLE_WATCH_TIMER_H_