Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / capture / content / feedback_signal_accumulator.cc
blob896c23a8a0b1e24660671a41b6ffec12072edd34
1 // Copyright (c) 2015 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 #include "media/capture/content/feedback_signal_accumulator.h"
7 #include <algorithm>
8 #include <cmath>
10 namespace media {
12 FeedbackSignalAccumulator::FeedbackSignalAccumulator(base::TimeDelta half_life)
13 : half_life_(half_life) {
14 DCHECK(half_life_ > base::TimeDelta());
17 void FeedbackSignalAccumulator::Reset(double starting_value,
18 base::TimeTicks timestamp) {
19 DCHECK(!timestamp.is_null());
20 average_ = update_value_ = prior_average_ = starting_value;
21 reset_time_ = update_time_ = prior_update_time_ = timestamp;
24 bool FeedbackSignalAccumulator::Update(double value,
25 base::TimeTicks timestamp) {
26 DCHECK(!reset_time_.is_null());
28 if (timestamp < update_time_) {
29 return false; // Not in chronological order.
30 } else if (timestamp == update_time_) {
31 if (timestamp == reset_time_) {
32 // Edge case: Multiple updates at reset timestamp.
33 average_ = update_value_ = prior_average_ =
34 std::max(value, update_value_);
35 return true;
37 if (value <= update_value_)
38 return true;
39 update_value_ = value;
40 } else {
41 prior_average_ = average_;
42 prior_update_time_ = update_time_;
43 update_value_ = value;
44 update_time_ = timestamp;
47 const double elapsed_us =
48 static_cast<double>((update_time_ - prior_update_time_).InMicroseconds());
49 const double weight = elapsed_us / (elapsed_us + half_life_.InMicroseconds());
50 average_ = weight * update_value_ + (1.0 - weight) * prior_average_;
51 DCHECK(std::isfinite(average_));
53 return true;
56 } // namespace media