Roll leveldb 3f7758:803d69 (v1.17 -> v1.18)
[chromium-blink-merge.git] / media / cast / common / clock_drift_smoother.h
blob0511da9f3d925e3d9de30362a202dd77be66477d
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.
5 #ifndef MEDIA_CAST_COMMON_CLOCK_DRIFT_SMOOTHER_H_
6 #define MEDIA_CAST_COMMON_CLOCK_DRIFT_SMOOTHER_H_
8 #include "base/time/time.h"
10 namespace media {
11 namespace cast {
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();
25 // Returns the current offset.
26 base::TimeDelta Current() const;
28 // Discard all history and reset to exactly |offset|, measured |now|.
29 void Reset(base::TimeTicks now, base::TimeDelta offset);
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);
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();
43 private:
44 const base::TimeDelta time_constant_;
45 base::TimeTicks last_update_time_;
46 double estimate_us_;
49 } // namespace cast
50 } // namespace media
52 #endif // MEDIA_CAST_COMMON_CLOCK_DRIFT_SMOOTHER_H_