forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock_drift_smoother.cc
59 lines (47 loc) · 1.72 KB
/
clock_drift_smoother.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/cast/common/clock_drift_smoother.h"
#include <stdint.h>
#include "base/check.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
namespace media {
namespace cast {
ClockDriftSmoother::ClockDriftSmoother(base::TimeDelta time_constant)
: time_constant_(time_constant),
estimate_us_(0.0) {
DCHECK(time_constant_ > base::TimeDelta());
}
ClockDriftSmoother::~ClockDriftSmoother() = default;
base::TimeDelta ClockDriftSmoother::Current() const {
DCHECK(!last_update_time_.is_null());
return base::TimeDelta::FromMicroseconds(
base::ClampRound<int64_t>(estimate_us_));
}
void ClockDriftSmoother::Reset(base::TimeTicks now,
base::TimeDelta measured_offset) {
DCHECK(!now.is_null());
last_update_time_ = now;
estimate_us_ = measured_offset.InMicrosecondsF();
}
void ClockDriftSmoother::Update(base::TimeTicks now,
base::TimeDelta measured_offset) {
DCHECK(!now.is_null());
if (last_update_time_.is_null()) {
Reset(now, measured_offset);
return;
}
DCHECK_GE(now, last_update_time_); // |now| is monotonically non-decreasing.
const base::TimeDelta elapsed = now - last_update_time_;
last_update_time_ = now;
const double weight = elapsed / (elapsed + time_constant_);
estimate_us_ = weight * measured_offset.InMicrosecondsF() +
(1.0 - weight) * estimate_us_;
}
// static
base::TimeDelta ClockDriftSmoother::GetDefaultTimeConstant() {
return base::TimeDelta::FromSeconds(30);
}
} // namespace cast
} // namespace media