blob: 85b9eb951d2445f3f55e851d92208d13ee7b9c86 [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"
Danil Chapovalov1aa75812019-03-05 11:11:35 +010019#include "rtc_base/task_utils/to_queued_task.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "test/gmock.h"
22#include "test/gtest.h"
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000023
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000024using ::testing::AnyNumber;
Tommi38c5d932018-03-27 23:11:09 +020025using ::testing::InvokeWithoutArgs;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000026using ::testing::Return;
27
28namespace webrtc {
29
fischman@webrtc.orgaea96d32013-02-19 22:09:36 +000030class MockStatsObserver : public CallStatsObserver {
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000031 public:
32 MockStatsObserver() {}
33 virtual ~MockStatsObserver() {}
34
stefan2328a942015-08-07 04:27:51 -070035 MOCK_METHOD2(OnRttUpdate, void(int64_t, int64_t));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000036};
37
38class CallStatsTest : public ::testing::Test {
Peter Boströmd3c94472015-12-09 11:20:58 +010039 public:
Tommi38c5d932018-03-27 23:11:09 +020040 CallStatsTest() {
41 process_thread_->RegisterModule(&call_stats_, RTC_FROM_HERE);
42 process_thread_->Start();
43 }
44 ~CallStatsTest() override {
45 process_thread_->Stop();
46 process_thread_->DeRegisterModule(&call_stats_);
47 }
Peter Boströmd3c94472015-12-09 11:20:58 +010048
Tommic89468a2019-08-05 15:13:20 +020049 // Queues an rtt update call on the process thread.
50 void AsyncSimulateRttUpdate(int64_t rtt) {
51 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
52 process_thread_->PostTask(ToQueuedTask(
53 [rtcp_rtt_stats, rtt] { rtcp_rtt_stats->OnRttUpdate(rtt); }));
54 }
55
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000056 protected:
Tommi38c5d932018-03-27 23:11:09 +020057 std::unique_ptr<ProcessThread> process_thread_{
58 ProcessThread::Create("CallStats")};
59 SimulatedClock fake_clock_{12345};
60 CallStats call_stats_{&fake_clock_, process_thread_.get()};
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000061};
62
63TEST_F(CallStatsTest, AddAndTriggerCallback) {
Niels Möllerc572ff32018-11-07 08:43:50 +010064 rtc::Event event;
Tommi38c5d932018-03-27 23:11:09 +020065
66 static constexpr const int64_t kRtt = 25;
67
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000068 MockStatsObserver stats_observer;
Tommi38c5d932018-03-27 23:11:09 +020069 EXPECT_CALL(stats_observer, OnRttUpdate(kRtt, kRtt))
70 .Times(1)
71 .WillOnce(InvokeWithoutArgs([&event] { event.Set(); }));
72
73 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
74 call_stats_.RegisterStatsObserver(&stats_observer);
sprange2d83d62016-02-19 09:03:26 -080075 EXPECT_EQ(-1, rtcp_rtt_stats->LastProcessedRtt());
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000076
Tommic89468a2019-08-05 15:13:20 +020077 AsyncSimulateRttUpdate(kRtt);
Tommi38c5d932018-03-27 23:11:09 +020078
79 EXPECT_TRUE(event.Wait(1000));
Tommic89468a2019-08-05 15:13:20 +020080
asapersson@webrtc.org1ae1d0c2013-11-20 12:46:11 +000081 EXPECT_EQ(kRtt, rtcp_rtt_stats->LastProcessedRtt());
82
Tommi38c5d932018-03-27 23:11:09 +020083 call_stats_.DeregisterStatsObserver(&stats_observer);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000084}
85
86TEST_F(CallStatsTest, ProcessTime) {
Niels Möllerc572ff32018-11-07 08:43:50 +010087 rtc::Event event;
Tommi38c5d932018-03-27 23:11:09 +020088
89 static constexpr const int64_t kRtt = 100;
90 static constexpr const int64_t kRtt2 = 80;
91
92 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
93
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000094 MockStatsObserver stats_observer;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +000095
Tommi38c5d932018-03-27 23:11:09 +020096 EXPECT_CALL(stats_observer, OnRttUpdate(kRtt, kRtt))
97 .Times(2)
98 .WillOnce(InvokeWithoutArgs([this] {
99 // Advance clock and verify we get an update.
100 fake_clock_.AdvanceTimeMilliseconds(CallStats::kUpdateIntervalMs);
101 }))
102 .WillRepeatedly(InvokeWithoutArgs([this, rtcp_rtt_stats] {
103 rtcp_rtt_stats->OnRttUpdate(kRtt2);
104 // Advance clock just too little to get an update.
105 fake_clock_.AdvanceTimeMilliseconds(CallStats::kUpdateIntervalMs - 1);
106 }));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000107
Tommi38c5d932018-03-27 23:11:09 +0200108 // In case you're reading this and wondering how this number is arrived at,
109 // please see comments in the ChangeRtt test that go into some detail.
110 static constexpr const int64_t kLastAvg = 94;
111 EXPECT_CALL(stats_observer, OnRttUpdate(kLastAvg, kRtt2))
112 .Times(1)
113 .WillOnce(InvokeWithoutArgs([&event] { event.Set(); }));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000114
Tommi38c5d932018-03-27 23:11:09 +0200115 call_stats_.RegisterStatsObserver(&stats_observer);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000116
Tommic89468a2019-08-05 15:13:20 +0200117 AsyncSimulateRttUpdate(kRtt);
Tommi38c5d932018-03-27 23:11:09 +0200118 EXPECT_TRUE(event.Wait(1000));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000119
Tommi38c5d932018-03-27 23:11:09 +0200120 call_stats_.DeregisterStatsObserver(&stats_observer);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000121}
122
123// Verify all observers get correct estimates and observers can be added and
124// removed.
125TEST_F(CallStatsTest, MultipleObservers) {
126 MockStatsObserver stats_observer_1;
Tommi38c5d932018-03-27 23:11:09 +0200127 call_stats_.RegisterStatsObserver(&stats_observer_1);
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000128 // Add the second observer twice, there should still be only one report to the
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000129 // observer.
130 MockStatsObserver stats_observer_2;
Tommi38c5d932018-03-27 23:11:09 +0200131 call_stats_.RegisterStatsObserver(&stats_observer_2);
132 call_stats_.RegisterStatsObserver(&stats_observer_2);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000133
Tommi38c5d932018-03-27 23:11:09 +0200134 static constexpr const int64_t kRtt = 100;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000135
136 // Verify both observers are updated.
Niels Möllerc572ff32018-11-07 08:43:50 +0100137 rtc::Event ev1;
138 rtc::Event ev2;
Tommi38c5d932018-03-27 23:11:09 +0200139 EXPECT_CALL(stats_observer_1, OnRttUpdate(kRtt, kRtt))
140 .Times(AnyNumber())
141 .WillOnce(InvokeWithoutArgs([&ev1] { ev1.Set(); }))
142 .WillRepeatedly(Return());
143 EXPECT_CALL(stats_observer_2, OnRttUpdate(kRtt, kRtt))
144 .Times(AnyNumber())
145 .WillOnce(InvokeWithoutArgs([&ev2] { ev2.Set(); }))
146 .WillRepeatedly(Return());
Tommic89468a2019-08-05 15:13:20 +0200147 AsyncSimulateRttUpdate(kRtt);
Tommi38c5d932018-03-27 23:11:09 +0200148 ASSERT_TRUE(ev1.Wait(100));
149 ASSERT_TRUE(ev2.Wait(100));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000150
151 // Deregister the second observer and verify update is only sent to the first
152 // observer.
Tommi38c5d932018-03-27 23:11:09 +0200153 call_stats_.DeregisterStatsObserver(&stats_observer_2);
154
155 EXPECT_CALL(stats_observer_1, OnRttUpdate(kRtt, kRtt))
156 .Times(AnyNumber())
157 .WillOnce(InvokeWithoutArgs([&ev1] { ev1.Set(); }))
158 .WillRepeatedly(Return());
stefan2328a942015-08-07 04:27:51 -0700159 EXPECT_CALL(stats_observer_2, OnRttUpdate(kRtt, kRtt)).Times(0);
Tommic89468a2019-08-05 15:13:20 +0200160 AsyncSimulateRttUpdate(kRtt);
Tommi38c5d932018-03-27 23:11:09 +0200161 ASSERT_TRUE(ev1.Wait(100));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000162
163 // Deregister the first observer.
Tommi38c5d932018-03-27 23:11:09 +0200164 call_stats_.DeregisterStatsObserver(&stats_observer_1);
165
166 // Now make sure we don't get any callbacks.
stefan2328a942015-08-07 04:27:51 -0700167 EXPECT_CALL(stats_observer_1, OnRttUpdate(kRtt, kRtt)).Times(0);
168 EXPECT_CALL(stats_observer_2, OnRttUpdate(kRtt, kRtt)).Times(0);
Tommic89468a2019-08-05 15:13:20 +0200169 AsyncSimulateRttUpdate(kRtt);
Tommi38c5d932018-03-27 23:11:09 +0200170
171 // Force a call to Process().
172 process_thread_->WakeUp(&call_stats_);
173
174 // Flush the queue on the process thread to make sure we return after
175 // Process() has been called.
Niels Möllerc572ff32018-11-07 08:43:50 +0100176 rtc::Event event;
Danil Chapovalov1aa75812019-03-05 11:11:35 +0100177 process_thread_->PostTask(ToQueuedTask([&event] { event.Set(); }));
Tommi38c5d932018-03-27 23:11:09 +0200178 event.Wait(rtc::Event::kForever);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000179}
180
181// Verify increasing and decreasing rtt triggers callbacks with correct values.
182TEST_F(CallStatsTest, ChangeRtt) {
Tommi38c5d932018-03-27 23:11:09 +0200183 // TODO(tommi): This test assumes things about how old reports are removed
184 // inside of call_stats.cc. The threshold ms value is 1500ms, but it's not
185 // clear here that how the clock is advanced, affects that algorithm and
186 // subsequently the average reported rtt.
187
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000188 MockStatsObserver stats_observer;
Tommi38c5d932018-03-27 23:11:09 +0200189 call_stats_.RegisterStatsObserver(&stats_observer);
190 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000191
Niels Möllerc572ff32018-11-07 08:43:50 +0100192 rtc::Event event;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000193
Tommi38c5d932018-03-27 23:11:09 +0200194 static constexpr const int64_t kFirstRtt = 100;
195 static constexpr const int64_t kLowRtt = kFirstRtt - 20;
196 static constexpr const int64_t kHighRtt = kFirstRtt + 20;
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000197
Tommi38c5d932018-03-27 23:11:09 +0200198 EXPECT_CALL(stats_observer, OnRttUpdate(kFirstRtt, kFirstRtt))
199 .Times(1)
200 .WillOnce(InvokeWithoutArgs([&rtcp_rtt_stats, this] {
201 fake_clock_.AdvanceTimeMilliseconds(1000);
202 rtcp_rtt_stats->OnRttUpdate(kHighRtt); // Reported at T1 (1000ms).
203 }));
204
205 // TODO(tommi): This relies on the internal algorithms of call_stats.cc.
206 // There's a weight factor there (0.3), that weighs the previous average to
207 // the new one by 70%, so the number 103 in this case is arrived at like so:
208 // (100) / 1 * 0.7 + (100+120)/2 * 0.3 = 103
209 static constexpr const int64_t kAvgRtt1 = 103;
210 EXPECT_CALL(stats_observer, OnRttUpdate(kAvgRtt1, kHighRtt))
211 .Times(1)
212 .WillOnce(InvokeWithoutArgs([&rtcp_rtt_stats, this] {
213 // This interacts with an internal implementation detail in call_stats
214 // that decays the oldest rtt value. See more below.
215 fake_clock_.AdvanceTimeMilliseconds(1000);
216 rtcp_rtt_stats->OnRttUpdate(kLowRtt); // Reported at T2 (2000ms).
217 }));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000218
219 // Increase time enough for a new update, but not too much to make the
220 // rtt invalid. Report a lower rtt and verify the old/high value still is sent
221 // in the callback.
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000222
Tommi38c5d932018-03-27 23:11:09 +0200223 // Here, enough time must have passed in order to remove exactly the first
224 // report and nothing else (>1500ms has passed since the first rtt).
225 // So, this value is arrived by doing:
226 // (kAvgRtt1)/1 * 0.7 + (kHighRtt+kLowRtt)/2 * 0.3 = 102.1
227 static constexpr const int64_t kAvgRtt2 = 102;
228 EXPECT_CALL(stats_observer, OnRttUpdate(kAvgRtt2, kHighRtt))
229 .Times(1)
230 .WillOnce(InvokeWithoutArgs([this] {
231 // Advance time to make the high report invalid, the lower rtt should
232 // now be in the callback.
233 fake_clock_.AdvanceTimeMilliseconds(1000);
234 }));
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000235
Tommi38c5d932018-03-27 23:11:09 +0200236 static constexpr const int64_t kAvgRtt3 = 95;
237 EXPECT_CALL(stats_observer, OnRttUpdate(kAvgRtt3, kLowRtt))
238 .Times(1)
239 .WillOnce(InvokeWithoutArgs([&event] { event.Set(); }));
240
241 // Trigger the first rtt value and set off the chain of callbacks.
Tommic89468a2019-08-05 15:13:20 +0200242 AsyncSimulateRttUpdate(kFirstRtt); // Reported at T0 (0ms).
Tommi38c5d932018-03-27 23:11:09 +0200243 EXPECT_TRUE(event.Wait(1000));
244
245 call_stats_.DeregisterStatsObserver(&stats_observer);
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000246}
247
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000248TEST_F(CallStatsTest, LastProcessedRtt) {
Niels Möllerc572ff32018-11-07 08:43:50 +0100249 rtc::Event event;
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000250 MockStatsObserver stats_observer;
Tommi38c5d932018-03-27 23:11:09 +0200251 call_stats_.RegisterStatsObserver(&stats_observer);
252 RtcpRttStats* rtcp_rtt_stats = &call_stats_;
253
254 static constexpr const int64_t kRttLow = 10;
255 static constexpr const int64_t kRttHigh = 30;
256 // The following two average numbers dependend on average + weight
257 // calculations in call_stats.cc.
258 static constexpr const int64_t kAvgRtt1 = 13;
259 static constexpr const int64_t kAvgRtt2 = 15;
260
261 EXPECT_CALL(stats_observer, OnRttUpdate(kRttLow, kRttLow))
262 .Times(1)
263 .WillOnce(InvokeWithoutArgs([rtcp_rtt_stats] {
264 EXPECT_EQ(kRttLow, rtcp_rtt_stats->LastProcessedRtt());
265 // Don't advance the clock to make sure that low and high rtt values
266 // are associated with the same time stamp.
267 rtcp_rtt_stats->OnRttUpdate(kRttHigh);
268 }));
269
270 EXPECT_CALL(stats_observer, OnRttUpdate(kAvgRtt1, kRttHigh))
271 .Times(1)
272 .WillOnce(InvokeWithoutArgs([rtcp_rtt_stats, this] {
273 EXPECT_EQ(kAvgRtt1, rtcp_rtt_stats->LastProcessedRtt());
274 fake_clock_.AdvanceTimeMilliseconds(CallStats::kUpdateIntervalMs);
275 rtcp_rtt_stats->OnRttUpdate(kRttLow);
276 rtcp_rtt_stats->OnRttUpdate(kRttHigh);
277 }));
278
279 EXPECT_CALL(stats_observer, OnRttUpdate(kAvgRtt2, kRttHigh))
280 .Times(1)
281 .WillOnce(InvokeWithoutArgs([rtcp_rtt_stats, &event] {
282 EXPECT_EQ(kAvgRtt2, rtcp_rtt_stats->LastProcessedRtt());
283 event.Set();
284 }));
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000285
286 // Set a first values and verify that LastProcessedRtt initially returns the
287 // average rtt.
Tommi38c5d932018-03-27 23:11:09 +0200288 fake_clock_.AdvanceTimeMilliseconds(CallStats::kUpdateIntervalMs);
Tommic89468a2019-08-05 15:13:20 +0200289 AsyncSimulateRttUpdate(kRttLow);
Tommi38c5d932018-03-27 23:11:09 +0200290 EXPECT_TRUE(event.Wait(1000));
291 EXPECT_EQ(kAvgRtt2, rtcp_rtt_stats->LastProcessedRtt());
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000292
Tommi38c5d932018-03-27 23:11:09 +0200293 call_stats_.DeregisterStatsObserver(&stats_observer);
asapersson@webrtc.org8084f952014-12-10 11:04:13 +0000294}
295
sprange2d83d62016-02-19 09:03:26 -0800296TEST_F(CallStatsTest, ProducesHistogramMetrics) {
asapersson01d70a32016-05-20 06:29:46 -0700297 metrics::Reset();
Niels Möllerc572ff32018-11-07 08:43:50 +0100298 rtc::Event event;
Tommi38c5d932018-03-27 23:11:09 +0200299 static constexpr const int64_t kRtt = 123;
Tommi38c5d932018-03-27 23:11:09 +0200300 MockStatsObserver stats_observer;
301 call_stats_.RegisterStatsObserver(&stats_observer);
302 EXPECT_CALL(stats_observer, OnRttUpdate(kRtt, kRtt))
303 .Times(AnyNumber())
Tommic89468a2019-08-05 15:13:20 +0200304 .WillRepeatedly(InvokeWithoutArgs([&event] { event.Set(); }));
Tommi38c5d932018-03-27 23:11:09 +0200305
Tommic89468a2019-08-05 15:13:20 +0200306 AsyncSimulateRttUpdate(kRtt);
307 EXPECT_TRUE(event.Wait(1000));
Tommi38c5d932018-03-27 23:11:09 +0200308 fake_clock_.AdvanceTimeMilliseconds(metrics::kMinRunTimeInSeconds *
309 CallStats::kUpdateIntervalMs);
Tommic89468a2019-08-05 15:13:20 +0200310 AsyncSimulateRttUpdate(kRtt);
Tommi38c5d932018-03-27 23:11:09 +0200311 EXPECT_TRUE(event.Wait(1000));
312
313 call_stats_.DeregisterStatsObserver(&stats_observer);
314
315 process_thread_->Stop();
316 call_stats_.UpdateHistogramsForTest();
sprange2d83d62016-02-19 09:03:26 -0800317
asapersson01d70a32016-05-20 06:29:46 -0700318 EXPECT_EQ(1, metrics::NumSamples(
sprange2d83d62016-02-19 09:03:26 -0800319 "WebRTC.Video.AverageRoundTripTimeInMilliseconds"));
asapersson01d70a32016-05-20 06:29:46 -0700320 EXPECT_EQ(1, metrics::NumEvents(
321 "WebRTC.Video.AverageRoundTripTimeInMilliseconds", kRtt));
sprange2d83d62016-02-19 09:03:26 -0800322}
323
mflodman@webrtc.orgb2f474e2012-11-16 13:57:26 +0000324} // namespace webrtc