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 "video/call_stats.h"
12 
13 #include <memory>
14 
15 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
16 #include "modules/utility/include/process_thread.h"
17 #include "rtc_base/event.h"
18 #include "rtc_base/location.h"
19 #include "rtc_base/task_utils/to_queued_task.h"
20 #include "system_wrappers/include/metrics.h"
21 #include "test/gmock.h"
22 #include "test/gtest.h"
23 
24 using ::testing::AnyNumber;
25 using ::testing::InvokeWithoutArgs;
26 using ::testing::Return;
27 
28 namespace webrtc {
29 
30 class MockStatsObserver : public CallStatsObserver {
31  public:
MockStatsObserver()32   MockStatsObserver() {}
~MockStatsObserver()33   virtual ~MockStatsObserver() {}
34 
35   MOCK_METHOD(void, OnRttUpdate, (int64_t, int64_t), (override));
36 };
37 
38 class CallStatsTest : public ::testing::Test {
39  public:
CallStatsTest()40   CallStatsTest() {
41     process_thread_->RegisterModule(&call_stats_, RTC_FROM_HERE);
42     process_thread_->Start();
43   }
~CallStatsTest()44   ~CallStatsTest() override {
45     process_thread_->Stop();
46     process_thread_->DeRegisterModule(&call_stats_);
47   }
48 
49   // Queues an rtt update call on the process thread.
AsyncSimulateRttUpdate(int64_t rtt)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 
56  protected:
57   std::unique_ptr<ProcessThread> process_thread_{
58       ProcessThread::Create("CallStats")};
59   SimulatedClock fake_clock_{12345};
60   CallStats call_stats_{&fake_clock_, process_thread_.get()};
61 };
62 
TEST_F(CallStatsTest,AddAndTriggerCallback)63 TEST_F(CallStatsTest, AddAndTriggerCallback) {
64   rtc::Event event;
65 
66   static constexpr const int64_t kRtt = 25;
67 
68   MockStatsObserver stats_observer;
69   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);
75   EXPECT_EQ(-1, rtcp_rtt_stats->LastProcessedRtt());
76 
77   AsyncSimulateRttUpdate(kRtt);
78 
79   EXPECT_TRUE(event.Wait(1000));
80 
81   EXPECT_EQ(kRtt, rtcp_rtt_stats->LastProcessedRtt());
82 
83   call_stats_.DeregisterStatsObserver(&stats_observer);
84 }
85 
TEST_F(CallStatsTest,ProcessTime)86 TEST_F(CallStatsTest, ProcessTime) {
87   rtc::Event event;
88 
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 
94   MockStatsObserver stats_observer;
95 
96   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       }));
107 
108   // 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(); }));
114 
115   call_stats_.RegisterStatsObserver(&stats_observer);
116 
117   AsyncSimulateRttUpdate(kRtt);
118   EXPECT_TRUE(event.Wait(1000));
119 
120   call_stats_.DeregisterStatsObserver(&stats_observer);
121 }
122 
123 // Verify all observers get correct estimates and observers can be added and
124 // removed.
TEST_F(CallStatsTest,MultipleObservers)125 TEST_F(CallStatsTest, MultipleObservers) {
126   MockStatsObserver stats_observer_1;
127   call_stats_.RegisterStatsObserver(&stats_observer_1);
128   // Add the second observer twice, there should still be only one report to the
129   // observer.
130   MockStatsObserver stats_observer_2;
131   call_stats_.RegisterStatsObserver(&stats_observer_2);
132   call_stats_.RegisterStatsObserver(&stats_observer_2);
133 
134   static constexpr const int64_t kRtt = 100;
135 
136   // Verify both observers are updated.
137   rtc::Event ev1;
138   rtc::Event ev2;
139   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());
147   AsyncSimulateRttUpdate(kRtt);
148   ASSERT_TRUE(ev1.Wait(100));
149   ASSERT_TRUE(ev2.Wait(100));
150 
151   // Deregister the second observer and verify update is only sent to the first
152   // observer.
153   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());
159   EXPECT_CALL(stats_observer_2, OnRttUpdate(kRtt, kRtt)).Times(0);
160   AsyncSimulateRttUpdate(kRtt);
161   ASSERT_TRUE(ev1.Wait(100));
162 
163   // Deregister the first observer.
164   call_stats_.DeregisterStatsObserver(&stats_observer_1);
165 
166   // Now make sure we don't get any callbacks.
167   EXPECT_CALL(stats_observer_1, OnRttUpdate(kRtt, kRtt)).Times(0);
168   EXPECT_CALL(stats_observer_2, OnRttUpdate(kRtt, kRtt)).Times(0);
169   AsyncSimulateRttUpdate(kRtt);
170 
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.
176   rtc::Event event;
177   process_thread_->PostTask(ToQueuedTask([&event] { event.Set(); }));
178   event.Wait(rtc::Event::kForever);
179 }
180 
181 // Verify increasing and decreasing rtt triggers callbacks with correct values.
TEST_F(CallStatsTest,ChangeRtt)182 TEST_F(CallStatsTest, ChangeRtt) {
183   // 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 
188   MockStatsObserver stats_observer;
189   call_stats_.RegisterStatsObserver(&stats_observer);
190   RtcpRttStats* rtcp_rtt_stats = &call_stats_;
191 
192   rtc::Event event;
193 
194   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;
197 
198   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       }));
218 
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.
222 
223   // 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       }));
235 
236   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.
242   AsyncSimulateRttUpdate(kFirstRtt);  // Reported at T0 (0ms).
243   EXPECT_TRUE(event.Wait(1000));
244 
245   call_stats_.DeregisterStatsObserver(&stats_observer);
246 }
247 
TEST_F(CallStatsTest,LastProcessedRtt)248 TEST_F(CallStatsTest, LastProcessedRtt) {
249   rtc::Event event;
250   MockStatsObserver stats_observer;
251   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       }));
285 
286   // Set a first values and verify that LastProcessedRtt initially returns the
287   // average rtt.
288   fake_clock_.AdvanceTimeMilliseconds(CallStats::kUpdateIntervalMs);
289   AsyncSimulateRttUpdate(kRttLow);
290   EXPECT_TRUE(event.Wait(1000));
291   EXPECT_EQ(kAvgRtt2, rtcp_rtt_stats->LastProcessedRtt());
292 
293   call_stats_.DeregisterStatsObserver(&stats_observer);
294 }
295 
TEST_F(CallStatsTest,ProducesHistogramMetrics)296 TEST_F(CallStatsTest, ProducesHistogramMetrics) {
297   metrics::Reset();
298   rtc::Event event;
299   static constexpr const int64_t kRtt = 123;
300   MockStatsObserver stats_observer;
301   call_stats_.RegisterStatsObserver(&stats_observer);
302   EXPECT_CALL(stats_observer, OnRttUpdate(kRtt, kRtt))
303       .Times(AnyNumber())
304       .WillRepeatedly(InvokeWithoutArgs([&event] { event.Set(); }));
305 
306   AsyncSimulateRttUpdate(kRtt);
307   EXPECT_TRUE(event.Wait(1000));
308   fake_clock_.AdvanceTimeMilliseconds(metrics::kMinRunTimeInSeconds *
309                                       CallStats::kUpdateIntervalMs);
310   AsyncSimulateRttUpdate(kRtt);
311   EXPECT_TRUE(event.Wait(1000));
312 
313   call_stats_.DeregisterStatsObserver(&stats_observer);
314 
315   process_thread_->Stop();
316   call_stats_.UpdateHistogramsForTest();
317 
318   EXPECT_METRIC_EQ(1, metrics::NumSamples(
319                           "WebRTC.Video.AverageRoundTripTimeInMilliseconds"));
320   EXPECT_METRIC_EQ(
321       1, metrics::NumEvents("WebRTC.Video.AverageRoundTripTimeInMilliseconds",
322                             kRtt));
323 }
324 
325 }  // namespace webrtc
326