blob: ce93d04e6d9917e3388043ca2c7006b595177ec0 [file] [log] [blame]
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/call_stats.h"
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000012
Peter Boström7623ce42015-12-09 12:13:30 +010013#include <algorithm>
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <memory>
Peter Boström7623ce42015-12-09 12:13:30 +010015
Steve Antonbd631a02019-03-28 10:51:27 -070016#include "absl/algorithm/container.h"
Tommi38c5d932018-03-27 23:11:09 +020017#include "modules/utility/include/process_thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Tommi38c5d932018-03-27 23:11:09 +020019#include "rtc_base/location.h"
Danil Chapovalov1aa75812019-03-05 11:11:35 +010020#include "rtc_base/task_utils/to_queued_task.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "system_wrappers/include/metrics.h"
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000022
23namespace webrtc {
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000024namespace {
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000025
26void RemoveOldReports(int64_t now, std::list<CallStats::RttTime>* reports) {
Tommi38c5d932018-03-27 23:11:09 +020027 static constexpr const int64_t kRttTimeoutMs = 1500;
28 reports->remove_if(
29 [&now](CallStats::RttTime& r) { return now - r.time > kRttTimeoutMs; });
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000030}
31
Tommi38c5d932018-03-27 23:11:09 +020032int64_t GetMaxRttMs(const std::list<CallStats::RttTime>& reports) {
33 int64_t max_rtt_ms = -1;
34 for (const CallStats::RttTime& rtt_time : reports)
sprange2d83d62016-02-19 09:03:26 -080035 max_rtt_ms = std::max(rtt_time.rtt, max_rtt_ms);
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000036 return max_rtt_ms;
37}
38
Tommi38c5d932018-03-27 23:11:09 +020039int64_t GetAvgRttMs(const std::list<CallStats::RttTime>& reports) {
40 RTC_DCHECK(!reports.empty());
pkasting@chromium.org16825b12015-01-12 21:51:21 +000041 int64_t sum = 0;
Tommi38c5d932018-03-27 23:11:09 +020042 for (std::list<CallStats::RttTime>::const_iterator it = reports.begin();
43 it != reports.end(); ++it) {
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000044 sum += it->rtt;
45 }
Tommi38c5d932018-03-27 23:11:09 +020046 return sum / reports.size();
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000047}
48
Tommi38c5d932018-03-27 23:11:09 +020049int64_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
sprange2d83d62016-02-19 09:03:26 -080054 int64_t cur_rtt_ms = GetAvgRttMs(reports);
Tommi38c5d932018-03-27 23:11:09 +020055 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.org8084f952014-12-10 11:04:13 +000062}
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000063
Tommi38c5d932018-03-27 23:11:09 +020064// 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.
69class TemporaryDeregistration {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000070 public:
Tommi38c5d932018-03-27 23:11:09 +020071 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.orgb2f474e2012-11-16 13:57:26 +000079 }
Tommi38c5d932018-03-27 23:11:09 +020080 ~TemporaryDeregistration() {
81 if (deregistered_)
82 process_thread_->RegisterModule(module_, RTC_FROM_HERE);
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +000083 }
84
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000085 private:
Tommi38c5d932018-03-27 23:11:09 +020086 Module* const module_;
87 ProcessThread* const process_thread_;
88 const bool deregistered_;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000089};
90
Tommi38c5d932018-03-27 23:11:09 +020091} // namespace
92
93CallStats::CallStats(Clock* clock, ProcessThread* process_thread)
Peter Boströmd3c94472015-12-09 11:20:58 +010094 : clock_(clock),
Peter Boströmd3c94472015-12-09 11:20:58 +010095 last_process_time_(clock_->TimeInMilliseconds()),
sprange2d83d62016-02-19 09:03:26 -080096 max_rtt_ms_(-1),
97 avg_rtt_ms_(-1),
98 sum_avg_rtt_ms_(0),
99 num_avg_rtt_(0),
Tommi38c5d932018-03-27 23:11:09 +0200100 time_of_first_rtt_ms_(-1),
101 process_thread_(process_thread),
102 process_thread_running_(false) {
103 RTC_DCHECK(process_thread_);
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200104 process_thread_checker_.Detach();
Tommi38c5d932018-03-27 23:11:09 +0200105}
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000106
107CallStats::~CallStats() {
Tommi38c5d932018-03-27 23:11:09 +0200108 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
109 RTC_DCHECK(!process_thread_running_);
sprange2d83d62016-02-19 09:03:26 -0800110 RTC_DCHECK(observers_.empty());
Tommi38c5d932018-03-27 23:11:09 +0200111
sprange2d83d62016-02-19 09:03:26 -0800112 UpdateHistograms();
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000113}
114
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000115int64_t CallStats::TimeUntilNextProcess() {
Tommi38c5d932018-03-27 23:11:09 +0200116 RTC_DCHECK_RUN_ON(&process_thread_checker_);
Peter Boströmd3c94472015-12-09 11:20:58 +0100117 return last_process_time_ + kUpdateIntervalMs - clock_->TimeInMilliseconds();
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000118}
119
pbosa26ac922016-02-25 04:50:01 -0800120void CallStats::Process() {
Tommi38c5d932018-03-27 23:11:09 +0200121 RTC_DCHECK_RUN_ON(&process_thread_checker_);
Peter Boströmd3c94472015-12-09 11:20:58 +0100122 int64_t now = clock_->TimeInMilliseconds();
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000123 last_process_time_ = now;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000124
Tommi38c5d932018-03-27 23:11:09 +0200125 int64_t avg_rtt_ms = avg_rtt_ms_;
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000126 RemoveOldReports(now, &reports_);
Tommi38c5d932018-03-27 23:11:09 +0200127 max_rtt_ms_ = GetMaxRttMs(reports_);
128 avg_rtt_ms = GetNewAvgRttMs(reports_, avg_rtt_ms);
129 {
130 rtc::CritScope lock(&avg_rtt_ms_lock_);
131 avg_rtt_ms_ = avg_rtt_ms;
132 }
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +0000133
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000134 // If there is a valid rtt, update all observers with the max rtt.
sprange2d83d62016-02-19 09:03:26 -0800135 if (max_rtt_ms_ >= 0) {
Tommi38c5d932018-03-27 23:11:09 +0200136 RTC_DCHECK_GE(avg_rtt_ms, 0);
137 for (CallStatsObserver* observer : observers_)
138 observer->OnRttUpdate(avg_rtt_ms, max_rtt_ms_);
sprange2d83d62016-02-19 09:03:26 -0800139 // Sum for Histogram of average RTT reported over the entire call.
Tommi38c5d932018-03-27 23:11:09 +0200140 sum_avg_rtt_ms_ += avg_rtt_ms;
sprange2d83d62016-02-19 09:03:26 -0800141 ++num_avg_rtt_;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000142 }
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000143}
144
Tommi38c5d932018-03-27 23:11:09 +0200145void CallStats::ProcessThreadAttached(ProcessThread* process_thread) {
146 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
147 RTC_DCHECK(!process_thread || process_thread_ == process_thread);
148 process_thread_running_ = process_thread != nullptr;
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +0000149
Tommi38c5d932018-03-27 23:11:09 +0200150 // Whether we just got attached or detached, we clear the
151 // |process_thread_checker_| so that it can be used to protect variables
152 // in either the process thread when it starts again, or UpdateHistograms()
153 // (mutually exclusive).
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200154 process_thread_checker_.Detach();
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000155}
156
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +0000157void CallStats::RegisterStatsObserver(CallStatsObserver* observer) {
Tommi38c5d932018-03-27 23:11:09 +0200158 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
159 TemporaryDeregistration deregister(this, process_thread_,
160 process_thread_running_);
161
Steve Antonbd631a02019-03-28 10:51:27 -0700162 if (!absl::c_linear_search(observers_, observer))
Tommi38c5d932018-03-27 23:11:09 +0200163 observers_.push_back(observer);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000164}
165
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +0000166void CallStats::DeregisterStatsObserver(CallStatsObserver* observer) {
Tommi38c5d932018-03-27 23:11:09 +0200167 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
168 TemporaryDeregistration deregister(this, process_thread_,
169 process_thread_running_);
170 observers_.remove(observer);
171}
172
173int64_t CallStats::LastProcessedRtt() const {
174 rtc::CritScope cs(&avg_rtt_ms_lock_);
175 return avg_rtt_ms_;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000176}
177
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000178void CallStats::OnRttUpdate(int64_t rtt) {
sprange2d83d62016-02-19 09:03:26 -0800179 int64_t now_ms = clock_->TimeInMilliseconds();
Danil Chapovalov1aa75812019-03-05 11:11:35 +0100180 process_thread_->PostTask(ToQueuedTask([rtt, now_ms, this]() {
Tommi38c5d932018-03-27 23:11:09 +0200181 RTC_DCHECK_RUN_ON(&process_thread_checker_);
182 reports_.push_back(RttTime(rtt, now_ms));
183 if (time_of_first_rtt_ms_ == -1)
184 time_of_first_rtt_ms_ = now_ms;
185
186 process_thread_->WakeUp(this);
187 }));
sprange2d83d62016-02-19 09:03:26 -0800188}
189
190void CallStats::UpdateHistograms() {
Tommi38c5d932018-03-27 23:11:09 +0200191 RTC_DCHECK_RUN_ON(&construction_thread_checker_);
192 RTC_DCHECK(!process_thread_running_);
sprange2d83d62016-02-19 09:03:26 -0800193
Tommi38c5d932018-03-27 23:11:09 +0200194 // The extra scope is because we have two 'dcheck run on' thread checkers.
195 // This is a special case since it's safe to access variables on the current
196 // thread that normally are only touched on the process thread.
197 // Since we're not attached to the process thread and/or the process thread
198 // isn't running, it's OK to touch these variables here.
199 {
200 // This method is called on the ctor thread (usually from the dtor, unless
201 // a test calls it). It's a requirement that the function be called when
202 // the process thread is not running (a condition that's met at destruction
203 // time), and thanks to that, we don't need a lock to synchronize against
204 // it.
205 RTC_DCHECK_RUN_ON(&process_thread_checker_);
206
207 if (time_of_first_rtt_ms_ == -1 || num_avg_rtt_ < 1)
208 return;
209
210 int64_t elapsed_sec =
211 (clock_->TimeInMilliseconds() - time_of_first_rtt_ms_) / 1000;
212 if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
213 int64_t avg_rtt_ms = (sum_avg_rtt_ms_ + num_avg_rtt_ / 2) / num_avg_rtt_;
214 RTC_HISTOGRAM_COUNTS_10000(
215 "WebRTC.Video.AverageRoundTripTimeInMilliseconds", avg_rtt_ms);
216 }
sprange2d83d62016-02-19 09:03:26 -0800217 }
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000218}
219
220} // namespace webrtc