1 /*
2 * Copyright (c) 2016 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 "logging/rtc_event_log/rtc_event_log_unittest_helper.h"
12
13 #include <string.h> // memcmp
14
15 #include <cmath>
16 #include <cstdint>
17 #include <limits>
18 #include <memory>
19 #include <numeric>
20 #include <string>
21 #include <utility>
22 #include <vector>
23
24 #include "absl/types/optional.h"
25 #include "api/array_view.h"
26 #include "api/rtp_headers.h"
27 #include "api/rtp_parameters.h"
28 #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h"
29 #include "modules/remote_bitrate_estimator/include/bwe_defines.h"
30 #include "modules/rtp_rtcp/include/rtp_cvo.h"
31 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
32 #include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
33 #include "modules/rtp_rtcp/source/rtcp_packet/rrtr.h"
34 #include "modules/rtp_rtcp/source/rtcp_packet/target_bitrate.h"
35 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
36 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
37 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
38 #include "rtc_base/buffer.h"
39 #include "rtc_base/checks.h"
40 #include "system_wrappers/include/ntp_time.h"
41 #include "test/gtest.h"
42
43 namespace webrtc {
44
45 namespace test {
46
47 namespace {
48
49 struct ExtensionPair {
50 RTPExtensionType type;
51 const char* name;
52 };
53
54 constexpr int kMaxCsrcs = 3;
55
56 // Maximum serialized size of a header extension, including 1 byte ID.
57 constexpr int kMaxExtensionSizeBytes = 4;
58 constexpr int kMaxNumExtensions = 5;
59
60 constexpr ExtensionPair kExtensions[kMaxNumExtensions] = {
61 {RTPExtensionType::kRtpExtensionTransmissionTimeOffset,
62 RtpExtension::kTimestampOffsetUri},
63 {RTPExtensionType::kRtpExtensionAbsoluteSendTime,
64 RtpExtension::kAbsSendTimeUri},
65 {RTPExtensionType::kRtpExtensionTransportSequenceNumber,
66 RtpExtension::kTransportSequenceNumberUri},
67 {RTPExtensionType::kRtpExtensionAudioLevel, RtpExtension::kAudioLevelUri},
68 {RTPExtensionType::kRtpExtensionVideoRotation,
69 RtpExtension::kVideoRotationUri}};
70
71 template <typename T>
ShuffleInPlace(Random * prng,rtc::ArrayView<T> array)72 void ShuffleInPlace(Random* prng, rtc::ArrayView<T> array) {
73 RTC_DCHECK_LE(array.size(), std::numeric_limits<uint32_t>::max());
74 for (uint32_t i = 0; i + 1 < array.size(); i++) {
75 uint32_t other = prng->Rand(i, static_cast<uint32_t>(array.size() - 1));
76 std::swap(array[i], array[other]);
77 }
78 }
79
GetExtensionId(const std::vector<RtpExtension> & extensions,const std::string & uri)80 absl::optional<int> GetExtensionId(const std::vector<RtpExtension>& extensions,
81 const std::string& uri) {
82 for (const auto& extension : extensions) {
83 if (extension.uri == uri)
84 return extension.id;
85 }
86 return absl::nullopt;
87 }
88
89 } // namespace
90
NewAlrState()91 std::unique_ptr<RtcEventAlrState> EventGenerator::NewAlrState() {
92 return std::make_unique<RtcEventAlrState>(prng_.Rand<bool>());
93 }
94
NewAudioPlayout(uint32_t ssrc)95 std::unique_ptr<RtcEventAudioPlayout> EventGenerator::NewAudioPlayout(
96 uint32_t ssrc) {
97 return std::make_unique<RtcEventAudioPlayout>(ssrc);
98 }
99
100 std::unique_ptr<RtcEventAudioNetworkAdaptation>
NewAudioNetworkAdaptation()101 EventGenerator::NewAudioNetworkAdaptation() {
102 std::unique_ptr<AudioEncoderRuntimeConfig> config =
103 std::make_unique<AudioEncoderRuntimeConfig>();
104
105 config->bitrate_bps = prng_.Rand(0, 3000000);
106 config->enable_fec = prng_.Rand<bool>();
107 config->enable_dtx = prng_.Rand<bool>();
108 config->frame_length_ms = prng_.Rand(10, 120);
109 config->num_channels = prng_.Rand(1, 2);
110 config->uplink_packet_loss_fraction = prng_.Rand<float>();
111
112 return std::make_unique<RtcEventAudioNetworkAdaptation>(std::move(config));
113 }
114
115 std::unique_ptr<RtcEventBweUpdateDelayBased>
NewBweUpdateDelayBased()116 EventGenerator::NewBweUpdateDelayBased() {
117 constexpr int32_t kMaxBweBps = 20000000;
118 int32_t bitrate_bps = prng_.Rand(0, kMaxBweBps);
119 BandwidthUsage state = static_cast<BandwidthUsage>(
120 prng_.Rand(static_cast<uint32_t>(BandwidthUsage::kLast) - 1));
121 return std::make_unique<RtcEventBweUpdateDelayBased>(bitrate_bps, state);
122 }
123
124 std::unique_ptr<RtcEventBweUpdateLossBased>
NewBweUpdateLossBased()125 EventGenerator::NewBweUpdateLossBased() {
126 constexpr int32_t kMaxBweBps = 20000000;
127 constexpr int32_t kMaxPackets = 1000;
128 int32_t bitrate_bps = prng_.Rand(0, kMaxBweBps);
129 uint8_t fraction_lost = prng_.Rand<uint8_t>();
130 int32_t total_packets = prng_.Rand(1, kMaxPackets);
131
132 return std::make_unique<RtcEventBweUpdateLossBased>(
133 bitrate_bps, fraction_lost, total_packets);
134 }
135
136 std::unique_ptr<RtcEventDtlsTransportState>
NewDtlsTransportState()137 EventGenerator::NewDtlsTransportState() {
138 DtlsTransportState state = static_cast<DtlsTransportState>(
139 prng_.Rand(static_cast<uint32_t>(DtlsTransportState::kNumValues) - 1));
140
141 return std::make_unique<RtcEventDtlsTransportState>(state);
142 }
143
144 std::unique_ptr<RtcEventDtlsWritableState>
NewDtlsWritableState()145 EventGenerator::NewDtlsWritableState() {
146 bool writable = prng_.Rand<bool>();
147 return std::make_unique<RtcEventDtlsWritableState>(writable);
148 }
149
150 std::unique_ptr<RtcEventProbeClusterCreated>
NewProbeClusterCreated()151 EventGenerator::NewProbeClusterCreated() {
152 constexpr int kMaxBweBps = 20000000;
153 constexpr int kMaxNumProbes = 10000;
154 int id = prng_.Rand(1, kMaxNumProbes);
155 int bitrate_bps = prng_.Rand(0, kMaxBweBps);
156 int min_probes = prng_.Rand(5, 50);
157 int min_bytes = prng_.Rand(500, 50000);
158
159 return std::make_unique<RtcEventProbeClusterCreated>(id, bitrate_bps,
160 min_probes, min_bytes);
161 }
162
163 std::unique_ptr<RtcEventProbeResultFailure>
NewProbeResultFailure()164 EventGenerator::NewProbeResultFailure() {
165 constexpr int kMaxNumProbes = 10000;
166 int id = prng_.Rand(1, kMaxNumProbes);
167 ProbeFailureReason reason = static_cast<ProbeFailureReason>(
168 prng_.Rand(static_cast<uint32_t>(ProbeFailureReason::kLast) - 1));
169
170 return std::make_unique<RtcEventProbeResultFailure>(id, reason);
171 }
172
173 std::unique_ptr<RtcEventProbeResultSuccess>
NewProbeResultSuccess()174 EventGenerator::NewProbeResultSuccess() {
175 constexpr int kMaxBweBps = 20000000;
176 constexpr int kMaxNumProbes = 10000;
177 int id = prng_.Rand(1, kMaxNumProbes);
178 int bitrate_bps = prng_.Rand(0, kMaxBweBps);
179
180 return std::make_unique<RtcEventProbeResultSuccess>(id, bitrate_bps);
181 }
182
183 std::unique_ptr<RtcEventIceCandidatePairConfig>
NewIceCandidatePairConfig()184 EventGenerator::NewIceCandidatePairConfig() {
185 IceCandidateType local_candidate_type = static_cast<IceCandidateType>(
186 prng_.Rand(static_cast<uint32_t>(IceCandidateType::kNumValues) - 1));
187 IceCandidateNetworkType local_network_type =
188 static_cast<IceCandidateNetworkType>(prng_.Rand(
189 static_cast<uint32_t>(IceCandidateNetworkType::kNumValues) - 1));
190 IceCandidatePairAddressFamily local_address_family =
191 static_cast<IceCandidatePairAddressFamily>(prng_.Rand(
192 static_cast<uint32_t>(IceCandidatePairAddressFamily::kNumValues) -
193 1));
194 IceCandidateType remote_candidate_type = static_cast<IceCandidateType>(
195 prng_.Rand(static_cast<uint32_t>(IceCandidateType::kNumValues) - 1));
196 IceCandidatePairAddressFamily remote_address_family =
197 static_cast<IceCandidatePairAddressFamily>(prng_.Rand(
198 static_cast<uint32_t>(IceCandidatePairAddressFamily::kNumValues) -
199 1));
200 IceCandidatePairProtocol protocol_type =
201 static_cast<IceCandidatePairProtocol>(prng_.Rand(
202 static_cast<uint32_t>(IceCandidatePairProtocol::kNumValues) - 1));
203
204 IceCandidatePairDescription desc;
205 desc.local_candidate_type = local_candidate_type;
206 desc.local_relay_protocol = protocol_type;
207 desc.local_network_type = local_network_type;
208 desc.local_address_family = local_address_family;
209 desc.remote_candidate_type = remote_candidate_type;
210 desc.remote_address_family = remote_address_family;
211 desc.candidate_pair_protocol = protocol_type;
212
213 IceCandidatePairConfigType type =
214 static_cast<IceCandidatePairConfigType>(prng_.Rand(
215 static_cast<uint32_t>(IceCandidatePairConfigType::kNumValues) - 1));
216 uint32_t pair_id = prng_.Rand<uint32_t>();
217 return std::make_unique<RtcEventIceCandidatePairConfig>(type, pair_id, desc);
218 }
219
220 std::unique_ptr<RtcEventIceCandidatePair>
NewIceCandidatePair()221 EventGenerator::NewIceCandidatePair() {
222 IceCandidatePairEventType type =
223 static_cast<IceCandidatePairEventType>(prng_.Rand(
224 static_cast<uint32_t>(IceCandidatePairEventType::kNumValues) - 1));
225 uint32_t pair_id = prng_.Rand<uint32_t>();
226 uint32_t transaction_id = prng_.Rand<uint32_t>();
227
228 return std::make_unique<RtcEventIceCandidatePair>(type, pair_id,
229 transaction_id);
230 }
231
NewReportBlock()232 rtcp::ReportBlock EventGenerator::NewReportBlock() {
233 rtcp::ReportBlock report_block;
234 report_block.SetMediaSsrc(prng_.Rand<uint32_t>());
235 report_block.SetFractionLost(prng_.Rand<uint8_t>());
236 // cumulative_lost is a 3-byte signed value.
237 RTC_DCHECK(report_block.SetCumulativeLost(
238 prng_.Rand(-(1 << 23) + 1, (1 << 23) - 1)));
239 report_block.SetExtHighestSeqNum(prng_.Rand<uint32_t>());
240 report_block.SetJitter(prng_.Rand<uint32_t>());
241 report_block.SetLastSr(prng_.Rand<uint32_t>());
242 report_block.SetDelayLastSr(prng_.Rand<uint32_t>());
243 return report_block;
244 }
245
NewSenderReport()246 rtcp::SenderReport EventGenerator::NewSenderReport() {
247 rtcp::SenderReport sender_report;
248 sender_report.SetSenderSsrc(prng_.Rand<uint32_t>());
249 sender_report.SetNtp(NtpTime(prng_.Rand<uint32_t>(), prng_.Rand<uint32_t>()));
250 sender_report.SetRtpTimestamp(prng_.Rand<uint32_t>());
251 sender_report.SetPacketCount(prng_.Rand<uint32_t>());
252 sender_report.SetOctetCount(prng_.Rand<uint32_t>());
253 sender_report.AddReportBlock(NewReportBlock());
254 return sender_report;
255 }
256
NewReceiverReport()257 rtcp::ReceiverReport EventGenerator::NewReceiverReport() {
258 rtcp::ReceiverReport receiver_report;
259 receiver_report.SetSenderSsrc(prng_.Rand<uint32_t>());
260 receiver_report.AddReportBlock(NewReportBlock());
261 return receiver_report;
262 }
263
NewExtendedReports()264 rtcp::ExtendedReports EventGenerator::NewExtendedReports() {
265 rtcp::ExtendedReports extended_report;
266 extended_report.SetSenderSsrc(prng_.Rand<uint32_t>());
267
268 rtcp::Rrtr rrtr;
269 rrtr.SetNtp(NtpTime(prng_.Rand<uint32_t>(), prng_.Rand<uint32_t>()));
270 extended_report.SetRrtr(rrtr);
271
272 rtcp::ReceiveTimeInfo time_info(
273 prng_.Rand<uint32_t>(), prng_.Rand<uint32_t>(), prng_.Rand<uint32_t>());
274 extended_report.AddDlrrItem(time_info);
275
276 rtcp::TargetBitrate target_bitrate;
277 target_bitrate.AddTargetBitrate(/*spatial layer*/ prng_.Rand(0, 3),
278 /*temporal layer*/ prng_.Rand(0, 3),
279 /*bitrate kbps*/ prng_.Rand(0, 50000));
280 target_bitrate.AddTargetBitrate(/*spatial layer*/ prng_.Rand(4, 7),
281 /*temporal layer*/ prng_.Rand(4, 7),
282 /*bitrate kbps*/ prng_.Rand(0, 50000));
283 extended_report.SetTargetBitrate(target_bitrate);
284 return extended_report;
285 }
286
NewNack()287 rtcp::Nack EventGenerator::NewNack() {
288 rtcp::Nack nack;
289 uint16_t base_seq_no = prng_.Rand<uint16_t>();
290 std::vector<uint16_t> nack_list;
291 nack_list.push_back(base_seq_no);
292 for (uint16_t i = 1u; i < 10u; i++) {
293 if (prng_.Rand<bool>())
294 nack_list.push_back(base_seq_no + i);
295 }
296 nack.SetPacketIds(nack_list);
297 return nack;
298 }
299
NewFir()300 rtcp::Fir EventGenerator::NewFir() {
301 rtcp::Fir fir;
302 fir.SetSenderSsrc(prng_.Rand<uint32_t>());
303 fir.AddRequestTo(/*ssrc*/ prng_.Rand<uint32_t>(),
304 /*seq num*/ prng_.Rand<uint8_t>());
305 fir.AddRequestTo(/*ssrc*/ prng_.Rand<uint32_t>(),
306 /*seq num*/ prng_.Rand<uint8_t>());
307 return fir;
308 }
309
NewPli()310 rtcp::Pli EventGenerator::NewPli() {
311 rtcp::Pli pli;
312 pli.SetSenderSsrc(prng_.Rand<uint32_t>());
313 pli.SetMediaSsrc(prng_.Rand<uint32_t>());
314 return pli;
315 }
316
NewTransportFeedback()317 rtcp::TransportFeedback EventGenerator::NewTransportFeedback() {
318 rtcp::TransportFeedback transport_feedback;
319 uint16_t base_seq_no = prng_.Rand<uint16_t>();
320 int64_t base_time_us = prng_.Rand<uint32_t>();
321 transport_feedback.SetBase(base_seq_no, base_time_us);
322 transport_feedback.AddReceivedPacket(base_seq_no, base_time_us);
323 int64_t time_us = base_time_us;
324 for (uint16_t i = 1u; i < 10u; i++) {
325 time_us += prng_.Rand(0, 100000);
326 if (prng_.Rand<bool>()) {
327 transport_feedback.AddReceivedPacket(base_seq_no + i, time_us);
328 }
329 }
330 return transport_feedback;
331 }
332
NewRemb()333 rtcp::Remb EventGenerator::NewRemb() {
334 rtcp::Remb remb;
335 // The remb bitrate is transported as a 16-bit mantissa and an 8-bit exponent.
336 uint64_t bitrate_bps = prng_.Rand(0, (1 << 16) - 1) << prng_.Rand(7);
337 std::vector<uint32_t> ssrcs{prng_.Rand<uint32_t>(), prng_.Rand<uint32_t>()};
338 remb.SetSsrcs(ssrcs);
339 remb.SetBitrateBps(bitrate_bps);
340 return remb;
341 }
342
NewLossNotification()343 rtcp::LossNotification EventGenerator::NewLossNotification() {
344 rtcp::LossNotification loss_notification;
345 const uint16_t last_decoded = prng_.Rand<uint16_t>();
346 const uint16_t last_received =
347 last_decoded + (prng_.Rand<uint16_t>() & 0x7fff);
348 const bool decodability_flag = prng_.Rand<bool>();
349 EXPECT_TRUE(
350 loss_notification.Set(last_decoded, last_received, decodability_flag));
351 return loss_notification;
352 }
353
NewRouteChange()354 std::unique_ptr<RtcEventRouteChange> EventGenerator::NewRouteChange() {
355 return std::make_unique<RtcEventRouteChange>(prng_.Rand<bool>(),
356 prng_.Rand(0, 128));
357 }
358
NewRemoteEstimate()359 std::unique_ptr<RtcEventRemoteEstimate> EventGenerator::NewRemoteEstimate() {
360 return std::make_unique<RtcEventRemoteEstimate>(
361 DataRate::KilobitsPerSec(prng_.Rand(0, 100000)),
362 DataRate::KilobitsPerSec(prng_.Rand(0, 100000)));
363 }
364
365 std::unique_ptr<RtcEventRtcpPacketIncoming>
NewRtcpPacketIncoming()366 EventGenerator::NewRtcpPacketIncoming() {
367 enum class SupportedRtcpTypes {
368 kSenderReport = 0,
369 kReceiverReport,
370 kExtendedReports,
371 kFir,
372 kPli,
373 kNack,
374 kRemb,
375 kTransportFeedback,
376 kNumValues
377 };
378 SupportedRtcpTypes type = static_cast<SupportedRtcpTypes>(
379 prng_.Rand(0, static_cast<int>(SupportedRtcpTypes::kNumValues) - 1));
380 switch (type) {
381 case SupportedRtcpTypes::kSenderReport: {
382 rtcp::SenderReport sender_report = NewSenderReport();
383 rtc::Buffer buffer = sender_report.Build();
384 return std::make_unique<RtcEventRtcpPacketIncoming>(buffer);
385 }
386 case SupportedRtcpTypes::kReceiverReport: {
387 rtcp::ReceiverReport receiver_report = NewReceiverReport();
388 rtc::Buffer buffer = receiver_report.Build();
389 return std::make_unique<RtcEventRtcpPacketIncoming>(buffer);
390 }
391 case SupportedRtcpTypes::kExtendedReports: {
392 rtcp::ExtendedReports extended_report = NewExtendedReports();
393 rtc::Buffer buffer = extended_report.Build();
394 return std::make_unique<RtcEventRtcpPacketIncoming>(buffer);
395 }
396 case SupportedRtcpTypes::kFir: {
397 rtcp::Fir fir = NewFir();
398 rtc::Buffer buffer = fir.Build();
399 return std::make_unique<RtcEventRtcpPacketIncoming>(buffer);
400 }
401 case SupportedRtcpTypes::kPli: {
402 rtcp::Pli pli = NewPli();
403 rtc::Buffer buffer = pli.Build();
404 return std::make_unique<RtcEventRtcpPacketIncoming>(buffer);
405 }
406 case SupportedRtcpTypes::kNack: {
407 rtcp::Nack nack = NewNack();
408 rtc::Buffer buffer = nack.Build();
409 return std::make_unique<RtcEventRtcpPacketIncoming>(buffer);
410 }
411 case SupportedRtcpTypes::kRemb: {
412 rtcp::Remb remb = NewRemb();
413 rtc::Buffer buffer = remb.Build();
414 return std::make_unique<RtcEventRtcpPacketIncoming>(buffer);
415 }
416 case SupportedRtcpTypes::kTransportFeedback: {
417 rtcp::TransportFeedback transport_feedback = NewTransportFeedback();
418 rtc::Buffer buffer = transport_feedback.Build();
419 return std::make_unique<RtcEventRtcpPacketIncoming>(buffer);
420 }
421 default:
422 RTC_NOTREACHED();
423 rtc::Buffer buffer;
424 return std::make_unique<RtcEventRtcpPacketIncoming>(buffer);
425 }
426 }
427
428 std::unique_ptr<RtcEventRtcpPacketOutgoing>
NewRtcpPacketOutgoing()429 EventGenerator::NewRtcpPacketOutgoing() {
430 enum class SupportedRtcpTypes {
431 kSenderReport = 0,
432 kReceiverReport,
433 kExtendedReports,
434 kFir,
435 kPli,
436 kNack,
437 kRemb,
438 kTransportFeedback,
439 kNumValues
440 };
441 SupportedRtcpTypes type = static_cast<SupportedRtcpTypes>(
442 prng_.Rand(0, static_cast<int>(SupportedRtcpTypes::kNumValues) - 1));
443 switch (type) {
444 case SupportedRtcpTypes::kSenderReport: {
445 rtcp::SenderReport sender_report = NewSenderReport();
446 rtc::Buffer buffer = sender_report.Build();
447 return std::make_unique<RtcEventRtcpPacketOutgoing>(buffer);
448 }
449 case SupportedRtcpTypes::kReceiverReport: {
450 rtcp::ReceiverReport receiver_report = NewReceiverReport();
451 rtc::Buffer buffer = receiver_report.Build();
452 return std::make_unique<RtcEventRtcpPacketOutgoing>(buffer);
453 }
454 case SupportedRtcpTypes::kExtendedReports: {
455 rtcp::ExtendedReports extended_report = NewExtendedReports();
456 rtc::Buffer buffer = extended_report.Build();
457 return std::make_unique<RtcEventRtcpPacketOutgoing>(buffer);
458 }
459 case SupportedRtcpTypes::kFir: {
460 rtcp::Fir fir = NewFir();
461 rtc::Buffer buffer = fir.Build();
462 return std::make_unique<RtcEventRtcpPacketOutgoing>(buffer);
463 }
464 case SupportedRtcpTypes::kPli: {
465 rtcp::Pli pli = NewPli();
466 rtc::Buffer buffer = pli.Build();
467 return std::make_unique<RtcEventRtcpPacketOutgoing>(buffer);
468 }
469 case SupportedRtcpTypes::kNack: {
470 rtcp::Nack nack = NewNack();
471 rtc::Buffer buffer = nack.Build();
472 return std::make_unique<RtcEventRtcpPacketOutgoing>(buffer);
473 }
474 case SupportedRtcpTypes::kRemb: {
475 rtcp::Remb remb = NewRemb();
476 rtc::Buffer buffer = remb.Build();
477 return std::make_unique<RtcEventRtcpPacketOutgoing>(buffer);
478 }
479 case SupportedRtcpTypes::kTransportFeedback: {
480 rtcp::TransportFeedback transport_feedback = NewTransportFeedback();
481 rtc::Buffer buffer = transport_feedback.Build();
482 return std::make_unique<RtcEventRtcpPacketOutgoing>(buffer);
483 }
484 default:
485 RTC_NOTREACHED();
486 rtc::Buffer buffer;
487 return std::make_unique<RtcEventRtcpPacketOutgoing>(buffer);
488 }
489 }
490
491 std::unique_ptr<RtcEventGenericPacketSent>
NewGenericPacketSent()492 EventGenerator::NewGenericPacketSent() {
493 return std::make_unique<RtcEventGenericPacketSent>(
494 sent_packet_number_++, prng_.Rand(40, 50), prng_.Rand(0, 150),
495 prng_.Rand(0, 1000));
496 }
497 std::unique_ptr<RtcEventGenericPacketReceived>
NewGenericPacketReceived()498 EventGenerator::NewGenericPacketReceived() {
499 return std::make_unique<RtcEventGenericPacketReceived>(
500 received_packet_number_++, prng_.Rand(40, 250));
501 }
502 std::unique_ptr<RtcEventGenericAckReceived>
NewGenericAckReceived()503 EventGenerator::NewGenericAckReceived() {
504 absl::optional<int64_t> receive_timestamp = absl::nullopt;
505 if (prng_.Rand(0, 2) > 0) {
506 receive_timestamp = prng_.Rand(0, 100000);
507 }
508 AckedPacket packet = {prng_.Rand(40, 250), receive_timestamp};
509 return std::move(RtcEventGenericAckReceived::CreateLogs(
510 received_packet_number_++, std::vector<AckedPacket>{packet})[0]);
511 }
512
RandomizeRtpPacket(size_t payload_size,size_t padding_size,uint32_t ssrc,const RtpHeaderExtensionMap & extension_map,RtpPacket * rtp_packet,bool all_configured_exts)513 void EventGenerator::RandomizeRtpPacket(
514 size_t payload_size,
515 size_t padding_size,
516 uint32_t ssrc,
517 const RtpHeaderExtensionMap& extension_map,
518 RtpPacket* rtp_packet,
519 bool all_configured_exts) {
520 constexpr int kMaxPayloadType = 127;
521 rtp_packet->SetPayloadType(prng_.Rand(kMaxPayloadType));
522 rtp_packet->SetMarker(prng_.Rand<bool>());
523 rtp_packet->SetSequenceNumber(prng_.Rand<uint16_t>());
524 rtp_packet->SetSsrc(ssrc);
525 rtp_packet->SetTimestamp(prng_.Rand<uint32_t>());
526
527 uint32_t csrcs_count = prng_.Rand(0, kMaxCsrcs);
528 std::vector<uint32_t> csrcs;
529 for (size_t i = 0; i < csrcs_count; i++) {
530 csrcs.push_back(prng_.Rand<uint32_t>());
531 }
532 rtp_packet->SetCsrcs(csrcs);
533
534 if (extension_map.IsRegistered(TransmissionOffset::kId) &&
535 (all_configured_exts || prng_.Rand<bool>())) {
536 rtp_packet->SetExtension<TransmissionOffset>(prng_.Rand(0x00ffffff));
537 }
538
539 if (extension_map.IsRegistered(AudioLevel::kId) &&
540 (all_configured_exts || prng_.Rand<bool>())) {
541 rtp_packet->SetExtension<AudioLevel>(prng_.Rand<bool>(), prng_.Rand(127));
542 }
543
544 if (extension_map.IsRegistered(AbsoluteSendTime::kId) &&
545 (all_configured_exts || prng_.Rand<bool>())) {
546 rtp_packet->SetExtension<AbsoluteSendTime>(prng_.Rand(0x00ffffff));
547 }
548
549 if (extension_map.IsRegistered(VideoOrientation::kId) &&
550 (all_configured_exts || prng_.Rand<bool>())) {
551 rtp_packet->SetExtension<VideoOrientation>(prng_.Rand(3));
552 }
553
554 if (extension_map.IsRegistered(TransportSequenceNumber::kId) &&
555 (all_configured_exts || prng_.Rand<bool>())) {
556 rtp_packet->SetExtension<TransportSequenceNumber>(prng_.Rand<uint16_t>());
557 }
558
559 RTC_CHECK_LE(rtp_packet->headers_size() + payload_size, IP_PACKET_SIZE);
560
561 uint8_t* payload = rtp_packet->AllocatePayload(payload_size);
562 RTC_DCHECK(payload != nullptr);
563 for (size_t i = 0; i < payload_size; i++) {
564 payload[i] = prng_.Rand<uint8_t>();
565 }
566 RTC_CHECK(rtp_packet->SetPadding(padding_size));
567 }
568
NewRtpPacketIncoming(uint32_t ssrc,const RtpHeaderExtensionMap & extension_map,bool all_configured_exts)569 std::unique_ptr<RtcEventRtpPacketIncoming> EventGenerator::NewRtpPacketIncoming(
570 uint32_t ssrc,
571 const RtpHeaderExtensionMap& extension_map,
572 bool all_configured_exts) {
573 constexpr size_t kMaxPaddingLength = 224;
574 const bool padding = prng_.Rand(0, 9) == 0; // Let padding be 10% probable.
575 const size_t padding_size = !padding ? 0u : prng_.Rand(0u, kMaxPaddingLength);
576
577 // 12 bytes RTP header, 4 bytes for 0xBEDE + alignment, 4 bytes per CSRC.
578 constexpr size_t kMaxHeaderSize =
579 16 + 4 * kMaxCsrcs + kMaxExtensionSizeBytes * kMaxNumExtensions;
580
581 // In principle, a packet can contain both padding and other payload.
582 // Currently, RTC eventlog encoder-parser can only maintain padding length if
583 // packet is full padding.
584 // TODO(webrtc:9730): Remove the deterministic logic for padding_size > 0.
585 size_t payload_size =
586 padding_size > 0 ? 0
587 : prng_.Rand(0u, static_cast<uint32_t>(IP_PACKET_SIZE -
588 1 - padding_size -
589 kMaxHeaderSize));
590
591 RtpPacketReceived rtp_packet(&extension_map);
592 RandomizeRtpPacket(payload_size, padding_size, ssrc, extension_map,
593 &rtp_packet, all_configured_exts);
594
595 return std::make_unique<RtcEventRtpPacketIncoming>(rtp_packet);
596 }
597
NewRtpPacketOutgoing(uint32_t ssrc,const RtpHeaderExtensionMap & extension_map,bool all_configured_exts)598 std::unique_ptr<RtcEventRtpPacketOutgoing> EventGenerator::NewRtpPacketOutgoing(
599 uint32_t ssrc,
600 const RtpHeaderExtensionMap& extension_map,
601 bool all_configured_exts) {
602 constexpr size_t kMaxPaddingLength = 224;
603 const bool padding = prng_.Rand(0, 9) == 0; // Let padding be 10% probable.
604 const size_t padding_size = !padding ? 0u : prng_.Rand(0u, kMaxPaddingLength);
605
606 // 12 bytes RTP header, 4 bytes for 0xBEDE + alignment, 4 bytes per CSRC.
607 constexpr size_t kMaxHeaderSize =
608 16 + 4 * kMaxCsrcs + kMaxExtensionSizeBytes * kMaxNumExtensions;
609
610 // In principle,a packet can contain both padding and other payload.
611 // Currently, RTC eventlog encoder-parser can only maintain padding length if
612 // packet is full padding.
613 // TODO(webrtc:9730): Remove the deterministic logic for padding_size > 0.
614 size_t payload_size =
615 padding_size > 0 ? 0
616 : prng_.Rand(0u, static_cast<uint32_t>(IP_PACKET_SIZE -
617 1 - padding_size -
618 kMaxHeaderSize));
619
620 RtpPacketToSend rtp_packet(&extension_map,
621 kMaxHeaderSize + payload_size + padding_size);
622 RandomizeRtpPacket(payload_size, padding_size, ssrc, extension_map,
623 &rtp_packet, all_configured_exts);
624
625 int probe_cluster_id = prng_.Rand(0, 100000);
626 return std::make_unique<RtcEventRtpPacketOutgoing>(rtp_packet,
627 probe_cluster_id);
628 }
629
NewRtpHeaderExtensionMap(bool configure_all)630 RtpHeaderExtensionMap EventGenerator::NewRtpHeaderExtensionMap(
631 bool configure_all) {
632 RtpHeaderExtensionMap extension_map;
633 std::vector<int> id(RtpExtension::kOneByteHeaderExtensionMaxId -
634 RtpExtension::kMinId + 1);
635 std::iota(id.begin(), id.end(), RtpExtension::kMinId);
636 ShuffleInPlace(&prng_, rtc::ArrayView<int>(id));
637
638 if (configure_all || prng_.Rand<bool>()) {
639 extension_map.Register<AudioLevel>(id[0]);
640 }
641 if (configure_all || prng_.Rand<bool>()) {
642 extension_map.Register<TransmissionOffset>(id[1]);
643 }
644 if (configure_all || prng_.Rand<bool>()) {
645 extension_map.Register<AbsoluteSendTime>(id[2]);
646 }
647 if (configure_all || prng_.Rand<bool>()) {
648 extension_map.Register<VideoOrientation>(id[3]);
649 }
650 if (configure_all || prng_.Rand<bool>()) {
651 extension_map.Register<TransportSequenceNumber>(id[4]);
652 }
653
654 return extension_map;
655 }
656
657 std::unique_ptr<RtcEventAudioReceiveStreamConfig>
NewAudioReceiveStreamConfig(uint32_t ssrc,const RtpHeaderExtensionMap & extensions)658 EventGenerator::NewAudioReceiveStreamConfig(
659 uint32_t ssrc,
660 const RtpHeaderExtensionMap& extensions) {
661 auto config = std::make_unique<rtclog::StreamConfig>();
662 // Add SSRCs for the stream.
663 config->remote_ssrc = ssrc;
664 config->local_ssrc = prng_.Rand<uint32_t>();
665 // Add header extensions.
666 for (size_t i = 0; i < kMaxNumExtensions; i++) {
667 uint8_t id = extensions.GetId(kExtensions[i].type);
668 if (id != RtpHeaderExtensionMap::kInvalidId) {
669 config->rtp_extensions.emplace_back(kExtensions[i].name, id);
670 }
671 }
672
673 return std::make_unique<RtcEventAudioReceiveStreamConfig>(std::move(config));
674 }
675
676 std::unique_ptr<RtcEventAudioSendStreamConfig>
NewAudioSendStreamConfig(uint32_t ssrc,const RtpHeaderExtensionMap & extensions)677 EventGenerator::NewAudioSendStreamConfig(
678 uint32_t ssrc,
679 const RtpHeaderExtensionMap& extensions) {
680 auto config = std::make_unique<rtclog::StreamConfig>();
681 // Add SSRC to the stream.
682 config->local_ssrc = ssrc;
683 // Add header extensions.
684 for (size_t i = 0; i < kMaxNumExtensions; i++) {
685 uint8_t id = extensions.GetId(kExtensions[i].type);
686 if (id != RtpHeaderExtensionMap::kInvalidId) {
687 config->rtp_extensions.emplace_back(kExtensions[i].name, id);
688 }
689 }
690 return std::make_unique<RtcEventAudioSendStreamConfig>(std::move(config));
691 }
692
693 std::unique_ptr<RtcEventVideoReceiveStreamConfig>
NewVideoReceiveStreamConfig(uint32_t ssrc,const RtpHeaderExtensionMap & extensions)694 EventGenerator::NewVideoReceiveStreamConfig(
695 uint32_t ssrc,
696 const RtpHeaderExtensionMap& extensions) {
697 auto config = std::make_unique<rtclog::StreamConfig>();
698
699 // Add SSRCs for the stream.
700 config->remote_ssrc = ssrc;
701 config->local_ssrc = prng_.Rand<uint32_t>();
702 // Add extensions and settings for RTCP.
703 config->rtcp_mode =
704 prng_.Rand<bool>() ? RtcpMode::kCompound : RtcpMode::kReducedSize;
705 config->remb = prng_.Rand<bool>();
706 config->rtx_ssrc = prng_.Rand<uint32_t>();
707 config->codecs.emplace_back(prng_.Rand<bool>() ? "VP8" : "H264",
708 prng_.Rand(127), prng_.Rand(127));
709 // Add header extensions.
710 for (size_t i = 0; i < kMaxNumExtensions; i++) {
711 uint8_t id = extensions.GetId(kExtensions[i].type);
712 if (id != RtpHeaderExtensionMap::kInvalidId) {
713 config->rtp_extensions.emplace_back(kExtensions[i].name, id);
714 }
715 }
716 return std::make_unique<RtcEventVideoReceiveStreamConfig>(std::move(config));
717 }
718
719 std::unique_ptr<RtcEventVideoSendStreamConfig>
NewVideoSendStreamConfig(uint32_t ssrc,const RtpHeaderExtensionMap & extensions)720 EventGenerator::NewVideoSendStreamConfig(
721 uint32_t ssrc,
722 const RtpHeaderExtensionMap& extensions) {
723 auto config = std::make_unique<rtclog::StreamConfig>();
724
725 config->codecs.emplace_back(prng_.Rand<bool>() ? "VP8" : "H264",
726 prng_.Rand(127), prng_.Rand(127));
727 config->local_ssrc = ssrc;
728 config->rtx_ssrc = prng_.Rand<uint32_t>();
729 // Add header extensions.
730 for (size_t i = 0; i < kMaxNumExtensions; i++) {
731 uint8_t id = extensions.GetId(kExtensions[i].type);
732 if (id != RtpHeaderExtensionMap::kInvalidId) {
733 config->rtp_extensions.emplace_back(kExtensions[i].name, id);
734 }
735 }
736 return std::make_unique<RtcEventVideoSendStreamConfig>(std::move(config));
737 }
738
VerifyLoggedAlrStateEvent(const RtcEventAlrState & original_event,const LoggedAlrStateEvent & logged_event) const739 void EventVerifier::VerifyLoggedAlrStateEvent(
740 const RtcEventAlrState& original_event,
741 const LoggedAlrStateEvent& logged_event) const {
742 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
743 EXPECT_EQ(original_event.in_alr(), logged_event.in_alr);
744 }
745
VerifyLoggedAudioPlayoutEvent(const RtcEventAudioPlayout & original_event,const LoggedAudioPlayoutEvent & logged_event) const746 void EventVerifier::VerifyLoggedAudioPlayoutEvent(
747 const RtcEventAudioPlayout& original_event,
748 const LoggedAudioPlayoutEvent& logged_event) const {
749 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
750 EXPECT_EQ(original_event.ssrc(), logged_event.ssrc);
751 }
752
VerifyLoggedAudioNetworkAdaptationEvent(const RtcEventAudioNetworkAdaptation & original_event,const LoggedAudioNetworkAdaptationEvent & logged_event) const753 void EventVerifier::VerifyLoggedAudioNetworkAdaptationEvent(
754 const RtcEventAudioNetworkAdaptation& original_event,
755 const LoggedAudioNetworkAdaptationEvent& logged_event) const {
756 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
757
758 EXPECT_EQ(original_event.config().bitrate_bps,
759 logged_event.config.bitrate_bps);
760 EXPECT_EQ(original_event.config().enable_dtx, logged_event.config.enable_dtx);
761 EXPECT_EQ(original_event.config().enable_fec, logged_event.config.enable_fec);
762 EXPECT_EQ(original_event.config().frame_length_ms,
763 logged_event.config.frame_length_ms);
764 EXPECT_EQ(original_event.config().num_channels,
765 logged_event.config.num_channels);
766
767 // uplink_packet_loss_fraction
768 ASSERT_EQ(original_event.config().uplink_packet_loss_fraction.has_value(),
769 logged_event.config.uplink_packet_loss_fraction.has_value());
770 if (original_event.config().uplink_packet_loss_fraction.has_value()) {
771 const float original =
772 original_event.config().uplink_packet_loss_fraction.value();
773 const float logged =
774 logged_event.config.uplink_packet_loss_fraction.value();
775 const float uplink_packet_loss_fraction_delta = std::abs(original - logged);
776 EXPECT_LE(uplink_packet_loss_fraction_delta, 0.0001f);
777 }
778 }
779
VerifyLoggedBweDelayBasedUpdate(const RtcEventBweUpdateDelayBased & original_event,const LoggedBweDelayBasedUpdate & logged_event) const780 void EventVerifier::VerifyLoggedBweDelayBasedUpdate(
781 const RtcEventBweUpdateDelayBased& original_event,
782 const LoggedBweDelayBasedUpdate& logged_event) const {
783 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
784 EXPECT_EQ(original_event.bitrate_bps(), logged_event.bitrate_bps);
785 EXPECT_EQ(original_event.detector_state(), logged_event.detector_state);
786 }
787
VerifyLoggedBweLossBasedUpdate(const RtcEventBweUpdateLossBased & original_event,const LoggedBweLossBasedUpdate & logged_event) const788 void EventVerifier::VerifyLoggedBweLossBasedUpdate(
789 const RtcEventBweUpdateLossBased& original_event,
790 const LoggedBweLossBasedUpdate& logged_event) const {
791 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
792 EXPECT_EQ(original_event.bitrate_bps(), logged_event.bitrate_bps);
793 EXPECT_EQ(original_event.fraction_loss(), logged_event.fraction_lost);
794 EXPECT_EQ(original_event.total_packets(), logged_event.expected_packets);
795 }
796
VerifyLoggedBweProbeClusterCreatedEvent(const RtcEventProbeClusterCreated & original_event,const LoggedBweProbeClusterCreatedEvent & logged_event) const797 void EventVerifier::VerifyLoggedBweProbeClusterCreatedEvent(
798 const RtcEventProbeClusterCreated& original_event,
799 const LoggedBweProbeClusterCreatedEvent& logged_event) const {
800 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
801 EXPECT_EQ(original_event.id(), logged_event.id);
802 EXPECT_EQ(original_event.bitrate_bps(), logged_event.bitrate_bps);
803 EXPECT_EQ(original_event.min_probes(), logged_event.min_packets);
804 EXPECT_EQ(original_event.min_bytes(), logged_event.min_bytes);
805 }
806
VerifyLoggedBweProbeFailureEvent(const RtcEventProbeResultFailure & original_event,const LoggedBweProbeFailureEvent & logged_event) const807 void EventVerifier::VerifyLoggedBweProbeFailureEvent(
808 const RtcEventProbeResultFailure& original_event,
809 const LoggedBweProbeFailureEvent& logged_event) const {
810 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
811 EXPECT_EQ(original_event.id(), logged_event.id);
812 EXPECT_EQ(original_event.failure_reason(), logged_event.failure_reason);
813 }
814
VerifyLoggedBweProbeSuccessEvent(const RtcEventProbeResultSuccess & original_event,const LoggedBweProbeSuccessEvent & logged_event) const815 void EventVerifier::VerifyLoggedBweProbeSuccessEvent(
816 const RtcEventProbeResultSuccess& original_event,
817 const LoggedBweProbeSuccessEvent& logged_event) const {
818 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
819 EXPECT_EQ(original_event.id(), logged_event.id);
820 EXPECT_EQ(original_event.bitrate_bps(), logged_event.bitrate_bps);
821 }
822
VerifyLoggedDtlsTransportState(const RtcEventDtlsTransportState & original_event,const LoggedDtlsTransportState & logged_event) const823 void EventVerifier::VerifyLoggedDtlsTransportState(
824 const RtcEventDtlsTransportState& original_event,
825 const LoggedDtlsTransportState& logged_event) const {
826 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
827 EXPECT_EQ(original_event.dtls_transport_state(),
828 logged_event.dtls_transport_state);
829 }
830
VerifyLoggedDtlsWritableState(const RtcEventDtlsWritableState & original_event,const LoggedDtlsWritableState & logged_event) const831 void EventVerifier::VerifyLoggedDtlsWritableState(
832 const RtcEventDtlsWritableState& original_event,
833 const LoggedDtlsWritableState& logged_event) const {
834 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
835 EXPECT_EQ(original_event.writable(), logged_event.writable);
836 }
837
VerifyLoggedIceCandidatePairConfig(const RtcEventIceCandidatePairConfig & original_event,const LoggedIceCandidatePairConfig & logged_event) const838 void EventVerifier::VerifyLoggedIceCandidatePairConfig(
839 const RtcEventIceCandidatePairConfig& original_event,
840 const LoggedIceCandidatePairConfig& logged_event) const {
841 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
842
843 EXPECT_EQ(original_event.type(), logged_event.type);
844 EXPECT_EQ(original_event.candidate_pair_id(), logged_event.candidate_pair_id);
845 EXPECT_EQ(original_event.candidate_pair_desc().local_candidate_type,
846 logged_event.local_candidate_type);
847 EXPECT_EQ(original_event.candidate_pair_desc().local_relay_protocol,
848 logged_event.local_relay_protocol);
849 EXPECT_EQ(original_event.candidate_pair_desc().local_network_type,
850 logged_event.local_network_type);
851 EXPECT_EQ(original_event.candidate_pair_desc().local_address_family,
852 logged_event.local_address_family);
853 EXPECT_EQ(original_event.candidate_pair_desc().remote_candidate_type,
854 logged_event.remote_candidate_type);
855 EXPECT_EQ(original_event.candidate_pair_desc().remote_address_family,
856 logged_event.remote_address_family);
857 EXPECT_EQ(original_event.candidate_pair_desc().candidate_pair_protocol,
858 logged_event.candidate_pair_protocol);
859 }
860
VerifyLoggedIceCandidatePairEvent(const RtcEventIceCandidatePair & original_event,const LoggedIceCandidatePairEvent & logged_event) const861 void EventVerifier::VerifyLoggedIceCandidatePairEvent(
862 const RtcEventIceCandidatePair& original_event,
863 const LoggedIceCandidatePairEvent& logged_event) const {
864 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
865
866 EXPECT_EQ(original_event.type(), logged_event.type);
867 EXPECT_EQ(original_event.candidate_pair_id(), logged_event.candidate_pair_id);
868 if (encoding_type_ == RtcEventLog::EncodingType::NewFormat) {
869 EXPECT_EQ(original_event.transaction_id(), logged_event.transaction_id);
870 }
871 }
872
VerifyLoggedRtpHeader(const RtpPacket & original_header,const RTPHeader & logged_header)873 void VerifyLoggedRtpHeader(const RtpPacket& original_header,
874 const RTPHeader& logged_header) {
875 // Standard RTP header.
876 EXPECT_EQ(original_header.Marker(), logged_header.markerBit);
877 EXPECT_EQ(original_header.PayloadType(), logged_header.payloadType);
878 EXPECT_EQ(original_header.SequenceNumber(), logged_header.sequenceNumber);
879 EXPECT_EQ(original_header.Timestamp(), logged_header.timestamp);
880 EXPECT_EQ(original_header.Ssrc(), logged_header.ssrc);
881
882 EXPECT_EQ(original_header.headers_size(), logged_header.headerLength);
883
884 // TransmissionOffset header extension.
885 ASSERT_EQ(original_header.HasExtension<TransmissionOffset>(),
886 logged_header.extension.hasTransmissionTimeOffset);
887 if (logged_header.extension.hasTransmissionTimeOffset) {
888 int32_t offset;
889 ASSERT_TRUE(original_header.GetExtension<TransmissionOffset>(&offset));
890 EXPECT_EQ(offset, logged_header.extension.transmissionTimeOffset);
891 }
892
893 // AbsoluteSendTime header extension.
894 ASSERT_EQ(original_header.HasExtension<AbsoluteSendTime>(),
895 logged_header.extension.hasAbsoluteSendTime);
896 if (logged_header.extension.hasAbsoluteSendTime) {
897 uint32_t sendtime;
898 ASSERT_TRUE(original_header.GetExtension<AbsoluteSendTime>(&sendtime));
899 EXPECT_EQ(sendtime, logged_header.extension.absoluteSendTime);
900 }
901
902 // TransportSequenceNumber header extension.
903 ASSERT_EQ(original_header.HasExtension<TransportSequenceNumber>(),
904 logged_header.extension.hasTransportSequenceNumber);
905 if (logged_header.extension.hasTransportSequenceNumber) {
906 uint16_t seqnum;
907 ASSERT_TRUE(original_header.GetExtension<TransportSequenceNumber>(&seqnum));
908 EXPECT_EQ(seqnum, logged_header.extension.transportSequenceNumber);
909 }
910
911 // AudioLevel header extension.
912 ASSERT_EQ(original_header.HasExtension<AudioLevel>(),
913 logged_header.extension.hasAudioLevel);
914 if (logged_header.extension.hasAudioLevel) {
915 bool voice_activity;
916 uint8_t audio_level;
917 ASSERT_TRUE(original_header.GetExtension<AudioLevel>(&voice_activity,
918 &audio_level));
919 EXPECT_EQ(voice_activity, logged_header.extension.voiceActivity);
920 EXPECT_EQ(audio_level, logged_header.extension.audioLevel);
921 }
922
923 // VideoOrientation header extension.
924 ASSERT_EQ(original_header.HasExtension<VideoOrientation>(),
925 logged_header.extension.hasVideoRotation);
926 if (logged_header.extension.hasVideoRotation) {
927 uint8_t rotation;
928 ASSERT_TRUE(original_header.GetExtension<VideoOrientation>(&rotation));
929 EXPECT_EQ(ConvertCVOByteToVideoRotation(rotation),
930 logged_header.extension.videoRotation);
931 }
932 }
933
VerifyLoggedRouteChangeEvent(const RtcEventRouteChange & original_event,const LoggedRouteChangeEvent & logged_event) const934 void EventVerifier::VerifyLoggedRouteChangeEvent(
935 const RtcEventRouteChange& original_event,
936 const LoggedRouteChangeEvent& logged_event) const {
937 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
938 EXPECT_EQ(original_event.connected(), logged_event.connected);
939 EXPECT_EQ(original_event.overhead(), logged_event.overhead);
940 }
941
VerifyLoggedRemoteEstimateEvent(const RtcEventRemoteEstimate & original_event,const LoggedRemoteEstimateEvent & logged_event) const942 void EventVerifier::VerifyLoggedRemoteEstimateEvent(
943 const RtcEventRemoteEstimate& original_event,
944 const LoggedRemoteEstimateEvent& logged_event) const {
945 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
946 EXPECT_EQ(original_event.link_capacity_lower_,
947 logged_event.link_capacity_lower);
948 EXPECT_EQ(original_event.link_capacity_upper_,
949 logged_event.link_capacity_upper);
950 }
951
VerifyLoggedRtpPacketIncoming(const RtcEventRtpPacketIncoming & original_event,const LoggedRtpPacketIncoming & logged_event) const952 void EventVerifier::VerifyLoggedRtpPacketIncoming(
953 const RtcEventRtpPacketIncoming& original_event,
954 const LoggedRtpPacketIncoming& logged_event) const {
955 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
956
957 EXPECT_EQ(original_event.header().headers_size(),
958 logged_event.rtp.header_length);
959
960 EXPECT_EQ(original_event.packet_length(), logged_event.rtp.total_length);
961
962 // Currently, RTC eventlog encoder-parser can only maintain padding length
963 // if packet is full padding.
964 EXPECT_EQ(original_event.padding_length(),
965 logged_event.rtp.header.paddingLength);
966
967 VerifyLoggedRtpHeader(original_event.header(), logged_event.rtp.header);
968 }
969
VerifyLoggedRtpPacketOutgoing(const RtcEventRtpPacketOutgoing & original_event,const LoggedRtpPacketOutgoing & logged_event) const970 void EventVerifier::VerifyLoggedRtpPacketOutgoing(
971 const RtcEventRtpPacketOutgoing& original_event,
972 const LoggedRtpPacketOutgoing& logged_event) const {
973 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
974
975 EXPECT_EQ(original_event.header().headers_size(),
976 logged_event.rtp.header_length);
977
978 EXPECT_EQ(original_event.packet_length(), logged_event.rtp.total_length);
979
980 // Currently, RTC eventlog encoder-parser can only maintain padding length
981 // if packet is full padding.
982 EXPECT_EQ(original_event.padding_length(),
983 logged_event.rtp.header.paddingLength);
984
985 // TODO(terelius): Probe cluster ID isn't parsed, used or tested. Unless
986 // someone has a strong reason to keep it, it'll be removed.
987
988 VerifyLoggedRtpHeader(original_event.header(), logged_event.rtp.header);
989 }
990
VerifyLoggedGenericPacketSent(const RtcEventGenericPacketSent & original_event,const LoggedGenericPacketSent & logged_event) const991 void EventVerifier::VerifyLoggedGenericPacketSent(
992 const RtcEventGenericPacketSent& original_event,
993 const LoggedGenericPacketSent& logged_event) const {
994 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
995 EXPECT_EQ(original_event.packet_number(), logged_event.packet_number);
996 EXPECT_EQ(original_event.overhead_length(), logged_event.overhead_length);
997 EXPECT_EQ(original_event.payload_length(), logged_event.payload_length);
998 EXPECT_EQ(original_event.padding_length(), logged_event.padding_length);
999 }
1000
VerifyLoggedGenericPacketReceived(const RtcEventGenericPacketReceived & original_event,const LoggedGenericPacketReceived & logged_event) const1001 void EventVerifier::VerifyLoggedGenericPacketReceived(
1002 const RtcEventGenericPacketReceived& original_event,
1003 const LoggedGenericPacketReceived& logged_event) const {
1004 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
1005 EXPECT_EQ(original_event.packet_number(), logged_event.packet_number);
1006 EXPECT_EQ(static_cast<int>(original_event.packet_length()),
1007 logged_event.packet_length);
1008 }
1009
VerifyLoggedGenericAckReceived(const RtcEventGenericAckReceived & original_event,const LoggedGenericAckReceived & logged_event) const1010 void EventVerifier::VerifyLoggedGenericAckReceived(
1011 const RtcEventGenericAckReceived& original_event,
1012 const LoggedGenericAckReceived& logged_event) const {
1013 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
1014 EXPECT_EQ(original_event.packet_number(), logged_event.packet_number);
1015 EXPECT_EQ(original_event.acked_packet_number(),
1016 logged_event.acked_packet_number);
1017 EXPECT_EQ(original_event.receive_acked_packet_time_ms(),
1018 logged_event.receive_acked_packet_time_ms);
1019 }
1020
VerifyLoggedRtcpPacketIncoming(const RtcEventRtcpPacketIncoming & original_event,const LoggedRtcpPacketIncoming & logged_event) const1021 void EventVerifier::VerifyLoggedRtcpPacketIncoming(
1022 const RtcEventRtcpPacketIncoming& original_event,
1023 const LoggedRtcpPacketIncoming& logged_event) const {
1024 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
1025
1026 ASSERT_EQ(original_event.packet().size(), logged_event.rtcp.raw_data.size());
1027 EXPECT_EQ(
1028 memcmp(original_event.packet().data(), logged_event.rtcp.raw_data.data(),
1029 original_event.packet().size()),
1030 0);
1031 }
1032
VerifyLoggedRtcpPacketOutgoing(const RtcEventRtcpPacketOutgoing & original_event,const LoggedRtcpPacketOutgoing & logged_event) const1033 void EventVerifier::VerifyLoggedRtcpPacketOutgoing(
1034 const RtcEventRtcpPacketOutgoing& original_event,
1035 const LoggedRtcpPacketOutgoing& logged_event) const {
1036 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
1037
1038 ASSERT_EQ(original_event.packet().size(), logged_event.rtcp.raw_data.size());
1039 EXPECT_EQ(
1040 memcmp(original_event.packet().data(), logged_event.rtcp.raw_data.data(),
1041 original_event.packet().size()),
1042 0);
1043 }
1044
VerifyReportBlock(const rtcp::ReportBlock & original_report_block,const rtcp::ReportBlock & logged_report_block)1045 void EventVerifier::VerifyReportBlock(
1046 const rtcp::ReportBlock& original_report_block,
1047 const rtcp::ReportBlock& logged_report_block) {
1048 EXPECT_EQ(original_report_block.source_ssrc(),
1049 logged_report_block.source_ssrc());
1050 EXPECT_EQ(original_report_block.fraction_lost(),
1051 logged_report_block.fraction_lost());
1052 EXPECT_EQ(original_report_block.cumulative_lost_signed(),
1053 logged_report_block.cumulative_lost_signed());
1054 EXPECT_EQ(original_report_block.extended_high_seq_num(),
1055 logged_report_block.extended_high_seq_num());
1056 EXPECT_EQ(original_report_block.jitter(), logged_report_block.jitter());
1057 EXPECT_EQ(original_report_block.last_sr(), logged_report_block.last_sr());
1058 EXPECT_EQ(original_report_block.delay_since_last_sr(),
1059 logged_report_block.delay_since_last_sr());
1060 }
1061
VerifyLoggedSenderReport(int64_t log_time_us,const rtcp::SenderReport & original_sr,const LoggedRtcpPacketSenderReport & logged_sr)1062 void EventVerifier::VerifyLoggedSenderReport(
1063 int64_t log_time_us,
1064 const rtcp::SenderReport& original_sr,
1065 const LoggedRtcpPacketSenderReport& logged_sr) {
1066 EXPECT_EQ(log_time_us, logged_sr.log_time_us());
1067 EXPECT_EQ(original_sr.sender_ssrc(), logged_sr.sr.sender_ssrc());
1068 EXPECT_EQ(original_sr.ntp(), logged_sr.sr.ntp());
1069 EXPECT_EQ(original_sr.rtp_timestamp(), logged_sr.sr.rtp_timestamp());
1070 EXPECT_EQ(original_sr.sender_packet_count(),
1071 logged_sr.sr.sender_packet_count());
1072 EXPECT_EQ(original_sr.sender_octet_count(),
1073 logged_sr.sr.sender_octet_count());
1074 ASSERT_EQ(original_sr.report_blocks().size(),
1075 logged_sr.sr.report_blocks().size());
1076 for (size_t i = 0; i < original_sr.report_blocks().size(); i++) {
1077 VerifyReportBlock(original_sr.report_blocks()[i],
1078 logged_sr.sr.report_blocks()[i]);
1079 }
1080 }
1081
VerifyLoggedReceiverReport(int64_t log_time_us,const rtcp::ReceiverReport & original_rr,const LoggedRtcpPacketReceiverReport & logged_rr)1082 void EventVerifier::VerifyLoggedReceiverReport(
1083 int64_t log_time_us,
1084 const rtcp::ReceiverReport& original_rr,
1085 const LoggedRtcpPacketReceiverReport& logged_rr) {
1086 EXPECT_EQ(log_time_us, logged_rr.log_time_us());
1087 EXPECT_EQ(original_rr.sender_ssrc(), logged_rr.rr.sender_ssrc());
1088 ASSERT_EQ(original_rr.report_blocks().size(),
1089 logged_rr.rr.report_blocks().size());
1090 for (size_t i = 0; i < original_rr.report_blocks().size(); i++) {
1091 VerifyReportBlock(original_rr.report_blocks()[i],
1092 logged_rr.rr.report_blocks()[i]);
1093 }
1094 }
1095
VerifyLoggedExtendedReports(int64_t log_time_us,const rtcp::ExtendedReports & original_xr,const LoggedRtcpPacketExtendedReports & logged_xr)1096 void EventVerifier::VerifyLoggedExtendedReports(
1097 int64_t log_time_us,
1098 const rtcp::ExtendedReports& original_xr,
1099 const LoggedRtcpPacketExtendedReports& logged_xr) {
1100 EXPECT_EQ(original_xr.sender_ssrc(), logged_xr.xr.sender_ssrc());
1101
1102 EXPECT_EQ(original_xr.rrtr().has_value(), logged_xr.xr.rrtr().has_value());
1103 if (original_xr.rrtr().has_value() && logged_xr.xr.rrtr().has_value()) {
1104 EXPECT_EQ(original_xr.rrtr()->ntp(), logged_xr.xr.rrtr()->ntp());
1105 }
1106
1107 const auto& original_subblocks = original_xr.dlrr().sub_blocks();
1108 const auto& logged_subblocks = logged_xr.xr.dlrr().sub_blocks();
1109 ASSERT_EQ(original_subblocks.size(), logged_subblocks.size());
1110 for (size_t i = 0; i < original_subblocks.size(); i++) {
1111 EXPECT_EQ(original_subblocks[i].ssrc, logged_subblocks[i].ssrc);
1112 EXPECT_EQ(original_subblocks[i].last_rr, logged_subblocks[i].last_rr);
1113 EXPECT_EQ(original_subblocks[i].delay_since_last_rr,
1114 logged_subblocks[i].delay_since_last_rr);
1115 }
1116
1117 EXPECT_EQ(original_xr.target_bitrate().has_value(),
1118 logged_xr.xr.target_bitrate().has_value());
1119 if (original_xr.target_bitrate().has_value() &&
1120 logged_xr.xr.target_bitrate().has_value()) {
1121 const auto& original_bitrates =
1122 original_xr.target_bitrate()->GetTargetBitrates();
1123 const auto& logged_bitrates =
1124 logged_xr.xr.target_bitrate()->GetTargetBitrates();
1125 ASSERT_EQ(original_bitrates.size(), logged_bitrates.size());
1126 for (size_t i = 0; i < original_bitrates.size(); i++) {
1127 EXPECT_EQ(original_bitrates[i].spatial_layer,
1128 logged_bitrates[i].spatial_layer);
1129 EXPECT_EQ(original_bitrates[i].temporal_layer,
1130 logged_bitrates[i].temporal_layer);
1131 EXPECT_EQ(original_bitrates[i].target_bitrate_kbps,
1132 logged_bitrates[i].target_bitrate_kbps);
1133 }
1134 }
1135 }
1136
VerifyLoggedFir(int64_t log_time_us,const rtcp::Fir & original_fir,const LoggedRtcpPacketFir & logged_fir)1137 void EventVerifier::VerifyLoggedFir(int64_t log_time_us,
1138 const rtcp::Fir& original_fir,
1139 const LoggedRtcpPacketFir& logged_fir) {
1140 EXPECT_EQ(original_fir.sender_ssrc(), logged_fir.fir.sender_ssrc());
1141
1142 const auto& original_requests = original_fir.requests();
1143 const auto& logged_requests = logged_fir.fir.requests();
1144 ASSERT_EQ(original_requests.size(), logged_requests.size());
1145 for (size_t i = 0; i < original_requests.size(); i++) {
1146 EXPECT_EQ(original_requests[i].ssrc, logged_requests[i].ssrc);
1147 EXPECT_EQ(original_requests[i].seq_nr, logged_requests[i].seq_nr);
1148 }
1149 }
1150
VerifyLoggedPli(int64_t log_time_us,const rtcp::Pli & original_pli,const LoggedRtcpPacketPli & logged_pli)1151 void EventVerifier::VerifyLoggedPli(int64_t log_time_us,
1152 const rtcp::Pli& original_pli,
1153 const LoggedRtcpPacketPli& logged_pli) {
1154 EXPECT_EQ(original_pli.sender_ssrc(), logged_pli.pli.sender_ssrc());
1155 EXPECT_EQ(original_pli.media_ssrc(), logged_pli.pli.media_ssrc());
1156 }
1157
VerifyLoggedNack(int64_t log_time_us,const rtcp::Nack & original_nack,const LoggedRtcpPacketNack & logged_nack)1158 void EventVerifier::VerifyLoggedNack(int64_t log_time_us,
1159 const rtcp::Nack& original_nack,
1160 const LoggedRtcpPacketNack& logged_nack) {
1161 EXPECT_EQ(log_time_us, logged_nack.log_time_us());
1162 EXPECT_EQ(original_nack.packet_ids(), logged_nack.nack.packet_ids());
1163 }
1164
VerifyLoggedTransportFeedback(int64_t log_time_us,const rtcp::TransportFeedback & original_transport_feedback,const LoggedRtcpPacketTransportFeedback & logged_transport_feedback)1165 void EventVerifier::VerifyLoggedTransportFeedback(
1166 int64_t log_time_us,
1167 const rtcp::TransportFeedback& original_transport_feedback,
1168 const LoggedRtcpPacketTransportFeedback& logged_transport_feedback) {
1169 EXPECT_EQ(log_time_us, logged_transport_feedback.log_time_us());
1170 ASSERT_EQ(
1171 original_transport_feedback.GetReceivedPackets().size(),
1172 logged_transport_feedback.transport_feedback.GetReceivedPackets().size());
1173 for (size_t i = 0;
1174 i < original_transport_feedback.GetReceivedPackets().size(); i++) {
1175 EXPECT_EQ(
1176 original_transport_feedback.GetReceivedPackets()[i].sequence_number(),
1177 logged_transport_feedback.transport_feedback.GetReceivedPackets()[i]
1178 .sequence_number());
1179 EXPECT_EQ(
1180 original_transport_feedback.GetReceivedPackets()[i].delta_us(),
1181 logged_transport_feedback.transport_feedback.GetReceivedPackets()[i]
1182 .delta_us());
1183 }
1184 }
1185
VerifyLoggedRemb(int64_t log_time_us,const rtcp::Remb & original_remb,const LoggedRtcpPacketRemb & logged_remb)1186 void EventVerifier::VerifyLoggedRemb(int64_t log_time_us,
1187 const rtcp::Remb& original_remb,
1188 const LoggedRtcpPacketRemb& logged_remb) {
1189 EXPECT_EQ(log_time_us, logged_remb.log_time_us());
1190 EXPECT_EQ(original_remb.ssrcs(), logged_remb.remb.ssrcs());
1191 EXPECT_EQ(original_remb.bitrate_bps(), logged_remb.remb.bitrate_bps());
1192 }
1193
VerifyLoggedLossNotification(int64_t log_time_us,const rtcp::LossNotification & original_loss_notification,const LoggedRtcpPacketLossNotification & logged_loss_notification)1194 void EventVerifier::VerifyLoggedLossNotification(
1195 int64_t log_time_us,
1196 const rtcp::LossNotification& original_loss_notification,
1197 const LoggedRtcpPacketLossNotification& logged_loss_notification) {
1198 EXPECT_EQ(log_time_us, logged_loss_notification.log_time_us());
1199 EXPECT_EQ(original_loss_notification.last_decoded(),
1200 logged_loss_notification.loss_notification.last_decoded());
1201 EXPECT_EQ(original_loss_notification.last_received(),
1202 logged_loss_notification.loss_notification.last_received());
1203 EXPECT_EQ(original_loss_notification.decodability_flag(),
1204 logged_loss_notification.loss_notification.decodability_flag());
1205 }
1206
VerifyLoggedStartEvent(int64_t start_time_us,int64_t utc_start_time_us,const LoggedStartEvent & logged_event) const1207 void EventVerifier::VerifyLoggedStartEvent(
1208 int64_t start_time_us,
1209 int64_t utc_start_time_us,
1210 const LoggedStartEvent& logged_event) const {
1211 EXPECT_EQ(start_time_us / 1000, logged_event.log_time_ms());
1212 if (encoding_type_ == RtcEventLog::EncodingType::NewFormat) {
1213 EXPECT_EQ(utc_start_time_us / 1000, logged_event.utc_start_time_ms);
1214 }
1215 }
1216
VerifyLoggedStopEvent(int64_t stop_time_us,const LoggedStopEvent & logged_event) const1217 void EventVerifier::VerifyLoggedStopEvent(
1218 int64_t stop_time_us,
1219 const LoggedStopEvent& logged_event) const {
1220 EXPECT_EQ(stop_time_us / 1000, logged_event.log_time_ms());
1221 }
1222
VerifyLoggedStreamConfig(const rtclog::StreamConfig & original_config,const rtclog::StreamConfig & logged_config)1223 void VerifyLoggedStreamConfig(const rtclog::StreamConfig& original_config,
1224 const rtclog::StreamConfig& logged_config) {
1225 EXPECT_EQ(original_config.local_ssrc, logged_config.local_ssrc);
1226 EXPECT_EQ(original_config.remote_ssrc, logged_config.remote_ssrc);
1227 EXPECT_EQ(original_config.rtx_ssrc, logged_config.rtx_ssrc);
1228
1229 EXPECT_EQ(original_config.rtp_extensions.size(),
1230 logged_config.rtp_extensions.size());
1231 size_t recognized_extensions = 0;
1232 for (size_t i = 0; i < kMaxNumExtensions; i++) {
1233 auto original_id =
1234 GetExtensionId(original_config.rtp_extensions, kExtensions[i].name);
1235 auto logged_id =
1236 GetExtensionId(logged_config.rtp_extensions, kExtensions[i].name);
1237 EXPECT_EQ(original_id, logged_id)
1238 << "IDs for " << kExtensions[i].name << " don't match. Original ID "
1239 << original_id.value_or(-1) << ". Parsed ID " << logged_id.value_or(-1)
1240 << ".";
1241 if (original_id) {
1242 recognized_extensions++;
1243 }
1244 }
1245 EXPECT_EQ(recognized_extensions, original_config.rtp_extensions.size());
1246 }
1247
VerifyLoggedAudioRecvConfig(const RtcEventAudioReceiveStreamConfig & original_event,const LoggedAudioRecvConfig & logged_event) const1248 void EventVerifier::VerifyLoggedAudioRecvConfig(
1249 const RtcEventAudioReceiveStreamConfig& original_event,
1250 const LoggedAudioRecvConfig& logged_event) const {
1251 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
1252 VerifyLoggedStreamConfig(original_event.config(), logged_event.config);
1253 }
1254
VerifyLoggedAudioSendConfig(const RtcEventAudioSendStreamConfig & original_event,const LoggedAudioSendConfig & logged_event) const1255 void EventVerifier::VerifyLoggedAudioSendConfig(
1256 const RtcEventAudioSendStreamConfig& original_event,
1257 const LoggedAudioSendConfig& logged_event) const {
1258 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
1259 VerifyLoggedStreamConfig(original_event.config(), logged_event.config);
1260 }
1261
VerifyLoggedVideoRecvConfig(const RtcEventVideoReceiveStreamConfig & original_event,const LoggedVideoRecvConfig & logged_event) const1262 void EventVerifier::VerifyLoggedVideoRecvConfig(
1263 const RtcEventVideoReceiveStreamConfig& original_event,
1264 const LoggedVideoRecvConfig& logged_event) const {
1265 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
1266 VerifyLoggedStreamConfig(original_event.config(), logged_event.config);
1267 }
1268
VerifyLoggedVideoSendConfig(const RtcEventVideoSendStreamConfig & original_event,const LoggedVideoSendConfig & logged_event) const1269 void EventVerifier::VerifyLoggedVideoSendConfig(
1270 const RtcEventVideoSendStreamConfig& original_event,
1271 const LoggedVideoSendConfig& logged_event) const {
1272 EXPECT_EQ(original_event.timestamp_ms(), logged_event.log_time_ms());
1273 VerifyLoggedStreamConfig(original_event.config(), logged_event.config);
1274 }
1275
1276 } // namespace test
1277 } // namespace webrtc
1278