blob: d48af60faaae471be224368dbe27ec242ab99fa1 [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) {
116 LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
117 << " expected to be in the range (" << min_difference / 2
118 << "," << max_difference / 2 << ") but is " << difference
119 << ". Correct unwrapping is uncertain.";
120 }
terelius54ce6802016-07-13 06:44:41 -0700121 return difference;
122}
123
ivocaac9d6f2016-09-22 07:01:47 -0700124// Return default values for header extensions, to use on streams without stored
125// mapping data. Currently this only applies to audio streams, since the mapping
126// is not stored in the event log.
127// TODO(ivoc): Remove this once this mapping is stored in the event log for
128// audio streams. Tracking bug: webrtc:6399
129webrtc::RtpHeaderExtensionMap GetDefaultHeaderExtensionMap() {
130 webrtc::RtpHeaderExtensionMap default_map;
danilchap4aecc582016-11-15 09:21:00 -0800131 default_map.Register<AudioLevel>(webrtc::RtpExtension::kAudioLevelDefaultId);
terelius007d5622017-08-08 05:40:26 -0700132 default_map.Register<TransmissionOffset>(
133 webrtc::RtpExtension::kTimestampOffsetDefaultId);
danilchap4aecc582016-11-15 09:21:00 -0800134 default_map.Register<AbsoluteSendTime>(
ivocaac9d6f2016-09-22 07:01:47 -0700135 webrtc::RtpExtension::kAbsSendTimeDefaultId);
terelius007d5622017-08-08 05:40:26 -0700136 default_map.Register<VideoOrientation>(
137 webrtc::RtpExtension::kVideoRotationDefaultId);
138 default_map.Register<VideoContentTypeExtension>(
139 webrtc::RtpExtension::kVideoContentTypeDefaultId);
140 default_map.Register<VideoTimingExtension>(
141 webrtc::RtpExtension::kVideoTimingDefaultId);
142 default_map.Register<TransportSequenceNumber>(
143 webrtc::RtpExtension::kTransportSequenceNumberDefaultId);
144 default_map.Register<PlayoutDelayLimits>(
145 webrtc::RtpExtension::kPlayoutDelayDefaultId);
ivocaac9d6f2016-09-22 07:01:47 -0700146 return default_map;
147}
148
tereliusdc35dcd2016-08-01 12:03:27 -0700149constexpr float kLeftMargin = 0.01f;
150constexpr float kRightMargin = 0.02f;
151constexpr float kBottomMargin = 0.02f;
152constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700153
terelius53dc23c2017-03-13 05:24:05 -0700154rtc::Optional<double> NetworkDelayDiff_AbsSendTime(
155 const LoggedRtpPacket& old_packet,
156 const LoggedRtpPacket& new_packet) {
157 if (old_packet.header.extension.hasAbsoluteSendTime &&
158 new_packet.header.extension.hasAbsoluteSendTime) {
159 int64_t send_time_diff = WrappingDifference(
160 new_packet.header.extension.absoluteSendTime,
161 old_packet.header.extension.absoluteSendTime, 1ul << 24);
162 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
163 double delay_change_us =
164 recv_time_diff - AbsSendTimeToMicroseconds(send_time_diff);
165 return rtc::Optional<double>(delay_change_us / 1000);
166 } else {
167 return rtc::Optional<double>();
terelius6addf492016-08-23 17:34:07 -0700168 }
169}
170
terelius53dc23c2017-03-13 05:24:05 -0700171rtc::Optional<double> NetworkDelayDiff_CaptureTime(
172 const LoggedRtpPacket& old_packet,
173 const LoggedRtpPacket& new_packet) {
174 int64_t send_time_diff = WrappingDifference(
175 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
176 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
177
178 const double kVideoSampleRate = 90000;
179 // TODO(terelius): We treat all streams as video for now, even though
180 // audio might be sampled at e.g. 16kHz, because it is really difficult to
181 // figure out the true sampling rate of a stream. The effect is that the
182 // delay will be scaled incorrectly for non-video streams.
183
184 double delay_change =
185 static_cast<double>(recv_time_diff) / 1000 -
186 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
187 if (delay_change < -10000 || 10000 < delay_change) {
188 LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
189 LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
190 << ", received time " << old_packet.timestamp;
191 LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
192 << ", received time " << new_packet.timestamp;
193 LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
194 << static_cast<double>(recv_time_diff) / 1000000 << "s";
195 LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
196 << static_cast<double>(send_time_diff) / kVideoSampleRate
197 << "s";
198 }
199 return rtc::Optional<double>(delay_change);
200}
201
202// For each element in data, use |get_y()| to extract a y-coordinate and
203// store the result in a TimeSeries.
204template <typename DataType>
205void ProcessPoints(
206 rtc::FunctionView<rtc::Optional<float>(const DataType&)> get_y,
207 const std::vector<DataType>& data,
208 uint64_t begin_time,
209 TimeSeries* result) {
210 for (size_t i = 0; i < data.size(); i++) {
211 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
212 rtc::Optional<float> y = get_y(data[i]);
213 if (y)
214 result->points.emplace_back(x, *y);
215 }
216}
217
218// For each pair of adjacent elements in |data|, use |get_y| to extract a
terelius6addf492016-08-23 17:34:07 -0700219// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
220// will be the time of the second element in the pair.
terelius53dc23c2017-03-13 05:24:05 -0700221template <typename DataType, typename ResultType>
222void ProcessPairs(
223 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
224 const DataType&)> get_y,
225 const std::vector<DataType>& data,
226 uint64_t begin_time,
227 TimeSeries* result) {
tereliusccbbf8d2016-08-10 07:34:28 -0700228 for (size_t i = 1; i < data.size(); i++) {
229 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
terelius53dc23c2017-03-13 05:24:05 -0700230 rtc::Optional<ResultType> y = get_y(data[i - 1], data[i]);
231 if (y)
232 result->points.emplace_back(x, static_cast<float>(*y));
233 }
234}
235
236// For each element in data, use |extract()| to extract a y-coordinate and
237// store the result in a TimeSeries.
238template <typename DataType, typename ResultType>
239void AccumulatePoints(
240 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> extract,
241 const std::vector<DataType>& data,
242 uint64_t begin_time,
243 TimeSeries* result) {
244 ResultType sum = 0;
245 for (size_t i = 0; i < data.size(); i++) {
246 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
247 rtc::Optional<ResultType> y = extract(data[i]);
248 if (y) {
249 sum += *y;
250 result->points.emplace_back(x, static_cast<float>(sum));
251 }
252 }
253}
254
255// For each pair of adjacent elements in |data|, use |extract()| to extract a
256// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
257// will be the time of the second element in the pair.
258template <typename DataType, typename ResultType>
259void AccumulatePairs(
260 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
261 const DataType&)> extract,
262 const std::vector<DataType>& data,
263 uint64_t begin_time,
264 TimeSeries* result) {
265 ResultType sum = 0;
266 for (size_t i = 1; i < data.size(); i++) {
267 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
268 rtc::Optional<ResultType> y = extract(data[i - 1], data[i]);
269 if (y)
270 sum += *y;
271 result->points.emplace_back(x, static_cast<float>(sum));
tereliusccbbf8d2016-08-10 07:34:28 -0700272 }
273}
274
terelius6addf492016-08-23 17:34:07 -0700275// Calculates a moving average of |data| and stores the result in a TimeSeries.
276// A data point is generated every |step| microseconds from |begin_time|
277// to |end_time|. The value of each data point is the average of the data
278// during the preceeding |window_duration_us| microseconds.
terelius53dc23c2017-03-13 05:24:05 -0700279template <typename DataType, typename ResultType>
280void MovingAverage(
281 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> extract,
282 const std::vector<DataType>& data,
283 uint64_t begin_time,
284 uint64_t end_time,
285 uint64_t window_duration_us,
286 uint64_t step,
287 webrtc::plotting::TimeSeries* result) {
terelius6addf492016-08-23 17:34:07 -0700288 size_t window_index_begin = 0;
289 size_t window_index_end = 0;
terelius53dc23c2017-03-13 05:24:05 -0700290 ResultType sum_in_window = 0;
terelius6addf492016-08-23 17:34:07 -0700291
292 for (uint64_t t = begin_time; t < end_time + step; t += step) {
293 while (window_index_end < data.size() &&
294 data[window_index_end].timestamp < t) {
terelius53dc23c2017-03-13 05:24:05 -0700295 rtc::Optional<ResultType> value = extract(data[window_index_end]);
296 if (value)
297 sum_in_window += *value;
terelius6addf492016-08-23 17:34:07 -0700298 ++window_index_end;
299 }
300 while (window_index_begin < data.size() &&
301 data[window_index_begin].timestamp < t - window_duration_us) {
terelius53dc23c2017-03-13 05:24:05 -0700302 rtc::Optional<ResultType> value = extract(data[window_index_begin]);
303 if (value)
304 sum_in_window -= *value;
terelius6addf492016-08-23 17:34:07 -0700305 ++window_index_begin;
306 }
307 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
308 float x = static_cast<float>(t - begin_time) / 1000000;
terelius53dc23c2017-03-13 05:24:05 -0700309 float y = sum_in_window / window_duration_s;
terelius6addf492016-08-23 17:34:07 -0700310 result->points.emplace_back(x, y);
311 }
312}
313
terelius54ce6802016-07-13 06:44:41 -0700314} // namespace
315
terelius54ce6802016-07-13 06:44:41 -0700316EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
317 : parsed_log_(log), window_duration_(250000), step_(10000) {
318 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
319 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700320
terelius88e64e52016-07-19 01:51:06 -0700321 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700322 uint8_t header[IP_PACKET_SIZE];
323 size_t header_length;
324 size_t total_length;
325
perkjbbbad6d2017-05-19 06:30:28 -0700326 uint8_t last_incoming_rtcp_packet[IP_PACKET_SIZE];
327 uint8_t last_incoming_rtcp_packet_length = 0;
328
ivocaac9d6f2016-09-22 07:01:47 -0700329 // Make a default extension map for streams without configuration information.
330 // TODO(ivoc): Once configuration of audio streams is stored in the event log,
331 // this can be removed. Tracking bug: webrtc:6399
332 RtpHeaderExtensionMap default_extension_map = GetDefaultHeaderExtensionMap();
333
henrik.lundin3c938fc2017-06-14 06:09:58 -0700334 rtc::Optional<uint64_t> last_log_start;
335
terelius54ce6802016-07-13 06:44:41 -0700336 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
337 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700338 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
339 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
340 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700341 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
342 event_type != ParsedRtcEventLog::LOG_START &&
343 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700344 uint64_t timestamp = parsed_log_.GetTimestamp(i);
345 first_timestamp = std::min(first_timestamp, timestamp);
346 last_timestamp = std::max(last_timestamp, timestamp);
347 }
348
349 switch (parsed_log_.GetEventType(i)) {
350 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700351 rtclog::StreamConfig config = parsed_log_.GetVideoReceiveConfig(i);
perkj09e71da2017-05-22 03:26:49 -0700352 StreamId stream(config.remote_ssrc, kIncomingPacket);
terelius0740a202016-08-08 10:21:04 -0700353 video_ssrcs_.insert(stream);
perkj09e71da2017-05-22 03:26:49 -0700354 StreamId rtx_stream(config.rtx_ssrc, kIncomingPacket);
brandtr14742122017-01-27 04:53:07 -0800355 video_ssrcs_.insert(rtx_stream);
356 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700357 break;
358 }
359 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700360 std::vector<rtclog::StreamConfig> configs =
361 parsed_log_.GetVideoSendConfig(i);
terelius405f90c2017-06-01 03:50:31 -0700362 for (const auto& config : configs) {
363 StreamId stream(config.local_ssrc, kOutgoingPacket);
terelius8fbc7652017-05-31 02:03:16 -0700364 video_ssrcs_.insert(stream);
terelius405f90c2017-06-01 03:50:31 -0700365 StreamId rtx_stream(config.rtx_ssrc, kOutgoingPacket);
terelius8fbc7652017-05-31 02:03:16 -0700366 video_ssrcs_.insert(rtx_stream);
367 rtx_ssrcs_.insert(rtx_stream);
368 }
terelius88e64e52016-07-19 01:51:06 -0700369 break;
370 }
371 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700372 rtclog::StreamConfig config = parsed_log_.GetAudioReceiveConfig(i);
perkjac8f52d2017-05-22 09:36:28 -0700373 StreamId stream(config.remote_ssrc, kIncomingPacket);
ivoce0928d82016-10-10 05:12:51 -0700374 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700375 break;
376 }
377 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700378 rtclog::StreamConfig config = parsed_log_.GetAudioSendConfig(i);
perkjf4726992017-05-22 10:12:26 -0700379 StreamId stream(config.local_ssrc, kOutgoingPacket);
ivoce0928d82016-10-10 05:12:51 -0700380 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700381 break;
382 }
383 case ParsedRtcEventLog::RTP_EVENT: {
ilnika8e781a2017-06-12 01:02:46 -0700384 RtpHeaderExtensionMap* extension_map = parsed_log_.GetRtpHeader(
Elad Alon1d87b0e2017-10-03 15:01:03 +0200385 i, &direction, header, &header_length, &total_length, nullptr);
terelius88e64e52016-07-19 01:51:06 -0700386 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
387 RTPHeader parsed_header;
ilnika8e781a2017-06-12 01:02:46 -0700388 if (extension_map != nullptr) {
terelius88e64e52016-07-19 01:51:06 -0700389 rtp_parser.Parse(&parsed_header, extension_map);
ivocaac9d6f2016-09-22 07:01:47 -0700390 } else {
391 // Use the default extension map.
392 // TODO(ivoc): Once configuration of audio streams is stored in the
393 // event log, this can be removed.
394 // Tracking bug: webrtc:6399
395 rtp_parser.Parse(&parsed_header, &default_extension_map);
terelius88e64e52016-07-19 01:51:06 -0700396 }
397 uint64_t timestamp = parsed_log_.GetTimestamp(i);
ilnika8e781a2017-06-12 01:02:46 -0700398 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700399 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200400 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700401 break;
402 }
403 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200404 uint8_t packet[IP_PACKET_SIZE];
perkj77cd58e2017-05-30 03:52:10 -0700405 parsed_log_.GetRtcpPacket(i, &direction, packet, &total_length);
perkjbbbad6d2017-05-19 06:30:28 -0700406 // Currently incoming RTCP packets are logged twice, both for audio and
407 // video. Only act on one of them. Compare against the previous parsed
408 // incoming RTCP packet.
409 if (direction == webrtc::kIncomingPacket) {
410 RTC_CHECK_LE(total_length, IP_PACKET_SIZE);
411 if (total_length == last_incoming_rtcp_packet_length &&
412 memcmp(last_incoming_rtcp_packet, packet, total_length) == 0) {
413 continue;
414 } else {
415 memcpy(last_incoming_rtcp_packet, packet, total_length);
416 last_incoming_rtcp_packet_length = total_length;
417 }
418 }
419 rtcp::CommonHeader header;
420 const uint8_t* packet_end = packet + total_length;
421 for (const uint8_t* block = packet; block < packet_end;
422 block = header.NextPacket()) {
423 RTC_CHECK(header.Parse(block, packet_end - block));
424 if (header.type() == rtcp::TransportFeedback::kPacketType &&
425 header.fmt() == rtcp::TransportFeedback::kFeedbackMessageType) {
426 std::unique_ptr<rtcp::TransportFeedback> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700427 rtc::MakeUnique<rtcp::TransportFeedback>());
perkjbbbad6d2017-05-19 06:30:28 -0700428 if (rtcp_packet->Parse(header)) {
429 uint32_t ssrc = rtcp_packet->sender_ssrc();
430 StreamId stream(ssrc, direction);
431 uint64_t timestamp = parsed_log_.GetTimestamp(i);
432 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
433 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
434 }
435 } else if (header.type() == rtcp::SenderReport::kPacketType) {
436 std::unique_ptr<rtcp::SenderReport> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700437 rtc::MakeUnique<rtcp::SenderReport>());
perkjbbbad6d2017-05-19 06:30:28 -0700438 if (rtcp_packet->Parse(header)) {
439 uint32_t ssrc = rtcp_packet->sender_ssrc();
440 StreamId stream(ssrc, direction);
441 uint64_t timestamp = parsed_log_.GetTimestamp(i);
442 rtcp_packets_[stream].push_back(
443 LoggedRtcpPacket(timestamp, kRtcpSr, std::move(rtcp_packet)));
444 }
445 } else if (header.type() == rtcp::ReceiverReport::kPacketType) {
446 std::unique_ptr<rtcp::ReceiverReport> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700447 rtc::MakeUnique<rtcp::ReceiverReport>());
perkjbbbad6d2017-05-19 06:30:28 -0700448 if (rtcp_packet->Parse(header)) {
449 uint32_t ssrc = rtcp_packet->sender_ssrc();
450 StreamId stream(ssrc, direction);
451 uint64_t timestamp = parsed_log_.GetTimestamp(i);
452 rtcp_packets_[stream].push_back(
453 LoggedRtcpPacket(timestamp, kRtcpRr, std::move(rtcp_packet)));
Stefan Holmer13181032016-07-29 14:48:54 +0200454 }
terelius2c8e8a32017-06-02 01:29:48 -0700455 } else if (header.type() == rtcp::Remb::kPacketType &&
456 header.fmt() == rtcp::Remb::kFeedbackMessageType) {
457 std::unique_ptr<rtcp::Remb> rtcp_packet(
458 rtc::MakeUnique<rtcp::Remb>());
459 if (rtcp_packet->Parse(header)) {
460 uint32_t ssrc = rtcp_packet->sender_ssrc();
461 StreamId stream(ssrc, direction);
462 uint64_t timestamp = parsed_log_.GetTimestamp(i);
463 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
464 timestamp, kRtcpRemb, std::move(rtcp_packet)));
465 }
Stefan Holmer13181032016-07-29 14:48:54 +0200466 }
Stefan Holmer13181032016-07-29 14:48:54 +0200467 }
terelius88e64e52016-07-19 01:51:06 -0700468 break;
469 }
470 case ParsedRtcEventLog::LOG_START: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700471 if (last_log_start) {
472 // A LOG_END event was missing. Use last_timestamp.
473 RTC_DCHECK_GE(last_timestamp, *last_log_start);
474 log_segments_.push_back(
475 std::make_pair(*last_log_start, last_timestamp));
476 }
477 last_log_start = rtc::Optional<uint64_t>(parsed_log_.GetTimestamp(i));
terelius88e64e52016-07-19 01:51:06 -0700478 break;
479 }
480 case ParsedRtcEventLog::LOG_END: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700481 RTC_DCHECK(last_log_start);
482 log_segments_.push_back(
483 std::make_pair(*last_log_start, parsed_log_.GetTimestamp(i)));
484 last_log_start.reset();
terelius88e64e52016-07-19 01:51:06 -0700485 break;
486 }
terelius424e6cf2017-02-20 05:14:41 -0800487 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700488 uint32_t this_ssrc;
489 parsed_log_.GetAudioPlayout(i, &this_ssrc);
490 audio_playout_events_[this_ssrc].push_back(parsed_log_.GetTimestamp(i));
terelius424e6cf2017-02-20 05:14:41 -0800491 break;
492 }
493 case ParsedRtcEventLog::LOSS_BASED_BWE_UPDATE: {
494 LossBasedBweUpdate bwe_update;
terelius8058e582016-07-25 01:32:41 -0700495 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
terelius424e6cf2017-02-20 05:14:41 -0800496 parsed_log_.GetLossBasedBweUpdate(i, &bwe_update.new_bitrate,
497 &bwe_update.fraction_loss,
498 &bwe_update.expected_packets);
terelius8058e582016-07-25 01:32:41 -0700499 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700500 break;
501 }
terelius424e6cf2017-02-20 05:14:41 -0800502 case ParsedRtcEventLog::DELAY_BASED_BWE_UPDATE: {
philipel10fc0e62017-04-11 01:50:23 -0700503 bwe_delay_updates_.push_back(parsed_log_.GetDelayBasedBweUpdate(i));
terelius424e6cf2017-02-20 05:14:41 -0800504 break;
505 }
minyue4b7c9522017-01-24 04:54:59 -0800506 case ParsedRtcEventLog::AUDIO_NETWORK_ADAPTATION_EVENT: {
michaelt6e5b2192017-02-22 07:33:27 -0800507 AudioNetworkAdaptationEvent ana_event;
508 ana_event.timestamp = parsed_log_.GetTimestamp(i);
509 parsed_log_.GetAudioNetworkAdaptation(i, &ana_event.config);
510 audio_network_adaptation_events_.push_back(ana_event);
minyue4b7c9522017-01-24 04:54:59 -0800511 break;
512 }
philipel32d00102017-02-27 02:18:46 -0800513 case ParsedRtcEventLog::BWE_PROBE_CLUSTER_CREATED_EVENT: {
philipele127e7a2017-03-29 16:28:53 +0200514 bwe_probe_cluster_created_events_.push_back(
515 parsed_log_.GetBweProbeClusterCreated(i));
philipel32d00102017-02-27 02:18:46 -0800516 break;
517 }
518 case ParsedRtcEventLog::BWE_PROBE_RESULT_EVENT: {
philipele127e7a2017-03-29 16:28:53 +0200519 bwe_probe_result_events_.push_back(parsed_log_.GetBweProbeResult(i));
philipel32d00102017-02-27 02:18:46 -0800520 break;
521 }
terelius88e64e52016-07-19 01:51:06 -0700522 case ParsedRtcEventLog::UNKNOWN_EVENT: {
523 break;
524 }
525 }
terelius54ce6802016-07-13 06:44:41 -0700526 }
terelius88e64e52016-07-19 01:51:06 -0700527
terelius54ce6802016-07-13 06:44:41 -0700528 if (last_timestamp < first_timestamp) {
529 // No useful events in the log.
530 first_timestamp = last_timestamp = 0;
531 }
532 begin_time_ = first_timestamp;
533 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700534 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
henrik.lundin3c938fc2017-06-14 06:09:58 -0700535 if (last_log_start) {
536 // The log was missing the last LOG_END event. Fake it.
537 log_segments_.push_back(std::make_pair(*last_log_start, end_time_));
538 }
terelius54ce6802016-07-13 06:44:41 -0700539}
540
Niels Möller245f17e2017-08-21 10:45:07 +0200541class BitrateObserver : public SendSideCongestionController::Observer,
Stefan Holmer13181032016-07-29 14:48:54 +0200542 public RemoteBitrateObserver {
543 public:
544 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
545
546 void OnNetworkChanged(uint32_t bitrate_bps,
547 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800548 int64_t rtt_ms,
549 int64_t probing_interval_ms) override {
Stefan Holmer13181032016-07-29 14:48:54 +0200550 last_bitrate_bps_ = bitrate_bps;
551 bitrate_updated_ = true;
552 }
553
554 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
555 uint32_t bitrate) override {}
556
557 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
558 bool GetAndResetBitrateUpdated() {
559 bool bitrate_updated = bitrate_updated_;
560 bitrate_updated_ = false;
561 return bitrate_updated;
562 }
563
564 private:
565 uint32_t last_bitrate_bps_;
566 bool bitrate_updated_;
567};
568
Stefan Holmer99f8e082016-09-09 13:37:50 +0200569bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700570 return rtx_ssrcs_.count(stream_id) == 1;
571}
572
Stefan Holmer99f8e082016-09-09 13:37:50 +0200573bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700574 return video_ssrcs_.count(stream_id) == 1;
575}
576
Stefan Holmer99f8e082016-09-09 13:37:50 +0200577bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700578 return audio_ssrcs_.count(stream_id) == 1;
579}
580
Stefan Holmer99f8e082016-09-09 13:37:50 +0200581std::string EventLogAnalyzer::GetStreamName(StreamId stream_id) const {
582 std::stringstream name;
583 if (IsAudioSsrc(stream_id)) {
584 name << "Audio ";
585 } else if (IsVideoSsrc(stream_id)) {
586 name << "Video ";
587 } else {
588 name << "Unknown ";
589 }
590 if (IsRtxSsrc(stream_id))
591 name << "RTX ";
ivocaac9d6f2016-09-22 07:01:47 -0700592 if (stream_id.GetDirection() == kIncomingPacket) {
593 name << "(In) ";
594 } else {
595 name << "(Out) ";
596 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200597 name << SsrcToString(stream_id.GetSsrc());
598 return name.str();
599}
600
Bjorn Terelius0295a962017-10-25 17:42:41 +0200601// This is much more reliable for outgoing streams than for incoming streams.
602rtc::Optional<uint32_t> EventLogAnalyzer::EstimateRtpClockFrequency(
603 const std::vector<LoggedRtpPacket>& packets) const {
604 RTC_CHECK(packets.size() >= 2);
605 uint64_t end_time_us = log_segments_.empty()
606 ? std::numeric_limits<uint64_t>::max()
607 : log_segments_.front().second;
608 SeqNumUnwrapper<uint32_t> unwrapper;
609 uint64_t first_rtp_timestamp = unwrapper.Unwrap(packets[0].header.timestamp);
610 uint64_t first_log_timestamp = packets[0].timestamp;
611 uint64_t last_rtp_timestamp = first_rtp_timestamp;
612 uint64_t last_log_timestamp = first_log_timestamp;
613 for (size_t i = 1; i < packets.size(); i++) {
614 if (packets[i].timestamp > end_time_us)
615 break;
616 last_rtp_timestamp = unwrapper.Unwrap(packets[i].header.timestamp);
617 last_log_timestamp = packets[i].timestamp;
618 }
619 if (last_log_timestamp - first_log_timestamp < 1000000) {
620 LOG(LS_WARNING)
621 << "Failed to estimate RTP clock frequency: Stream too short. ("
622 << packets.size() << " packets, "
623 << last_log_timestamp - first_log_timestamp << " us)";
624 return rtc::Optional<uint32_t>();
625 }
626 double duration =
627 static_cast<double>(last_log_timestamp - first_log_timestamp) / 1000000;
628 double estimated_frequency =
629 (last_rtp_timestamp - first_rtp_timestamp) / duration;
630 for (uint32_t f : {8000, 16000, 32000, 48000, 90000}) {
631 if (std::fabs(estimated_frequency - f) < 0.05 * f) {
632 return rtc::Optional<uint32_t>(f);
633 }
634 }
635 LOG(LS_WARNING) << "Failed to estimate RTP clock frequency: Estimate "
636 << estimated_frequency
637 << "not close to any stardard RTP frequency.";
638 return rtc::Optional<uint32_t>();
639}
640
terelius54ce6802016-07-13 06:44:41 -0700641void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
642 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700643 for (auto& kv : rtp_packets_) {
644 StreamId stream_id = kv.first;
645 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
646 // Filter on direction and SSRC.
647 if (stream_id.GetDirection() != desired_direction ||
648 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
649 continue;
terelius54ce6802016-07-13 06:44:41 -0700650 }
terelius54ce6802016-07-13 06:44:41 -0700651
terelius23c595a2017-03-15 01:59:12 -0700652 TimeSeries time_series(GetStreamName(stream_id), BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700653 ProcessPoints<LoggedRtpPacket>(
654 [](const LoggedRtpPacket& packet) -> rtc::Optional<float> {
655 return rtc::Optional<float>(packet.total_length);
656 },
657 packet_stream, begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700658 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700659 }
660
tereliusdc35dcd2016-08-01 12:03:27 -0700661 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
662 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
663 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700664 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700665 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700666 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700667 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700668 }
669}
670
philipelccd74892016-09-05 02:46:25 -0700671template <typename T>
672void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
673 PacketDirection desired_direction,
674 Plot* plot,
675 const std::map<StreamId, std::vector<T>>& packets,
676 const std::string& label_prefix) {
677 for (auto& kv : packets) {
678 StreamId stream_id = kv.first;
679 const std::vector<T>& packet_stream = kv.second;
680 // Filter on direction and SSRC.
681 if (stream_id.GetDirection() != desired_direction ||
682 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
683 continue;
684 }
685
terelius23c595a2017-03-15 01:59:12 -0700686 std::string label = label_prefix + " " + GetStreamName(stream_id);
687 TimeSeries time_series(label, LINE_STEP_GRAPH);
philipelccd74892016-09-05 02:46:25 -0700688 for (size_t i = 0; i < packet_stream.size(); i++) {
689 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
690 1000000;
philipelccd74892016-09-05 02:46:25 -0700691 time_series.points.emplace_back(x, i + 1);
692 }
693
philipel35ba9bd2017-04-19 05:58:51 -0700694 plot->AppendTimeSeries(std::move(time_series));
philipelccd74892016-09-05 02:46:25 -0700695 }
696}
697
698void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
699 PacketDirection desired_direction,
700 Plot* plot) {
701 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
702 "RTP");
703 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
704 "RTCP");
705
706 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
707 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
708 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
709 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
710 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
711 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
712 }
713}
714
terelius54ce6802016-07-13 06:44:41 -0700715// For each SSRC, plot the time between the consecutive playouts.
716void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
717 std::map<uint32_t, TimeSeries> time_series;
718 std::map<uint32_t, uint64_t> last_playout;
719
720 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700721
722 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
723 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
724 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
725 parsed_log_.GetAudioPlayout(i, &ssrc);
726 uint64_t timestamp = parsed_log_.GetTimestamp(i);
727 if (MatchingSsrc(ssrc, desired_ssrc_)) {
728 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
729 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
730 if (time_series[ssrc].points.size() == 0) {
731 // There were no previusly logged playout for this SSRC.
732 // Generate a point, but place it on the x-axis.
733 y = 0;
734 }
terelius54ce6802016-07-13 06:44:41 -0700735 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
736 last_playout[ssrc] = timestamp;
737 }
738 }
739 }
740
741 // Set labels and put in graph.
742 for (auto& kv : time_series) {
743 kv.second.label = SsrcToString(kv.first);
744 kv.second.style = BAR_GRAPH;
philipel35ba9bd2017-04-19 05:58:51 -0700745 plot->AppendTimeSeries(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700746 }
747
tereliusdc35dcd2016-08-01 12:03:27 -0700748 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
749 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
750 kTopMargin);
751 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700752}
753
ivocaac9d6f2016-09-22 07:01:47 -0700754// For audio SSRCs, plot the audio level.
755void EventLogAnalyzer::CreateAudioLevelGraph(Plot* plot) {
756 std::map<StreamId, TimeSeries> time_series;
757
758 for (auto& kv : rtp_packets_) {
759 StreamId stream_id = kv.first;
760 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
761 // TODO(ivoc): When audio send/receive configs are stored in the event
762 // log, a check should be added here to only process audio
763 // streams. Tracking bug: webrtc:6399
764 for (auto& packet : packet_stream) {
765 if (packet.header.extension.hasAudioLevel) {
766 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
767 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
768 // Here we convert it to dBov.
769 float y = static_cast<float>(-packet.header.extension.audioLevel);
770 time_series[stream_id].points.emplace_back(TimeSeriesPoint(x, y));
771 }
772 }
773 }
774
775 for (auto& series : time_series) {
776 series.second.label = GetStreamName(series.first);
777 series.second.style = LINE_GRAPH;
philipel35ba9bd2017-04-19 05:58:51 -0700778 plot->AppendTimeSeries(std::move(series.second));
ivocaac9d6f2016-09-22 07:01:47 -0700779 }
780
781 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
ivocbf676632016-11-24 08:30:34 -0800782 plot->SetYAxis(-127, 0, "Audio level (dBov)", kBottomMargin,
ivocaac9d6f2016-09-22 07:01:47 -0700783 kTopMargin);
784 plot->SetTitle("Audio level");
785}
786
terelius54ce6802016-07-13 06:44:41 -0700787// For each SSRC, plot the time between the consecutive playouts.
788void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700789 for (auto& kv : rtp_packets_) {
790 StreamId stream_id = kv.first;
791 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
792 // Filter on direction and SSRC.
793 if (stream_id.GetDirection() != kIncomingPacket ||
794 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
795 continue;
terelius54ce6802016-07-13 06:44:41 -0700796 }
terelius54ce6802016-07-13 06:44:41 -0700797
terelius23c595a2017-03-15 01:59:12 -0700798 TimeSeries time_series(GetStreamName(stream_id), BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700799 ProcessPairs<LoggedRtpPacket, float>(
800 [](const LoggedRtpPacket& old_packet,
801 const LoggedRtpPacket& new_packet) {
802 int64_t diff =
803 WrappingDifference(new_packet.header.sequenceNumber,
804 old_packet.header.sequenceNumber, 1ul << 16);
805 return rtc::Optional<float>(diff);
806 },
807 packet_stream, begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700808 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700809 }
810
tereliusdc35dcd2016-08-01 12:03:27 -0700811 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
812 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
813 kTopMargin);
814 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700815}
816
Stefan Holmer99f8e082016-09-09 13:37:50 +0200817void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
818 for (auto& kv : rtp_packets_) {
819 StreamId stream_id = kv.first;
820 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
821 // Filter on direction and SSRC.
822 if (stream_id.GetDirection() != kIncomingPacket ||
terelius4c9b4af2017-01-30 08:44:51 -0800823 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
824 packet_stream.size() == 0) {
Stefan Holmer99f8e082016-09-09 13:37:50 +0200825 continue;
826 }
827
terelius23c595a2017-03-15 01:59:12 -0700828 TimeSeries time_series(GetStreamName(stream_id), LINE_DOT_GRAPH);
Stefan Holmer99f8e082016-09-09 13:37:50 +0200829 const uint64_t kWindowUs = 1000000;
terelius4c9b4af2017-01-30 08:44:51 -0800830 const uint64_t kStep = 1000000;
831 SequenceNumberUnwrapper unwrapper_;
832 SequenceNumberUnwrapper prior_unwrapper_;
833 size_t window_index_begin = 0;
834 size_t window_index_end = 0;
835 int64_t highest_seq_number =
836 unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
837 int64_t highest_prior_seq_number =
838 prior_unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
839
840 for (uint64_t t = begin_time_; t < end_time_ + kStep; t += kStep) {
841 while (window_index_end < packet_stream.size() &&
842 packet_stream[window_index_end].timestamp < t) {
843 int64_t sequence_number = unwrapper_.Unwrap(
844 packet_stream[window_index_end].header.sequenceNumber);
845 highest_seq_number = std::max(highest_seq_number, sequence_number);
846 ++window_index_end;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200847 }
terelius4c9b4af2017-01-30 08:44:51 -0800848 while (window_index_begin < packet_stream.size() &&
849 packet_stream[window_index_begin].timestamp < t - kWindowUs) {
850 int64_t sequence_number = prior_unwrapper_.Unwrap(
851 packet_stream[window_index_begin].header.sequenceNumber);
852 highest_prior_seq_number =
853 std::max(highest_prior_seq_number, sequence_number);
854 ++window_index_begin;
855 }
856 float x = static_cast<float>(t - begin_time_) / 1000000;
857 int64_t expected_packets = highest_seq_number - highest_prior_seq_number;
858 if (expected_packets > 0) {
859 int64_t received_packets = window_index_end - window_index_begin;
860 int64_t lost_packets = expected_packets - received_packets;
861 float y = static_cast<float>(lost_packets) / expected_packets * 100;
862 time_series.points.emplace_back(x, y);
863 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200864 }
philipel35ba9bd2017-04-19 05:58:51 -0700865 plot->AppendTimeSeries(std::move(time_series));
Stefan Holmer99f8e082016-09-09 13:37:50 +0200866 }
867
868 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
869 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
870 kTopMargin);
871 plot->SetTitle("Estimated incoming loss rate");
872}
873
terelius2ee076d2017-08-15 02:04:02 -0700874void EventLogAnalyzer::CreateIncomingDelayDeltaGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700875 for (auto& kv : rtp_packets_) {
876 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700877 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700878 // Filter on direction and SSRC.
879 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200880 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
881 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
882 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700883 continue;
884 }
terelius54ce6802016-07-13 06:44:41 -0700885
terelius23c595a2017-03-15 01:59:12 -0700886 TimeSeries capture_time_data(GetStreamName(stream_id) + " capture-time",
887 BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700888 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
889 packet_stream, begin_time_,
890 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700891 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700892
terelius23c595a2017-03-15 01:59:12 -0700893 TimeSeries send_time_data(GetStreamName(stream_id) + " abs-send-time",
894 BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700895 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
896 packet_stream, begin_time_,
897 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700898 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700899 }
900
tereliusdc35dcd2016-08-01 12:03:27 -0700901 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
902 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
903 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700904 plot->SetTitle("Network latency difference between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700905}
906
terelius2ee076d2017-08-15 02:04:02 -0700907void EventLogAnalyzer::CreateIncomingDelayGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700908 for (auto& kv : rtp_packets_) {
909 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700910 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700911 // Filter on direction and SSRC.
912 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200913 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
914 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
915 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700916 continue;
917 }
terelius54ce6802016-07-13 06:44:41 -0700918
terelius23c595a2017-03-15 01:59:12 -0700919 TimeSeries capture_time_data(GetStreamName(stream_id) + " capture-time",
920 LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700921 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
922 packet_stream, begin_time_,
923 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700924 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700925
terelius23c595a2017-03-15 01:59:12 -0700926 TimeSeries send_time_data(GetStreamName(stream_id) + " abs-send-time",
927 LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700928 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
929 packet_stream, begin_time_,
930 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700931 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700932 }
933
tereliusdc35dcd2016-08-01 12:03:27 -0700934 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
935 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
936 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700937 plot->SetTitle("Network latency (relative to first packet)");
terelius54ce6802016-07-13 06:44:41 -0700938}
939
tereliusf736d232016-08-04 10:00:11 -0700940// Plot the fraction of packets lost (as perceived by the loss-based BWE).
941void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -0700942 TimeSeries time_series("Fraction lost", LINE_DOT_GRAPH);
tereliusf736d232016-08-04 10:00:11 -0700943 for (auto& bwe_update : bwe_loss_updates_) {
944 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
945 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
philipel35ba9bd2017-04-19 05:58:51 -0700946 time_series.points.emplace_back(x, y);
tereliusf736d232016-08-04 10:00:11 -0700947 }
tereliusf736d232016-08-04 10:00:11 -0700948
Bjorn Terelius19f5be32017-10-18 12:39:49 +0200949 plot->AppendTimeSeries(std::move(time_series));
tereliusf736d232016-08-04 10:00:11 -0700950 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
951 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
952 kTopMargin);
953 plot->SetTitle("Reported packet loss");
954}
955
terelius54ce6802016-07-13 06:44:41 -0700956// Plot the total bandwidth used by all RTP streams.
957void EventLogAnalyzer::CreateTotalBitrateGraph(
958 PacketDirection desired_direction,
philipel23c7f252017-07-14 06:30:03 -0700959 Plot* plot,
960 bool show_detector_state) {
terelius54ce6802016-07-13 06:44:41 -0700961 struct TimestampSize {
962 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
963 uint64_t timestamp;
964 size_t size;
965 };
966 std::vector<TimestampSize> packets;
967
968 PacketDirection direction;
969 size_t total_length;
970
971 // Extract timestamps and sizes for the relevant packets.
972 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
973 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
974 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
Elad Alon1d87b0e2017-10-03 15:01:03 +0200975 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, &total_length,
976 nullptr);
terelius54ce6802016-07-13 06:44:41 -0700977 if (direction == desired_direction) {
978 uint64_t timestamp = parsed_log_.GetTimestamp(i);
979 packets.push_back(TimestampSize(timestamp, total_length));
980 }
981 }
982 }
983
984 size_t window_index_begin = 0;
985 size_t window_index_end = 0;
986 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700987
988 // Calculate a moving average of the bitrate and store in a TimeSeries.
philipel35ba9bd2017-04-19 05:58:51 -0700989 TimeSeries bitrate_series("Bitrate", LINE_GRAPH);
terelius54ce6802016-07-13 06:44:41 -0700990 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
991 while (window_index_end < packets.size() &&
992 packets[window_index_end].timestamp < time) {
993 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700994 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700995 }
996 while (window_index_begin < packets.size() &&
997 packets[window_index_begin].timestamp < time - window_duration_) {
998 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
999 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -07001000 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -07001001 }
1002 float window_duration_in_seconds =
1003 static_cast<float>(window_duration_) / 1000000;
1004 float x = static_cast<float>(time - begin_time_) / 1000000;
1005 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001006 bitrate_series.points.emplace_back(x, y);
terelius54ce6802016-07-13 06:44:41 -07001007 }
philipel35ba9bd2017-04-19 05:58:51 -07001008 plot->AppendTimeSeries(std::move(bitrate_series));
terelius54ce6802016-07-13 06:44:41 -07001009
terelius8058e582016-07-25 01:32:41 -07001010 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
1011 if (desired_direction == kOutgoingPacket) {
philipel35ba9bd2017-04-19 05:58:51 -07001012 TimeSeries loss_series("Loss-based estimate", LINE_STEP_GRAPH);
philipel10fc0e62017-04-11 01:50:23 -07001013 for (auto& loss_update : bwe_loss_updates_) {
terelius8058e582016-07-25 01:32:41 -07001014 float x =
philipel10fc0e62017-04-11 01:50:23 -07001015 static_cast<float>(loss_update.timestamp - begin_time_) / 1000000;
1016 float y = static_cast<float>(loss_update.new_bitrate) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001017 loss_series.points.emplace_back(x, y);
philipel10fc0e62017-04-11 01:50:23 -07001018 }
1019
philipel35ba9bd2017-04-19 05:58:51 -07001020 TimeSeries delay_series("Delay-based estimate", LINE_STEP_GRAPH);
philipel23c7f252017-07-14 06:30:03 -07001021 IntervalSeries overusing_series("Overusing", "#ff8e82",
1022 IntervalSeries::kHorizontal);
1023 IntervalSeries underusing_series("Underusing", "#5092fc",
1024 IntervalSeries::kHorizontal);
1025 IntervalSeries normal_series("Normal", "#c4ffc4",
1026 IntervalSeries::kHorizontal);
1027 IntervalSeries* last_series = &normal_series;
1028 double last_detector_switch = 0.0;
1029
1030 BandwidthUsage last_detector_state = BandwidthUsage::kBwNormal;
1031
philipel10fc0e62017-04-11 01:50:23 -07001032 for (auto& delay_update : bwe_delay_updates_) {
1033 float x =
1034 static_cast<float>(delay_update.timestamp - begin_time_) / 1000000;
1035 float y = static_cast<float>(delay_update.bitrate_bps) / 1000;
philipel23c7f252017-07-14 06:30:03 -07001036
1037 if (last_detector_state != delay_update.detector_state) {
1038 last_series->intervals.emplace_back(last_detector_switch, x);
1039 last_detector_state = delay_update.detector_state;
1040 last_detector_switch = x;
1041
1042 switch (delay_update.detector_state) {
1043 case BandwidthUsage::kBwNormal:
1044 last_series = &normal_series;
1045 break;
1046 case BandwidthUsage::kBwUnderusing:
1047 last_series = &underusing_series;
1048 break;
1049 case BandwidthUsage::kBwOverusing:
1050 last_series = &overusing_series;
1051 break;
Elad Alon1d87b0e2017-10-03 15:01:03 +02001052 case BandwidthUsage::kLast:
1053 RTC_NOTREACHED();
philipel23c7f252017-07-14 06:30:03 -07001054 }
1055 }
1056
philipel35ba9bd2017-04-19 05:58:51 -07001057 delay_series.points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -07001058 }
philipele127e7a2017-03-29 16:28:53 +02001059
philipel23c7f252017-07-14 06:30:03 -07001060 RTC_CHECK(last_series);
1061 last_series->intervals.emplace_back(last_detector_switch, end_time_);
1062
philipel35ba9bd2017-04-19 05:58:51 -07001063 TimeSeries created_series("Probe cluster created.", DOT_GRAPH);
philipele127e7a2017-03-29 16:28:53 +02001064 for (auto& cluster : bwe_probe_cluster_created_events_) {
1065 float x = static_cast<float>(cluster.timestamp - begin_time_) / 1000000;
1066 float y = static_cast<float>(cluster.bitrate_bps) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001067 created_series.points.emplace_back(x, y);
philipele127e7a2017-03-29 16:28:53 +02001068 }
1069
philipel35ba9bd2017-04-19 05:58:51 -07001070 TimeSeries result_series("Probing results.", DOT_GRAPH);
philipele127e7a2017-03-29 16:28:53 +02001071 for (auto& result : bwe_probe_result_events_) {
1072 if (result.bitrate_bps) {
1073 float x = static_cast<float>(result.timestamp - begin_time_) / 1000000;
1074 float y = static_cast<float>(*result.bitrate_bps) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001075 result_series.points.emplace_back(x, y);
philipele127e7a2017-03-29 16:28:53 +02001076 }
1077 }
philipel23c7f252017-07-14 06:30:03 -07001078
1079 if (show_detector_state) {
1080 plot->AppendIntervalSeries(std::move(overusing_series));
1081 plot->AppendIntervalSeries(std::move(underusing_series));
1082 plot->AppendIntervalSeries(std::move(normal_series));
1083 }
1084
philipel35ba9bd2017-04-19 05:58:51 -07001085 plot->AppendTimeSeries(std::move(loss_series));
1086 plot->AppendTimeSeries(std::move(delay_series));
1087 plot->AppendTimeSeries(std::move(created_series));
1088 plot->AppendTimeSeries(std::move(result_series));
terelius8058e582016-07-25 01:32:41 -07001089 }
philipele127e7a2017-03-29 16:28:53 +02001090
terelius2c8e8a32017-06-02 01:29:48 -07001091 // Overlay the incoming REMB over the outgoing bitrate
1092 // and outgoing REMB over incoming bitrate.
1093 PacketDirection remb_direction =
1094 desired_direction == kOutgoingPacket ? kIncomingPacket : kOutgoingPacket;
1095 TimeSeries remb_series("Remb", LINE_STEP_GRAPH);
1096 std::multimap<uint64_t, const LoggedRtcpPacket*> remb_packets;
1097 for (const auto& kv : rtcp_packets_) {
1098 if (kv.first.GetDirection() == remb_direction) {
1099 for (const LoggedRtcpPacket& rtcp_packet : kv.second) {
1100 if (rtcp_packet.type == kRtcpRemb) {
1101 remb_packets.insert(
1102 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1103 }
1104 }
1105 }
1106 }
1107
1108 for (const auto& kv : remb_packets) {
1109 const LoggedRtcpPacket* const rtcp = kv.second;
1110 const rtcp::Remb* const remb = static_cast<rtcp::Remb*>(rtcp->packet.get());
1111 float x = static_cast<float>(rtcp->timestamp - begin_time_) / 1000000;
1112 float y = static_cast<float>(remb->bitrate_bps()) / 1000;
1113 remb_series.points.emplace_back(x, y);
1114 }
1115 plot->AppendTimeSeriesIfNotEmpty(std::move(remb_series));
1116
tereliusdc35dcd2016-08-01 12:03:27 -07001117 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1118 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -07001119 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001120 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001121 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001122 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001123 }
1124}
1125
1126// For each SSRC, plot the bandwidth used by that stream.
1127void EventLogAnalyzer::CreateStreamBitrateGraph(
1128 PacketDirection desired_direction,
1129 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -07001130 for (auto& kv : rtp_packets_) {
1131 StreamId stream_id = kv.first;
1132 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
1133 // Filter on direction and SSRC.
1134 if (stream_id.GetDirection() != desired_direction ||
1135 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
1136 continue;
terelius54ce6802016-07-13 06:44:41 -07001137 }
1138
terelius23c595a2017-03-15 01:59:12 -07001139 TimeSeries time_series(GetStreamName(stream_id), LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001140 MovingAverage<LoggedRtpPacket, double>(
1141 [](const LoggedRtpPacket& packet) {
1142 return rtc::Optional<double>(packet.total_length * 8.0 / 1000.0);
1143 },
1144 packet_stream, begin_time_, end_time_, window_duration_, step_,
1145 &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001146 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -07001147 }
1148
tereliusdc35dcd2016-08-01 12:03:27 -07001149 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1150 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -07001151 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001152 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001153 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001154 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001155 }
1156}
1157
Bjorn Terelius28db2662017-10-04 14:22:43 +02001158void EventLogAnalyzer::CreateSendSideBweSimulationGraph(Plot* plot) {
stefanff421622017-04-20 03:24:01 -07001159 std::multimap<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1160 std::multimap<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
Stefan Holmer13181032016-07-29 14:48:54 +02001161
1162 for (const auto& kv : rtp_packets_) {
1163 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1164 for (const LoggedRtpPacket& rtp_packet : kv.second)
1165 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1166 }
1167 }
1168
1169 for (const auto& kv : rtcp_packets_) {
1170 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1171 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1172 incoming_rtcp.insert(
1173 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1174 }
1175 }
1176
1177 SimulatedClock clock(0);
1178 BitrateObserver observer;
1179 RtcEventLogNullImpl null_event_log;
nisse0245da02016-11-30 03:35:20 -08001180 PacketRouter packet_router;
Stefan Holmer5c8942a2017-08-22 16:16:44 +02001181 PacedSender pacer(&clock, &packet_router, &null_event_log);
1182 SendSideCongestionController cc(&clock, &observer, &null_event_log, &pacer);
Stefan Holmer13181032016-07-29 14:48:54 +02001183 // TODO(holmer): Log the call config and use that here instead.
1184 static const uint32_t kDefaultStartBitrateBps = 300000;
1185 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1186
terelius23c595a2017-03-15 01:59:12 -07001187 TimeSeries time_series("Delay-based estimate", LINE_DOT_GRAPH);
1188 TimeSeries acked_time_series("Acked bitrate", LINE_DOT_GRAPH);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001189 TimeSeries acked_estimate_time_series("Acked bitrate estimate",
1190 LINE_DOT_GRAPH);
Stefan Holmer13181032016-07-29 14:48:54 +02001191
1192 auto rtp_iterator = outgoing_rtp.begin();
1193 auto rtcp_iterator = incoming_rtcp.begin();
1194
1195 auto NextRtpTime = [&]() {
1196 if (rtp_iterator != outgoing_rtp.end())
1197 return static_cast<int64_t>(rtp_iterator->first);
1198 return std::numeric_limits<int64_t>::max();
1199 };
1200
1201 auto NextRtcpTime = [&]() {
1202 if (rtcp_iterator != incoming_rtcp.end())
1203 return static_cast<int64_t>(rtcp_iterator->first);
1204 return std::numeric_limits<int64_t>::max();
1205 };
1206
1207 auto NextProcessTime = [&]() {
1208 if (rtcp_iterator != incoming_rtcp.end() ||
1209 rtp_iterator != outgoing_rtp.end()) {
1210 return clock.TimeInMicroseconds() +
1211 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
1212 }
1213 return std::numeric_limits<int64_t>::max();
1214 };
1215
Stefan Holmer492ee282016-10-27 17:19:20 +02001216 RateStatistics acked_bitrate(250, 8000);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001217#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1218 // The event_log_visualizer should normally not be compiled with
1219 // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE since the normal plots won't work.
1220 // However, compiling with BWE_TEST_LOGGING, runnning with --plot_sendside_bwe
1221 // and piping the output to plot_dynamics.py can be used as a hack to get the
1222 // internal state of various BWE components. In this case, it is important
1223 // we don't instantiate the AcknowledgedBitrateEstimator both here and in
1224 // SendSideCongestionController since that would lead to duplicate outputs.
1225 AcknowledgedBitrateEstimator acknowledged_bitrate_estimator(
1226 rtc::MakeUnique<BitrateEstimator>());
1227#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Stefan Holmer13181032016-07-29 14:48:54 +02001228 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
Stefan Holmer492ee282016-10-27 17:19:20 +02001229 int64_t last_update_us = 0;
Stefan Holmer13181032016-07-29 14:48:54 +02001230 while (time_us != std::numeric_limits<int64_t>::max()) {
1231 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1232 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001233 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001234 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1235 if (rtcp.type == kRtcpTransportFeedback) {
elad.alon5bbf43f2017-03-09 06:40:08 -08001236 cc.OnTransportFeedback(
1237 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
1238 std::vector<PacketFeedback> feedback = cc.GetTransportFeedbackVector();
elad.alonec304f92017-03-08 05:03:53 -08001239 SortPacketFeedbackVector(&feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +02001240 rtc::Optional<uint32_t> bitrate_bps;
1241 if (!feedback.empty()) {
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001242#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1243 acknowledged_bitrate_estimator.IncomingPacketFeedbackVector(feedback);
1244#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
elad.alonf9490002017-03-06 05:32:21 -08001245 for (const PacketFeedback& packet : feedback)
Stefan Holmer60e43462016-09-07 09:58:20 +02001246 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1247 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
1248 }
Stefan Holmer60e43462016-09-07 09:58:20 +02001249 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1250 1000000;
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001251 float y = bitrate_bps.value_or(0) / 1000;
Stefan Holmer60e43462016-09-07 09:58:20 +02001252 acked_time_series.points.emplace_back(x, y);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001253#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1254 y = acknowledged_bitrate_estimator.bitrate_bps().value_or(0) / 1000;
1255 acked_estimate_time_series.points.emplace_back(x, y);
1256#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Stefan Holmer13181032016-07-29 14:48:54 +02001257 }
1258 ++rtcp_iterator;
1259 }
1260 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001261 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001262 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1263 if (rtp.header.extension.hasTransportSequenceNumber) {
1264 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
elad.alond12a8e12017-03-23 11:04:48 -07001265 cc.AddPacket(rtp.header.ssrc,
1266 rtp.header.extension.transportSequenceNumber,
elad.alon5bbf43f2017-03-09 06:40:08 -08001267 rtp.total_length, PacedPacketInfo());
Stefan Holmer13181032016-07-29 14:48:54 +02001268 rtc::SentPacket sent_packet(
1269 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1270 cc.OnSentPacket(sent_packet);
1271 }
1272 ++rtp_iterator;
1273 }
stefanc3de0332016-08-02 07:22:17 -07001274 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1275 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001276 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001277 }
Stefan Holmer492ee282016-10-27 17:19:20 +02001278 if (observer.GetAndResetBitrateUpdated() ||
1279 time_us - last_update_us >= 1e6) {
Stefan Holmer13181032016-07-29 14:48:54 +02001280 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +02001281 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1282 1000000;
1283 time_series.points.emplace_back(x, y);
Stefan Holmer492ee282016-10-27 17:19:20 +02001284 last_update_us = time_us;
Stefan Holmer13181032016-07-29 14:48:54 +02001285 }
1286 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1287 }
1288 // Add the data set to the plot.
philipel35ba9bd2017-04-19 05:58:51 -07001289 plot->AppendTimeSeries(std::move(time_series));
1290 plot->AppendTimeSeries(std::move(acked_time_series));
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001291 plot->AppendTimeSeriesIfNotEmpty(std::move(acked_estimate_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001292
tereliusdc35dcd2016-08-01 12:03:27 -07001293 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1294 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001295 plot->SetTitle("Simulated send-side BWE behavior");
1296}
1297
1298void EventLogAnalyzer::CreateReceiveSideBweSimulationGraph(Plot* plot) {
1299 class RembInterceptingPacketRouter : public PacketRouter {
1300 public:
1301 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
1302 uint32_t bitrate_bps) override {
1303 last_bitrate_bps_ = bitrate_bps;
1304 bitrate_updated_ = true;
1305 PacketRouter::OnReceiveBitrateChanged(ssrcs, bitrate_bps);
1306 }
1307 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
1308 bool GetAndResetBitrateUpdated() {
1309 bool bitrate_updated = bitrate_updated_;
1310 bitrate_updated_ = false;
1311 return bitrate_updated;
1312 }
1313
1314 private:
1315 uint32_t last_bitrate_bps_;
1316 bool bitrate_updated_;
1317 };
1318
1319 std::multimap<uint64_t, const LoggedRtpPacket*> incoming_rtp;
1320
1321 for (const auto& kv : rtp_packets_) {
1322 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket &&
1323 IsVideoSsrc(kv.first)) {
1324 for (const LoggedRtpPacket& rtp_packet : kv.second)
1325 incoming_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1326 }
1327 }
1328
1329 SimulatedClock clock(0);
1330 RembInterceptingPacketRouter packet_router;
1331 // TODO(terelius): The PacketRrouter is the used as the RemoteBitrateObserver.
1332 // Is this intentional?
1333 ReceiveSideCongestionController rscc(&clock, &packet_router);
1334 // TODO(holmer): Log the call config and use that here instead.
1335 // static const uint32_t kDefaultStartBitrateBps = 300000;
1336 // rscc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1337
1338 TimeSeries time_series("Receive side estimate", LINE_DOT_GRAPH);
1339 TimeSeries acked_time_series("Received bitrate", LINE_GRAPH);
1340
1341 RateStatistics acked_bitrate(250, 8000);
1342 int64_t last_update_us = 0;
1343 for (const auto& kv : incoming_rtp) {
1344 const LoggedRtpPacket& packet = *kv.second;
1345 int64_t arrival_time_ms = packet.timestamp / 1000;
1346 size_t payload = packet.total_length; /*Should subtract header?*/
1347 clock.AdvanceTimeMicroseconds(packet.timestamp -
1348 clock.TimeInMicroseconds());
1349 rscc.OnReceivedPacket(arrival_time_ms, payload, packet.header);
1350 acked_bitrate.Update(payload, arrival_time_ms);
1351 rtc::Optional<uint32_t> bitrate_bps = acked_bitrate.Rate(arrival_time_ms);
1352 if (bitrate_bps) {
1353 uint32_t y = *bitrate_bps / 1000;
1354 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1355 1000000;
1356 acked_time_series.points.emplace_back(x, y);
1357 }
1358 if (packet_router.GetAndResetBitrateUpdated() ||
1359 clock.TimeInMicroseconds() - last_update_us >= 1e6) {
1360 uint32_t y = packet_router.last_bitrate_bps() / 1000;
1361 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1362 1000000;
1363 time_series.points.emplace_back(x, y);
1364 last_update_us = clock.TimeInMicroseconds();
1365 }
1366 }
1367 // Add the data set to the plot.
1368 plot->AppendTimeSeries(std::move(time_series));
1369 plot->AppendTimeSeries(std::move(acked_time_series));
1370
1371 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1372 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1373 plot->SetTitle("Simulated receive-side BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001374}
1375
tereliuse34c19c2016-08-15 08:47:14 -07001376void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanff421622017-04-20 03:24:01 -07001377 std::multimap<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1378 std::multimap<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
stefanc3de0332016-08-02 07:22:17 -07001379
1380 for (const auto& kv : rtp_packets_) {
1381 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1382 for (const LoggedRtpPacket& rtp_packet : kv.second)
1383 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1384 }
1385 }
1386
1387 for (const auto& kv : rtcp_packets_) {
1388 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1389 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1390 incoming_rtcp.insert(
1391 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1392 }
1393 }
1394
1395 SimulatedClock clock(0);
elad.alon5bbf43f2017-03-09 06:40:08 -08001396 TransportFeedbackAdapter feedback_adapter(&clock);
stefanc3de0332016-08-02 07:22:17 -07001397
stefana0a8ed72017-09-06 02:06:32 -07001398 TimeSeries late_feedback_series("Late feedback results.", DOT_GRAPH);
terelius23c595a2017-03-15 01:59:12 -07001399 TimeSeries time_series("Network Delay Change", LINE_DOT_GRAPH);
stefanc3de0332016-08-02 07:22:17 -07001400 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1401
1402 auto rtp_iterator = outgoing_rtp.begin();
1403 auto rtcp_iterator = incoming_rtcp.begin();
1404
1405 auto NextRtpTime = [&]() {
1406 if (rtp_iterator != outgoing_rtp.end())
1407 return static_cast<int64_t>(rtp_iterator->first);
1408 return std::numeric_limits<int64_t>::max();
1409 };
1410
1411 auto NextRtcpTime = [&]() {
1412 if (rtcp_iterator != incoming_rtcp.end())
1413 return static_cast<int64_t>(rtcp_iterator->first);
1414 return std::numeric_limits<int64_t>::max();
1415 };
1416
1417 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
stefana0a8ed72017-09-06 02:06:32 -07001418 int64_t prev_y = 0;
stefanc3de0332016-08-02 07:22:17 -07001419 while (time_us != std::numeric_limits<int64_t>::max()) {
1420 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1421 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1422 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1423 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1424 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001425 feedback_adapter.OnTransportFeedback(
1426 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
elad.alonf9490002017-03-06 05:32:21 -08001427 std::vector<PacketFeedback> feedback =
1428 feedback_adapter.GetTransportFeedbackVector();
elad.alonec304f92017-03-08 05:03:53 -08001429 SortPacketFeedbackVector(&feedback);
elad.alonf9490002017-03-06 05:32:21 -08001430 for (const PacketFeedback& packet : feedback) {
stefanc3de0332016-08-02 07:22:17 -07001431 float x =
1432 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1433 1000000;
stefana0a8ed72017-09-06 02:06:32 -07001434 if (packet.send_time_ms == -1) {
1435 late_feedback_series.points.emplace_back(x, prev_y);
1436 continue;
1437 }
1438 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1439 prev_y = y;
stefanc3de0332016-08-02 07:22:17 -07001440 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1441 time_series.points.emplace_back(x, y);
1442 }
1443 }
1444 ++rtcp_iterator;
1445 }
1446 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1447 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1448 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1449 if (rtp.header.extension.hasTransportSequenceNumber) {
1450 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
elad.alond12a8e12017-03-23 11:04:48 -07001451 feedback_adapter.AddPacket(rtp.header.ssrc,
1452 rtp.header.extension.transportSequenceNumber,
philipel8aadd502017-02-23 02:56:13 -08001453 rtp.total_length, PacedPacketInfo());
stefanc3de0332016-08-02 07:22:17 -07001454 feedback_adapter.OnSentPacket(
1455 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1456 }
1457 ++rtp_iterator;
1458 }
1459 time_us = std::min(NextRtpTime(), NextRtcpTime());
1460 }
1461 // We assume that the base network delay (w/o queues) is the min delay
1462 // observed during the call.
1463 for (TimeSeriesPoint& point : time_series.points)
1464 point.y -= estimated_base_delay_ms;
stefana0a8ed72017-09-06 02:06:32 -07001465 for (TimeSeriesPoint& point : late_feedback_series.points)
1466 point.y -= estimated_base_delay_ms;
stefanc3de0332016-08-02 07:22:17 -07001467 // Add the data set to the plot.
stefana0a8ed72017-09-06 02:06:32 -07001468 plot->AppendTimeSeriesIfNotEmpty(std::move(time_series));
1469 plot->AppendTimeSeriesIfNotEmpty(std::move(late_feedback_series));
stefanc3de0332016-08-02 07:22:17 -07001470
1471 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1472 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1473 plot->SetTitle("Network Delay Change.");
1474}
stefan08383272016-12-20 08:51:52 -08001475
1476std::vector<std::pair<int64_t, int64_t>> EventLogAnalyzer::GetFrameTimestamps()
1477 const {
1478 std::vector<std::pair<int64_t, int64_t>> timestamps;
1479 size_t largest_stream_size = 0;
1480 const std::vector<LoggedRtpPacket>* largest_video_stream = nullptr;
1481 // Find the incoming video stream with the most number of packets that is
1482 // not rtx.
1483 for (const auto& kv : rtp_packets_) {
1484 if (kv.first.GetDirection() == kIncomingPacket &&
1485 video_ssrcs_.find(kv.first) != video_ssrcs_.end() &&
1486 rtx_ssrcs_.find(kv.first) == rtx_ssrcs_.end() &&
1487 kv.second.size() > largest_stream_size) {
1488 largest_stream_size = kv.second.size();
1489 largest_video_stream = &kv.second;
1490 }
1491 }
1492 if (largest_video_stream == nullptr) {
1493 for (auto& packet : *largest_video_stream) {
1494 if (packet.header.markerBit) {
1495 int64_t capture_ms = packet.header.timestamp / 90.0;
1496 int64_t arrival_ms = packet.timestamp / 1000.0;
1497 timestamps.push_back(std::make_pair(capture_ms, arrival_ms));
1498 }
1499 }
1500 }
1501 return timestamps;
1502}
stefane372d3c2017-02-02 08:04:18 -08001503
Bjorn Terelius0295a962017-10-25 17:42:41 +02001504void EventLogAnalyzer::CreatePacerDelayGraph(Plot* plot) {
1505 for (const auto& kv : rtp_packets_) {
1506 const std::vector<LoggedRtpPacket>& packets = kv.second;
1507 StreamId stream_id = kv.first;
1508
1509 if (packets.size() < 2) {
1510 LOG(LS_WARNING) << "Can't estimate a the RTP clock frequency or the "
1511 "pacer delay with less than 2 packets in the stream";
1512 continue;
1513 }
1514 rtc::Optional<uint32_t> estimated_frequency =
1515 EstimateRtpClockFrequency(packets);
1516 if (!estimated_frequency)
1517 continue;
1518 if (IsVideoSsrc(stream_id) && *estimated_frequency != 90000) {
1519 LOG(LS_WARNING)
1520 << "Video stream should use a 90 kHz clock but appears to use "
1521 << *estimated_frequency / 1000 << ". Discarding.";
1522 continue;
1523 }
1524
1525 TimeSeries pacer_delay_series(
1526 GetStreamName(stream_id) + "(" +
1527 std::to_string(*estimated_frequency / 1000) + " kHz)",
1528 LINE_DOT_GRAPH);
1529 SeqNumUnwrapper<uint32_t> timestamp_unwrapper;
1530 uint64_t first_capture_timestamp =
1531 timestamp_unwrapper.Unwrap(packets.front().header.timestamp);
1532 uint64_t first_send_timestamp = packets.front().timestamp;
1533 for (LoggedRtpPacket packet : packets) {
1534 double capture_time_ms = (static_cast<double>(timestamp_unwrapper.Unwrap(
1535 packet.header.timestamp)) -
1536 first_capture_timestamp) /
1537 *estimated_frequency * 1000;
1538 double send_time_ms =
1539 static_cast<double>(packet.timestamp - first_send_timestamp) / 1000;
1540 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
1541 float y = send_time_ms - capture_time_ms;
1542 pacer_delay_series.points.emplace_back(x, y);
1543 }
1544 plot->AppendTimeSeries(std::move(pacer_delay_series));
1545 }
1546
1547 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1548 plot->SetSuggestedYAxis(0, 10, "Pacer delay (ms)", kBottomMargin, kTopMargin);
1549 plot->SetTitle(
1550 "Delay from capture to send time. (First packet normalized to 0.)");
1551}
1552
stefane372d3c2017-02-02 08:04:18 -08001553void EventLogAnalyzer::CreateTimestampGraph(Plot* plot) {
1554 for (const auto& kv : rtp_packets_) {
1555 const std::vector<LoggedRtpPacket>& rtp_packets = kv.second;
1556 StreamId stream_id = kv.first;
1557
1558 {
terelius23c595a2017-03-15 01:59:12 -07001559 TimeSeries timestamp_data(GetStreamName(stream_id) + " capture-time",
1560 LINE_DOT_GRAPH);
stefane372d3c2017-02-02 08:04:18 -08001561 for (LoggedRtpPacket packet : rtp_packets) {
1562 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
1563 float y = packet.header.timestamp;
1564 timestamp_data.points.emplace_back(x, y);
1565 }
philipel35ba9bd2017-04-19 05:58:51 -07001566 plot->AppendTimeSeries(std::move(timestamp_data));
stefane372d3c2017-02-02 08:04:18 -08001567 }
1568
1569 {
1570 auto kv = rtcp_packets_.find(stream_id);
1571 if (kv != rtcp_packets_.end()) {
1572 const auto& packets = kv->second;
terelius23c595a2017-03-15 01:59:12 -07001573 TimeSeries timestamp_data(
1574 GetStreamName(stream_id) + " rtcp capture-time", LINE_DOT_GRAPH);
stefane372d3c2017-02-02 08:04:18 -08001575 for (const LoggedRtcpPacket& rtcp : packets) {
1576 if (rtcp.type != kRtcpSr)
1577 continue;
1578 rtcp::SenderReport* sr;
1579 sr = static_cast<rtcp::SenderReport*>(rtcp.packet.get());
1580 float x = static_cast<float>(rtcp.timestamp - begin_time_) / 1000000;
1581 float y = sr->rtp_timestamp();
1582 timestamp_data.points.emplace_back(x, y);
1583 }
philipel35ba9bd2017-04-19 05:58:51 -07001584 plot->AppendTimeSeries(std::move(timestamp_data));
stefane372d3c2017-02-02 08:04:18 -08001585 }
1586 }
1587 }
1588
1589 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1590 plot->SetSuggestedYAxis(0, 1, "Timestamp (90khz)", kBottomMargin, kTopMargin);
1591 plot->SetTitle("Timestamps");
1592}
michaelt6e5b2192017-02-22 07:33:27 -08001593
1594void EventLogAnalyzer::CreateAudioEncoderTargetBitrateGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001595 TimeSeries time_series("Audio encoder target bitrate", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001596 ProcessPoints<AudioNetworkAdaptationEvent>(
1597 [](const AudioNetworkAdaptationEvent& ana_event) -> rtc::Optional<float> {
michaelt6e5b2192017-02-22 07:33:27 -08001598 if (ana_event.config.bitrate_bps)
1599 return rtc::Optional<float>(
1600 static_cast<float>(*ana_event.config.bitrate_bps));
1601 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001602 },
philipel35ba9bd2017-04-19 05:58:51 -07001603 audio_network_adaptation_events_, begin_time_, &time_series);
1604 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001605 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1606 plot->SetSuggestedYAxis(0, 1, "Bitrate (bps)", kBottomMargin, kTopMargin);
1607 plot->SetTitle("Reported audio encoder target bitrate");
1608}
1609
1610void EventLogAnalyzer::CreateAudioEncoderFrameLengthGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001611 TimeSeries time_series("Audio encoder frame length", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001612 ProcessPoints<AudioNetworkAdaptationEvent>(
1613 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001614 if (ana_event.config.frame_length_ms)
1615 return rtc::Optional<float>(
1616 static_cast<float>(*ana_event.config.frame_length_ms));
1617 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001618 },
philipel35ba9bd2017-04-19 05:58:51 -07001619 audio_network_adaptation_events_, begin_time_, &time_series);
1620 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001621 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1622 plot->SetSuggestedYAxis(0, 1, "Frame length (ms)", kBottomMargin, kTopMargin);
1623 plot->SetTitle("Reported audio encoder frame length");
1624}
1625
terelius2ee076d2017-08-15 02:04:02 -07001626void EventLogAnalyzer::CreateAudioEncoderPacketLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001627 TimeSeries time_series("Audio encoder uplink packet loss fraction",
1628 LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001629 ProcessPoints<AudioNetworkAdaptationEvent>(
1630 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001631 if (ana_event.config.uplink_packet_loss_fraction)
1632 return rtc::Optional<float>(static_cast<float>(
1633 *ana_event.config.uplink_packet_loss_fraction));
1634 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001635 },
philipel35ba9bd2017-04-19 05:58:51 -07001636 audio_network_adaptation_events_, begin_time_, &time_series);
1637 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001638 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1639 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
1640 kTopMargin);
1641 plot->SetTitle("Reported audio encoder lost packets");
1642}
1643
1644void EventLogAnalyzer::CreateAudioEncoderEnableFecGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001645 TimeSeries time_series("Audio encoder FEC", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001646 ProcessPoints<AudioNetworkAdaptationEvent>(
1647 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001648 if (ana_event.config.enable_fec)
1649 return rtc::Optional<float>(
1650 static_cast<float>(*ana_event.config.enable_fec));
1651 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001652 },
philipel35ba9bd2017-04-19 05:58:51 -07001653 audio_network_adaptation_events_, begin_time_, &time_series);
1654 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001655 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1656 plot->SetSuggestedYAxis(0, 1, "FEC (false/true)", kBottomMargin, kTopMargin);
1657 plot->SetTitle("Reported audio encoder FEC");
1658}
1659
1660void EventLogAnalyzer::CreateAudioEncoderEnableDtxGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001661 TimeSeries time_series("Audio encoder DTX", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001662 ProcessPoints<AudioNetworkAdaptationEvent>(
1663 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001664 if (ana_event.config.enable_dtx)
1665 return rtc::Optional<float>(
1666 static_cast<float>(*ana_event.config.enable_dtx));
1667 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001668 },
philipel35ba9bd2017-04-19 05:58:51 -07001669 audio_network_adaptation_events_, begin_time_, &time_series);
1670 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001671 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1672 plot->SetSuggestedYAxis(0, 1, "DTX (false/true)", kBottomMargin, kTopMargin);
1673 plot->SetTitle("Reported audio encoder DTX");
1674}
1675
1676void EventLogAnalyzer::CreateAudioEncoderNumChannelsGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001677 TimeSeries time_series("Audio encoder number of channels", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001678 ProcessPoints<AudioNetworkAdaptationEvent>(
1679 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001680 if (ana_event.config.num_channels)
1681 return rtc::Optional<float>(
1682 static_cast<float>(*ana_event.config.num_channels));
1683 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001684 },
philipel35ba9bd2017-04-19 05:58:51 -07001685 audio_network_adaptation_events_, begin_time_, &time_series);
1686 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001687 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1688 plot->SetSuggestedYAxis(0, 1, "Number of channels (1 (mono)/2 (stereo))",
1689 kBottomMargin, kTopMargin);
1690 plot->SetTitle("Reported audio encoder number of channels");
1691}
henrik.lundin3c938fc2017-06-14 06:09:58 -07001692
1693class NetEqStreamInput : public test::NetEqInput {
1694 public:
1695 // Does not take any ownership, and all pointers must refer to valid objects
1696 // that outlive the one constructed.
1697 NetEqStreamInput(const std::vector<LoggedRtpPacket>* packet_stream,
1698 const std::vector<uint64_t>* output_events_us,
1699 rtc::Optional<uint64_t> end_time_us)
1700 : packet_stream_(*packet_stream),
1701 packet_stream_it_(packet_stream_.begin()),
1702 output_events_us_it_(output_events_us->begin()),
1703 output_events_us_end_(output_events_us->end()),
1704 end_time_us_(end_time_us) {
1705 RTC_DCHECK(packet_stream);
1706 RTC_DCHECK(output_events_us);
1707 }
1708
1709 rtc::Optional<int64_t> NextPacketTime() const override {
1710 if (packet_stream_it_ == packet_stream_.end()) {
1711 return rtc::Optional<int64_t>();
1712 }
1713 if (end_time_us_ && packet_stream_it_->timestamp > *end_time_us_) {
1714 return rtc::Optional<int64_t>();
1715 }
1716 // Convert from us to ms.
1717 return rtc::Optional<int64_t>(packet_stream_it_->timestamp / 1000);
1718 }
1719
1720 rtc::Optional<int64_t> NextOutputEventTime() const override {
1721 if (output_events_us_it_ == output_events_us_end_) {
1722 return rtc::Optional<int64_t>();
1723 }
1724 if (end_time_us_ && *output_events_us_it_ > *end_time_us_) {
1725 return rtc::Optional<int64_t>();
1726 }
1727 // Convert from us to ms.
1728 return rtc::Optional<int64_t>(
1729 rtc::checked_cast<int64_t>(*output_events_us_it_ / 1000));
1730 }
1731
1732 std::unique_ptr<PacketData> PopPacket() override {
1733 if (packet_stream_it_ == packet_stream_.end()) {
1734 return std::unique_ptr<PacketData>();
1735 }
1736 std::unique_ptr<PacketData> packet_data(new PacketData());
1737 packet_data->header = packet_stream_it_->header;
1738 // Convert from us to ms.
1739 packet_data->time_ms = packet_stream_it_->timestamp / 1000.0;
1740
1741 // This is a header-only "dummy" packet. Set the payload to all zeros, with
1742 // length according to the virtual length.
1743 packet_data->payload.SetSize(packet_stream_it_->total_length);
1744 std::fill_n(packet_data->payload.data(), packet_data->payload.size(), 0);
1745
1746 ++packet_stream_it_;
1747 return packet_data;
1748 }
1749
1750 void AdvanceOutputEvent() override {
1751 if (output_events_us_it_ != output_events_us_end_) {
1752 ++output_events_us_it_;
1753 }
1754 }
1755
1756 bool ended() const override { return !NextEventTime(); }
1757
1758 rtc::Optional<RTPHeader> NextHeader() const override {
1759 if (packet_stream_it_ == packet_stream_.end()) {
1760 return rtc::Optional<RTPHeader>();
1761 }
1762 return rtc::Optional<RTPHeader>(packet_stream_it_->header);
1763 }
1764
1765 private:
1766 const std::vector<LoggedRtpPacket>& packet_stream_;
1767 std::vector<LoggedRtpPacket>::const_iterator packet_stream_it_;
1768 std::vector<uint64_t>::const_iterator output_events_us_it_;
1769 const std::vector<uint64_t>::const_iterator output_events_us_end_;
1770 const rtc::Optional<uint64_t> end_time_us_;
1771};
1772
1773namespace {
1774// Creates a NetEq test object and all necessary input and output helpers. Runs
1775// the test and returns the NetEqDelayAnalyzer object that was used to
1776// instrument the test.
1777std::unique_ptr<test::NetEqDelayAnalyzer> CreateNetEqTestAndRun(
1778 const std::vector<LoggedRtpPacket>* packet_stream,
1779 const std::vector<uint64_t>* output_events_us,
1780 rtc::Optional<uint64_t> end_time_us,
1781 const std::string& replacement_file_name,
1782 int file_sample_rate_hz) {
1783 std::unique_ptr<test::NetEqInput> input(
1784 new NetEqStreamInput(packet_stream, output_events_us, end_time_us));
1785
1786 constexpr int kReplacementPt = 127;
1787 std::set<uint8_t> cn_types;
1788 std::set<uint8_t> forbidden_types;
1789 input.reset(new test::NetEqReplacementInput(std::move(input), kReplacementPt,
1790 cn_types, forbidden_types));
1791
1792 NetEq::Config config;
1793 config.max_packets_in_buffer = 200;
1794 config.enable_fast_accelerate = true;
1795
1796 std::unique_ptr<test::VoidAudioSink> output(new test::VoidAudioSink());
1797
1798 test::NetEqTest::DecoderMap codecs;
1799
1800 // Create a "replacement decoder" that produces the decoded audio by reading
1801 // from a file rather than from the encoded payloads.
1802 std::unique_ptr<test::ResampleInputAudioFile> replacement_file(
1803 new test::ResampleInputAudioFile(replacement_file_name,
1804 file_sample_rate_hz));
1805 replacement_file->set_output_rate_hz(48000);
1806 std::unique_ptr<AudioDecoder> replacement_decoder(
1807 new test::FakeDecodeFromFile(std::move(replacement_file), 48000, false));
1808 test::NetEqTest::ExtDecoderMap ext_codecs;
1809 ext_codecs[kReplacementPt] = {replacement_decoder.get(),
1810 NetEqDecoder::kDecoderArbitrary,
1811 "replacement codec"};
1812
1813 std::unique_ptr<test::NetEqDelayAnalyzer> delay_cb(
1814 new test::NetEqDelayAnalyzer);
1815 test::DefaultNetEqTestErrorCallback error_cb;
1816 test::NetEqTest::Callbacks callbacks;
1817 callbacks.error_callback = &error_cb;
1818 callbacks.post_insert_packet = delay_cb.get();
1819 callbacks.get_audio_callback = delay_cb.get();
1820
1821 test::NetEqTest test(config, codecs, ext_codecs, std::move(input),
1822 std::move(output), callbacks);
1823 test.Run();
1824 return delay_cb;
1825}
1826} // namespace
1827
1828// Plots the jitter buffer delay profile. This will plot only for the first
1829// incoming audio SSRC. If the stream contains more than one incoming audio
1830// SSRC, all but the first will be ignored.
1831void EventLogAnalyzer::CreateAudioJitterBufferGraph(
1832 const std::string& replacement_file_name,
1833 int file_sample_rate_hz,
1834 Plot* plot) {
1835 const auto& incoming_audio_kv = std::find_if(
1836 rtp_packets_.begin(), rtp_packets_.end(),
1837 [this](std::pair<StreamId, std::vector<LoggedRtpPacket>> kv) {
1838 return kv.first.GetDirection() == kIncomingPacket &&
1839 this->IsAudioSsrc(kv.first);
1840 });
1841 if (incoming_audio_kv == rtp_packets_.end()) {
1842 // No incoming audio stream found.
1843 return;
1844 }
1845
1846 const uint32_t ssrc = incoming_audio_kv->first.GetSsrc();
1847
1848 std::map<uint32_t, std::vector<uint64_t>>::const_iterator output_events_it =
1849 audio_playout_events_.find(ssrc);
1850 if (output_events_it == audio_playout_events_.end()) {
1851 // Could not find output events with SSRC matching the input audio stream.
1852 // Using the first available stream of output events.
1853 output_events_it = audio_playout_events_.cbegin();
1854 }
1855
1856 rtc::Optional<uint64_t> end_time_us =
1857 log_segments_.empty()
1858 ? rtc::Optional<uint64_t>()
1859 : rtc::Optional<uint64_t>(log_segments_.front().second);
1860
1861 auto delay_cb = CreateNetEqTestAndRun(
1862 &incoming_audio_kv->second, &output_events_it->second, end_time_us,
1863 replacement_file_name, file_sample_rate_hz);
1864
1865 std::vector<float> send_times_s;
1866 std::vector<float> arrival_delay_ms;
1867 std::vector<float> corrected_arrival_delay_ms;
1868 std::vector<rtc::Optional<float>> playout_delay_ms;
1869 std::vector<rtc::Optional<float>> target_delay_ms;
1870 delay_cb->CreateGraphs(&send_times_s, &arrival_delay_ms,
1871 &corrected_arrival_delay_ms, &playout_delay_ms,
1872 &target_delay_ms);
1873 RTC_DCHECK_EQ(send_times_s.size(), arrival_delay_ms.size());
1874 RTC_DCHECK_EQ(send_times_s.size(), corrected_arrival_delay_ms.size());
1875 RTC_DCHECK_EQ(send_times_s.size(), playout_delay_ms.size());
1876 RTC_DCHECK_EQ(send_times_s.size(), target_delay_ms.size());
1877
1878 std::map<StreamId, TimeSeries> time_series_packet_arrival;
1879 std::map<StreamId, TimeSeries> time_series_relative_packet_arrival;
1880 std::map<StreamId, TimeSeries> time_series_play_time;
1881 std::map<StreamId, TimeSeries> time_series_target_time;
1882 float min_y_axis = 0.f;
1883 float max_y_axis = 0.f;
1884 const StreamId stream_id = incoming_audio_kv->first;
1885 for (size_t i = 0; i < send_times_s.size(); ++i) {
1886 time_series_packet_arrival[stream_id].points.emplace_back(
1887 TimeSeriesPoint(send_times_s[i], arrival_delay_ms[i]));
1888 time_series_relative_packet_arrival[stream_id].points.emplace_back(
1889 TimeSeriesPoint(send_times_s[i], corrected_arrival_delay_ms[i]));
1890 min_y_axis = std::min(min_y_axis, corrected_arrival_delay_ms[i]);
1891 max_y_axis = std::max(max_y_axis, corrected_arrival_delay_ms[i]);
1892 if (playout_delay_ms[i]) {
1893 time_series_play_time[stream_id].points.emplace_back(
1894 TimeSeriesPoint(send_times_s[i], *playout_delay_ms[i]));
1895 min_y_axis = std::min(min_y_axis, *playout_delay_ms[i]);
1896 max_y_axis = std::max(max_y_axis, *playout_delay_ms[i]);
1897 }
1898 if (target_delay_ms[i]) {
1899 time_series_target_time[stream_id].points.emplace_back(
1900 TimeSeriesPoint(send_times_s[i], *target_delay_ms[i]));
1901 min_y_axis = std::min(min_y_axis, *target_delay_ms[i]);
1902 max_y_axis = std::max(max_y_axis, *target_delay_ms[i]);
1903 }
1904 }
1905
1906 // This code is adapted for a single stream. The creation of the streams above
1907 // guarantee that no more than one steam is included. If multiple streams are
1908 // to be plotted, they should likely be given distinct labels below.
1909 RTC_DCHECK_EQ(time_series_relative_packet_arrival.size(), 1);
1910 for (auto& series : time_series_relative_packet_arrival) {
1911 series.second.label = "Relative packet arrival delay";
1912 series.second.style = LINE_GRAPH;
1913 plot->AppendTimeSeries(std::move(series.second));
1914 }
1915 RTC_DCHECK_EQ(time_series_play_time.size(), 1);
1916 for (auto& series : time_series_play_time) {
1917 series.second.label = "Playout delay";
1918 series.second.style = LINE_GRAPH;
1919 plot->AppendTimeSeries(std::move(series.second));
1920 }
1921 RTC_DCHECK_EQ(time_series_target_time.size(), 1);
1922 for (auto& series : time_series_target_time) {
1923 series.second.label = "Target delay";
1924 series.second.style = LINE_DOT_GRAPH;
1925 plot->AppendTimeSeries(std::move(series.second));
1926 }
1927
1928 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1929 plot->SetYAxis(min_y_axis, max_y_axis, "Relative delay (ms)", kBottomMargin,
1930 kTopMargin);
1931 plot->SetTitle("NetEq timing");
1932}
terelius54ce6802016-07-13 06:44:41 -07001933} // namespace plotting
1934} // namespace webrtc