time_delta_interpolator.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2014 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_TIME_DELTA_INTERPOLATOR_H_
  5. #define MEDIA_BASE_TIME_DELTA_INTERPOLATOR_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/time/time.h"
  8. #include "media/base/media_export.h"
  9. namespace base {
  10. class TickClock;
  11. } // namespace base
  12. namespace media {
  13. // Interpolates between two TimeDeltas based on the passage of wall clock time
  14. // and the current playback rate.
  15. //
  16. // TimeDeltaInterpolator is not thread-safe and must be externally locked.
  17. class MEDIA_EXPORT TimeDeltaInterpolator {
  18. public:
  19. // Constructs an interpolator initialized to zero with a rate of 1.0.
  20. //
  21. // |tick_clock| is used for sampling wall clock time for interpolating.
  22. explicit TimeDeltaInterpolator(const base::TickClock* tick_clock);
  23. TimeDeltaInterpolator(const TimeDeltaInterpolator&) = delete;
  24. TimeDeltaInterpolator& operator=(const TimeDeltaInterpolator&) = delete;
  25. ~TimeDeltaInterpolator();
  26. bool interpolating() { return interpolating_; }
  27. // Starts returning interpolated TimeDelta values.
  28. //
  29. // |tick_clock| will be queried for a new reference time value.
  30. base::TimeDelta StartInterpolating();
  31. // Stops returning interpolated TimeDelta values.
  32. //
  33. // |tick_clock| will be queried for a new reference time value.
  34. base::TimeDelta StopInterpolating();
  35. // Sets a new rate at which to interpolate.
  36. // The default rate is 0.
  37. //
  38. // |tick_clock| will be queried for a new reference time value.
  39. void SetPlaybackRate(double playback_rate);
  40. // Sets the two timestamps to interpolate between at |playback_rate_|.
  41. // |upper_bound| must be greater or equal to |lower_bound|.
  42. //
  43. // |upper_bound| is typically the media timestamp of the last audio frame
  44. // buffered by the audio hardware.
  45. void SetBounds(base::TimeDelta lower_bound,
  46. base::TimeDelta upper_bound,
  47. base::TimeTicks capture_time);
  48. // Sets the upper bound used for interpolation. Note that if |upper_bound| is
  49. // less than what was previously set via SetTime(), then all future calls
  50. // to GetInterpolatedTime() will return |upper_bound|.
  51. void SetUpperBound(base::TimeDelta upper_bound);
  52. // Computes an interpolated time based on SetTime().
  53. base::TimeDelta GetInterpolatedTime();
  54. private:
  55. const raw_ptr<const base::TickClock> tick_clock_;
  56. bool interpolating_;
  57. // The range of time to interpolate between.
  58. base::TimeDelta lower_bound_;
  59. base::TimeDelta upper_bound_;
  60. // The monotonic system clock time used for interpolating between
  61. // |lower_bound_| and |upper_bound_|.
  62. base::TimeTicks reference_;
  63. double playback_rate_;
  64. };
  65. } // namespace media
  66. #endif // MEDIA_BASE_TIME_DELTA_INTERPOLATOR_H_