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