• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CAST_BASE_CLOCK_DRIFT_SMOOTHER_H_
6 #define MEDIA_CAST_BASE_CLOCK_DRIFT_SMOOTHER_H_
7 
8 #include "base/time/time.h"
9 
10 namespace media {
11 namespace cast {
12 
13 // Tracks the jitter and drift between clocks, providing a smoothed offset.
14 // Internally, a Simple IIR filter is used to maintain a running average that
15 // moves at a rate based on the passage of time.
16 class ClockDriftSmoother {
17  public:
18   // |time_constant| is the amount of time an impulse signal takes to decay by
19   // ~62.6%.  Interpretation: If the value passed to several Update() calls is
20   // held constant for T seconds, then the running average will have moved
21   // towards the value by ~62.6% from where it started.
22   explicit ClockDriftSmoother(base::TimeDelta time_constant);
23   ~ClockDriftSmoother();
24 
25   // Returns the current offset.
26   base::TimeDelta Current() const;
27 
28   // Discard all history and reset to exactly |offset|, measured |now|.
29   void Reset(base::TimeTicks now, base::TimeDelta offset);
30 
31   // Update the current offset, which was measured |now|.  The weighting that
32   // |measured_offset| will have on the running average is influenced by how
33   // much time has passed since the last call to this method (or Reset()).
34   // |now| should be monotonically non-decreasing over successive calls of this
35   // method.
36   void Update(base::TimeTicks now, base::TimeDelta measured_offset);
37 
38   // Returns a time constant suitable for most use cases, where the clocks
39   // are expected to drift very little with respect to each other, and the
40   // jitter caused by clock imprecision is effectively canceled out.
41   static base::TimeDelta GetDefaultTimeConstant();
42 
43  private:
44   const base::TimeDelta time_constant_;
45   base::TimeTicks last_update_time_;
46   double estimate_us_;
47 };
48 
49 }  // namespace cast
50 }  // namespace media
51 
52 #endif  // MEDIA_CAST_BASE_CLOCK_DRIFT_SMOOTHER_H_
53