| OLD | NEW |
| (Empty) |
| 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 | |
| 11 #include "webrtc/video_engine/call_stats.h" | |
| 12 | |
| 13 #include <assert.h> | |
| 14 | |
| 15 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | |
| 16 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
| 17 #include "webrtc/system_wrappers/include/tick_util.h" | |
| 18 | |
| 19 namespace webrtc { | |
| 20 namespace { | |
| 21 // Time interval for updating the observers. | |
| 22 const int64_t kUpdateIntervalMs = 1000; | |
| 23 // Weight factor to apply to the average rtt. | |
| 24 const float kWeightFactor = 0.3f; | |
| 25 | |
| 26 void RemoveOldReports(int64_t now, std::list<CallStats::RttTime>* reports) { | |
| 27 // A rtt report is considered valid for this long. | |
| 28 const int64_t kRttTimeoutMs = 1500; | |
| 29 while (!reports->empty() && | |
| 30 (now - reports->front().time) > kRttTimeoutMs) { | |
| 31 reports->pop_front(); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 int64_t GetMaxRttMs(std::list<CallStats::RttTime>* reports) { | |
| 36 int64_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 | |
| 44 int64_t GetAvgRttMs(std::list<CallStats::RttTime>* reports) { | |
| 45 if (reports->empty()) { | |
| 46 return 0; | |
| 47 } | |
| 48 int64_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 | |
| 56 void UpdateAvgRttMs(std::list<CallStats::RttTime>* reports, int64_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 | |
| 71 | |
| 72 class RtcpObserver : public RtcpRttStats { | |
| 73 public: | |
| 74 explicit RtcpObserver(CallStats* owner) : owner_(owner) {} | |
| 75 virtual ~RtcpObserver() {} | |
| 76 | |
| 77 virtual void OnRttUpdate(int64_t rtt) { | |
| 78 owner_->OnRttUpdate(rtt); | |
| 79 } | |
| 80 | |
| 81 // Returns the average RTT. | |
| 82 virtual int64_t LastProcessedRtt() const { | |
| 83 return owner_->avg_rtt_ms(); | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 CallStats* owner_; | |
| 88 | |
| 89 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpObserver); | |
| 90 }; | |
| 91 | |
| 92 CallStats::CallStats() | |
| 93 : crit_(CriticalSectionWrapper::CreateCriticalSection()), | |
| 94 rtcp_rtt_stats_(new RtcpObserver(this)), | |
| 95 last_process_time_(TickTime::MillisecondTimestamp()), | |
| 96 max_rtt_ms_(0), | |
| 97 avg_rtt_ms_(0) { | |
| 98 } | |
| 99 | |
| 100 CallStats::~CallStats() { | |
| 101 assert(observers_.empty()); | |
| 102 } | |
| 103 | |
| 104 int64_t CallStats::TimeUntilNextProcess() { | |
| 105 return last_process_time_ + kUpdateIntervalMs - | |
| 106 TickTime::MillisecondTimestamp(); | |
| 107 } | |
| 108 | |
| 109 int32_t CallStats::Process() { | |
| 110 CriticalSectionScoped cs(crit_.get()); | |
| 111 int64_t now = TickTime::MillisecondTimestamp(); | |
| 112 if (now < last_process_time_ + kUpdateIntervalMs) | |
| 113 return 0; | |
| 114 | |
| 115 last_process_time_ = now; | |
| 116 | |
| 117 RemoveOldReports(now, &reports_); | |
| 118 max_rtt_ms_ = GetMaxRttMs(&reports_); | |
| 119 UpdateAvgRttMs(&reports_, &avg_rtt_ms_); | |
| 120 | |
| 121 // 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) { | |
| 124 for (std::list<CallStatsObserver*>::iterator it = observers_.begin(); | |
| 125 it != observers_.end(); ++it) { | |
| 126 (*it)->OnRttUpdate(avg_rtt_ms_, max_rtt_ms_); | |
| 127 } | |
| 128 } | |
| 129 return 0; | |
| 130 } | |
| 131 | |
| 132 int64_t CallStats::avg_rtt_ms() const { | |
| 133 CriticalSectionScoped cs(crit_.get()); | |
| 134 return avg_rtt_ms_; | |
| 135 } | |
| 136 | |
| 137 RtcpRttStats* CallStats::rtcp_rtt_stats() const { | |
| 138 return rtcp_rtt_stats_.get(); | |
| 139 } | |
| 140 | |
| 141 void CallStats::RegisterStatsObserver(CallStatsObserver* observer) { | |
| 142 CriticalSectionScoped cs(crit_.get()); | |
| 143 for (std::list<CallStatsObserver*>::iterator it = observers_.begin(); | |
| 144 it != observers_.end(); ++it) { | |
| 145 if (*it == observer) | |
| 146 return; | |
| 147 } | |
| 148 observers_.push_back(observer); | |
| 149 } | |
| 150 | |
| 151 void CallStats::DeregisterStatsObserver(CallStatsObserver* observer) { | |
| 152 CriticalSectionScoped cs(crit_.get()); | |
| 153 for (std::list<CallStatsObserver*>::iterator it = observers_.begin(); | |
| 154 it != observers_.end(); ++it) { | |
| 155 if (*it == observer) { | |
| 156 observers_.erase(it); | |
| 157 return; | |
| 158 } | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 void CallStats::OnRttUpdate(int64_t rtt) { | |
| 163 CriticalSectionScoped cs(crit_.get()); | |
| 164 reports_.push_back(RttTime(rtt, TickTime::MillisecondTimestamp())); | |
| 165 } | |
| 166 | |
| 167 } // namespace webrtc | |
| OLD | NEW |