blob: 5c3e0061af69d2c7b290b1788fe44b119618e60f [file] [log] [blame]
terelius54ce6802016-07-13 06:44:41 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_tools/event_log_visualizer/analyzer.h"
terelius54ce6802016-07-13 06:44:41 -070012
13#include <algorithm>
Oleh Prypin6581f212017-11-16 00:17:05 +010014#include <cmath>
terelius54ce6802016-07-13 06:44:41 -070015#include <limits>
16#include <map>
terelius54ce6802016-07-13 06:44:41 -070017#include <string>
18#include <utility>
19
Karl Wiberg918f50c2018-07-05 11:40:33 +020020#include "absl/memory/memory.h"
Bjorn Terelius6c4b1b72019-01-11 13:01:29 +010021#include "absl/strings/string_view.h"
Sebastian Jansson95edb032019-01-17 16:24:12 +010022#include "api/transport/field_trial_based_config.h"
Sebastian Jansson5c94f552018-10-15 18:46:51 +020023#include "api/transport/goog_cc_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "call/audio_receive_stream.h"
25#include "call/audio_send_stream.h"
26#include "call/call.h"
27#include "call/video_receive_stream.h"
28#include "call/video_send_stream.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020029#include "common_types.h" // NOLINT(build/include)
Elad Alon99a81b62017-09-21 10:25:29 +020030#include "logging/rtc_event_log/rtc_stream_config.h"
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +020031#include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_coding/neteq/tools/audio_sink.h"
33#include "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
34#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
35#include "modules/audio_coding/neteq/tools/neteq_replacement_input.h"
36#include "modules/audio_coding/neteq/tools/neteq_test.h"
37#include "modules/audio_coding/neteq/tools/resample_input_audio_file.h"
Sebastian Jansson172fd852018-05-24 14:17:06 +020038#include "modules/congestion_controller/goog_cc/acknowledged_bitrate_estimator.h"
39#include "modules/congestion_controller/goog_cc/bitrate_estimator.h"
Sebastian Jansson04b18cb2018-07-02 09:25:25 +020040#include "modules/congestion_controller/goog_cc/delay_based_bwe.h"
Bjorn Terelius28db2662017-10-04 14:22:43 +020041#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
Sebastian Jansson5c94f552018-10-15 18:46:51 +020042#include "modules/congestion_controller/rtp/transport_feedback_adapter.h"
Niels Möllerfd6c0912017-10-31 10:19:10 +010043#include "modules/pacing/packet_router.h"
Sebastian Jansson5c94f552018-10-15 18:46:51 +020044#include "modules/remote_bitrate_estimator/include/bwe_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "modules/rtp_rtcp/include/rtp_rtcp.h"
46#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +020047#include "modules/rtp_rtcp/source/rtcp_packet.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
49#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
50#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
51#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
52#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
53#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
54#include "modules/rtp_rtcp/source/rtp_utility.h"
55#include "rtc_base/checks.h"
56#include "rtc_base/format_macros.h"
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +020057#include "rtc_base/function_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020058#include "rtc_base/logging.h"
Bjorn Terelius0295a962017-10-25 17:42:41 +020059#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "rtc_base/rate_statistics.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020061#include "rtc_base/strings/string_builder.h"
terelius54ce6802016-07-13 06:44:41 -070062
Bjorn Terelius6984ad22017-10-24 12:19:45 +020063#ifndef BWE_TEST_LOGGING_COMPILE_TIME_ENABLE
64#define BWE_TEST_LOGGING_COMPILE_TIME_ENABLE 0
65#endif // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE
66
tereliusdc35dcd2016-08-01 12:03:27 -070067namespace webrtc {
tereliusdc35dcd2016-08-01 12:03:27 -070068
terelius54ce6802016-07-13 06:44:41 -070069namespace {
70
Qingsi Wang8eca1ff2018-02-02 11:49:44 -080071const int kNumMicrosecsPerSec = 1000000;
72
elad.alonec304f92017-03-08 05:03:53 -080073void SortPacketFeedbackVector(std::vector<PacketFeedback>* vec) {
74 auto pred = [](const PacketFeedback& packet_feedback) {
75 return packet_feedback.arrival_time_ms == PacketFeedback::kNotReceived;
76 };
77 vec->erase(std::remove_if(vec->begin(), vec->end(), pred), vec->end());
78 std::sort(vec->begin(), vec->end(), PacketFeedbackComparator());
79}
80
terelius54ce6802016-07-13 06:44:41 -070081std::string SsrcToString(uint32_t ssrc) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020082 rtc::StringBuilder ss;
terelius54ce6802016-07-13 06:44:41 -070083 ss << "SSRC " << ssrc;
Jonas Olsson84df1c72018-09-14 16:59:32 +020084 return ss.Release();
terelius54ce6802016-07-13 06:44:41 -070085}
86
87// Checks whether an SSRC is contained in the list of desired SSRCs.
88// Note that an empty SSRC list matches every SSRC.
89bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
90 if (desired_ssrc.size() == 0)
91 return true;
92 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
93 desired_ssrc.end();
94}
95
96double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
97 // The timestamp is a fixed point representation with 6 bits for seconds
98 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
Qingsi Wang8eca1ff2018-02-02 11:49:44 -080099 // time in seconds and then multiply by kNumMicrosecsPerSec to convert to
100 // microseconds.
terelius54ce6802016-07-13 06:44:41 -0700101 static constexpr double kTimestampToMicroSec =
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800102 static_cast<double>(kNumMicrosecsPerSec) / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -0700103 return abs_send_time * kTimestampToMicroSec;
104}
105
106// Computes the difference |later| - |earlier| where |later| and |earlier|
107// are counters that wrap at |modulus|. The difference is chosen to have the
108// least absolute value. For example if |modulus| is 8, then the difference will
109// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
110// be in [-4, 4].
111int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
112 RTC_DCHECK_LE(1, modulus);
113 RTC_DCHECK_LT(later, modulus);
114 RTC_DCHECK_LT(earlier, modulus);
115 int64_t difference =
116 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
117 int64_t max_difference = modulus / 2;
118 int64_t min_difference = max_difference - modulus + 1;
119 if (difference > max_difference) {
120 difference -= modulus;
121 }
122 if (difference < min_difference) {
123 difference += modulus;
124 }
terelius6addf492016-08-23 17:34:07 -0700125 if (difference > max_difference / 2 || difference < min_difference / 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100126 RTC_LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
127 << " expected to be in the range ("
128 << min_difference / 2 << "," << max_difference / 2
129 << ") but is " << difference
130 << ". Correct unwrapping is uncertain.";
terelius6addf492016-08-23 17:34:07 -0700131 }
terelius54ce6802016-07-13 06:44:41 -0700132 return difference;
133}
134
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200135// This is much more reliable for outgoing streams than for incoming streams.
136template <typename RtpPacketContainer>
Danil Chapovalov431abd92018-06-18 12:54:17 +0200137absl::optional<uint32_t> EstimateRtpClockFrequency(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200138 const RtpPacketContainer& packets,
139 int64_t end_time_us) {
140 RTC_CHECK(packets.size() >= 2);
141 SeqNumUnwrapper<uint32_t> unwrapper;
142 uint64_t first_rtp_timestamp =
143 unwrapper.Unwrap(packets[0].rtp.header.timestamp);
144 int64_t first_log_timestamp = packets[0].log_time_us();
145 uint64_t last_rtp_timestamp = first_rtp_timestamp;
146 int64_t last_log_timestamp = first_log_timestamp;
147 for (size_t i = 1; i < packets.size(); i++) {
148 if (packets[i].log_time_us() > end_time_us)
149 break;
150 last_rtp_timestamp = unwrapper.Unwrap(packets[i].rtp.header.timestamp);
151 last_log_timestamp = packets[i].log_time_us();
152 }
153 if (last_log_timestamp - first_log_timestamp < kNumMicrosecsPerSec) {
154 RTC_LOG(LS_WARNING)
155 << "Failed to estimate RTP clock frequency: Stream too short. ("
156 << packets.size() << " packets, "
157 << last_log_timestamp - first_log_timestamp << " us)";
Danil Chapovalov431abd92018-06-18 12:54:17 +0200158 return absl::nullopt;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200159 }
160 double duration =
161 static_cast<double>(last_log_timestamp - first_log_timestamp) /
162 kNumMicrosecsPerSec;
163 double estimated_frequency =
164 (last_rtp_timestamp - first_rtp_timestamp) / duration;
165 for (uint32_t f : {8000, 16000, 32000, 48000, 90000}) {
166 if (std::fabs(estimated_frequency - f) < 0.05 * f) {
167 return f;
168 }
169 }
170 RTC_LOG(LS_WARNING) << "Failed to estimate RTP clock frequency: Estimate "
171 << estimated_frequency
172 << "not close to any stardard RTP frequency.";
Danil Chapovalov431abd92018-06-18 12:54:17 +0200173 return absl::nullopt;
ivocaac9d6f2016-09-22 07:01:47 -0700174}
175
tereliusdc35dcd2016-08-01 12:03:27 -0700176constexpr float kLeftMargin = 0.01f;
177constexpr float kRightMargin = 0.02f;
178constexpr float kBottomMargin = 0.02f;
179constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700180
Danil Chapovalov431abd92018-06-18 12:54:17 +0200181absl::optional<double> NetworkDelayDiff_AbsSendTime(
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100182 const LoggedRtpPacketIncoming& old_packet,
183 const LoggedRtpPacketIncoming& new_packet) {
184 if (old_packet.rtp.header.extension.hasAbsoluteSendTime &&
185 new_packet.rtp.header.extension.hasAbsoluteSendTime) {
terelius53dc23c2017-03-13 05:24:05 -0700186 int64_t send_time_diff = WrappingDifference(
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100187 new_packet.rtp.header.extension.absoluteSendTime,
188 old_packet.rtp.header.extension.absoluteSendTime, 1ul << 24);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200189 int64_t recv_time_diff =
190 new_packet.log_time_us() - old_packet.log_time_us();
terelius53dc23c2017-03-13 05:24:05 -0700191 double delay_change_us =
192 recv_time_diff - AbsSendTimeToMicroseconds(send_time_diff);
Oskar Sundbom3928dbc2017-11-16 10:53:09 +0100193 return delay_change_us / 1000;
terelius53dc23c2017-03-13 05:24:05 -0700194 } else {
Danil Chapovalov431abd92018-06-18 12:54:17 +0200195 return absl::nullopt;
terelius6addf492016-08-23 17:34:07 -0700196 }
197}
198
Danil Chapovalov431abd92018-06-18 12:54:17 +0200199absl::optional<double> NetworkDelayDiff_CaptureTime(
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100200 const LoggedRtpPacketIncoming& old_packet,
201 const LoggedRtpPacketIncoming& new_packet,
202 const double sample_rate) {
203 int64_t send_time_diff =
204 WrappingDifference(new_packet.rtp.header.timestamp,
205 old_packet.rtp.header.timestamp, 1ull << 32);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200206 int64_t recv_time_diff = new_packet.log_time_us() - old_packet.log_time_us();
terelius53dc23c2017-03-13 05:24:05 -0700207
terelius53dc23c2017-03-13 05:24:05 -0700208 double delay_change =
209 static_cast<double>(recv_time_diff) / 1000 -
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100210 static_cast<double>(send_time_diff) / sample_rate * 1000;
terelius53dc23c2017-03-13 05:24:05 -0700211 if (delay_change < -10000 || 10000 < delay_change) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100212 RTC_LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100213 RTC_LOG(LS_WARNING) << "Old capture time "
214 << old_packet.rtp.header.timestamp << ", received time "
215 << old_packet.log_time_us();
216 RTC_LOG(LS_WARNING) << "New capture time "
217 << new_packet.rtp.header.timestamp << ", received time "
218 << new_packet.log_time_us();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100219 RTC_LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800220 << static_cast<double>(recv_time_diff) /
221 kNumMicrosecsPerSec
222 << "s";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100223 RTC_LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100224 << static_cast<double>(send_time_diff) / sample_rate
Mirko Bonadei675513b2017-11-09 11:09:25 +0100225 << "s";
terelius53dc23c2017-03-13 05:24:05 -0700226 }
Oskar Sundbom3928dbc2017-11-16 10:53:09 +0100227 return delay_change;
terelius53dc23c2017-03-13 05:24:05 -0700228}
229
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200230// For each element in data_view, use |f()| to extract a y-coordinate and
terelius53dc23c2017-03-13 05:24:05 -0700231// store the result in a TimeSeries.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200232template <typename DataType, typename IterableType>
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200233void ProcessPoints(rtc::FunctionView<float(const DataType&)> fx,
Danil Chapovalov431abd92018-06-18 12:54:17 +0200234 rtc::FunctionView<absl::optional<float>(const DataType&)> fy,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200235 const IterableType& data_view,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200236 TimeSeries* result) {
237 for (size_t i = 0; i < data_view.size(); i++) {
238 const DataType& elem = data_view[i];
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200239 float x = fx(elem);
Danil Chapovalov431abd92018-06-18 12:54:17 +0200240 absl::optional<float> y = fy(elem);
terelius53dc23c2017-03-13 05:24:05 -0700241 if (y)
242 result->points.emplace_back(x, *y);
243 }
244}
245
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200246// For each pair of adjacent elements in |data|, use |f()| to extract a
terelius6addf492016-08-23 17:34:07 -0700247// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
248// will be the time of the second element in the pair.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200249template <typename DataType, typename ResultType, typename IterableType>
terelius53dc23c2017-03-13 05:24:05 -0700250void ProcessPairs(
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200251 rtc::FunctionView<float(const DataType&)> fx,
Danil Chapovalov431abd92018-06-18 12:54:17 +0200252 rtc::FunctionView<absl::optional<ResultType>(const DataType&,
253 const DataType&)> fy,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200254 const IterableType& data,
terelius53dc23c2017-03-13 05:24:05 -0700255 TimeSeries* result) {
tereliusccbbf8d2016-08-10 07:34:28 -0700256 for (size_t i = 1; i < data.size(); i++) {
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200257 float x = fx(data[i]);
Danil Chapovalov431abd92018-06-18 12:54:17 +0200258 absl::optional<ResultType> y = fy(data[i - 1], data[i]);
terelius53dc23c2017-03-13 05:24:05 -0700259 if (y)
260 result->points.emplace_back(x, static_cast<float>(*y));
261 }
262}
263
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200264// For each pair of adjacent elements in |data|, use |f()| to extract a
terelius53dc23c2017-03-13 05:24:05 -0700265// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
266// will be the time of the second element in the pair.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200267template <typename DataType, typename ResultType, typename IterableType>
terelius53dc23c2017-03-13 05:24:05 -0700268void AccumulatePairs(
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200269 rtc::FunctionView<float(const DataType&)> fx,
Danil Chapovalov431abd92018-06-18 12:54:17 +0200270 rtc::FunctionView<absl::optional<ResultType>(const DataType&,
271 const DataType&)> fy,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200272 const IterableType& data,
terelius53dc23c2017-03-13 05:24:05 -0700273 TimeSeries* result) {
274 ResultType sum = 0;
275 for (size_t i = 1; i < data.size(); i++) {
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200276 float x = fx(data[i]);
Danil Chapovalov431abd92018-06-18 12:54:17 +0200277 absl::optional<ResultType> y = fy(data[i - 1], data[i]);
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100278 if (y) {
terelius53dc23c2017-03-13 05:24:05 -0700279 sum += *y;
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100280 result->points.emplace_back(x, static_cast<float>(sum));
281 }
tereliusccbbf8d2016-08-10 07:34:28 -0700282 }
283}
284
terelius6addf492016-08-23 17:34:07 -0700285// Calculates a moving average of |data| and stores the result in a TimeSeries.
286// A data point is generated every |step| microseconds from |begin_time|
287// to |end_time|. The value of each data point is the average of the data
288// during the preceeding |window_duration_us| microseconds.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200289template <typename DataType, typename ResultType, typename IterableType>
terelius53dc23c2017-03-13 05:24:05 -0700290void MovingAverage(
Danil Chapovalov431abd92018-06-18 12:54:17 +0200291 rtc::FunctionView<absl::optional<ResultType>(const DataType&)> fy,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200292 const IterableType& data_view,
Bjorn Terelius068fc352019-02-13 22:38:25 +0100293 AnalyzerConfig config,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200294 TimeSeries* result) {
terelius6addf492016-08-23 17:34:07 -0700295 size_t window_index_begin = 0;
296 size_t window_index_end = 0;
terelius53dc23c2017-03-13 05:24:05 -0700297 ResultType sum_in_window = 0;
terelius6addf492016-08-23 17:34:07 -0700298
Bjorn Terelius068fc352019-02-13 22:38:25 +0100299 for (int64_t t = config.begin_time_; t < config.end_time_ + config.step_;
300 t += config.step_) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200301 while (window_index_end < data_view.size() &&
302 data_view[window_index_end].log_time_us() < t) {
Danil Chapovalov431abd92018-06-18 12:54:17 +0200303 absl::optional<ResultType> value = fy(data_view[window_index_end]);
terelius53dc23c2017-03-13 05:24:05 -0700304 if (value)
305 sum_in_window += *value;
terelius6addf492016-08-23 17:34:07 -0700306 ++window_index_end;
307 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200308 while (window_index_begin < data_view.size() &&
309 data_view[window_index_begin].log_time_us() <
Bjorn Terelius068fc352019-02-13 22:38:25 +0100310 t - config.window_duration_) {
Danil Chapovalov431abd92018-06-18 12:54:17 +0200311 absl::optional<ResultType> value = fy(data_view[window_index_begin]);
terelius53dc23c2017-03-13 05:24:05 -0700312 if (value)
313 sum_in_window -= *value;
terelius6addf492016-08-23 17:34:07 -0700314 ++window_index_begin;
315 }
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800316 float window_duration_s =
Bjorn Terelius068fc352019-02-13 22:38:25 +0100317 static_cast<float>(config.window_duration_) / kNumMicrosecsPerSec;
318 float x = config.GetCallTimeSec(t);
terelius53dc23c2017-03-13 05:24:05 -0700319 float y = sum_in_window / window_duration_s;
terelius6addf492016-08-23 17:34:07 -0700320 result->points.emplace_back(x, y);
321 }
322}
323
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800324const char kUnknownEnumValue[] = "unknown";
325
326const char kIceCandidateTypeLocal[] = "local";
327const char kIceCandidateTypeStun[] = "stun";
328const char kIceCandidateTypePrflx[] = "prflx";
329const char kIceCandidateTypeRelay[] = "relay";
330
331const char kProtocolUdp[] = "udp";
332const char kProtocolTcp[] = "tcp";
333const char kProtocolSsltcp[] = "ssltcp";
334const char kProtocolTls[] = "tls";
335
336const char kAddressFamilyIpv4[] = "ipv4";
337const char kAddressFamilyIpv6[] = "ipv6";
338
339const char kNetworkTypeEthernet[] = "ethernet";
340const char kNetworkTypeLoopback[] = "loopback";
341const char kNetworkTypeWifi[] = "wifi";
342const char kNetworkTypeVpn[] = "vpn";
343const char kNetworkTypeCellular[] = "cellular";
344
345std::string GetIceCandidateTypeAsString(webrtc::IceCandidateType type) {
346 switch (type) {
347 case webrtc::IceCandidateType::kLocal:
348 return kIceCandidateTypeLocal;
349 case webrtc::IceCandidateType::kStun:
350 return kIceCandidateTypeStun;
351 case webrtc::IceCandidateType::kPrflx:
352 return kIceCandidateTypePrflx;
353 case webrtc::IceCandidateType::kRelay:
354 return kIceCandidateTypeRelay;
355 default:
356 return kUnknownEnumValue;
357 }
358}
359
360std::string GetProtocolAsString(webrtc::IceCandidatePairProtocol protocol) {
361 switch (protocol) {
362 case webrtc::IceCandidatePairProtocol::kUdp:
363 return kProtocolUdp;
364 case webrtc::IceCandidatePairProtocol::kTcp:
365 return kProtocolTcp;
366 case webrtc::IceCandidatePairProtocol::kSsltcp:
367 return kProtocolSsltcp;
368 case webrtc::IceCandidatePairProtocol::kTls:
369 return kProtocolTls;
370 default:
371 return kUnknownEnumValue;
372 }
373}
374
375std::string GetAddressFamilyAsString(
376 webrtc::IceCandidatePairAddressFamily family) {
377 switch (family) {
378 case webrtc::IceCandidatePairAddressFamily::kIpv4:
379 return kAddressFamilyIpv4;
380 case webrtc::IceCandidatePairAddressFamily::kIpv6:
381 return kAddressFamilyIpv6;
382 default:
383 return kUnknownEnumValue;
384 }
385}
386
387std::string GetNetworkTypeAsString(webrtc::IceCandidateNetworkType type) {
388 switch (type) {
389 case webrtc::IceCandidateNetworkType::kEthernet:
390 return kNetworkTypeEthernet;
391 case webrtc::IceCandidateNetworkType::kLoopback:
392 return kNetworkTypeLoopback;
393 case webrtc::IceCandidateNetworkType::kWifi:
394 return kNetworkTypeWifi;
395 case webrtc::IceCandidateNetworkType::kVpn:
396 return kNetworkTypeVpn;
397 case webrtc::IceCandidateNetworkType::kCellular:
398 return kNetworkTypeCellular;
399 default:
400 return kUnknownEnumValue;
401 }
402}
403
404std::string GetCandidatePairLogDescriptionAsString(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200405 const LoggedIceCandidatePairConfig& config) {
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800406 // Example: stun:wifi->relay(tcp):cellular@udp:ipv4
407 // represents a pair of a local server-reflexive candidate on a WiFi network
408 // and a remote relay candidate using TCP as the relay protocol on a cell
409 // network, when the candidate pair communicates over UDP using IPv4.
Jonas Olsson366a50c2018-09-06 13:41:30 +0200410 rtc::StringBuilder ss;
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800411 std::string local_candidate_type =
412 GetIceCandidateTypeAsString(config.local_candidate_type);
413 std::string remote_candidate_type =
414 GetIceCandidateTypeAsString(config.remote_candidate_type);
415 if (config.local_candidate_type == webrtc::IceCandidateType::kRelay) {
416 local_candidate_type +=
417 "(" + GetProtocolAsString(config.local_relay_protocol) + ")";
418 }
419 ss << local_candidate_type << ":"
420 << GetNetworkTypeAsString(config.local_network_type) << ":"
421 << GetAddressFamilyAsString(config.local_address_family) << "->"
422 << remote_candidate_type << ":"
423 << GetAddressFamilyAsString(config.remote_address_family) << "@"
424 << GetProtocolAsString(config.candidate_pair_protocol);
Jonas Olsson84df1c72018-09-14 16:59:32 +0200425 return ss.Release();
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800426}
427
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200428std::string GetDirectionAsString(PacketDirection direction) {
429 if (direction == kIncomingPacket) {
430 return "Incoming";
431 } else {
432 return "Outgoing";
433 }
434}
435
436std::string GetDirectionAsShortString(PacketDirection direction) {
437 if (direction == kIncomingPacket) {
438 return "In";
439 } else {
440 return "Out";
441 }
442}
443
terelius54ce6802016-07-13 06:44:41 -0700444} // namespace
445
Sebastian Janssonb290a6d2019-01-03 14:46:23 +0100446EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log,
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200447 bool normalize_time)
Bjorn Terelius068fc352019-02-13 22:38:25 +0100448 : parsed_log_(log) {
449 config_.window_duration_ = 250000;
450 config_.step_ = 10000;
451 config_.normalize_time_ = normalize_time;
452 config_.begin_time_ = parsed_log_.first_timestamp();
453 config_.end_time_ = parsed_log_.last_timestamp();
454 if (config_.end_time_ < config_.begin_time_) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200455 RTC_LOG(LS_WARNING) << "No useful events in the log.";
Bjorn Terelius068fc352019-02-13 22:38:25 +0100456 config_.begin_time_ = config_.end_time_ = 0;
Björn Tereliusff612732018-04-25 14:23:01 +0000457 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200458
459 const auto& log_start_events = parsed_log_.start_log_events();
460 const auto& log_end_events = parsed_log_.stop_log_events();
461 auto start_iter = log_start_events.begin();
462 auto end_iter = log_end_events.begin();
463 while (start_iter != log_start_events.end()) {
464 int64_t start = start_iter->log_time_us();
465 ++start_iter;
Danil Chapovalov431abd92018-06-18 12:54:17 +0200466 absl::optional<int64_t> next_start;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200467 if (start_iter != log_start_events.end())
468 next_start.emplace(start_iter->log_time_us());
469 if (end_iter != log_end_events.end() &&
470 end_iter->log_time_us() <=
471 next_start.value_or(std::numeric_limits<int64_t>::max())) {
472 int64_t end = end_iter->log_time_us();
473 RTC_DCHECK_LE(start, end);
474 log_segments_.push_back(std::make_pair(start, end));
475 ++end_iter;
476 } else {
477 // we're missing an end event. Assume that it occurred just before the
478 // next start.
479 log_segments_.push_back(
Bjorn Terelius068fc352019-02-13 22:38:25 +0100480 std::make_pair(start, next_start.value_or(config_.end_time_)));
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200481 }
henrik.lundin3c938fc2017-06-14 06:09:58 -0700482 }
Bjorn Terelius2eb31882017-11-30 15:15:25 +0100483 RTC_LOG(LS_INFO) << "Found " << log_segments_.size()
Yves Gerey665174f2018-06-19 15:03:05 +0200484 << " (LOG_START, LOG_END) segments in log.";
terelius54ce6802016-07-13 06:44:41 -0700485}
486
Sebastian Jansson5c94f552018-10-15 18:46:51 +0200487class BitrateObserver : public RemoteBitrateObserver {
Stefan Holmer13181032016-07-29 14:48:54 +0200488 public:
489 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
490
Sebastian Jansson5c94f552018-10-15 18:46:51 +0200491 void Update(NetworkControlUpdate update) {
492 if (update.target_rate) {
493 last_bitrate_bps_ = update.target_rate->target_rate.bps();
494 bitrate_updated_ = true;
495 }
Stefan Holmer13181032016-07-29 14:48:54 +0200496 }
497
498 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
499 uint32_t bitrate) override {}
500
501 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
502 bool GetAndResetBitrateUpdated() {
503 bool bitrate_updated = bitrate_updated_;
504 bitrate_updated_ = false;
505 return bitrate_updated;
506 }
507
508 private:
509 uint32_t last_bitrate_bps_;
510 bool bitrate_updated_;
511};
512
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200513void EventLogAnalyzer::CreatePacketGraph(PacketDirection direction,
terelius54ce6802016-07-13 06:44:41 -0700514 Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200515 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
516 // Filter on SSRC.
517 if (!MatchingSsrc(stream.ssrc, desired_ssrc_)) {
terelius6addf492016-08-23 17:34:07 -0700518 continue;
terelius54ce6802016-07-13 06:44:41 -0700519 }
terelius54ce6802016-07-13 06:44:41 -0700520
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200521 TimeSeries time_series(GetStreamName(direction, stream.ssrc),
522 LineStyle::kBar);
523 auto GetPacketSize = [](const LoggedRtpPacket& packet) {
Danil Chapovalov431abd92018-06-18 12:54:17 +0200524 return absl::optional<float>(packet.total_length);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200525 };
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200526 auto ToCallTime = [this](const LoggedRtpPacket& packet) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100527 return this->config_.GetCallTimeSec(packet.log_time_us());
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200528 };
529 ProcessPoints<LoggedRtpPacket>(ToCallTime, GetPacketSize,
530 stream.packet_view, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700531 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700532 }
533
Bjorn Terelius068fc352019-02-13 22:38:25 +0100534 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
535 "Time (s)", kLeftMargin, kRightMargin);
tereliusdc35dcd2016-08-01 12:03:27 -0700536 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
537 kTopMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200538 plot->SetTitle(GetDirectionAsString(direction) + " RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700539}
540
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200541template <typename IterableType>
philipelccd74892016-09-05 02:46:25 -0700542void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
philipelccd74892016-09-05 02:46:25 -0700543 Plot* plot,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200544 const IterableType& packets,
545 const std::string& label) {
546 TimeSeries time_series(label, LineStyle::kStep);
547 for (size_t i = 0; i < packets.size(); i++) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100548 float x = config_.GetCallTimeSec(packets[i].log_time_us());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200549 time_series.points.emplace_back(x, i + 1);
philipelccd74892016-09-05 02:46:25 -0700550 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200551 plot->AppendTimeSeries(std::move(time_series));
philipelccd74892016-09-05 02:46:25 -0700552}
553
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200554void EventLogAnalyzer::CreateAccumulatedPacketsGraph(PacketDirection direction,
555 Plot* plot) {
556 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
557 if (!MatchingSsrc(stream.ssrc, desired_ssrc_))
558 continue;
559 std::string label =
560 std::string("RTP ") + GetStreamName(direction, stream.ssrc);
561 CreateAccumulatedPacketsTimeSeries(plot, stream.packet_view, label);
562 }
563 std::string label =
564 std::string("RTCP ") + "(" + GetDirectionAsShortString(direction) + ")";
565 if (direction == kIncomingPacket) {
566 CreateAccumulatedPacketsTimeSeries(
567 plot, parsed_log_.incoming_rtcp_packets(), label);
568 } else {
569 CreateAccumulatedPacketsTimeSeries(
570 plot, parsed_log_.outgoing_rtcp_packets(), label);
571 }
philipelccd74892016-09-05 02:46:25 -0700572
Bjorn Terelius068fc352019-02-13 22:38:25 +0100573 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
574 "Time (s)", kLeftMargin, kRightMargin);
philipelccd74892016-09-05 02:46:25 -0700575 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200576 plot->SetTitle(std::string("Accumulated ") + GetDirectionAsString(direction) +
577 " RTP/RTCP packets");
philipelccd74892016-09-05 02:46:25 -0700578}
579
terelius54ce6802016-07-13 06:44:41 -0700580// For each SSRC, plot the time between the consecutive playouts.
581void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200582 for (const auto& playout_stream : parsed_log_.audio_playout_events()) {
583 uint32_t ssrc = playout_stream.first;
584 if (!MatchingSsrc(ssrc, desired_ssrc_))
585 continue;
Danil Chapovalov431abd92018-06-18 12:54:17 +0200586 absl::optional<int64_t> last_playout_ms;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200587 TimeSeries time_series(SsrcToString(ssrc), LineStyle::kBar);
Minyue Li27e2b7d2018-05-07 15:20:24 +0200588 for (const auto& playout_event : playout_stream.second) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100589 float x = config_.GetCallTimeSec(playout_event.log_time_us());
Minyue Li27e2b7d2018-05-07 15:20:24 +0200590 int64_t playout_time_ms = playout_event.log_time_ms();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200591 // If there were no previous playouts, place the point on the x-axis.
Minyue Li27e2b7d2018-05-07 15:20:24 +0200592 float y = playout_time_ms - last_playout_ms.value_or(playout_time_ms);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200593 time_series.points.push_back(TimeSeriesPoint(x, y));
Minyue Li27e2b7d2018-05-07 15:20:24 +0200594 last_playout_ms.emplace(playout_time_ms);
terelius54ce6802016-07-13 06:44:41 -0700595 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200596 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700597 }
598
Bjorn Terelius068fc352019-02-13 22:38:25 +0100599 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
600 "Time (s)", kLeftMargin, kRightMargin);
tereliusdc35dcd2016-08-01 12:03:27 -0700601 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
602 kTopMargin);
603 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700604}
605
ivocaac9d6f2016-09-22 07:01:47 -0700606// For audio SSRCs, plot the audio level.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200607void EventLogAnalyzer::CreateAudioLevelGraph(PacketDirection direction,
608 Plot* plot) {
609 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
610 if (!IsAudioSsrc(direction, stream.ssrc))
611 continue;
612 TimeSeries time_series(GetStreamName(direction, stream.ssrc),
613 LineStyle::kLine);
614 for (auto& packet : stream.packet_view) {
ivocaac9d6f2016-09-22 07:01:47 -0700615 if (packet.header.extension.hasAudioLevel) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100616 float x = config_.GetCallTimeSec(packet.log_time_us());
ivocaac9d6f2016-09-22 07:01:47 -0700617 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
618 // Here we convert it to dBov.
619 float y = static_cast<float>(-packet.header.extension.audioLevel);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200620 time_series.points.emplace_back(TimeSeriesPoint(x, y));
ivocaac9d6f2016-09-22 07:01:47 -0700621 }
622 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200623 plot->AppendTimeSeries(std::move(time_series));
ivocaac9d6f2016-09-22 07:01:47 -0700624 }
625
Bjorn Terelius068fc352019-02-13 22:38:25 +0100626 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
627 "Time (s)", kLeftMargin, kRightMargin);
Yves Gerey665174f2018-06-19 15:03:05 +0200628 plot->SetYAxis(-127, 0, "Audio level (dBov)", kBottomMargin, kTopMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200629 plot->SetTitle(GetDirectionAsString(direction) + " audio level");
ivocaac9d6f2016-09-22 07:01:47 -0700630}
631
terelius54ce6802016-07-13 06:44:41 -0700632// For each SSRC, plot the time between the consecutive playouts.
633void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200634 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
635 // Filter on SSRC.
636 if (!MatchingSsrc(stream.ssrc, desired_ssrc_)) {
terelius6addf492016-08-23 17:34:07 -0700637 continue;
terelius54ce6802016-07-13 06:44:41 -0700638 }
terelius54ce6802016-07-13 06:44:41 -0700639
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200640 TimeSeries time_series(GetStreamName(kIncomingPacket, stream.ssrc),
641 LineStyle::kBar);
642 auto GetSequenceNumberDiff = [](const LoggedRtpPacketIncoming& old_packet,
643 const LoggedRtpPacketIncoming& new_packet) {
644 int64_t diff =
645 WrappingDifference(new_packet.rtp.header.sequenceNumber,
646 old_packet.rtp.header.sequenceNumber, 1ul << 16);
647 return diff;
648 };
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200649 auto ToCallTime = [this](const LoggedRtpPacketIncoming& packet) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100650 return this->config_.GetCallTimeSec(packet.log_time_us());
Stefan Holmer1d4a2272018-05-24 13:48:09 +0200651 };
652 ProcessPairs<LoggedRtpPacketIncoming, float>(
653 ToCallTime, GetSequenceNumberDiff, stream.incoming_packets,
654 &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700655 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700656 }
657
Bjorn Terelius068fc352019-02-13 22:38:25 +0100658 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
659 "Time (s)", kLeftMargin, kRightMargin);
tereliusdc35dcd2016-08-01 12:03:27 -0700660 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
661 kTopMargin);
662 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700663}
664
Stefan Holmer99f8e082016-09-09 13:37:50 +0200665void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200666 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
667 const std::vector<LoggedRtpPacketIncoming>& packets =
668 stream.incoming_packets;
669 // Filter on SSRC.
670 if (!MatchingSsrc(stream.ssrc, desired_ssrc_) || packets.size() == 0) {
Stefan Holmer99f8e082016-09-09 13:37:50 +0200671 continue;
672 }
673
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200674 TimeSeries time_series(GetStreamName(kIncomingPacket, stream.ssrc),
675 LineStyle::kLine, PointStyle::kHighlight);
676 // TODO(terelius): Should the window and step size be read from the class
677 // instead?
678 const int64_t kWindowUs = 1000000;
679 const int64_t kStep = 1000000;
Bjorn Terelius2eb31882017-11-30 15:15:25 +0100680 SeqNumUnwrapper<uint16_t> unwrapper_;
681 SeqNumUnwrapper<uint16_t> prior_unwrapper_;
terelius4c9b4af2017-01-30 08:44:51 -0800682 size_t window_index_begin = 0;
683 size_t window_index_end = 0;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200684 uint64_t highest_seq_number =
685 unwrapper_.Unwrap(packets[0].rtp.header.sequenceNumber) - 1;
686 uint64_t highest_prior_seq_number =
687 prior_unwrapper_.Unwrap(packets[0].rtp.header.sequenceNumber) - 1;
terelius4c9b4af2017-01-30 08:44:51 -0800688
Bjorn Terelius068fc352019-02-13 22:38:25 +0100689 for (int64_t t = config_.begin_time_; t < config_.end_time_ + kStep;
690 t += kStep) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200691 while (window_index_end < packets.size() &&
692 packets[window_index_end].rtp.log_time_us() < t) {
693 uint64_t sequence_number = unwrapper_.Unwrap(
694 packets[window_index_end].rtp.header.sequenceNumber);
terelius4c9b4af2017-01-30 08:44:51 -0800695 highest_seq_number = std::max(highest_seq_number, sequence_number);
696 ++window_index_end;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200697 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200698 while (window_index_begin < packets.size() &&
699 packets[window_index_begin].rtp.log_time_us() < t - kWindowUs) {
700 uint64_t sequence_number = prior_unwrapper_.Unwrap(
701 packets[window_index_begin].rtp.header.sequenceNumber);
terelius4c9b4af2017-01-30 08:44:51 -0800702 highest_prior_seq_number =
703 std::max(highest_prior_seq_number, sequence_number);
704 ++window_index_begin;
705 }
Bjorn Terelius068fc352019-02-13 22:38:25 +0100706 float x = config_.GetCallTimeSec(t);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200707 uint64_t expected_packets = highest_seq_number - highest_prior_seq_number;
terelius4c9b4af2017-01-30 08:44:51 -0800708 if (expected_packets > 0) {
709 int64_t received_packets = window_index_end - window_index_begin;
710 int64_t lost_packets = expected_packets - received_packets;
711 float y = static_cast<float>(lost_packets) / expected_packets * 100;
712 time_series.points.emplace_back(x, y);
713 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200714 }
philipel35ba9bd2017-04-19 05:58:51 -0700715 plot->AppendTimeSeries(std::move(time_series));
Stefan Holmer99f8e082016-09-09 13:37:50 +0200716 }
717
Bjorn Terelius068fc352019-02-13 22:38:25 +0100718 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
719 "Time (s)", kLeftMargin, kRightMargin);
Stefan Holmer99f8e082016-09-09 13:37:50 +0200720 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
721 kTopMargin);
722 plot->SetTitle("Estimated incoming loss rate");
723}
724
terelius2ee076d2017-08-15 02:04:02 -0700725void EventLogAnalyzer::CreateIncomingDelayGraph(Plot* plot) {
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100726 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200727 // Filter on SSRC.
728 if (!MatchingSsrc(stream.ssrc, desired_ssrc_) ||
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200729 IsRtxSsrc(kIncomingPacket, stream.ssrc)) {
terelius88e64e52016-07-19 01:51:06 -0700730 continue;
731 }
terelius54ce6802016-07-13 06:44:41 -0700732
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100733 const std::vector<LoggedRtpPacketIncoming>& packets =
734 stream.incoming_packets;
735 if (packets.size() < 100) {
736 RTC_LOG(LS_WARNING) << "Can't estimate the RTP clock frequency with "
737 << packets.size() << " packets in the stream.";
738 continue;
739 }
740 int64_t end_time_us = log_segments_.empty()
741 ? std::numeric_limits<int64_t>::max()
742 : log_segments_.front().second;
743 absl::optional<uint32_t> estimated_frequency =
744 EstimateRtpClockFrequency(packets, end_time_us);
745 if (!estimated_frequency)
746 continue;
747 const double frequency_hz = *estimated_frequency;
748 if (IsVideoSsrc(kIncomingPacket, stream.ssrc) && frequency_hz != 90000) {
749 RTC_LOG(LS_WARNING)
750 << "Video stream should use a 90 kHz clock but appears to use "
751 << frequency_hz / 1000 << ". Discarding.";
752 continue;
753 }
754
755 auto ToCallTime = [this](const LoggedRtpPacketIncoming& packet) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100756 return this->config_.GetCallTimeSec(packet.log_time_us());
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100757 };
758 auto ToNetworkDelay = [frequency_hz](
759 const LoggedRtpPacketIncoming& old_packet,
760 const LoggedRtpPacketIncoming& new_packet) {
761 return NetworkDelayDiff_CaptureTime(old_packet, new_packet, frequency_hz);
762 };
763
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200764 TimeSeries capture_time_data(
765 GetStreamName(kIncomingPacket, stream.ssrc) + " capture-time",
766 LineStyle::kLine);
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100767 AccumulatePairs<LoggedRtpPacketIncoming, double>(
768 ToCallTime, ToNetworkDelay, packets, &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700769 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700770
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200771 TimeSeries send_time_data(
772 GetStreamName(kIncomingPacket, stream.ssrc) + " abs-send-time",
773 LineStyle::kLine);
Bjorn Terelius6c373cc2018-11-01 14:31:10 +0100774 AccumulatePairs<LoggedRtpPacketIncoming, double>(
775 ToCallTime, NetworkDelayDiff_AbsSendTime, packets, &send_time_data);
776 plot->AppendTimeSeriesIfNotEmpty(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700777 }
778
Bjorn Terelius068fc352019-02-13 22:38:25 +0100779 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
780 "Time (s)", kLeftMargin, kRightMargin);
Konrad Hofbauer5be3bbd2019-01-21 12:31:07 +0100781 plot->SetSuggestedYAxis(0, 1, "Delay (ms)", kBottomMargin, kTopMargin);
782 plot->SetTitle("Incoming network delay (relative to first packet)");
terelius54ce6802016-07-13 06:44:41 -0700783}
784
tereliusf736d232016-08-04 10:00:11 -0700785// Plot the fraction of packets lost (as perceived by the loss-based BWE).
786void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +0100787 TimeSeries time_series("Fraction lost", LineStyle::kLine,
788 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200789 for (auto& bwe_update : parsed_log_.bwe_loss_updates()) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100790 float x = config_.GetCallTimeSec(bwe_update.log_time_us());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200791 float y = static_cast<float>(bwe_update.fraction_lost) / 255 * 100;
philipel35ba9bd2017-04-19 05:58:51 -0700792 time_series.points.emplace_back(x, y);
tereliusf736d232016-08-04 10:00:11 -0700793 }
tereliusf736d232016-08-04 10:00:11 -0700794
Bjorn Terelius19f5be32017-10-18 12:39:49 +0200795 plot->AppendTimeSeries(std::move(time_series));
Bjorn Terelius068fc352019-02-13 22:38:25 +0100796 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
797 "Time (s)", kLeftMargin, kRightMargin);
tereliusf736d232016-08-04 10:00:11 -0700798 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
799 kTopMargin);
800 plot->SetTitle("Reported packet loss");
801}
802
terelius54ce6802016-07-13 06:44:41 -0700803// Plot the total bandwidth used by all RTP streams.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200804void EventLogAnalyzer::CreateTotalIncomingBitrateGraph(Plot* plot) {
805 // TODO(terelius): This could be provided by the parser.
806 std::multimap<int64_t, size_t> packets_in_order;
807 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
808 for (const LoggedRtpPacketIncoming& packet : stream.incoming_packets)
809 packets_in_order.insert(
810 std::make_pair(packet.rtp.log_time_us(), packet.rtp.total_length));
terelius54ce6802016-07-13 06:44:41 -0700811 }
812
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200813 auto window_begin = packets_in_order.begin();
814 auto window_end = packets_in_order.begin();
terelius54ce6802016-07-13 06:44:41 -0700815 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700816
817 // Calculate a moving average of the bitrate and store in a TimeSeries.
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +0100818 TimeSeries bitrate_series("Bitrate", LineStyle::kLine);
Bjorn Terelius068fc352019-02-13 22:38:25 +0100819 for (int64_t time = config_.begin_time_;
820 time < config_.end_time_ + config_.step_; time += config_.step_) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200821 while (window_end != packets_in_order.end() && window_end->first < time) {
822 bytes_in_window += window_end->second;
823 ++window_end;
terelius54ce6802016-07-13 06:44:41 -0700824 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200825 while (window_begin != packets_in_order.end() &&
Bjorn Terelius068fc352019-02-13 22:38:25 +0100826 window_begin->first < time - config_.window_duration_) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200827 RTC_DCHECK_LE(window_begin->second, bytes_in_window);
828 bytes_in_window -= window_begin->second;
829 ++window_begin;
830 }
831 float window_duration_in_seconds =
Bjorn Terelius068fc352019-02-13 22:38:25 +0100832 static_cast<float>(config_.window_duration_) / kNumMicrosecsPerSec;
833 float x = config_.GetCallTimeSec(time);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200834 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
835 bitrate_series.points.emplace_back(x, y);
836 }
837 plot->AppendTimeSeries(std::move(bitrate_series));
838
839 // Overlay the outgoing REMB over incoming bitrate.
840 TimeSeries remb_series("Remb", LineStyle::kStep);
841 for (const auto& rtcp : parsed_log_.rembs(kOutgoingPacket)) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100842 float x = config_.GetCallTimeSec(rtcp.log_time_us());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200843 float y = static_cast<float>(rtcp.remb.bitrate_bps()) / 1000;
844 remb_series.points.emplace_back(x, y);
845 }
846 plot->AppendTimeSeriesIfNotEmpty(std::move(remb_series));
847
Bjorn Terelius068fc352019-02-13 22:38:25 +0100848 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
849 "Time (s)", kLeftMargin, kRightMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200850 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
851 plot->SetTitle("Incoming RTP bitrate");
852}
853
854// Plot the total bandwidth used by all RTP streams.
855void EventLogAnalyzer::CreateTotalOutgoingBitrateGraph(Plot* plot,
856 bool show_detector_state,
857 bool show_alr_state) {
858 // TODO(terelius): This could be provided by the parser.
859 std::multimap<int64_t, size_t> packets_in_order;
860 for (const auto& stream : parsed_log_.outgoing_rtp_packets_by_ssrc()) {
861 for (const LoggedRtpPacketOutgoing& packet : stream.outgoing_packets)
862 packets_in_order.insert(
863 std::make_pair(packet.rtp.log_time_us(), packet.rtp.total_length));
864 }
865
866 auto window_begin = packets_in_order.begin();
867 auto window_end = packets_in_order.begin();
868 size_t bytes_in_window = 0;
869
870 // Calculate a moving average of the bitrate and store in a TimeSeries.
871 TimeSeries bitrate_series("Bitrate", LineStyle::kLine);
Bjorn Terelius068fc352019-02-13 22:38:25 +0100872 for (int64_t time = config_.begin_time_;
873 time < config_.end_time_ + config_.step_; time += config_.step_) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200874 while (window_end != packets_in_order.end() && window_end->first < time) {
875 bytes_in_window += window_end->second;
876 ++window_end;
877 }
878 while (window_begin != packets_in_order.end() &&
Bjorn Terelius068fc352019-02-13 22:38:25 +0100879 window_begin->first < time - config_.window_duration_) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200880 RTC_DCHECK_LE(window_begin->second, bytes_in_window);
881 bytes_in_window -= window_begin->second;
882 ++window_begin;
terelius54ce6802016-07-13 06:44:41 -0700883 }
884 float window_duration_in_seconds =
Bjorn Terelius068fc352019-02-13 22:38:25 +0100885 static_cast<float>(config_.window_duration_) / kNumMicrosecsPerSec;
886 float x = config_.GetCallTimeSec(time);
terelius54ce6802016-07-13 06:44:41 -0700887 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
philipel35ba9bd2017-04-19 05:58:51 -0700888 bitrate_series.points.emplace_back(x, y);
terelius54ce6802016-07-13 06:44:41 -0700889 }
philipel35ba9bd2017-04-19 05:58:51 -0700890 plot->AppendTimeSeries(std::move(bitrate_series));
terelius54ce6802016-07-13 06:44:41 -0700891
terelius8058e582016-07-25 01:32:41 -0700892 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200893 TimeSeries loss_series("Loss-based estimate", LineStyle::kStep);
894 for (auto& loss_update : parsed_log_.bwe_loss_updates()) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100895 float x = config_.GetCallTimeSec(loss_update.log_time_us());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200896 float y = static_cast<float>(loss_update.bitrate_bps) / 1000;
897 loss_series.points.emplace_back(x, y);
898 }
philipel10fc0e62017-04-11 01:50:23 -0700899
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200900 TimeSeries delay_series("Delay-based estimate", LineStyle::kStep);
901 IntervalSeries overusing_series("Overusing", "#ff8e82",
902 IntervalSeries::kHorizontal);
903 IntervalSeries underusing_series("Underusing", "#5092fc",
904 IntervalSeries::kHorizontal);
905 IntervalSeries normal_series("Normal", "#c4ffc4",
906 IntervalSeries::kHorizontal);
907 IntervalSeries* last_series = &normal_series;
908 double last_detector_switch = 0.0;
philipel23c7f252017-07-14 06:30:03 -0700909
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200910 BandwidthUsage last_detector_state = BandwidthUsage::kBwNormal;
philipel23c7f252017-07-14 06:30:03 -0700911
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200912 for (auto& delay_update : parsed_log_.bwe_delay_updates()) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100913 float x = config_.GetCallTimeSec(delay_update.log_time_us());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200914 float y = static_cast<float>(delay_update.bitrate_bps) / 1000;
philipel23c7f252017-07-14 06:30:03 -0700915
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200916 if (last_detector_state != delay_update.detector_state) {
917 last_series->intervals.emplace_back(last_detector_switch, x);
918 last_detector_state = delay_update.detector_state;
919 last_detector_switch = x;
philipel23c7f252017-07-14 06:30:03 -0700920
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200921 switch (delay_update.detector_state) {
922 case BandwidthUsage::kBwNormal:
923 last_series = &normal_series;
924 break;
925 case BandwidthUsage::kBwUnderusing:
926 last_series = &underusing_series;
927 break;
928 case BandwidthUsage::kBwOverusing:
929 last_series = &overusing_series;
930 break;
931 case BandwidthUsage::kLast:
932 RTC_NOTREACHED();
philipele127e7a2017-03-29 16:28:53 +0200933 }
934 }
philipel23c7f252017-07-14 06:30:03 -0700935
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200936 delay_series.points.emplace_back(x, y);
937 }
Bjorn Terelius9e336ec2018-04-24 16:28:35 +0200938
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200939 RTC_CHECK(last_series);
Bjorn Terelius068fc352019-02-13 22:38:25 +0100940 last_series->intervals.emplace_back(last_detector_switch, config_.end_time_);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200941
942 TimeSeries created_series("Probe cluster created.", LineStyle::kNone,
943 PointStyle::kHighlight);
944 for (auto& cluster : parsed_log_.bwe_probe_cluster_created_events()) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100945 float x = config_.GetCallTimeSec(cluster.log_time_us());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200946 float y = static_cast<float>(cluster.bitrate_bps) / 1000;
947 created_series.points.emplace_back(x, y);
948 }
949
950 TimeSeries result_series("Probing results.", LineStyle::kNone,
951 PointStyle::kHighlight);
Bjorn Terelius7a0bb002018-05-29 14:45:53 +0200952 for (auto& result : parsed_log_.bwe_probe_success_events()) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100953 float x = config_.GetCallTimeSec(result.log_time_us());
Bjorn Terelius7a0bb002018-05-29 14:45:53 +0200954 float y = static_cast<float>(result.bitrate_bps) / 1000;
955 result_series.points.emplace_back(x, y);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200956 }
957
Piotr (Peter) Slatalaf996c842019-01-04 06:54:22 -0800958 TimeSeries probe_failures_series("Probe failed", LineStyle::kNone,
959 PointStyle::kHighlight);
960 for (auto& failure : parsed_log_.bwe_probe_failure_events()) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100961 float x = config_.GetCallTimeSec(failure.log_time_us());
Piotr (Peter) Slatalaf996c842019-01-04 06:54:22 -0800962 probe_failures_series.points.emplace_back(x, 0);
963 }
964
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200965 IntervalSeries alr_state("ALR", "#555555", IntervalSeries::kHorizontal);
966 bool previously_in_alr = false;
967 int64_t alr_start = 0;
968 for (auto& alr : parsed_log_.alr_state_events()) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100969 float y = config_.GetCallTimeSec(alr.log_time_us());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200970 if (!previously_in_alr && alr.in_alr) {
971 alr_start = alr.log_time_us();
972 previously_in_alr = true;
973 } else if (previously_in_alr && !alr.in_alr) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100974 float x = config_.GetCallTimeSec(alr_start);
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +0100975 alr_state.intervals.emplace_back(x, y);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200976 previously_in_alr = false;
Björn Tereliusff612732018-04-25 14:23:01 +0000977 }
Björn Tereliusff612732018-04-25 14:23:01 +0000978 }
979
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200980 if (previously_in_alr) {
Bjorn Terelius068fc352019-02-13 22:38:25 +0100981 float x = config_.GetCallTimeSec(alr_start);
982 float y = config_.GetCallTimeSec(config_.end_time_);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200983 alr_state.intervals.emplace_back(x, y);
984 }
985
986 if (show_detector_state) {
987 plot->AppendIntervalSeries(std::move(overusing_series));
988 plot->AppendIntervalSeries(std::move(underusing_series));
989 plot->AppendIntervalSeries(std::move(normal_series));
990 }
991
992 if (show_alr_state) {
993 plot->AppendIntervalSeries(std::move(alr_state));
994 }
995 plot->AppendTimeSeries(std::move(loss_series));
Piotr (Peter) Slatalaf996c842019-01-04 06:54:22 -0800996 plot->AppendTimeSeriesIfNotEmpty(std::move(probe_failures_series));
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200997 plot->AppendTimeSeries(std::move(delay_series));
998 plot->AppendTimeSeries(std::move(created_series));
999 plot->AppendTimeSeries(std::move(result_series));
1000
1001 // Overlay the incoming REMB over the outgoing bitrate.
Björn Tereliusff612732018-04-25 14:23:01 +00001002 TimeSeries remb_series("Remb", LineStyle::kStep);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001003 for (const auto& rtcp : parsed_log_.rembs(kIncomingPacket)) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001004 float x = config_.GetCallTimeSec(rtcp.log_time_us());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001005 float y = static_cast<float>(rtcp.remb.bitrate_bps()) / 1000;
terelius2c8e8a32017-06-02 01:29:48 -07001006 remb_series.points.emplace_back(x, y);
1007 }
1008 plot->AppendTimeSeriesIfNotEmpty(std::move(remb_series));
1009
Bjorn Terelius068fc352019-02-13 22:38:25 +01001010 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1011 "Time (s)", kLeftMargin, kRightMargin);
tereliusdc35dcd2016-08-01 12:03:27 -07001012 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001013 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001014}
1015
1016// For each SSRC, plot the bandwidth used by that stream.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001017void EventLogAnalyzer::CreateStreamBitrateGraph(PacketDirection direction,
1018 Plot* plot) {
1019 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
1020 // Filter on SSRC.
1021 if (!MatchingSsrc(stream.ssrc, desired_ssrc_)) {
terelius6addf492016-08-23 17:34:07 -07001022 continue;
terelius54ce6802016-07-13 06:44:41 -07001023 }
1024
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001025 TimeSeries time_series(GetStreamName(direction, stream.ssrc),
1026 LineStyle::kLine);
1027 auto GetPacketSizeKilobits = [](const LoggedRtpPacket& packet) {
1028 return packet.total_length * 8.0 / 1000.0;
1029 };
terelius53dc23c2017-03-13 05:24:05 -07001030 MovingAverage<LoggedRtpPacket, double>(
Bjorn Terelius068fc352019-02-13 22:38:25 +01001031 GetPacketSizeKilobits, stream.packet_view, config_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001032 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -07001033 }
1034
Bjorn Terelius068fc352019-02-13 22:38:25 +01001035 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1036 "Time (s)", kLeftMargin, kRightMargin);
tereliusdc35dcd2016-08-01 12:03:27 -07001037 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001038 plot->SetTitle(GetDirectionAsString(direction) + " bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001039}
1040
Bjorn Terelius28db2662017-10-04 14:22:43 +02001041void EventLogAnalyzer::CreateSendSideBweSimulationGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001042 using RtpPacketType = LoggedRtpPacketOutgoing;
1043 using TransportFeedbackType = LoggedRtcpPacketTransportFeedback;
Stefan Holmer13181032016-07-29 14:48:54 +02001044
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001045 // TODO(terelius): This could be provided by the parser.
1046 std::multimap<int64_t, const RtpPacketType*> outgoing_rtp;
1047 for (const auto& stream : parsed_log_.outgoing_rtp_packets_by_ssrc()) {
1048 for (const RtpPacketType& rtp_packet : stream.outgoing_packets)
1049 outgoing_rtp.insert(
1050 std::make_pair(rtp_packet.rtp.log_time_us(), &rtp_packet));
Stefan Holmer13181032016-07-29 14:48:54 +02001051 }
1052
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001053 const std::vector<TransportFeedbackType>& incoming_rtcp =
1054 parsed_log_.transport_feedbacks(kIncomingPacket);
Stefan Holmer13181032016-07-29 14:48:54 +02001055
1056 SimulatedClock clock(0);
1057 BitrateObserver observer;
1058 RtcEventLogNullImpl null_event_log;
nisse0245da02016-11-30 03:35:20 -08001059 PacketRouter packet_router;
Stefan Holmer5c8942a2017-08-22 16:16:44 +02001060 PacedSender pacer(&clock, &packet_router, &null_event_log);
Sebastian Jansson05acd2b2019-01-21 13:07:47 +01001061 TransportFeedbackAdapter transport_feedback;
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001062 auto factory = GoogCcNetworkControllerFactory(&null_event_log);
1063 TimeDelta process_interval = factory.GetProcessInterval();
Stefan Holmer13181032016-07-29 14:48:54 +02001064 // TODO(holmer): Log the call config and use that here instead.
1065 static const uint32_t kDefaultStartBitrateBps = 300000;
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001066 NetworkControllerConfig cc_config;
Sebastian Jansson78416b62018-10-18 13:08:17 +02001067 cc_config.constraints.at_time = Timestamp::us(clock.TimeInMicroseconds());
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001068 cc_config.constraints.starting_rate = DataRate::bps(kDefaultStartBitrateBps);
1069 auto goog_cc = factory.Create(cc_config);
Stefan Holmer13181032016-07-29 14:48:54 +02001070
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001071 TimeSeries time_series("Delay-based estimate", LineStyle::kStep,
1072 PointStyle::kHighlight);
1073 TimeSeries acked_time_series("Acked bitrate", LineStyle::kLine,
1074 PointStyle::kHighlight);
1075 TimeSeries acked_estimate_time_series(
1076 "Acked bitrate estimate", LineStyle::kLine, PointStyle::kHighlight);
Stefan Holmer13181032016-07-29 14:48:54 +02001077
1078 auto rtp_iterator = outgoing_rtp.begin();
1079 auto rtcp_iterator = incoming_rtcp.begin();
1080
1081 auto NextRtpTime = [&]() {
1082 if (rtp_iterator != outgoing_rtp.end())
1083 return static_cast<int64_t>(rtp_iterator->first);
1084 return std::numeric_limits<int64_t>::max();
1085 };
1086
1087 auto NextRtcpTime = [&]() {
1088 if (rtcp_iterator != incoming_rtcp.end())
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001089 return static_cast<int64_t>(rtcp_iterator->log_time_us());
Stefan Holmer13181032016-07-29 14:48:54 +02001090 return std::numeric_limits<int64_t>::max();
1091 };
Bjorn Tereliusa728c912018-12-06 12:26:30 +01001092 int64_t next_process_time_us_ = std::min({NextRtpTime(), NextRtcpTime()});
Stefan Holmer13181032016-07-29 14:48:54 +02001093
1094 auto NextProcessTime = [&]() {
1095 if (rtcp_iterator != incoming_rtcp.end() ||
1096 rtp_iterator != outgoing_rtp.end()) {
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001097 return next_process_time_us_;
Stefan Holmer13181032016-07-29 14:48:54 +02001098 }
1099 return std::numeric_limits<int64_t>::max();
1100 };
1101
Stefan Holmer492ee282016-10-27 17:19:20 +02001102 RateStatistics acked_bitrate(250, 8000);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001103#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Sebastian Jansson95edb032019-01-17 16:24:12 +01001104 FieldTrialBasedConfig field_trial_config_;
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001105 // The event_log_visualizer should normally not be compiled with
1106 // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE since the normal plots won't work.
1107 // However, compiling with BWE_TEST_LOGGING, runnning with --plot_sendside_bwe
1108 // and piping the output to plot_dynamics.py can be used as a hack to get the
1109 // internal state of various BWE components. In this case, it is important
1110 // we don't instantiate the AcknowledgedBitrateEstimator both here and in
1111 // SendSideCongestionController since that would lead to duplicate outputs.
1112 AcknowledgedBitrateEstimator acknowledged_bitrate_estimator(
Sebastian Jansson95edb032019-01-17 16:24:12 +01001113 &field_trial_config_,
1114 absl::make_unique<BitrateEstimator>(&field_trial_config_));
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001115#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001116 int64_t time_us =
1117 std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
Stefan Holmer492ee282016-10-27 17:19:20 +02001118 int64_t last_update_us = 0;
Stefan Holmer13181032016-07-29 14:48:54 +02001119 while (time_us != std::numeric_limits<int64_t>::max()) {
1120 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
Bjorn Tereliusc60a7772018-12-05 21:15:30 +01001121 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1122 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1123 const RtpPacketType& rtp_packet = *rtp_iterator->second;
1124 if (rtp_packet.rtp.header.extension.hasTransportSequenceNumber) {
1125 RTC_DCHECK(rtp_packet.rtp.header.extension.hasTransportSequenceNumber);
1126 transport_feedback.AddPacket(
1127 rtp_packet.rtp.header.ssrc,
1128 rtp_packet.rtp.header.extension.transportSequenceNumber,
Sebastian Jansson05acd2b2019-01-21 13:07:47 +01001129 rtp_packet.rtp.total_length, PacedPacketInfo(),
1130 Timestamp::us(rtp_packet.rtp.log_time_us()));
Bjorn Tereliusc60a7772018-12-05 21:15:30 +01001131 rtc::SentPacket sent_packet(
1132 rtp_packet.rtp.header.extension.transportSequenceNumber,
1133 rtp_packet.rtp.log_time_us() / 1000);
1134 auto sent_msg = transport_feedback.ProcessSentPacket(sent_packet);
1135 if (sent_msg)
1136 observer.Update(goog_cc->OnSentPacket(*sent_msg));
1137 }
1138 ++rtp_iterator;
1139 }
Stefan Holmer13181032016-07-29 14:48:54 +02001140 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001141 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001142
1143 auto feedback_msg = transport_feedback.ProcessTransportFeedback(
Sebastian Jansson05acd2b2019-01-21 13:07:47 +01001144 rtcp_iterator->transport_feedback,
1145 Timestamp::ms(clock.TimeInMilliseconds()));
Danil Chapovalov431abd92018-06-18 12:54:17 +02001146 absl::optional<uint32_t> bitrate_bps;
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001147 if (feedback_msg) {
1148 observer.Update(goog_cc->OnTransportPacketsFeedback(*feedback_msg));
1149 std::vector<PacketFeedback> feedback =
1150 transport_feedback.GetTransportFeedbackVector();
1151 SortPacketFeedbackVector(&feedback);
1152 if (!feedback.empty()) {
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001153#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001154 acknowledged_bitrate_estimator.IncomingPacketFeedbackVector(feedback);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001155#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001156 for (const PacketFeedback& packet : feedback)
1157 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1158 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
1159 }
Stefan Holmer13181032016-07-29 14:48:54 +02001160 }
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001161
Bjorn Terelius068fc352019-02-13 22:38:25 +01001162 float x = config_.GetCallTimeSec(clock.TimeInMicroseconds());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001163 float y = bitrate_bps.value_or(0) / 1000;
1164 acked_time_series.points.emplace_back(x, y);
1165#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1166 y = acknowledged_bitrate_estimator.bitrate_bps().value_or(0) / 1000;
1167 acked_estimate_time_series.points.emplace_back(x, y);
1168#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Stefan Holmer13181032016-07-29 14:48:54 +02001169 ++rtcp_iterator;
1170 }
stefanc3de0332016-08-02 07:22:17 -07001171 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1172 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Sebastian Jansson5c94f552018-10-15 18:46:51 +02001173 ProcessInterval msg;
1174 msg.at_time = Timestamp::us(clock.TimeInMicroseconds());
1175 observer.Update(goog_cc->OnProcessInterval(msg));
1176 next_process_time_us_ += process_interval.us();
stefanc3de0332016-08-02 07:22:17 -07001177 }
Stefan Holmer492ee282016-10-27 17:19:20 +02001178 if (observer.GetAndResetBitrateUpdated() ||
1179 time_us - last_update_us >= 1e6) {
Stefan Holmer13181032016-07-29 14:48:54 +02001180 uint32_t y = observer.last_bitrate_bps() / 1000;
Bjorn Terelius068fc352019-02-13 22:38:25 +01001181 float x = config_.GetCallTimeSec(clock.TimeInMicroseconds());
Stefan Holmer13181032016-07-29 14:48:54 +02001182 time_series.points.emplace_back(x, y);
Stefan Holmer492ee282016-10-27 17:19:20 +02001183 last_update_us = time_us;
Stefan Holmer13181032016-07-29 14:48:54 +02001184 }
1185 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1186 }
1187 // Add the data set to the plot.
philipel35ba9bd2017-04-19 05:58:51 -07001188 plot->AppendTimeSeries(std::move(time_series));
1189 plot->AppendTimeSeries(std::move(acked_time_series));
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001190 plot->AppendTimeSeriesIfNotEmpty(std::move(acked_estimate_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001191
Bjorn Terelius068fc352019-02-13 22:38:25 +01001192 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1193 "Time (s)", kLeftMargin, kRightMargin);
tereliusdc35dcd2016-08-01 12:03:27 -07001194 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001195 plot->SetTitle("Simulated send-side BWE behavior");
1196}
1197
1198void EventLogAnalyzer::CreateReceiveSideBweSimulationGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001199 using RtpPacketType = LoggedRtpPacketIncoming;
Bjorn Terelius28db2662017-10-04 14:22:43 +02001200 class RembInterceptingPacketRouter : public PacketRouter {
1201 public:
1202 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
1203 uint32_t bitrate_bps) override {
1204 last_bitrate_bps_ = bitrate_bps;
1205 bitrate_updated_ = true;
1206 PacketRouter::OnReceiveBitrateChanged(ssrcs, bitrate_bps);
1207 }
1208 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
1209 bool GetAndResetBitrateUpdated() {
1210 bool bitrate_updated = bitrate_updated_;
1211 bitrate_updated_ = false;
1212 return bitrate_updated;
1213 }
1214
1215 private:
1216 uint32_t last_bitrate_bps_;
1217 bool bitrate_updated_;
1218 };
1219
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001220 std::multimap<int64_t, const RtpPacketType*> incoming_rtp;
Bjorn Terelius28db2662017-10-04 14:22:43 +02001221
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001222 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
1223 if (IsVideoSsrc(kIncomingPacket, stream.ssrc)) {
1224 for (const auto& rtp_packet : stream.incoming_packets)
1225 incoming_rtp.insert(
1226 std::make_pair(rtp_packet.rtp.log_time_us(), &rtp_packet));
Bjorn Terelius28db2662017-10-04 14:22:43 +02001227 }
1228 }
1229
1230 SimulatedClock clock(0);
1231 RembInterceptingPacketRouter packet_router;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001232 // TODO(terelius): The PacketRouter is used as the RemoteBitrateObserver.
Bjorn Terelius28db2662017-10-04 14:22:43 +02001233 // Is this intentional?
1234 ReceiveSideCongestionController rscc(&clock, &packet_router);
1235 // TODO(holmer): Log the call config and use that here instead.
1236 // static const uint32_t kDefaultStartBitrateBps = 300000;
1237 // rscc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1238
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001239 TimeSeries time_series("Receive side estimate", LineStyle::kLine,
1240 PointStyle::kHighlight);
1241 TimeSeries acked_time_series("Received bitrate", LineStyle::kLine);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001242
1243 RateStatistics acked_bitrate(250, 8000);
1244 int64_t last_update_us = 0;
1245 for (const auto& kv : incoming_rtp) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001246 const RtpPacketType& packet = *kv.second;
1247 int64_t arrival_time_ms = packet.rtp.log_time_us() / 1000;
1248 size_t payload = packet.rtp.total_length; /*Should subtract header?*/
1249 clock.AdvanceTimeMicroseconds(packet.rtp.log_time_us() -
Bjorn Terelius28db2662017-10-04 14:22:43 +02001250 clock.TimeInMicroseconds());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001251 rscc.OnReceivedPacket(arrival_time_ms, payload, packet.rtp.header);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001252 acked_bitrate.Update(payload, arrival_time_ms);
Danil Chapovalov431abd92018-06-18 12:54:17 +02001253 absl::optional<uint32_t> bitrate_bps = acked_bitrate.Rate(arrival_time_ms);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001254 if (bitrate_bps) {
1255 uint32_t y = *bitrate_bps / 1000;
Bjorn Terelius068fc352019-02-13 22:38:25 +01001256 float x = config_.GetCallTimeSec(clock.TimeInMicroseconds());
Bjorn Terelius28db2662017-10-04 14:22:43 +02001257 acked_time_series.points.emplace_back(x, y);
1258 }
1259 if (packet_router.GetAndResetBitrateUpdated() ||
1260 clock.TimeInMicroseconds() - last_update_us >= 1e6) {
1261 uint32_t y = packet_router.last_bitrate_bps() / 1000;
Bjorn Terelius068fc352019-02-13 22:38:25 +01001262 float x = config_.GetCallTimeSec(clock.TimeInMicroseconds());
Bjorn Terelius28db2662017-10-04 14:22:43 +02001263 time_series.points.emplace_back(x, y);
1264 last_update_us = clock.TimeInMicroseconds();
1265 }
1266 }
1267 // Add the data set to the plot.
1268 plot->AppendTimeSeries(std::move(time_series));
1269 plot->AppendTimeSeries(std::move(acked_time_series));
1270
Bjorn Terelius068fc352019-02-13 22:38:25 +01001271 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1272 "Time (s)", kLeftMargin, kRightMargin);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001273 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1274 plot->SetTitle("Simulated receive-side BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001275}
1276
tereliuse34c19c2016-08-15 08:47:14 -07001277void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001278 TimeSeries late_feedback_series("Late feedback results.", LineStyle::kNone,
1279 PointStyle::kHighlight);
Bjorn Terelius57ba7e12018-10-25 15:31:35 +02001280 TimeSeries time_series("Network delay", LineStyle::kLine,
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001281 PointStyle::kHighlight);
Bjorn Terelius57ba7e12018-10-25 15:31:35 +02001282 int64_t min_send_receive_diff_ms = std::numeric_limits<int64_t>::max();
1283 int64_t min_rtt_ms = std::numeric_limits<int64_t>::max();
stefanc3de0332016-08-02 07:22:17 -07001284
stefana0a8ed72017-09-06 02:06:32 -07001285 int64_t prev_y = 0;
Christoffer Rodbro89f64d32018-09-27 14:29:35 +02001286 for (auto packet : GetNetworkTrace(parsed_log_)) {
1287 if (packet.arrival_time_ms == PacketFeedback::kNotReceived)
1288 continue;
Bjorn Terelius068fc352019-02-13 22:38:25 +01001289 float x = config_.GetCallTimeSec(1000 * packet.feedback_arrival_time_ms);
Christoffer Rodbro89f64d32018-09-27 14:29:35 +02001290 if (packet.send_time_ms == PacketFeedback::kNoSendTime) {
1291 late_feedback_series.points.emplace_back(x, prev_y);
1292 continue;
stefanc3de0332016-08-02 07:22:17 -07001293 }
Christoffer Rodbro89f64d32018-09-27 14:29:35 +02001294 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1295 prev_y = y;
Bjorn Terelius57ba7e12018-10-25 15:31:35 +02001296 int64_t rtt_ms = packet.feedback_arrival_time_ms - packet.send_time_ms;
1297 min_rtt_ms = std::min(rtt_ms, min_rtt_ms);
1298 min_send_receive_diff_ms = std::min(y, min_send_receive_diff_ms);
Christoffer Rodbro89f64d32018-09-27 14:29:35 +02001299 time_series.points.emplace_back(x, y);
stefanc3de0332016-08-02 07:22:17 -07001300 }
Christoffer Rodbro89f64d32018-09-27 14:29:35 +02001301
Bjorn Terelius57ba7e12018-10-25 15:31:35 +02001302 // We assume that the base network delay (w/o queues) is equal to half
1303 // the minimum RTT. Therefore rescale the delays by subtracting the minimum
1304 // observed 1-ways delay and add half the minumum RTT.
1305 const int64_t estimated_clock_offset_ms =
1306 min_send_receive_diff_ms - min_rtt_ms / 2;
stefanc3de0332016-08-02 07:22:17 -07001307 for (TimeSeriesPoint& point : time_series.points)
Bjorn Terelius57ba7e12018-10-25 15:31:35 +02001308 point.y -= estimated_clock_offset_ms;
stefana0a8ed72017-09-06 02:06:32 -07001309 for (TimeSeriesPoint& point : late_feedback_series.points)
Bjorn Terelius57ba7e12018-10-25 15:31:35 +02001310 point.y -= estimated_clock_offset_ms;
1311
stefanc3de0332016-08-02 07:22:17 -07001312 // Add the data set to the plot.
stefana0a8ed72017-09-06 02:06:32 -07001313 plot->AppendTimeSeriesIfNotEmpty(std::move(time_series));
1314 plot->AppendTimeSeriesIfNotEmpty(std::move(late_feedback_series));
stefanc3de0332016-08-02 07:22:17 -07001315
Bjorn Terelius068fc352019-02-13 22:38:25 +01001316 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1317 "Time (s)", kLeftMargin, kRightMargin);
stefanc3de0332016-08-02 07:22:17 -07001318 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
Konrad Hofbauer5be3bbd2019-01-21 12:31:07 +01001319 plot->SetTitle("Outgoing network delay (based on per-packet feedback)");
stefanc3de0332016-08-02 07:22:17 -07001320}
stefan08383272016-12-20 08:51:52 -08001321
Bjorn Terelius0295a962017-10-25 17:42:41 +02001322void EventLogAnalyzer::CreatePacerDelayGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001323 for (const auto& stream : parsed_log_.outgoing_rtp_packets_by_ssrc()) {
1324 const std::vector<LoggedRtpPacketOutgoing>& packets =
1325 stream.outgoing_packets;
Bjorn Terelius0295a962017-10-25 17:42:41 +02001326
1327 if (packets.size() < 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001328 RTC_LOG(LS_WARNING)
1329 << "Can't estimate a the RTP clock frequency or the "
1330 "pacer delay with less than 2 packets in the stream";
Bjorn Terelius0295a962017-10-25 17:42:41 +02001331 continue;
1332 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001333 int64_t end_time_us = log_segments_.empty()
1334 ? std::numeric_limits<int64_t>::max()
1335 : log_segments_.front().second;
Danil Chapovalov431abd92018-06-18 12:54:17 +02001336 absl::optional<uint32_t> estimated_frequency =
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001337 EstimateRtpClockFrequency(packets, end_time_us);
Bjorn Terelius0295a962017-10-25 17:42:41 +02001338 if (!estimated_frequency)
1339 continue;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001340 if (IsVideoSsrc(kOutgoingPacket, stream.ssrc) &&
1341 *estimated_frequency != 90000) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001342 RTC_LOG(LS_WARNING)
Bjorn Terelius0295a962017-10-25 17:42:41 +02001343 << "Video stream should use a 90 kHz clock but appears to use "
1344 << *estimated_frequency / 1000 << ". Discarding.";
1345 continue;
1346 }
1347
1348 TimeSeries pacer_delay_series(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001349 GetStreamName(kOutgoingPacket, stream.ssrc) + "(" +
Bjorn Terelius0295a962017-10-25 17:42:41 +02001350 std::to_string(*estimated_frequency / 1000) + " kHz)",
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001351 LineStyle::kLine, PointStyle::kHighlight);
Bjorn Terelius0295a962017-10-25 17:42:41 +02001352 SeqNumUnwrapper<uint32_t> timestamp_unwrapper;
1353 uint64_t first_capture_timestamp =
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001354 timestamp_unwrapper.Unwrap(packets.front().rtp.header.timestamp);
1355 uint64_t first_send_timestamp = packets.front().rtp.log_time_us();
1356 for (const auto& packet : packets) {
Bjorn Terelius0295a962017-10-25 17:42:41 +02001357 double capture_time_ms = (static_cast<double>(timestamp_unwrapper.Unwrap(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001358 packet.rtp.header.timestamp)) -
Bjorn Terelius0295a962017-10-25 17:42:41 +02001359 first_capture_timestamp) /
1360 *estimated_frequency * 1000;
1361 double send_time_ms =
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001362 static_cast<double>(packet.rtp.log_time_us() - first_send_timestamp) /
1363 1000;
Bjorn Terelius068fc352019-02-13 22:38:25 +01001364 float x = config_.GetCallTimeSec(packet.rtp.log_time_us());
Bjorn Terelius0295a962017-10-25 17:42:41 +02001365 float y = send_time_ms - capture_time_ms;
1366 pacer_delay_series.points.emplace_back(x, y);
1367 }
1368 plot->AppendTimeSeries(std::move(pacer_delay_series));
1369 }
1370
Bjorn Terelius068fc352019-02-13 22:38:25 +01001371 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1372 "Time (s)", kLeftMargin, kRightMargin);
Bjorn Terelius0295a962017-10-25 17:42:41 +02001373 plot->SetSuggestedYAxis(0, 10, "Pacer delay (ms)", kBottomMargin, kTopMargin);
1374 plot->SetTitle(
1375 "Delay from capture to send time. (First packet normalized to 0.)");
1376}
1377
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001378void EventLogAnalyzer::CreateTimestampGraph(PacketDirection direction,
1379 Plot* plot) {
1380 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
1381 TimeSeries rtp_timestamps(
1382 GetStreamName(direction, stream.ssrc) + " capture-time",
1383 LineStyle::kLine, PointStyle::kHighlight);
1384 for (const auto& packet : stream.packet_view) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001385 float x = config_.GetCallTimeSec(packet.log_time_us());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001386 float y = packet.header.timestamp;
1387 rtp_timestamps.points.emplace_back(x, y);
stefane372d3c2017-02-02 08:04:18 -08001388 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001389 plot->AppendTimeSeries(std::move(rtp_timestamps));
Björn Tereliusff612732018-04-25 14:23:01 +00001390
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001391 TimeSeries rtcp_timestamps(
1392 GetStreamName(direction, stream.ssrc) + " rtcp capture-time",
1393 LineStyle::kLine, PointStyle::kHighlight);
1394 // TODO(terelius): Why only sender reports?
1395 const auto& sender_reports = parsed_log_.sender_reports(direction);
1396 for (const auto& rtcp : sender_reports) {
1397 if (rtcp.sr.sender_ssrc() != stream.ssrc)
1398 continue;
Bjorn Terelius068fc352019-02-13 22:38:25 +01001399 float x = config_.GetCallTimeSec(rtcp.log_time_us());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001400 float y = rtcp.sr.rtp_timestamp();
1401 rtcp_timestamps.points.emplace_back(x, y);
Björn Tereliusff612732018-04-25 14:23:01 +00001402 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001403 plot->AppendTimeSeriesIfNotEmpty(std::move(rtcp_timestamps));
stefane372d3c2017-02-02 08:04:18 -08001404 }
1405
Bjorn Terelius068fc352019-02-13 22:38:25 +01001406 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1407 "Time (s)", kLeftMargin, kRightMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001408 plot->SetSuggestedYAxis(0, 1, "RTP timestamp", kBottomMargin, kTopMargin);
1409 plot->SetTitle(GetDirectionAsString(direction) + " timestamps");
stefane372d3c2017-02-02 08:04:18 -08001410}
michaelt6e5b2192017-02-22 07:33:27 -08001411
Bjorn Tereliusb1222c22018-07-24 13:45:31 +02001412void EventLogAnalyzer::CreateSenderAndReceiverReportPlot(
1413 PacketDirection direction,
1414 rtc::FunctionView<float(const rtcp::ReportBlock&)> fy,
1415 std::string title,
1416 std::string yaxis_label,
1417 Plot* plot) {
1418 std::map<uint32_t, TimeSeries> sr_reports_by_ssrc;
1419 const auto& sender_reports = parsed_log_.sender_reports(direction);
1420 for (const auto& rtcp : sender_reports) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001421 float x = config_.GetCallTimeSec(rtcp.log_time_us());
Bjorn Tereliusb1222c22018-07-24 13:45:31 +02001422 uint32_t ssrc = rtcp.sr.sender_ssrc();
1423 for (const auto& block : rtcp.sr.report_blocks()) {
1424 float y = fy(block);
1425 auto sr_report_it = sr_reports_by_ssrc.find(ssrc);
1426 bool inserted;
1427 if (sr_report_it == sr_reports_by_ssrc.end()) {
1428 std::tie(sr_report_it, inserted) = sr_reports_by_ssrc.emplace(
1429 ssrc, TimeSeries(GetStreamName(direction, ssrc) + " Sender Reports",
1430 LineStyle::kLine, PointStyle::kHighlight));
1431 }
1432 sr_report_it->second.points.emplace_back(x, y);
1433 }
1434 }
1435 for (auto& kv : sr_reports_by_ssrc) {
1436 plot->AppendTimeSeries(std::move(kv.second));
1437 }
1438
1439 std::map<uint32_t, TimeSeries> rr_reports_by_ssrc;
1440 const auto& receiver_reports = parsed_log_.receiver_reports(direction);
1441 for (const auto& rtcp : receiver_reports) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001442 float x = config_.GetCallTimeSec(rtcp.log_time_us());
Bjorn Tereliusb1222c22018-07-24 13:45:31 +02001443 uint32_t ssrc = rtcp.rr.sender_ssrc();
1444 for (const auto& block : rtcp.rr.report_blocks()) {
1445 float y = fy(block);
1446 auto rr_report_it = rr_reports_by_ssrc.find(ssrc);
1447 bool inserted;
1448 if (rr_report_it == rr_reports_by_ssrc.end()) {
1449 std::tie(rr_report_it, inserted) = rr_reports_by_ssrc.emplace(
1450 ssrc,
1451 TimeSeries(GetStreamName(direction, ssrc) + " Receiver Reports",
1452 LineStyle::kLine, PointStyle::kHighlight));
1453 }
1454 rr_report_it->second.points.emplace_back(x, y);
1455 }
1456 }
1457 for (auto& kv : rr_reports_by_ssrc) {
1458 plot->AppendTimeSeries(std::move(kv.second));
1459 }
1460
Bjorn Terelius068fc352019-02-13 22:38:25 +01001461 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1462 "Time (s)", kLeftMargin, kRightMargin);
Bjorn Tereliusb1222c22018-07-24 13:45:31 +02001463 plot->SetSuggestedYAxis(0, 1, yaxis_label, kBottomMargin, kTopMargin);
1464 plot->SetTitle(title);
1465}
1466
michaelt6e5b2192017-02-22 07:33:27 -08001467void EventLogAnalyzer::CreateAudioEncoderTargetBitrateGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001468 TimeSeries time_series("Audio encoder target bitrate", LineStyle::kLine,
1469 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001470 auto GetAnaBitrateBps = [](const LoggedAudioNetworkAdaptationEvent& ana_event)
Danil Chapovalov431abd92018-06-18 12:54:17 +02001471 -> absl::optional<float> {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001472 if (ana_event.config.bitrate_bps)
Danil Chapovalov431abd92018-06-18 12:54:17 +02001473 return absl::optional<float>(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001474 static_cast<float>(*ana_event.config.bitrate_bps));
Danil Chapovalov431abd92018-06-18 12:54:17 +02001475 return absl::nullopt;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001476 };
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001477 auto ToCallTime = [this](const LoggedAudioNetworkAdaptationEvent& packet) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001478 return this->config_.GetCallTimeSec(packet.log_time_us());
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001479 };
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001480 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001481 ToCallTime, GetAnaBitrateBps,
1482 parsed_log_.audio_network_adaptation_events(), &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001483 plot->AppendTimeSeries(std::move(time_series));
Bjorn Terelius068fc352019-02-13 22:38:25 +01001484 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1485 "Time (s)", kLeftMargin, kRightMargin);
michaelt6e5b2192017-02-22 07:33:27 -08001486 plot->SetSuggestedYAxis(0, 1, "Bitrate (bps)", kBottomMargin, kTopMargin);
1487 plot->SetTitle("Reported audio encoder target bitrate");
1488}
1489
1490void EventLogAnalyzer::CreateAudioEncoderFrameLengthGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001491 TimeSeries time_series("Audio encoder frame length", LineStyle::kLine,
1492 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001493 auto GetAnaFrameLengthMs =
1494 [](const LoggedAudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001495 if (ana_event.config.frame_length_ms)
Danil Chapovalov431abd92018-06-18 12:54:17 +02001496 return absl::optional<float>(
michaelt6e5b2192017-02-22 07:33:27 -08001497 static_cast<float>(*ana_event.config.frame_length_ms));
Danil Chapovalov431abd92018-06-18 12:54:17 +02001498 return absl::optional<float>();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001499 };
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001500 auto ToCallTime = [this](const LoggedAudioNetworkAdaptationEvent& packet) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001501 return this->config_.GetCallTimeSec(packet.log_time_us());
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001502 };
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001503 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001504 ToCallTime, GetAnaFrameLengthMs,
1505 parsed_log_.audio_network_adaptation_events(), &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001506 plot->AppendTimeSeries(std::move(time_series));
Bjorn Terelius068fc352019-02-13 22:38:25 +01001507 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1508 "Time (s)", kLeftMargin, kRightMargin);
michaelt6e5b2192017-02-22 07:33:27 -08001509 plot->SetSuggestedYAxis(0, 1, "Frame length (ms)", kBottomMargin, kTopMargin);
1510 plot->SetTitle("Reported audio encoder frame length");
1511}
1512
terelius2ee076d2017-08-15 02:04:02 -07001513void EventLogAnalyzer::CreateAudioEncoderPacketLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001514 TimeSeries time_series("Audio encoder uplink packet loss fraction",
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001515 LineStyle::kLine, PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001516 auto GetAnaPacketLoss =
1517 [](const LoggedAudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001518 if (ana_event.config.uplink_packet_loss_fraction)
Danil Chapovalov431abd92018-06-18 12:54:17 +02001519 return absl::optional<float>(static_cast<float>(
michaelt6e5b2192017-02-22 07:33:27 -08001520 *ana_event.config.uplink_packet_loss_fraction));
Danil Chapovalov431abd92018-06-18 12:54:17 +02001521 return absl::optional<float>();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001522 };
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001523 auto ToCallTime = [this](const LoggedAudioNetworkAdaptationEvent& packet) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001524 return this->config_.GetCallTimeSec(packet.log_time_us());
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001525 };
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001526 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001527 ToCallTime, GetAnaPacketLoss,
1528 parsed_log_.audio_network_adaptation_events(), &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001529 plot->AppendTimeSeries(std::move(time_series));
Bjorn Terelius068fc352019-02-13 22:38:25 +01001530 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1531 "Time (s)", kLeftMargin, kRightMargin);
michaelt6e5b2192017-02-22 07:33:27 -08001532 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
1533 kTopMargin);
1534 plot->SetTitle("Reported audio encoder lost packets");
1535}
1536
1537void EventLogAnalyzer::CreateAudioEncoderEnableFecGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001538 TimeSeries time_series("Audio encoder FEC", LineStyle::kLine,
1539 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001540 auto GetAnaFecEnabled =
1541 [](const LoggedAudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001542 if (ana_event.config.enable_fec)
Danil Chapovalov431abd92018-06-18 12:54:17 +02001543 return absl::optional<float>(
michaelt6e5b2192017-02-22 07:33:27 -08001544 static_cast<float>(*ana_event.config.enable_fec));
Danil Chapovalov431abd92018-06-18 12:54:17 +02001545 return absl::optional<float>();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001546 };
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001547 auto ToCallTime = [this](const LoggedAudioNetworkAdaptationEvent& packet) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001548 return this->config_.GetCallTimeSec(packet.log_time_us());
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001549 };
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001550 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001551 ToCallTime, GetAnaFecEnabled,
1552 parsed_log_.audio_network_adaptation_events(), &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001553 plot->AppendTimeSeries(std::move(time_series));
Bjorn Terelius068fc352019-02-13 22:38:25 +01001554 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1555 "Time (s)", kLeftMargin, kRightMargin);
michaelt6e5b2192017-02-22 07:33:27 -08001556 plot->SetSuggestedYAxis(0, 1, "FEC (false/true)", kBottomMargin, kTopMargin);
1557 plot->SetTitle("Reported audio encoder FEC");
1558}
1559
1560void EventLogAnalyzer::CreateAudioEncoderEnableDtxGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001561 TimeSeries time_series("Audio encoder DTX", LineStyle::kLine,
1562 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001563 auto GetAnaDtxEnabled =
1564 [](const LoggedAudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001565 if (ana_event.config.enable_dtx)
Danil Chapovalov431abd92018-06-18 12:54:17 +02001566 return absl::optional<float>(
michaelt6e5b2192017-02-22 07:33:27 -08001567 static_cast<float>(*ana_event.config.enable_dtx));
Danil Chapovalov431abd92018-06-18 12:54:17 +02001568 return absl::optional<float>();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001569 };
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001570 auto ToCallTime = [this](const LoggedAudioNetworkAdaptationEvent& packet) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001571 return this->config_.GetCallTimeSec(packet.log_time_us());
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001572 };
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001573 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001574 ToCallTime, GetAnaDtxEnabled,
1575 parsed_log_.audio_network_adaptation_events(), &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001576 plot->AppendTimeSeries(std::move(time_series));
Bjorn Terelius068fc352019-02-13 22:38:25 +01001577 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1578 "Time (s)", kLeftMargin, kRightMargin);
michaelt6e5b2192017-02-22 07:33:27 -08001579 plot->SetSuggestedYAxis(0, 1, "DTX (false/true)", kBottomMargin, kTopMargin);
1580 plot->SetTitle("Reported audio encoder DTX");
1581}
1582
1583void EventLogAnalyzer::CreateAudioEncoderNumChannelsGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001584 TimeSeries time_series("Audio encoder number of channels", LineStyle::kLine,
1585 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001586 auto GetAnaNumChannels =
1587 [](const LoggedAudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001588 if (ana_event.config.num_channels)
Danil Chapovalov431abd92018-06-18 12:54:17 +02001589 return absl::optional<float>(
michaelt6e5b2192017-02-22 07:33:27 -08001590 static_cast<float>(*ana_event.config.num_channels));
Danil Chapovalov431abd92018-06-18 12:54:17 +02001591 return absl::optional<float>();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001592 };
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001593 auto ToCallTime = [this](const LoggedAudioNetworkAdaptationEvent& packet) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001594 return this->config_.GetCallTimeSec(packet.log_time_us());
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001595 };
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001596 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
Stefan Holmer1d4a2272018-05-24 13:48:09 +02001597 ToCallTime, GetAnaNumChannels,
1598 parsed_log_.audio_network_adaptation_events(), &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001599 plot->AppendTimeSeries(std::move(time_series));
Bjorn Terelius068fc352019-02-13 22:38:25 +01001600 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1601 "Time (s)", kLeftMargin, kRightMargin);
michaelt6e5b2192017-02-22 07:33:27 -08001602 plot->SetSuggestedYAxis(0, 1, "Number of channels (1 (mono)/2 (stereo))",
1603 kBottomMargin, kTopMargin);
1604 plot->SetTitle("Reported audio encoder number of channels");
1605}
henrik.lundin3c938fc2017-06-14 06:09:58 -07001606
1607class NetEqStreamInput : public test::NetEqInput {
1608 public:
1609 // Does not take any ownership, and all pointers must refer to valid objects
1610 // that outlive the one constructed.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001611 NetEqStreamInput(const std::vector<LoggedRtpPacketIncoming>* packet_stream,
Minyue Li27e2b7d2018-05-07 15:20:24 +02001612 const std::vector<LoggedAudioPlayoutEvent>* output_events,
Danil Chapovalov431abd92018-06-18 12:54:17 +02001613 absl::optional<int64_t> end_time_ms)
henrik.lundin3c938fc2017-06-14 06:09:58 -07001614 : packet_stream_(*packet_stream),
1615 packet_stream_it_(packet_stream_.begin()),
Minyue Li27e2b7d2018-05-07 15:20:24 +02001616 output_events_it_(output_events->begin()),
1617 output_events_end_(output_events->end()),
1618 end_time_ms_(end_time_ms) {
henrik.lundin3c938fc2017-06-14 06:09:58 -07001619 RTC_DCHECK(packet_stream);
Minyue Li27e2b7d2018-05-07 15:20:24 +02001620 RTC_DCHECK(output_events);
henrik.lundin3c938fc2017-06-14 06:09:58 -07001621 }
1622
Danil Chapovalov431abd92018-06-18 12:54:17 +02001623 absl::optional<int64_t> NextPacketTime() const override {
henrik.lundin3c938fc2017-06-14 06:09:58 -07001624 if (packet_stream_it_ == packet_stream_.end()) {
Danil Chapovalov431abd92018-06-18 12:54:17 +02001625 return absl::nullopt;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001626 }
Minyue Li27e2b7d2018-05-07 15:20:24 +02001627 if (end_time_ms_ && packet_stream_it_->rtp.log_time_ms() > *end_time_ms_) {
Danil Chapovalov431abd92018-06-18 12:54:17 +02001628 return absl::nullopt;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001629 }
Minyue Li27e2b7d2018-05-07 15:20:24 +02001630 return packet_stream_it_->rtp.log_time_ms();
henrik.lundin3c938fc2017-06-14 06:09:58 -07001631 }
1632
Danil Chapovalov431abd92018-06-18 12:54:17 +02001633 absl::optional<int64_t> NextOutputEventTime() const override {
Minyue Li27e2b7d2018-05-07 15:20:24 +02001634 if (output_events_it_ == output_events_end_) {
Danil Chapovalov431abd92018-06-18 12:54:17 +02001635 return absl::nullopt;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001636 }
Minyue Li27e2b7d2018-05-07 15:20:24 +02001637 if (end_time_ms_ && output_events_it_->log_time_ms() > *end_time_ms_) {
Danil Chapovalov431abd92018-06-18 12:54:17 +02001638 return absl::nullopt;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001639 }
Minyue Li27e2b7d2018-05-07 15:20:24 +02001640 return output_events_it_->log_time_ms();
henrik.lundin3c938fc2017-06-14 06:09:58 -07001641 }
1642
1643 std::unique_ptr<PacketData> PopPacket() override {
1644 if (packet_stream_it_ == packet_stream_.end()) {
1645 return std::unique_ptr<PacketData>();
1646 }
1647 std::unique_ptr<PacketData> packet_data(new PacketData());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001648 packet_data->header = packet_stream_it_->rtp.header;
Minyue Li27e2b7d2018-05-07 15:20:24 +02001649 packet_data->time_ms = packet_stream_it_->rtp.log_time_ms();
henrik.lundin3c938fc2017-06-14 06:09:58 -07001650
1651 // This is a header-only "dummy" packet. Set the payload to all zeros, with
1652 // length according to the virtual length.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001653 packet_data->payload.SetSize(packet_stream_it_->rtp.total_length -
1654 packet_stream_it_->rtp.header_length);
henrik.lundin3c938fc2017-06-14 06:09:58 -07001655 std::fill_n(packet_data->payload.data(), packet_data->payload.size(), 0);
1656
1657 ++packet_stream_it_;
1658 return packet_data;
1659 }
1660
1661 void AdvanceOutputEvent() override {
Minyue Li27e2b7d2018-05-07 15:20:24 +02001662 if (output_events_it_ != output_events_end_) {
1663 ++output_events_it_;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001664 }
1665 }
1666
1667 bool ended() const override { return !NextEventTime(); }
1668
Danil Chapovalov431abd92018-06-18 12:54:17 +02001669 absl::optional<RTPHeader> NextHeader() const override {
henrik.lundin3c938fc2017-06-14 06:09:58 -07001670 if (packet_stream_it_ == packet_stream_.end()) {
Danil Chapovalov431abd92018-06-18 12:54:17 +02001671 return absl::nullopt;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001672 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001673 return packet_stream_it_->rtp.header;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001674 }
1675
1676 private:
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001677 const std::vector<LoggedRtpPacketIncoming>& packet_stream_;
1678 std::vector<LoggedRtpPacketIncoming>::const_iterator packet_stream_it_;
Minyue Li27e2b7d2018-05-07 15:20:24 +02001679 std::vector<LoggedAudioPlayoutEvent>::const_iterator output_events_it_;
1680 const std::vector<LoggedAudioPlayoutEvent>::const_iterator output_events_end_;
Danil Chapovalov431abd92018-06-18 12:54:17 +02001681 const absl::optional<int64_t> end_time_ms_;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001682};
1683
1684namespace {
Bjorn Terelius6c4b1b72019-01-11 13:01:29 +01001685
1686// Factory to create a "replacement decoder" that produces the decoded audio
1687// by reading from a file rather than from the encoded payloads.
1688class ReplacementAudioDecoderFactory : public AudioDecoderFactory {
1689 public:
1690 ReplacementAudioDecoderFactory(const absl::string_view replacement_file_name,
1691 int file_sample_rate_hz)
1692 : replacement_file_name_(replacement_file_name),
1693 file_sample_rate_hz_(file_sample_rate_hz) {}
1694
1695 std::vector<AudioCodecSpec> GetSupportedDecoders() override {
1696 RTC_NOTREACHED();
1697 return {};
1698 }
1699
1700 bool IsSupportedDecoder(const SdpAudioFormat& format) override {
1701 return true;
1702 }
1703
1704 std::unique_ptr<AudioDecoder> MakeAudioDecoder(
1705 const SdpAudioFormat& format,
1706 absl::optional<AudioCodecPairId> codec_pair_id) override {
1707 auto replacement_file = absl::make_unique<test::ResampleInputAudioFile>(
1708 replacement_file_name_, file_sample_rate_hz_);
1709 replacement_file->set_output_rate_hz(48000);
1710 return absl::make_unique<test::FakeDecodeFromFile>(
1711 std::move(replacement_file), 48000, false);
1712 }
1713
1714 private:
1715 const std::string replacement_file_name_;
1716 const int file_sample_rate_hz_;
1717};
1718
henrik.lundin3c938fc2017-06-14 06:09:58 -07001719// Creates a NetEq test object and all necessary input and output helpers. Runs
1720// the test and returns the NetEqDelayAnalyzer object that was used to
1721// instrument the test.
Minyue Lic6ff7572018-05-04 09:46:44 +02001722std::unique_ptr<test::NetEqStatsGetter> CreateNetEqTestAndRun(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001723 const std::vector<LoggedRtpPacketIncoming>* packet_stream,
Minyue Li27e2b7d2018-05-07 15:20:24 +02001724 const std::vector<LoggedAudioPlayoutEvent>* output_events,
Danil Chapovalov431abd92018-06-18 12:54:17 +02001725 absl::optional<int64_t> end_time_ms,
henrik.lundin3c938fc2017-06-14 06:09:58 -07001726 const std::string& replacement_file_name,
1727 int file_sample_rate_hz) {
1728 std::unique_ptr<test::NetEqInput> input(
Minyue Li27e2b7d2018-05-07 15:20:24 +02001729 new NetEqStreamInput(packet_stream, output_events, end_time_ms));
henrik.lundin3c938fc2017-06-14 06:09:58 -07001730
1731 constexpr int kReplacementPt = 127;
1732 std::set<uint8_t> cn_types;
1733 std::set<uint8_t> forbidden_types;
1734 input.reset(new test::NetEqReplacementInput(std::move(input), kReplacementPt,
1735 cn_types, forbidden_types));
1736
1737 NetEq::Config config;
1738 config.max_packets_in_buffer = 200;
1739 config.enable_fast_accelerate = true;
1740
1741 std::unique_ptr<test::VoidAudioSink> output(new test::VoidAudioSink());
1742
Niels Möller3f651d82018-12-19 15:06:17 +01001743 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
Bjorn Terelius6c4b1b72019-01-11 13:01:29 +01001744 new rtc::RefCountedObject<ReplacementAudioDecoderFactory>(
1745 replacement_file_name, file_sample_rate_hz);
Niels Möller3f651d82018-12-19 15:06:17 +01001746
Niels Möller05543682019-01-10 16:55:06 +01001747 test::NetEqTest::DecoderMap codecs = {
1748 {kReplacementPt, SdpAudioFormat("l16", 48000, 1)}};
henrik.lundin3c938fc2017-06-14 06:09:58 -07001749
1750 std::unique_ptr<test::NetEqDelayAnalyzer> delay_cb(
1751 new test::NetEqDelayAnalyzer);
Minyue Lic6ff7572018-05-04 09:46:44 +02001752 std::unique_ptr<test::NetEqStatsGetter> neteq_stats_getter(
1753 new test::NetEqStatsGetter(std::move(delay_cb)));
henrik.lundin3c938fc2017-06-14 06:09:58 -07001754 test::DefaultNetEqTestErrorCallback error_cb;
1755 test::NetEqTest::Callbacks callbacks;
1756 callbacks.error_callback = &error_cb;
Minyue Lic6ff7572018-05-04 09:46:44 +02001757 callbacks.post_insert_packet = neteq_stats_getter->delay_analyzer();
1758 callbacks.get_audio_callback = neteq_stats_getter.get();
henrik.lundin3c938fc2017-06-14 06:09:58 -07001759
Niels Möllerbd6dee82019-01-02 09:39:47 +01001760 test::NetEqTest test(config, decoder_factory, codecs, nullptr,
Niels Möller3f651d82018-12-19 15:06:17 +01001761 std::move(input), std::move(output), callbacks);
henrik.lundin3c938fc2017-06-14 06:09:58 -07001762 test.Run();
Minyue Lic6ff7572018-05-04 09:46:44 +02001763 return neteq_stats_getter;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001764}
1765} // namespace
1766
Minyue Lic6ff7572018-05-04 09:46:44 +02001767EventLogAnalyzer::NetEqStatsGetterMap EventLogAnalyzer::SimulateNetEq(
henrik.lundin3c938fc2017-06-14 06:09:58 -07001768 const std::string& replacement_file_name,
Minyue Lic6ff7572018-05-04 09:46:44 +02001769 int file_sample_rate_hz) const {
1770 NetEqStatsGetterMap neteq_stats;
1771
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001772 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
Minyue Li27e2b7d2018-05-07 15:20:24 +02001773 const uint32_t ssrc = stream.ssrc;
1774 if (!IsAudioSsrc(kIncomingPacket, ssrc))
1775 continue;
1776 const std::vector<LoggedRtpPacketIncoming>* audio_packets =
1777 &stream.incoming_packets;
1778 if (audio_packets == nullptr) {
1779 // No incoming audio stream found.
1780 continue;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001781 }
henrik.lundin3c938fc2017-06-14 06:09:58 -07001782
Minyue Li27e2b7d2018-05-07 15:20:24 +02001783 RTC_DCHECK(neteq_stats.find(ssrc) == neteq_stats.end());
1784
1785 std::map<uint32_t, std::vector<LoggedAudioPlayoutEvent>>::const_iterator
1786 output_events_it = parsed_log_.audio_playout_events().find(ssrc);
1787 if (output_events_it == parsed_log_.audio_playout_events().end()) {
1788 // Could not find output events with SSRC matching the input audio stream.
1789 // Using the first available stream of output events.
1790 output_events_it = parsed_log_.audio_playout_events().cbegin();
1791 }
1792
Danil Chapovalov431abd92018-06-18 12:54:17 +02001793 absl::optional<int64_t> end_time_ms =
Minyue Li27e2b7d2018-05-07 15:20:24 +02001794 log_segments_.empty()
Danil Chapovalov431abd92018-06-18 12:54:17 +02001795 ? absl::nullopt
1796 : absl::optional<int64_t>(log_segments_.front().second / 1000);
Minyue Li27e2b7d2018-05-07 15:20:24 +02001797
1798 neteq_stats[ssrc] = CreateNetEqTestAndRun(
1799 audio_packets, &output_events_it->second, end_time_ms,
1800 replacement_file_name, file_sample_rate_hz);
henrik.lundin3c938fc2017-06-14 06:09:58 -07001801 }
1802
Minyue Lic6ff7572018-05-04 09:46:44 +02001803 return neteq_stats;
1804}
1805
Minyue Lic97933f2018-08-10 12:51:15 +02001806// Given a NetEqStatsGetter and the SSRC that the NetEqStatsGetter was created
1807// for, this method generates a plot for the jitter buffer delay profile.
Minyue Lic6ff7572018-05-04 09:46:44 +02001808void EventLogAnalyzer::CreateAudioJitterBufferGraph(
Minyue Li01d2a672018-06-21 21:17:19 +02001809 uint32_t ssrc,
1810 const test::NetEqStatsGetter* stats_getter,
Minyue Lic6ff7572018-05-04 09:46:44 +02001811 Plot* plot) const {
Minyue Li45fc6df2018-06-21 11:47:14 +02001812 test::NetEqDelayAnalyzer::Delays arrival_delay_ms;
1813 test::NetEqDelayAnalyzer::Delays corrected_arrival_delay_ms;
1814 test::NetEqDelayAnalyzer::Delays playout_delay_ms;
1815 test::NetEqDelayAnalyzer::Delays target_delay_ms;
1816
Minyue Li01d2a672018-06-21 21:17:19 +02001817 stats_getter->delay_analyzer()->CreateGraphs(
Minyue Li45fc6df2018-06-21 11:47:14 +02001818 &arrival_delay_ms, &corrected_arrival_delay_ms, &playout_delay_ms,
1819 &target_delay_ms);
henrik.lundin3c938fc2017-06-14 06:09:58 -07001820
Minyue Lic97933f2018-08-10 12:51:15 +02001821 TimeSeries time_series_packet_arrival("packet arrival delay",
1822 LineStyle::kLine);
1823 TimeSeries time_series_relative_packet_arrival(
1824 "Relative packet arrival delay", LineStyle::kLine);
1825 TimeSeries time_series_play_time("Playout delay", LineStyle::kLine);
1826 TimeSeries time_series_target_time("Target delay", LineStyle::kLine,
1827 PointStyle::kHighlight);
Minyue Li45fc6df2018-06-21 11:47:14 +02001828
1829 for (const auto& data : arrival_delay_ms) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001830 const float x = config_.GetCallTimeSec(data.first * 1000); // ms to us.
Minyue Li45fc6df2018-06-21 11:47:14 +02001831 const float y = data.second;
Minyue Lic97933f2018-08-10 12:51:15 +02001832 time_series_packet_arrival.points.emplace_back(TimeSeriesPoint(x, y));
Minyue Li45fc6df2018-06-21 11:47:14 +02001833 }
1834 for (const auto& data : corrected_arrival_delay_ms) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001835 const float x = config_.GetCallTimeSec(data.first * 1000); // ms to us.
Minyue Li45fc6df2018-06-21 11:47:14 +02001836 const float y = data.second;
Minyue Lic97933f2018-08-10 12:51:15 +02001837 time_series_relative_packet_arrival.points.emplace_back(
Minyue Li45fc6df2018-06-21 11:47:14 +02001838 TimeSeriesPoint(x, y));
1839 }
1840 for (const auto& data : playout_delay_ms) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001841 const float x = config_.GetCallTimeSec(data.first * 1000); // ms to us.
Minyue Li45fc6df2018-06-21 11:47:14 +02001842 const float y = data.second;
Minyue Lic97933f2018-08-10 12:51:15 +02001843 time_series_play_time.points.emplace_back(TimeSeriesPoint(x, y));
Minyue Li45fc6df2018-06-21 11:47:14 +02001844 }
1845 for (const auto& data : target_delay_ms) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001846 const float x = config_.GetCallTimeSec(data.first * 1000); // ms to us.
Minyue Li45fc6df2018-06-21 11:47:14 +02001847 const float y = data.second;
Minyue Lic97933f2018-08-10 12:51:15 +02001848 time_series_target_time.points.emplace_back(TimeSeriesPoint(x, y));
henrik.lundin3c938fc2017-06-14 06:09:58 -07001849 }
1850
Minyue Lic97933f2018-08-10 12:51:15 +02001851 plot->AppendTimeSeries(std::move(time_series_packet_arrival));
1852 plot->AppendTimeSeries(std::move(time_series_relative_packet_arrival));
1853 plot->AppendTimeSeries(std::move(time_series_play_time));
1854 plot->AppendTimeSeries(std::move(time_series_target_time));
henrik.lundin3c938fc2017-06-14 06:09:58 -07001855
Bjorn Terelius068fc352019-02-13 22:38:25 +01001856 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1857 "Time (s)", kLeftMargin, kRightMargin);
Minyue Li45fc6df2018-06-21 11:47:14 +02001858 plot->SetSuggestedYAxis(0, 1, "Relative delay (ms)", kBottomMargin,
1859 kTopMargin);
Minyue Li27e2b7d2018-05-07 15:20:24 +02001860 plot->SetTitle("NetEq timing for " + GetStreamName(kIncomingPacket, ssrc));
1861}
1862
Minyue Lic9ac93f2018-06-26 13:01:32 +02001863template <typename NetEqStatsType>
1864void EventLogAnalyzer::CreateNetEqStatsGraphInternal(
Minyue Li27e2b7d2018-05-07 15:20:24 +02001865 const NetEqStatsGetterMap& neteq_stats,
Minyue Lic9ac93f2018-06-26 13:01:32 +02001866 rtc::FunctionView<const std::vector<std::pair<int64_t, NetEqStatsType>>*(
1867 const test::NetEqStatsGetter*)> data_extractor,
1868 rtc::FunctionView<float(const NetEqStatsType&)> stats_extractor,
Minyue Li27e2b7d2018-05-07 15:20:24 +02001869 const std::string& plot_name,
1870 Plot* plot) const {
Minyue Li27e2b7d2018-05-07 15:20:24 +02001871 std::map<uint32_t, TimeSeries> time_series;
Minyue Li27e2b7d2018-05-07 15:20:24 +02001872
1873 for (const auto& st : neteq_stats) {
1874 const uint32_t ssrc = st.first;
Minyue Lic9ac93f2018-06-26 13:01:32 +02001875 const std::vector<std::pair<int64_t, NetEqStatsType>>* data_vector =
1876 data_extractor(st.second.get());
1877 for (const auto& data : *data_vector) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01001878 const float time =
1879 config_.GetCallTimeSec(data.first * 1000); // ms to us.
Minyue Lic9ac93f2018-06-26 13:01:32 +02001880 const float value = stats_extractor(data.second);
Minyue Li27e2b7d2018-05-07 15:20:24 +02001881 time_series[ssrc].points.emplace_back(TimeSeriesPoint(time, value));
Minyue Li27e2b7d2018-05-07 15:20:24 +02001882 }
1883 }
1884
1885 for (auto& series : time_series) {
1886 series.second.label = GetStreamName(kIncomingPacket, series.first);
1887 series.second.line_style = LineStyle::kLine;
1888 plot->AppendTimeSeries(std::move(series.second));
1889 }
1890
Bjorn Terelius068fc352019-02-13 22:38:25 +01001891 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1892 "Time (s)", kLeftMargin, kRightMargin);
Minyue Li45fc6df2018-06-21 11:47:14 +02001893 plot->SetSuggestedYAxis(0, 1, plot_name, kBottomMargin, kTopMargin);
Minyue Li27e2b7d2018-05-07 15:20:24 +02001894 plot->SetTitle(plot_name);
henrik.lundin3c938fc2017-06-14 06:09:58 -07001895}
Bjorn Terelius2eb31882017-11-30 15:15:25 +01001896
Minyue Lic9ac93f2018-06-26 13:01:32 +02001897void EventLogAnalyzer::CreateNetEqNetworkStatsGraph(
1898 const NetEqStatsGetterMap& neteq_stats,
1899 rtc::FunctionView<float(const NetEqNetworkStatistics&)> stats_extractor,
1900 const std::string& plot_name,
1901 Plot* plot) const {
1902 CreateNetEqStatsGraphInternal<NetEqNetworkStatistics>(
1903 neteq_stats,
1904 [](const test::NetEqStatsGetter* stats_getter) {
1905 return stats_getter->stats();
1906 },
1907 stats_extractor, plot_name, plot);
1908}
1909
1910void EventLogAnalyzer::CreateNetEqLifetimeStatsGraph(
1911 const NetEqStatsGetterMap& neteq_stats,
1912 rtc::FunctionView<float(const NetEqLifetimeStatistics&)> stats_extractor,
1913 const std::string& plot_name,
1914 Plot* plot) const {
1915 CreateNetEqStatsGraphInternal<NetEqLifetimeStatistics>(
1916 neteq_stats,
1917 [](const test::NetEqStatsGetter* stats_getter) {
1918 return stats_getter->lifetime_stats();
1919 },
1920 stats_extractor, plot_name, plot);
1921}
1922
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001923void EventLogAnalyzer::CreateIceCandidatePairConfigGraph(Plot* plot) {
1924 std::map<uint32_t, TimeSeries> configs_by_cp_id;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001925 for (const auto& config : parsed_log_.ice_candidate_pair_configs()) {
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001926 if (configs_by_cp_id.find(config.candidate_pair_id) ==
1927 configs_by_cp_id.end()) {
1928 const std::string candidate_pair_desc =
1929 GetCandidatePairLogDescriptionAsString(config);
Qingsi Wang93a84392018-01-30 17:13:09 -08001930 configs_by_cp_id[config.candidate_pair_id] =
1931 TimeSeries("[" + std::to_string(config.candidate_pair_id) + "]" +
1932 candidate_pair_desc,
1933 LineStyle::kNone, PointStyle::kHighlight);
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001934 candidate_pair_desc_by_id_[config.candidate_pair_id] =
1935 candidate_pair_desc;
1936 }
Bjorn Terelius068fc352019-02-13 22:38:25 +01001937 float x = config_.GetCallTimeSec(config.log_time_us());
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001938 float y = static_cast<float>(config.type);
1939 configs_by_cp_id[config.candidate_pair_id].points.emplace_back(x, y);
1940 }
1941
1942 // TODO(qingsi): There can be a large number of candidate pairs generated by
1943 // certain calls and the frontend cannot render the chart in this case due to
1944 // the failure of generating a palette with the same number of colors.
1945 for (auto& kv : configs_by_cp_id) {
1946 plot->AppendTimeSeries(std::move(kv.second));
1947 }
1948
Bjorn Terelius068fc352019-02-13 22:38:25 +01001949 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1950 "Time (s)", kLeftMargin, kRightMargin);
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001951 plot->SetSuggestedYAxis(0, 3, "Numeric Config Type", kBottomMargin,
1952 kTopMargin);
1953 plot->SetTitle("[IceEventLog] ICE candidate pair configs");
1954}
1955
1956std::string EventLogAnalyzer::GetCandidatePairLogDescriptionFromId(
1957 uint32_t candidate_pair_id) {
1958 if (candidate_pair_desc_by_id_.find(candidate_pair_id) !=
1959 candidate_pair_desc_by_id_.end()) {
1960 return candidate_pair_desc_by_id_[candidate_pair_id];
1961 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001962 for (const auto& config : parsed_log_.ice_candidate_pair_configs()) {
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001963 // TODO(qingsi): Add the handling of the "Updated" config event after the
1964 // visualization of property change for candidate pairs is introduced.
1965 if (candidate_pair_desc_by_id_.find(config.candidate_pair_id) ==
1966 candidate_pair_desc_by_id_.end()) {
1967 const std::string candidate_pair_desc =
1968 GetCandidatePairLogDescriptionAsString(config);
1969 candidate_pair_desc_by_id_[config.candidate_pair_id] =
1970 candidate_pair_desc;
1971 }
1972 }
1973 return candidate_pair_desc_by_id_[candidate_pair_id];
1974}
1975
1976void EventLogAnalyzer::CreateIceConnectivityCheckGraph(Plot* plot) {
1977 std::map<uint32_t, TimeSeries> checks_by_cp_id;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001978 for (const auto& event : parsed_log_.ice_candidate_pair_events()) {
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001979 if (checks_by_cp_id.find(event.candidate_pair_id) ==
1980 checks_by_cp_id.end()) {
1981 checks_by_cp_id[event.candidate_pair_id] = TimeSeries(
Qingsi Wang93a84392018-01-30 17:13:09 -08001982 "[" + std::to_string(event.candidate_pair_id) + "]" +
1983 GetCandidatePairLogDescriptionFromId(event.candidate_pair_id),
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001984 LineStyle::kNone, PointStyle::kHighlight);
1985 }
Bjorn Terelius068fc352019-02-13 22:38:25 +01001986 float x = config_.GetCallTimeSec(event.log_time_us());
Zach Stein6353fbc2018-12-03 14:35:59 -08001987 constexpr int kIceCandidatePairEventTypeOffset =
1988 static_cast<int>(IceCandidatePairConfigType::kNumValues);
1989 float y = static_cast<float>(event.type) + kIceCandidatePairEventTypeOffset;
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001990 checks_by_cp_id[event.candidate_pair_id].points.emplace_back(x, y);
1991 }
1992
1993 // TODO(qingsi): The same issue as in CreateIceCandidatePairConfigGraph.
1994 for (auto& kv : checks_by_cp_id) {
1995 plot->AppendTimeSeries(std::move(kv.second));
1996 }
1997
Bjorn Terelius068fc352019-02-13 22:38:25 +01001998 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
1999 "Time (s)", kLeftMargin, kRightMargin);
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08002000 plot->SetSuggestedYAxis(0, 4, "Numeric Connectivity State", kBottomMargin,
2001 kTopMargin);
2002 plot->SetTitle("[IceEventLog] ICE connectivity checks");
2003}
2004
Zach Stein10a58012018-12-07 12:26:28 -08002005void EventLogAnalyzer::CreateDtlsTransportStateGraph(Plot* plot) {
2006 TimeSeries states("DTLS Transport State", LineStyle::kNone,
2007 PointStyle::kHighlight);
2008 for (const auto& event : parsed_log_.dtls_transport_states()) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01002009 float x = config_.GetCallTimeSec(event.log_time_us());
Zach Stein10a58012018-12-07 12:26:28 -08002010 float y = static_cast<float>(event.dtls_transport_state);
2011 states.points.emplace_back(x, y);
2012 }
2013 plot->AppendTimeSeries(std::move(states));
Bjorn Terelius068fc352019-02-13 22:38:25 +01002014 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
2015 "Time (s)", kLeftMargin, kRightMargin);
Zach Stein10a58012018-12-07 12:26:28 -08002016 plot->SetSuggestedYAxis(0, static_cast<float>(DtlsTransportState::kNumValues),
2017 "Numeric Transport State", kBottomMargin, kTopMargin);
2018 plot->SetTitle("DTLS Transport State");
2019}
2020
2021void EventLogAnalyzer::CreateDtlsWritableStateGraph(Plot* plot) {
2022 TimeSeries writable("DTLS Writable", LineStyle::kNone,
2023 PointStyle::kHighlight);
2024 for (const auto& event : parsed_log_.dtls_writable_states()) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01002025 float x = config_.GetCallTimeSec(event.log_time_us());
Zach Stein10a58012018-12-07 12:26:28 -08002026 float y = static_cast<float>(event.writable);
2027 writable.points.emplace_back(x, y);
2028 }
2029 plot->AppendTimeSeries(std::move(writable));
Bjorn Terelius068fc352019-02-13 22:38:25 +01002030 plot->SetXAxis(config_.CallBeginTimeSec(), config_.CallEndTimeSec(),
2031 "Time (s)", kLeftMargin, kRightMargin);
Zach Stein10a58012018-12-07 12:26:28 -08002032 plot->SetSuggestedYAxis(0, 1, "Writable", kBottomMargin, kTopMargin);
2033 plot->SetTitle("DTLS Writable State");
2034}
2035
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002036void EventLogAnalyzer::PrintNotifications(FILE* file) {
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002037 fprintf(file, "========== TRIAGE NOTIFICATIONS ==========\n");
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002038 for (const auto& alert : incoming_rtp_recv_time_gaps_) {
2039 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
2040 }
2041 for (const auto& alert : incoming_rtcp_recv_time_gaps_) {
2042 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
2043 }
2044 for (const auto& alert : outgoing_rtp_send_time_gaps_) {
2045 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
2046 }
2047 for (const auto& alert : outgoing_rtcp_send_time_gaps_) {
2048 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
2049 }
2050 for (const auto& alert : incoming_seq_num_jumps_) {
2051 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
2052 }
2053 for (const auto& alert : incoming_capture_time_jumps_) {
2054 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
2055 }
2056 for (const auto& alert : outgoing_seq_num_jumps_) {
2057 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
2058 }
2059 for (const auto& alert : outgoing_capture_time_jumps_) {
2060 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
2061 }
2062 for (const auto& alert : outgoing_high_loss_alerts_) {
2063 fprintf(file, " : %s\n", alert.ToString().c_str());
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002064 }
2065 fprintf(file, "========== END TRIAGE NOTIFICATIONS ==========\n");
2066}
2067
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002068void EventLogAnalyzer::CreateStreamGapAlerts(PacketDirection direction) {
2069 // With 100 packets/s (~800kbps), false positives would require 10 s without
2070 // data.
2071 constexpr int64_t kMaxSeqNumJump = 1000;
2072 // With a 90 kHz clock, false positives would require 10 s without data.
2073 constexpr int64_t kMaxCaptureTimeJump = 900000;
2074
2075 int64_t end_time_us = log_segments_.empty()
2076 ? std::numeric_limits<int64_t>::max()
2077 : log_segments_.front().second;
2078
2079 SeqNumUnwrapper<uint16_t> seq_num_unwrapper;
Danil Chapovalov431abd92018-06-18 12:54:17 +02002080 absl::optional<int64_t> last_seq_num;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002081 SeqNumUnwrapper<uint32_t> capture_time_unwrapper;
Danil Chapovalov431abd92018-06-18 12:54:17 +02002082 absl::optional<int64_t> last_capture_time;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002083 // Check for gaps in sequence numbers and capture timestamps.
2084 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
2085 for (const auto& packet : stream.packet_view) {
2086 if (packet.log_time_us() > end_time_us) {
2087 // Only process the first (LOG_START, LOG_END) segment.
2088 break;
2089 }
2090
2091 int64_t seq_num = seq_num_unwrapper.Unwrap(packet.header.sequenceNumber);
2092 if (last_seq_num.has_value() &&
2093 std::abs(seq_num - last_seq_num.value()) > kMaxSeqNumJump) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01002094 Alert_SeqNumJump(direction,
2095 config_.GetCallTimeSec(packet.log_time_us()),
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002096 packet.header.ssrc);
2097 }
2098 last_seq_num.emplace(seq_num);
2099
2100 int64_t capture_time =
2101 capture_time_unwrapper.Unwrap(packet.header.timestamp);
2102 if (last_capture_time.has_value() &&
2103 std::abs(capture_time - last_capture_time.value()) >
2104 kMaxCaptureTimeJump) {
Bjorn Terelius068fc352019-02-13 22:38:25 +01002105 Alert_CaptureTimeJump(direction,
2106 config_.GetCallTimeSec(packet.log_time_us()),
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002107 packet.header.ssrc);
2108 }
2109 last_capture_time.emplace(capture_time);
2110 }
2111 }
2112}
2113
2114void EventLogAnalyzer::CreateTransmissionGapAlerts(PacketDirection direction) {
2115 constexpr int64_t kMaxRtpTransmissionGap = 500000;
2116 constexpr int64_t kMaxRtcpTransmissionGap = 2000000;
2117 int64_t end_time_us = log_segments_.empty()
2118 ? std::numeric_limits<int64_t>::max()
2119 : log_segments_.front().second;
2120
2121 // TODO(terelius): The parser could provide a list of all packets, ordered
2122 // by time, for each direction.
2123 std::multimap<int64_t, const LoggedRtpPacket*> rtp_in_direction;
2124 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
2125 for (const LoggedRtpPacket& rtp_packet : stream.packet_view)
2126 rtp_in_direction.emplace(rtp_packet.log_time_us(), &rtp_packet);
2127 }
Danil Chapovalov431abd92018-06-18 12:54:17 +02002128 absl::optional<int64_t> last_rtp_time;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002129 for (const auto& kv : rtp_in_direction) {
2130 int64_t timestamp = kv.first;
2131 if (timestamp > end_time_us) {
2132 // Only process the first (LOG_START, LOG_END) segment.
2133 break;
2134 }
2135 int64_t duration = timestamp - last_rtp_time.value_or(0);
2136 if (last_rtp_time.has_value() && duration > kMaxRtpTransmissionGap) {
2137 // No packet sent/received for more than 500 ms.
Bjorn Terelius068fc352019-02-13 22:38:25 +01002138 Alert_RtpLogTimeGap(direction, config_.GetCallTimeSec(timestamp),
2139 duration / 1000);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002140 }
2141 last_rtp_time.emplace(timestamp);
2142 }
2143
Danil Chapovalov431abd92018-06-18 12:54:17 +02002144 absl::optional<int64_t> last_rtcp_time;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002145 if (direction == kIncomingPacket) {
2146 for (const auto& rtcp : parsed_log_.incoming_rtcp_packets()) {
2147 if (rtcp.log_time_us() > end_time_us) {
2148 // Only process the first (LOG_START, LOG_END) segment.
2149 break;
2150 }
2151 int64_t duration = rtcp.log_time_us() - last_rtcp_time.value_or(0);
2152 if (last_rtcp_time.has_value() && duration > kMaxRtcpTransmissionGap) {
2153 // No feedback sent/received for more than 2000 ms.
Bjorn Terelius068fc352019-02-13 22:38:25 +01002154 Alert_RtcpLogTimeGap(direction,
2155 config_.GetCallTimeSec(rtcp.log_time_us()),
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002156 duration / 1000);
2157 }
2158 last_rtcp_time.emplace(rtcp.log_time_us());
2159 }
2160 } else {
2161 for (const auto& rtcp : parsed_log_.outgoing_rtcp_packets()) {
2162 if (rtcp.log_time_us() > end_time_us) {
2163 // Only process the first (LOG_START, LOG_END) segment.
2164 break;
2165 }
2166 int64_t duration = rtcp.log_time_us() - last_rtcp_time.value_or(0);
2167 if (last_rtcp_time.has_value() && duration > kMaxRtcpTransmissionGap) {
2168 // No feedback sent/received for more than 2000 ms.
Bjorn Terelius068fc352019-02-13 22:38:25 +01002169 Alert_RtcpLogTimeGap(direction,
2170 config_.GetCallTimeSec(rtcp.log_time_us()),
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002171 duration / 1000);
2172 }
2173 last_rtcp_time.emplace(rtcp.log_time_us());
2174 }
2175 }
2176}
2177
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002178// TODO(terelius): Notifications could possibly be generated by the same code
2179// that produces the graphs. There is some code duplication that could be
2180// avoided, but that might be solved anyway when we move functionality from the
2181// analyzer to the parser.
2182void EventLogAnalyzer::CreateTriageNotifications() {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002183 CreateStreamGapAlerts(kIncomingPacket);
2184 CreateStreamGapAlerts(kOutgoingPacket);
2185 CreateTransmissionGapAlerts(kIncomingPacket);
2186 CreateTransmissionGapAlerts(kOutgoingPacket);
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002187
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002188 int64_t end_time_us = log_segments_.empty()
2189 ? std::numeric_limits<int64_t>::max()
2190 : log_segments_.front().second;
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002191
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002192 constexpr double kMaxLossFraction = 0.05;
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002193 // Loss feedback
2194 int64_t total_lost_packets = 0;
2195 int64_t total_expected_packets = 0;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002196 for (auto& bwe_update : parsed_log_.bwe_loss_updates()) {
2197 if (bwe_update.log_time_us() > end_time_us) {
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002198 // Only process the first (LOG_START, LOG_END) segment.
2199 break;
2200 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002201 int64_t lost_packets = static_cast<double>(bwe_update.fraction_lost) / 255 *
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002202 bwe_update.expected_packets;
2203 total_lost_packets += lost_packets;
2204 total_expected_packets += bwe_update.expected_packets;
2205 }
2206 double avg_outgoing_loss =
2207 static_cast<double>(total_lost_packets) / total_expected_packets;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002208 if (avg_outgoing_loss > kMaxLossFraction) {
2209 Alert_OutgoingHighLoss(avg_outgoing_loss);
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002210 }
2211}
2212
terelius54ce6802016-07-13 06:44:41 -07002213} // namespace webrtc