Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: webrtc/modules/congestion_controller/median_slope_estimator.cc

Issue 2578543002: Avoid precision loss in MedianSlopeEstimator from int64_t -> double conversion (Closed)
Patch Set: Move test function to anonymous namespace Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 17 matching lines...) Expand all
28 num_of_deltas_(0), 28 num_of_deltas_(0),
29 accumulated_delay_(0), 29 accumulated_delay_(0),
30 delay_hist_(), 30 delay_hist_(),
31 median_filter_(0.5), 31 median_filter_(0.5),
32 trendline_(0) {} 32 trendline_(0) {}
33 33
34 MedianSlopeEstimator::~MedianSlopeEstimator() {} 34 MedianSlopeEstimator::~MedianSlopeEstimator() {}
35 35
36 void MedianSlopeEstimator::Update(double recv_delta_ms, 36 void MedianSlopeEstimator::Update(double recv_delta_ms,
37 double send_delta_ms, 37 double send_delta_ms,
38 double now_ms) { 38 int64_t arrival_time_ms) {
39 const double delta_ms = recv_delta_ms - send_delta_ms; 39 const double delta_ms = recv_delta_ms - send_delta_ms;
40 ++num_of_deltas_; 40 ++num_of_deltas_;
41 if (num_of_deltas_ > kDeltaCounterMax) { 41 if (num_of_deltas_ > kDeltaCounterMax)
42 num_of_deltas_ = kDeltaCounterMax; 42 num_of_deltas_ = kDeltaCounterMax;
43 }
44 43
45 accumulated_delay_ += delta_ms; 44 accumulated_delay_ += delta_ms;
46 BWE_TEST_LOGGING_PLOT(1, "accumulated_delay_ms", now_ms, accumulated_delay_); 45 BWE_TEST_LOGGING_PLOT(1, "accumulated_delay_ms", arrival_time_ms,
46 accumulated_delay_);
47 47
48 // If the window is full, remove the |window_size_| - 1 slopes that belong to 48 // If the window is full, remove the |window_size_| - 1 slopes that belong to
49 // the oldest point. 49 // the oldest point.
50 if (delay_hist_.size() == window_size_) { 50 if (delay_hist_.size() == window_size_) {
51 for (double slope : delay_hist_.front().slopes) { 51 for (double slope : delay_hist_.front().slopes) {
52 const bool success = median_filter_.Erase(slope); 52 const bool success = median_filter_.Erase(slope);
53 RTC_CHECK(success); 53 RTC_CHECK(success);
54 } 54 }
55 delay_hist_.pop_front(); 55 delay_hist_.pop_front();
56 } 56 }
57 // Add |window_size_| - 1 new slopes. 57 // Add |window_size_| - 1 new slopes.
58 for (auto& old_delay : delay_hist_) { 58 for (auto& old_delay : delay_hist_) {
59 if (now_ms - old_delay.time != 0) { 59 if (arrival_time_ms - old_delay.time != 0) {
60 // The C99 standard explicitly states that casts and assignments must 60 // The C99 standard explicitly states that casts and assignments must
61 // perform the associated conversions. This means that |slope| will be 61 // perform the associated conversions. This means that |slope| will be
62 // a 64-bit double even if the division is computed using, e.g., 80-bit 62 // a 64-bit double even if the division is computed using, e.g., 80-bit
63 // extended precision. I believe this also holds in C++ even though the 63 // extended precision. I believe this also holds in C++ even though the
64 // C++11 standard isn't as explicit. Furthermore, there are good reasons 64 // C++11 standard isn't as explicit. Furthermore, there are good reasons
65 // to believe that compilers couldn't perform optimizations that break 65 // to believe that compilers couldn't perform optimizations that break
66 // this assumption even if they wanted to. 66 // this assumption even if they wanted to.
67 double slope = 67 double slope = (accumulated_delay_ - old_delay.delay) /
68 (accumulated_delay_ - old_delay.delay) / (now_ms - old_delay.time); 68 static_cast<double>(arrival_time_ms - old_delay.time);
69 median_filter_.Insert(slope); 69 median_filter_.Insert(slope);
70 // We want to avoid issues with different rounding mode / precision 70 // We want to avoid issues with different rounding mode / precision
71 // which we might get if we recomputed the slope when we remove it. 71 // which we might get if we recomputed the slope when we remove it.
72 old_delay.slopes.push_back(slope); 72 old_delay.slopes.push_back(slope);
73 } 73 }
74 } 74 }
75 delay_hist_.emplace_back(now_ms, accumulated_delay_, window_size_ - 1); 75 delay_hist_.emplace_back(arrival_time_ms, accumulated_delay_,
76 window_size_ - 1);
76 // Recompute the median slope. 77 // Recompute the median slope.
77 if (delay_hist_.size() == window_size_) 78 if (delay_hist_.size() == window_size_)
78 trendline_ = median_filter_.GetPercentileValue(); 79 trendline_ = median_filter_.GetPercentileValue();
79 80
80 BWE_TEST_LOGGING_PLOT(1, "trendline_slope", now_ms, trendline_); 81 BWE_TEST_LOGGING_PLOT(1, "trendline_slope", arrival_time_ms, trendline_);
81 } 82 }
82 83
83 } // namespace webrtc 84 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698