blob: 0184e5b32e1b96700be709ab29d5e58d66cc4d72 [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"
18#include "webrtc/system_wrappers/interface/trace.h"
19
20namespace webrtc {
21
22// A rtt report is considered valid for this long.
23const int kRttTimeoutMs = 1500;
24// Time interval for updating the observers.
25const int kUpdateIntervalMs = 1000;
26
27class 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
42CallStats::CallStats()
43 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
44 rtcp_rtt_observer_(new RtcpObserver(this)),
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +000045 last_process_time_(TickTime::MillisecondTimestamp()) {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000046}
47
48CallStats::~CallStats() {
49 assert(observers_.empty());
50}
51
52int32_t CallStats::TimeUntilNextProcess() {
53 return last_process_time_ + kUpdateIntervalMs -
54 TickTime::MillisecondTimestamp();
55}
56
57int32_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.org8ca8a712013-04-23 16:48:32 +000076
stefan@webrtc.orgccd4b2a2013-04-23 15:58:23 +000077 // If there is a valid rtt, update all observers.
stefan@webrtc.org8ca8a712013-04-23 16:48:32 +000078 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.orgb2f474e2012-11-16 13:57:26 +000083 }
84 last_process_time_ = time_now;
85 return 0;
86}
87
88RtcpRttObserver* CallStats::rtcp_rtt_observer() const {
89 return rtcp_rtt_observer_.get();
90}
91
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +000092void CallStats::RegisterStatsObserver(CallStatsObserver* observer) {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000093 CriticalSectionScoped cs(crit_.get());
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +000094 for (std::list<CallStatsObserver*>::iterator it = observers_.begin();
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000095 it != observers_.end(); ++it) {
96 if (*it == observer)
97 return;
98 }
99 observers_.push_back(observer);
100}
101
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +0000102void CallStats::DeregisterStatsObserver(CallStatsObserver* observer) {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000103 CriticalSectionScoped cs(crit_.get());
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +0000104 for (std::list<CallStatsObserver*>::iterator it = observers_.begin();
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000105 it != observers_.end(); ++it) {
106 if (*it == observer) {
107 observers_.erase(it);
108 return;
109 }
110 }
111}
112
113void 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.orgb2f474e2012-11-16 13:57:26 +0000117}
118
119} // namespace webrtc