OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "content/browser/media/capture/feedback_signal_accumulator.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <cmath> |
| 9 |
| 10 namespace content { |
| 11 |
| 12 FeedbackSignalAccumulator::FeedbackSignalAccumulator(base::TimeDelta half_life) |
| 13 : half_life_(half_life) { |
| 14 DCHECK(half_life_ > base::TimeDelta()); |
| 15 } |
| 16 |
| 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; |
| 22 } |
| 23 |
| 24 bool FeedbackSignalAccumulator::Update(double value, |
| 25 base::TimeTicks timestamp) { |
| 26 DCHECK(!reset_time_.is_null()); |
| 27 |
| 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; |
| 36 } |
| 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; |
| 45 } |
| 46 |
| 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_)); |
| 52 |
| 53 return true; |
| 54 } |
| 55 |
| 56 } // namespace content |
OLD | NEW |