blob: 00df00690f3dff295973b9afc79f8154d29b8795 [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>
14#include <limits>
15#include <map>
16#include <sstream>
17#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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_coding/neteq/tools/audio_sink.h"
28#include "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
29#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
30#include "modules/audio_coding/neteq/tools/neteq_replacement_input.h"
31#include "modules/audio_coding/neteq/tools/neteq_test.h"
32#include "modules/audio_coding/neteq/tools/resample_input_audio_file.h"
Bjorn Terelius6984ad22017-10-24 12:19:45 +020033#include "modules/congestion_controller/acknowledged_bitrate_estimator.h"
34#include "modules/congestion_controller/bitrate_estimator.h"
Bjorn Terelius28db2662017-10-04 14:22:43 +020035#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "modules/congestion_controller/include/send_side_congestion_controller.h"
37#include "modules/include/module_common_types.h"
Niels Möllerfd6c0912017-10-31 10:19:10 +010038#include "modules/pacing/packet_router.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "modules/rtp_rtcp/include/rtp_rtcp.h"
40#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
41#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
42#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
43#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
44#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
45#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
46#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
47#include "modules/rtp_rtcp/source/rtp_utility.h"
48#include "rtc_base/checks.h"
49#include "rtc_base/format_macros.h"
50#include "rtc_base/logging.h"
Bjorn Terelius0295a962017-10-25 17:42:41 +020051#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "rtc_base/ptr_util.h"
53#include "rtc_base/rate_statistics.h"
terelius54ce6802016-07-13 06:44:41 -070054
Bjorn Terelius6984ad22017-10-24 12:19:45 +020055#ifndef BWE_TEST_LOGGING_COMPILE_TIME_ENABLE
56#define BWE_TEST_LOGGING_COMPILE_TIME_ENABLE 0
57#endif // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE
58
tereliusdc35dcd2016-08-01 12:03:27 -070059namespace webrtc {
60namespace plotting {
61
terelius54ce6802016-07-13 06:44:41 -070062namespace {
63
elad.alonec304f92017-03-08 05:03:53 -080064void SortPacketFeedbackVector(std::vector<PacketFeedback>* vec) {
65 auto pred = [](const PacketFeedback& packet_feedback) {
66 return packet_feedback.arrival_time_ms == PacketFeedback::kNotReceived;
67 };
68 vec->erase(std::remove_if(vec->begin(), vec->end(), pred), vec->end());
69 std::sort(vec->begin(), vec->end(), PacketFeedbackComparator());
70}
71
terelius54ce6802016-07-13 06:44:41 -070072std::string SsrcToString(uint32_t ssrc) {
73 std::stringstream ss;
74 ss << "SSRC " << ssrc;
75 return ss.str();
76}
77
78// Checks whether an SSRC is contained in the list of desired SSRCs.
79// Note that an empty SSRC list matches every SSRC.
80bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
81 if (desired_ssrc.size() == 0)
82 return true;
83 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
84 desired_ssrc.end();
85}
86
87double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
88 // The timestamp is a fixed point representation with 6 bits for seconds
89 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
90 // time in seconds and then multiply by 1000000 to convert to microseconds.
91 static constexpr double kTimestampToMicroSec =
tereliusccbbf8d2016-08-10 07:34:28 -070092 1000000.0 / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -070093 return abs_send_time * kTimestampToMicroSec;
94}
95
96// Computes the difference |later| - |earlier| where |later| and |earlier|
97// are counters that wrap at |modulus|. The difference is chosen to have the
98// least absolute value. For example if |modulus| is 8, then the difference will
99// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
100// be in [-4, 4].
101int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
102 RTC_DCHECK_LE(1, modulus);
103 RTC_DCHECK_LT(later, modulus);
104 RTC_DCHECK_LT(earlier, modulus);
105 int64_t difference =
106 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
107 int64_t max_difference = modulus / 2;
108 int64_t min_difference = max_difference - modulus + 1;
109 if (difference > max_difference) {
110 difference -= modulus;
111 }
112 if (difference < min_difference) {
113 difference += modulus;
114 }
terelius6addf492016-08-23 17:34:07 -0700115 if (difference > max_difference / 2 || difference < min_difference / 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
117 << " expected to be in the range ("
118 << min_difference / 2 << "," << max_difference / 2
119 << ") but is " << difference
120 << ". Correct unwrapping is uncertain.";
terelius6addf492016-08-23 17:34:07 -0700121 }
terelius54ce6802016-07-13 06:44:41 -0700122 return difference;
123}
124
ivocaac9d6f2016-09-22 07:01:47 -0700125// Return default values for header extensions, to use on streams without stored
126// mapping data. Currently this only applies to audio streams, since the mapping
127// is not stored in the event log.
128// TODO(ivoc): Remove this once this mapping is stored in the event log for
129// audio streams. Tracking bug: webrtc:6399
130webrtc::RtpHeaderExtensionMap GetDefaultHeaderExtensionMap() {
131 webrtc::RtpHeaderExtensionMap default_map;
danilchap4aecc582016-11-15 09:21:00 -0800132 default_map.Register<AudioLevel>(webrtc::RtpExtension::kAudioLevelDefaultId);
terelius007d5622017-08-08 05:40:26 -0700133 default_map.Register<TransmissionOffset>(
134 webrtc::RtpExtension::kTimestampOffsetDefaultId);
danilchap4aecc582016-11-15 09:21:00 -0800135 default_map.Register<AbsoluteSendTime>(
ivocaac9d6f2016-09-22 07:01:47 -0700136 webrtc::RtpExtension::kAbsSendTimeDefaultId);
terelius007d5622017-08-08 05:40:26 -0700137 default_map.Register<VideoOrientation>(
138 webrtc::RtpExtension::kVideoRotationDefaultId);
139 default_map.Register<VideoContentTypeExtension>(
140 webrtc::RtpExtension::kVideoContentTypeDefaultId);
141 default_map.Register<VideoTimingExtension>(
142 webrtc::RtpExtension::kVideoTimingDefaultId);
143 default_map.Register<TransportSequenceNumber>(
144 webrtc::RtpExtension::kTransportSequenceNumberDefaultId);
145 default_map.Register<PlayoutDelayLimits>(
146 webrtc::RtpExtension::kPlayoutDelayDefaultId);
ivocaac9d6f2016-09-22 07:01:47 -0700147 return default_map;
148}
149
tereliusdc35dcd2016-08-01 12:03:27 -0700150constexpr float kLeftMargin = 0.01f;
151constexpr float kRightMargin = 0.02f;
152constexpr float kBottomMargin = 0.02f;
153constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700154
terelius53dc23c2017-03-13 05:24:05 -0700155rtc::Optional<double> NetworkDelayDiff_AbsSendTime(
156 const LoggedRtpPacket& old_packet,
157 const LoggedRtpPacket& new_packet) {
158 if (old_packet.header.extension.hasAbsoluteSendTime &&
159 new_packet.header.extension.hasAbsoluteSendTime) {
160 int64_t send_time_diff = WrappingDifference(
161 new_packet.header.extension.absoluteSendTime,
162 old_packet.header.extension.absoluteSendTime, 1ul << 24);
163 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
164 double delay_change_us =
165 recv_time_diff - AbsSendTimeToMicroseconds(send_time_diff);
166 return rtc::Optional<double>(delay_change_us / 1000);
167 } else {
168 return rtc::Optional<double>();
terelius6addf492016-08-23 17:34:07 -0700169 }
170}
171
terelius53dc23c2017-03-13 05:24:05 -0700172rtc::Optional<double> NetworkDelayDiff_CaptureTime(
173 const LoggedRtpPacket& old_packet,
174 const LoggedRtpPacket& new_packet) {
175 int64_t send_time_diff = WrappingDifference(
176 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
177 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
178
179 const double kVideoSampleRate = 90000;
180 // TODO(terelius): We treat all streams as video for now, even though
181 // audio might be sampled at e.g. 16kHz, because it is really difficult to
182 // figure out the true sampling rate of a stream. The effect is that the
183 // delay will be scaled incorrectly for non-video streams.
184
185 double delay_change =
186 static_cast<double>(recv_time_diff) / 1000 -
187 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
188 if (delay_change < -10000 || 10000 < delay_change) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100189 RTC_LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
190 RTC_LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
191 << ", received time " << old_packet.timestamp;
192 RTC_LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
193 << ", received time " << new_packet.timestamp;
194 RTC_LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
195 << static_cast<double>(recv_time_diff) / 1000000 << "s";
196 RTC_LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
197 << static_cast<double>(send_time_diff) /
198 kVideoSampleRate
199 << "s";
terelius53dc23c2017-03-13 05:24:05 -0700200 }
201 return rtc::Optional<double>(delay_change);
202}
203
204// For each element in data, use |get_y()| to extract a y-coordinate and
205// store the result in a TimeSeries.
206template <typename DataType>
207void ProcessPoints(
208 rtc::FunctionView<rtc::Optional<float>(const DataType&)> get_y,
209 const std::vector<DataType>& data,
210 uint64_t begin_time,
211 TimeSeries* result) {
212 for (size_t i = 0; i < data.size(); i++) {
213 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
214 rtc::Optional<float> y = get_y(data[i]);
215 if (y)
216 result->points.emplace_back(x, *y);
217 }
218}
219
220// For each pair of adjacent elements in |data|, use |get_y| to extract a
terelius6addf492016-08-23 17:34:07 -0700221// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
222// will be the time of the second element in the pair.
terelius53dc23c2017-03-13 05:24:05 -0700223template <typename DataType, typename ResultType>
224void ProcessPairs(
225 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
226 const DataType&)> get_y,
227 const std::vector<DataType>& data,
228 uint64_t begin_time,
229 TimeSeries* result) {
tereliusccbbf8d2016-08-10 07:34:28 -0700230 for (size_t i = 1; i < data.size(); i++) {
231 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
terelius53dc23c2017-03-13 05:24:05 -0700232 rtc::Optional<ResultType> y = get_y(data[i - 1], data[i]);
233 if (y)
234 result->points.emplace_back(x, static_cast<float>(*y));
235 }
236}
237
238// For each element in data, use |extract()| to extract a y-coordinate and
239// store the result in a TimeSeries.
240template <typename DataType, typename ResultType>
241void AccumulatePoints(
242 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> extract,
243 const std::vector<DataType>& data,
244 uint64_t begin_time,
245 TimeSeries* result) {
246 ResultType sum = 0;
247 for (size_t i = 0; i < data.size(); i++) {
248 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
249 rtc::Optional<ResultType> y = extract(data[i]);
250 if (y) {
251 sum += *y;
252 result->points.emplace_back(x, static_cast<float>(sum));
253 }
254 }
255}
256
257// For each pair of adjacent elements in |data|, use |extract()| to extract a
258// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
259// will be the time of the second element in the pair.
260template <typename DataType, typename ResultType>
261void AccumulatePairs(
262 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
263 const DataType&)> extract,
264 const std::vector<DataType>& data,
265 uint64_t begin_time,
266 TimeSeries* result) {
267 ResultType sum = 0;
268 for (size_t i = 1; i < data.size(); i++) {
269 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
270 rtc::Optional<ResultType> y = extract(data[i - 1], data[i]);
271 if (y)
272 sum += *y;
273 result->points.emplace_back(x, static_cast<float>(sum));
tereliusccbbf8d2016-08-10 07:34:28 -0700274 }
275}
276
terelius6addf492016-08-23 17:34:07 -0700277// Calculates a moving average of |data| and stores the result in a TimeSeries.
278// A data point is generated every |step| microseconds from |begin_time|
279// to |end_time|. The value of each data point is the average of the data
280// during the preceeding |window_duration_us| microseconds.
terelius53dc23c2017-03-13 05:24:05 -0700281template <typename DataType, typename ResultType>
282void MovingAverage(
283 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> extract,
284 const std::vector<DataType>& data,
285 uint64_t begin_time,
286 uint64_t end_time,
287 uint64_t window_duration_us,
288 uint64_t step,
289 webrtc::plotting::TimeSeries* result) {
terelius6addf492016-08-23 17:34:07 -0700290 size_t window_index_begin = 0;
291 size_t window_index_end = 0;
terelius53dc23c2017-03-13 05:24:05 -0700292 ResultType sum_in_window = 0;
terelius6addf492016-08-23 17:34:07 -0700293
294 for (uint64_t t = begin_time; t < end_time + step; t += step) {
295 while (window_index_end < data.size() &&
296 data[window_index_end].timestamp < t) {
terelius53dc23c2017-03-13 05:24:05 -0700297 rtc::Optional<ResultType> value = extract(data[window_index_end]);
298 if (value)
299 sum_in_window += *value;
terelius6addf492016-08-23 17:34:07 -0700300 ++window_index_end;
301 }
302 while (window_index_begin < data.size() &&
303 data[window_index_begin].timestamp < t - window_duration_us) {
terelius53dc23c2017-03-13 05:24:05 -0700304 rtc::Optional<ResultType> value = extract(data[window_index_begin]);
305 if (value)
306 sum_in_window -= *value;
terelius6addf492016-08-23 17:34:07 -0700307 ++window_index_begin;
308 }
309 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
310 float x = static_cast<float>(t - begin_time) / 1000000;
terelius53dc23c2017-03-13 05:24:05 -0700311 float y = sum_in_window / window_duration_s;
terelius6addf492016-08-23 17:34:07 -0700312 result->points.emplace_back(x, y);
313 }
314}
315
terelius54ce6802016-07-13 06:44:41 -0700316} // namespace
317
terelius54ce6802016-07-13 06:44:41 -0700318EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
319 : parsed_log_(log), window_duration_(250000), step_(10000) {
320 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
321 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700322
terelius88e64e52016-07-19 01:51:06 -0700323 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700324 uint8_t header[IP_PACKET_SIZE];
325 size_t header_length;
326 size_t total_length;
327
perkjbbbad6d2017-05-19 06:30:28 -0700328 uint8_t last_incoming_rtcp_packet[IP_PACKET_SIZE];
329 uint8_t last_incoming_rtcp_packet_length = 0;
330
ivocaac9d6f2016-09-22 07:01:47 -0700331 // Make a default extension map for streams without configuration information.
332 // TODO(ivoc): Once configuration of audio streams is stored in the event log,
333 // this can be removed. Tracking bug: webrtc:6399
334 RtpHeaderExtensionMap default_extension_map = GetDefaultHeaderExtensionMap();
335
henrik.lundin3c938fc2017-06-14 06:09:58 -0700336 rtc::Optional<uint64_t> last_log_start;
337
terelius54ce6802016-07-13 06:44:41 -0700338 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
339 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700340 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
341 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
342 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700343 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
344 event_type != ParsedRtcEventLog::LOG_START &&
345 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700346 uint64_t timestamp = parsed_log_.GetTimestamp(i);
347 first_timestamp = std::min(first_timestamp, timestamp);
348 last_timestamp = std::max(last_timestamp, timestamp);
349 }
350
351 switch (parsed_log_.GetEventType(i)) {
352 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700353 rtclog::StreamConfig config = parsed_log_.GetVideoReceiveConfig(i);
perkj09e71da2017-05-22 03:26:49 -0700354 StreamId stream(config.remote_ssrc, kIncomingPacket);
terelius0740a202016-08-08 10:21:04 -0700355 video_ssrcs_.insert(stream);
perkj09e71da2017-05-22 03:26:49 -0700356 StreamId rtx_stream(config.rtx_ssrc, kIncomingPacket);
brandtr14742122017-01-27 04:53:07 -0800357 video_ssrcs_.insert(rtx_stream);
358 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700359 break;
360 }
361 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700362 std::vector<rtclog::StreamConfig> configs =
363 parsed_log_.GetVideoSendConfig(i);
terelius405f90c2017-06-01 03:50:31 -0700364 for (const auto& config : configs) {
365 StreamId stream(config.local_ssrc, kOutgoingPacket);
terelius8fbc7652017-05-31 02:03:16 -0700366 video_ssrcs_.insert(stream);
terelius405f90c2017-06-01 03:50:31 -0700367 StreamId rtx_stream(config.rtx_ssrc, kOutgoingPacket);
terelius8fbc7652017-05-31 02:03:16 -0700368 video_ssrcs_.insert(rtx_stream);
369 rtx_ssrcs_.insert(rtx_stream);
370 }
terelius88e64e52016-07-19 01:51:06 -0700371 break;
372 }
373 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700374 rtclog::StreamConfig config = parsed_log_.GetAudioReceiveConfig(i);
perkjac8f52d2017-05-22 09:36:28 -0700375 StreamId stream(config.remote_ssrc, kIncomingPacket);
ivoce0928d82016-10-10 05:12:51 -0700376 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700377 break;
378 }
379 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700380 rtclog::StreamConfig config = parsed_log_.GetAudioSendConfig(i);
perkjf4726992017-05-22 10:12:26 -0700381 StreamId stream(config.local_ssrc, kOutgoingPacket);
ivoce0928d82016-10-10 05:12:51 -0700382 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700383 break;
384 }
385 case ParsedRtcEventLog::RTP_EVENT: {
ilnika8e781a2017-06-12 01:02:46 -0700386 RtpHeaderExtensionMap* extension_map = parsed_log_.GetRtpHeader(
Elad Alon1d87b0e2017-10-03 15:01:03 +0200387 i, &direction, header, &header_length, &total_length, nullptr);
terelius88e64e52016-07-19 01:51:06 -0700388 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
389 RTPHeader parsed_header;
ilnika8e781a2017-06-12 01:02:46 -0700390 if (extension_map != nullptr) {
terelius88e64e52016-07-19 01:51:06 -0700391 rtp_parser.Parse(&parsed_header, extension_map);
ivocaac9d6f2016-09-22 07:01:47 -0700392 } else {
393 // Use the default extension map.
394 // TODO(ivoc): Once configuration of audio streams is stored in the
395 // event log, this can be removed.
396 // Tracking bug: webrtc:6399
397 rtp_parser.Parse(&parsed_header, &default_extension_map);
terelius88e64e52016-07-19 01:51:06 -0700398 }
399 uint64_t timestamp = parsed_log_.GetTimestamp(i);
ilnika8e781a2017-06-12 01:02:46 -0700400 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700401 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200402 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700403 break;
404 }
405 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200406 uint8_t packet[IP_PACKET_SIZE];
perkj77cd58e2017-05-30 03:52:10 -0700407 parsed_log_.GetRtcpPacket(i, &direction, packet, &total_length);
perkjbbbad6d2017-05-19 06:30:28 -0700408 // Currently incoming RTCP packets are logged twice, both for audio and
409 // video. Only act on one of them. Compare against the previous parsed
410 // incoming RTCP packet.
411 if (direction == webrtc::kIncomingPacket) {
412 RTC_CHECK_LE(total_length, IP_PACKET_SIZE);
413 if (total_length == last_incoming_rtcp_packet_length &&
414 memcmp(last_incoming_rtcp_packet, packet, total_length) == 0) {
415 continue;
416 } else {
417 memcpy(last_incoming_rtcp_packet, packet, total_length);
418 last_incoming_rtcp_packet_length = total_length;
419 }
420 }
421 rtcp::CommonHeader header;
422 const uint8_t* packet_end = packet + total_length;
423 for (const uint8_t* block = packet; block < packet_end;
424 block = header.NextPacket()) {
425 RTC_CHECK(header.Parse(block, packet_end - block));
426 if (header.type() == rtcp::TransportFeedback::kPacketType &&
427 header.fmt() == rtcp::TransportFeedback::kFeedbackMessageType) {
428 std::unique_ptr<rtcp::TransportFeedback> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700429 rtc::MakeUnique<rtcp::TransportFeedback>());
perkjbbbad6d2017-05-19 06:30:28 -0700430 if (rtcp_packet->Parse(header)) {
431 uint32_t ssrc = rtcp_packet->sender_ssrc();
432 StreamId stream(ssrc, direction);
433 uint64_t timestamp = parsed_log_.GetTimestamp(i);
434 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
435 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
436 }
437 } else if (header.type() == rtcp::SenderReport::kPacketType) {
438 std::unique_ptr<rtcp::SenderReport> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700439 rtc::MakeUnique<rtcp::SenderReport>());
perkjbbbad6d2017-05-19 06:30:28 -0700440 if (rtcp_packet->Parse(header)) {
441 uint32_t ssrc = rtcp_packet->sender_ssrc();
442 StreamId stream(ssrc, direction);
443 uint64_t timestamp = parsed_log_.GetTimestamp(i);
444 rtcp_packets_[stream].push_back(
445 LoggedRtcpPacket(timestamp, kRtcpSr, std::move(rtcp_packet)));
446 }
447 } else if (header.type() == rtcp::ReceiverReport::kPacketType) {
448 std::unique_ptr<rtcp::ReceiverReport> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700449 rtc::MakeUnique<rtcp::ReceiverReport>());
perkjbbbad6d2017-05-19 06:30:28 -0700450 if (rtcp_packet->Parse(header)) {
451 uint32_t ssrc = rtcp_packet->sender_ssrc();
452 StreamId stream(ssrc, direction);
453 uint64_t timestamp = parsed_log_.GetTimestamp(i);
454 rtcp_packets_[stream].push_back(
455 LoggedRtcpPacket(timestamp, kRtcpRr, std::move(rtcp_packet)));
Stefan Holmer13181032016-07-29 14:48:54 +0200456 }
terelius2c8e8a32017-06-02 01:29:48 -0700457 } else if (header.type() == rtcp::Remb::kPacketType &&
458 header.fmt() == rtcp::Remb::kFeedbackMessageType) {
459 std::unique_ptr<rtcp::Remb> rtcp_packet(
460 rtc::MakeUnique<rtcp::Remb>());
461 if (rtcp_packet->Parse(header)) {
462 uint32_t ssrc = rtcp_packet->sender_ssrc();
463 StreamId stream(ssrc, direction);
464 uint64_t timestamp = parsed_log_.GetTimestamp(i);
465 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
466 timestamp, kRtcpRemb, std::move(rtcp_packet)));
467 }
Stefan Holmer13181032016-07-29 14:48:54 +0200468 }
Stefan Holmer13181032016-07-29 14:48:54 +0200469 }
terelius88e64e52016-07-19 01:51:06 -0700470 break;
471 }
472 case ParsedRtcEventLog::LOG_START: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700473 if (last_log_start) {
474 // A LOG_END event was missing. Use last_timestamp.
475 RTC_DCHECK_GE(last_timestamp, *last_log_start);
476 log_segments_.push_back(
477 std::make_pair(*last_log_start, last_timestamp));
478 }
479 last_log_start = rtc::Optional<uint64_t>(parsed_log_.GetTimestamp(i));
terelius88e64e52016-07-19 01:51:06 -0700480 break;
481 }
482 case ParsedRtcEventLog::LOG_END: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700483 RTC_DCHECK(last_log_start);
484 log_segments_.push_back(
485 std::make_pair(*last_log_start, parsed_log_.GetTimestamp(i)));
486 last_log_start.reset();
terelius88e64e52016-07-19 01:51:06 -0700487 break;
488 }
terelius424e6cf2017-02-20 05:14:41 -0800489 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700490 uint32_t this_ssrc;
491 parsed_log_.GetAudioPlayout(i, &this_ssrc);
492 audio_playout_events_[this_ssrc].push_back(parsed_log_.GetTimestamp(i));
terelius424e6cf2017-02-20 05:14:41 -0800493 break;
494 }
495 case ParsedRtcEventLog::LOSS_BASED_BWE_UPDATE: {
496 LossBasedBweUpdate bwe_update;
terelius8058e582016-07-25 01:32:41 -0700497 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
terelius424e6cf2017-02-20 05:14:41 -0800498 parsed_log_.GetLossBasedBweUpdate(i, &bwe_update.new_bitrate,
499 &bwe_update.fraction_loss,
500 &bwe_update.expected_packets);
terelius8058e582016-07-25 01:32:41 -0700501 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700502 break;
503 }
terelius424e6cf2017-02-20 05:14:41 -0800504 case ParsedRtcEventLog::DELAY_BASED_BWE_UPDATE: {
philipel10fc0e62017-04-11 01:50:23 -0700505 bwe_delay_updates_.push_back(parsed_log_.GetDelayBasedBweUpdate(i));
terelius424e6cf2017-02-20 05:14:41 -0800506 break;
507 }
minyue4b7c9522017-01-24 04:54:59 -0800508 case ParsedRtcEventLog::AUDIO_NETWORK_ADAPTATION_EVENT: {
michaelt6e5b2192017-02-22 07:33:27 -0800509 AudioNetworkAdaptationEvent ana_event;
510 ana_event.timestamp = parsed_log_.GetTimestamp(i);
511 parsed_log_.GetAudioNetworkAdaptation(i, &ana_event.config);
512 audio_network_adaptation_events_.push_back(ana_event);
minyue4b7c9522017-01-24 04:54:59 -0800513 break;
514 }
philipel32d00102017-02-27 02:18:46 -0800515 case ParsedRtcEventLog::BWE_PROBE_CLUSTER_CREATED_EVENT: {
philipele127e7a2017-03-29 16:28:53 +0200516 bwe_probe_cluster_created_events_.push_back(
517 parsed_log_.GetBweProbeClusterCreated(i));
philipel32d00102017-02-27 02:18:46 -0800518 break;
519 }
520 case ParsedRtcEventLog::BWE_PROBE_RESULT_EVENT: {
philipele127e7a2017-03-29 16:28:53 +0200521 bwe_probe_result_events_.push_back(parsed_log_.GetBweProbeResult(i));
philipel32d00102017-02-27 02:18:46 -0800522 break;
523 }
terelius88e64e52016-07-19 01:51:06 -0700524 case ParsedRtcEventLog::UNKNOWN_EVENT: {
525 break;
526 }
527 }
terelius54ce6802016-07-13 06:44:41 -0700528 }
terelius88e64e52016-07-19 01:51:06 -0700529
terelius54ce6802016-07-13 06:44:41 -0700530 if (last_timestamp < first_timestamp) {
531 // No useful events in the log.
532 first_timestamp = last_timestamp = 0;
533 }
534 begin_time_ = first_timestamp;
535 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700536 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
henrik.lundin3c938fc2017-06-14 06:09:58 -0700537 if (last_log_start) {
538 // The log was missing the last LOG_END event. Fake it.
539 log_segments_.push_back(std::make_pair(*last_log_start, end_time_));
540 }
terelius54ce6802016-07-13 06:44:41 -0700541}
542
Niels Möller245f17e2017-08-21 10:45:07 +0200543class BitrateObserver : public SendSideCongestionController::Observer,
Stefan Holmer13181032016-07-29 14:48:54 +0200544 public RemoteBitrateObserver {
545 public:
546 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
547
548 void OnNetworkChanged(uint32_t bitrate_bps,
549 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800550 int64_t rtt_ms,
551 int64_t probing_interval_ms) override {
Stefan Holmer13181032016-07-29 14:48:54 +0200552 last_bitrate_bps_ = bitrate_bps;
553 bitrate_updated_ = true;
554 }
555
556 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
557 uint32_t bitrate) override {}
558
559 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
560 bool GetAndResetBitrateUpdated() {
561 bool bitrate_updated = bitrate_updated_;
562 bitrate_updated_ = false;
563 return bitrate_updated;
564 }
565
566 private:
567 uint32_t last_bitrate_bps_;
568 bool bitrate_updated_;
569};
570
Stefan Holmer99f8e082016-09-09 13:37:50 +0200571bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700572 return rtx_ssrcs_.count(stream_id) == 1;
573}
574
Stefan Holmer99f8e082016-09-09 13:37:50 +0200575bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700576 return video_ssrcs_.count(stream_id) == 1;
577}
578
Stefan Holmer99f8e082016-09-09 13:37:50 +0200579bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700580 return audio_ssrcs_.count(stream_id) == 1;
581}
582
Stefan Holmer99f8e082016-09-09 13:37:50 +0200583std::string EventLogAnalyzer::GetStreamName(StreamId stream_id) const {
584 std::stringstream name;
585 if (IsAudioSsrc(stream_id)) {
586 name << "Audio ";
587 } else if (IsVideoSsrc(stream_id)) {
588 name << "Video ";
589 } else {
590 name << "Unknown ";
591 }
592 if (IsRtxSsrc(stream_id))
593 name << "RTX ";
ivocaac9d6f2016-09-22 07:01:47 -0700594 if (stream_id.GetDirection() == kIncomingPacket) {
595 name << "(In) ";
596 } else {
597 name << "(Out) ";
598 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200599 name << SsrcToString(stream_id.GetSsrc());
600 return name.str();
601}
602
Bjorn Terelius0295a962017-10-25 17:42:41 +0200603// This is much more reliable for outgoing streams than for incoming streams.
604rtc::Optional<uint32_t> EventLogAnalyzer::EstimateRtpClockFrequency(
605 const std::vector<LoggedRtpPacket>& packets) const {
606 RTC_CHECK(packets.size() >= 2);
607 uint64_t end_time_us = log_segments_.empty()
608 ? std::numeric_limits<uint64_t>::max()
609 : log_segments_.front().second;
610 SeqNumUnwrapper<uint32_t> unwrapper;
611 uint64_t first_rtp_timestamp = unwrapper.Unwrap(packets[0].header.timestamp);
612 uint64_t first_log_timestamp = packets[0].timestamp;
613 uint64_t last_rtp_timestamp = first_rtp_timestamp;
614 uint64_t last_log_timestamp = first_log_timestamp;
615 for (size_t i = 1; i < packets.size(); i++) {
616 if (packets[i].timestamp > end_time_us)
617 break;
618 last_rtp_timestamp = unwrapper.Unwrap(packets[i].header.timestamp);
619 last_log_timestamp = packets[i].timestamp;
620 }
621 if (last_log_timestamp - first_log_timestamp < 1000000) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100622 RTC_LOG(LS_WARNING)
Bjorn Terelius0295a962017-10-25 17:42:41 +0200623 << "Failed to estimate RTP clock frequency: Stream too short. ("
624 << packets.size() << " packets, "
625 << last_log_timestamp - first_log_timestamp << " us)";
626 return rtc::Optional<uint32_t>();
627 }
628 double duration =
629 static_cast<double>(last_log_timestamp - first_log_timestamp) / 1000000;
630 double estimated_frequency =
631 (last_rtp_timestamp - first_rtp_timestamp) / duration;
632 for (uint32_t f : {8000, 16000, 32000, 48000, 90000}) {
633 if (std::fabs(estimated_frequency - f) < 0.05 * f) {
634 return rtc::Optional<uint32_t>(f);
635 }
636 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100637 RTC_LOG(LS_WARNING) << "Failed to estimate RTP clock frequency: Estimate "
638 << estimated_frequency
639 << "not close to any stardard RTP frequency.";
Bjorn Terelius0295a962017-10-25 17:42:41 +0200640 return rtc::Optional<uint32_t>();
641}
642
terelius54ce6802016-07-13 06:44:41 -0700643void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
644 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700645 for (auto& kv : rtp_packets_) {
646 StreamId stream_id = kv.first;
647 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
648 // Filter on direction and SSRC.
649 if (stream_id.GetDirection() != desired_direction ||
650 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
651 continue;
terelius54ce6802016-07-13 06:44:41 -0700652 }
terelius54ce6802016-07-13 06:44:41 -0700653
terelius23c595a2017-03-15 01:59:12 -0700654 TimeSeries time_series(GetStreamName(stream_id), BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700655 ProcessPoints<LoggedRtpPacket>(
656 [](const LoggedRtpPacket& packet) -> rtc::Optional<float> {
657 return rtc::Optional<float>(packet.total_length);
658 },
659 packet_stream, begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700660 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700661 }
662
tereliusdc35dcd2016-08-01 12:03:27 -0700663 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
664 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
665 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700666 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700667 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700668 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700669 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700670 }
671}
672
philipelccd74892016-09-05 02:46:25 -0700673template <typename T>
674void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
675 PacketDirection desired_direction,
676 Plot* plot,
677 const std::map<StreamId, std::vector<T>>& packets,
678 const std::string& label_prefix) {
679 for (auto& kv : packets) {
680 StreamId stream_id = kv.first;
681 const std::vector<T>& packet_stream = kv.second;
682 // Filter on direction and SSRC.
683 if (stream_id.GetDirection() != desired_direction ||
684 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
685 continue;
686 }
687
terelius23c595a2017-03-15 01:59:12 -0700688 std::string label = label_prefix + " " + GetStreamName(stream_id);
689 TimeSeries time_series(label, LINE_STEP_GRAPH);
philipelccd74892016-09-05 02:46:25 -0700690 for (size_t i = 0; i < packet_stream.size(); i++) {
691 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
692 1000000;
philipelccd74892016-09-05 02:46:25 -0700693 time_series.points.emplace_back(x, i + 1);
694 }
695
philipel35ba9bd2017-04-19 05:58:51 -0700696 plot->AppendTimeSeries(std::move(time_series));
philipelccd74892016-09-05 02:46:25 -0700697 }
698}
699
700void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
701 PacketDirection desired_direction,
702 Plot* plot) {
703 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
704 "RTP");
705 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
706 "RTCP");
707
708 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
709 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
710 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
711 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
712 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
713 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
714 }
715}
716
terelius54ce6802016-07-13 06:44:41 -0700717// For each SSRC, plot the time between the consecutive playouts.
718void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
719 std::map<uint32_t, TimeSeries> time_series;
720 std::map<uint32_t, uint64_t> last_playout;
721
722 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700723
724 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
725 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
726 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
727 parsed_log_.GetAudioPlayout(i, &ssrc);
728 uint64_t timestamp = parsed_log_.GetTimestamp(i);
729 if (MatchingSsrc(ssrc, desired_ssrc_)) {
730 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
731 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
732 if (time_series[ssrc].points.size() == 0) {
733 // There were no previusly logged playout for this SSRC.
734 // Generate a point, but place it on the x-axis.
735 y = 0;
736 }
terelius54ce6802016-07-13 06:44:41 -0700737 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
738 last_playout[ssrc] = timestamp;
739 }
740 }
741 }
742
743 // Set labels and put in graph.
744 for (auto& kv : time_series) {
745 kv.second.label = SsrcToString(kv.first);
746 kv.second.style = BAR_GRAPH;
philipel35ba9bd2017-04-19 05:58:51 -0700747 plot->AppendTimeSeries(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700748 }
749
tereliusdc35dcd2016-08-01 12:03:27 -0700750 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
751 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
752 kTopMargin);
753 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700754}
755
ivocaac9d6f2016-09-22 07:01:47 -0700756// For audio SSRCs, plot the audio level.
757void EventLogAnalyzer::CreateAudioLevelGraph(Plot* plot) {
758 std::map<StreamId, TimeSeries> time_series;
759
760 for (auto& kv : rtp_packets_) {
761 StreamId stream_id = kv.first;
762 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
763 // TODO(ivoc): When audio send/receive configs are stored in the event
764 // log, a check should be added here to only process audio
765 // streams. Tracking bug: webrtc:6399
766 for (auto& packet : packet_stream) {
767 if (packet.header.extension.hasAudioLevel) {
768 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
769 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
770 // Here we convert it to dBov.
771 float y = static_cast<float>(-packet.header.extension.audioLevel);
772 time_series[stream_id].points.emplace_back(TimeSeriesPoint(x, y));
773 }
774 }
775 }
776
777 for (auto& series : time_series) {
778 series.second.label = GetStreamName(series.first);
779 series.second.style = LINE_GRAPH;
philipel35ba9bd2017-04-19 05:58:51 -0700780 plot->AppendTimeSeries(std::move(series.second));
ivocaac9d6f2016-09-22 07:01:47 -0700781 }
782
783 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
ivocbf676632016-11-24 08:30:34 -0800784 plot->SetYAxis(-127, 0, "Audio level (dBov)", kBottomMargin,
ivocaac9d6f2016-09-22 07:01:47 -0700785 kTopMargin);
786 plot->SetTitle("Audio level");
787}
788
terelius54ce6802016-07-13 06:44:41 -0700789// For each SSRC, plot the time between the consecutive playouts.
790void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700791 for (auto& kv : rtp_packets_) {
792 StreamId stream_id = kv.first;
793 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
794 // Filter on direction and SSRC.
795 if (stream_id.GetDirection() != kIncomingPacket ||
796 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
797 continue;
terelius54ce6802016-07-13 06:44:41 -0700798 }
terelius54ce6802016-07-13 06:44:41 -0700799
terelius23c595a2017-03-15 01:59:12 -0700800 TimeSeries time_series(GetStreamName(stream_id), BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700801 ProcessPairs<LoggedRtpPacket, float>(
802 [](const LoggedRtpPacket& old_packet,
803 const LoggedRtpPacket& new_packet) {
804 int64_t diff =
805 WrappingDifference(new_packet.header.sequenceNumber,
806 old_packet.header.sequenceNumber, 1ul << 16);
807 return rtc::Optional<float>(diff);
808 },
809 packet_stream, begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700810 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700811 }
812
tereliusdc35dcd2016-08-01 12:03:27 -0700813 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
814 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
815 kTopMargin);
816 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700817}
818
Stefan Holmer99f8e082016-09-09 13:37:50 +0200819void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
820 for (auto& kv : rtp_packets_) {
821 StreamId stream_id = kv.first;
822 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
823 // Filter on direction and SSRC.
824 if (stream_id.GetDirection() != kIncomingPacket ||
terelius4c9b4af2017-01-30 08:44:51 -0800825 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
826 packet_stream.size() == 0) {
Stefan Holmer99f8e082016-09-09 13:37:50 +0200827 continue;
828 }
829
terelius23c595a2017-03-15 01:59:12 -0700830 TimeSeries time_series(GetStreamName(stream_id), LINE_DOT_GRAPH);
Stefan Holmer99f8e082016-09-09 13:37:50 +0200831 const uint64_t kWindowUs = 1000000;
terelius4c9b4af2017-01-30 08:44:51 -0800832 const uint64_t kStep = 1000000;
833 SequenceNumberUnwrapper unwrapper_;
834 SequenceNumberUnwrapper prior_unwrapper_;
835 size_t window_index_begin = 0;
836 size_t window_index_end = 0;
837 int64_t highest_seq_number =
838 unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
839 int64_t highest_prior_seq_number =
840 prior_unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
841
842 for (uint64_t t = begin_time_; t < end_time_ + kStep; t += kStep) {
843 while (window_index_end < packet_stream.size() &&
844 packet_stream[window_index_end].timestamp < t) {
845 int64_t sequence_number = unwrapper_.Unwrap(
846 packet_stream[window_index_end].header.sequenceNumber);
847 highest_seq_number = std::max(highest_seq_number, sequence_number);
848 ++window_index_end;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200849 }
terelius4c9b4af2017-01-30 08:44:51 -0800850 while (window_index_begin < packet_stream.size() &&
851 packet_stream[window_index_begin].timestamp < t - kWindowUs) {
852 int64_t sequence_number = prior_unwrapper_.Unwrap(
853 packet_stream[window_index_begin].header.sequenceNumber);
854 highest_prior_seq_number =
855 std::max(highest_prior_seq_number, sequence_number);
856 ++window_index_begin;
857 }
858 float x = static_cast<float>(t - begin_time_) / 1000000;
859 int64_t expected_packets = highest_seq_number - highest_prior_seq_number;
860 if (expected_packets > 0) {
861 int64_t received_packets = window_index_end - window_index_begin;
862 int64_t lost_packets = expected_packets - received_packets;
863 float y = static_cast<float>(lost_packets) / expected_packets * 100;
864 time_series.points.emplace_back(x, y);
865 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200866 }
philipel35ba9bd2017-04-19 05:58:51 -0700867 plot->AppendTimeSeries(std::move(time_series));
Stefan Holmer99f8e082016-09-09 13:37:50 +0200868 }
869
870 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
871 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
872 kTopMargin);
873 plot->SetTitle("Estimated incoming loss rate");
874}
875
terelius2ee076d2017-08-15 02:04:02 -0700876void EventLogAnalyzer::CreateIncomingDelayDeltaGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700877 for (auto& kv : rtp_packets_) {
878 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700879 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700880 // Filter on direction and SSRC.
881 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200882 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
883 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
884 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700885 continue;
886 }
terelius54ce6802016-07-13 06:44:41 -0700887
terelius23c595a2017-03-15 01:59:12 -0700888 TimeSeries capture_time_data(GetStreamName(stream_id) + " capture-time",
889 BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700890 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
891 packet_stream, begin_time_,
892 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700893 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700894
terelius23c595a2017-03-15 01:59:12 -0700895 TimeSeries send_time_data(GetStreamName(stream_id) + " abs-send-time",
896 BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700897 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
898 packet_stream, begin_time_,
899 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700900 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700901 }
902
tereliusdc35dcd2016-08-01 12:03:27 -0700903 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
904 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
905 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700906 plot->SetTitle("Network latency difference between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700907}
908
terelius2ee076d2017-08-15 02:04:02 -0700909void EventLogAnalyzer::CreateIncomingDelayGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700910 for (auto& kv : rtp_packets_) {
911 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700912 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700913 // Filter on direction and SSRC.
914 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200915 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
916 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
917 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700918 continue;
919 }
terelius54ce6802016-07-13 06:44:41 -0700920
terelius23c595a2017-03-15 01:59:12 -0700921 TimeSeries capture_time_data(GetStreamName(stream_id) + " capture-time",
922 LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700923 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
924 packet_stream, begin_time_,
925 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700926 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700927
terelius23c595a2017-03-15 01:59:12 -0700928 TimeSeries send_time_data(GetStreamName(stream_id) + " abs-send-time",
929 LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700930 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
931 packet_stream, begin_time_,
932 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700933 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700934 }
935
tereliusdc35dcd2016-08-01 12:03:27 -0700936 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
937 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
938 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700939 plot->SetTitle("Network latency (relative to first packet)");
terelius54ce6802016-07-13 06:44:41 -0700940}
941
tereliusf736d232016-08-04 10:00:11 -0700942// Plot the fraction of packets lost (as perceived by the loss-based BWE).
943void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -0700944 TimeSeries time_series("Fraction lost", LINE_DOT_GRAPH);
tereliusf736d232016-08-04 10:00:11 -0700945 for (auto& bwe_update : bwe_loss_updates_) {
946 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
947 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
philipel35ba9bd2017-04-19 05:58:51 -0700948 time_series.points.emplace_back(x, y);
tereliusf736d232016-08-04 10:00:11 -0700949 }
tereliusf736d232016-08-04 10:00:11 -0700950
Bjorn Terelius19f5be32017-10-18 12:39:49 +0200951 plot->AppendTimeSeries(std::move(time_series));
tereliusf736d232016-08-04 10:00:11 -0700952 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
953 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
954 kTopMargin);
955 plot->SetTitle("Reported packet loss");
956}
957
terelius54ce6802016-07-13 06:44:41 -0700958// Plot the total bandwidth used by all RTP streams.
959void EventLogAnalyzer::CreateTotalBitrateGraph(
960 PacketDirection desired_direction,
philipel23c7f252017-07-14 06:30:03 -0700961 Plot* plot,
962 bool show_detector_state) {
terelius54ce6802016-07-13 06:44:41 -0700963 struct TimestampSize {
964 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
965 uint64_t timestamp;
966 size_t size;
967 };
968 std::vector<TimestampSize> packets;
969
970 PacketDirection direction;
971 size_t total_length;
972
973 // Extract timestamps and sizes for the relevant packets.
974 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
975 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
976 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
Elad Alon1d87b0e2017-10-03 15:01:03 +0200977 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, &total_length,
978 nullptr);
terelius54ce6802016-07-13 06:44:41 -0700979 if (direction == desired_direction) {
980 uint64_t timestamp = parsed_log_.GetTimestamp(i);
981 packets.push_back(TimestampSize(timestamp, total_length));
982 }
983 }
984 }
985
986 size_t window_index_begin = 0;
987 size_t window_index_end = 0;
988 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700989
990 // Calculate a moving average of the bitrate and store in a TimeSeries.
philipel35ba9bd2017-04-19 05:58:51 -0700991 TimeSeries bitrate_series("Bitrate", LINE_GRAPH);
terelius54ce6802016-07-13 06:44:41 -0700992 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
993 while (window_index_end < packets.size() &&
994 packets[window_index_end].timestamp < time) {
995 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700996 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700997 }
998 while (window_index_begin < packets.size() &&
999 packets[window_index_begin].timestamp < time - window_duration_) {
1000 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
1001 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -07001002 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -07001003 }
1004 float window_duration_in_seconds =
1005 static_cast<float>(window_duration_) / 1000000;
1006 float x = static_cast<float>(time - begin_time_) / 1000000;
1007 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001008 bitrate_series.points.emplace_back(x, y);
terelius54ce6802016-07-13 06:44:41 -07001009 }
philipel35ba9bd2017-04-19 05:58:51 -07001010 plot->AppendTimeSeries(std::move(bitrate_series));
terelius54ce6802016-07-13 06:44:41 -07001011
terelius8058e582016-07-25 01:32:41 -07001012 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
1013 if (desired_direction == kOutgoingPacket) {
philipel35ba9bd2017-04-19 05:58:51 -07001014 TimeSeries loss_series("Loss-based estimate", LINE_STEP_GRAPH);
philipel10fc0e62017-04-11 01:50:23 -07001015 for (auto& loss_update : bwe_loss_updates_) {
terelius8058e582016-07-25 01:32:41 -07001016 float x =
philipel10fc0e62017-04-11 01:50:23 -07001017 static_cast<float>(loss_update.timestamp - begin_time_) / 1000000;
1018 float y = static_cast<float>(loss_update.new_bitrate) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001019 loss_series.points.emplace_back(x, y);
philipel10fc0e62017-04-11 01:50:23 -07001020 }
1021
philipel35ba9bd2017-04-19 05:58:51 -07001022 TimeSeries delay_series("Delay-based estimate", LINE_STEP_GRAPH);
philipel23c7f252017-07-14 06:30:03 -07001023 IntervalSeries overusing_series("Overusing", "#ff8e82",
1024 IntervalSeries::kHorizontal);
1025 IntervalSeries underusing_series("Underusing", "#5092fc",
1026 IntervalSeries::kHorizontal);
1027 IntervalSeries normal_series("Normal", "#c4ffc4",
1028 IntervalSeries::kHorizontal);
1029 IntervalSeries* last_series = &normal_series;
1030 double last_detector_switch = 0.0;
1031
1032 BandwidthUsage last_detector_state = BandwidthUsage::kBwNormal;
1033
philipel10fc0e62017-04-11 01:50:23 -07001034 for (auto& delay_update : bwe_delay_updates_) {
1035 float x =
1036 static_cast<float>(delay_update.timestamp - begin_time_) / 1000000;
1037 float y = static_cast<float>(delay_update.bitrate_bps) / 1000;
philipel23c7f252017-07-14 06:30:03 -07001038
1039 if (last_detector_state != delay_update.detector_state) {
1040 last_series->intervals.emplace_back(last_detector_switch, x);
1041 last_detector_state = delay_update.detector_state;
1042 last_detector_switch = x;
1043
1044 switch (delay_update.detector_state) {
1045 case BandwidthUsage::kBwNormal:
1046 last_series = &normal_series;
1047 break;
1048 case BandwidthUsage::kBwUnderusing:
1049 last_series = &underusing_series;
1050 break;
1051 case BandwidthUsage::kBwOverusing:
1052 last_series = &overusing_series;
1053 break;
Elad Alon1d87b0e2017-10-03 15:01:03 +02001054 case BandwidthUsage::kLast:
1055 RTC_NOTREACHED();
philipel23c7f252017-07-14 06:30:03 -07001056 }
1057 }
1058
philipel35ba9bd2017-04-19 05:58:51 -07001059 delay_series.points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -07001060 }
philipele127e7a2017-03-29 16:28:53 +02001061
philipel23c7f252017-07-14 06:30:03 -07001062 RTC_CHECK(last_series);
1063 last_series->intervals.emplace_back(last_detector_switch, end_time_);
1064
philipel35ba9bd2017-04-19 05:58:51 -07001065 TimeSeries created_series("Probe cluster created.", DOT_GRAPH);
philipele127e7a2017-03-29 16:28:53 +02001066 for (auto& cluster : bwe_probe_cluster_created_events_) {
1067 float x = static_cast<float>(cluster.timestamp - begin_time_) / 1000000;
1068 float y = static_cast<float>(cluster.bitrate_bps) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001069 created_series.points.emplace_back(x, y);
philipele127e7a2017-03-29 16:28:53 +02001070 }
1071
philipel35ba9bd2017-04-19 05:58:51 -07001072 TimeSeries result_series("Probing results.", DOT_GRAPH);
philipele127e7a2017-03-29 16:28:53 +02001073 for (auto& result : bwe_probe_result_events_) {
1074 if (result.bitrate_bps) {
1075 float x = static_cast<float>(result.timestamp - begin_time_) / 1000000;
1076 float y = static_cast<float>(*result.bitrate_bps) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001077 result_series.points.emplace_back(x, y);
philipele127e7a2017-03-29 16:28:53 +02001078 }
1079 }
philipel23c7f252017-07-14 06:30:03 -07001080
1081 if (show_detector_state) {
1082 plot->AppendIntervalSeries(std::move(overusing_series));
1083 plot->AppendIntervalSeries(std::move(underusing_series));
1084 plot->AppendIntervalSeries(std::move(normal_series));
1085 }
1086
philipel35ba9bd2017-04-19 05:58:51 -07001087 plot->AppendTimeSeries(std::move(loss_series));
1088 plot->AppendTimeSeries(std::move(delay_series));
1089 plot->AppendTimeSeries(std::move(created_series));
1090 plot->AppendTimeSeries(std::move(result_series));
terelius8058e582016-07-25 01:32:41 -07001091 }
philipele127e7a2017-03-29 16:28:53 +02001092
terelius2c8e8a32017-06-02 01:29:48 -07001093 // Overlay the incoming REMB over the outgoing bitrate
1094 // and outgoing REMB over incoming bitrate.
1095 PacketDirection remb_direction =
1096 desired_direction == kOutgoingPacket ? kIncomingPacket : kOutgoingPacket;
1097 TimeSeries remb_series("Remb", LINE_STEP_GRAPH);
1098 std::multimap<uint64_t, const LoggedRtcpPacket*> remb_packets;
1099 for (const auto& kv : rtcp_packets_) {
1100 if (kv.first.GetDirection() == remb_direction) {
1101 for (const LoggedRtcpPacket& rtcp_packet : kv.second) {
1102 if (rtcp_packet.type == kRtcpRemb) {
1103 remb_packets.insert(
1104 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1105 }
1106 }
1107 }
1108 }
1109
1110 for (const auto& kv : remb_packets) {
1111 const LoggedRtcpPacket* const rtcp = kv.second;
1112 const rtcp::Remb* const remb = static_cast<rtcp::Remb*>(rtcp->packet.get());
1113 float x = static_cast<float>(rtcp->timestamp - begin_time_) / 1000000;
1114 float y = static_cast<float>(remb->bitrate_bps()) / 1000;
1115 remb_series.points.emplace_back(x, y);
1116 }
1117 plot->AppendTimeSeriesIfNotEmpty(std::move(remb_series));
1118
tereliusdc35dcd2016-08-01 12:03:27 -07001119 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1120 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -07001121 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001122 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001123 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001124 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001125 }
1126}
1127
1128// For each SSRC, plot the bandwidth used by that stream.
1129void EventLogAnalyzer::CreateStreamBitrateGraph(
1130 PacketDirection desired_direction,
1131 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -07001132 for (auto& kv : rtp_packets_) {
1133 StreamId stream_id = kv.first;
1134 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
1135 // Filter on direction and SSRC.
1136 if (stream_id.GetDirection() != desired_direction ||
1137 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
1138 continue;
terelius54ce6802016-07-13 06:44:41 -07001139 }
1140
terelius23c595a2017-03-15 01:59:12 -07001141 TimeSeries time_series(GetStreamName(stream_id), LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001142 MovingAverage<LoggedRtpPacket, double>(
1143 [](const LoggedRtpPacket& packet) {
1144 return rtc::Optional<double>(packet.total_length * 8.0 / 1000.0);
1145 },
1146 packet_stream, begin_time_, end_time_, window_duration_, step_,
1147 &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001148 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -07001149 }
1150
tereliusdc35dcd2016-08-01 12:03:27 -07001151 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1152 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -07001153 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001154 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001155 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001156 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001157 }
1158}
1159
Bjorn Terelius28db2662017-10-04 14:22:43 +02001160void EventLogAnalyzer::CreateSendSideBweSimulationGraph(Plot* plot) {
stefanff421622017-04-20 03:24:01 -07001161 std::multimap<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1162 std::multimap<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
Stefan Holmer13181032016-07-29 14:48:54 +02001163
1164 for (const auto& kv : rtp_packets_) {
1165 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1166 for (const LoggedRtpPacket& rtp_packet : kv.second)
1167 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1168 }
1169 }
1170
1171 for (const auto& kv : rtcp_packets_) {
1172 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1173 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1174 incoming_rtcp.insert(
1175 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1176 }
1177 }
1178
1179 SimulatedClock clock(0);
1180 BitrateObserver observer;
1181 RtcEventLogNullImpl null_event_log;
nisse0245da02016-11-30 03:35:20 -08001182 PacketRouter packet_router;
Stefan Holmer5c8942a2017-08-22 16:16:44 +02001183 PacedSender pacer(&clock, &packet_router, &null_event_log);
1184 SendSideCongestionController cc(&clock, &observer, &null_event_log, &pacer);
Stefan Holmer13181032016-07-29 14:48:54 +02001185 // TODO(holmer): Log the call config and use that here instead.
1186 static const uint32_t kDefaultStartBitrateBps = 300000;
1187 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1188
terelius23c595a2017-03-15 01:59:12 -07001189 TimeSeries time_series("Delay-based estimate", LINE_DOT_GRAPH);
1190 TimeSeries acked_time_series("Acked bitrate", LINE_DOT_GRAPH);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001191 TimeSeries acked_estimate_time_series("Acked bitrate estimate",
1192 LINE_DOT_GRAPH);
Stefan Holmer13181032016-07-29 14:48:54 +02001193
1194 auto rtp_iterator = outgoing_rtp.begin();
1195 auto rtcp_iterator = incoming_rtcp.begin();
1196
1197 auto NextRtpTime = [&]() {
1198 if (rtp_iterator != outgoing_rtp.end())
1199 return static_cast<int64_t>(rtp_iterator->first);
1200 return std::numeric_limits<int64_t>::max();
1201 };
1202
1203 auto NextRtcpTime = [&]() {
1204 if (rtcp_iterator != incoming_rtcp.end())
1205 return static_cast<int64_t>(rtcp_iterator->first);
1206 return std::numeric_limits<int64_t>::max();
1207 };
1208
1209 auto NextProcessTime = [&]() {
1210 if (rtcp_iterator != incoming_rtcp.end() ||
1211 rtp_iterator != outgoing_rtp.end()) {
1212 return clock.TimeInMicroseconds() +
1213 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
1214 }
1215 return std::numeric_limits<int64_t>::max();
1216 };
1217
Stefan Holmer492ee282016-10-27 17:19:20 +02001218 RateStatistics acked_bitrate(250, 8000);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001219#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1220 // The event_log_visualizer should normally not be compiled with
1221 // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE since the normal plots won't work.
1222 // However, compiling with BWE_TEST_LOGGING, runnning with --plot_sendside_bwe
1223 // and piping the output to plot_dynamics.py can be used as a hack to get the
1224 // internal state of various BWE components. In this case, it is important
1225 // we don't instantiate the AcknowledgedBitrateEstimator both here and in
1226 // SendSideCongestionController since that would lead to duplicate outputs.
1227 AcknowledgedBitrateEstimator acknowledged_bitrate_estimator(
1228 rtc::MakeUnique<BitrateEstimator>());
1229#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Stefan Holmer13181032016-07-29 14:48:54 +02001230 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
Stefan Holmer492ee282016-10-27 17:19:20 +02001231 int64_t last_update_us = 0;
Stefan Holmer13181032016-07-29 14:48:54 +02001232 while (time_us != std::numeric_limits<int64_t>::max()) {
1233 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1234 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001235 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001236 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1237 if (rtcp.type == kRtcpTransportFeedback) {
elad.alon5bbf43f2017-03-09 06:40:08 -08001238 cc.OnTransportFeedback(
1239 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
1240 std::vector<PacketFeedback> feedback = cc.GetTransportFeedbackVector();
elad.alonec304f92017-03-08 05:03:53 -08001241 SortPacketFeedbackVector(&feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +02001242 rtc::Optional<uint32_t> bitrate_bps;
1243 if (!feedback.empty()) {
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001244#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1245 acknowledged_bitrate_estimator.IncomingPacketFeedbackVector(feedback);
1246#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
elad.alonf9490002017-03-06 05:32:21 -08001247 for (const PacketFeedback& packet : feedback)
Stefan Holmer60e43462016-09-07 09:58:20 +02001248 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1249 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
1250 }
Stefan Holmer60e43462016-09-07 09:58:20 +02001251 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1252 1000000;
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001253 float y = bitrate_bps.value_or(0) / 1000;
Stefan Holmer60e43462016-09-07 09:58:20 +02001254 acked_time_series.points.emplace_back(x, y);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001255#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1256 y = acknowledged_bitrate_estimator.bitrate_bps().value_or(0) / 1000;
1257 acked_estimate_time_series.points.emplace_back(x, y);
1258#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Stefan Holmer13181032016-07-29 14:48:54 +02001259 }
1260 ++rtcp_iterator;
1261 }
1262 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001263 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001264 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1265 if (rtp.header.extension.hasTransportSequenceNumber) {
1266 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
elad.alond12a8e12017-03-23 11:04:48 -07001267 cc.AddPacket(rtp.header.ssrc,
1268 rtp.header.extension.transportSequenceNumber,
elad.alon5bbf43f2017-03-09 06:40:08 -08001269 rtp.total_length, PacedPacketInfo());
Stefan Holmer13181032016-07-29 14:48:54 +02001270 rtc::SentPacket sent_packet(
1271 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1272 cc.OnSentPacket(sent_packet);
1273 }
1274 ++rtp_iterator;
1275 }
stefanc3de0332016-08-02 07:22:17 -07001276 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1277 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001278 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001279 }
Stefan Holmer492ee282016-10-27 17:19:20 +02001280 if (observer.GetAndResetBitrateUpdated() ||
1281 time_us - last_update_us >= 1e6) {
Stefan Holmer13181032016-07-29 14:48:54 +02001282 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +02001283 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1284 1000000;
1285 time_series.points.emplace_back(x, y);
Stefan Holmer492ee282016-10-27 17:19:20 +02001286 last_update_us = time_us;
Stefan Holmer13181032016-07-29 14:48:54 +02001287 }
1288 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1289 }
1290 // Add the data set to the plot.
philipel35ba9bd2017-04-19 05:58:51 -07001291 plot->AppendTimeSeries(std::move(time_series));
1292 plot->AppendTimeSeries(std::move(acked_time_series));
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001293 plot->AppendTimeSeriesIfNotEmpty(std::move(acked_estimate_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001294
tereliusdc35dcd2016-08-01 12:03:27 -07001295 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1296 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001297 plot->SetTitle("Simulated send-side BWE behavior");
1298}
1299
1300void EventLogAnalyzer::CreateReceiveSideBweSimulationGraph(Plot* plot) {
1301 class RembInterceptingPacketRouter : public PacketRouter {
1302 public:
1303 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
1304 uint32_t bitrate_bps) override {
1305 last_bitrate_bps_ = bitrate_bps;
1306 bitrate_updated_ = true;
1307 PacketRouter::OnReceiveBitrateChanged(ssrcs, bitrate_bps);
1308 }
1309 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
1310 bool GetAndResetBitrateUpdated() {
1311 bool bitrate_updated = bitrate_updated_;
1312 bitrate_updated_ = false;
1313 return bitrate_updated;
1314 }
1315
1316 private:
1317 uint32_t last_bitrate_bps_;
1318 bool bitrate_updated_;
1319 };
1320
1321 std::multimap<uint64_t, const LoggedRtpPacket*> incoming_rtp;
1322
1323 for (const auto& kv : rtp_packets_) {
1324 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket &&
1325 IsVideoSsrc(kv.first)) {
1326 for (const LoggedRtpPacket& rtp_packet : kv.second)
1327 incoming_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1328 }
1329 }
1330
1331 SimulatedClock clock(0);
1332 RembInterceptingPacketRouter packet_router;
1333 // TODO(terelius): The PacketRrouter is the used as the RemoteBitrateObserver.
1334 // Is this intentional?
1335 ReceiveSideCongestionController rscc(&clock, &packet_router);
1336 // TODO(holmer): Log the call config and use that here instead.
1337 // static const uint32_t kDefaultStartBitrateBps = 300000;
1338 // rscc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1339
1340 TimeSeries time_series("Receive side estimate", LINE_DOT_GRAPH);
1341 TimeSeries acked_time_series("Received bitrate", LINE_GRAPH);
1342
1343 RateStatistics acked_bitrate(250, 8000);
1344 int64_t last_update_us = 0;
1345 for (const auto& kv : incoming_rtp) {
1346 const LoggedRtpPacket& packet = *kv.second;
1347 int64_t arrival_time_ms = packet.timestamp / 1000;
1348 size_t payload = packet.total_length; /*Should subtract header?*/
1349 clock.AdvanceTimeMicroseconds(packet.timestamp -
1350 clock.TimeInMicroseconds());
1351 rscc.OnReceivedPacket(arrival_time_ms, payload, packet.header);
1352 acked_bitrate.Update(payload, arrival_time_ms);
1353 rtc::Optional<uint32_t> bitrate_bps = acked_bitrate.Rate(arrival_time_ms);
1354 if (bitrate_bps) {
1355 uint32_t y = *bitrate_bps / 1000;
1356 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1357 1000000;
1358 acked_time_series.points.emplace_back(x, y);
1359 }
1360 if (packet_router.GetAndResetBitrateUpdated() ||
1361 clock.TimeInMicroseconds() - last_update_us >= 1e6) {
1362 uint32_t y = packet_router.last_bitrate_bps() / 1000;
1363 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1364 1000000;
1365 time_series.points.emplace_back(x, y);
1366 last_update_us = clock.TimeInMicroseconds();
1367 }
1368 }
1369 // Add the data set to the plot.
1370 plot->AppendTimeSeries(std::move(time_series));
1371 plot->AppendTimeSeries(std::move(acked_time_series));
1372
1373 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1374 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1375 plot->SetTitle("Simulated receive-side BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001376}
1377
tereliuse34c19c2016-08-15 08:47:14 -07001378void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanff421622017-04-20 03:24:01 -07001379 std::multimap<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1380 std::multimap<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
stefanc3de0332016-08-02 07:22:17 -07001381
1382 for (const auto& kv : rtp_packets_) {
1383 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1384 for (const LoggedRtpPacket& rtp_packet : kv.second)
1385 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1386 }
1387 }
1388
1389 for (const auto& kv : rtcp_packets_) {
1390 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1391 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1392 incoming_rtcp.insert(
1393 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1394 }
1395 }
1396
1397 SimulatedClock clock(0);
elad.alon5bbf43f2017-03-09 06:40:08 -08001398 TransportFeedbackAdapter feedback_adapter(&clock);
stefanc3de0332016-08-02 07:22:17 -07001399
stefana0a8ed72017-09-06 02:06:32 -07001400 TimeSeries late_feedback_series("Late feedback results.", DOT_GRAPH);
terelius23c595a2017-03-15 01:59:12 -07001401 TimeSeries time_series("Network Delay Change", LINE_DOT_GRAPH);
stefanc3de0332016-08-02 07:22:17 -07001402 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1403
1404 auto rtp_iterator = outgoing_rtp.begin();
1405 auto rtcp_iterator = incoming_rtcp.begin();
1406
1407 auto NextRtpTime = [&]() {
1408 if (rtp_iterator != outgoing_rtp.end())
1409 return static_cast<int64_t>(rtp_iterator->first);
1410 return std::numeric_limits<int64_t>::max();
1411 };
1412
1413 auto NextRtcpTime = [&]() {
1414 if (rtcp_iterator != incoming_rtcp.end())
1415 return static_cast<int64_t>(rtcp_iterator->first);
1416 return std::numeric_limits<int64_t>::max();
1417 };
1418
1419 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
stefana0a8ed72017-09-06 02:06:32 -07001420 int64_t prev_y = 0;
stefanc3de0332016-08-02 07:22:17 -07001421 while (time_us != std::numeric_limits<int64_t>::max()) {
1422 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1423 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1424 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1425 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1426 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001427 feedback_adapter.OnTransportFeedback(
1428 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
elad.alonf9490002017-03-06 05:32:21 -08001429 std::vector<PacketFeedback> feedback =
1430 feedback_adapter.GetTransportFeedbackVector();
elad.alonec304f92017-03-08 05:03:53 -08001431 SortPacketFeedbackVector(&feedback);
elad.alonf9490002017-03-06 05:32:21 -08001432 for (const PacketFeedback& packet : feedback) {
stefanc3de0332016-08-02 07:22:17 -07001433 float x =
1434 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1435 1000000;
stefana0a8ed72017-09-06 02:06:32 -07001436 if (packet.send_time_ms == -1) {
1437 late_feedback_series.points.emplace_back(x, prev_y);
1438 continue;
1439 }
1440 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1441 prev_y = y;
stefanc3de0332016-08-02 07:22:17 -07001442 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1443 time_series.points.emplace_back(x, y);
1444 }
1445 }
1446 ++rtcp_iterator;
1447 }
1448 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1449 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1450 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1451 if (rtp.header.extension.hasTransportSequenceNumber) {
1452 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
elad.alond12a8e12017-03-23 11:04:48 -07001453 feedback_adapter.AddPacket(rtp.header.ssrc,
1454 rtp.header.extension.transportSequenceNumber,
philipel8aadd502017-02-23 02:56:13 -08001455 rtp.total_length, PacedPacketInfo());
stefanc3de0332016-08-02 07:22:17 -07001456 feedback_adapter.OnSentPacket(
1457 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1458 }
1459 ++rtp_iterator;
1460 }
1461 time_us = std::min(NextRtpTime(), NextRtcpTime());
1462 }
1463 // We assume that the base network delay (w/o queues) is the min delay
1464 // observed during the call.
1465 for (TimeSeriesPoint& point : time_series.points)
1466 point.y -= estimated_base_delay_ms;
stefana0a8ed72017-09-06 02:06:32 -07001467 for (TimeSeriesPoint& point : late_feedback_series.points)
1468 point.y -= estimated_base_delay_ms;
stefanc3de0332016-08-02 07:22:17 -07001469 // Add the data set to the plot.
stefana0a8ed72017-09-06 02:06:32 -07001470 plot->AppendTimeSeriesIfNotEmpty(std::move(time_series));
1471 plot->AppendTimeSeriesIfNotEmpty(std::move(late_feedback_series));
stefanc3de0332016-08-02 07:22:17 -07001472
1473 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1474 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1475 plot->SetTitle("Network Delay Change.");
1476}
stefan08383272016-12-20 08:51:52 -08001477
1478std::vector<std::pair<int64_t, int64_t>> EventLogAnalyzer::GetFrameTimestamps()
1479 const {
1480 std::vector<std::pair<int64_t, int64_t>> timestamps;
1481 size_t largest_stream_size = 0;
1482 const std::vector<LoggedRtpPacket>* largest_video_stream = nullptr;
1483 // Find the incoming video stream with the most number of packets that is
1484 // not rtx.
1485 for (const auto& kv : rtp_packets_) {
1486 if (kv.first.GetDirection() == kIncomingPacket &&
1487 video_ssrcs_.find(kv.first) != video_ssrcs_.end() &&
1488 rtx_ssrcs_.find(kv.first) == rtx_ssrcs_.end() &&
1489 kv.second.size() > largest_stream_size) {
1490 largest_stream_size = kv.second.size();
1491 largest_video_stream = &kv.second;
1492 }
1493 }
1494 if (largest_video_stream == nullptr) {
1495 for (auto& packet : *largest_video_stream) {
1496 if (packet.header.markerBit) {
1497 int64_t capture_ms = packet.header.timestamp / 90.0;
1498 int64_t arrival_ms = packet.timestamp / 1000.0;
1499 timestamps.push_back(std::make_pair(capture_ms, arrival_ms));
1500 }
1501 }
1502 }
1503 return timestamps;
1504}
stefane372d3c2017-02-02 08:04:18 -08001505
Bjorn Terelius0295a962017-10-25 17:42:41 +02001506void EventLogAnalyzer::CreatePacerDelayGraph(Plot* plot) {
1507 for (const auto& kv : rtp_packets_) {
1508 const std::vector<LoggedRtpPacket>& packets = kv.second;
1509 StreamId stream_id = kv.first;
1510
1511 if (packets.size() < 2) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001512 RTC_LOG(LS_WARNING)
1513 << "Can't estimate a the RTP clock frequency or the "
1514 "pacer delay with less than 2 packets in the stream";
Bjorn Terelius0295a962017-10-25 17:42:41 +02001515 continue;
1516 }
1517 rtc::Optional<uint32_t> estimated_frequency =
1518 EstimateRtpClockFrequency(packets);
1519 if (!estimated_frequency)
1520 continue;
1521 if (IsVideoSsrc(stream_id) && *estimated_frequency != 90000) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001522 RTC_LOG(LS_WARNING)
Bjorn Terelius0295a962017-10-25 17:42:41 +02001523 << "Video stream should use a 90 kHz clock but appears to use "
1524 << *estimated_frequency / 1000 << ". Discarding.";
1525 continue;
1526 }
1527
1528 TimeSeries pacer_delay_series(
1529 GetStreamName(stream_id) + "(" +
1530 std::to_string(*estimated_frequency / 1000) + " kHz)",
1531 LINE_DOT_GRAPH);
1532 SeqNumUnwrapper<uint32_t> timestamp_unwrapper;
1533 uint64_t first_capture_timestamp =
1534 timestamp_unwrapper.Unwrap(packets.front().header.timestamp);
1535 uint64_t first_send_timestamp = packets.front().timestamp;
1536 for (LoggedRtpPacket packet : packets) {
1537 double capture_time_ms = (static_cast<double>(timestamp_unwrapper.Unwrap(
1538 packet.header.timestamp)) -
1539 first_capture_timestamp) /
1540 *estimated_frequency * 1000;
1541 double send_time_ms =
1542 static_cast<double>(packet.timestamp - first_send_timestamp) / 1000;
1543 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
1544 float y = send_time_ms - capture_time_ms;
1545 pacer_delay_series.points.emplace_back(x, y);
1546 }
1547 plot->AppendTimeSeries(std::move(pacer_delay_series));
1548 }
1549
1550 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1551 plot->SetSuggestedYAxis(0, 10, "Pacer delay (ms)", kBottomMargin, kTopMargin);
1552 plot->SetTitle(
1553 "Delay from capture to send time. (First packet normalized to 0.)");
1554}
1555
stefane372d3c2017-02-02 08:04:18 -08001556void EventLogAnalyzer::CreateTimestampGraph(Plot* plot) {
1557 for (const auto& kv : rtp_packets_) {
1558 const std::vector<LoggedRtpPacket>& rtp_packets = kv.second;
1559 StreamId stream_id = kv.first;
1560
1561 {
terelius23c595a2017-03-15 01:59:12 -07001562 TimeSeries timestamp_data(GetStreamName(stream_id) + " capture-time",
1563 LINE_DOT_GRAPH);
stefane372d3c2017-02-02 08:04:18 -08001564 for (LoggedRtpPacket packet : rtp_packets) {
1565 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
1566 float y = packet.header.timestamp;
1567 timestamp_data.points.emplace_back(x, y);
1568 }
philipel35ba9bd2017-04-19 05:58:51 -07001569 plot->AppendTimeSeries(std::move(timestamp_data));
stefane372d3c2017-02-02 08:04:18 -08001570 }
1571
1572 {
1573 auto kv = rtcp_packets_.find(stream_id);
1574 if (kv != rtcp_packets_.end()) {
1575 const auto& packets = kv->second;
terelius23c595a2017-03-15 01:59:12 -07001576 TimeSeries timestamp_data(
1577 GetStreamName(stream_id) + " rtcp capture-time", LINE_DOT_GRAPH);
stefane372d3c2017-02-02 08:04:18 -08001578 for (const LoggedRtcpPacket& rtcp : packets) {
1579 if (rtcp.type != kRtcpSr)
1580 continue;
1581 rtcp::SenderReport* sr;
1582 sr = static_cast<rtcp::SenderReport*>(rtcp.packet.get());
1583 float x = static_cast<float>(rtcp.timestamp - begin_time_) / 1000000;
1584 float y = sr->rtp_timestamp();
1585 timestamp_data.points.emplace_back(x, y);
1586 }
philipel35ba9bd2017-04-19 05:58:51 -07001587 plot->AppendTimeSeries(std::move(timestamp_data));
stefane372d3c2017-02-02 08:04:18 -08001588 }
1589 }
1590 }
1591
1592 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1593 plot->SetSuggestedYAxis(0, 1, "Timestamp (90khz)", kBottomMargin, kTopMargin);
1594 plot->SetTitle("Timestamps");
1595}
michaelt6e5b2192017-02-22 07:33:27 -08001596
1597void EventLogAnalyzer::CreateAudioEncoderTargetBitrateGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001598 TimeSeries time_series("Audio encoder target bitrate", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001599 ProcessPoints<AudioNetworkAdaptationEvent>(
1600 [](const AudioNetworkAdaptationEvent& ana_event) -> rtc::Optional<float> {
michaelt6e5b2192017-02-22 07:33:27 -08001601 if (ana_event.config.bitrate_bps)
1602 return rtc::Optional<float>(
1603 static_cast<float>(*ana_event.config.bitrate_bps));
1604 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001605 },
philipel35ba9bd2017-04-19 05:58:51 -07001606 audio_network_adaptation_events_, begin_time_, &time_series);
1607 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001608 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1609 plot->SetSuggestedYAxis(0, 1, "Bitrate (bps)", kBottomMargin, kTopMargin);
1610 plot->SetTitle("Reported audio encoder target bitrate");
1611}
1612
1613void EventLogAnalyzer::CreateAudioEncoderFrameLengthGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001614 TimeSeries time_series("Audio encoder frame length", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001615 ProcessPoints<AudioNetworkAdaptationEvent>(
1616 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001617 if (ana_event.config.frame_length_ms)
1618 return rtc::Optional<float>(
1619 static_cast<float>(*ana_event.config.frame_length_ms));
1620 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001621 },
philipel35ba9bd2017-04-19 05:58:51 -07001622 audio_network_adaptation_events_, begin_time_, &time_series);
1623 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001624 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1625 plot->SetSuggestedYAxis(0, 1, "Frame length (ms)", kBottomMargin, kTopMargin);
1626 plot->SetTitle("Reported audio encoder frame length");
1627}
1628
terelius2ee076d2017-08-15 02:04:02 -07001629void EventLogAnalyzer::CreateAudioEncoderPacketLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001630 TimeSeries time_series("Audio encoder uplink packet loss fraction",
1631 LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001632 ProcessPoints<AudioNetworkAdaptationEvent>(
1633 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001634 if (ana_event.config.uplink_packet_loss_fraction)
1635 return rtc::Optional<float>(static_cast<float>(
1636 *ana_event.config.uplink_packet_loss_fraction));
1637 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001638 },
philipel35ba9bd2017-04-19 05:58:51 -07001639 audio_network_adaptation_events_, begin_time_, &time_series);
1640 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001641 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1642 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
1643 kTopMargin);
1644 plot->SetTitle("Reported audio encoder lost packets");
1645}
1646
1647void EventLogAnalyzer::CreateAudioEncoderEnableFecGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001648 TimeSeries time_series("Audio encoder FEC", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001649 ProcessPoints<AudioNetworkAdaptationEvent>(
1650 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001651 if (ana_event.config.enable_fec)
1652 return rtc::Optional<float>(
1653 static_cast<float>(*ana_event.config.enable_fec));
1654 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001655 },
philipel35ba9bd2017-04-19 05:58:51 -07001656 audio_network_adaptation_events_, begin_time_, &time_series);
1657 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001658 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1659 plot->SetSuggestedYAxis(0, 1, "FEC (false/true)", kBottomMargin, kTopMargin);
1660 plot->SetTitle("Reported audio encoder FEC");
1661}
1662
1663void EventLogAnalyzer::CreateAudioEncoderEnableDtxGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001664 TimeSeries time_series("Audio encoder DTX", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001665 ProcessPoints<AudioNetworkAdaptationEvent>(
1666 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001667 if (ana_event.config.enable_dtx)
1668 return rtc::Optional<float>(
1669 static_cast<float>(*ana_event.config.enable_dtx));
1670 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001671 },
philipel35ba9bd2017-04-19 05:58:51 -07001672 audio_network_adaptation_events_, begin_time_, &time_series);
1673 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001674 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1675 plot->SetSuggestedYAxis(0, 1, "DTX (false/true)", kBottomMargin, kTopMargin);
1676 plot->SetTitle("Reported audio encoder DTX");
1677}
1678
1679void EventLogAnalyzer::CreateAudioEncoderNumChannelsGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001680 TimeSeries time_series("Audio encoder number of channels", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001681 ProcessPoints<AudioNetworkAdaptationEvent>(
1682 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001683 if (ana_event.config.num_channels)
1684 return rtc::Optional<float>(
1685 static_cast<float>(*ana_event.config.num_channels));
1686 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001687 },
philipel35ba9bd2017-04-19 05:58:51 -07001688 audio_network_adaptation_events_, begin_time_, &time_series);
1689 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001690 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1691 plot->SetSuggestedYAxis(0, 1, "Number of channels (1 (mono)/2 (stereo))",
1692 kBottomMargin, kTopMargin);
1693 plot->SetTitle("Reported audio encoder number of channels");
1694}
henrik.lundin3c938fc2017-06-14 06:09:58 -07001695
1696class NetEqStreamInput : public test::NetEqInput {
1697 public:
1698 // Does not take any ownership, and all pointers must refer to valid objects
1699 // that outlive the one constructed.
1700 NetEqStreamInput(const std::vector<LoggedRtpPacket>* packet_stream,
1701 const std::vector<uint64_t>* output_events_us,
1702 rtc::Optional<uint64_t> end_time_us)
1703 : packet_stream_(*packet_stream),
1704 packet_stream_it_(packet_stream_.begin()),
1705 output_events_us_it_(output_events_us->begin()),
1706 output_events_us_end_(output_events_us->end()),
1707 end_time_us_(end_time_us) {
1708 RTC_DCHECK(packet_stream);
1709 RTC_DCHECK(output_events_us);
1710 }
1711
1712 rtc::Optional<int64_t> NextPacketTime() const override {
1713 if (packet_stream_it_ == packet_stream_.end()) {
1714 return rtc::Optional<int64_t>();
1715 }
1716 if (end_time_us_ && packet_stream_it_->timestamp > *end_time_us_) {
1717 return rtc::Optional<int64_t>();
1718 }
1719 // Convert from us to ms.
1720 return rtc::Optional<int64_t>(packet_stream_it_->timestamp / 1000);
1721 }
1722
1723 rtc::Optional<int64_t> NextOutputEventTime() const override {
1724 if (output_events_us_it_ == output_events_us_end_) {
1725 return rtc::Optional<int64_t>();
1726 }
1727 if (end_time_us_ && *output_events_us_it_ > *end_time_us_) {
1728 return rtc::Optional<int64_t>();
1729 }
1730 // Convert from us to ms.
1731 return rtc::Optional<int64_t>(
1732 rtc::checked_cast<int64_t>(*output_events_us_it_ / 1000));
1733 }
1734
1735 std::unique_ptr<PacketData> PopPacket() override {
1736 if (packet_stream_it_ == packet_stream_.end()) {
1737 return std::unique_ptr<PacketData>();
1738 }
1739 std::unique_ptr<PacketData> packet_data(new PacketData());
1740 packet_data->header = packet_stream_it_->header;
1741 // Convert from us to ms.
1742 packet_data->time_ms = packet_stream_it_->timestamp / 1000.0;
1743
1744 // This is a header-only "dummy" packet. Set the payload to all zeros, with
1745 // length according to the virtual length.
1746 packet_data->payload.SetSize(packet_stream_it_->total_length);
1747 std::fill_n(packet_data->payload.data(), packet_data->payload.size(), 0);
1748
1749 ++packet_stream_it_;
1750 return packet_data;
1751 }
1752
1753 void AdvanceOutputEvent() override {
1754 if (output_events_us_it_ != output_events_us_end_) {
1755 ++output_events_us_it_;
1756 }
1757 }
1758
1759 bool ended() const override { return !NextEventTime(); }
1760
1761 rtc::Optional<RTPHeader> NextHeader() const override {
1762 if (packet_stream_it_ == packet_stream_.end()) {
1763 return rtc::Optional<RTPHeader>();
1764 }
1765 return rtc::Optional<RTPHeader>(packet_stream_it_->header);
1766 }
1767
1768 private:
1769 const std::vector<LoggedRtpPacket>& packet_stream_;
1770 std::vector<LoggedRtpPacket>::const_iterator packet_stream_it_;
1771 std::vector<uint64_t>::const_iterator output_events_us_it_;
1772 const std::vector<uint64_t>::const_iterator output_events_us_end_;
1773 const rtc::Optional<uint64_t> end_time_us_;
1774};
1775
1776namespace {
1777// Creates a NetEq test object and all necessary input and output helpers. Runs
1778// the test and returns the NetEqDelayAnalyzer object that was used to
1779// instrument the test.
1780std::unique_ptr<test::NetEqDelayAnalyzer> CreateNetEqTestAndRun(
1781 const std::vector<LoggedRtpPacket>* packet_stream,
1782 const std::vector<uint64_t>* output_events_us,
1783 rtc::Optional<uint64_t> end_time_us,
1784 const std::string& replacement_file_name,
1785 int file_sample_rate_hz) {
1786 std::unique_ptr<test::NetEqInput> input(
1787 new NetEqStreamInput(packet_stream, output_events_us, end_time_us));
1788
1789 constexpr int kReplacementPt = 127;
1790 std::set<uint8_t> cn_types;
1791 std::set<uint8_t> forbidden_types;
1792 input.reset(new test::NetEqReplacementInput(std::move(input), kReplacementPt,
1793 cn_types, forbidden_types));
1794
1795 NetEq::Config config;
1796 config.max_packets_in_buffer = 200;
1797 config.enable_fast_accelerate = true;
1798
1799 std::unique_ptr<test::VoidAudioSink> output(new test::VoidAudioSink());
1800
1801 test::NetEqTest::DecoderMap codecs;
1802
1803 // Create a "replacement decoder" that produces the decoded audio by reading
1804 // from a file rather than from the encoded payloads.
1805 std::unique_ptr<test::ResampleInputAudioFile> replacement_file(
1806 new test::ResampleInputAudioFile(replacement_file_name,
1807 file_sample_rate_hz));
1808 replacement_file->set_output_rate_hz(48000);
1809 std::unique_ptr<AudioDecoder> replacement_decoder(
1810 new test::FakeDecodeFromFile(std::move(replacement_file), 48000, false));
1811 test::NetEqTest::ExtDecoderMap ext_codecs;
1812 ext_codecs[kReplacementPt] = {replacement_decoder.get(),
1813 NetEqDecoder::kDecoderArbitrary,
1814 "replacement codec"};
1815
1816 std::unique_ptr<test::NetEqDelayAnalyzer> delay_cb(
1817 new test::NetEqDelayAnalyzer);
1818 test::DefaultNetEqTestErrorCallback error_cb;
1819 test::NetEqTest::Callbacks callbacks;
1820 callbacks.error_callback = &error_cb;
1821 callbacks.post_insert_packet = delay_cb.get();
1822 callbacks.get_audio_callback = delay_cb.get();
1823
1824 test::NetEqTest test(config, codecs, ext_codecs, std::move(input),
1825 std::move(output), callbacks);
1826 test.Run();
1827 return delay_cb;
1828}
1829} // namespace
1830
1831// Plots the jitter buffer delay profile. This will plot only for the first
1832// incoming audio SSRC. If the stream contains more than one incoming audio
1833// SSRC, all but the first will be ignored.
1834void EventLogAnalyzer::CreateAudioJitterBufferGraph(
1835 const std::string& replacement_file_name,
1836 int file_sample_rate_hz,
1837 Plot* plot) {
1838 const auto& incoming_audio_kv = std::find_if(
1839 rtp_packets_.begin(), rtp_packets_.end(),
1840 [this](std::pair<StreamId, std::vector<LoggedRtpPacket>> kv) {
1841 return kv.first.GetDirection() == kIncomingPacket &&
1842 this->IsAudioSsrc(kv.first);
1843 });
1844 if (incoming_audio_kv == rtp_packets_.end()) {
1845 // No incoming audio stream found.
1846 return;
1847 }
1848
1849 const uint32_t ssrc = incoming_audio_kv->first.GetSsrc();
1850
1851 std::map<uint32_t, std::vector<uint64_t>>::const_iterator output_events_it =
1852 audio_playout_events_.find(ssrc);
1853 if (output_events_it == audio_playout_events_.end()) {
1854 // Could not find output events with SSRC matching the input audio stream.
1855 // Using the first available stream of output events.
1856 output_events_it = audio_playout_events_.cbegin();
1857 }
1858
1859 rtc::Optional<uint64_t> end_time_us =
1860 log_segments_.empty()
1861 ? rtc::Optional<uint64_t>()
1862 : rtc::Optional<uint64_t>(log_segments_.front().second);
1863
1864 auto delay_cb = CreateNetEqTestAndRun(
1865 &incoming_audio_kv->second, &output_events_it->second, end_time_us,
1866 replacement_file_name, file_sample_rate_hz);
1867
1868 std::vector<float> send_times_s;
1869 std::vector<float> arrival_delay_ms;
1870 std::vector<float> corrected_arrival_delay_ms;
1871 std::vector<rtc::Optional<float>> playout_delay_ms;
1872 std::vector<rtc::Optional<float>> target_delay_ms;
1873 delay_cb->CreateGraphs(&send_times_s, &arrival_delay_ms,
1874 &corrected_arrival_delay_ms, &playout_delay_ms,
1875 &target_delay_ms);
1876 RTC_DCHECK_EQ(send_times_s.size(), arrival_delay_ms.size());
1877 RTC_DCHECK_EQ(send_times_s.size(), corrected_arrival_delay_ms.size());
1878 RTC_DCHECK_EQ(send_times_s.size(), playout_delay_ms.size());
1879 RTC_DCHECK_EQ(send_times_s.size(), target_delay_ms.size());
1880
1881 std::map<StreamId, TimeSeries> time_series_packet_arrival;
1882 std::map<StreamId, TimeSeries> time_series_relative_packet_arrival;
1883 std::map<StreamId, TimeSeries> time_series_play_time;
1884 std::map<StreamId, TimeSeries> time_series_target_time;
1885 float min_y_axis = 0.f;
1886 float max_y_axis = 0.f;
1887 const StreamId stream_id = incoming_audio_kv->first;
1888 for (size_t i = 0; i < send_times_s.size(); ++i) {
1889 time_series_packet_arrival[stream_id].points.emplace_back(
1890 TimeSeriesPoint(send_times_s[i], arrival_delay_ms[i]));
1891 time_series_relative_packet_arrival[stream_id].points.emplace_back(
1892 TimeSeriesPoint(send_times_s[i], corrected_arrival_delay_ms[i]));
1893 min_y_axis = std::min(min_y_axis, corrected_arrival_delay_ms[i]);
1894 max_y_axis = std::max(max_y_axis, corrected_arrival_delay_ms[i]);
1895 if (playout_delay_ms[i]) {
1896 time_series_play_time[stream_id].points.emplace_back(
1897 TimeSeriesPoint(send_times_s[i], *playout_delay_ms[i]));
1898 min_y_axis = std::min(min_y_axis, *playout_delay_ms[i]);
1899 max_y_axis = std::max(max_y_axis, *playout_delay_ms[i]);
1900 }
1901 if (target_delay_ms[i]) {
1902 time_series_target_time[stream_id].points.emplace_back(
1903 TimeSeriesPoint(send_times_s[i], *target_delay_ms[i]));
1904 min_y_axis = std::min(min_y_axis, *target_delay_ms[i]);
1905 max_y_axis = std::max(max_y_axis, *target_delay_ms[i]);
1906 }
1907 }
1908
1909 // This code is adapted for a single stream. The creation of the streams above
1910 // guarantee that no more than one steam is included. If multiple streams are
1911 // to be plotted, they should likely be given distinct labels below.
1912 RTC_DCHECK_EQ(time_series_relative_packet_arrival.size(), 1);
1913 for (auto& series : time_series_relative_packet_arrival) {
1914 series.second.label = "Relative packet arrival delay";
1915 series.second.style = LINE_GRAPH;
1916 plot->AppendTimeSeries(std::move(series.second));
1917 }
1918 RTC_DCHECK_EQ(time_series_play_time.size(), 1);
1919 for (auto& series : time_series_play_time) {
1920 series.second.label = "Playout delay";
1921 series.second.style = LINE_GRAPH;
1922 plot->AppendTimeSeries(std::move(series.second));
1923 }
1924 RTC_DCHECK_EQ(time_series_target_time.size(), 1);
1925 for (auto& series : time_series_target_time) {
1926 series.second.label = "Target delay";
1927 series.second.style = LINE_DOT_GRAPH;
1928 plot->AppendTimeSeries(std::move(series.second));
1929 }
1930
1931 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1932 plot->SetYAxis(min_y_axis, max_y_axis, "Relative delay (ms)", kBottomMargin,
1933 kTopMargin);
1934 plot->SetTitle("NetEq timing");
1935}
terelius54ce6802016-07-13 06:44:41 -07001936} // namespace plotting
1937} // namespace webrtc