Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 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 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 13 | #include <map> |
kwiberg | b25345e | 2016-03-12 06:10:44 -0800 | [diff] [blame] | 14 | #include <memory> |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 15 | #include <string> |
Fredrik Solenberg | ea07373 | 2015-12-01 11:26:34 +0100 | [diff] [blame] | 16 | #include <utility> |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 20 | #include "webrtc/base/buffer.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 21 | #include "webrtc/base/checks.h" |
terelius | 84e78f9 | 2015-12-10 01:50:55 -0800 | [diff] [blame] | 22 | #include "webrtc/base/random.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 23 | #include "webrtc/call.h" |
Peter Boström | 5c389d3 | 2015-09-25 13:58:30 +0200 | [diff] [blame] | 24 | #include "webrtc/call/rtc_event_log.h" |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 25 | #include "webrtc/call/rtc_event_log_parser.h" |
| 26 | #include "webrtc/call/rtc_event_log_unittest_helper.h" |
terelius | d66daa2 | 2015-11-06 09:00:18 -0800 | [diff] [blame] | 27 | #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h" |
danilchap | 34ed2b9 | 2016-01-18 02:43:32 -0800 | [diff] [blame] | 28 | #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h" |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 29 | #include "webrtc/modules/rtp_rtcp/source/rtp_sender.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 30 | #include "webrtc/system_wrappers/include/clock.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 31 | #include "webrtc/test/test_suite.h" |
| 32 | #include "webrtc/test/testsupport/fileutils.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 33 | |
| 34 | // Files generated at build-time by the protobuf compiler. |
| 35 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
Peter Boström | 5c389d3 | 2015-09-25 13:58:30 +0200 | [diff] [blame] | 36 | #include "external/webrtc/webrtc/call/rtc_event_log.pb.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 37 | #else |
Peter Boström | 5c389d3 | 2015-09-25 13:58:30 +0200 | [diff] [blame] | 38 | #include "webrtc/call/rtc_event_log.pb.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | namespace webrtc { |
| 42 | |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 43 | namespace { |
| 44 | |
| 45 | const RTPExtensionType kExtensionTypes[] = { |
| 46 | RTPExtensionType::kRtpExtensionTransmissionTimeOffset, |
| 47 | RTPExtensionType::kRtpExtensionAudioLevel, |
| 48 | RTPExtensionType::kRtpExtensionAbsoluteSendTime, |
| 49 | RTPExtensionType::kRtpExtensionVideoRotation, |
| 50 | RTPExtensionType::kRtpExtensionTransportSequenceNumber}; |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame^] | 51 | const char* kExtensionNames[] = { |
| 52 | RtpExtension::kTimestampOffsetUri, RtpExtension::kAudioLevelUri, |
| 53 | RtpExtension::kAbsSendTimeUri, RtpExtension::kVideoRotationUri, |
| 54 | RtpExtension::kTransportSequenceNumberUri}; |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 55 | const size_t kNumExtensions = 5; |
| 56 | |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 57 | void PrintActualEvents(const ParsedRtcEventLog& parsed_log) { |
| 58 | std::map<int, size_t> actual_event_counts; |
| 59 | for (size_t i = 0; i < parsed_log.GetNumberOfEvents(); i++) { |
| 60 | actual_event_counts[parsed_log.GetEventType(i)]++; |
| 61 | } |
| 62 | printf("Actual events: "); |
| 63 | for (auto kv : actual_event_counts) { |
| 64 | printf("%d_count = %zu, ", kv.first, kv.second); |
| 65 | } |
| 66 | printf("\n"); |
| 67 | for (size_t i = 0; i < parsed_log.GetNumberOfEvents(); i++) { |
| 68 | printf("%4d ", parsed_log.GetEventType(i)); |
| 69 | } |
| 70 | printf("\n"); |
| 71 | } |
| 72 | |
| 73 | void PrintExpectedEvents(size_t rtp_count, |
| 74 | size_t rtcp_count, |
| 75 | size_t playout_count, |
| 76 | size_t bwe_loss_count) { |
| 77 | printf( |
| 78 | "Expected events: rtp_count = %zu, rtcp_count = %zu," |
| 79 | "playout_count = %zu, bwe_loss_count = %zu\n", |
| 80 | rtp_count, rtcp_count, playout_count, bwe_loss_count); |
| 81 | size_t rtcp_index = 1, playout_index = 1, bwe_loss_index = 1; |
| 82 | printf("strt cfg cfg "); |
| 83 | for (size_t i = 1; i <= rtp_count; i++) { |
| 84 | printf(" rtp "); |
| 85 | if (i * rtcp_count >= rtcp_index * rtp_count) { |
| 86 | printf("rtcp "); |
| 87 | rtcp_index++; |
| 88 | } |
| 89 | if (i * playout_count >= playout_index * rtp_count) { |
| 90 | printf("play "); |
| 91 | playout_index++; |
| 92 | } |
| 93 | if (i * bwe_loss_count >= bwe_loss_index * rtp_count) { |
| 94 | printf("loss "); |
| 95 | bwe_loss_index++; |
| 96 | } |
| 97 | } |
| 98 | printf("end \n"); |
| 99 | } |
Peter Boström | 5c389d3 | 2015-09-25 13:58:30 +0200 | [diff] [blame] | 100 | } // namespace |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 101 | |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 102 | /* |
| 103 | * Bit number i of extension_bitvector is set to indicate the |
| 104 | * presence of extension number i from kExtensionTypes / kExtensionNames. |
| 105 | * The least significant bit extension_bitvector has number 0. |
| 106 | */ |
| 107 | size_t GenerateRtpPacket(uint32_t extensions_bitvector, |
| 108 | uint32_t csrcs_count, |
| 109 | uint8_t* packet, |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 110 | size_t packet_size, |
terelius | 84e78f9 | 2015-12-10 01:50:55 -0800 | [diff] [blame] | 111 | Random* prng) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 112 | RTC_CHECK_GE(packet_size, 16 + 4 * csrcs_count + 4 * kNumExtensions); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 113 | Clock* clock = Clock::GetRealTimeClock(); |
| 114 | |
Peter Boström | ac547a6 | 2015-09-17 23:03:57 +0200 | [diff] [blame] | 115 | RTPSender rtp_sender(false, // bool audio |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 116 | clock, // Clock* clock |
| 117 | nullptr, // Transport* |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 118 | nullptr, // PacedSender* |
| 119 | nullptr, // PacketRouter* |
| 120 | nullptr, // SendTimeObserver* |
| 121 | nullptr, // BitrateStatisticsObserver* |
| 122 | nullptr, // FrameCountObserver* |
terelius | 429c345 | 2016-01-21 05:42:04 -0800 | [diff] [blame] | 123 | nullptr, // SendSideDelayObserver* |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 124 | nullptr, // RtcEventLog* |
| 125 | nullptr); // SendPacketObserver* |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 126 | |
| 127 | std::vector<uint32_t> csrcs; |
| 128 | for (unsigned i = 0; i < csrcs_count; i++) { |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 129 | csrcs.push_back(prng->Rand<uint32_t>()); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 130 | } |
| 131 | rtp_sender.SetCsrcs(csrcs); |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 132 | rtp_sender.SetSSRC(prng->Rand<uint32_t>()); |
| 133 | rtp_sender.SetStartTimestamp(prng->Rand<uint32_t>(), true); |
| 134 | rtp_sender.SetSequenceNumber(prng->Rand<uint16_t>()); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 135 | |
| 136 | for (unsigned i = 0; i < kNumExtensions; i++) { |
| 137 | if (extensions_bitvector & (1u << i)) { |
| 138 | rtp_sender.RegisterRtpHeaderExtension(kExtensionTypes[i], i + 1); |
| 139 | } |
| 140 | } |
| 141 | |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 142 | int8_t payload_type = prng->Rand(0, 127); |
| 143 | bool marker_bit = prng->Rand<bool>(); |
| 144 | uint32_t capture_timestamp = prng->Rand<uint32_t>(); |
| 145 | int64_t capture_time_ms = prng->Rand<uint32_t>(); |
| 146 | bool timestamp_provided = prng->Rand<bool>(); |
| 147 | bool inc_sequence_number = prng->Rand<bool>(); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 148 | |
| 149 | size_t header_size = rtp_sender.BuildRTPheader( |
| 150 | packet, payload_type, marker_bit, capture_timestamp, capture_time_ms, |
| 151 | timestamp_provided, inc_sequence_number); |
| 152 | |
| 153 | for (size_t i = header_size; i < packet_size; i++) { |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 154 | packet[i] = prng->Rand<uint8_t>(); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return header_size; |
| 158 | } |
| 159 | |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 160 | rtc::Buffer GenerateRtcpPacket(Random* prng) { |
terelius | d66daa2 | 2015-11-06 09:00:18 -0800 | [diff] [blame] | 161 | rtcp::ReportBlock report_block; |
| 162 | report_block.To(prng->Rand<uint32_t>()); // Remote SSRC. |
| 163 | report_block.WithFractionLost(prng->Rand(50)); |
| 164 | |
| 165 | rtcp::SenderReport sender_report; |
| 166 | sender_report.From(prng->Rand<uint32_t>()); // Sender SSRC. |
danilchap | 34ed2b9 | 2016-01-18 02:43:32 -0800 | [diff] [blame] | 167 | sender_report.WithNtp( |
| 168 | NtpTime(prng->Rand<uint32_t>(), prng->Rand<uint32_t>())); |
terelius | d66daa2 | 2015-11-06 09:00:18 -0800 | [diff] [blame] | 169 | sender_report.WithPacketCount(prng->Rand<uint32_t>()); |
| 170 | sender_report.WithReportBlock(report_block); |
| 171 | |
| 172 | return sender_report.Build(); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void GenerateVideoReceiveConfig(uint32_t extensions_bitvector, |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 176 | VideoReceiveStream::Config* config, |
terelius | 84e78f9 | 2015-12-10 01:50:55 -0800 | [diff] [blame] | 177 | Random* prng) { |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 178 | // Create a map from a payload type to an encoder name. |
| 179 | VideoReceiveStream::Decoder decoder; |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 180 | decoder.payload_type = prng->Rand(0, 127); |
| 181 | decoder.payload_name = (prng->Rand<bool>() ? "VP8" : "H264"); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 182 | config->decoders.push_back(decoder); |
| 183 | // Add SSRCs for the stream. |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 184 | config->rtp.remote_ssrc = prng->Rand<uint32_t>(); |
| 185 | config->rtp.local_ssrc = prng->Rand<uint32_t>(); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 186 | // Add extensions and settings for RTCP. |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 187 | config->rtp.rtcp_mode = |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 188 | prng->Rand<bool>() ? RtcpMode::kCompound : RtcpMode::kReducedSize; |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 189 | config->rtp.remb = prng->Rand<bool>(); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 190 | // Add a map from a payload type to a new ssrc and a new payload type for RTX. |
| 191 | VideoReceiveStream::Config::Rtp::Rtx rtx_pair; |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 192 | rtx_pair.ssrc = prng->Rand<uint32_t>(); |
| 193 | rtx_pair.payload_type = prng->Rand(0, 127); |
| 194 | config->rtp.rtx.insert(std::make_pair(prng->Rand(0, 127), rtx_pair)); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 195 | // Add header extensions. |
| 196 | for (unsigned i = 0; i < kNumExtensions; i++) { |
| 197 | if (extensions_bitvector & (1u << i)) { |
| 198 | config->rtp.extensions.push_back( |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 199 | RtpExtension(kExtensionNames[i], prng->Rand<int>())); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 202 | } |
| 203 | |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 204 | void GenerateVideoSendConfig(uint32_t extensions_bitvector, |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 205 | VideoSendStream::Config* config, |
terelius | 84e78f9 | 2015-12-10 01:50:55 -0800 | [diff] [blame] | 206 | Random* prng) { |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 207 | // Create a map from a payload type to an encoder name. |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 208 | config->encoder_settings.payload_type = prng->Rand(0, 127); |
| 209 | config->encoder_settings.payload_name = (prng->Rand<bool>() ? "VP8" : "H264"); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 210 | // Add SSRCs for the stream. |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 211 | config->rtp.ssrcs.push_back(prng->Rand<uint32_t>()); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 212 | // Add a map from a payload type to new ssrcs and a new payload type for RTX. |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 213 | config->rtp.rtx.ssrcs.push_back(prng->Rand<uint32_t>()); |
| 214 | config->rtp.rtx.payload_type = prng->Rand(0, 127); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 215 | // Add header extensions. |
| 216 | for (unsigned i = 0; i < kNumExtensions; i++) { |
| 217 | if (extensions_bitvector & (1u << i)) { |
| 218 | config->rtp.extensions.push_back( |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 219 | RtpExtension(kExtensionNames[i], prng->Rand<int>())); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 222 | } |
| 223 | |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 224 | // Test for the RtcEventLog class. Dumps some RTP packets and other events |
| 225 | // to disk, then reads them back to see if they match. |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 226 | void LogSessionAndReadBack(size_t rtp_count, |
| 227 | size_t rtcp_count, |
Ivo Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 228 | size_t playout_count, |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 229 | size_t bwe_loss_count, |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 230 | uint32_t extensions_bitvector, |
| 231 | uint32_t csrcs_count, |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 232 | unsigned int random_seed) { |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 233 | ASSERT_LE(rtcp_count, rtp_count); |
Ivo Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 234 | ASSERT_LE(playout_count, rtp_count); |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 235 | ASSERT_LE(bwe_loss_count, rtp_count); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 236 | std::vector<rtc::Buffer> rtp_packets; |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 237 | std::vector<rtc::Buffer> rtcp_packets; |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 238 | std::vector<size_t> rtp_header_sizes; |
Ivo Creusen | ae856f2 | 2015-09-17 16:30:16 +0200 | [diff] [blame] | 239 | std::vector<uint32_t> playout_ssrcs; |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 240 | std::vector<std::pair<int32_t, uint8_t> > bwe_loss_updates; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 241 | |
solenberg | 4fbae2b | 2015-08-28 04:07:10 -0700 | [diff] [blame] | 242 | VideoReceiveStream::Config receiver_config(nullptr); |
| 243 | VideoSendStream::Config sender_config(nullptr); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 244 | |
terelius | 84e78f9 | 2015-12-10 01:50:55 -0800 | [diff] [blame] | 245 | Random prng(random_seed); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 246 | |
| 247 | // Create rtp_count RTP packets containing random data. |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 248 | for (size_t i = 0; i < rtp_count; i++) { |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 249 | size_t packet_size = prng.Rand(1000, 1100); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 250 | rtp_packets.push_back(rtc::Buffer(packet_size)); |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 251 | size_t header_size = |
| 252 | GenerateRtpPacket(extensions_bitvector, csrcs_count, |
| 253 | rtp_packets[i].data(), packet_size, &prng); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 254 | rtp_header_sizes.push_back(header_size); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 255 | } |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 256 | // Create rtcp_count RTCP packets containing random data. |
| 257 | for (size_t i = 0; i < rtcp_count; i++) { |
terelius | d66daa2 | 2015-11-06 09:00:18 -0800 | [diff] [blame] | 258 | rtcp_packets.push_back(GenerateRtcpPacket(&prng)); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 259 | } |
Ivo Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 260 | // Create playout_count random SSRCs to use when logging AudioPlayout events. |
| 261 | for (size_t i = 0; i < playout_count; i++) { |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 262 | playout_ssrcs.push_back(prng.Rand<uint32_t>()); |
Ivo Creusen | ae856f2 | 2015-09-17 16:30:16 +0200 | [diff] [blame] | 263 | } |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 264 | // Create bwe_loss_count random bitrate updates for BwePacketLoss. |
| 265 | for (size_t i = 0; i < bwe_loss_count; i++) { |
terelius | d66daa2 | 2015-11-06 09:00:18 -0800 | [diff] [blame] | 266 | bwe_loss_updates.push_back( |
| 267 | std::make_pair(prng.Rand<int32_t>(), prng.Rand<uint8_t>())); |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 268 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 269 | // Create configurations for the video streams. |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 270 | GenerateVideoReceiveConfig(extensions_bitvector, &receiver_config, &prng); |
| 271 | GenerateVideoSendConfig(extensions_bitvector, &sender_config, &prng); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 272 | const int config_count = 2; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 273 | |
| 274 | // Find the name of the current test, in order to use it as a temporary |
| 275 | // filename. |
| 276 | auto test_info = ::testing::UnitTest::GetInstance()->current_test_info(); |
| 277 | const std::string temp_filename = |
| 278 | test::OutputPath() + test_info->test_case_name() + test_info->name(); |
| 279 | |
| 280 | // When log_dumper goes out of scope, it causes the log file to be flushed |
| 281 | // to disk. |
| 282 | { |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 283 | SimulatedClock fake_clock(prng.Rand<uint32_t>()); |
| 284 | std::unique_ptr<RtcEventLog> log_dumper(RtcEventLog::Create(&fake_clock)); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 285 | log_dumper->LogVideoReceiveStreamConfig(receiver_config); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 286 | fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 287 | log_dumper->LogVideoSendStreamConfig(sender_config); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 288 | fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 289 | size_t rtcp_index = 1; |
| 290 | size_t playout_index = 1; |
| 291 | size_t bwe_loss_index = 1; |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 292 | for (size_t i = 1; i <= rtp_count; i++) { |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 293 | log_dumper->LogRtpHeader( |
terelius | 429c345 | 2016-01-21 05:42:04 -0800 | [diff] [blame] | 294 | (i % 2 == 0) ? kIncomingPacket : kOutgoingPacket, |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 295 | (i % 3 == 0) ? MediaType::AUDIO : MediaType::VIDEO, |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 296 | rtp_packets[i - 1].data(), rtp_packets[i - 1].size()); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 297 | fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 298 | if (i * rtcp_count >= rtcp_index * rtp_count) { |
| 299 | log_dumper->LogRtcpPacket( |
terelius | 429c345 | 2016-01-21 05:42:04 -0800 | [diff] [blame] | 300 | (rtcp_index % 2 == 0) ? kIncomingPacket : kOutgoingPacket, |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 301 | rtcp_index % 3 == 0 ? MediaType::AUDIO : MediaType::VIDEO, |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 302 | rtcp_packets[rtcp_index - 1].data(), |
| 303 | rtcp_packets[rtcp_index - 1].size()); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 304 | rtcp_index++; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 305 | fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 306 | } |
Ivo Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 307 | if (i * playout_count >= playout_index * rtp_count) { |
| 308 | log_dumper->LogAudioPlayout(playout_ssrcs[playout_index - 1]); |
| 309 | playout_index++; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 310 | fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 311 | } |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 312 | if (i * bwe_loss_count >= bwe_loss_index * rtp_count) { |
| 313 | log_dumper->LogBwePacketLossEvent( |
| 314 | bwe_loss_updates[bwe_loss_index - 1].first, |
| 315 | bwe_loss_updates[bwe_loss_index - 1].second, i); |
| 316 | bwe_loss_index++; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 317 | fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 318 | } |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 319 | if (i == rtp_count / 2) { |
| 320 | log_dumper->StartLogging(temp_filename, 10000000); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 321 | fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 322 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 323 | } |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 324 | log_dumper->StopLogging(); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 327 | // Read the generated file from disk. |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 328 | ParsedRtcEventLog parsed_log; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 329 | |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 330 | ASSERT_TRUE(parsed_log.ParseFile(temp_filename)); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 331 | |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 332 | // Verify that what we read back from the event log is the same as |
| 333 | // what we wrote down. For RTCP we log the full packets, but for |
| 334 | // RTP we should only log the header. |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 335 | const size_t event_count = config_count + playout_count + bwe_loss_count + |
| 336 | rtcp_count + rtp_count + 2; |
| 337 | EXPECT_GE(1000u, event_count); // The events must fit in the message queue. |
| 338 | EXPECT_EQ(event_count, parsed_log.GetNumberOfEvents()); |
| 339 | if (event_count != parsed_log.GetNumberOfEvents()) { |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 340 | // Print the expected and actual event types for easier debugging. |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 341 | PrintActualEvents(parsed_log); |
| 342 | PrintExpectedEvents(rtp_count, rtcp_count, playout_count, bwe_loss_count); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 343 | } |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 344 | RtcEventLogTestHelper::VerifyLogStartEvent(parsed_log, 0); |
| 345 | RtcEventLogTestHelper::VerifyReceiveStreamConfig(parsed_log, 1, |
| 346 | receiver_config); |
| 347 | RtcEventLogTestHelper::VerifySendStreamConfig(parsed_log, 2, sender_config); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 348 | size_t event_index = config_count + 1; |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 349 | size_t rtcp_index = 1; |
| 350 | size_t playout_index = 1; |
| 351 | size_t bwe_loss_index = 1; |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 352 | for (size_t i = 1; i <= rtp_count; i++) { |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 353 | RtcEventLogTestHelper::VerifyRtpEvent( |
| 354 | parsed_log, event_index, |
| 355 | (i % 2 == 0) ? kIncomingPacket : kOutgoingPacket, |
| 356 | (i % 3 == 0) ? MediaType::AUDIO : MediaType::VIDEO, |
| 357 | rtp_packets[i - 1].data(), rtp_header_sizes[i - 1], |
| 358 | rtp_packets[i - 1].size()); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 359 | event_index++; |
| 360 | if (i * rtcp_count >= rtcp_index * rtp_count) { |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 361 | RtcEventLogTestHelper::VerifyRtcpEvent( |
| 362 | parsed_log, event_index, |
| 363 | rtcp_index % 2 == 0 ? kIncomingPacket : kOutgoingPacket, |
| 364 | rtcp_index % 3 == 0 ? MediaType::AUDIO : MediaType::VIDEO, |
| 365 | rtcp_packets[rtcp_index - 1].data(), |
| 366 | rtcp_packets[rtcp_index - 1].size()); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 367 | event_index++; |
| 368 | rtcp_index++; |
| 369 | } |
Ivo Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 370 | if (i * playout_count >= playout_index * rtp_count) { |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 371 | RtcEventLogTestHelper::VerifyPlayoutEvent( |
| 372 | parsed_log, event_index, playout_ssrcs[playout_index - 1]); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 373 | event_index++; |
Ivo Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 374 | playout_index++; |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 375 | } |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 376 | if (i * bwe_loss_count >= bwe_loss_index * rtp_count) { |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 377 | RtcEventLogTestHelper::VerifyBweLossEvent( |
| 378 | parsed_log, event_index, bwe_loss_updates[bwe_loss_index - 1].first, |
| 379 | bwe_loss_updates[bwe_loss_index - 1].second, i); |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 380 | event_index++; |
| 381 | bwe_loss_index++; |
| 382 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 383 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 384 | |
| 385 | // Clean up temporary file - can be pretty slow. |
| 386 | remove(temp_filename.c_str()); |
| 387 | } |
| 388 | |
| 389 | TEST(RtcEventLogTest, LogSessionAndReadBack) { |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 390 | // Log 5 RTP, 2 RTCP, 0 playout events and 0 BWE events |
| 391 | // with no header extensions or CSRCS. |
| 392 | LogSessionAndReadBack(5, 2, 0, 0, 0, 0, 321); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 393 | |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 394 | // Enable AbsSendTime and TransportSequenceNumbers. |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 395 | uint32_t extensions = 0; |
| 396 | for (uint32_t i = 0; i < kNumExtensions; i++) { |
| 397 | if (kExtensionTypes[i] == RTPExtensionType::kRtpExtensionAbsoluteSendTime || |
| 398 | kExtensionTypes[i] == |
| 399 | RTPExtensionType::kRtpExtensionTransportSequenceNumber) { |
| 400 | extensions |= 1u << i; |
| 401 | } |
| 402 | } |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 403 | LogSessionAndReadBack(8, 2, 0, 0, extensions, 0, 3141592653u); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 404 | |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 405 | extensions = (1u << kNumExtensions) - 1; // Enable all header extensions. |
| 406 | LogSessionAndReadBack(9, 2, 3, 2, extensions, 2, 2718281828u); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 407 | |
| 408 | // Try all combinations of header extensions and up to 2 CSRCS. |
| 409 | for (extensions = 0; extensions < (1u << kNumExtensions); extensions++) { |
| 410 | for (uint32_t csrcs_count = 0; csrcs_count < 3; csrcs_count++) { |
| 411 | LogSessionAndReadBack(5 + extensions, // Number of RTP packets. |
| 412 | 2 + csrcs_count, // Number of RTCP packets. |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 413 | 3 + csrcs_count, // Number of playout events. |
| 414 | 1 + csrcs_count, // Number of BWE loss events. |
| 415 | extensions, // Bit vector choosing extensions. |
| 416 | csrcs_count, // Number of contributing sources. |
terelius | 84e78f9 | 2015-12-10 01:50:55 -0800 | [diff] [blame] | 417 | extensions * 3 + csrcs_count + 1); // Random seed. |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 420 | } |
| 421 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 422 | TEST(RtcEventLogTest, LogEventAndReadBack) { |
| 423 | Random prng(987654321); |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 424 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 425 | // Create one RTP and one RTCP packet containing random data. |
terelius | 56b1128 | 2015-11-06 05:13:55 -0800 | [diff] [blame] | 426 | size_t packet_size = prng.Rand(1000, 1100); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 427 | rtc::Buffer rtp_packet(packet_size); |
| 428 | size_t header_size = |
| 429 | GenerateRtpPacket(0, 0, rtp_packet.data(), packet_size, &prng); |
| 430 | rtc::Buffer rtcp_packet = GenerateRtcpPacket(&prng); |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 431 | |
| 432 | // Find the name of the current test, in order to use it as a temporary |
| 433 | // filename. |
| 434 | auto test_info = ::testing::UnitTest::GetInstance()->current_test_info(); |
| 435 | const std::string temp_filename = |
| 436 | test::OutputPath() + test_info->test_case_name() + test_info->name(); |
| 437 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 438 | // Add RTP, start logging, add RTCP and then stop logging |
| 439 | SimulatedClock fake_clock(prng.Rand<uint32_t>()); |
| 440 | std::unique_ptr<RtcEventLog> log_dumper(RtcEventLog::Create(&fake_clock)); |
| 441 | |
| 442 | log_dumper->LogRtpHeader(kIncomingPacket, MediaType::VIDEO, rtp_packet.data(), |
| 443 | rtp_packet.size()); |
| 444 | fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); |
| 445 | |
| 446 | log_dumper->StartLogging(temp_filename, 10000000); |
| 447 | fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); |
| 448 | |
| 449 | log_dumper->LogRtcpPacket(kOutgoingPacket, MediaType::VIDEO, |
| 450 | rtcp_packet.data(), rtcp_packet.size()); |
| 451 | fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); |
| 452 | |
| 453 | log_dumper->StopLogging(); |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 454 | |
| 455 | // Read the generated file from disk. |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 456 | ParsedRtcEventLog parsed_log; |
| 457 | ASSERT_TRUE(parsed_log.ParseFile(temp_filename)); |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 458 | |
| 459 | // Verify that what we read back from the event log is the same as |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 460 | // what we wrote down. |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 461 | EXPECT_EQ(4u, parsed_log.GetNumberOfEvents()); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 462 | |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 463 | RtcEventLogTestHelper::VerifyLogStartEvent(parsed_log, 0); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 464 | |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 465 | RtcEventLogTestHelper::VerifyRtpEvent(parsed_log, 1, kIncomingPacket, |
| 466 | MediaType::VIDEO, rtp_packet.data(), |
| 467 | header_size, rtp_packet.size()); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 468 | |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 469 | RtcEventLogTestHelper::VerifyRtcpEvent(parsed_log, 2, kOutgoingPacket, |
| 470 | MediaType::VIDEO, rtcp_packet.data(), |
| 471 | rtcp_packet.size()); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 472 | |
terelius | d5c1a0b | 2016-05-13 00:42:59 -0700 | [diff] [blame] | 473 | RtcEventLogTestHelper::VerifyLogEndEvent(parsed_log, 3); |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 474 | |
| 475 | // Clean up temporary file - can be pretty slow. |
| 476 | remove(temp_filename.c_str()); |
| 477 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 478 | } // namespace webrtc |
| 479 | |
| 480 | #endif // ENABLE_RTC_EVENT_LOG |