• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2013 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 // This file includes unit tests for SendStatisticsProxy.
12 #include "webrtc/video/send_statistics_proxy.h"
13 
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "webrtc/test/histogram.h"
20 
21 namespace webrtc {
22 
23 class SendStatisticsProxyTest : public ::testing::Test {
24  public:
SendStatisticsProxyTest()25   SendStatisticsProxyTest()
26       : fake_clock_(1234), config_(GetTestConfig()), avg_delay_ms_(0),
27         max_delay_ms_(0) {}
~SendStatisticsProxyTest()28   virtual ~SendStatisticsProxyTest() {}
29 
30  protected:
SetUp()31   virtual void SetUp() {
32     statistics_proxy_.reset(new SendStatisticsProxy(
33         &fake_clock_, GetTestConfig(),
34         VideoEncoderConfig::ContentType::kRealtimeVideo));
35     expected_ = VideoSendStream::Stats();
36   }
37 
GetTestConfig()38   VideoSendStream::Config GetTestConfig() {
39     VideoSendStream::Config config(nullptr);
40     config.rtp.ssrcs.push_back(17);
41     config.rtp.ssrcs.push_back(42);
42     config.rtp.rtx.ssrcs.push_back(18);
43     config.rtp.rtx.ssrcs.push_back(43);
44     return config;
45   }
46 
ExpectEqual(VideoSendStream::Stats one,VideoSendStream::Stats other)47   void ExpectEqual(VideoSendStream::Stats one, VideoSendStream::Stats other) {
48     EXPECT_EQ(one.input_frame_rate, other.input_frame_rate);
49     EXPECT_EQ(one.encode_frame_rate, other.encode_frame_rate);
50     EXPECT_EQ(one.media_bitrate_bps, other.media_bitrate_bps);
51     EXPECT_EQ(one.suspended, other.suspended);
52 
53     EXPECT_EQ(one.substreams.size(), other.substreams.size());
54     for (std::map<uint32_t, VideoSendStream::StreamStats>::const_iterator it =
55              one.substreams.begin();
56          it != one.substreams.end(); ++it) {
57       std::map<uint32_t, VideoSendStream::StreamStats>::const_iterator
58           corresponding_it = other.substreams.find(it->first);
59       ASSERT_TRUE(corresponding_it != other.substreams.end());
60       const VideoSendStream::StreamStats& a = it->second;
61       const VideoSendStream::StreamStats& b = corresponding_it->second;
62 
63       EXPECT_EQ(a.frame_counts.key_frames, b.frame_counts.key_frames);
64       EXPECT_EQ(a.frame_counts.delta_frames, b.frame_counts.delta_frames);
65       EXPECT_EQ(a.total_bitrate_bps, b.total_bitrate_bps);
66       EXPECT_EQ(a.avg_delay_ms, b.avg_delay_ms);
67       EXPECT_EQ(a.max_delay_ms, b.max_delay_ms);
68 
69       EXPECT_EQ(a.rtp_stats.transmitted.payload_bytes,
70                 b.rtp_stats.transmitted.payload_bytes);
71       EXPECT_EQ(a.rtp_stats.transmitted.header_bytes,
72                 b.rtp_stats.transmitted.header_bytes);
73       EXPECT_EQ(a.rtp_stats.transmitted.padding_bytes,
74                 b.rtp_stats.transmitted.padding_bytes);
75       EXPECT_EQ(a.rtp_stats.transmitted.packets,
76                 b.rtp_stats.transmitted.packets);
77       EXPECT_EQ(a.rtp_stats.retransmitted.packets,
78                 b.rtp_stats.retransmitted.packets);
79       EXPECT_EQ(a.rtp_stats.fec.packets, b.rtp_stats.fec.packets);
80 
81       EXPECT_EQ(a.rtcp_stats.fraction_lost, b.rtcp_stats.fraction_lost);
82       EXPECT_EQ(a.rtcp_stats.cumulative_lost, b.rtcp_stats.cumulative_lost);
83       EXPECT_EQ(a.rtcp_stats.extended_max_sequence_number,
84                 b.rtcp_stats.extended_max_sequence_number);
85       EXPECT_EQ(a.rtcp_stats.jitter, b.rtcp_stats.jitter);
86     }
87   }
88 
89   rtc::scoped_ptr<SendStatisticsProxy> statistics_proxy_;
90   SimulatedClock fake_clock_;
91   VideoSendStream::Config config_;
92   int avg_delay_ms_;
93   int max_delay_ms_;
94   VideoSendStream::Stats expected_;
95   typedef std::map<uint32_t, VideoSendStream::StreamStats>::const_iterator
96       StreamIterator;
97 };
98 
TEST_F(SendStatisticsProxyTest,RtcpStatistics)99 TEST_F(SendStatisticsProxyTest, RtcpStatistics) {
100   RtcpStatisticsCallback* callback = statistics_proxy_.get();
101   for (std::vector<uint32_t>::const_iterator it = config_.rtp.ssrcs.begin();
102        it != config_.rtp.ssrcs.end();
103        ++it) {
104     const uint32_t ssrc = *it;
105     VideoSendStream::StreamStats& ssrc_stats = expected_.substreams[ssrc];
106 
107     // Add statistics with some arbitrary, but unique, numbers.
108     uint32_t offset = ssrc * sizeof(RtcpStatistics);
109     ssrc_stats.rtcp_stats.cumulative_lost = offset;
110     ssrc_stats.rtcp_stats.extended_max_sequence_number = offset + 1;
111     ssrc_stats.rtcp_stats.fraction_lost = offset + 2;
112     ssrc_stats.rtcp_stats.jitter = offset + 3;
113     callback->StatisticsUpdated(ssrc_stats.rtcp_stats, ssrc);
114   }
115   for (std::vector<uint32_t>::const_iterator it = config_.rtp.rtx.ssrcs.begin();
116        it != config_.rtp.rtx.ssrcs.end();
117        ++it) {
118     const uint32_t ssrc = *it;
119     VideoSendStream::StreamStats& ssrc_stats = expected_.substreams[ssrc];
120 
121     // Add statistics with some arbitrary, but unique, numbers.
122     uint32_t offset = ssrc * sizeof(RtcpStatistics);
123     ssrc_stats.rtcp_stats.cumulative_lost = offset;
124     ssrc_stats.rtcp_stats.extended_max_sequence_number = offset + 1;
125     ssrc_stats.rtcp_stats.fraction_lost = offset + 2;
126     ssrc_stats.rtcp_stats.jitter = offset + 3;
127     callback->StatisticsUpdated(ssrc_stats.rtcp_stats, ssrc);
128   }
129   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
130   ExpectEqual(expected_, stats);
131 }
132 
TEST_F(SendStatisticsProxyTest,EncodedBitrateAndFramerate)133 TEST_F(SendStatisticsProxyTest, EncodedBitrateAndFramerate) {
134   int media_bitrate_bps = 500;
135   int encode_fps = 29;
136 
137   statistics_proxy_->OnOutgoingRate(encode_fps, media_bitrate_bps);
138 
139   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
140   EXPECT_EQ(media_bitrate_bps, stats.media_bitrate_bps);
141   EXPECT_EQ(encode_fps, stats.encode_frame_rate);
142 }
143 
TEST_F(SendStatisticsProxyTest,Suspended)144 TEST_F(SendStatisticsProxyTest, Suspended) {
145   // Verify that the value is false by default.
146   EXPECT_FALSE(statistics_proxy_->GetStats().suspended);
147 
148   // Verify that we can set it to true.
149   statistics_proxy_->OnSuspendChange(true);
150   EXPECT_TRUE(statistics_proxy_->GetStats().suspended);
151 
152   // Verify that we can set it back to false again.
153   statistics_proxy_->OnSuspendChange(false);
154   EXPECT_FALSE(statistics_proxy_->GetStats().suspended);
155 }
156 
TEST_F(SendStatisticsProxyTest,FrameCounts)157 TEST_F(SendStatisticsProxyTest, FrameCounts) {
158   FrameCountObserver* observer = statistics_proxy_.get();
159   for (std::vector<uint32_t>::const_iterator it = config_.rtp.ssrcs.begin();
160        it != config_.rtp.ssrcs.end();
161        ++it) {
162     const uint32_t ssrc = *it;
163     // Add statistics with some arbitrary, but unique, numbers.
164     VideoSendStream::StreamStats& stats = expected_.substreams[ssrc];
165     uint32_t offset = ssrc * sizeof(VideoSendStream::StreamStats);
166     FrameCounts frame_counts;
167     frame_counts.key_frames = offset;
168     frame_counts.delta_frames = offset + 1;
169     stats.frame_counts = frame_counts;
170     observer->FrameCountUpdated(frame_counts, ssrc);
171   }
172   for (std::vector<uint32_t>::const_iterator it = config_.rtp.rtx.ssrcs.begin();
173        it != config_.rtp.rtx.ssrcs.end();
174        ++it) {
175     const uint32_t ssrc = *it;
176     // Add statistics with some arbitrary, but unique, numbers.
177     VideoSendStream::StreamStats& stats = expected_.substreams[ssrc];
178     uint32_t offset = ssrc * sizeof(VideoSendStream::StreamStats);
179     FrameCounts frame_counts;
180     frame_counts.key_frames = offset;
181     frame_counts.delta_frames = offset + 1;
182     stats.frame_counts = frame_counts;
183     observer->FrameCountUpdated(frame_counts, ssrc);
184   }
185 
186   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
187   ExpectEqual(expected_, stats);
188 }
189 
TEST_F(SendStatisticsProxyTest,DataCounters)190 TEST_F(SendStatisticsProxyTest, DataCounters) {
191   StreamDataCountersCallback* callback = statistics_proxy_.get();
192   for (std::vector<uint32_t>::const_iterator it = config_.rtp.ssrcs.begin();
193        it != config_.rtp.ssrcs.end();
194        ++it) {
195     const uint32_t ssrc = *it;
196     StreamDataCounters& counters = expected_.substreams[ssrc].rtp_stats;
197     // Add statistics with some arbitrary, but unique, numbers.
198     size_t offset = ssrc * sizeof(StreamDataCounters);
199     uint32_t offset_uint32 = static_cast<uint32_t>(offset);
200     counters.transmitted.payload_bytes = offset;
201     counters.transmitted.header_bytes = offset + 1;
202     counters.fec.packets = offset_uint32 + 2;
203     counters.transmitted.padding_bytes = offset + 3;
204     counters.retransmitted.packets = offset_uint32 + 4;
205     counters.transmitted.packets = offset_uint32 + 5;
206     callback->DataCountersUpdated(counters, ssrc);
207   }
208   for (std::vector<uint32_t>::const_iterator it = config_.rtp.rtx.ssrcs.begin();
209        it != config_.rtp.rtx.ssrcs.end();
210        ++it) {
211     const uint32_t ssrc = *it;
212     StreamDataCounters& counters = expected_.substreams[ssrc].rtp_stats;
213     // Add statistics with some arbitrary, but unique, numbers.
214     size_t offset = ssrc * sizeof(StreamDataCounters);
215     uint32_t offset_uint32 = static_cast<uint32_t>(offset);
216     counters.transmitted.payload_bytes = offset;
217     counters.transmitted.header_bytes = offset + 1;
218     counters.fec.packets = offset_uint32 + 2;
219     counters.transmitted.padding_bytes = offset + 3;
220     counters.retransmitted.packets = offset_uint32 + 4;
221     counters.transmitted.packets = offset_uint32 + 5;
222     callback->DataCountersUpdated(counters, ssrc);
223   }
224 
225   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
226   ExpectEqual(expected_, stats);
227 }
228 
TEST_F(SendStatisticsProxyTest,Bitrate)229 TEST_F(SendStatisticsProxyTest, Bitrate) {
230   BitrateStatisticsObserver* observer = statistics_proxy_.get();
231   for (std::vector<uint32_t>::const_iterator it = config_.rtp.ssrcs.begin();
232        it != config_.rtp.ssrcs.end();
233        ++it) {
234     const uint32_t ssrc = *it;
235     BitrateStatistics total;
236     BitrateStatistics retransmit;
237     // Use ssrc as bitrate_bps to get a unique value for each stream.
238     total.bitrate_bps = ssrc;
239     retransmit.bitrate_bps = ssrc + 1;
240     observer->Notify(total, retransmit, ssrc);
241     expected_.substreams[ssrc].total_bitrate_bps = total.bitrate_bps;
242     expected_.substreams[ssrc].retransmit_bitrate_bps = retransmit.bitrate_bps;
243   }
244   for (std::vector<uint32_t>::const_iterator it = config_.rtp.rtx.ssrcs.begin();
245        it != config_.rtp.rtx.ssrcs.end();
246        ++it) {
247     const uint32_t ssrc = *it;
248     BitrateStatistics total;
249     BitrateStatistics retransmit;
250     // Use ssrc as bitrate_bps to get a unique value for each stream.
251     total.bitrate_bps = ssrc;
252     retransmit.bitrate_bps = ssrc + 1;
253     observer->Notify(total, retransmit, ssrc);
254     expected_.substreams[ssrc].total_bitrate_bps = total.bitrate_bps;
255     expected_.substreams[ssrc].retransmit_bitrate_bps = retransmit.bitrate_bps;
256   }
257 
258   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
259   ExpectEqual(expected_, stats);
260 }
261 
TEST_F(SendStatisticsProxyTest,SendSideDelay)262 TEST_F(SendStatisticsProxyTest, SendSideDelay) {
263   SendSideDelayObserver* observer = statistics_proxy_.get();
264   for (std::vector<uint32_t>::const_iterator it = config_.rtp.ssrcs.begin();
265        it != config_.rtp.ssrcs.end();
266        ++it) {
267     const uint32_t ssrc = *it;
268     // Use ssrc as avg_delay_ms and max_delay_ms to get a unique value for each
269     // stream.
270     int avg_delay_ms = ssrc;
271     int max_delay_ms = ssrc + 1;
272     observer->SendSideDelayUpdated(avg_delay_ms, max_delay_ms, ssrc);
273     expected_.substreams[ssrc].avg_delay_ms = avg_delay_ms;
274     expected_.substreams[ssrc].max_delay_ms = max_delay_ms;
275   }
276   for (std::vector<uint32_t>::const_iterator it = config_.rtp.rtx.ssrcs.begin();
277        it != config_.rtp.rtx.ssrcs.end();
278        ++it) {
279     const uint32_t ssrc = *it;
280     // Use ssrc as avg_delay_ms and max_delay_ms to get a unique value for each
281     // stream.
282     int avg_delay_ms = ssrc;
283     int max_delay_ms = ssrc + 1;
284     observer->SendSideDelayUpdated(avg_delay_ms, max_delay_ms, ssrc);
285     expected_.substreams[ssrc].avg_delay_ms = avg_delay_ms;
286     expected_.substreams[ssrc].max_delay_ms = max_delay_ms;
287   }
288   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
289   ExpectEqual(expected_, stats);
290 }
291 
TEST_F(SendStatisticsProxyTest,OnEncodedFrame)292 TEST_F(SendStatisticsProxyTest, OnEncodedFrame) {
293   const int kEncodeTimeMs = 11;
294   statistics_proxy_->OnEncodedFrame(kEncodeTimeMs);
295 
296   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
297   EXPECT_EQ(kEncodeTimeMs, stats.avg_encode_time_ms);
298 }
299 
TEST_F(SendStatisticsProxyTest,SwitchContentTypeUpdatesHistograms)300 TEST_F(SendStatisticsProxyTest, SwitchContentTypeUpdatesHistograms) {
301   test::ClearHistograms();
302   const int kMinRequiredSamples = 200;
303   const int kWidth = 640;
304   const int kHeight = 480;
305 
306   for (int i = 0; i < kMinRequiredSamples; ++i)
307     statistics_proxy_->OnIncomingFrame(kWidth, kHeight);
308 
309   // No switch, stats not should be updated.
310   statistics_proxy_->SetContentType(
311       VideoEncoderConfig::ContentType::kRealtimeVideo);
312   EXPECT_EQ(0, test::NumHistogramSamples("WebRTC.Video.InputWidthInPixels"));
313 
314   // Switch to screenshare, real-time stats should be updated.
315   statistics_proxy_->SetContentType(VideoEncoderConfig::ContentType::kScreen);
316   EXPECT_EQ(1, test::NumHistogramSamples("WebRTC.Video.InputWidthInPixels"));
317 }
318 
TEST_F(SendStatisticsProxyTest,NoSubstreams)319 TEST_F(SendStatisticsProxyTest, NoSubstreams) {
320   uint32_t excluded_ssrc =
321       std::max(
322           *std::max_element(config_.rtp.ssrcs.begin(), config_.rtp.ssrcs.end()),
323           *std::max_element(config_.rtp.rtx.ssrcs.begin(),
324                             config_.rtp.rtx.ssrcs.end())) +
325       1;
326   // From RtcpStatisticsCallback.
327   RtcpStatistics rtcp_stats;
328   RtcpStatisticsCallback* rtcp_callback = statistics_proxy_.get();
329   rtcp_callback->StatisticsUpdated(rtcp_stats, excluded_ssrc);
330 
331   // From BitrateStatisticsObserver.
332   BitrateStatistics total;
333   BitrateStatistics retransmit;
334   BitrateStatisticsObserver* bitrate_observer = statistics_proxy_.get();
335   bitrate_observer->Notify(total, retransmit, excluded_ssrc);
336 
337   // From FrameCountObserver.
338   FrameCountObserver* fps_observer = statistics_proxy_.get();
339   FrameCounts frame_counts;
340   frame_counts.key_frames = 1;
341   fps_observer->FrameCountUpdated(frame_counts, excluded_ssrc);
342 
343   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
344   EXPECT_TRUE(stats.substreams.empty());
345 }
346 
TEST_F(SendStatisticsProxyTest,EncodedResolutionTimesOut)347 TEST_F(SendStatisticsProxyTest, EncodedResolutionTimesOut) {
348   static const int kEncodedWidth = 123;
349   static const int kEncodedHeight = 81;
350   EncodedImage encoded_image;
351   encoded_image._encodedWidth = kEncodedWidth;
352   encoded_image._encodedHeight = kEncodedHeight;
353 
354   RTPVideoHeader rtp_video_header;
355 
356   rtp_video_header.simulcastIdx = 0;
357   statistics_proxy_->OnSendEncodedImage(encoded_image, &rtp_video_header);
358   rtp_video_header.simulcastIdx = 1;
359   statistics_proxy_->OnSendEncodedImage(encoded_image, &rtp_video_header);
360 
361   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
362   EXPECT_EQ(kEncodedWidth, stats.substreams[config_.rtp.ssrcs[0]].width);
363   EXPECT_EQ(kEncodedHeight, stats.substreams[config_.rtp.ssrcs[0]].height);
364   EXPECT_EQ(kEncodedWidth, stats.substreams[config_.rtp.ssrcs[1]].width);
365   EXPECT_EQ(kEncodedHeight, stats.substreams[config_.rtp.ssrcs[1]].height);
366 
367   // Forward almost to timeout, this should not have removed stats.
368   fake_clock_.AdvanceTimeMilliseconds(SendStatisticsProxy::kStatsTimeoutMs - 1);
369   stats = statistics_proxy_->GetStats();
370   EXPECT_EQ(kEncodedWidth, stats.substreams[config_.rtp.ssrcs[0]].width);
371   EXPECT_EQ(kEncodedHeight, stats.substreams[config_.rtp.ssrcs[0]].height);
372 
373   // Update the first SSRC with bogus RTCP stats to make sure that encoded
374   // resolution still times out (no global timeout for all stats).
375   RtcpStatistics rtcp_statistics;
376   RtcpStatisticsCallback* rtcp_stats = statistics_proxy_.get();
377   rtcp_stats->StatisticsUpdated(rtcp_statistics, config_.rtp.ssrcs[0]);
378 
379   // Report stats for second SSRC to make sure it's not outdated along with the
380   // first SSRC.
381   rtp_video_header.simulcastIdx = 1;
382   statistics_proxy_->OnSendEncodedImage(encoded_image, &rtp_video_header);
383 
384   // Forward 1 ms, reach timeout, substream 0 should have no resolution
385   // reported, but substream 1 should.
386   fake_clock_.AdvanceTimeMilliseconds(1);
387   stats = statistics_proxy_->GetStats();
388   EXPECT_EQ(0, stats.substreams[config_.rtp.ssrcs[0]].width);
389   EXPECT_EQ(0, stats.substreams[config_.rtp.ssrcs[0]].height);
390   EXPECT_EQ(kEncodedWidth, stats.substreams[config_.rtp.ssrcs[1]].width);
391   EXPECT_EQ(kEncodedHeight, stats.substreams[config_.rtp.ssrcs[1]].height);
392 }
393 
TEST_F(SendStatisticsProxyTest,ClearsResolutionFromInactiveSsrcs)394 TEST_F(SendStatisticsProxyTest, ClearsResolutionFromInactiveSsrcs) {
395   static const int kEncodedWidth = 123;
396   static const int kEncodedHeight = 81;
397   EncodedImage encoded_image;
398   encoded_image._encodedWidth = kEncodedWidth;
399   encoded_image._encodedHeight = kEncodedHeight;
400 
401   RTPVideoHeader rtp_video_header;
402 
403   rtp_video_header.simulcastIdx = 0;
404   statistics_proxy_->OnSendEncodedImage(encoded_image, &rtp_video_header);
405   rtp_video_header.simulcastIdx = 1;
406   statistics_proxy_->OnSendEncodedImage(encoded_image, &rtp_video_header);
407 
408   statistics_proxy_->OnInactiveSsrc(config_.rtp.ssrcs[1]);
409   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
410   EXPECT_EQ(kEncodedWidth, stats.substreams[config_.rtp.ssrcs[0]].width);
411   EXPECT_EQ(kEncodedHeight, stats.substreams[config_.rtp.ssrcs[0]].height);
412   EXPECT_EQ(0, stats.substreams[config_.rtp.ssrcs[1]].width);
413   EXPECT_EQ(0, stats.substreams[config_.rtp.ssrcs[1]].height);
414 }
415 
TEST_F(SendStatisticsProxyTest,ClearsBitratesFromInactiveSsrcs)416 TEST_F(SendStatisticsProxyTest, ClearsBitratesFromInactiveSsrcs) {
417   BitrateStatistics bitrate;
418   bitrate.bitrate_bps = 42;
419   BitrateStatisticsObserver* observer = statistics_proxy_.get();
420   observer->Notify(bitrate, bitrate, config_.rtp.ssrcs[0]);
421   observer->Notify(bitrate, bitrate, config_.rtp.ssrcs[1]);
422 
423   statistics_proxy_->OnInactiveSsrc(config_.rtp.ssrcs[1]);
424 
425   VideoSendStream::Stats stats = statistics_proxy_->GetStats();
426   EXPECT_EQ(static_cast<int>(bitrate.bitrate_bps),
427             stats.substreams[config_.rtp.ssrcs[0]].total_bitrate_bps);
428   EXPECT_EQ(static_cast<int>(bitrate.bitrate_bps),
429             stats.substreams[config_.rtp.ssrcs[0]].retransmit_bitrate_bps);
430   EXPECT_EQ(0, stats.substreams[config_.rtp.ssrcs[1]].total_bitrate_bps);
431   EXPECT_EQ(0, stats.substreams[config_.rtp.ssrcs[1]].retransmit_bitrate_bps);
432 }
433 
434 }  // namespace webrtc
435