• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2015 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 #ifdef ENABLE_RTC_EVENT_LOG
12 
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "webrtc/base/buffer.h"
19 #include "webrtc/base/checks.h"
20 #include "webrtc/base/random.h"
21 #include "webrtc/base/scoped_ptr.h"
22 #include "webrtc/base/thread.h"
23 #include "webrtc/call.h"
24 #include "webrtc/call/rtc_event_log.h"
25 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h"
26 #include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
27 #include "webrtc/system_wrappers/include/clock.h"
28 #include "webrtc/test/test_suite.h"
29 #include "webrtc/test/testsupport/fileutils.h"
30 
31 // Files generated at build-time by the protobuf compiler.
32 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
33 #include "external/webrtc/webrtc/call/rtc_event_log.pb.h"
34 #else
35 #include "webrtc/call/rtc_event_log.pb.h"
36 #endif
37 
38 namespace webrtc {
39 
40 namespace {
41 
42 const RTPExtensionType kExtensionTypes[] = {
43     RTPExtensionType::kRtpExtensionTransmissionTimeOffset,
44     RTPExtensionType::kRtpExtensionAudioLevel,
45     RTPExtensionType::kRtpExtensionAbsoluteSendTime,
46     RTPExtensionType::kRtpExtensionVideoRotation,
47     RTPExtensionType::kRtpExtensionTransportSequenceNumber};
48 const char* kExtensionNames[] = {RtpExtension::kTOffset,
49                                  RtpExtension::kAudioLevel,
50                                  RtpExtension::kAbsSendTime,
51                                  RtpExtension::kVideoRotation,
52                                  RtpExtension::kTransportSequenceNumber};
53 const size_t kNumExtensions = 5;
54 
55 }  // namespace
56 
57 // TODO(terelius): Place this definition with other parsing functions?
GetRuntimeMediaType(rtclog::MediaType media_type)58 MediaType GetRuntimeMediaType(rtclog::MediaType media_type) {
59   switch (media_type) {
60     case rtclog::MediaType::ANY:
61       return MediaType::ANY;
62     case rtclog::MediaType::AUDIO:
63       return MediaType::AUDIO;
64     case rtclog::MediaType::VIDEO:
65       return MediaType::VIDEO;
66     case rtclog::MediaType::DATA:
67       return MediaType::DATA;
68   }
69   RTC_NOTREACHED();
70   return MediaType::ANY;
71 }
72 
73 // Checks that the event has a timestamp, a type and exactly the data field
74 // corresponding to the type.
IsValidBasicEvent(const rtclog::Event & event)75 ::testing::AssertionResult IsValidBasicEvent(const rtclog::Event& event) {
76   if (!event.has_timestamp_us())
77     return ::testing::AssertionFailure() << "Event has no timestamp";
78   if (!event.has_type())
79     return ::testing::AssertionFailure() << "Event has no event type";
80   rtclog::Event_EventType type = event.type();
81   if ((type == rtclog::Event::RTP_EVENT) != event.has_rtp_packet())
82     return ::testing::AssertionFailure()
83            << "Event of type " << type << " has "
84            << (event.has_rtp_packet() ? "" : "no ") << "RTP packet";
85   if ((type == rtclog::Event::RTCP_EVENT) != event.has_rtcp_packet())
86     return ::testing::AssertionFailure()
87            << "Event of type " << type << " has "
88            << (event.has_rtcp_packet() ? "" : "no ") << "RTCP packet";
89   if ((type == rtclog::Event::AUDIO_PLAYOUT_EVENT) !=
90       event.has_audio_playout_event())
91     return ::testing::AssertionFailure()
92            << "Event of type " << type << " has "
93            << (event.has_audio_playout_event() ? "" : "no ")
94            << "audio_playout event";
95   if ((type == rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT) !=
96       event.has_video_receiver_config())
97     return ::testing::AssertionFailure()
98            << "Event of type " << type << " has "
99            << (event.has_video_receiver_config() ? "" : "no ")
100            << "receiver config";
101   if ((type == rtclog::Event::VIDEO_SENDER_CONFIG_EVENT) !=
102       event.has_video_sender_config())
103     return ::testing::AssertionFailure()
104            << "Event of type " << type << " has "
105            << (event.has_video_sender_config() ? "" : "no ") << "sender config";
106   if ((type == rtclog::Event::AUDIO_RECEIVER_CONFIG_EVENT) !=
107       event.has_audio_receiver_config()) {
108     return ::testing::AssertionFailure()
109            << "Event of type " << type << " has "
110            << (event.has_audio_receiver_config() ? "" : "no ")
111            << "audio receiver config";
112   }
113   if ((type == rtclog::Event::AUDIO_SENDER_CONFIG_EVENT) !=
114       event.has_audio_sender_config()) {
115     return ::testing::AssertionFailure()
116            << "Event of type " << type << " has "
117            << (event.has_audio_sender_config() ? "" : "no ")
118            << "audio sender config";
119   }
120   return ::testing::AssertionSuccess();
121 }
122 
VerifyReceiveStreamConfig(const rtclog::Event & event,const VideoReceiveStream::Config & config)123 void VerifyReceiveStreamConfig(const rtclog::Event& event,
124                                const VideoReceiveStream::Config& config) {
125   ASSERT_TRUE(IsValidBasicEvent(event));
126   ASSERT_EQ(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT, event.type());
127   const rtclog::VideoReceiveConfig& receiver_config =
128       event.video_receiver_config();
129   // Check SSRCs.
130   ASSERT_TRUE(receiver_config.has_remote_ssrc());
131   EXPECT_EQ(config.rtp.remote_ssrc, receiver_config.remote_ssrc());
132   ASSERT_TRUE(receiver_config.has_local_ssrc());
133   EXPECT_EQ(config.rtp.local_ssrc, receiver_config.local_ssrc());
134   // Check RTCP settings.
135   ASSERT_TRUE(receiver_config.has_rtcp_mode());
136   if (config.rtp.rtcp_mode == RtcpMode::kCompound)
137     EXPECT_EQ(rtclog::VideoReceiveConfig::RTCP_COMPOUND,
138               receiver_config.rtcp_mode());
139   else
140     EXPECT_EQ(rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE,
141               receiver_config.rtcp_mode());
142   ASSERT_TRUE(receiver_config.has_remb());
143   EXPECT_EQ(config.rtp.remb, receiver_config.remb());
144   // Check RTX map.
145   ASSERT_EQ(static_cast<int>(config.rtp.rtx.size()),
146             receiver_config.rtx_map_size());
147   for (const rtclog::RtxMap& rtx_map : receiver_config.rtx_map()) {
148     ASSERT_TRUE(rtx_map.has_payload_type());
149     ASSERT_TRUE(rtx_map.has_config());
150     EXPECT_EQ(1u, config.rtp.rtx.count(rtx_map.payload_type()));
151     const rtclog::RtxConfig& rtx_config = rtx_map.config();
152     const VideoReceiveStream::Config::Rtp::Rtx& rtx =
153         config.rtp.rtx.at(rtx_map.payload_type());
154     ASSERT_TRUE(rtx_config.has_rtx_ssrc());
155     ASSERT_TRUE(rtx_config.has_rtx_payload_type());
156     EXPECT_EQ(rtx.ssrc, rtx_config.rtx_ssrc());
157     EXPECT_EQ(rtx.payload_type, rtx_config.rtx_payload_type());
158   }
159   // Check header extensions.
160   ASSERT_EQ(static_cast<int>(config.rtp.extensions.size()),
161             receiver_config.header_extensions_size());
162   for (int i = 0; i < receiver_config.header_extensions_size(); i++) {
163     ASSERT_TRUE(receiver_config.header_extensions(i).has_name());
164     ASSERT_TRUE(receiver_config.header_extensions(i).has_id());
165     const std::string& name = receiver_config.header_extensions(i).name();
166     int id = receiver_config.header_extensions(i).id();
167     EXPECT_EQ(config.rtp.extensions[i].id, id);
168     EXPECT_EQ(config.rtp.extensions[i].name, name);
169   }
170   // Check decoders.
171   ASSERT_EQ(static_cast<int>(config.decoders.size()),
172             receiver_config.decoders_size());
173   for (int i = 0; i < receiver_config.decoders_size(); i++) {
174     ASSERT_TRUE(receiver_config.decoders(i).has_name());
175     ASSERT_TRUE(receiver_config.decoders(i).has_payload_type());
176     const std::string& decoder_name = receiver_config.decoders(i).name();
177     int decoder_type = receiver_config.decoders(i).payload_type();
178     EXPECT_EQ(config.decoders[i].payload_name, decoder_name);
179     EXPECT_EQ(config.decoders[i].payload_type, decoder_type);
180   }
181 }
182 
VerifySendStreamConfig(const rtclog::Event & event,const VideoSendStream::Config & config)183 void VerifySendStreamConfig(const rtclog::Event& event,
184                             const VideoSendStream::Config& config) {
185   ASSERT_TRUE(IsValidBasicEvent(event));
186   ASSERT_EQ(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT, event.type());
187   const rtclog::VideoSendConfig& sender_config = event.video_sender_config();
188   // Check SSRCs.
189   ASSERT_EQ(static_cast<int>(config.rtp.ssrcs.size()),
190             sender_config.ssrcs_size());
191   for (int i = 0; i < sender_config.ssrcs_size(); i++) {
192     EXPECT_EQ(config.rtp.ssrcs[i], sender_config.ssrcs(i));
193   }
194   // Check header extensions.
195   ASSERT_EQ(static_cast<int>(config.rtp.extensions.size()),
196             sender_config.header_extensions_size());
197   for (int i = 0; i < sender_config.header_extensions_size(); i++) {
198     ASSERT_TRUE(sender_config.header_extensions(i).has_name());
199     ASSERT_TRUE(sender_config.header_extensions(i).has_id());
200     const std::string& name = sender_config.header_extensions(i).name();
201     int id = sender_config.header_extensions(i).id();
202     EXPECT_EQ(config.rtp.extensions[i].id, id);
203     EXPECT_EQ(config.rtp.extensions[i].name, name);
204   }
205   // Check RTX settings.
206   ASSERT_EQ(static_cast<int>(config.rtp.rtx.ssrcs.size()),
207             sender_config.rtx_ssrcs_size());
208   for (int i = 0; i < sender_config.rtx_ssrcs_size(); i++) {
209     EXPECT_EQ(config.rtp.rtx.ssrcs[i], sender_config.rtx_ssrcs(i));
210   }
211   if (sender_config.rtx_ssrcs_size() > 0) {
212     ASSERT_TRUE(sender_config.has_rtx_payload_type());
213     EXPECT_EQ(config.rtp.rtx.payload_type, sender_config.rtx_payload_type());
214   }
215   // Check encoder.
216   ASSERT_TRUE(sender_config.has_encoder());
217   ASSERT_TRUE(sender_config.encoder().has_name());
218   ASSERT_TRUE(sender_config.encoder().has_payload_type());
219   EXPECT_EQ(config.encoder_settings.payload_name,
220             sender_config.encoder().name());
221   EXPECT_EQ(config.encoder_settings.payload_type,
222             sender_config.encoder().payload_type());
223 }
224 
VerifyRtpEvent(const rtclog::Event & event,bool incoming,MediaType media_type,const uint8_t * header,size_t header_size,size_t total_size)225 void VerifyRtpEvent(const rtclog::Event& event,
226                     bool incoming,
227                     MediaType media_type,
228                     const uint8_t* header,
229                     size_t header_size,
230                     size_t total_size) {
231   ASSERT_TRUE(IsValidBasicEvent(event));
232   ASSERT_EQ(rtclog::Event::RTP_EVENT, event.type());
233   const rtclog::RtpPacket& rtp_packet = event.rtp_packet();
234   ASSERT_TRUE(rtp_packet.has_incoming());
235   EXPECT_EQ(incoming, rtp_packet.incoming());
236   ASSERT_TRUE(rtp_packet.has_type());
237   EXPECT_EQ(media_type, GetRuntimeMediaType(rtp_packet.type()));
238   ASSERT_TRUE(rtp_packet.has_packet_length());
239   EXPECT_EQ(total_size, rtp_packet.packet_length());
240   ASSERT_TRUE(rtp_packet.has_header());
241   ASSERT_EQ(header_size, rtp_packet.header().size());
242   for (size_t i = 0; i < header_size; i++) {
243     EXPECT_EQ(header[i], static_cast<uint8_t>(rtp_packet.header()[i]));
244   }
245 }
246 
VerifyRtcpEvent(const rtclog::Event & event,bool incoming,MediaType media_type,const uint8_t * packet,size_t total_size)247 void VerifyRtcpEvent(const rtclog::Event& event,
248                      bool incoming,
249                      MediaType media_type,
250                      const uint8_t* packet,
251                      size_t total_size) {
252   ASSERT_TRUE(IsValidBasicEvent(event));
253   ASSERT_EQ(rtclog::Event::RTCP_EVENT, event.type());
254   const rtclog::RtcpPacket& rtcp_packet = event.rtcp_packet();
255   ASSERT_TRUE(rtcp_packet.has_incoming());
256   EXPECT_EQ(incoming, rtcp_packet.incoming());
257   ASSERT_TRUE(rtcp_packet.has_type());
258   EXPECT_EQ(media_type, GetRuntimeMediaType(rtcp_packet.type()));
259   ASSERT_TRUE(rtcp_packet.has_packet_data());
260   ASSERT_EQ(total_size, rtcp_packet.packet_data().size());
261   for (size_t i = 0; i < total_size; i++) {
262     EXPECT_EQ(packet[i], static_cast<uint8_t>(rtcp_packet.packet_data()[i]));
263   }
264 }
265 
VerifyPlayoutEvent(const rtclog::Event & event,uint32_t ssrc)266 void VerifyPlayoutEvent(const rtclog::Event& event, uint32_t ssrc) {
267   ASSERT_TRUE(IsValidBasicEvent(event));
268   ASSERT_EQ(rtclog::Event::AUDIO_PLAYOUT_EVENT, event.type());
269   const rtclog::AudioPlayoutEvent& playout_event = event.audio_playout_event();
270   ASSERT_TRUE(playout_event.has_local_ssrc());
271   EXPECT_EQ(ssrc, playout_event.local_ssrc());
272 }
273 
VerifyBweLossEvent(const rtclog::Event & event,int32_t bitrate,uint8_t fraction_loss,int32_t total_packets)274 void VerifyBweLossEvent(const rtclog::Event& event,
275                         int32_t bitrate,
276                         uint8_t fraction_loss,
277                         int32_t total_packets) {
278   ASSERT_TRUE(IsValidBasicEvent(event));
279   ASSERT_EQ(rtclog::Event::BWE_PACKET_LOSS_EVENT, event.type());
280   const rtclog::BwePacketLossEvent& bwe_event = event.bwe_packet_loss_event();
281   ASSERT_TRUE(bwe_event.has_bitrate());
282   EXPECT_EQ(bitrate, bwe_event.bitrate());
283   ASSERT_TRUE(bwe_event.has_fraction_loss());
284   EXPECT_EQ(fraction_loss, bwe_event.fraction_loss());
285   ASSERT_TRUE(bwe_event.has_total_packets());
286   EXPECT_EQ(total_packets, bwe_event.total_packets());
287 }
288 
VerifyLogStartEvent(const rtclog::Event & event)289 void VerifyLogStartEvent(const rtclog::Event& event) {
290   ASSERT_TRUE(IsValidBasicEvent(event));
291   EXPECT_EQ(rtclog::Event::LOG_START, event.type());
292 }
293 
294 /*
295  * Bit number i of extension_bitvector is set to indicate the
296  * presence of extension number i from kExtensionTypes / kExtensionNames.
297  * The least significant bit extension_bitvector has number 0.
298  */
GenerateRtpPacket(uint32_t extensions_bitvector,uint32_t csrcs_count,uint8_t * packet,size_t packet_size,Random * prng)299 size_t GenerateRtpPacket(uint32_t extensions_bitvector,
300                          uint32_t csrcs_count,
301                          uint8_t* packet,
302                          size_t packet_size,
303                          Random* prng) {
304   RTC_CHECK_GE(packet_size, 16 + 4 * csrcs_count + 4 * kNumExtensions);
305   Clock* clock = Clock::GetRealTimeClock();
306 
307   RTPSender rtp_sender(false,     // bool audio
308                        clock,     // Clock* clock
309                        nullptr,   // Transport*
310                        nullptr,   // RtpAudioFeedback*
311                        nullptr,   // PacedSender*
312                        nullptr,   // PacketRouter*
313                        nullptr,   // SendTimeObserver*
314                        nullptr,   // BitrateStatisticsObserver*
315                        nullptr,   // FrameCountObserver*
316                        nullptr);  // SendSideDelayObserver*
317 
318   std::vector<uint32_t> csrcs;
319   for (unsigned i = 0; i < csrcs_count; i++) {
320     csrcs.push_back(prng->Rand<uint32_t>());
321   }
322   rtp_sender.SetCsrcs(csrcs);
323   rtp_sender.SetSSRC(prng->Rand<uint32_t>());
324   rtp_sender.SetStartTimestamp(prng->Rand<uint32_t>(), true);
325   rtp_sender.SetSequenceNumber(prng->Rand<uint16_t>());
326 
327   for (unsigned i = 0; i < kNumExtensions; i++) {
328     if (extensions_bitvector & (1u << i)) {
329       rtp_sender.RegisterRtpHeaderExtension(kExtensionTypes[i], i + 1);
330     }
331   }
332 
333   int8_t payload_type = prng->Rand(0, 127);
334   bool marker_bit = prng->Rand<bool>();
335   uint32_t capture_timestamp = prng->Rand<uint32_t>();
336   int64_t capture_time_ms = prng->Rand<uint32_t>();
337   bool timestamp_provided = prng->Rand<bool>();
338   bool inc_sequence_number = prng->Rand<bool>();
339 
340   size_t header_size = rtp_sender.BuildRTPheader(
341       packet, payload_type, marker_bit, capture_timestamp, capture_time_ms,
342       timestamp_provided, inc_sequence_number);
343 
344   for (size_t i = header_size; i < packet_size; i++) {
345     packet[i] = prng->Rand<uint8_t>();
346   }
347 
348   return header_size;
349 }
350 
GenerateRtcpPacket(Random * prng)351 rtc::scoped_ptr<rtcp::RawPacket> GenerateRtcpPacket(Random* prng) {
352   rtcp::ReportBlock report_block;
353   report_block.To(prng->Rand<uint32_t>());  // Remote SSRC.
354   report_block.WithFractionLost(prng->Rand(50));
355 
356   rtcp::SenderReport sender_report;
357   sender_report.From(prng->Rand<uint32_t>());  // Sender SSRC.
358   sender_report.WithNtpSec(prng->Rand<uint32_t>());
359   sender_report.WithNtpFrac(prng->Rand<uint32_t>());
360   sender_report.WithPacketCount(prng->Rand<uint32_t>());
361   sender_report.WithReportBlock(report_block);
362 
363   return sender_report.Build();
364 }
365 
GenerateVideoReceiveConfig(uint32_t extensions_bitvector,VideoReceiveStream::Config * config,Random * prng)366 void GenerateVideoReceiveConfig(uint32_t extensions_bitvector,
367                                 VideoReceiveStream::Config* config,
368                                 Random* prng) {
369   // Create a map from a payload type to an encoder name.
370   VideoReceiveStream::Decoder decoder;
371   decoder.payload_type = prng->Rand(0, 127);
372   decoder.payload_name = (prng->Rand<bool>() ? "VP8" : "H264");
373   config->decoders.push_back(decoder);
374   // Add SSRCs for the stream.
375   config->rtp.remote_ssrc = prng->Rand<uint32_t>();
376   config->rtp.local_ssrc = prng->Rand<uint32_t>();
377   // Add extensions and settings for RTCP.
378   config->rtp.rtcp_mode =
379       prng->Rand<bool>() ? RtcpMode::kCompound : RtcpMode::kReducedSize;
380   config->rtp.remb = prng->Rand<bool>();
381   // Add a map from a payload type to a new ssrc and a new payload type for RTX.
382   VideoReceiveStream::Config::Rtp::Rtx rtx_pair;
383   rtx_pair.ssrc = prng->Rand<uint32_t>();
384   rtx_pair.payload_type = prng->Rand(0, 127);
385   config->rtp.rtx.insert(std::make_pair(prng->Rand(0, 127), rtx_pair));
386   // Add header extensions.
387   for (unsigned i = 0; i < kNumExtensions; i++) {
388     if (extensions_bitvector & (1u << i)) {
389       config->rtp.extensions.push_back(
390           RtpExtension(kExtensionNames[i], prng->Rand<int>()));
391     }
392   }
393 }
394 
GenerateVideoSendConfig(uint32_t extensions_bitvector,VideoSendStream::Config * config,Random * prng)395 void GenerateVideoSendConfig(uint32_t extensions_bitvector,
396                              VideoSendStream::Config* config,
397                              Random* prng) {
398   // Create a map from a payload type to an encoder name.
399   config->encoder_settings.payload_type = prng->Rand(0, 127);
400   config->encoder_settings.payload_name = (prng->Rand<bool>() ? "VP8" : "H264");
401   // Add SSRCs for the stream.
402   config->rtp.ssrcs.push_back(prng->Rand<uint32_t>());
403   // Add a map from a payload type to new ssrcs and a new payload type for RTX.
404   config->rtp.rtx.ssrcs.push_back(prng->Rand<uint32_t>());
405   config->rtp.rtx.payload_type = prng->Rand(0, 127);
406   // Add header extensions.
407   for (unsigned i = 0; i < kNumExtensions; i++) {
408     if (extensions_bitvector & (1u << i)) {
409       config->rtp.extensions.push_back(
410           RtpExtension(kExtensionNames[i], prng->Rand<int>()));
411     }
412   }
413 }
414 
415 // Test for the RtcEventLog class. Dumps some RTP packets and other events
416 // to disk, then reads them back to see if they match.
LogSessionAndReadBack(size_t rtp_count,size_t rtcp_count,size_t playout_count,size_t bwe_loss_count,uint32_t extensions_bitvector,uint32_t csrcs_count,unsigned int random_seed)417 void LogSessionAndReadBack(size_t rtp_count,
418                            size_t rtcp_count,
419                            size_t playout_count,
420                            size_t bwe_loss_count,
421                            uint32_t extensions_bitvector,
422                            uint32_t csrcs_count,
423                            unsigned int random_seed) {
424   ASSERT_LE(rtcp_count, rtp_count);
425   ASSERT_LE(playout_count, rtp_count);
426   ASSERT_LE(bwe_loss_count, rtp_count);
427   std::vector<rtc::Buffer> rtp_packets;
428   std::vector<rtc::scoped_ptr<rtcp::RawPacket> > rtcp_packets;
429   std::vector<size_t> rtp_header_sizes;
430   std::vector<uint32_t> playout_ssrcs;
431   std::vector<std::pair<int32_t, uint8_t> > bwe_loss_updates;
432 
433   VideoReceiveStream::Config receiver_config(nullptr);
434   VideoSendStream::Config sender_config(nullptr);
435 
436   Random prng(random_seed);
437 
438   // Create rtp_count RTP packets containing random data.
439   for (size_t i = 0; i < rtp_count; i++) {
440     size_t packet_size = prng.Rand(1000, 1100);
441     rtp_packets.push_back(rtc::Buffer(packet_size));
442     size_t header_size =
443         GenerateRtpPacket(extensions_bitvector, csrcs_count,
444                           rtp_packets[i].data(), packet_size, &prng);
445     rtp_header_sizes.push_back(header_size);
446   }
447   // Create rtcp_count RTCP packets containing random data.
448   for (size_t i = 0; i < rtcp_count; i++) {
449     rtcp_packets.push_back(GenerateRtcpPacket(&prng));
450   }
451   // Create playout_count random SSRCs to use when logging AudioPlayout events.
452   for (size_t i = 0; i < playout_count; i++) {
453     playout_ssrcs.push_back(prng.Rand<uint32_t>());
454   }
455   // Create bwe_loss_count random bitrate updates for BwePacketLoss.
456   for (size_t i = 0; i < bwe_loss_count; i++) {
457     bwe_loss_updates.push_back(
458         std::make_pair(prng.Rand<int32_t>(), prng.Rand<uint8_t>()));
459   }
460   // Create configurations for the video streams.
461   GenerateVideoReceiveConfig(extensions_bitvector, &receiver_config, &prng);
462   GenerateVideoSendConfig(extensions_bitvector, &sender_config, &prng);
463   const int config_count = 2;
464 
465   // Find the name of the current test, in order to use it as a temporary
466   // filename.
467   auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
468   const std::string temp_filename =
469       test::OutputPath() + test_info->test_case_name() + test_info->name();
470 
471   // When log_dumper goes out of scope, it causes the log file to be flushed
472   // to disk.
473   {
474     rtc::scoped_ptr<RtcEventLog> log_dumper(RtcEventLog::Create());
475     log_dumper->LogVideoReceiveStreamConfig(receiver_config);
476     log_dumper->LogVideoSendStreamConfig(sender_config);
477     size_t rtcp_index = 1;
478     size_t playout_index = 1;
479     size_t bwe_loss_index = 1;
480     for (size_t i = 1; i <= rtp_count; i++) {
481       log_dumper->LogRtpHeader(
482           (i % 2 == 0),  // Every second packet is incoming.
483           (i % 3 == 0) ? MediaType::AUDIO : MediaType::VIDEO,
484           rtp_packets[i - 1].data(), rtp_packets[i - 1].size());
485       if (i * rtcp_count >= rtcp_index * rtp_count) {
486         log_dumper->LogRtcpPacket(
487             rtcp_index % 2 == 0,  // Every second packet is incoming
488             rtcp_index % 3 == 0 ? MediaType::AUDIO : MediaType::VIDEO,
489             rtcp_packets[rtcp_index - 1]->Buffer(),
490             rtcp_packets[rtcp_index - 1]->Length());
491         rtcp_index++;
492       }
493       if (i * playout_count >= playout_index * rtp_count) {
494         log_dumper->LogAudioPlayout(playout_ssrcs[playout_index - 1]);
495         playout_index++;
496       }
497       if (i * bwe_loss_count >= bwe_loss_index * rtp_count) {
498         log_dumper->LogBwePacketLossEvent(
499             bwe_loss_updates[bwe_loss_index - 1].first,
500             bwe_loss_updates[bwe_loss_index - 1].second, i);
501         bwe_loss_index++;
502       }
503       if (i == rtp_count / 2) {
504         log_dumper->StartLogging(temp_filename, 10000000);
505       }
506     }
507   }
508 
509   // Read the generated file from disk.
510   rtclog::EventStream parsed_stream;
511 
512   ASSERT_TRUE(RtcEventLog::ParseRtcEventLog(temp_filename, &parsed_stream));
513 
514   // Verify that what we read back from the event log is the same as
515   // what we wrote down. For RTCP we log the full packets, but for
516   // RTP we should only log the header.
517   const int event_count = config_count + playout_count + bwe_loss_count +
518                           rtcp_count + rtp_count + 1;
519   EXPECT_EQ(event_count, parsed_stream.stream_size());
520   VerifyReceiveStreamConfig(parsed_stream.stream(0), receiver_config);
521   VerifySendStreamConfig(parsed_stream.stream(1), sender_config);
522   size_t event_index = config_count;
523   size_t rtcp_index = 1;
524   size_t playout_index = 1;
525   size_t bwe_loss_index = 1;
526   for (size_t i = 1; i <= rtp_count; i++) {
527     VerifyRtpEvent(parsed_stream.stream(event_index),
528                    (i % 2 == 0),  // Every second packet is incoming.
529                    (i % 3 == 0) ? MediaType::AUDIO : MediaType::VIDEO,
530                    rtp_packets[i - 1].data(), rtp_header_sizes[i - 1],
531                    rtp_packets[i - 1].size());
532     event_index++;
533     if (i * rtcp_count >= rtcp_index * rtp_count) {
534       VerifyRtcpEvent(parsed_stream.stream(event_index),
535                       rtcp_index % 2 == 0,  // Every second packet is incoming.
536                       rtcp_index % 3 == 0 ? MediaType::AUDIO : MediaType::VIDEO,
537                       rtcp_packets[rtcp_index - 1]->Buffer(),
538                       rtcp_packets[rtcp_index - 1]->Length());
539       event_index++;
540       rtcp_index++;
541     }
542     if (i * playout_count >= playout_index * rtp_count) {
543       VerifyPlayoutEvent(parsed_stream.stream(event_index),
544                          playout_ssrcs[playout_index - 1]);
545       event_index++;
546       playout_index++;
547     }
548     if (i * bwe_loss_count >= bwe_loss_index * rtp_count) {
549       VerifyBweLossEvent(parsed_stream.stream(event_index),
550                          bwe_loss_updates[bwe_loss_index - 1].first,
551                          bwe_loss_updates[bwe_loss_index - 1].second, i);
552       event_index++;
553       bwe_loss_index++;
554     }
555     if (i == rtp_count / 2) {
556       VerifyLogStartEvent(parsed_stream.stream(event_index));
557       event_index++;
558     }
559   }
560 
561   // Clean up temporary file - can be pretty slow.
562   remove(temp_filename.c_str());
563 }
564 
TEST(RtcEventLogTest,LogSessionAndReadBack)565 TEST(RtcEventLogTest, LogSessionAndReadBack) {
566   // Log 5 RTP, 2 RTCP, 0 playout events and 0 BWE events
567   // with no header extensions or CSRCS.
568   LogSessionAndReadBack(5, 2, 0, 0, 0, 0, 321);
569 
570   // Enable AbsSendTime and TransportSequenceNumbers.
571   uint32_t extensions = 0;
572   for (uint32_t i = 0; i < kNumExtensions; i++) {
573     if (kExtensionTypes[i] == RTPExtensionType::kRtpExtensionAbsoluteSendTime ||
574         kExtensionTypes[i] ==
575             RTPExtensionType::kRtpExtensionTransportSequenceNumber) {
576       extensions |= 1u << i;
577     }
578   }
579   LogSessionAndReadBack(8, 2, 0, 0, extensions, 0, 3141592653u);
580 
581   extensions = (1u << kNumExtensions) - 1;  // Enable all header extensions.
582   LogSessionAndReadBack(9, 2, 3, 2, extensions, 2, 2718281828u);
583 
584   // Try all combinations of header extensions and up to 2 CSRCS.
585   for (extensions = 0; extensions < (1u << kNumExtensions); extensions++) {
586     for (uint32_t csrcs_count = 0; csrcs_count < 3; csrcs_count++) {
587       LogSessionAndReadBack(5 + extensions,   // Number of RTP packets.
588                             2 + csrcs_count,  // Number of RTCP packets.
589                             3 + csrcs_count,  // Number of playout events.
590                             1 + csrcs_count,  // Number of BWE loss events.
591                             extensions,       // Bit vector choosing extensions.
592                             csrcs_count,      // Number of contributing sources.
593                             extensions * 3 + csrcs_count + 1);  // Random seed.
594     }
595   }
596 }
597 
598 // Tests that the event queue works correctly, i.e. drops old RTP, RTCP and
599 // debug events, but keeps config events even if they are older than the limit.
DropOldEvents(uint32_t extensions_bitvector,uint32_t csrcs_count,unsigned int random_seed)600 void DropOldEvents(uint32_t extensions_bitvector,
601                    uint32_t csrcs_count,
602                    unsigned int random_seed) {
603   rtc::Buffer old_rtp_packet;
604   rtc::Buffer recent_rtp_packet;
605   rtc::scoped_ptr<rtcp::RawPacket> old_rtcp_packet;
606   rtc::scoped_ptr<rtcp::RawPacket> recent_rtcp_packet;
607 
608   VideoReceiveStream::Config receiver_config(nullptr);
609   VideoSendStream::Config sender_config(nullptr);
610 
611   Random prng(random_seed);
612 
613   // Create two RTP packets containing random data.
614   size_t packet_size = prng.Rand(1000, 1100);
615   old_rtp_packet.SetSize(packet_size);
616   GenerateRtpPacket(extensions_bitvector, csrcs_count, old_rtp_packet.data(),
617                     packet_size, &prng);
618   packet_size = prng.Rand(1000, 1100);
619   recent_rtp_packet.SetSize(packet_size);
620   size_t recent_header_size =
621       GenerateRtpPacket(extensions_bitvector, csrcs_count,
622                         recent_rtp_packet.data(), packet_size, &prng);
623 
624   // Create two RTCP packets containing random data.
625   old_rtcp_packet = GenerateRtcpPacket(&prng);
626   recent_rtcp_packet = GenerateRtcpPacket(&prng);
627 
628   // Create configurations for the video streams.
629   GenerateVideoReceiveConfig(extensions_bitvector, &receiver_config, &prng);
630   GenerateVideoSendConfig(extensions_bitvector, &sender_config, &prng);
631 
632   // Find the name of the current test, in order to use it as a temporary
633   // filename.
634   auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
635   const std::string temp_filename =
636       test::OutputPath() + test_info->test_case_name() + test_info->name();
637 
638   // The log file will be flushed to disk when the log_dumper goes out of scope.
639   {
640     rtc::scoped_ptr<RtcEventLog> log_dumper(RtcEventLog::Create());
641     // Reduce the time old events are stored to 50 ms.
642     log_dumper->SetBufferDuration(50000);
643     log_dumper->LogVideoReceiveStreamConfig(receiver_config);
644     log_dumper->LogVideoSendStreamConfig(sender_config);
645     log_dumper->LogRtpHeader(false, MediaType::AUDIO, old_rtp_packet.data(),
646                              old_rtp_packet.size());
647     log_dumper->LogRtcpPacket(true, MediaType::AUDIO, old_rtcp_packet->Buffer(),
648                               old_rtcp_packet->Length());
649     // Sleep 55 ms to let old events be removed from the queue.
650     rtc::Thread::SleepMs(55);
651     log_dumper->StartLogging(temp_filename, 10000000);
652     log_dumper->LogRtpHeader(true, MediaType::VIDEO, recent_rtp_packet.data(),
653                              recent_rtp_packet.size());
654     log_dumper->LogRtcpPacket(false, MediaType::VIDEO,
655                               recent_rtcp_packet->Buffer(),
656                               recent_rtcp_packet->Length());
657   }
658 
659   // Read the generated file from disk.
660   rtclog::EventStream parsed_stream;
661   ASSERT_TRUE(RtcEventLog::ParseRtcEventLog(temp_filename, &parsed_stream));
662 
663   // Verify that what we read back from the event log is the same as
664   // what we wrote. Old RTP and RTCP events should have been discarded,
665   // but old configuration events should still be available.
666   EXPECT_EQ(5, parsed_stream.stream_size());
667   VerifyReceiveStreamConfig(parsed_stream.stream(0), receiver_config);
668   VerifySendStreamConfig(parsed_stream.stream(1), sender_config);
669   VerifyLogStartEvent(parsed_stream.stream(2));
670   VerifyRtpEvent(parsed_stream.stream(3), true, MediaType::VIDEO,
671                  recent_rtp_packet.data(), recent_header_size,
672                  recent_rtp_packet.size());
673   VerifyRtcpEvent(parsed_stream.stream(4), false, MediaType::VIDEO,
674                   recent_rtcp_packet->Buffer(), recent_rtcp_packet->Length());
675 
676   // Clean up temporary file - can be pretty slow.
677   remove(temp_filename.c_str());
678 }
679 
TEST(RtcEventLogTest,DropOldEvents)680 TEST(RtcEventLogTest, DropOldEvents) {
681   // Enable all header extensions
682   uint32_t extensions = (1u << kNumExtensions) - 1;
683   uint32_t csrcs_count = 2;
684   DropOldEvents(extensions, csrcs_count, 141421356);
685   DropOldEvents(extensions, csrcs_count, 173205080);
686 }
687 
688 }  // namespace webrtc
689 
690 #endif  // ENABLE_RTC_EVENT_LOG
691