blob: ccc97fa55db4334a646d7b38d127174662306673 [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
11#include "webrtc/video_engine/call_stats.h"
12
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000014
15#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
16#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
17#include "webrtc/system_wrappers/interface/tick_util.h"
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000018
19namespace webrtc {
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000020namespace {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000021// A rtt report is considered valid for this long.
22const int kRttTimeoutMs = 1500;
23// Time interval for updating the observers.
24const int kUpdateIntervalMs = 1000;
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000025// Weight factor to apply to the average rtt.
26const float kWeightFactor = 0.3f;
27
28void RemoveOldReports(int64_t now, std::list<CallStats::RttTime>* reports) {
29 while (!reports->empty() &&
30 (now - reports->front().time) > kRttTimeoutMs) {
31 reports->pop_front();
32 }
33}
34
35uint32_t GetMaxRttMs(std::list<CallStats::RttTime>* reports) {
36 uint32_t max_rtt_ms = 0;
37 for (std::list<CallStats::RttTime>::const_iterator it = reports->begin();
38 it != reports->end(); ++it) {
39 max_rtt_ms = std::max(it->rtt, max_rtt_ms);
40 }
41 return max_rtt_ms;
42}
43
44uint32_t GetAvgRttMs(std::list<CallStats::RttTime>* reports) {
45 if (reports->empty()) {
46 return 0;
47 }
48 uint32_t sum = 0;
49 for (std::list<CallStats::RttTime>::const_iterator it = reports->begin();
50 it != reports->end(); ++it) {
51 sum += it->rtt;
52 }
53 return sum / reports->size();
54}
55
56void UpdateAvgRttMs(std::list<CallStats::RttTime>* reports, uint32_t* avg_rtt) {
57 uint32_t cur_rtt_ms = GetAvgRttMs(reports);
58 if (cur_rtt_ms == 0) {
59 // Reset.
60 *avg_rtt = 0;
61 return;
62 }
63 if (*avg_rtt == 0) {
64 // Initialize.
65 *avg_rtt = cur_rtt_ms;
66 return;
67 }
68 *avg_rtt = *avg_rtt * (1.0f - kWeightFactor) + cur_rtt_ms * kWeightFactor;
69}
70} // namespace
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000071
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +000072class RtcpObserver : public RtcpRttStats {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000073 public:
74 explicit RtcpObserver(CallStats* owner) : owner_(owner) {}
75 virtual ~RtcpObserver() {}
76
77 virtual void OnRttUpdate(uint32_t rtt) {
78 owner_->OnRttUpdate(rtt);
79 }
80
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000081 // Returns the average RTT.
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +000082 virtual uint32_t LastProcessedRtt() const {
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000083 return owner_->avg_rtt_ms();
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +000084 }
85
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000086 private:
87 CallStats* owner_;
88
89 DISALLOW_COPY_AND_ASSIGN(RtcpObserver);
90};
91
92CallStats::CallStats()
93 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +000094 rtcp_rtt_stats_(new RtcpObserver(this)),
95 last_process_time_(TickTime::MillisecondTimestamp()),
asapersson@webrtc.org8084f952014-12-10 11:04:13 +000096 max_rtt_ms_(0),
97 avg_rtt_ms_(0) {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000098}
99
100CallStats::~CallStats() {
101 assert(observers_.empty());
102}
103
104int32_t CallStats::TimeUntilNextProcess() {
105 return last_process_time_ + kUpdateIntervalMs -
106 TickTime::MillisecondTimestamp();
107}
108
109int32_t CallStats::Process() {
110 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000111 int64_t now = TickTime::MillisecondTimestamp();
112 if (now < last_process_time_ + kUpdateIntervalMs)
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000113 return 0;
114
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000115 last_process_time_ = now;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000116
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000117 RemoveOldReports(now, &reports_);
118 max_rtt_ms_ = GetMaxRttMs(&reports_);
119 UpdateAvgRttMs(&reports_, &avg_rtt_ms_);
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +0000120
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000121 // If there is a valid rtt, update all observers with the max rtt.
122 // TODO(asapersson): Consider changing this to report the average rtt.
123 if (max_rtt_ms_ > 0) {
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +0000124 for (std::list<CallStatsObserver*>::iterator it = observers_.begin();
125 it != observers_.end(); ++it) {
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000126 (*it)->OnRttUpdate(max_rtt_ms_);
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +0000127 }
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000128 }
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000129 return 0;
130}
131
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000132uint32_t CallStats::avg_rtt_ms() const {
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +0000133 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000134 return avg_rtt_ms_;
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +0000135}
136
137RtcpRttStats* CallStats::rtcp_rtt_stats() const {
138 return rtcp_rtt_stats_.get();
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000139}
140
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +0000141void CallStats::RegisterStatsObserver(CallStatsObserver* observer) {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000142 CriticalSectionScoped cs(crit_.get());
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +0000143 for (std::list<CallStatsObserver*>::iterator it = observers_.begin();
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000144 it != observers_.end(); ++it) {
145 if (*it == observer)
146 return;
147 }
148 observers_.push_back(observer);
149}
150
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +0000151void CallStats::DeregisterStatsObserver(CallStatsObserver* observer) {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000152 CriticalSectionScoped cs(crit_.get());
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +0000153 for (std::list<CallStatsObserver*>::iterator it = observers_.begin();
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000154 it != observers_.end(); ++it) {
155 if (*it == observer) {
156 observers_.erase(it);
157 return;
158 }
159 }
160}
161
162void CallStats::OnRttUpdate(uint32_t rtt) {
163 CriticalSectionScoped cs(crit_.get());
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000164 reports_.push_back(RttTime(rtt, TickTime::MillisecondTimestamp()));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000165}
166
167} // namespace webrtc