blob: 33076c518c2339c4da1810941a806715067757fc [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
Tommi38c5d932018-03-27 23:11:09 +020011#include "video/call_stats.h"
12
kwiberg27f982b2016-03-01 11:52:33 -080013#include <memory>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Tommi38c5d932018-03-27 23:11:09 +020016#include "modules/utility/include/process_thread.h"
17#include "rtc_base/event.h"
18#include "rtc_base/location.h"
19#include "rtc_base/task_queue.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "system_wrappers/include/metrics.h"
21#include "system_wrappers/include/metrics_default.h"
22#include "test/gmock.h"
23#include "test/gtest.h"
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000024
25using ::testing::_;
26using ::testing::AnyNumber;
Tommi38c5d932018-03-27 23:11:09 +020027using ::testing::InvokeWithoutArgs;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000028using ::testing::Return;
29
30namespace webrtc {
31
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +000032class MockStatsObserver : public CallStatsObserver {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000033 public:
34 MockStatsObserver() {}
35 virtual ~MockStatsObserver() {}
36
stefan2328a942015-08-07 04:27:51 -070037 MOCK_METHOD2(OnRttUpdate, void(int64_t, int64_t));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000038};
39
40class CallStatsTest : public ::testing::Test {
Peter Boströmd3c94472015-12-09 11:20:58 +010041 public:
Tommi38c5d932018-03-27 23:11:09 +020042 CallStatsTest() {
43 process_thread_->RegisterModule(&call_stats_, RTC_FROM_HERE);
44 process_thread_->Start();
45 }
46 ~CallStatsTest() override {
47 process_thread_->Stop();
48 process_thread_->DeRegisterModule(&call_stats_);
49 }
Peter Boströmd3c94472015-12-09 11:20:58 +010050
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000051 protected:
Tommi38c5d932018-03-27 23:11:09 +020052 std::unique_ptr<ProcessThread> process_thread_{
53 ProcessThread::Create("CallStats")};
54 SimulatedClock fake_clock_{12345};
55 CallStats call_stats_{&fake_clock_, process_thread_.get()};
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000056};
57
58TEST_F(CallStatsTest, AddAndTriggerCallback) {
Tommi38c5d932018-03-27 23:11:09 +020059 rtc::Event event(false, false);
60
61 static constexpr const int64_t kRtt = 25;
62
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000063 MockStatsObserver stats_observer;
Tommi38c5d932018-03-27 23:11:09 +020064 EXPECT_CALL(stats_observer, OnRttUpdate(kRtt, kRtt))
65 .Times(1)
66 .WillOnce(InvokeWithoutArgs([&event] { event.Set(); }));
67
68 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
69 call_stats_.RegisterStatsObserver(&stats_observer);
sprange2d83d62016-02-19 09:03:26 -080070 EXPECT_EQ(-1, rtcp_rtt_stats->LastProcessedRtt());
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000071
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +000072 rtcp_rtt_stats->OnRttUpdate(kRtt);
Tommi38c5d932018-03-27 23:11:09 +020073
74 EXPECT_TRUE(event.Wait(1000));
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +000075 EXPECT_EQ(kRtt, rtcp_rtt_stats->LastProcessedRtt());
76
Tommi38c5d932018-03-27 23:11:09 +020077 call_stats_.DeregisterStatsObserver(&stats_observer);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000078}
79
80TEST_F(CallStatsTest, ProcessTime) {
Tommi38c5d932018-03-27 23:11:09 +020081 rtc::Event event(false, false);
82
83 static constexpr const int64_t kRtt = 100;
84 static constexpr const int64_t kRtt2 = 80;
85
86 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
87
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000088 MockStatsObserver stats_observer;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000089
Tommi38c5d932018-03-27 23:11:09 +020090 EXPECT_CALL(stats_observer, OnRttUpdate(kRtt, kRtt))
91 .Times(2)
92 .WillOnce(InvokeWithoutArgs([this] {
93 // Advance clock and verify we get an update.
94 fake_clock_.AdvanceTimeMilliseconds(CallStats::kUpdateIntervalMs);
95 }))
96 .WillRepeatedly(InvokeWithoutArgs([this, rtcp_rtt_stats] {
97 rtcp_rtt_stats->OnRttUpdate(kRtt2);
98 // Advance clock just too little to get an update.
99 fake_clock_.AdvanceTimeMilliseconds(CallStats::kUpdateIntervalMs - 1);
100 }));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000101
Tommi38c5d932018-03-27 23:11:09 +0200102 // In case you're reading this and wondering how this number is arrived at,
103 // please see comments in the ChangeRtt test that go into some detail.
104 static constexpr const int64_t kLastAvg = 94;
105 EXPECT_CALL(stats_observer, OnRttUpdate(kLastAvg, kRtt2))
106 .Times(1)
107 .WillOnce(InvokeWithoutArgs([&event] { event.Set(); }));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000108
Tommi38c5d932018-03-27 23:11:09 +0200109 call_stats_.RegisterStatsObserver(&stats_observer);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000110
Tommi38c5d932018-03-27 23:11:09 +0200111 rtcp_rtt_stats->OnRttUpdate(kRtt);
112 EXPECT_TRUE(event.Wait(1000));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000113
Tommi38c5d932018-03-27 23:11:09 +0200114 call_stats_.DeregisterStatsObserver(&stats_observer);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000115}
116
117// Verify all observers get correct estimates and observers can be added and
118// removed.
119TEST_F(CallStatsTest, MultipleObservers) {
120 MockStatsObserver stats_observer_1;
Tommi38c5d932018-03-27 23:11:09 +0200121 call_stats_.RegisterStatsObserver(&stats_observer_1);
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000122 // Add the second observer twice, there should still be only one report to the
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000123 // observer.
124 MockStatsObserver stats_observer_2;
Tommi38c5d932018-03-27 23:11:09 +0200125 call_stats_.RegisterStatsObserver(&stats_observer_2);
126 call_stats_.RegisterStatsObserver(&stats_observer_2);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000127
Tommi38c5d932018-03-27 23:11:09 +0200128 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
129 static constexpr const int64_t kRtt = 100;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000130
131 // Verify both observers are updated.
Tommi38c5d932018-03-27 23:11:09 +0200132 rtc::Event ev1(false, false);
133 rtc::Event ev2(false, false);
134 EXPECT_CALL(stats_observer_1, OnRttUpdate(kRtt, kRtt))
135 .Times(AnyNumber())
136 .WillOnce(InvokeWithoutArgs([&ev1] { ev1.Set(); }))
137 .WillRepeatedly(Return());
138 EXPECT_CALL(stats_observer_2, OnRttUpdate(kRtt, kRtt))
139 .Times(AnyNumber())
140 .WillOnce(InvokeWithoutArgs([&ev2] { ev2.Set(); }))
141 .WillRepeatedly(Return());
142 rtcp_rtt_stats->OnRttUpdate(kRtt);
143 ASSERT_TRUE(ev1.Wait(100));
144 ASSERT_TRUE(ev2.Wait(100));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000145
146 // Deregister the second observer and verify update is only sent to the first
147 // observer.
Tommi38c5d932018-03-27 23:11:09 +0200148 call_stats_.DeregisterStatsObserver(&stats_observer_2);
149
150 EXPECT_CALL(stats_observer_1, OnRttUpdate(kRtt, kRtt))
151 .Times(AnyNumber())
152 .WillOnce(InvokeWithoutArgs([&ev1] { ev1.Set(); }))
153 .WillRepeatedly(Return());
stefan2328a942015-08-07 04:27:51 -0700154 EXPECT_CALL(stats_observer_2, OnRttUpdate(kRtt, kRtt)).Times(0);
Tommi38c5d932018-03-27 23:11:09 +0200155 rtcp_rtt_stats->OnRttUpdate(kRtt);
156 ASSERT_TRUE(ev1.Wait(100));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000157
158 // Deregister the first observer.
Tommi38c5d932018-03-27 23:11:09 +0200159 call_stats_.DeregisterStatsObserver(&stats_observer_1);
160
161 // Now make sure we don't get any callbacks.
stefan2328a942015-08-07 04:27:51 -0700162 EXPECT_CALL(stats_observer_1, OnRttUpdate(kRtt, kRtt)).Times(0);
163 EXPECT_CALL(stats_observer_2, OnRttUpdate(kRtt, kRtt)).Times(0);
Tommi38c5d932018-03-27 23:11:09 +0200164 rtcp_rtt_stats->OnRttUpdate(kRtt);
165
166 // Force a call to Process().
167 process_thread_->WakeUp(&call_stats_);
168
169 // Flush the queue on the process thread to make sure we return after
170 // Process() has been called.
171 rtc::Event event(false, false);
172 process_thread_->PostTask(rtc::NewClosure([&event]() { event.Set(); }));
173 event.Wait(rtc::Event::kForever);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000174}
175
176// Verify increasing and decreasing rtt triggers callbacks with correct values.
177TEST_F(CallStatsTest, ChangeRtt) {
Tommi38c5d932018-03-27 23:11:09 +0200178 // TODO(tommi): This test assumes things about how old reports are removed
179 // inside of call_stats.cc. The threshold ms value is 1500ms, but it's not
180 // clear here that how the clock is advanced, affects that algorithm and
181 // subsequently the average reported rtt.
182
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000183 MockStatsObserver stats_observer;
Tommi38c5d932018-03-27 23:11:09 +0200184 call_stats_.RegisterStatsObserver(&stats_observer);
185 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000186
Tommi38c5d932018-03-27 23:11:09 +0200187 rtc::Event event(false, false);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000188
Tommi38c5d932018-03-27 23:11:09 +0200189 static constexpr const int64_t kFirstRtt = 100;
190 static constexpr const int64_t kLowRtt = kFirstRtt - 20;
191 static constexpr const int64_t kHighRtt = kFirstRtt + 20;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000192
Tommi38c5d932018-03-27 23:11:09 +0200193 EXPECT_CALL(stats_observer, OnRttUpdate(kFirstRtt, kFirstRtt))
194 .Times(1)
195 .WillOnce(InvokeWithoutArgs([&rtcp_rtt_stats, this] {
196 fake_clock_.AdvanceTimeMilliseconds(1000);
197 rtcp_rtt_stats->OnRttUpdate(kHighRtt); // Reported at T1 (1000ms).
198 }));
199
200 // TODO(tommi): This relies on the internal algorithms of call_stats.cc.
201 // There's a weight factor there (0.3), that weighs the previous average to
202 // the new one by 70%, so the number 103 in this case is arrived at like so:
203 // (100) / 1 * 0.7 + (100+120)/2 * 0.3 = 103
204 static constexpr const int64_t kAvgRtt1 = 103;
205 EXPECT_CALL(stats_observer, OnRttUpdate(kAvgRtt1, kHighRtt))
206 .Times(1)
207 .WillOnce(InvokeWithoutArgs([&rtcp_rtt_stats, this] {
208 // This interacts with an internal implementation detail in call_stats
209 // that decays the oldest rtt value. See more below.
210 fake_clock_.AdvanceTimeMilliseconds(1000);
211 rtcp_rtt_stats->OnRttUpdate(kLowRtt); // Reported at T2 (2000ms).
212 }));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000213
214 // Increase time enough for a new update, but not too much to make the
215 // rtt invalid. Report a lower rtt and verify the old/high value still is sent
216 // in the callback.
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000217
Tommi38c5d932018-03-27 23:11:09 +0200218 // Here, enough time must have passed in order to remove exactly the first
219 // report and nothing else (>1500ms has passed since the first rtt).
220 // So, this value is arrived by doing:
221 // (kAvgRtt1)/1 * 0.7 + (kHighRtt+kLowRtt)/2 * 0.3 = 102.1
222 static constexpr const int64_t kAvgRtt2 = 102;
223 EXPECT_CALL(stats_observer, OnRttUpdate(kAvgRtt2, kHighRtt))
224 .Times(1)
225 .WillOnce(InvokeWithoutArgs([this] {
226 // Advance time to make the high report invalid, the lower rtt should
227 // now be in the callback.
228 fake_clock_.AdvanceTimeMilliseconds(1000);
229 }));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000230
Tommi38c5d932018-03-27 23:11:09 +0200231 static constexpr const int64_t kAvgRtt3 = 95;
232 EXPECT_CALL(stats_observer, OnRttUpdate(kAvgRtt3, kLowRtt))
233 .Times(1)
234 .WillOnce(InvokeWithoutArgs([&event] { event.Set(); }));
235
236 // Trigger the first rtt value and set off the chain of callbacks.
237 rtcp_rtt_stats->OnRttUpdate(kFirstRtt); // Reported at T0 (0ms).
238 EXPECT_TRUE(event.Wait(1000));
239
240 call_stats_.DeregisterStatsObserver(&stats_observer);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000241}
242
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000243TEST_F(CallStatsTest, LastProcessedRtt) {
Tommi38c5d932018-03-27 23:11:09 +0200244 rtc::Event event(false, false);
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000245 MockStatsObserver stats_observer;
Tommi38c5d932018-03-27 23:11:09 +0200246 call_stats_.RegisterStatsObserver(&stats_observer);
247 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
248
249 static constexpr const int64_t kRttLow = 10;
250 static constexpr const int64_t kRttHigh = 30;
251 // The following two average numbers dependend on average + weight
252 // calculations in call_stats.cc.
253 static constexpr const int64_t kAvgRtt1 = 13;
254 static constexpr const int64_t kAvgRtt2 = 15;
255
256 EXPECT_CALL(stats_observer, OnRttUpdate(kRttLow, kRttLow))
257 .Times(1)
258 .WillOnce(InvokeWithoutArgs([rtcp_rtt_stats] {
259 EXPECT_EQ(kRttLow, rtcp_rtt_stats->LastProcessedRtt());
260 // Don't advance the clock to make sure that low and high rtt values
261 // are associated with the same time stamp.
262 rtcp_rtt_stats->OnRttUpdate(kRttHigh);
263 }));
264
265 EXPECT_CALL(stats_observer, OnRttUpdate(kAvgRtt1, kRttHigh))
266 .Times(1)
267 .WillOnce(InvokeWithoutArgs([rtcp_rtt_stats, this] {
268 EXPECT_EQ(kAvgRtt1, rtcp_rtt_stats->LastProcessedRtt());
269 fake_clock_.AdvanceTimeMilliseconds(CallStats::kUpdateIntervalMs);
270 rtcp_rtt_stats->OnRttUpdate(kRttLow);
271 rtcp_rtt_stats->OnRttUpdate(kRttHigh);
272 }));
273
274 EXPECT_CALL(stats_observer, OnRttUpdate(kAvgRtt2, kRttHigh))
275 .Times(1)
276 .WillOnce(InvokeWithoutArgs([rtcp_rtt_stats, &event] {
277 EXPECT_EQ(kAvgRtt2, rtcp_rtt_stats->LastProcessedRtt());
278 event.Set();
279 }));
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000280
281 // Set a first values and verify that LastProcessedRtt initially returns the
282 // average rtt.
Tommi38c5d932018-03-27 23:11:09 +0200283 fake_clock_.AdvanceTimeMilliseconds(CallStats::kUpdateIntervalMs);
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000284 rtcp_rtt_stats->OnRttUpdate(kRttLow);
Tommi38c5d932018-03-27 23:11:09 +0200285 EXPECT_TRUE(event.Wait(1000));
286 EXPECT_EQ(kAvgRtt2, rtcp_rtt_stats->LastProcessedRtt());
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000287
Tommi38c5d932018-03-27 23:11:09 +0200288 call_stats_.DeregisterStatsObserver(&stats_observer);
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000289}
290
sprange2d83d62016-02-19 09:03:26 -0800291TEST_F(CallStatsTest, ProducesHistogramMetrics) {
asapersson01d70a32016-05-20 06:29:46 -0700292 metrics::Reset();
Tommi38c5d932018-03-27 23:11:09 +0200293 rtc::Event event(false, false);
294 static constexpr const int64_t kRtt = 123;
295 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
296 MockStatsObserver stats_observer;
297 call_stats_.RegisterStatsObserver(&stats_observer);
298 EXPECT_CALL(stats_observer, OnRttUpdate(kRtt, kRtt))
299 .Times(AnyNumber())
300 .WillOnce(InvokeWithoutArgs([&event] { event.Set(); }))
301 .WillRepeatedly(Return());
302
sprange2d83d62016-02-19 09:03:26 -0800303 rtcp_rtt_stats->OnRttUpdate(kRtt);
Tommi38c5d932018-03-27 23:11:09 +0200304 fake_clock_.AdvanceTimeMilliseconds(metrics::kMinRunTimeInSeconds *
305 CallStats::kUpdateIntervalMs);
sprange2d83d62016-02-19 09:03:26 -0800306 rtcp_rtt_stats->OnRttUpdate(kRtt);
Tommi38c5d932018-03-27 23:11:09 +0200307 EXPECT_TRUE(event.Wait(1000));
308
309 call_stats_.DeregisterStatsObserver(&stats_observer);
310
311 process_thread_->Stop();
312 call_stats_.UpdateHistogramsForTest();
sprange2d83d62016-02-19 09:03:26 -0800313
asapersson01d70a32016-05-20 06:29:46 -0700314 EXPECT_EQ(1, metrics::NumSamples(
sprange2d83d62016-02-19 09:03:26 -0800315 "WebRTC.Video.AverageRoundTripTimeInMilliseconds"));
asapersson01d70a32016-05-20 06:29:46 -0700316 EXPECT_EQ(1, metrics::NumEvents(
317 "WebRTC.Video.AverageRoundTripTimeInMilliseconds", kRtt));
sprange2d83d62016-02-19 09:03:26 -0800318}
319
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000320} // namespace webrtc