mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/call_stats.h" |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 12 | |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 13 | #include <algorithm> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <memory> |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 15 | |
Steve Anton | bd631a0 | 2019-03-28 10:51:27 -0700 | [diff] [blame] | 16 | #include "absl/algorithm/container.h" |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 17 | #include "modules/utility/include/process_thread.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 19 | #include "rtc_base/location.h" |
Danil Chapovalov | 1aa7581 | 2019-03-05 11:11:35 +0100 | [diff] [blame] | 20 | #include "rtc_base/task_utils/to_queued_task.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "system_wrappers/include/metrics.h" |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 24 | namespace { |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 25 | |
| 26 | void RemoveOldReports(int64_t now, std::list<CallStats::RttTime>* reports) { |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 27 | static constexpr const int64_t kRttTimeoutMs = 1500; |
| 28 | reports->remove_if( |
| 29 | [&now](CallStats::RttTime& r) { return now - r.time > kRttTimeoutMs; }); |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 32 | int64_t GetMaxRttMs(const std::list<CallStats::RttTime>& reports) { |
| 33 | int64_t max_rtt_ms = -1; |
| 34 | for (const CallStats::RttTime& rtt_time : reports) |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 35 | max_rtt_ms = std::max(rtt_time.rtt, max_rtt_ms); |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 36 | return max_rtt_ms; |
| 37 | } |
| 38 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 39 | int64_t GetAvgRttMs(const std::list<CallStats::RttTime>& reports) { |
| 40 | RTC_DCHECK(!reports.empty()); |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 41 | int64_t sum = 0; |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 42 | for (std::list<CallStats::RttTime>::const_iterator it = reports.begin(); |
| 43 | it != reports.end(); ++it) { |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 44 | sum += it->rtt; |
| 45 | } |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 46 | return sum / reports.size(); |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 49 | int64_t GetNewAvgRttMs(const std::list<CallStats::RttTime>& reports, |
| 50 | int64_t prev_avg_rtt) { |
| 51 | if (reports.empty()) |
| 52 | return -1; // Reset (invalid average). |
| 53 | |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 54 | int64_t cur_rtt_ms = GetAvgRttMs(reports); |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 55 | if (prev_avg_rtt == -1) |
| 56 | return cur_rtt_ms; // New initial average value. |
| 57 | |
| 58 | // Weight factor to apply to the average rtt. |
| 59 | // We weigh the old average at 70% against the new average (30%). |
| 60 | constexpr const float kWeightFactor = 0.3f; |
| 61 | return prev_avg_rtt * (1.0f - kWeightFactor) + cur_rtt_ms * kWeightFactor; |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 62 | } |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 63 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 64 | // This class is used to de-register a Module from a ProcessThread to satisfy |
| 65 | // threading requirements of the Module (CallStats). |
| 66 | // The guarantee offered by TemporaryDeregistration is that while its in scope, |
| 67 | // no calls to |TimeUntilNextProcess| or |Process()| will occur and therefore |
| 68 | // synchronization with those methods, is not necessary. |
| 69 | class TemporaryDeregistration { |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 70 | public: |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 71 | TemporaryDeregistration(Module* module, |
| 72 | ProcessThread* process_thread, |
| 73 | bool thread_running) |
| 74 | : module_(module), |
| 75 | process_thread_(process_thread), |
| 76 | deregistered_(thread_running) { |
| 77 | if (thread_running) |
| 78 | process_thread_->DeRegisterModule(module_); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 79 | } |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 80 | ~TemporaryDeregistration() { |
| 81 | if (deregistered_) |
| 82 | process_thread_->RegisterModule(module_, RTC_FROM_HERE); |
asapersson@webrtc.org | 1ae1d0c | 2013-11-20 12:46:11 +0000 | [diff] [blame] | 83 | } |
| 84 | |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 85 | private: |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 86 | Module* const module_; |
| 87 | ProcessThread* const process_thread_; |
| 88 | const bool deregistered_; |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 91 | } // namespace |
| 92 | |
| 93 | CallStats::CallStats(Clock* clock, ProcessThread* process_thread) |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 94 | : clock_(clock), |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 95 | last_process_time_(clock_->TimeInMilliseconds()), |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 96 | max_rtt_ms_(-1), |
| 97 | avg_rtt_ms_(-1), |
| 98 | sum_avg_rtt_ms_(0), |
| 99 | num_avg_rtt_(0), |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 100 | time_of_first_rtt_ms_(-1), |
| 101 | process_thread_(process_thread), |
| 102 | process_thread_running_(false) { |
| 103 | RTC_DCHECK(process_thread_); |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 104 | process_thread_checker_.Detach(); |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 105 | } |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 106 | |
| 107 | CallStats::~CallStats() { |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 108 | RTC_DCHECK_RUN_ON(&construction_thread_checker_); |
| 109 | RTC_DCHECK(!process_thread_running_); |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 110 | RTC_DCHECK(observers_.empty()); |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 111 | |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 112 | UpdateHistograms(); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 113 | } |
| 114 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 115 | int64_t CallStats::TimeUntilNextProcess() { |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 116 | RTC_DCHECK_RUN_ON(&process_thread_checker_); |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 117 | return last_process_time_ + kUpdateIntervalMs - clock_->TimeInMilliseconds(); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 118 | } |
| 119 | |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 120 | void CallStats::Process() { |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 121 | RTC_DCHECK_RUN_ON(&process_thread_checker_); |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 122 | int64_t now = clock_->TimeInMilliseconds(); |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 123 | last_process_time_ = now; |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 124 | |
Tommi | 48b48e5 | 2019-08-09 11:42:32 +0200 | [diff] [blame] | 125 | // |avg_rtt_ms_| is allowed to be read on the process thread since that's the |
| 126 | // only thread that modifies the value. |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 127 | int64_t avg_rtt_ms = avg_rtt_ms_; |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 128 | RemoveOldReports(now, &reports_); |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 129 | max_rtt_ms_ = GetMaxRttMs(reports_); |
| 130 | avg_rtt_ms = GetNewAvgRttMs(reports_, avg_rtt_ms); |
| 131 | { |
| 132 | rtc::CritScope lock(&avg_rtt_ms_lock_); |
| 133 | avg_rtt_ms_ = avg_rtt_ms; |
| 134 | } |
stefan@webrtc.org | 8ca8a71 | 2013-04-23 16:48:32 +0000 | [diff] [blame] | 135 | |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 136 | // If there is a valid rtt, update all observers with the max rtt. |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 137 | if (max_rtt_ms_ >= 0) { |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 138 | RTC_DCHECK_GE(avg_rtt_ms, 0); |
| 139 | for (CallStatsObserver* observer : observers_) |
| 140 | observer->OnRttUpdate(avg_rtt_ms, max_rtt_ms_); |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 141 | // Sum for Histogram of average RTT reported over the entire call. |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 142 | sum_avg_rtt_ms_ += avg_rtt_ms; |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 143 | ++num_avg_rtt_; |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 144 | } |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 147 | void CallStats::ProcessThreadAttached(ProcessThread* process_thread) { |
| 148 | RTC_DCHECK_RUN_ON(&construction_thread_checker_); |
| 149 | RTC_DCHECK(!process_thread || process_thread_ == process_thread); |
| 150 | process_thread_running_ = process_thread != nullptr; |
asapersson@webrtc.org | 1ae1d0c | 2013-11-20 12:46:11 +0000 | [diff] [blame] | 151 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 152 | // Whether we just got attached or detached, we clear the |
| 153 | // |process_thread_checker_| so that it can be used to protect variables |
| 154 | // in either the process thread when it starts again, or UpdateHistograms() |
| 155 | // (mutually exclusive). |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 156 | process_thread_checker_.Detach(); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 157 | } |
| 158 | |
fischman@webrtc.org | aea96d3 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 159 | void CallStats::RegisterStatsObserver(CallStatsObserver* observer) { |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 160 | RTC_DCHECK_RUN_ON(&construction_thread_checker_); |
| 161 | TemporaryDeregistration deregister(this, process_thread_, |
| 162 | process_thread_running_); |
| 163 | |
Steve Anton | bd631a0 | 2019-03-28 10:51:27 -0700 | [diff] [blame] | 164 | if (!absl::c_linear_search(observers_, observer)) |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 165 | observers_.push_back(observer); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 166 | } |
| 167 | |
fischman@webrtc.org | aea96d3 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 168 | void CallStats::DeregisterStatsObserver(CallStatsObserver* observer) { |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 169 | RTC_DCHECK_RUN_ON(&construction_thread_checker_); |
| 170 | TemporaryDeregistration deregister(this, process_thread_, |
| 171 | process_thread_running_); |
| 172 | observers_.remove(observer); |
| 173 | } |
| 174 | |
| 175 | int64_t CallStats::LastProcessedRtt() const { |
Tommi | 48b48e5 | 2019-08-09 11:42:32 +0200 | [diff] [blame] | 176 | // TODO(tommi): This currently gets called from the construction thread of |
| 177 | // Call as well as from the process thread. Look into restricting this to |
| 178 | // allow only reading this from the process thread (or TQ once we get there) |
| 179 | // so that the lock isn't necessary. |
| 180 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 181 | rtc::CritScope cs(&avg_rtt_ms_lock_); |
| 182 | return avg_rtt_ms_; |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 183 | } |
| 184 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 185 | void CallStats::OnRttUpdate(int64_t rtt) { |
Tommi | 48b48e5 | 2019-08-09 11:42:32 +0200 | [diff] [blame] | 186 | RTC_DCHECK_RUN_ON(&process_thread_checker_); |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 187 | |
Tommi | 48b48e5 | 2019-08-09 11:42:32 +0200 | [diff] [blame] | 188 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 189 | reports_.push_back(RttTime(rtt, now_ms)); |
| 190 | if (time_of_first_rtt_ms_ == -1) |
| 191 | time_of_first_rtt_ms_ = now_ms; |
| 192 | |
| 193 | // Make sure Process() will be called and deliver the updates asynchronously. |
| 194 | last_process_time_ -= kUpdateIntervalMs; |
| 195 | process_thread_->WakeUp(this); |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void CallStats::UpdateHistograms() { |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 199 | RTC_DCHECK_RUN_ON(&construction_thread_checker_); |
| 200 | RTC_DCHECK(!process_thread_running_); |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 201 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 202 | // The extra scope is because we have two 'dcheck run on' thread checkers. |
| 203 | // This is a special case since it's safe to access variables on the current |
| 204 | // thread that normally are only touched on the process thread. |
| 205 | // Since we're not attached to the process thread and/or the process thread |
| 206 | // isn't running, it's OK to touch these variables here. |
| 207 | { |
| 208 | // This method is called on the ctor thread (usually from the dtor, unless |
| 209 | // a test calls it). It's a requirement that the function be called when |
| 210 | // the process thread is not running (a condition that's met at destruction |
| 211 | // time), and thanks to that, we don't need a lock to synchronize against |
| 212 | // it. |
| 213 | RTC_DCHECK_RUN_ON(&process_thread_checker_); |
| 214 | |
| 215 | if (time_of_first_rtt_ms_ == -1 || num_avg_rtt_ < 1) |
| 216 | return; |
| 217 | |
| 218 | int64_t elapsed_sec = |
| 219 | (clock_->TimeInMilliseconds() - time_of_first_rtt_ms_) / 1000; |
| 220 | if (elapsed_sec >= metrics::kMinRunTimeInSeconds) { |
| 221 | int64_t avg_rtt_ms = (sum_avg_rtt_ms_ + num_avg_rtt_ / 2) / num_avg_rtt_; |
| 222 | RTC_HISTOGRAM_COUNTS_10000( |
| 223 | "WebRTC.Video.AverageRoundTripTimeInMilliseconds", avg_rtt_ms); |
| 224 | } |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 225 | } |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | } // namespace webrtc |