Home | History | Annotate | Download | only in base
      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/basictypes.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(base::TickClock* tick_clock);
     28   ~TimeDeltaInterpolator();
     29 
     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   //
     44   // |tick_clock| will be queried for a new reference time value.
     45   void SetPlaybackRate(float playback_rate);
     46 
     47   // Sets the two timestamps to interpolate between at |playback_rate_|.
     48   // |upper_bound| must be greater or equal to |lower_bound|.
     49   //
     50   // |upper_bound| is typically the media timestamp of the last audio frame
     51   // buffered by the audio hardware.
     52   void SetBounds(base::TimeDelta lower_bound, base::TimeDelta upper_bound);
     53 
     54   // Sets the upper bound used for interpolation. Note that if |upper_bound| is
     55   // less than what was previously set via SetTime(), then all future calls
     56   // to GetInterpolatedTime() will return |upper_bound|.
     57   void SetUpperBound(base::TimeDelta upper_bound);
     58 
     59   // Computes an interpolated time based on SetTime().
     60   base::TimeDelta GetInterpolatedTime();
     61 
     62  private:
     63   base::TickClock* const tick_clock_;
     64 
     65   bool interpolating_;
     66 
     67   // The range of time to interpolate between.
     68   base::TimeDelta lower_bound_;
     69   base::TimeDelta upper_bound_;
     70 
     71   // The monotonic system clock time used for interpolating between
     72   // |lower_bound_| and |upper_bound_|.
     73   base::TimeTicks reference_;
     74 
     75   float playback_rate_;
     76 
     77   DISALLOW_COPY_AND_ASSIGN(TimeDeltaInterpolator);
     78 };
     79 
     80 }  // namespace media
     81 
     82 #endif  // MEDIA_BASE_TIME_DELTA_INTERPOLATOR_H_
     83