blob: 89cf8905916496848ad6f9e89b1a1d9c451ba426 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "call/audio_receive_stream.h"
21#include "call/audio_send_stream.h"
22#include "call/call.h"
23#include "call/video_receive_stream.h"
24#include "call/video_send_stream.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020025#include "common_types.h" // NOLINT(build/include)
Elad Alon99a81b62017-09-21 10:25:29 +020026#include "logging/rtc_event_log/rtc_stream_config.h"
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +020027#include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_coding/neteq/tools/audio_sink.h"
29#include "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
30#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
31#include "modules/audio_coding/neteq/tools/neteq_replacement_input.h"
32#include "modules/audio_coding/neteq/tools/neteq_test.h"
33#include "modules/audio_coding/neteq/tools/resample_input_audio_file.h"
Danil Chapovalovbda50682018-02-14 09:08:28 +000034#include "modules/congestion_controller/acknowledged_bitrate_estimator.h"
35#include "modules/congestion_controller/bitrate_estimator.h"
Sebastian Jansson439f0bc2018-02-20 10:46:39 +010036#include "modules/congestion_controller/delay_based_bwe.h"
Bjorn Terelius28db2662017-10-04 14:22:43 +020037#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "modules/congestion_controller/include/send_side_congestion_controller.h"
39#include "modules/include/module_common_types.h"
Niels Möllerfd6c0912017-10-31 10:19:10 +010040#include "modules/pacing/packet_router.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "modules/rtp_rtcp/include/rtp_rtcp.h"
42#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +020043#include "modules/rtp_rtcp/source/rtcp_packet.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
45#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
46#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
47#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
48#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
49#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
50#include "modules/rtp_rtcp/source/rtp_utility.h"
51#include "rtc_base/checks.h"
52#include "rtc_base/format_macros.h"
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +020053#include "rtc_base/function_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "rtc_base/logging.h"
Bjorn Terelius0295a962017-10-25 17:42:41 +020055#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020056#include "rtc_base/ptr_util.h"
57#include "rtc_base/rate_statistics.h"
terelius54ce6802016-07-13 06:44:41 -070058
Bjorn Terelius6984ad22017-10-24 12:19:45 +020059#ifndef BWE_TEST_LOGGING_COMPILE_TIME_ENABLE
60#define BWE_TEST_LOGGING_COMPILE_TIME_ENABLE 0
61#endif // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE
62
tereliusdc35dcd2016-08-01 12:03:27 -070063namespace webrtc {
tereliusdc35dcd2016-08-01 12:03:27 -070064
terelius54ce6802016-07-13 06:44:41 -070065namespace {
66
Qingsi Wang8eca1ff2018-02-02 11:49:44 -080067const int kNumMicrosecsPerSec = 1000000;
68
elad.alonec304f92017-03-08 05:03:53 -080069void SortPacketFeedbackVector(std::vector<PacketFeedback>* vec) {
70 auto pred = [](const PacketFeedback& packet_feedback) {
71 return packet_feedback.arrival_time_ms == PacketFeedback::kNotReceived;
72 };
73 vec->erase(std::remove_if(vec->begin(), vec->end(), pred), vec->end());
74 std::sort(vec->begin(), vec->end(), PacketFeedbackComparator());
75}
76
terelius54ce6802016-07-13 06:44:41 -070077std::string SsrcToString(uint32_t ssrc) {
78 std::stringstream ss;
79 ss << "SSRC " << ssrc;
80 return ss.str();
81}
82
83// Checks whether an SSRC is contained in the list of desired SSRCs.
84// Note that an empty SSRC list matches every SSRC.
85bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
86 if (desired_ssrc.size() == 0)
87 return true;
88 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
89 desired_ssrc.end();
90}
91
92double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
93 // The timestamp is a fixed point representation with 6 bits for seconds
94 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
Qingsi Wang8eca1ff2018-02-02 11:49:44 -080095 // time in seconds and then multiply by kNumMicrosecsPerSec to convert to
96 // microseconds.
terelius54ce6802016-07-13 06:44:41 -070097 static constexpr double kTimestampToMicroSec =
Qingsi Wang8eca1ff2018-02-02 11:49:44 -080098 static_cast<double>(kNumMicrosecsPerSec) / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -070099 return abs_send_time * kTimestampToMicroSec;
100}
101
102// Computes the difference |later| - |earlier| where |later| and |earlier|
103// are counters that wrap at |modulus|. The difference is chosen to have the
104// least absolute value. For example if |modulus| is 8, then the difference will
105// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
106// be in [-4, 4].
107int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
108 RTC_DCHECK_LE(1, modulus);
109 RTC_DCHECK_LT(later, modulus);
110 RTC_DCHECK_LT(earlier, modulus);
111 int64_t difference =
112 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
113 int64_t max_difference = modulus / 2;
114 int64_t min_difference = max_difference - modulus + 1;
115 if (difference > max_difference) {
116 difference -= modulus;
117 }
118 if (difference < min_difference) {
119 difference += modulus;
120 }
terelius6addf492016-08-23 17:34:07 -0700121 if (difference > max_difference / 2 || difference < min_difference / 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100122 RTC_LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
123 << " expected to be in the range ("
124 << min_difference / 2 << "," << max_difference / 2
125 << ") but is " << difference
126 << ". Correct unwrapping is uncertain.";
terelius6addf492016-08-23 17:34:07 -0700127 }
terelius54ce6802016-07-13 06:44:41 -0700128 return difference;
129}
130
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200131// This is much more reliable for outgoing streams than for incoming streams.
132template <typename RtpPacketContainer>
133rtc::Optional<uint32_t> EstimateRtpClockFrequency(
134 const RtpPacketContainer& packets,
135 int64_t end_time_us) {
136 RTC_CHECK(packets.size() >= 2);
137 SeqNumUnwrapper<uint32_t> unwrapper;
138 uint64_t first_rtp_timestamp =
139 unwrapper.Unwrap(packets[0].rtp.header.timestamp);
140 int64_t first_log_timestamp = packets[0].log_time_us();
141 uint64_t last_rtp_timestamp = first_rtp_timestamp;
142 int64_t last_log_timestamp = first_log_timestamp;
143 for (size_t i = 1; i < packets.size(); i++) {
144 if (packets[i].log_time_us() > end_time_us)
145 break;
146 last_rtp_timestamp = unwrapper.Unwrap(packets[i].rtp.header.timestamp);
147 last_log_timestamp = packets[i].log_time_us();
148 }
149 if (last_log_timestamp - first_log_timestamp < kNumMicrosecsPerSec) {
150 RTC_LOG(LS_WARNING)
151 << "Failed to estimate RTP clock frequency: Stream too short. ("
152 << packets.size() << " packets, "
153 << last_log_timestamp - first_log_timestamp << " us)";
154 return rtc::nullopt;
155 }
156 double duration =
157 static_cast<double>(last_log_timestamp - first_log_timestamp) /
158 kNumMicrosecsPerSec;
159 double estimated_frequency =
160 (last_rtp_timestamp - first_rtp_timestamp) / duration;
161 for (uint32_t f : {8000, 16000, 32000, 48000, 90000}) {
162 if (std::fabs(estimated_frequency - f) < 0.05 * f) {
163 return f;
164 }
165 }
166 RTC_LOG(LS_WARNING) << "Failed to estimate RTP clock frequency: Estimate "
167 << estimated_frequency
168 << "not close to any stardard RTP frequency.";
169 return rtc::nullopt;
ivocaac9d6f2016-09-22 07:01:47 -0700170}
171
tereliusdc35dcd2016-08-01 12:03:27 -0700172constexpr float kLeftMargin = 0.01f;
173constexpr float kRightMargin = 0.02f;
174constexpr float kBottomMargin = 0.02f;
175constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700176
terelius53dc23c2017-03-13 05:24:05 -0700177rtc::Optional<double> NetworkDelayDiff_AbsSendTime(
178 const LoggedRtpPacket& old_packet,
179 const LoggedRtpPacket& new_packet) {
180 if (old_packet.header.extension.hasAbsoluteSendTime &&
181 new_packet.header.extension.hasAbsoluteSendTime) {
182 int64_t send_time_diff = WrappingDifference(
183 new_packet.header.extension.absoluteSendTime,
184 old_packet.header.extension.absoluteSendTime, 1ul << 24);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200185 int64_t recv_time_diff =
186 new_packet.log_time_us() - old_packet.log_time_us();
terelius53dc23c2017-03-13 05:24:05 -0700187 double delay_change_us =
188 recv_time_diff - AbsSendTimeToMicroseconds(send_time_diff);
Oskar Sundbom3928dbc2017-11-16 10:53:09 +0100189 return delay_change_us / 1000;
terelius53dc23c2017-03-13 05:24:05 -0700190 } else {
Oskar Sundbom3928dbc2017-11-16 10:53:09 +0100191 return rtc::nullopt;
terelius6addf492016-08-23 17:34:07 -0700192 }
193}
194
terelius53dc23c2017-03-13 05:24:05 -0700195rtc::Optional<double> NetworkDelayDiff_CaptureTime(
196 const LoggedRtpPacket& old_packet,
197 const LoggedRtpPacket& new_packet) {
198 int64_t send_time_diff = WrappingDifference(
199 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200200 int64_t recv_time_diff = new_packet.log_time_us() - old_packet.log_time_us();
terelius53dc23c2017-03-13 05:24:05 -0700201
202 const double kVideoSampleRate = 90000;
203 // TODO(terelius): We treat all streams as video for now, even though
204 // audio might be sampled at e.g. 16kHz, because it is really difficult to
205 // figure out the true sampling rate of a stream. The effect is that the
206 // delay will be scaled incorrectly for non-video streams.
207
208 double delay_change =
209 static_cast<double>(recv_time_diff) / 1000 -
210 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
211 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?";
213 RTC_LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200214 << ", received time " << old_packet.log_time_us();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100215 RTC_LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200216 << ", received time " << new_packet.log_time_us();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100217 RTC_LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800218 << static_cast<double>(recv_time_diff) /
219 kNumMicrosecsPerSec
220 << "s";
Mirko Bonadei675513b2017-11-09 11:09:25 +0100221 RTC_LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
222 << static_cast<double>(send_time_diff) /
223 kVideoSampleRate
224 << "s";
terelius53dc23c2017-03-13 05:24:05 -0700225 }
Oskar Sundbom3928dbc2017-11-16 10:53:09 +0100226 return delay_change;
terelius53dc23c2017-03-13 05:24:05 -0700227}
228
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200229// For each element in data_view, use |f()| to extract a y-coordinate and
terelius53dc23c2017-03-13 05:24:05 -0700230// store the result in a TimeSeries.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200231template <typename DataType, typename IterableType>
232void ProcessPoints(rtc::FunctionView<rtc::Optional<float>(const DataType&)> f,
233 const IterableType& data_view,
234 int64_t begin_time,
235 TimeSeries* result) {
236 for (size_t i = 0; i < data_view.size(); i++) {
237 const DataType& elem = data_view[i];
238 float x = static_cast<float>(elem.log_time_us() - begin_time) /
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800239 kNumMicrosecsPerSec;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200240 rtc::Optional<float> y = f(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(
251 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200252 const DataType&)> f,
253 const IterableType& data,
254 int64_t begin_time,
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++) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200257 float x = static_cast<float>(data[i].log_time_us() - begin_time) /
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800258 kNumMicrosecsPerSec;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200259 rtc::Optional<ResultType> y = f(data[i - 1], data[i]);
terelius53dc23c2017-03-13 05:24:05 -0700260 if (y)
261 result->points.emplace_back(x, static_cast<float>(*y));
262 }
263}
264
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200265// For each element in data, use |f()| to extract a y-coordinate and
terelius53dc23c2017-03-13 05:24:05 -0700266// store the result in a TimeSeries.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200267template <typename DataType, typename ResultType, typename IterableType>
terelius53dc23c2017-03-13 05:24:05 -0700268void AccumulatePoints(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200269 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> f,
270 const IterableType& data,
271 int64_t begin_time,
terelius53dc23c2017-03-13 05:24:05 -0700272 TimeSeries* result) {
273 ResultType sum = 0;
274 for (size_t i = 0; i < data.size(); i++) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200275 float x = static_cast<float>(data[i].log_time_us() - begin_time) /
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800276 kNumMicrosecsPerSec;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200277 rtc::Optional<ResultType> y = f(data[i]);
terelius53dc23c2017-03-13 05:24:05 -0700278 if (y) {
279 sum += *y;
280 result->points.emplace_back(x, static_cast<float>(sum));
281 }
282 }
283}
284
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200285// For each pair of adjacent elements in |data|, use |f()| to extract a
terelius53dc23c2017-03-13 05:24:05 -0700286// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
287// will be the time of the second element in the pair.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200288template <typename DataType, typename ResultType, typename IterableType>
terelius53dc23c2017-03-13 05:24:05 -0700289void AccumulatePairs(
290 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200291 const DataType&)> f,
292 const IterableType& data,
293 int64_t begin_time,
terelius53dc23c2017-03-13 05:24:05 -0700294 TimeSeries* result) {
295 ResultType sum = 0;
296 for (size_t i = 1; i < data.size(); i++) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200297 float x = static_cast<float>(data[i].log_time_us() - begin_time) /
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800298 kNumMicrosecsPerSec;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200299 rtc::Optional<ResultType> y = f(data[i - 1], data[i]);
terelius53dc23c2017-03-13 05:24:05 -0700300 if (y)
301 sum += *y;
302 result->points.emplace_back(x, static_cast<float>(sum));
tereliusccbbf8d2016-08-10 07:34:28 -0700303 }
304}
305
terelius6addf492016-08-23 17:34:07 -0700306// Calculates a moving average of |data| and stores the result in a TimeSeries.
307// A data point is generated every |step| microseconds from |begin_time|
308// to |end_time|. The value of each data point is the average of the data
309// during the preceeding |window_duration_us| microseconds.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200310template <typename DataType, typename ResultType, typename IterableType>
terelius53dc23c2017-03-13 05:24:05 -0700311void MovingAverage(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200312 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> f,
313 const IterableType& data_view,
314 int64_t begin_time,
315 int64_t end_time,
316 int64_t window_duration_us,
317 int64_t step,
318 TimeSeries* result) {
terelius6addf492016-08-23 17:34:07 -0700319 size_t window_index_begin = 0;
320 size_t window_index_end = 0;
terelius53dc23c2017-03-13 05:24:05 -0700321 ResultType sum_in_window = 0;
terelius6addf492016-08-23 17:34:07 -0700322
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200323 for (int64_t t = begin_time; t < end_time + step; t += step) {
324 while (window_index_end < data_view.size() &&
325 data_view[window_index_end].log_time_us() < t) {
326 rtc::Optional<ResultType> value = f(data_view[window_index_end]);
terelius53dc23c2017-03-13 05:24:05 -0700327 if (value)
328 sum_in_window += *value;
terelius6addf492016-08-23 17:34:07 -0700329 ++window_index_end;
330 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200331 while (window_index_begin < data_view.size() &&
332 data_view[window_index_begin].log_time_us() <
333 t - window_duration_us) {
334 rtc::Optional<ResultType> value = f(data_view[window_index_begin]);
terelius53dc23c2017-03-13 05:24:05 -0700335 if (value)
336 sum_in_window -= *value;
terelius6addf492016-08-23 17:34:07 -0700337 ++window_index_begin;
338 }
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800339 float window_duration_s =
340 static_cast<float>(window_duration_us) / kNumMicrosecsPerSec;
341 float x = static_cast<float>(t - begin_time) / kNumMicrosecsPerSec;
terelius53dc23c2017-03-13 05:24:05 -0700342 float y = sum_in_window / window_duration_s;
terelius6addf492016-08-23 17:34:07 -0700343 result->points.emplace_back(x, y);
344 }
345}
346
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800347const char kUnknownEnumValue[] = "unknown";
348
349const char kIceCandidateTypeLocal[] = "local";
350const char kIceCandidateTypeStun[] = "stun";
351const char kIceCandidateTypePrflx[] = "prflx";
352const char kIceCandidateTypeRelay[] = "relay";
353
354const char kProtocolUdp[] = "udp";
355const char kProtocolTcp[] = "tcp";
356const char kProtocolSsltcp[] = "ssltcp";
357const char kProtocolTls[] = "tls";
358
359const char kAddressFamilyIpv4[] = "ipv4";
360const char kAddressFamilyIpv6[] = "ipv6";
361
362const char kNetworkTypeEthernet[] = "ethernet";
363const char kNetworkTypeLoopback[] = "loopback";
364const char kNetworkTypeWifi[] = "wifi";
365const char kNetworkTypeVpn[] = "vpn";
366const char kNetworkTypeCellular[] = "cellular";
367
368std::string GetIceCandidateTypeAsString(webrtc::IceCandidateType type) {
369 switch (type) {
370 case webrtc::IceCandidateType::kLocal:
371 return kIceCandidateTypeLocal;
372 case webrtc::IceCandidateType::kStun:
373 return kIceCandidateTypeStun;
374 case webrtc::IceCandidateType::kPrflx:
375 return kIceCandidateTypePrflx;
376 case webrtc::IceCandidateType::kRelay:
377 return kIceCandidateTypeRelay;
378 default:
379 return kUnknownEnumValue;
380 }
381}
382
383std::string GetProtocolAsString(webrtc::IceCandidatePairProtocol protocol) {
384 switch (protocol) {
385 case webrtc::IceCandidatePairProtocol::kUdp:
386 return kProtocolUdp;
387 case webrtc::IceCandidatePairProtocol::kTcp:
388 return kProtocolTcp;
389 case webrtc::IceCandidatePairProtocol::kSsltcp:
390 return kProtocolSsltcp;
391 case webrtc::IceCandidatePairProtocol::kTls:
392 return kProtocolTls;
393 default:
394 return kUnknownEnumValue;
395 }
396}
397
398std::string GetAddressFamilyAsString(
399 webrtc::IceCandidatePairAddressFamily family) {
400 switch (family) {
401 case webrtc::IceCandidatePairAddressFamily::kIpv4:
402 return kAddressFamilyIpv4;
403 case webrtc::IceCandidatePairAddressFamily::kIpv6:
404 return kAddressFamilyIpv6;
405 default:
406 return kUnknownEnumValue;
407 }
408}
409
410std::string GetNetworkTypeAsString(webrtc::IceCandidateNetworkType type) {
411 switch (type) {
412 case webrtc::IceCandidateNetworkType::kEthernet:
413 return kNetworkTypeEthernet;
414 case webrtc::IceCandidateNetworkType::kLoopback:
415 return kNetworkTypeLoopback;
416 case webrtc::IceCandidateNetworkType::kWifi:
417 return kNetworkTypeWifi;
418 case webrtc::IceCandidateNetworkType::kVpn:
419 return kNetworkTypeVpn;
420 case webrtc::IceCandidateNetworkType::kCellular:
421 return kNetworkTypeCellular;
422 default:
423 return kUnknownEnumValue;
424 }
425}
426
427std::string GetCandidatePairLogDescriptionAsString(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200428 const LoggedIceCandidatePairConfig& config) {
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800429 // Example: stun:wifi->relay(tcp):cellular@udp:ipv4
430 // represents a pair of a local server-reflexive candidate on a WiFi network
431 // and a remote relay candidate using TCP as the relay protocol on a cell
432 // network, when the candidate pair communicates over UDP using IPv4.
433 std::stringstream ss;
434 std::string local_candidate_type =
435 GetIceCandidateTypeAsString(config.local_candidate_type);
436 std::string remote_candidate_type =
437 GetIceCandidateTypeAsString(config.remote_candidate_type);
438 if (config.local_candidate_type == webrtc::IceCandidateType::kRelay) {
439 local_candidate_type +=
440 "(" + GetProtocolAsString(config.local_relay_protocol) + ")";
441 }
442 ss << local_candidate_type << ":"
443 << GetNetworkTypeAsString(config.local_network_type) << ":"
444 << GetAddressFamilyAsString(config.local_address_family) << "->"
445 << remote_candidate_type << ":"
446 << GetAddressFamilyAsString(config.remote_address_family) << "@"
447 << GetProtocolAsString(config.candidate_pair_protocol);
448 return ss.str();
449}
450
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200451std::string GetDirectionAsString(PacketDirection direction) {
452 if (direction == kIncomingPacket) {
453 return "Incoming";
454 } else {
455 return "Outgoing";
456 }
457}
458
459std::string GetDirectionAsShortString(PacketDirection direction) {
460 if (direction == kIncomingPacket) {
461 return "In";
462 } else {
463 return "Out";
464 }
465}
466
terelius54ce6802016-07-13 06:44:41 -0700467} // namespace
468
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200469EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLogNew& log)
terelius54ce6802016-07-13 06:44:41 -0700470 : parsed_log_(log), window_duration_(250000), step_(10000) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200471 begin_time_ = parsed_log_.first_timestamp();
472 end_time_ = parsed_log_.last_timestamp();
473 if (end_time_ < begin_time_) {
474 RTC_LOG(LS_WARNING) << "No useful events in the log.";
475 begin_time_ = end_time_ = 0;
Björn Tereliusff612732018-04-25 14:23:01 +0000476 }
Björn Tereliusff612732018-04-25 14:23:01 +0000477 call_duration_s_ = ToCallTime(end_time_);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200478
479 const auto& log_start_events = parsed_log_.start_log_events();
480 const auto& log_end_events = parsed_log_.stop_log_events();
481 auto start_iter = log_start_events.begin();
482 auto end_iter = log_end_events.begin();
483 while (start_iter != log_start_events.end()) {
484 int64_t start = start_iter->log_time_us();
485 ++start_iter;
486 rtc::Optional<int64_t> next_start;
487 if (start_iter != log_start_events.end())
488 next_start.emplace(start_iter->log_time_us());
489 if (end_iter != log_end_events.end() &&
490 end_iter->log_time_us() <=
491 next_start.value_or(std::numeric_limits<int64_t>::max())) {
492 int64_t end = end_iter->log_time_us();
493 RTC_DCHECK_LE(start, end);
494 log_segments_.push_back(std::make_pair(start, end));
495 ++end_iter;
496 } else {
497 // we're missing an end event. Assume that it occurred just before the
498 // next start.
499 log_segments_.push_back(
500 std::make_pair(start, next_start.value_or(end_time_)));
501 }
henrik.lundin3c938fc2017-06-14 06:09:58 -0700502 }
Bjorn Terelius2eb31882017-11-30 15:15:25 +0100503 RTC_LOG(LS_INFO) << "Found " << log_segments_.size()
504 << " (LOG_START, LOG_END) segments in log.";
terelius54ce6802016-07-13 06:44:41 -0700505}
506
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100507class BitrateObserver : public NetworkChangedObserver,
Stefan Holmer13181032016-07-29 14:48:54 +0200508 public RemoteBitrateObserver {
509 public:
510 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
511
512 void OnNetworkChanged(uint32_t bitrate_bps,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200513 uint8_t fraction_lost,
minyue78b4d562016-11-30 04:47:39 -0800514 int64_t rtt_ms,
515 int64_t probing_interval_ms) override {
Stefan Holmer13181032016-07-29 14:48:54 +0200516 last_bitrate_bps_ = bitrate_bps;
517 bitrate_updated_ = true;
518 }
519
520 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
521 uint32_t bitrate) override {}
522
523 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
524 bool GetAndResetBitrateUpdated() {
525 bool bitrate_updated = bitrate_updated_;
526 bitrate_updated_ = false;
527 return bitrate_updated;
528 }
529
530 private:
531 uint32_t last_bitrate_bps_;
532 bool bitrate_updated_;
533};
534
Bjorn Terelius2eb31882017-11-30 15:15:25 +0100535float EventLogAnalyzer::ToCallTime(int64_t timestamp) const {
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800536 return static_cast<float>(timestamp - begin_time_) / kNumMicrosecsPerSec;
Bjorn Terelius2eb31882017-11-30 15:15:25 +0100537}
538
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200539void EventLogAnalyzer::CreatePacketGraph(PacketDirection direction,
terelius54ce6802016-07-13 06:44:41 -0700540 Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200541 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
542 // Filter on SSRC.
543 if (!MatchingSsrc(stream.ssrc, desired_ssrc_)) {
terelius6addf492016-08-23 17:34:07 -0700544 continue;
terelius54ce6802016-07-13 06:44:41 -0700545 }
terelius54ce6802016-07-13 06:44:41 -0700546
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200547 TimeSeries time_series(GetStreamName(direction, stream.ssrc),
548 LineStyle::kBar);
549 auto GetPacketSize = [](const LoggedRtpPacket& packet) {
550 return rtc::Optional<float>(packet.total_length);
551 };
552 ProcessPoints<LoggedRtpPacket>(GetPacketSize, stream.packet_view,
553 begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700554 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700555 }
556
tereliusdc35dcd2016-08-01 12:03:27 -0700557 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
558 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
559 kTopMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200560 plot->SetTitle(GetDirectionAsString(direction) + " RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700561}
562
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200563template <typename IterableType>
philipelccd74892016-09-05 02:46:25 -0700564void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
philipelccd74892016-09-05 02:46:25 -0700565 Plot* plot,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200566 const IterableType& packets,
567 const std::string& label) {
568 TimeSeries time_series(label, LineStyle::kStep);
569 for (size_t i = 0; i < packets.size(); i++) {
570 float x = ToCallTime(packets[i].log_time_us());
571 time_series.points.emplace_back(x, i + 1);
philipelccd74892016-09-05 02:46:25 -0700572 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200573 plot->AppendTimeSeries(std::move(time_series));
philipelccd74892016-09-05 02:46:25 -0700574}
575
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200576void EventLogAnalyzer::CreateAccumulatedPacketsGraph(PacketDirection direction,
577 Plot* plot) {
578 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
579 if (!MatchingSsrc(stream.ssrc, desired_ssrc_))
580 continue;
581 std::string label =
582 std::string("RTP ") + GetStreamName(direction, stream.ssrc);
583 CreateAccumulatedPacketsTimeSeries(plot, stream.packet_view, label);
584 }
585 std::string label =
586 std::string("RTCP ") + "(" + GetDirectionAsShortString(direction) + ")";
587 if (direction == kIncomingPacket) {
588 CreateAccumulatedPacketsTimeSeries(
589 plot, parsed_log_.incoming_rtcp_packets(), label);
590 } else {
591 CreateAccumulatedPacketsTimeSeries(
592 plot, parsed_log_.outgoing_rtcp_packets(), label);
593 }
philipelccd74892016-09-05 02:46:25 -0700594
595 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
596 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200597 plot->SetTitle(std::string("Accumulated ") + GetDirectionAsString(direction) +
598 " RTP/RTCP packets");
philipelccd74892016-09-05 02:46:25 -0700599}
600
terelius54ce6802016-07-13 06:44:41 -0700601// For each SSRC, plot the time between the consecutive playouts.
602void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200603 for (const auto& playout_stream : parsed_log_.audio_playout_events()) {
604 uint32_t ssrc = playout_stream.first;
605 if (!MatchingSsrc(ssrc, desired_ssrc_))
606 continue;
607 rtc::Optional<int64_t> last_playout;
608 TimeSeries time_series(SsrcToString(ssrc), LineStyle::kBar);
609 for (const auto& playout_time : playout_stream.second) {
610 float x = ToCallTime(playout_time);
611 // If there were no previous playouts, place the point on the x-axis.
612 float y = static_cast<float>(playout_time -
613 last_playout.value_or(playout_time)) /
614 1000;
615 time_series.points.push_back(TimeSeriesPoint(x, y));
616 last_playout.emplace(playout_time);
terelius54ce6802016-07-13 06:44:41 -0700617 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200618 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700619 }
620
tereliusdc35dcd2016-08-01 12:03:27 -0700621 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
622 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
623 kTopMargin);
624 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700625}
626
ivocaac9d6f2016-09-22 07:01:47 -0700627// For audio SSRCs, plot the audio level.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200628void EventLogAnalyzer::CreateAudioLevelGraph(PacketDirection direction,
629 Plot* plot) {
630 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
631 if (!IsAudioSsrc(direction, stream.ssrc))
632 continue;
633 TimeSeries time_series(GetStreamName(direction, stream.ssrc),
634 LineStyle::kLine);
635 for (auto& packet : stream.packet_view) {
ivocaac9d6f2016-09-22 07:01:47 -0700636 if (packet.header.extension.hasAudioLevel) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200637 float x = ToCallTime(packet.log_time_us());
ivocaac9d6f2016-09-22 07:01:47 -0700638 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
639 // Here we convert it to dBov.
640 float y = static_cast<float>(-packet.header.extension.audioLevel);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200641 time_series.points.emplace_back(TimeSeriesPoint(x, y));
ivocaac9d6f2016-09-22 07:01:47 -0700642 }
643 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200644 plot->AppendTimeSeries(std::move(time_series));
ivocaac9d6f2016-09-22 07:01:47 -0700645 }
646
647 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
ivocbf676632016-11-24 08:30:34 -0800648 plot->SetYAxis(-127, 0, "Audio level (dBov)", kBottomMargin,
ivocaac9d6f2016-09-22 07:01:47 -0700649 kTopMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200650 plot->SetTitle(GetDirectionAsString(direction) + " audio level");
ivocaac9d6f2016-09-22 07:01:47 -0700651}
652
terelius54ce6802016-07-13 06:44:41 -0700653// For each SSRC, plot the time between the consecutive playouts.
654void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200655 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
656 // Filter on SSRC.
657 if (!MatchingSsrc(stream.ssrc, desired_ssrc_)) {
terelius6addf492016-08-23 17:34:07 -0700658 continue;
terelius54ce6802016-07-13 06:44:41 -0700659 }
terelius54ce6802016-07-13 06:44:41 -0700660
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200661 TimeSeries time_series(GetStreamName(kIncomingPacket, stream.ssrc),
662 LineStyle::kBar);
663 auto GetSequenceNumberDiff = [](const LoggedRtpPacketIncoming& old_packet,
664 const LoggedRtpPacketIncoming& new_packet) {
665 int64_t diff =
666 WrappingDifference(new_packet.rtp.header.sequenceNumber,
667 old_packet.rtp.header.sequenceNumber, 1ul << 16);
668 return diff;
669 };
670 ProcessPairs<LoggedRtpPacketIncoming, float>(GetSequenceNumberDiff,
671 stream.incoming_packets,
672 begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700673 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700674 }
675
tereliusdc35dcd2016-08-01 12:03:27 -0700676 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
677 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
678 kTopMargin);
679 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700680}
681
Stefan Holmer99f8e082016-09-09 13:37:50 +0200682void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200683 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
684 const std::vector<LoggedRtpPacketIncoming>& packets =
685 stream.incoming_packets;
686 // Filter on SSRC.
687 if (!MatchingSsrc(stream.ssrc, desired_ssrc_) || packets.size() == 0) {
Stefan Holmer99f8e082016-09-09 13:37:50 +0200688 continue;
689 }
690
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200691 TimeSeries time_series(GetStreamName(kIncomingPacket, stream.ssrc),
692 LineStyle::kLine, PointStyle::kHighlight);
693 // TODO(terelius): Should the window and step size be read from the class
694 // instead?
695 const int64_t kWindowUs = 1000000;
696 const int64_t kStep = 1000000;
Bjorn Terelius2eb31882017-11-30 15:15:25 +0100697 SeqNumUnwrapper<uint16_t> unwrapper_;
698 SeqNumUnwrapper<uint16_t> prior_unwrapper_;
terelius4c9b4af2017-01-30 08:44:51 -0800699 size_t window_index_begin = 0;
700 size_t window_index_end = 0;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200701 uint64_t highest_seq_number =
702 unwrapper_.Unwrap(packets[0].rtp.header.sequenceNumber) - 1;
703 uint64_t highest_prior_seq_number =
704 prior_unwrapper_.Unwrap(packets[0].rtp.header.sequenceNumber) - 1;
terelius4c9b4af2017-01-30 08:44:51 -0800705
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200706 for (int64_t t = begin_time_; t < end_time_ + kStep; t += kStep) {
707 while (window_index_end < packets.size() &&
708 packets[window_index_end].rtp.log_time_us() < t) {
709 uint64_t sequence_number = unwrapper_.Unwrap(
710 packets[window_index_end].rtp.header.sequenceNumber);
terelius4c9b4af2017-01-30 08:44:51 -0800711 highest_seq_number = std::max(highest_seq_number, sequence_number);
712 ++window_index_end;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200713 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200714 while (window_index_begin < packets.size() &&
715 packets[window_index_begin].rtp.log_time_us() < t - kWindowUs) {
716 uint64_t sequence_number = prior_unwrapper_.Unwrap(
717 packets[window_index_begin].rtp.header.sequenceNumber);
terelius4c9b4af2017-01-30 08:44:51 -0800718 highest_prior_seq_number =
719 std::max(highest_prior_seq_number, sequence_number);
720 ++window_index_begin;
721 }
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800722 float x = ToCallTime(t);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200723 uint64_t expected_packets = highest_seq_number - highest_prior_seq_number;
terelius4c9b4af2017-01-30 08:44:51 -0800724 if (expected_packets > 0) {
725 int64_t received_packets = window_index_end - window_index_begin;
726 int64_t lost_packets = expected_packets - received_packets;
727 float y = static_cast<float>(lost_packets) / expected_packets * 100;
728 time_series.points.emplace_back(x, y);
729 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200730 }
philipel35ba9bd2017-04-19 05:58:51 -0700731 plot->AppendTimeSeries(std::move(time_series));
Stefan Holmer99f8e082016-09-09 13:37:50 +0200732 }
733
734 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
735 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
736 kTopMargin);
737 plot->SetTitle("Estimated incoming loss rate");
738}
739
terelius2ee076d2017-08-15 02:04:02 -0700740void EventLogAnalyzer::CreateIncomingDelayDeltaGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200741 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(kIncomingPacket)) {
742 // Filter on SSRC.
743 if (!MatchingSsrc(stream.ssrc, desired_ssrc_) ||
744 IsAudioSsrc(kIncomingPacket, stream.ssrc) ||
745 !IsVideoSsrc(kIncomingPacket, stream.ssrc) ||
746 IsRtxSsrc(kIncomingPacket, stream.ssrc)) {
terelius88e64e52016-07-19 01:51:06 -0700747 continue;
748 }
terelius54ce6802016-07-13 06:44:41 -0700749
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200750 TimeSeries capture_time_data(
751 GetStreamName(kIncomingPacket, stream.ssrc) + " capture-time",
752 LineStyle::kBar);
terelius53dc23c2017-03-13 05:24:05 -0700753 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200754 stream.packet_view, begin_time_,
terelius53dc23c2017-03-13 05:24:05 -0700755 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700756 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700757
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200758 TimeSeries send_time_data(
759 GetStreamName(kIncomingPacket, stream.ssrc) + " abs-send-time",
760 LineStyle::kBar);
terelius53dc23c2017-03-13 05:24:05 -0700761 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200762 stream.packet_view, begin_time_,
terelius53dc23c2017-03-13 05:24:05 -0700763 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700764 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700765 }
766
tereliusdc35dcd2016-08-01 12:03:27 -0700767 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
768 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
769 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700770 plot->SetTitle("Network latency difference between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700771}
772
terelius2ee076d2017-08-15 02:04:02 -0700773void EventLogAnalyzer::CreateIncomingDelayGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200774 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(kIncomingPacket)) {
775 // Filter on SSRC.
776 if (!MatchingSsrc(stream.ssrc, desired_ssrc_) ||
777 IsAudioSsrc(kIncomingPacket, stream.ssrc) ||
778 !IsVideoSsrc(kIncomingPacket, stream.ssrc) ||
779 IsRtxSsrc(kIncomingPacket, stream.ssrc)) {
terelius88e64e52016-07-19 01:51:06 -0700780 continue;
781 }
terelius54ce6802016-07-13 06:44:41 -0700782
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200783 TimeSeries capture_time_data(
784 GetStreamName(kIncomingPacket, stream.ssrc) + " capture-time",
785 LineStyle::kLine);
terelius53dc23c2017-03-13 05:24:05 -0700786 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200787 stream.packet_view, begin_time_,
terelius53dc23c2017-03-13 05:24:05 -0700788 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700789 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700790
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200791 TimeSeries send_time_data(
792 GetStreamName(kIncomingPacket, stream.ssrc) + " abs-send-time",
793 LineStyle::kLine);
terelius53dc23c2017-03-13 05:24:05 -0700794 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200795 stream.packet_view, begin_time_,
terelius53dc23c2017-03-13 05:24:05 -0700796 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700797 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700798 }
799
tereliusdc35dcd2016-08-01 12:03:27 -0700800 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
801 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
802 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700803 plot->SetTitle("Network latency (relative to first packet)");
terelius54ce6802016-07-13 06:44:41 -0700804}
805
tereliusf736d232016-08-04 10:00:11 -0700806// Plot the fraction of packets lost (as perceived by the loss-based BWE).
807void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +0100808 TimeSeries time_series("Fraction lost", LineStyle::kLine,
809 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200810 for (auto& bwe_update : parsed_log_.bwe_loss_updates()) {
811 float x = ToCallTime(bwe_update.log_time_us());
812 float y = static_cast<float>(bwe_update.fraction_lost) / 255 * 100;
philipel35ba9bd2017-04-19 05:58:51 -0700813 time_series.points.emplace_back(x, y);
tereliusf736d232016-08-04 10:00:11 -0700814 }
tereliusf736d232016-08-04 10:00:11 -0700815
Bjorn Terelius19f5be32017-10-18 12:39:49 +0200816 plot->AppendTimeSeries(std::move(time_series));
tereliusf736d232016-08-04 10:00:11 -0700817 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
818 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
819 kTopMargin);
820 plot->SetTitle("Reported packet loss");
821}
822
terelius54ce6802016-07-13 06:44:41 -0700823// Plot the total bandwidth used by all RTP streams.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200824void EventLogAnalyzer::CreateTotalIncomingBitrateGraph(Plot* plot) {
825 // TODO(terelius): This could be provided by the parser.
826 std::multimap<int64_t, size_t> packets_in_order;
827 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
828 for (const LoggedRtpPacketIncoming& packet : stream.incoming_packets)
829 packets_in_order.insert(
830 std::make_pair(packet.rtp.log_time_us(), packet.rtp.total_length));
terelius54ce6802016-07-13 06:44:41 -0700831 }
832
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200833 auto window_begin = packets_in_order.begin();
834 auto window_end = packets_in_order.begin();
terelius54ce6802016-07-13 06:44:41 -0700835 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700836
837 // Calculate a moving average of the bitrate and store in a TimeSeries.
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +0100838 TimeSeries bitrate_series("Bitrate", LineStyle::kLine);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200839 for (int64_t time = begin_time_; time < end_time_ + step_; time += step_) {
840 while (window_end != packets_in_order.end() && window_end->first < time) {
841 bytes_in_window += window_end->second;
842 ++window_end;
terelius54ce6802016-07-13 06:44:41 -0700843 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200844 while (window_begin != packets_in_order.end() &&
845 window_begin->first < time - window_duration_) {
846 RTC_DCHECK_LE(window_begin->second, bytes_in_window);
847 bytes_in_window -= window_begin->second;
848 ++window_begin;
849 }
850 float window_duration_in_seconds =
851 static_cast<float>(window_duration_) / kNumMicrosecsPerSec;
852 float x = ToCallTime(time);
853 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
854 bitrate_series.points.emplace_back(x, y);
855 }
856 plot->AppendTimeSeries(std::move(bitrate_series));
857
858 // Overlay the outgoing REMB over incoming bitrate.
859 TimeSeries remb_series("Remb", LineStyle::kStep);
860 for (const auto& rtcp : parsed_log_.rembs(kOutgoingPacket)) {
861 float x = ToCallTime(rtcp.log_time_us());
862 float y = static_cast<float>(rtcp.remb.bitrate_bps()) / 1000;
863 remb_series.points.emplace_back(x, y);
864 }
865 plot->AppendTimeSeriesIfNotEmpty(std::move(remb_series));
866
867 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
868 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
869 plot->SetTitle("Incoming RTP bitrate");
870}
871
872// Plot the total bandwidth used by all RTP streams.
873void EventLogAnalyzer::CreateTotalOutgoingBitrateGraph(Plot* plot,
874 bool show_detector_state,
875 bool show_alr_state) {
876 // TODO(terelius): This could be provided by the parser.
877 std::multimap<int64_t, size_t> packets_in_order;
878 for (const auto& stream : parsed_log_.outgoing_rtp_packets_by_ssrc()) {
879 for (const LoggedRtpPacketOutgoing& packet : stream.outgoing_packets)
880 packets_in_order.insert(
881 std::make_pair(packet.rtp.log_time_us(), packet.rtp.total_length));
882 }
883
884 auto window_begin = packets_in_order.begin();
885 auto window_end = packets_in_order.begin();
886 size_t bytes_in_window = 0;
887
888 // Calculate a moving average of the bitrate and store in a TimeSeries.
889 TimeSeries bitrate_series("Bitrate", LineStyle::kLine);
890 for (int64_t time = begin_time_; time < end_time_ + step_; time += step_) {
891 while (window_end != packets_in_order.end() && window_end->first < time) {
892 bytes_in_window += window_end->second;
893 ++window_end;
894 }
895 while (window_begin != packets_in_order.end() &&
896 window_begin->first < time - window_duration_) {
897 RTC_DCHECK_LE(window_begin->second, bytes_in_window);
898 bytes_in_window -= window_begin->second;
899 ++window_begin;
terelius54ce6802016-07-13 06:44:41 -0700900 }
901 float window_duration_in_seconds =
Qingsi Wang8eca1ff2018-02-02 11:49:44 -0800902 static_cast<float>(window_duration_) / kNumMicrosecsPerSec;
903 float x = ToCallTime(time);
terelius54ce6802016-07-13 06:44:41 -0700904 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
philipel35ba9bd2017-04-19 05:58:51 -0700905 bitrate_series.points.emplace_back(x, y);
terelius54ce6802016-07-13 06:44:41 -0700906 }
philipel35ba9bd2017-04-19 05:58:51 -0700907 plot->AppendTimeSeries(std::move(bitrate_series));
terelius54ce6802016-07-13 06:44:41 -0700908
terelius8058e582016-07-25 01:32:41 -0700909 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200910 TimeSeries loss_series("Loss-based estimate", LineStyle::kStep);
911 for (auto& loss_update : parsed_log_.bwe_loss_updates()) {
912 float x = ToCallTime(loss_update.log_time_us());
913 float y = static_cast<float>(loss_update.bitrate_bps) / 1000;
914 loss_series.points.emplace_back(x, y);
915 }
philipel10fc0e62017-04-11 01:50:23 -0700916
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200917 TimeSeries delay_series("Delay-based estimate", LineStyle::kStep);
918 IntervalSeries overusing_series("Overusing", "#ff8e82",
919 IntervalSeries::kHorizontal);
920 IntervalSeries underusing_series("Underusing", "#5092fc",
921 IntervalSeries::kHorizontal);
922 IntervalSeries normal_series("Normal", "#c4ffc4",
923 IntervalSeries::kHorizontal);
924 IntervalSeries* last_series = &normal_series;
925 double last_detector_switch = 0.0;
philipel23c7f252017-07-14 06:30:03 -0700926
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200927 BandwidthUsage last_detector_state = BandwidthUsage::kBwNormal;
philipel23c7f252017-07-14 06:30:03 -0700928
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200929 for (auto& delay_update : parsed_log_.bwe_delay_updates()) {
930 float x = ToCallTime(delay_update.log_time_us());
931 float y = static_cast<float>(delay_update.bitrate_bps) / 1000;
philipel23c7f252017-07-14 06:30:03 -0700932
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200933 if (last_detector_state != delay_update.detector_state) {
934 last_series->intervals.emplace_back(last_detector_switch, x);
935 last_detector_state = delay_update.detector_state;
936 last_detector_switch = x;
philipel23c7f252017-07-14 06:30:03 -0700937
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200938 switch (delay_update.detector_state) {
939 case BandwidthUsage::kBwNormal:
940 last_series = &normal_series;
941 break;
942 case BandwidthUsage::kBwUnderusing:
943 last_series = &underusing_series;
944 break;
945 case BandwidthUsage::kBwOverusing:
946 last_series = &overusing_series;
947 break;
948 case BandwidthUsage::kLast:
949 RTC_NOTREACHED();
philipele127e7a2017-03-29 16:28:53 +0200950 }
951 }
philipel23c7f252017-07-14 06:30:03 -0700952
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200953 delay_series.points.emplace_back(x, y);
954 }
Bjorn Terelius9e336ec2018-04-24 16:28:35 +0200955
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200956 RTC_CHECK(last_series);
957 last_series->intervals.emplace_back(last_detector_switch, end_time_);
958
959 TimeSeries created_series("Probe cluster created.", LineStyle::kNone,
960 PointStyle::kHighlight);
961 for (auto& cluster : parsed_log_.bwe_probe_cluster_created_events()) {
962 float x = ToCallTime(cluster.log_time_us());
963 float y = static_cast<float>(cluster.bitrate_bps) / 1000;
964 created_series.points.emplace_back(x, y);
965 }
966
967 TimeSeries result_series("Probing results.", LineStyle::kNone,
968 PointStyle::kHighlight);
969 for (auto& result : parsed_log_.bwe_probe_result_events()) {
970 if (result.bitrate_bps) {
971 float x = ToCallTime(result.log_time_us());
972 float y = static_cast<float>(*result.bitrate_bps) / 1000;
973 result_series.points.emplace_back(x, y);
974 }
975 }
976
977 IntervalSeries alr_state("ALR", "#555555", IntervalSeries::kHorizontal);
978 bool previously_in_alr = false;
979 int64_t alr_start = 0;
980 for (auto& alr : parsed_log_.alr_state_events()) {
981 float y = ToCallTime(alr.log_time_us());
982 if (!previously_in_alr && alr.in_alr) {
983 alr_start = alr.log_time_us();
984 previously_in_alr = true;
985 } else if (previously_in_alr && !alr.in_alr) {
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +0100986 float x = ToCallTime(alr_start);
Ilya Nikolaevskiya4259f62017-12-05 13:19:45 +0100987 alr_state.intervals.emplace_back(x, y);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200988 previously_in_alr = false;
Björn Tereliusff612732018-04-25 14:23:01 +0000989 }
Björn Tereliusff612732018-04-25 14:23:01 +0000990 }
991
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +0200992 if (previously_in_alr) {
993 float x = ToCallTime(alr_start);
994 float y = ToCallTime(end_time_);
995 alr_state.intervals.emplace_back(x, y);
996 }
997
998 if (show_detector_state) {
999 plot->AppendIntervalSeries(std::move(overusing_series));
1000 plot->AppendIntervalSeries(std::move(underusing_series));
1001 plot->AppendIntervalSeries(std::move(normal_series));
1002 }
1003
1004 if (show_alr_state) {
1005 plot->AppendIntervalSeries(std::move(alr_state));
1006 }
1007 plot->AppendTimeSeries(std::move(loss_series));
1008 plot->AppendTimeSeries(std::move(delay_series));
1009 plot->AppendTimeSeries(std::move(created_series));
1010 plot->AppendTimeSeries(std::move(result_series));
1011
1012 // Overlay the incoming REMB over the outgoing bitrate.
Björn Tereliusff612732018-04-25 14:23:01 +00001013 TimeSeries remb_series("Remb", LineStyle::kStep);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001014 for (const auto& rtcp : parsed_log_.rembs(kIncomingPacket)) {
1015 float x = ToCallTime(rtcp.log_time_us());
1016 float y = static_cast<float>(rtcp.remb.bitrate_bps()) / 1000;
terelius2c8e8a32017-06-02 01:29:48 -07001017 remb_series.points.emplace_back(x, y);
1018 }
1019 plot->AppendTimeSeriesIfNotEmpty(std::move(remb_series));
1020
tereliusdc35dcd2016-08-01 12:03:27 -07001021 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1022 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001023 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001024}
1025
1026// For each SSRC, plot the bandwidth used by that stream.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001027void EventLogAnalyzer::CreateStreamBitrateGraph(PacketDirection direction,
1028 Plot* plot) {
1029 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
1030 // Filter on SSRC.
1031 if (!MatchingSsrc(stream.ssrc, desired_ssrc_)) {
terelius6addf492016-08-23 17:34:07 -07001032 continue;
terelius54ce6802016-07-13 06:44:41 -07001033 }
1034
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001035 TimeSeries time_series(GetStreamName(direction, stream.ssrc),
1036 LineStyle::kLine);
1037 auto GetPacketSizeKilobits = [](const LoggedRtpPacket& packet) {
1038 return packet.total_length * 8.0 / 1000.0;
1039 };
terelius53dc23c2017-03-13 05:24:05 -07001040 MovingAverage<LoggedRtpPacket, double>(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001041 GetPacketSizeKilobits, stream.packet_view, begin_time_, end_time_,
1042 window_duration_, step_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001043 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -07001044 }
1045
tereliusdc35dcd2016-08-01 12:03:27 -07001046 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1047 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001048 plot->SetTitle(GetDirectionAsString(direction) + " bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001049}
1050
Bjorn Terelius28db2662017-10-04 14:22:43 +02001051void EventLogAnalyzer::CreateSendSideBweSimulationGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001052 using RtpPacketType = LoggedRtpPacketOutgoing;
1053 using TransportFeedbackType = LoggedRtcpPacketTransportFeedback;
Stefan Holmer13181032016-07-29 14:48:54 +02001054
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001055 // TODO(terelius): This could be provided by the parser.
1056 std::multimap<int64_t, const RtpPacketType*> outgoing_rtp;
1057 for (const auto& stream : parsed_log_.outgoing_rtp_packets_by_ssrc()) {
1058 for (const RtpPacketType& rtp_packet : stream.outgoing_packets)
1059 outgoing_rtp.insert(
1060 std::make_pair(rtp_packet.rtp.log_time_us(), &rtp_packet));
Stefan Holmer13181032016-07-29 14:48:54 +02001061 }
1062
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001063 const std::vector<TransportFeedbackType>& incoming_rtcp =
1064 parsed_log_.transport_feedbacks(kIncomingPacket);
Stefan Holmer13181032016-07-29 14:48:54 +02001065
1066 SimulatedClock clock(0);
1067 BitrateObserver observer;
1068 RtcEventLogNullImpl null_event_log;
nisse0245da02016-11-30 03:35:20 -08001069 PacketRouter packet_router;
Stefan Holmer5c8942a2017-08-22 16:16:44 +02001070 PacedSender pacer(&clock, &packet_router, &null_event_log);
1071 SendSideCongestionController cc(&clock, &observer, &null_event_log, &pacer);
Stefan Holmer13181032016-07-29 14:48:54 +02001072 // TODO(holmer): Log the call config and use that here instead.
1073 static const uint32_t kDefaultStartBitrateBps = 300000;
1074 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1075
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001076 TimeSeries time_series("Delay-based estimate", LineStyle::kStep,
1077 PointStyle::kHighlight);
1078 TimeSeries acked_time_series("Acked bitrate", LineStyle::kLine,
1079 PointStyle::kHighlight);
1080 TimeSeries acked_estimate_time_series(
1081 "Acked bitrate estimate", LineStyle::kLine, PointStyle::kHighlight);
Stefan Holmer13181032016-07-29 14:48:54 +02001082
1083 auto rtp_iterator = outgoing_rtp.begin();
1084 auto rtcp_iterator = incoming_rtcp.begin();
1085
1086 auto NextRtpTime = [&]() {
1087 if (rtp_iterator != outgoing_rtp.end())
1088 return static_cast<int64_t>(rtp_iterator->first);
1089 return std::numeric_limits<int64_t>::max();
1090 };
1091
1092 auto NextRtcpTime = [&]() {
1093 if (rtcp_iterator != incoming_rtcp.end())
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001094 return static_cast<int64_t>(rtcp_iterator->log_time_us());
Stefan Holmer13181032016-07-29 14:48:54 +02001095 return std::numeric_limits<int64_t>::max();
1096 };
1097
1098 auto NextProcessTime = [&]() {
1099 if (rtcp_iterator != incoming_rtcp.end() ||
1100 rtp_iterator != outgoing_rtp.end()) {
1101 return clock.TimeInMicroseconds() +
1102 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
1103 }
1104 return std::numeric_limits<int64_t>::max();
1105 };
1106
Stefan Holmer492ee282016-10-27 17:19:20 +02001107 RateStatistics acked_bitrate(250, 8000);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001108#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1109 // The event_log_visualizer should normally not be compiled with
1110 // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE since the normal plots won't work.
1111 // However, compiling with BWE_TEST_LOGGING, runnning with --plot_sendside_bwe
1112 // and piping the output to plot_dynamics.py can be used as a hack to get the
1113 // internal state of various BWE components. In this case, it is important
1114 // we don't instantiate the AcknowledgedBitrateEstimator both here and in
1115 // SendSideCongestionController since that would lead to duplicate outputs.
1116 AcknowledgedBitrateEstimator acknowledged_bitrate_estimator(
1117 rtc::MakeUnique<BitrateEstimator>());
1118#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Stefan Holmer13181032016-07-29 14:48:54 +02001119 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
Stefan Holmer492ee282016-10-27 17:19:20 +02001120 int64_t last_update_us = 0;
Stefan Holmer13181032016-07-29 14:48:54 +02001121 while (time_us != std::numeric_limits<int64_t>::max()) {
1122 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1123 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001124 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001125 cc.OnTransportFeedback(rtcp_iterator->transport_feedback);
1126 std::vector<PacketFeedback> feedback = cc.GetTransportFeedbackVector();
1127 SortPacketFeedbackVector(&feedback);
1128 rtc::Optional<uint32_t> bitrate_bps;
1129 if (!feedback.empty()) {
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001130#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001131 acknowledged_bitrate_estimator.IncomingPacketFeedbackVector(feedback);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001132#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001133 for (const PacketFeedback& packet : feedback)
1134 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1135 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
Stefan Holmer13181032016-07-29 14:48:54 +02001136 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001137 float x = ToCallTime(clock.TimeInMicroseconds());
1138 float y = bitrate_bps.value_or(0) / 1000;
1139 acked_time_series.points.emplace_back(x, y);
1140#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1141 y = acknowledged_bitrate_estimator.bitrate_bps().value_or(0) / 1000;
1142 acked_estimate_time_series.points.emplace_back(x, y);
1143#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Stefan Holmer13181032016-07-29 14:48:54 +02001144 ++rtcp_iterator;
1145 }
1146 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001147 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001148 const RtpPacketType& rtp_packet = *rtp_iterator->second;
1149 if (rtp_packet.rtp.header.extension.hasTransportSequenceNumber) {
1150 RTC_DCHECK(rtp_packet.rtp.header.extension.hasTransportSequenceNumber);
1151 cc.AddPacket(rtp_packet.rtp.header.ssrc,
1152 rtp_packet.rtp.header.extension.transportSequenceNumber,
1153 rtp_packet.rtp.total_length, PacedPacketInfo());
Stefan Holmer13181032016-07-29 14:48:54 +02001154 rtc::SentPacket sent_packet(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001155 rtp_packet.rtp.header.extension.transportSequenceNumber,
1156 rtp_packet.rtp.log_time_us() / 1000);
Stefan Holmer13181032016-07-29 14:48:54 +02001157 cc.OnSentPacket(sent_packet);
1158 }
1159 ++rtp_iterator;
1160 }
stefanc3de0332016-08-02 07:22:17 -07001161 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1162 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001163 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001164 }
Stefan Holmer492ee282016-10-27 17:19:20 +02001165 if (observer.GetAndResetBitrateUpdated() ||
1166 time_us - last_update_us >= 1e6) {
Stefan Holmer13181032016-07-29 14:48:54 +02001167 uint32_t y = observer.last_bitrate_bps() / 1000;
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001168 float x = ToCallTime(clock.TimeInMicroseconds());
Stefan Holmer13181032016-07-29 14:48:54 +02001169 time_series.points.emplace_back(x, y);
Stefan Holmer492ee282016-10-27 17:19:20 +02001170 last_update_us = time_us;
Stefan Holmer13181032016-07-29 14:48:54 +02001171 }
1172 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1173 }
1174 // Add the data set to the plot.
philipel35ba9bd2017-04-19 05:58:51 -07001175 plot->AppendTimeSeries(std::move(time_series));
1176 plot->AppendTimeSeries(std::move(acked_time_series));
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001177 plot->AppendTimeSeriesIfNotEmpty(std::move(acked_estimate_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001178
tereliusdc35dcd2016-08-01 12:03:27 -07001179 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1180 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001181 plot->SetTitle("Simulated send-side BWE behavior");
1182}
1183
1184void EventLogAnalyzer::CreateReceiveSideBweSimulationGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001185 using RtpPacketType = LoggedRtpPacketIncoming;
Bjorn Terelius28db2662017-10-04 14:22:43 +02001186 class RembInterceptingPacketRouter : public PacketRouter {
1187 public:
1188 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
1189 uint32_t bitrate_bps) override {
1190 last_bitrate_bps_ = bitrate_bps;
1191 bitrate_updated_ = true;
1192 PacketRouter::OnReceiveBitrateChanged(ssrcs, bitrate_bps);
1193 }
1194 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
1195 bool GetAndResetBitrateUpdated() {
1196 bool bitrate_updated = bitrate_updated_;
1197 bitrate_updated_ = false;
1198 return bitrate_updated;
1199 }
1200
1201 private:
1202 uint32_t last_bitrate_bps_;
1203 bool bitrate_updated_;
1204 };
1205
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001206 std::multimap<int64_t, const RtpPacketType*> incoming_rtp;
Bjorn Terelius28db2662017-10-04 14:22:43 +02001207
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001208 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
1209 if (IsVideoSsrc(kIncomingPacket, stream.ssrc)) {
1210 for (const auto& rtp_packet : stream.incoming_packets)
1211 incoming_rtp.insert(
1212 std::make_pair(rtp_packet.rtp.log_time_us(), &rtp_packet));
Bjorn Terelius28db2662017-10-04 14:22:43 +02001213 }
1214 }
1215
1216 SimulatedClock clock(0);
1217 RembInterceptingPacketRouter packet_router;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001218 // TODO(terelius): The PacketRouter is used as the RemoteBitrateObserver.
Bjorn Terelius28db2662017-10-04 14:22:43 +02001219 // Is this intentional?
1220 ReceiveSideCongestionController rscc(&clock, &packet_router);
1221 // TODO(holmer): Log the call config and use that here instead.
1222 // static const uint32_t kDefaultStartBitrateBps = 300000;
1223 // rscc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1224
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001225 TimeSeries time_series("Receive side estimate", LineStyle::kLine,
1226 PointStyle::kHighlight);
1227 TimeSeries acked_time_series("Received bitrate", LineStyle::kLine);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001228
1229 RateStatistics acked_bitrate(250, 8000);
1230 int64_t last_update_us = 0;
1231 for (const auto& kv : incoming_rtp) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001232 const RtpPacketType& packet = *kv.second;
1233 int64_t arrival_time_ms = packet.rtp.log_time_us() / 1000;
1234 size_t payload = packet.rtp.total_length; /*Should subtract header?*/
1235 clock.AdvanceTimeMicroseconds(packet.rtp.log_time_us() -
Bjorn Terelius28db2662017-10-04 14:22:43 +02001236 clock.TimeInMicroseconds());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001237 rscc.OnReceivedPacket(arrival_time_ms, payload, packet.rtp.header);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001238 acked_bitrate.Update(payload, arrival_time_ms);
1239 rtc::Optional<uint32_t> bitrate_bps = acked_bitrate.Rate(arrival_time_ms);
1240 if (bitrate_bps) {
1241 uint32_t y = *bitrate_bps / 1000;
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001242 float x = ToCallTime(clock.TimeInMicroseconds());
Bjorn Terelius28db2662017-10-04 14:22:43 +02001243 acked_time_series.points.emplace_back(x, y);
1244 }
1245 if (packet_router.GetAndResetBitrateUpdated() ||
1246 clock.TimeInMicroseconds() - last_update_us >= 1e6) {
1247 uint32_t y = packet_router.last_bitrate_bps() / 1000;
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001248 float x = ToCallTime(clock.TimeInMicroseconds());
Bjorn Terelius28db2662017-10-04 14:22:43 +02001249 time_series.points.emplace_back(x, y);
1250 last_update_us = clock.TimeInMicroseconds();
1251 }
1252 }
1253 // Add the data set to the plot.
1254 plot->AppendTimeSeries(std::move(time_series));
1255 plot->AppendTimeSeries(std::move(acked_time_series));
1256
1257 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1258 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1259 plot->SetTitle("Simulated receive-side BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001260}
1261
tereliuse34c19c2016-08-15 08:47:14 -07001262void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001263 using RtpPacketType = LoggedRtpPacketOutgoing;
1264 using TransportFeedbackType = LoggedRtcpPacketTransportFeedback;
stefanc3de0332016-08-02 07:22:17 -07001265
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001266 // TODO(terelius): This could be provided by the parser.
1267 std::multimap<int64_t, const RtpPacketType*> outgoing_rtp;
1268 for (const auto& stream : parsed_log_.outgoing_rtp_packets_by_ssrc()) {
1269 for (const RtpPacketType& rtp_packet : stream.outgoing_packets)
1270 outgoing_rtp.insert(
1271 std::make_pair(rtp_packet.rtp.log_time_us(), &rtp_packet));
stefanc3de0332016-08-02 07:22:17 -07001272 }
1273
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001274 const std::vector<TransportFeedbackType>& incoming_rtcp =
1275 parsed_log_.transport_feedbacks(kIncomingPacket);
stefanc3de0332016-08-02 07:22:17 -07001276
1277 SimulatedClock clock(0);
elad.alon5bbf43f2017-03-09 06:40:08 -08001278 TransportFeedbackAdapter feedback_adapter(&clock);
stefanc3de0332016-08-02 07:22:17 -07001279
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001280 TimeSeries late_feedback_series("Late feedback results.", LineStyle::kNone,
1281 PointStyle::kHighlight);
1282 TimeSeries time_series("Network Delay Change", LineStyle::kLine,
1283 PointStyle::kHighlight);
stefanc3de0332016-08-02 07:22:17 -07001284 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1285
1286 auto rtp_iterator = outgoing_rtp.begin();
1287 auto rtcp_iterator = incoming_rtcp.begin();
1288
1289 auto NextRtpTime = [&]() {
1290 if (rtp_iterator != outgoing_rtp.end())
1291 return static_cast<int64_t>(rtp_iterator->first);
1292 return std::numeric_limits<int64_t>::max();
1293 };
1294
1295 auto NextRtcpTime = [&]() {
1296 if (rtcp_iterator != incoming_rtcp.end())
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001297 return static_cast<int64_t>(rtcp_iterator->log_time_us());
stefanc3de0332016-08-02 07:22:17 -07001298 return std::numeric_limits<int64_t>::max();
1299 };
1300
1301 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
stefana0a8ed72017-09-06 02:06:32 -07001302 int64_t prev_y = 0;
stefanc3de0332016-08-02 07:22:17 -07001303 while (time_us != std::numeric_limits<int64_t>::max()) {
1304 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1305 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1306 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001307 feedback_adapter.OnTransportFeedback(rtcp_iterator->transport_feedback);
1308 std::vector<PacketFeedback> feedback =
1309 feedback_adapter.GetTransportFeedbackVector();
1310 SortPacketFeedbackVector(&feedback);
1311 for (const PacketFeedback& packet : feedback) {
1312 float x = ToCallTime(clock.TimeInMicroseconds());
1313 if (packet.send_time_ms == PacketFeedback::kNoSendTime) {
1314 late_feedback_series.points.emplace_back(x, prev_y);
1315 continue;
stefanc3de0332016-08-02 07:22:17 -07001316 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001317 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1318 prev_y = y;
1319 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1320 time_series.points.emplace_back(x, y);
stefanc3de0332016-08-02 07:22:17 -07001321 }
1322 ++rtcp_iterator;
1323 }
1324 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1325 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001326 const RtpPacketType& rtp_packet = *rtp_iterator->second;
1327 if (rtp_packet.rtp.header.extension.hasTransportSequenceNumber) {
1328 feedback_adapter.AddPacket(
1329 rtp_packet.rtp.header.ssrc,
1330 rtp_packet.rtp.header.extension.transportSequenceNumber,
1331 rtp_packet.rtp.total_length, PacedPacketInfo());
stefanc3de0332016-08-02 07:22:17 -07001332 feedback_adapter.OnSentPacket(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001333 rtp_packet.rtp.header.extension.transportSequenceNumber,
1334 rtp_packet.rtp.log_time_us() / 1000);
stefanc3de0332016-08-02 07:22:17 -07001335 }
1336 ++rtp_iterator;
1337 }
1338 time_us = std::min(NextRtpTime(), NextRtcpTime());
1339 }
1340 // We assume that the base network delay (w/o queues) is the min delay
1341 // observed during the call.
1342 for (TimeSeriesPoint& point : time_series.points)
1343 point.y -= estimated_base_delay_ms;
stefana0a8ed72017-09-06 02:06:32 -07001344 for (TimeSeriesPoint& point : late_feedback_series.points)
1345 point.y -= estimated_base_delay_ms;
stefanc3de0332016-08-02 07:22:17 -07001346 // Add the data set to the plot.
stefana0a8ed72017-09-06 02:06:32 -07001347 plot->AppendTimeSeriesIfNotEmpty(std::move(time_series));
1348 plot->AppendTimeSeriesIfNotEmpty(std::move(late_feedback_series));
stefanc3de0332016-08-02 07:22:17 -07001349
1350 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1351 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1352 plot->SetTitle("Network Delay Change.");
1353}
stefan08383272016-12-20 08:51:52 -08001354
Bjorn Terelius0295a962017-10-25 17:42:41 +02001355void EventLogAnalyzer::CreatePacerDelayGraph(Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001356 for (const auto& stream : parsed_log_.outgoing_rtp_packets_by_ssrc()) {
1357 const std::vector<LoggedRtpPacketOutgoing>& packets =
1358 stream.outgoing_packets;
Bjorn Terelius0295a962017-10-25 17:42:41 +02001359
1360 if (packets.size() < 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001361 RTC_LOG(LS_WARNING)
1362 << "Can't estimate a the RTP clock frequency or the "
1363 "pacer delay with less than 2 packets in the stream";
Bjorn Terelius0295a962017-10-25 17:42:41 +02001364 continue;
1365 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001366 int64_t end_time_us = log_segments_.empty()
1367 ? std::numeric_limits<int64_t>::max()
1368 : log_segments_.front().second;
Bjorn Terelius0295a962017-10-25 17:42:41 +02001369 rtc::Optional<uint32_t> estimated_frequency =
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001370 EstimateRtpClockFrequency(packets, end_time_us);
Bjorn Terelius0295a962017-10-25 17:42:41 +02001371 if (!estimated_frequency)
1372 continue;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001373 if (IsVideoSsrc(kOutgoingPacket, stream.ssrc) &&
1374 *estimated_frequency != 90000) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001375 RTC_LOG(LS_WARNING)
Bjorn Terelius0295a962017-10-25 17:42:41 +02001376 << "Video stream should use a 90 kHz clock but appears to use "
1377 << *estimated_frequency / 1000 << ". Discarding.";
1378 continue;
1379 }
1380
1381 TimeSeries pacer_delay_series(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001382 GetStreamName(kOutgoingPacket, stream.ssrc) + "(" +
Bjorn Terelius0295a962017-10-25 17:42:41 +02001383 std::to_string(*estimated_frequency / 1000) + " kHz)",
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001384 LineStyle::kLine, PointStyle::kHighlight);
Bjorn Terelius0295a962017-10-25 17:42:41 +02001385 SeqNumUnwrapper<uint32_t> timestamp_unwrapper;
1386 uint64_t first_capture_timestamp =
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001387 timestamp_unwrapper.Unwrap(packets.front().rtp.header.timestamp);
1388 uint64_t first_send_timestamp = packets.front().rtp.log_time_us();
1389 for (const auto& packet : packets) {
Bjorn Terelius0295a962017-10-25 17:42:41 +02001390 double capture_time_ms = (static_cast<double>(timestamp_unwrapper.Unwrap(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001391 packet.rtp.header.timestamp)) -
Bjorn Terelius0295a962017-10-25 17:42:41 +02001392 first_capture_timestamp) /
1393 *estimated_frequency * 1000;
1394 double send_time_ms =
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001395 static_cast<double>(packet.rtp.log_time_us() - first_send_timestamp) /
1396 1000;
1397 float x = ToCallTime(packet.rtp.log_time_us());
Bjorn Terelius0295a962017-10-25 17:42:41 +02001398 float y = send_time_ms - capture_time_ms;
1399 pacer_delay_series.points.emplace_back(x, y);
1400 }
1401 plot->AppendTimeSeries(std::move(pacer_delay_series));
1402 }
1403
1404 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1405 plot->SetSuggestedYAxis(0, 10, "Pacer delay (ms)", kBottomMargin, kTopMargin);
1406 plot->SetTitle(
1407 "Delay from capture to send time. (First packet normalized to 0.)");
1408}
1409
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001410void EventLogAnalyzer::CreateTimestampGraph(PacketDirection direction,
1411 Plot* plot) {
1412 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
1413 TimeSeries rtp_timestamps(
1414 GetStreamName(direction, stream.ssrc) + " capture-time",
1415 LineStyle::kLine, PointStyle::kHighlight);
1416 for (const auto& packet : stream.packet_view) {
1417 float x = ToCallTime(packet.log_time_us());
1418 float y = packet.header.timestamp;
1419 rtp_timestamps.points.emplace_back(x, y);
stefane372d3c2017-02-02 08:04:18 -08001420 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001421 plot->AppendTimeSeries(std::move(rtp_timestamps));
Björn Tereliusff612732018-04-25 14:23:01 +00001422
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001423 TimeSeries rtcp_timestamps(
1424 GetStreamName(direction, stream.ssrc) + " rtcp capture-time",
1425 LineStyle::kLine, PointStyle::kHighlight);
1426 // TODO(terelius): Why only sender reports?
1427 const auto& sender_reports = parsed_log_.sender_reports(direction);
1428 for (const auto& rtcp : sender_reports) {
1429 if (rtcp.sr.sender_ssrc() != stream.ssrc)
1430 continue;
1431 float x = ToCallTime(rtcp.log_time_us());
1432 float y = rtcp.sr.rtp_timestamp();
1433 rtcp_timestamps.points.emplace_back(x, y);
Björn Tereliusff612732018-04-25 14:23:01 +00001434 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001435 plot->AppendTimeSeriesIfNotEmpty(std::move(rtcp_timestamps));
stefane372d3c2017-02-02 08:04:18 -08001436 }
1437
1438 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001439 plot->SetSuggestedYAxis(0, 1, "RTP timestamp", kBottomMargin, kTopMargin);
1440 plot->SetTitle(GetDirectionAsString(direction) + " timestamps");
stefane372d3c2017-02-02 08:04:18 -08001441}
michaelt6e5b2192017-02-22 07:33:27 -08001442
1443void EventLogAnalyzer::CreateAudioEncoderTargetBitrateGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001444 TimeSeries time_series("Audio encoder target bitrate", LineStyle::kLine,
1445 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001446 auto GetAnaBitrateBps = [](const LoggedAudioNetworkAdaptationEvent& ana_event)
1447 -> rtc::Optional<float> {
1448 if (ana_event.config.bitrate_bps)
1449 return rtc::Optional<float>(
1450 static_cast<float>(*ana_event.config.bitrate_bps));
1451 return rtc::nullopt;
1452 };
1453 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
1454 GetAnaBitrateBps, parsed_log_.audio_network_adaptation_events(),
1455 begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001456 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001457 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1458 plot->SetSuggestedYAxis(0, 1, "Bitrate (bps)", kBottomMargin, kTopMargin);
1459 plot->SetTitle("Reported audio encoder target bitrate");
1460}
1461
1462void EventLogAnalyzer::CreateAudioEncoderFrameLengthGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001463 TimeSeries time_series("Audio encoder frame length", LineStyle::kLine,
1464 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001465 auto GetAnaFrameLengthMs =
1466 [](const LoggedAudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001467 if (ana_event.config.frame_length_ms)
1468 return rtc::Optional<float>(
1469 static_cast<float>(*ana_event.config.frame_length_ms));
1470 return rtc::Optional<float>();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001471 };
1472 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
1473 GetAnaFrameLengthMs, parsed_log_.audio_network_adaptation_events(),
1474 begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001475 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001476 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1477 plot->SetSuggestedYAxis(0, 1, "Frame length (ms)", kBottomMargin, kTopMargin);
1478 plot->SetTitle("Reported audio encoder frame length");
1479}
1480
terelius2ee076d2017-08-15 02:04:02 -07001481void EventLogAnalyzer::CreateAudioEncoderPacketLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001482 TimeSeries time_series("Audio encoder uplink packet loss fraction",
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001483 LineStyle::kLine, PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001484 auto GetAnaPacketLoss =
1485 [](const LoggedAudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001486 if (ana_event.config.uplink_packet_loss_fraction)
1487 return rtc::Optional<float>(static_cast<float>(
1488 *ana_event.config.uplink_packet_loss_fraction));
1489 return rtc::Optional<float>();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001490 };
1491 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
1492 GetAnaPacketLoss, parsed_log_.audio_network_adaptation_events(),
1493 begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001494 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001495 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1496 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
1497 kTopMargin);
1498 plot->SetTitle("Reported audio encoder lost packets");
1499}
1500
1501void EventLogAnalyzer::CreateAudioEncoderEnableFecGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001502 TimeSeries time_series("Audio encoder FEC", LineStyle::kLine,
1503 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001504 auto GetAnaFecEnabled =
1505 [](const LoggedAudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001506 if (ana_event.config.enable_fec)
1507 return rtc::Optional<float>(
1508 static_cast<float>(*ana_event.config.enable_fec));
1509 return rtc::Optional<float>();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001510 };
1511 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
1512 GetAnaFecEnabled, parsed_log_.audio_network_adaptation_events(),
1513 begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001514 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001515 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1516 plot->SetSuggestedYAxis(0, 1, "FEC (false/true)", kBottomMargin, kTopMargin);
1517 plot->SetTitle("Reported audio encoder FEC");
1518}
1519
1520void EventLogAnalyzer::CreateAudioEncoderEnableDtxGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001521 TimeSeries time_series("Audio encoder DTX", LineStyle::kLine,
1522 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001523 auto GetAnaDtxEnabled =
1524 [](const LoggedAudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001525 if (ana_event.config.enable_dtx)
1526 return rtc::Optional<float>(
1527 static_cast<float>(*ana_event.config.enable_dtx));
1528 return rtc::Optional<float>();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001529 };
1530 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
1531 GetAnaDtxEnabled, parsed_log_.audio_network_adaptation_events(),
1532 begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001533 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001534 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1535 plot->SetSuggestedYAxis(0, 1, "DTX (false/true)", kBottomMargin, kTopMargin);
1536 plot->SetTitle("Reported audio encoder DTX");
1537}
1538
1539void EventLogAnalyzer::CreateAudioEncoderNumChannelsGraph(Plot* plot) {
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001540 TimeSeries time_series("Audio encoder number of channels", LineStyle::kLine,
1541 PointStyle::kHighlight);
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001542 auto GetAnaNumChannels =
1543 [](const LoggedAudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001544 if (ana_event.config.num_channels)
1545 return rtc::Optional<float>(
1546 static_cast<float>(*ana_event.config.num_channels));
1547 return rtc::Optional<float>();
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001548 };
1549 ProcessPoints<LoggedAudioNetworkAdaptationEvent>(
1550 GetAnaNumChannels, parsed_log_.audio_network_adaptation_events(),
1551 begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001552 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001553 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1554 plot->SetSuggestedYAxis(0, 1, "Number of channels (1 (mono)/2 (stereo))",
1555 kBottomMargin, kTopMargin);
1556 plot->SetTitle("Reported audio encoder number of channels");
1557}
henrik.lundin3c938fc2017-06-14 06:09:58 -07001558
1559class NetEqStreamInput : public test::NetEqInput {
1560 public:
1561 // Does not take any ownership, and all pointers must refer to valid objects
1562 // that outlive the one constructed.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001563 NetEqStreamInput(const std::vector<LoggedRtpPacketIncoming>* packet_stream,
1564 const std::vector<int64_t>* output_events_us,
1565 rtc::Optional<int64_t> end_time_us)
henrik.lundin3c938fc2017-06-14 06:09:58 -07001566 : packet_stream_(*packet_stream),
1567 packet_stream_it_(packet_stream_.begin()),
1568 output_events_us_it_(output_events_us->begin()),
1569 output_events_us_end_(output_events_us->end()),
1570 end_time_us_(end_time_us) {
1571 RTC_DCHECK(packet_stream);
1572 RTC_DCHECK(output_events_us);
1573 }
1574
1575 rtc::Optional<int64_t> NextPacketTime() const override {
1576 if (packet_stream_it_ == packet_stream_.end()) {
Oskar Sundbom3928dbc2017-11-16 10:53:09 +01001577 return rtc::nullopt;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001578 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001579 if (end_time_us_ && packet_stream_it_->rtp.log_time_us() > *end_time_us_) {
Oskar Sundbom3928dbc2017-11-16 10:53:09 +01001580 return rtc::nullopt;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001581 }
1582 // Convert from us to ms.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001583 return packet_stream_it_->rtp.log_time_us() / 1000;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001584 }
1585
1586 rtc::Optional<int64_t> NextOutputEventTime() const override {
1587 if (output_events_us_it_ == output_events_us_end_) {
Oskar Sundbom3928dbc2017-11-16 10:53:09 +01001588 return rtc::nullopt;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001589 }
1590 if (end_time_us_ && *output_events_us_it_ > *end_time_us_) {
Oskar Sundbom3928dbc2017-11-16 10:53:09 +01001591 return rtc::nullopt;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001592 }
1593 // Convert from us to ms.
Oskar Sundbom3928dbc2017-11-16 10:53:09 +01001594 return rtc::checked_cast<int64_t>(*output_events_us_it_ / 1000);
henrik.lundin3c938fc2017-06-14 06:09:58 -07001595 }
1596
1597 std::unique_ptr<PacketData> PopPacket() override {
1598 if (packet_stream_it_ == packet_stream_.end()) {
1599 return std::unique_ptr<PacketData>();
1600 }
1601 std::unique_ptr<PacketData> packet_data(new PacketData());
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001602 packet_data->header = packet_stream_it_->rtp.header;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001603 // Convert from us to ms.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001604 packet_data->time_ms = packet_stream_it_->rtp.log_time_us() / 1000.0;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001605
1606 // This is a header-only "dummy" packet. Set the payload to all zeros, with
1607 // length according to the virtual length.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001608 packet_data->payload.SetSize(packet_stream_it_->rtp.total_length -
1609 packet_stream_it_->rtp.header_length);
henrik.lundin3c938fc2017-06-14 06:09:58 -07001610 std::fill_n(packet_data->payload.data(), packet_data->payload.size(), 0);
1611
1612 ++packet_stream_it_;
1613 return packet_data;
1614 }
1615
1616 void AdvanceOutputEvent() override {
1617 if (output_events_us_it_ != output_events_us_end_) {
1618 ++output_events_us_it_;
1619 }
1620 }
1621
1622 bool ended() const override { return !NextEventTime(); }
1623
1624 rtc::Optional<RTPHeader> NextHeader() const override {
1625 if (packet_stream_it_ == packet_stream_.end()) {
Oskar Sundbom3928dbc2017-11-16 10:53:09 +01001626 return rtc::nullopt;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001627 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001628 return packet_stream_it_->rtp.header;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001629 }
1630
1631 private:
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001632 const std::vector<LoggedRtpPacketIncoming>& packet_stream_;
1633 std::vector<LoggedRtpPacketIncoming>::const_iterator packet_stream_it_;
1634 std::vector<int64_t>::const_iterator output_events_us_it_;
1635 const std::vector<int64_t>::const_iterator output_events_us_end_;
1636 const rtc::Optional<int64_t> end_time_us_;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001637};
1638
1639namespace {
1640// Creates a NetEq test object and all necessary input and output helpers. Runs
1641// the test and returns the NetEqDelayAnalyzer object that was used to
1642// instrument the test.
1643std::unique_ptr<test::NetEqDelayAnalyzer> CreateNetEqTestAndRun(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001644 const std::vector<LoggedRtpPacketIncoming>* packet_stream,
1645 const std::vector<int64_t>* output_events_us,
1646 rtc::Optional<int64_t> end_time_us,
henrik.lundin3c938fc2017-06-14 06:09:58 -07001647 const std::string& replacement_file_name,
1648 int file_sample_rate_hz) {
1649 std::unique_ptr<test::NetEqInput> input(
1650 new NetEqStreamInput(packet_stream, output_events_us, end_time_us));
1651
1652 constexpr int kReplacementPt = 127;
1653 std::set<uint8_t> cn_types;
1654 std::set<uint8_t> forbidden_types;
1655 input.reset(new test::NetEqReplacementInput(std::move(input), kReplacementPt,
1656 cn_types, forbidden_types));
1657
1658 NetEq::Config config;
1659 config.max_packets_in_buffer = 200;
1660 config.enable_fast_accelerate = true;
1661
1662 std::unique_ptr<test::VoidAudioSink> output(new test::VoidAudioSink());
1663
1664 test::NetEqTest::DecoderMap codecs;
1665
1666 // Create a "replacement decoder" that produces the decoded audio by reading
1667 // from a file rather than from the encoded payloads.
1668 std::unique_ptr<test::ResampleInputAudioFile> replacement_file(
1669 new test::ResampleInputAudioFile(replacement_file_name,
1670 file_sample_rate_hz));
1671 replacement_file->set_output_rate_hz(48000);
1672 std::unique_ptr<AudioDecoder> replacement_decoder(
1673 new test::FakeDecodeFromFile(std::move(replacement_file), 48000, false));
1674 test::NetEqTest::ExtDecoderMap ext_codecs;
1675 ext_codecs[kReplacementPt] = {replacement_decoder.get(),
1676 NetEqDecoder::kDecoderArbitrary,
1677 "replacement codec"};
1678
1679 std::unique_ptr<test::NetEqDelayAnalyzer> delay_cb(
1680 new test::NetEqDelayAnalyzer);
1681 test::DefaultNetEqTestErrorCallback error_cb;
1682 test::NetEqTest::Callbacks callbacks;
1683 callbacks.error_callback = &error_cb;
1684 callbacks.post_insert_packet = delay_cb.get();
1685 callbacks.get_audio_callback = delay_cb.get();
1686
1687 test::NetEqTest test(config, codecs, ext_codecs, std::move(input),
1688 std::move(output), callbacks);
1689 test.Run();
1690 return delay_cb;
1691}
1692} // namespace
1693
1694// Plots the jitter buffer delay profile. This will plot only for the first
1695// incoming audio SSRC. If the stream contains more than one incoming audio
1696// SSRC, all but the first will be ignored.
1697void EventLogAnalyzer::CreateAudioJitterBufferGraph(
1698 const std::string& replacement_file_name,
1699 int file_sample_rate_hz,
1700 Plot* plot) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001701 const std::vector<LoggedRtpPacketIncoming>* audio_packets = nullptr;
1702 uint32_t ssrc;
1703 for (const auto& stream : parsed_log_.incoming_rtp_packets_by_ssrc()) {
1704 if (IsAudioSsrc(kIncomingPacket, stream.ssrc)) {
1705 audio_packets = &stream.incoming_packets;
1706 ssrc = stream.ssrc;
1707 break;
1708 }
1709 }
1710 if (audio_packets == nullptr) {
henrik.lundin3c938fc2017-06-14 06:09:58 -07001711 // No incoming audio stream found.
1712 return;
1713 }
1714
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001715 std::map<uint32_t, std::vector<int64_t>>::const_iterator output_events_it =
1716 parsed_log_.audio_playout_events().find(ssrc);
1717 if (output_events_it == parsed_log_.audio_playout_events().end()) {
henrik.lundin3c938fc2017-06-14 06:09:58 -07001718 // Could not find output events with SSRC matching the input audio stream.
1719 // Using the first available stream of output events.
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001720 output_events_it = parsed_log_.audio_playout_events().cbegin();
henrik.lundin3c938fc2017-06-14 06:09:58 -07001721 }
1722
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001723 rtc::Optional<int64_t> end_time_us =
henrik.lundin3c938fc2017-06-14 06:09:58 -07001724 log_segments_.empty()
Oskar Sundbom3928dbc2017-11-16 10:53:09 +01001725 ? rtc::nullopt
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001726 : rtc::Optional<int64_t>(log_segments_.front().second);
henrik.lundin3c938fc2017-06-14 06:09:58 -07001727
1728 auto delay_cb = CreateNetEqTestAndRun(
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001729 audio_packets, &output_events_it->second, end_time_us,
henrik.lundin3c938fc2017-06-14 06:09:58 -07001730 replacement_file_name, file_sample_rate_hz);
1731
1732 std::vector<float> send_times_s;
1733 std::vector<float> arrival_delay_ms;
1734 std::vector<float> corrected_arrival_delay_ms;
1735 std::vector<rtc::Optional<float>> playout_delay_ms;
1736 std::vector<rtc::Optional<float>> target_delay_ms;
1737 delay_cb->CreateGraphs(&send_times_s, &arrival_delay_ms,
1738 &corrected_arrival_delay_ms, &playout_delay_ms,
1739 &target_delay_ms);
1740 RTC_DCHECK_EQ(send_times_s.size(), arrival_delay_ms.size());
1741 RTC_DCHECK_EQ(send_times_s.size(), corrected_arrival_delay_ms.size());
1742 RTC_DCHECK_EQ(send_times_s.size(), playout_delay_ms.size());
1743 RTC_DCHECK_EQ(send_times_s.size(), target_delay_ms.size());
1744
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001745 std::map<uint32_t, TimeSeries> time_series_packet_arrival;
1746 std::map<uint32_t, TimeSeries> time_series_relative_packet_arrival;
1747 std::map<uint32_t, TimeSeries> time_series_play_time;
1748 std::map<uint32_t, TimeSeries> time_series_target_time;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001749 float min_y_axis = 0.f;
1750 float max_y_axis = 0.f;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001751 for (size_t i = 0; i < send_times_s.size(); ++i) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001752 time_series_packet_arrival[ssrc].points.emplace_back(
henrik.lundin3c938fc2017-06-14 06:09:58 -07001753 TimeSeriesPoint(send_times_s[i], arrival_delay_ms[i]));
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001754 time_series_relative_packet_arrival[ssrc].points.emplace_back(
henrik.lundin3c938fc2017-06-14 06:09:58 -07001755 TimeSeriesPoint(send_times_s[i], corrected_arrival_delay_ms[i]));
1756 min_y_axis = std::min(min_y_axis, corrected_arrival_delay_ms[i]);
1757 max_y_axis = std::max(max_y_axis, corrected_arrival_delay_ms[i]);
1758 if (playout_delay_ms[i]) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001759 time_series_play_time[ssrc].points.emplace_back(
henrik.lundin3c938fc2017-06-14 06:09:58 -07001760 TimeSeriesPoint(send_times_s[i], *playout_delay_ms[i]));
1761 min_y_axis = std::min(min_y_axis, *playout_delay_ms[i]);
1762 max_y_axis = std::max(max_y_axis, *playout_delay_ms[i]);
1763 }
1764 if (target_delay_ms[i]) {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001765 time_series_target_time[ssrc].points.emplace_back(
henrik.lundin3c938fc2017-06-14 06:09:58 -07001766 TimeSeriesPoint(send_times_s[i], *target_delay_ms[i]));
1767 min_y_axis = std::min(min_y_axis, *target_delay_ms[i]);
1768 max_y_axis = std::max(max_y_axis, *target_delay_ms[i]);
1769 }
1770 }
1771
1772 // This code is adapted for a single stream. The creation of the streams above
1773 // guarantee that no more than one steam is included. If multiple streams are
1774 // to be plotted, they should likely be given distinct labels below.
1775 RTC_DCHECK_EQ(time_series_relative_packet_arrival.size(), 1);
1776 for (auto& series : time_series_relative_packet_arrival) {
1777 series.second.label = "Relative packet arrival delay";
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001778 series.second.line_style = LineStyle::kLine;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001779 plot->AppendTimeSeries(std::move(series.second));
1780 }
1781 RTC_DCHECK_EQ(time_series_play_time.size(), 1);
1782 for (auto& series : time_series_play_time) {
1783 series.second.label = "Playout delay";
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001784 series.second.line_style = LineStyle::kLine;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001785 plot->AppendTimeSeries(std::move(series.second));
1786 }
1787 RTC_DCHECK_EQ(time_series_target_time.size(), 1);
1788 for (auto& series : time_series_target_time) {
1789 series.second.label = "Target delay";
Bjorn Tereliusb577d5e2017-11-10 16:21:34 +01001790 series.second.line_style = LineStyle::kLine;
1791 series.second.point_style = PointStyle::kHighlight;
henrik.lundin3c938fc2017-06-14 06:09:58 -07001792 plot->AppendTimeSeries(std::move(series.second));
1793 }
1794
1795 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1796 plot->SetYAxis(min_y_axis, max_y_axis, "Relative delay (ms)", kBottomMargin,
1797 kTopMargin);
1798 plot->SetTitle("NetEq timing");
1799}
Bjorn Terelius2eb31882017-11-30 15:15:25 +01001800
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001801void EventLogAnalyzer::CreateIceCandidatePairConfigGraph(Plot* plot) {
1802 std::map<uint32_t, TimeSeries> configs_by_cp_id;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001803 for (const auto& config : parsed_log_.ice_candidate_pair_configs()) {
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001804 if (configs_by_cp_id.find(config.candidate_pair_id) ==
1805 configs_by_cp_id.end()) {
1806 const std::string candidate_pair_desc =
1807 GetCandidatePairLogDescriptionAsString(config);
Qingsi Wang93a84392018-01-30 17:13:09 -08001808 configs_by_cp_id[config.candidate_pair_id] =
1809 TimeSeries("[" + std::to_string(config.candidate_pair_id) + "]" +
1810 candidate_pair_desc,
1811 LineStyle::kNone, PointStyle::kHighlight);
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001812 candidate_pair_desc_by_id_[config.candidate_pair_id] =
1813 candidate_pair_desc;
1814 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001815 float x = ToCallTime(config.log_time_us());
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001816 float y = static_cast<float>(config.type);
1817 configs_by_cp_id[config.candidate_pair_id].points.emplace_back(x, y);
1818 }
1819
1820 // TODO(qingsi): There can be a large number of candidate pairs generated by
1821 // certain calls and the frontend cannot render the chart in this case due to
1822 // the failure of generating a palette with the same number of colors.
1823 for (auto& kv : configs_by_cp_id) {
1824 plot->AppendTimeSeries(std::move(kv.second));
1825 }
1826
1827 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1828 plot->SetSuggestedYAxis(0, 3, "Numeric Config Type", kBottomMargin,
1829 kTopMargin);
1830 plot->SetTitle("[IceEventLog] ICE candidate pair configs");
1831}
1832
1833std::string EventLogAnalyzer::GetCandidatePairLogDescriptionFromId(
1834 uint32_t candidate_pair_id) {
1835 if (candidate_pair_desc_by_id_.find(candidate_pair_id) !=
1836 candidate_pair_desc_by_id_.end()) {
1837 return candidate_pair_desc_by_id_[candidate_pair_id];
1838 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001839 for (const auto& config : parsed_log_.ice_candidate_pair_configs()) {
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001840 // TODO(qingsi): Add the handling of the "Updated" config event after the
1841 // visualization of property change for candidate pairs is introduced.
1842 if (candidate_pair_desc_by_id_.find(config.candidate_pair_id) ==
1843 candidate_pair_desc_by_id_.end()) {
1844 const std::string candidate_pair_desc =
1845 GetCandidatePairLogDescriptionAsString(config);
1846 candidate_pair_desc_by_id_[config.candidate_pair_id] =
1847 candidate_pair_desc;
1848 }
1849 }
1850 return candidate_pair_desc_by_id_[candidate_pair_id];
1851}
1852
1853void EventLogAnalyzer::CreateIceConnectivityCheckGraph(Plot* plot) {
1854 std::map<uint32_t, TimeSeries> checks_by_cp_id;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001855 for (const auto& event : parsed_log_.ice_candidate_pair_events()) {
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001856 if (checks_by_cp_id.find(event.candidate_pair_id) ==
1857 checks_by_cp_id.end()) {
1858 checks_by_cp_id[event.candidate_pair_id] = TimeSeries(
Qingsi Wang93a84392018-01-30 17:13:09 -08001859 "[" + std::to_string(event.candidate_pair_id) + "]" +
1860 GetCandidatePairLogDescriptionFromId(event.candidate_pair_id),
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001861 LineStyle::kNone, PointStyle::kHighlight);
1862 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001863 float x = ToCallTime(event.log_time_us());
Qingsi Wang8eca1ff2018-02-02 11:49:44 -08001864 float y = static_cast<float>(event.type);
1865 checks_by_cp_id[event.candidate_pair_id].points.emplace_back(x, y);
1866 }
1867
1868 // TODO(qingsi): The same issue as in CreateIceCandidatePairConfigGraph.
1869 for (auto& kv : checks_by_cp_id) {
1870 plot->AppendTimeSeries(std::move(kv.second));
1871 }
1872
1873 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1874 plot->SetSuggestedYAxis(0, 4, "Numeric Connectivity State", kBottomMargin,
1875 kTopMargin);
1876 plot->SetTitle("[IceEventLog] ICE connectivity checks");
1877}
1878
Bjorn Terelius2eb31882017-11-30 15:15:25 +01001879void EventLogAnalyzer::PrintNotifications(FILE* file) {
Bjorn Terelius2eb31882017-11-30 15:15:25 +01001880 fprintf(file, "========== TRIAGE NOTIFICATIONS ==========\n");
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001881 for (const auto& alert : incoming_rtp_recv_time_gaps_) {
1882 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
1883 }
1884 for (const auto& alert : incoming_rtcp_recv_time_gaps_) {
1885 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
1886 }
1887 for (const auto& alert : outgoing_rtp_send_time_gaps_) {
1888 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
1889 }
1890 for (const auto& alert : outgoing_rtcp_send_time_gaps_) {
1891 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
1892 }
1893 for (const auto& alert : incoming_seq_num_jumps_) {
1894 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
1895 }
1896 for (const auto& alert : incoming_capture_time_jumps_) {
1897 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
1898 }
1899 for (const auto& alert : outgoing_seq_num_jumps_) {
1900 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
1901 }
1902 for (const auto& alert : outgoing_capture_time_jumps_) {
1903 fprintf(file, "%3.3lf s : %s\n", alert.Time(), alert.ToString().c_str());
1904 }
1905 for (const auto& alert : outgoing_high_loss_alerts_) {
1906 fprintf(file, " : %s\n", alert.ToString().c_str());
Bjorn Terelius2eb31882017-11-30 15:15:25 +01001907 }
1908 fprintf(file, "========== END TRIAGE NOTIFICATIONS ==========\n");
1909}
1910
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02001911void EventLogAnalyzer::CreateStreamGapAlerts(PacketDirection direction) {
1912 // With 100 packets/s (~800kbps), false positives would require 10 s without
1913 // data.
1914 constexpr int64_t kMaxSeqNumJump = 1000;
1915 // With a 90 kHz clock, false positives would require 10 s without data.
1916 constexpr int64_t kMaxCaptureTimeJump = 900000;
1917
1918 int64_t end_time_us = log_segments_.empty()
1919 ? std::numeric_limits<int64_t>::max()
1920 : log_segments_.front().second;
1921
1922 SeqNumUnwrapper<uint16_t> seq_num_unwrapper;
1923 rtc::Optional<int64_t> last_seq_num;
1924 SeqNumUnwrapper<uint32_t> capture_time_unwrapper;
1925 rtc::Optional<int64_t> last_capture_time;
1926 // Check for gaps in sequence numbers and capture timestamps.
1927 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
1928 for (const auto& packet : stream.packet_view) {
1929 if (packet.log_time_us() > end_time_us) {
1930 // Only process the first (LOG_START, LOG_END) segment.
1931 break;
1932 }
1933
1934 int64_t seq_num = seq_num_unwrapper.Unwrap(packet.header.sequenceNumber);
1935 if (last_seq_num.has_value() &&
1936 std::abs(seq_num - last_seq_num.value()) > kMaxSeqNumJump) {
1937 Alert_SeqNumJump(direction, ToCallTime(packet.log_time_us()),
1938 packet.header.ssrc);
1939 }
1940 last_seq_num.emplace(seq_num);
1941
1942 int64_t capture_time =
1943 capture_time_unwrapper.Unwrap(packet.header.timestamp);
1944 if (last_capture_time.has_value() &&
1945 std::abs(capture_time - last_capture_time.value()) >
1946 kMaxCaptureTimeJump) {
1947 Alert_CaptureTimeJump(direction, ToCallTime(packet.log_time_us()),
1948 packet.header.ssrc);
1949 }
1950 last_capture_time.emplace(capture_time);
1951 }
1952 }
1953}
1954
1955void EventLogAnalyzer::CreateTransmissionGapAlerts(PacketDirection direction) {
1956 constexpr int64_t kMaxRtpTransmissionGap = 500000;
1957 constexpr int64_t kMaxRtcpTransmissionGap = 2000000;
1958 int64_t end_time_us = log_segments_.empty()
1959 ? std::numeric_limits<int64_t>::max()
1960 : log_segments_.front().second;
1961
1962 // TODO(terelius): The parser could provide a list of all packets, ordered
1963 // by time, for each direction.
1964 std::multimap<int64_t, const LoggedRtpPacket*> rtp_in_direction;
1965 for (const auto& stream : parsed_log_.rtp_packets_by_ssrc(direction)) {
1966 for (const LoggedRtpPacket& rtp_packet : stream.packet_view)
1967 rtp_in_direction.emplace(rtp_packet.log_time_us(), &rtp_packet);
1968 }
1969 rtc::Optional<int64_t> last_rtp_time;
1970 for (const auto& kv : rtp_in_direction) {
1971 int64_t timestamp = kv.first;
1972 if (timestamp > end_time_us) {
1973 // Only process the first (LOG_START, LOG_END) segment.
1974 break;
1975 }
1976 int64_t duration = timestamp - last_rtp_time.value_or(0);
1977 if (last_rtp_time.has_value() && duration > kMaxRtpTransmissionGap) {
1978 // No packet sent/received for more than 500 ms.
1979 Alert_RtpLogTimeGap(direction, ToCallTime(timestamp), duration / 1000);
1980 }
1981 last_rtp_time.emplace(timestamp);
1982 }
1983
1984 rtc::Optional<int64_t> last_rtcp_time;
1985 if (direction == kIncomingPacket) {
1986 for (const auto& rtcp : parsed_log_.incoming_rtcp_packets()) {
1987 if (rtcp.log_time_us() > end_time_us) {
1988 // Only process the first (LOG_START, LOG_END) segment.
1989 break;
1990 }
1991 int64_t duration = rtcp.log_time_us() - last_rtcp_time.value_or(0);
1992 if (last_rtcp_time.has_value() && duration > kMaxRtcpTransmissionGap) {
1993 // No feedback sent/received for more than 2000 ms.
1994 Alert_RtcpLogTimeGap(direction, ToCallTime(rtcp.log_time_us()),
1995 duration / 1000);
1996 }
1997 last_rtcp_time.emplace(rtcp.log_time_us());
1998 }
1999 } else {
2000 for (const auto& rtcp : parsed_log_.outgoing_rtcp_packets()) {
2001 if (rtcp.log_time_us() > end_time_us) {
2002 // Only process the first (LOG_START, LOG_END) segment.
2003 break;
2004 }
2005 int64_t duration = rtcp.log_time_us() - last_rtcp_time.value_or(0);
2006 if (last_rtcp_time.has_value() && duration > kMaxRtcpTransmissionGap) {
2007 // No feedback sent/received for more than 2000 ms.
2008 Alert_RtcpLogTimeGap(direction, ToCallTime(rtcp.log_time_us()),
2009 duration / 1000);
2010 }
2011 last_rtcp_time.emplace(rtcp.log_time_us());
2012 }
2013 }
2014}
2015
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002016// TODO(terelius): Notifications could possibly be generated by the same code
2017// that produces the graphs. There is some code duplication that could be
2018// avoided, but that might be solved anyway when we move functionality from the
2019// analyzer to the parser.
2020void EventLogAnalyzer::CreateTriageNotifications() {
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002021 CreateStreamGapAlerts(kIncomingPacket);
2022 CreateStreamGapAlerts(kOutgoingPacket);
2023 CreateTransmissionGapAlerts(kIncomingPacket);
2024 CreateTransmissionGapAlerts(kOutgoingPacket);
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002025
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002026 int64_t end_time_us = log_segments_.empty()
2027 ? std::numeric_limits<int64_t>::max()
2028 : log_segments_.front().second;
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002029
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002030 constexpr double kMaxLossFraction = 0.05;
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002031 // Loss feedback
2032 int64_t total_lost_packets = 0;
2033 int64_t total_expected_packets = 0;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002034 for (auto& bwe_update : parsed_log_.bwe_loss_updates()) {
2035 if (bwe_update.log_time_us() > end_time_us) {
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002036 // Only process the first (LOG_START, LOG_END) segment.
2037 break;
2038 }
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002039 int64_t lost_packets = static_cast<double>(bwe_update.fraction_lost) / 255 *
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002040 bwe_update.expected_packets;
2041 total_lost_packets += lost_packets;
2042 total_expected_packets += bwe_update.expected_packets;
2043 }
2044 double avg_outgoing_loss =
2045 static_cast<double>(total_lost_packets) / total_expected_packets;
Bjorn Tereliusc4ca1d32018-04-27 14:33:34 +02002046 if (avg_outgoing_loss > kMaxLossFraction) {
2047 Alert_OutgoingHighLoss(avg_outgoing_loss);
Bjorn Terelius2eb31882017-11-30 15:15:25 +01002048 }
2049}
2050
terelius54ce6802016-07-13 06:44:41 -07002051} // namespace webrtc