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 | |
| 11 | #include "webrtc/video_engine/call_stats.h" |
| 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame^] | 13 | #include <assert.h> |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 14 | |
| 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" |
| 18 | #include "webrtc/system_wrappers/interface/trace.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // A rtt report is considered valid for this long. |
| 23 | const int kRttTimeoutMs = 1500; |
| 24 | // Time interval for updating the observers. |
| 25 | const int kUpdateIntervalMs = 1000; |
| 26 | |
| 27 | class RtcpObserver : public RtcpRttObserver { |
| 28 | public: |
| 29 | explicit RtcpObserver(CallStats* owner) : owner_(owner) {} |
| 30 | virtual ~RtcpObserver() {} |
| 31 | |
| 32 | virtual void OnRttUpdate(uint32_t rtt) { |
| 33 | owner_->OnRttUpdate(rtt); |
| 34 | } |
| 35 | |
| 36 | private: |
| 37 | CallStats* owner_; |
| 38 | |
| 39 | DISALLOW_COPY_AND_ASSIGN(RtcpObserver); |
| 40 | }; |
| 41 | |
| 42 | CallStats::CallStats() |
| 43 | : crit_(CriticalSectionWrapper::CreateCriticalSection()), |
| 44 | rtcp_rtt_observer_(new RtcpObserver(this)), |
stefan@webrtc.org | 8ca8a71 | 2013-04-23 16:48:32 +0000 | [diff] [blame] | 45 | last_process_time_(TickTime::MillisecondTimestamp()) { |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | CallStats::~CallStats() { |
| 49 | assert(observers_.empty()); |
| 50 | } |
| 51 | |
| 52 | int32_t CallStats::TimeUntilNextProcess() { |
| 53 | return last_process_time_ + kUpdateIntervalMs - |
| 54 | TickTime::MillisecondTimestamp(); |
| 55 | } |
| 56 | |
| 57 | int32_t CallStats::Process() { |
| 58 | CriticalSectionScoped cs(crit_.get()); |
| 59 | if (TickTime::MillisecondTimestamp() < last_process_time_ + kUpdateIntervalMs) |
| 60 | return 0; |
| 61 | |
| 62 | // Remove invalid, as in too old, rtt values. |
| 63 | int64_t time_now = TickTime::MillisecondTimestamp(); |
| 64 | while (!reports_.empty() && reports_.front().time + kRttTimeoutMs < |
| 65 | time_now) { |
| 66 | reports_.pop_front(); |
| 67 | } |
| 68 | |
| 69 | // Find the max stored RTT. |
| 70 | uint32_t max_rtt = 0; |
| 71 | for (std::list<RttTime>::const_iterator it = reports_.begin(); |
| 72 | it != reports_.end(); ++it) { |
| 73 | if (it->rtt > max_rtt) |
| 74 | max_rtt = it->rtt; |
| 75 | } |
stefan@webrtc.org | 8ca8a71 | 2013-04-23 16:48:32 +0000 | [diff] [blame] | 76 | |
stefan@webrtc.org | ccd4b2a | 2013-04-23 15:58:23 +0000 | [diff] [blame] | 77 | // If there is a valid rtt, update all observers. |
stefan@webrtc.org | 8ca8a71 | 2013-04-23 16:48:32 +0000 | [diff] [blame] | 78 | if (max_rtt > 0) { |
| 79 | for (std::list<CallStatsObserver*>::iterator it = observers_.begin(); |
| 80 | it != observers_.end(); ++it) { |
| 81 | (*it)->OnRttUpdate(max_rtt); |
| 82 | } |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 83 | } |
| 84 | last_process_time_ = time_now; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | RtcpRttObserver* CallStats::rtcp_rtt_observer() const { |
| 89 | return rtcp_rtt_observer_.get(); |
| 90 | } |
| 91 | |
fischman@webrtc.org | aea96d3 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 92 | void CallStats::RegisterStatsObserver(CallStatsObserver* observer) { |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 93 | CriticalSectionScoped cs(crit_.get()); |
fischman@webrtc.org | aea96d3 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 94 | for (std::list<CallStatsObserver*>::iterator it = observers_.begin(); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 95 | it != observers_.end(); ++it) { |
| 96 | if (*it == observer) |
| 97 | return; |
| 98 | } |
| 99 | observers_.push_back(observer); |
| 100 | } |
| 101 | |
fischman@webrtc.org | aea96d3 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 102 | void CallStats::DeregisterStatsObserver(CallStatsObserver* observer) { |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 103 | CriticalSectionScoped cs(crit_.get()); |
fischman@webrtc.org | aea96d3 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 104 | for (std::list<CallStatsObserver*>::iterator it = observers_.begin(); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 105 | it != observers_.end(); ++it) { |
| 106 | if (*it == observer) { |
| 107 | observers_.erase(it); |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void CallStats::OnRttUpdate(uint32_t rtt) { |
| 114 | CriticalSectionScoped cs(crit_.get()); |
| 115 | int64_t time_now = TickTime::MillisecondTimestamp(); |
| 116 | reports_.push_back(RttTime(rtt, time_now)); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } // namespace webrtc |