blob: 7043c68f41d22ea4b2e612f2e9a0fc19dd9ed9a7 [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"
38#include "modules/rtp_rtcp/include/rtp_rtcp.h"
39#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
40#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
41#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
42#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
43#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
44#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
45#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
46#include "modules/rtp_rtcp/source/rtp_utility.h"
47#include "rtc_base/checks.h"
48#include "rtc_base/format_macros.h"
49#include "rtc_base/logging.h"
50#include "rtc_base/ptr_util.h"
51#include "rtc_base/rate_statistics.h"
terelius54ce6802016-07-13 06:44:41 -070052
Bjorn Terelius6984ad22017-10-24 12:19:45 +020053#ifndef BWE_TEST_LOGGING_COMPILE_TIME_ENABLE
54#define BWE_TEST_LOGGING_COMPILE_TIME_ENABLE 0
55#endif // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE
56
tereliusdc35dcd2016-08-01 12:03:27 -070057namespace webrtc {
58namespace plotting {
59
terelius54ce6802016-07-13 06:44:41 -070060namespace {
61
elad.alonec304f92017-03-08 05:03:53 -080062void SortPacketFeedbackVector(std::vector<PacketFeedback>* vec) {
63 auto pred = [](const PacketFeedback& packet_feedback) {
64 return packet_feedback.arrival_time_ms == PacketFeedback::kNotReceived;
65 };
66 vec->erase(std::remove_if(vec->begin(), vec->end(), pred), vec->end());
67 std::sort(vec->begin(), vec->end(), PacketFeedbackComparator());
68}
69
terelius54ce6802016-07-13 06:44:41 -070070std::string SsrcToString(uint32_t ssrc) {
71 std::stringstream ss;
72 ss << "SSRC " << ssrc;
73 return ss.str();
74}
75
76// Checks whether an SSRC is contained in the list of desired SSRCs.
77// Note that an empty SSRC list matches every SSRC.
78bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
79 if (desired_ssrc.size() == 0)
80 return true;
81 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
82 desired_ssrc.end();
83}
84
85double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
86 // The timestamp is a fixed point representation with 6 bits for seconds
87 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
88 // time in seconds and then multiply by 1000000 to convert to microseconds.
89 static constexpr double kTimestampToMicroSec =
tereliusccbbf8d2016-08-10 07:34:28 -070090 1000000.0 / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -070091 return abs_send_time * kTimestampToMicroSec;
92}
93
94// Computes the difference |later| - |earlier| where |later| and |earlier|
95// are counters that wrap at |modulus|. The difference is chosen to have the
96// least absolute value. For example if |modulus| is 8, then the difference will
97// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
98// be in [-4, 4].
99int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
100 RTC_DCHECK_LE(1, modulus);
101 RTC_DCHECK_LT(later, modulus);
102 RTC_DCHECK_LT(earlier, modulus);
103 int64_t difference =
104 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
105 int64_t max_difference = modulus / 2;
106 int64_t min_difference = max_difference - modulus + 1;
107 if (difference > max_difference) {
108 difference -= modulus;
109 }
110 if (difference < min_difference) {
111 difference += modulus;
112 }
terelius6addf492016-08-23 17:34:07 -0700113 if (difference > max_difference / 2 || difference < min_difference / 2) {
114 LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
115 << " expected to be in the range (" << min_difference / 2
116 << "," << max_difference / 2 << ") but is " << difference
117 << ". Correct unwrapping is uncertain.";
118 }
terelius54ce6802016-07-13 06:44:41 -0700119 return difference;
120}
121
ivocaac9d6f2016-09-22 07:01:47 -0700122// Return default values for header extensions, to use on streams without stored
123// mapping data. Currently this only applies to audio streams, since the mapping
124// is not stored in the event log.
125// TODO(ivoc): Remove this once this mapping is stored in the event log for
126// audio streams. Tracking bug: webrtc:6399
127webrtc::RtpHeaderExtensionMap GetDefaultHeaderExtensionMap() {
128 webrtc::RtpHeaderExtensionMap default_map;
danilchap4aecc582016-11-15 09:21:00 -0800129 default_map.Register<AudioLevel>(webrtc::RtpExtension::kAudioLevelDefaultId);
terelius007d5622017-08-08 05:40:26 -0700130 default_map.Register<TransmissionOffset>(
131 webrtc::RtpExtension::kTimestampOffsetDefaultId);
danilchap4aecc582016-11-15 09:21:00 -0800132 default_map.Register<AbsoluteSendTime>(
ivocaac9d6f2016-09-22 07:01:47 -0700133 webrtc::RtpExtension::kAbsSendTimeDefaultId);
terelius007d5622017-08-08 05:40:26 -0700134 default_map.Register<VideoOrientation>(
135 webrtc::RtpExtension::kVideoRotationDefaultId);
136 default_map.Register<VideoContentTypeExtension>(
137 webrtc::RtpExtension::kVideoContentTypeDefaultId);
138 default_map.Register<VideoTimingExtension>(
139 webrtc::RtpExtension::kVideoTimingDefaultId);
140 default_map.Register<TransportSequenceNumber>(
141 webrtc::RtpExtension::kTransportSequenceNumberDefaultId);
142 default_map.Register<PlayoutDelayLimits>(
143 webrtc::RtpExtension::kPlayoutDelayDefaultId);
ivocaac9d6f2016-09-22 07:01:47 -0700144 return default_map;
145}
146
tereliusdc35dcd2016-08-01 12:03:27 -0700147constexpr float kLeftMargin = 0.01f;
148constexpr float kRightMargin = 0.02f;
149constexpr float kBottomMargin = 0.02f;
150constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700151
terelius53dc23c2017-03-13 05:24:05 -0700152rtc::Optional<double> NetworkDelayDiff_AbsSendTime(
153 const LoggedRtpPacket& old_packet,
154 const LoggedRtpPacket& new_packet) {
155 if (old_packet.header.extension.hasAbsoluteSendTime &&
156 new_packet.header.extension.hasAbsoluteSendTime) {
157 int64_t send_time_diff = WrappingDifference(
158 new_packet.header.extension.absoluteSendTime,
159 old_packet.header.extension.absoluteSendTime, 1ul << 24);
160 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
161 double delay_change_us =
162 recv_time_diff - AbsSendTimeToMicroseconds(send_time_diff);
163 return rtc::Optional<double>(delay_change_us / 1000);
164 } else {
165 return rtc::Optional<double>();
terelius6addf492016-08-23 17:34:07 -0700166 }
167}
168
terelius53dc23c2017-03-13 05:24:05 -0700169rtc::Optional<double> NetworkDelayDiff_CaptureTime(
170 const LoggedRtpPacket& old_packet,
171 const LoggedRtpPacket& new_packet) {
172 int64_t send_time_diff = WrappingDifference(
173 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
174 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
175
176 const double kVideoSampleRate = 90000;
177 // TODO(terelius): We treat all streams as video for now, even though
178 // audio might be sampled at e.g. 16kHz, because it is really difficult to
179 // figure out the true sampling rate of a stream. The effect is that the
180 // delay will be scaled incorrectly for non-video streams.
181
182 double delay_change =
183 static_cast<double>(recv_time_diff) / 1000 -
184 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
185 if (delay_change < -10000 || 10000 < delay_change) {
186 LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
187 LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
188 << ", received time " << old_packet.timestamp;
189 LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
190 << ", received time " << new_packet.timestamp;
191 LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
192 << static_cast<double>(recv_time_diff) / 1000000 << "s";
193 LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
194 << static_cast<double>(send_time_diff) / kVideoSampleRate
195 << "s";
196 }
197 return rtc::Optional<double>(delay_change);
198}
199
200// For each element in data, use |get_y()| to extract a y-coordinate and
201// store the result in a TimeSeries.
202template <typename DataType>
203void ProcessPoints(
204 rtc::FunctionView<rtc::Optional<float>(const DataType&)> get_y,
205 const std::vector<DataType>& data,
206 uint64_t begin_time,
207 TimeSeries* result) {
208 for (size_t i = 0; i < data.size(); i++) {
209 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
210 rtc::Optional<float> y = get_y(data[i]);
211 if (y)
212 result->points.emplace_back(x, *y);
213 }
214}
215
216// For each pair of adjacent elements in |data|, use |get_y| to extract a
terelius6addf492016-08-23 17:34:07 -0700217// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
218// will be the time of the second element in the pair.
terelius53dc23c2017-03-13 05:24:05 -0700219template <typename DataType, typename ResultType>
220void ProcessPairs(
221 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
222 const DataType&)> get_y,
223 const std::vector<DataType>& data,
224 uint64_t begin_time,
225 TimeSeries* result) {
tereliusccbbf8d2016-08-10 07:34:28 -0700226 for (size_t i = 1; i < data.size(); i++) {
227 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
terelius53dc23c2017-03-13 05:24:05 -0700228 rtc::Optional<ResultType> y = get_y(data[i - 1], data[i]);
229 if (y)
230 result->points.emplace_back(x, static_cast<float>(*y));
231 }
232}
233
234// For each element in data, use |extract()| to extract a y-coordinate and
235// store the result in a TimeSeries.
236template <typename DataType, typename ResultType>
237void AccumulatePoints(
238 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> extract,
239 const std::vector<DataType>& data,
240 uint64_t begin_time,
241 TimeSeries* result) {
242 ResultType sum = 0;
243 for (size_t i = 0; i < data.size(); i++) {
244 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
245 rtc::Optional<ResultType> y = extract(data[i]);
246 if (y) {
247 sum += *y;
248 result->points.emplace_back(x, static_cast<float>(sum));
249 }
250 }
251}
252
253// For each pair of adjacent elements in |data|, use |extract()| to extract a
254// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
255// will be the time of the second element in the pair.
256template <typename DataType, typename ResultType>
257void AccumulatePairs(
258 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
259 const DataType&)> extract,
260 const std::vector<DataType>& data,
261 uint64_t begin_time,
262 TimeSeries* result) {
263 ResultType sum = 0;
264 for (size_t i = 1; i < data.size(); i++) {
265 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
266 rtc::Optional<ResultType> y = extract(data[i - 1], data[i]);
267 if (y)
268 sum += *y;
269 result->points.emplace_back(x, static_cast<float>(sum));
tereliusccbbf8d2016-08-10 07:34:28 -0700270 }
271}
272
terelius6addf492016-08-23 17:34:07 -0700273// Calculates a moving average of |data| and stores the result in a TimeSeries.
274// A data point is generated every |step| microseconds from |begin_time|
275// to |end_time|. The value of each data point is the average of the data
276// during the preceeding |window_duration_us| microseconds.
terelius53dc23c2017-03-13 05:24:05 -0700277template <typename DataType, typename ResultType>
278void MovingAverage(
279 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> extract,
280 const std::vector<DataType>& data,
281 uint64_t begin_time,
282 uint64_t end_time,
283 uint64_t window_duration_us,
284 uint64_t step,
285 webrtc::plotting::TimeSeries* result) {
terelius6addf492016-08-23 17:34:07 -0700286 size_t window_index_begin = 0;
287 size_t window_index_end = 0;
terelius53dc23c2017-03-13 05:24:05 -0700288 ResultType sum_in_window = 0;
terelius6addf492016-08-23 17:34:07 -0700289
290 for (uint64_t t = begin_time; t < end_time + step; t += step) {
291 while (window_index_end < data.size() &&
292 data[window_index_end].timestamp < t) {
terelius53dc23c2017-03-13 05:24:05 -0700293 rtc::Optional<ResultType> value = extract(data[window_index_end]);
294 if (value)
295 sum_in_window += *value;
terelius6addf492016-08-23 17:34:07 -0700296 ++window_index_end;
297 }
298 while (window_index_begin < data.size() &&
299 data[window_index_begin].timestamp < t - window_duration_us) {
terelius53dc23c2017-03-13 05:24:05 -0700300 rtc::Optional<ResultType> value = extract(data[window_index_begin]);
301 if (value)
302 sum_in_window -= *value;
terelius6addf492016-08-23 17:34:07 -0700303 ++window_index_begin;
304 }
305 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
306 float x = static_cast<float>(t - begin_time) / 1000000;
terelius53dc23c2017-03-13 05:24:05 -0700307 float y = sum_in_window / window_duration_s;
terelius6addf492016-08-23 17:34:07 -0700308 result->points.emplace_back(x, y);
309 }
310}
311
terelius54ce6802016-07-13 06:44:41 -0700312} // namespace
313
terelius54ce6802016-07-13 06:44:41 -0700314EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
315 : parsed_log_(log), window_duration_(250000), step_(10000) {
316 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
317 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700318
terelius88e64e52016-07-19 01:51:06 -0700319 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700320 uint8_t header[IP_PACKET_SIZE];
321 size_t header_length;
322 size_t total_length;
323
perkjbbbad6d2017-05-19 06:30:28 -0700324 uint8_t last_incoming_rtcp_packet[IP_PACKET_SIZE];
325 uint8_t last_incoming_rtcp_packet_length = 0;
326
ivocaac9d6f2016-09-22 07:01:47 -0700327 // Make a default extension map for streams without configuration information.
328 // TODO(ivoc): Once configuration of audio streams is stored in the event log,
329 // this can be removed. Tracking bug: webrtc:6399
330 RtpHeaderExtensionMap default_extension_map = GetDefaultHeaderExtensionMap();
331
henrik.lundin3c938fc2017-06-14 06:09:58 -0700332 rtc::Optional<uint64_t> last_log_start;
333
terelius54ce6802016-07-13 06:44:41 -0700334 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
335 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700336 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
337 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
338 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700339 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
340 event_type != ParsedRtcEventLog::LOG_START &&
341 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700342 uint64_t timestamp = parsed_log_.GetTimestamp(i);
343 first_timestamp = std::min(first_timestamp, timestamp);
344 last_timestamp = std::max(last_timestamp, timestamp);
345 }
346
347 switch (parsed_log_.GetEventType(i)) {
348 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700349 rtclog::StreamConfig config = parsed_log_.GetVideoReceiveConfig(i);
perkj09e71da2017-05-22 03:26:49 -0700350 StreamId stream(config.remote_ssrc, kIncomingPacket);
terelius0740a202016-08-08 10:21:04 -0700351 video_ssrcs_.insert(stream);
perkj09e71da2017-05-22 03:26:49 -0700352 StreamId rtx_stream(config.rtx_ssrc, kIncomingPacket);
brandtr14742122017-01-27 04:53:07 -0800353 video_ssrcs_.insert(rtx_stream);
354 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700355 break;
356 }
357 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700358 std::vector<rtclog::StreamConfig> configs =
359 parsed_log_.GetVideoSendConfig(i);
terelius405f90c2017-06-01 03:50:31 -0700360 for (const auto& config : configs) {
361 StreamId stream(config.local_ssrc, kOutgoingPacket);
terelius8fbc7652017-05-31 02:03:16 -0700362 video_ssrcs_.insert(stream);
terelius405f90c2017-06-01 03:50:31 -0700363 StreamId rtx_stream(config.rtx_ssrc, kOutgoingPacket);
terelius8fbc7652017-05-31 02:03:16 -0700364 video_ssrcs_.insert(rtx_stream);
365 rtx_ssrcs_.insert(rtx_stream);
366 }
terelius88e64e52016-07-19 01:51:06 -0700367 break;
368 }
369 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700370 rtclog::StreamConfig config = parsed_log_.GetAudioReceiveConfig(i);
perkjac8f52d2017-05-22 09:36:28 -0700371 StreamId stream(config.remote_ssrc, kIncomingPacket);
ivoce0928d82016-10-10 05:12:51 -0700372 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700373 break;
374 }
375 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700376 rtclog::StreamConfig config = parsed_log_.GetAudioSendConfig(i);
perkjf4726992017-05-22 10:12:26 -0700377 StreamId stream(config.local_ssrc, kOutgoingPacket);
ivoce0928d82016-10-10 05:12:51 -0700378 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700379 break;
380 }
381 case ParsedRtcEventLog::RTP_EVENT: {
ilnika8e781a2017-06-12 01:02:46 -0700382 RtpHeaderExtensionMap* extension_map = parsed_log_.GetRtpHeader(
Elad Alon1d87b0e2017-10-03 15:01:03 +0200383 i, &direction, header, &header_length, &total_length, nullptr);
terelius88e64e52016-07-19 01:51:06 -0700384 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
385 RTPHeader parsed_header;
ilnika8e781a2017-06-12 01:02:46 -0700386 if (extension_map != nullptr) {
terelius88e64e52016-07-19 01:51:06 -0700387 rtp_parser.Parse(&parsed_header, extension_map);
ivocaac9d6f2016-09-22 07:01:47 -0700388 } else {
389 // Use the default extension map.
390 // TODO(ivoc): Once configuration of audio streams is stored in the
391 // event log, this can be removed.
392 // Tracking bug: webrtc:6399
393 rtp_parser.Parse(&parsed_header, &default_extension_map);
terelius88e64e52016-07-19 01:51:06 -0700394 }
395 uint64_t timestamp = parsed_log_.GetTimestamp(i);
ilnika8e781a2017-06-12 01:02:46 -0700396 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700397 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200398 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700399 break;
400 }
401 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200402 uint8_t packet[IP_PACKET_SIZE];
perkj77cd58e2017-05-30 03:52:10 -0700403 parsed_log_.GetRtcpPacket(i, &direction, packet, &total_length);
perkjbbbad6d2017-05-19 06:30:28 -0700404 // Currently incoming RTCP packets are logged twice, both for audio and
405 // video. Only act on one of them. Compare against the previous parsed
406 // incoming RTCP packet.
407 if (direction == webrtc::kIncomingPacket) {
408 RTC_CHECK_LE(total_length, IP_PACKET_SIZE);
409 if (total_length == last_incoming_rtcp_packet_length &&
410 memcmp(last_incoming_rtcp_packet, packet, total_length) == 0) {
411 continue;
412 } else {
413 memcpy(last_incoming_rtcp_packet, packet, total_length);
414 last_incoming_rtcp_packet_length = total_length;
415 }
416 }
417 rtcp::CommonHeader header;
418 const uint8_t* packet_end = packet + total_length;
419 for (const uint8_t* block = packet; block < packet_end;
420 block = header.NextPacket()) {
421 RTC_CHECK(header.Parse(block, packet_end - block));
422 if (header.type() == rtcp::TransportFeedback::kPacketType &&
423 header.fmt() == rtcp::TransportFeedback::kFeedbackMessageType) {
424 std::unique_ptr<rtcp::TransportFeedback> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700425 rtc::MakeUnique<rtcp::TransportFeedback>());
perkjbbbad6d2017-05-19 06:30:28 -0700426 if (rtcp_packet->Parse(header)) {
427 uint32_t ssrc = rtcp_packet->sender_ssrc();
428 StreamId stream(ssrc, direction);
429 uint64_t timestamp = parsed_log_.GetTimestamp(i);
430 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
431 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
432 }
433 } else if (header.type() == rtcp::SenderReport::kPacketType) {
434 std::unique_ptr<rtcp::SenderReport> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700435 rtc::MakeUnique<rtcp::SenderReport>());
perkjbbbad6d2017-05-19 06:30:28 -0700436 if (rtcp_packet->Parse(header)) {
437 uint32_t ssrc = rtcp_packet->sender_ssrc();
438 StreamId stream(ssrc, direction);
439 uint64_t timestamp = parsed_log_.GetTimestamp(i);
440 rtcp_packets_[stream].push_back(
441 LoggedRtcpPacket(timestamp, kRtcpSr, std::move(rtcp_packet)));
442 }
443 } else if (header.type() == rtcp::ReceiverReport::kPacketType) {
444 std::unique_ptr<rtcp::ReceiverReport> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700445 rtc::MakeUnique<rtcp::ReceiverReport>());
perkjbbbad6d2017-05-19 06:30:28 -0700446 if (rtcp_packet->Parse(header)) {
447 uint32_t ssrc = rtcp_packet->sender_ssrc();
448 StreamId stream(ssrc, direction);
449 uint64_t timestamp = parsed_log_.GetTimestamp(i);
450 rtcp_packets_[stream].push_back(
451 LoggedRtcpPacket(timestamp, kRtcpRr, std::move(rtcp_packet)));
Stefan Holmer13181032016-07-29 14:48:54 +0200452 }
terelius2c8e8a32017-06-02 01:29:48 -0700453 } else if (header.type() == rtcp::Remb::kPacketType &&
454 header.fmt() == rtcp::Remb::kFeedbackMessageType) {
455 std::unique_ptr<rtcp::Remb> rtcp_packet(
456 rtc::MakeUnique<rtcp::Remb>());
457 if (rtcp_packet->Parse(header)) {
458 uint32_t ssrc = rtcp_packet->sender_ssrc();
459 StreamId stream(ssrc, direction);
460 uint64_t timestamp = parsed_log_.GetTimestamp(i);
461 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
462 timestamp, kRtcpRemb, std::move(rtcp_packet)));
463 }
Stefan Holmer13181032016-07-29 14:48:54 +0200464 }
Stefan Holmer13181032016-07-29 14:48:54 +0200465 }
terelius88e64e52016-07-19 01:51:06 -0700466 break;
467 }
468 case ParsedRtcEventLog::LOG_START: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700469 if (last_log_start) {
470 // A LOG_END event was missing. Use last_timestamp.
471 RTC_DCHECK_GE(last_timestamp, *last_log_start);
472 log_segments_.push_back(
473 std::make_pair(*last_log_start, last_timestamp));
474 }
475 last_log_start = rtc::Optional<uint64_t>(parsed_log_.GetTimestamp(i));
terelius88e64e52016-07-19 01:51:06 -0700476 break;
477 }
478 case ParsedRtcEventLog::LOG_END: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700479 RTC_DCHECK(last_log_start);
480 log_segments_.push_back(
481 std::make_pair(*last_log_start, parsed_log_.GetTimestamp(i)));
482 last_log_start.reset();
terelius88e64e52016-07-19 01:51:06 -0700483 break;
484 }
terelius424e6cf2017-02-20 05:14:41 -0800485 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700486 uint32_t this_ssrc;
487 parsed_log_.GetAudioPlayout(i, &this_ssrc);
488 audio_playout_events_[this_ssrc].push_back(parsed_log_.GetTimestamp(i));
terelius424e6cf2017-02-20 05:14:41 -0800489 break;
490 }
491 case ParsedRtcEventLog::LOSS_BASED_BWE_UPDATE: {
492 LossBasedBweUpdate bwe_update;
terelius8058e582016-07-25 01:32:41 -0700493 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
terelius424e6cf2017-02-20 05:14:41 -0800494 parsed_log_.GetLossBasedBweUpdate(i, &bwe_update.new_bitrate,
495 &bwe_update.fraction_loss,
496 &bwe_update.expected_packets);
terelius8058e582016-07-25 01:32:41 -0700497 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700498 break;
499 }
terelius424e6cf2017-02-20 05:14:41 -0800500 case ParsedRtcEventLog::DELAY_BASED_BWE_UPDATE: {
philipel10fc0e62017-04-11 01:50:23 -0700501 bwe_delay_updates_.push_back(parsed_log_.GetDelayBasedBweUpdate(i));
terelius424e6cf2017-02-20 05:14:41 -0800502 break;
503 }
minyue4b7c9522017-01-24 04:54:59 -0800504 case ParsedRtcEventLog::AUDIO_NETWORK_ADAPTATION_EVENT: {
michaelt6e5b2192017-02-22 07:33:27 -0800505 AudioNetworkAdaptationEvent ana_event;
506 ana_event.timestamp = parsed_log_.GetTimestamp(i);
507 parsed_log_.GetAudioNetworkAdaptation(i, &ana_event.config);
508 audio_network_adaptation_events_.push_back(ana_event);
minyue4b7c9522017-01-24 04:54:59 -0800509 break;
510 }
philipel32d00102017-02-27 02:18:46 -0800511 case ParsedRtcEventLog::BWE_PROBE_CLUSTER_CREATED_EVENT: {
philipele127e7a2017-03-29 16:28:53 +0200512 bwe_probe_cluster_created_events_.push_back(
513 parsed_log_.GetBweProbeClusterCreated(i));
philipel32d00102017-02-27 02:18:46 -0800514 break;
515 }
516 case ParsedRtcEventLog::BWE_PROBE_RESULT_EVENT: {
philipele127e7a2017-03-29 16:28:53 +0200517 bwe_probe_result_events_.push_back(parsed_log_.GetBweProbeResult(i));
philipel32d00102017-02-27 02:18:46 -0800518 break;
519 }
terelius88e64e52016-07-19 01:51:06 -0700520 case ParsedRtcEventLog::UNKNOWN_EVENT: {
521 break;
522 }
523 }
terelius54ce6802016-07-13 06:44:41 -0700524 }
terelius88e64e52016-07-19 01:51:06 -0700525
terelius54ce6802016-07-13 06:44:41 -0700526 if (last_timestamp < first_timestamp) {
527 // No useful events in the log.
528 first_timestamp = last_timestamp = 0;
529 }
530 begin_time_ = first_timestamp;
531 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700532 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
henrik.lundin3c938fc2017-06-14 06:09:58 -0700533 if (last_log_start) {
534 // The log was missing the last LOG_END event. Fake it.
535 log_segments_.push_back(std::make_pair(*last_log_start, end_time_));
536 }
terelius54ce6802016-07-13 06:44:41 -0700537}
538
Niels Möller245f17e2017-08-21 10:45:07 +0200539class BitrateObserver : public SendSideCongestionController::Observer,
Stefan Holmer13181032016-07-29 14:48:54 +0200540 public RemoteBitrateObserver {
541 public:
542 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
543
544 void OnNetworkChanged(uint32_t bitrate_bps,
545 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800546 int64_t rtt_ms,
547 int64_t probing_interval_ms) override {
Stefan Holmer13181032016-07-29 14:48:54 +0200548 last_bitrate_bps_ = bitrate_bps;
549 bitrate_updated_ = true;
550 }
551
552 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
553 uint32_t bitrate) override {}
554
555 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
556 bool GetAndResetBitrateUpdated() {
557 bool bitrate_updated = bitrate_updated_;
558 bitrate_updated_ = false;
559 return bitrate_updated;
560 }
561
562 private:
563 uint32_t last_bitrate_bps_;
564 bool bitrate_updated_;
565};
566
Stefan Holmer99f8e082016-09-09 13:37:50 +0200567bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700568 return rtx_ssrcs_.count(stream_id) == 1;
569}
570
Stefan Holmer99f8e082016-09-09 13:37:50 +0200571bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700572 return video_ssrcs_.count(stream_id) == 1;
573}
574
Stefan Holmer99f8e082016-09-09 13:37:50 +0200575bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700576 return audio_ssrcs_.count(stream_id) == 1;
577}
578
Stefan Holmer99f8e082016-09-09 13:37:50 +0200579std::string EventLogAnalyzer::GetStreamName(StreamId stream_id) const {
580 std::stringstream name;
581 if (IsAudioSsrc(stream_id)) {
582 name << "Audio ";
583 } else if (IsVideoSsrc(stream_id)) {
584 name << "Video ";
585 } else {
586 name << "Unknown ";
587 }
588 if (IsRtxSsrc(stream_id))
589 name << "RTX ";
ivocaac9d6f2016-09-22 07:01:47 -0700590 if (stream_id.GetDirection() == kIncomingPacket) {
591 name << "(In) ";
592 } else {
593 name << "(Out) ";
594 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200595 name << SsrcToString(stream_id.GetSsrc());
596 return name.str();
597}
598
terelius54ce6802016-07-13 06:44:41 -0700599void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
600 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700601 for (auto& kv : rtp_packets_) {
602 StreamId stream_id = kv.first;
603 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
604 // Filter on direction and SSRC.
605 if (stream_id.GetDirection() != desired_direction ||
606 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
607 continue;
terelius54ce6802016-07-13 06:44:41 -0700608 }
terelius54ce6802016-07-13 06:44:41 -0700609
terelius23c595a2017-03-15 01:59:12 -0700610 TimeSeries time_series(GetStreamName(stream_id), BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700611 ProcessPoints<LoggedRtpPacket>(
612 [](const LoggedRtpPacket& packet) -> rtc::Optional<float> {
613 return rtc::Optional<float>(packet.total_length);
614 },
615 packet_stream, begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700616 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700617 }
618
tereliusdc35dcd2016-08-01 12:03:27 -0700619 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
620 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
621 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700622 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700623 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700624 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700625 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700626 }
627}
628
philipelccd74892016-09-05 02:46:25 -0700629template <typename T>
630void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
631 PacketDirection desired_direction,
632 Plot* plot,
633 const std::map<StreamId, std::vector<T>>& packets,
634 const std::string& label_prefix) {
635 for (auto& kv : packets) {
636 StreamId stream_id = kv.first;
637 const std::vector<T>& packet_stream = kv.second;
638 // Filter on direction and SSRC.
639 if (stream_id.GetDirection() != desired_direction ||
640 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
641 continue;
642 }
643
terelius23c595a2017-03-15 01:59:12 -0700644 std::string label = label_prefix + " " + GetStreamName(stream_id);
645 TimeSeries time_series(label, LINE_STEP_GRAPH);
philipelccd74892016-09-05 02:46:25 -0700646 for (size_t i = 0; i < packet_stream.size(); i++) {
647 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
648 1000000;
philipelccd74892016-09-05 02:46:25 -0700649 time_series.points.emplace_back(x, i + 1);
650 }
651
philipel35ba9bd2017-04-19 05:58:51 -0700652 plot->AppendTimeSeries(std::move(time_series));
philipelccd74892016-09-05 02:46:25 -0700653 }
654}
655
656void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
657 PacketDirection desired_direction,
658 Plot* plot) {
659 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
660 "RTP");
661 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
662 "RTCP");
663
664 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
665 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
666 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
667 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
668 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
669 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
670 }
671}
672
terelius54ce6802016-07-13 06:44:41 -0700673// For each SSRC, plot the time between the consecutive playouts.
674void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
675 std::map<uint32_t, TimeSeries> time_series;
676 std::map<uint32_t, uint64_t> last_playout;
677
678 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700679
680 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
681 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
682 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
683 parsed_log_.GetAudioPlayout(i, &ssrc);
684 uint64_t timestamp = parsed_log_.GetTimestamp(i);
685 if (MatchingSsrc(ssrc, desired_ssrc_)) {
686 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
687 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
688 if (time_series[ssrc].points.size() == 0) {
689 // There were no previusly logged playout for this SSRC.
690 // Generate a point, but place it on the x-axis.
691 y = 0;
692 }
terelius54ce6802016-07-13 06:44:41 -0700693 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
694 last_playout[ssrc] = timestamp;
695 }
696 }
697 }
698
699 // Set labels and put in graph.
700 for (auto& kv : time_series) {
701 kv.second.label = SsrcToString(kv.first);
702 kv.second.style = BAR_GRAPH;
philipel35ba9bd2017-04-19 05:58:51 -0700703 plot->AppendTimeSeries(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700704 }
705
tereliusdc35dcd2016-08-01 12:03:27 -0700706 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
707 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
708 kTopMargin);
709 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700710}
711
ivocaac9d6f2016-09-22 07:01:47 -0700712// For audio SSRCs, plot the audio level.
713void EventLogAnalyzer::CreateAudioLevelGraph(Plot* plot) {
714 std::map<StreamId, TimeSeries> time_series;
715
716 for (auto& kv : rtp_packets_) {
717 StreamId stream_id = kv.first;
718 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
719 // TODO(ivoc): When audio send/receive configs are stored in the event
720 // log, a check should be added here to only process audio
721 // streams. Tracking bug: webrtc:6399
722 for (auto& packet : packet_stream) {
723 if (packet.header.extension.hasAudioLevel) {
724 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
725 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
726 // Here we convert it to dBov.
727 float y = static_cast<float>(-packet.header.extension.audioLevel);
728 time_series[stream_id].points.emplace_back(TimeSeriesPoint(x, y));
729 }
730 }
731 }
732
733 for (auto& series : time_series) {
734 series.second.label = GetStreamName(series.first);
735 series.second.style = LINE_GRAPH;
philipel35ba9bd2017-04-19 05:58:51 -0700736 plot->AppendTimeSeries(std::move(series.second));
ivocaac9d6f2016-09-22 07:01:47 -0700737 }
738
739 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
ivocbf676632016-11-24 08:30:34 -0800740 plot->SetYAxis(-127, 0, "Audio level (dBov)", kBottomMargin,
ivocaac9d6f2016-09-22 07:01:47 -0700741 kTopMargin);
742 plot->SetTitle("Audio level");
743}
744
terelius54ce6802016-07-13 06:44:41 -0700745// For each SSRC, plot the time between the consecutive playouts.
746void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700747 for (auto& kv : rtp_packets_) {
748 StreamId stream_id = kv.first;
749 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
750 // Filter on direction and SSRC.
751 if (stream_id.GetDirection() != kIncomingPacket ||
752 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
753 continue;
terelius54ce6802016-07-13 06:44:41 -0700754 }
terelius54ce6802016-07-13 06:44:41 -0700755
terelius23c595a2017-03-15 01:59:12 -0700756 TimeSeries time_series(GetStreamName(stream_id), BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700757 ProcessPairs<LoggedRtpPacket, float>(
758 [](const LoggedRtpPacket& old_packet,
759 const LoggedRtpPacket& new_packet) {
760 int64_t diff =
761 WrappingDifference(new_packet.header.sequenceNumber,
762 old_packet.header.sequenceNumber, 1ul << 16);
763 return rtc::Optional<float>(diff);
764 },
765 packet_stream, begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700766 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700767 }
768
tereliusdc35dcd2016-08-01 12:03:27 -0700769 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
770 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
771 kTopMargin);
772 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700773}
774
Stefan Holmer99f8e082016-09-09 13:37:50 +0200775void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
776 for (auto& kv : rtp_packets_) {
777 StreamId stream_id = kv.first;
778 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
779 // Filter on direction and SSRC.
780 if (stream_id.GetDirection() != kIncomingPacket ||
terelius4c9b4af2017-01-30 08:44:51 -0800781 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
782 packet_stream.size() == 0) {
Stefan Holmer99f8e082016-09-09 13:37:50 +0200783 continue;
784 }
785
terelius23c595a2017-03-15 01:59:12 -0700786 TimeSeries time_series(GetStreamName(stream_id), LINE_DOT_GRAPH);
Stefan Holmer99f8e082016-09-09 13:37:50 +0200787 const uint64_t kWindowUs = 1000000;
terelius4c9b4af2017-01-30 08:44:51 -0800788 const uint64_t kStep = 1000000;
789 SequenceNumberUnwrapper unwrapper_;
790 SequenceNumberUnwrapper prior_unwrapper_;
791 size_t window_index_begin = 0;
792 size_t window_index_end = 0;
793 int64_t highest_seq_number =
794 unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
795 int64_t highest_prior_seq_number =
796 prior_unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
797
798 for (uint64_t t = begin_time_; t < end_time_ + kStep; t += kStep) {
799 while (window_index_end < packet_stream.size() &&
800 packet_stream[window_index_end].timestamp < t) {
801 int64_t sequence_number = unwrapper_.Unwrap(
802 packet_stream[window_index_end].header.sequenceNumber);
803 highest_seq_number = std::max(highest_seq_number, sequence_number);
804 ++window_index_end;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200805 }
terelius4c9b4af2017-01-30 08:44:51 -0800806 while (window_index_begin < packet_stream.size() &&
807 packet_stream[window_index_begin].timestamp < t - kWindowUs) {
808 int64_t sequence_number = prior_unwrapper_.Unwrap(
809 packet_stream[window_index_begin].header.sequenceNumber);
810 highest_prior_seq_number =
811 std::max(highest_prior_seq_number, sequence_number);
812 ++window_index_begin;
813 }
814 float x = static_cast<float>(t - begin_time_) / 1000000;
815 int64_t expected_packets = highest_seq_number - highest_prior_seq_number;
816 if (expected_packets > 0) {
817 int64_t received_packets = window_index_end - window_index_begin;
818 int64_t lost_packets = expected_packets - received_packets;
819 float y = static_cast<float>(lost_packets) / expected_packets * 100;
820 time_series.points.emplace_back(x, y);
821 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200822 }
philipel35ba9bd2017-04-19 05:58:51 -0700823 plot->AppendTimeSeries(std::move(time_series));
Stefan Holmer99f8e082016-09-09 13:37:50 +0200824 }
825
826 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
827 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
828 kTopMargin);
829 plot->SetTitle("Estimated incoming loss rate");
830}
831
terelius2ee076d2017-08-15 02:04:02 -0700832void EventLogAnalyzer::CreateIncomingDelayDeltaGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700833 for (auto& kv : rtp_packets_) {
834 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700835 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700836 // Filter on direction and SSRC.
837 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200838 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
839 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
840 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700841 continue;
842 }
terelius54ce6802016-07-13 06:44:41 -0700843
terelius23c595a2017-03-15 01:59:12 -0700844 TimeSeries capture_time_data(GetStreamName(stream_id) + " capture-time",
845 BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700846 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
847 packet_stream, begin_time_,
848 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700849 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700850
terelius23c595a2017-03-15 01:59:12 -0700851 TimeSeries send_time_data(GetStreamName(stream_id) + " abs-send-time",
852 BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700853 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
854 packet_stream, begin_time_,
855 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700856 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700857 }
858
tereliusdc35dcd2016-08-01 12:03:27 -0700859 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
860 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
861 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700862 plot->SetTitle("Network latency difference between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700863}
864
terelius2ee076d2017-08-15 02:04:02 -0700865void EventLogAnalyzer::CreateIncomingDelayGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700866 for (auto& kv : rtp_packets_) {
867 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700868 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700869 // Filter on direction and SSRC.
870 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200871 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
872 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
873 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700874 continue;
875 }
terelius54ce6802016-07-13 06:44:41 -0700876
terelius23c595a2017-03-15 01:59:12 -0700877 TimeSeries capture_time_data(GetStreamName(stream_id) + " capture-time",
878 LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700879 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
880 packet_stream, begin_time_,
881 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700882 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700883
terelius23c595a2017-03-15 01:59:12 -0700884 TimeSeries send_time_data(GetStreamName(stream_id) + " abs-send-time",
885 LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700886 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
887 packet_stream, begin_time_,
888 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700889 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700890 }
891
tereliusdc35dcd2016-08-01 12:03:27 -0700892 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
893 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
894 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700895 plot->SetTitle("Network latency (relative to first packet)");
terelius54ce6802016-07-13 06:44:41 -0700896}
897
tereliusf736d232016-08-04 10:00:11 -0700898// Plot the fraction of packets lost (as perceived by the loss-based BWE).
899void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -0700900 TimeSeries time_series("Fraction lost", LINE_DOT_GRAPH);
tereliusf736d232016-08-04 10:00:11 -0700901 for (auto& bwe_update : bwe_loss_updates_) {
902 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
903 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
philipel35ba9bd2017-04-19 05:58:51 -0700904 time_series.points.emplace_back(x, y);
tereliusf736d232016-08-04 10:00:11 -0700905 }
tereliusf736d232016-08-04 10:00:11 -0700906
Bjorn Terelius19f5be32017-10-18 12:39:49 +0200907 plot->AppendTimeSeries(std::move(time_series));
tereliusf736d232016-08-04 10:00:11 -0700908 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
909 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
910 kTopMargin);
911 plot->SetTitle("Reported packet loss");
912}
913
terelius54ce6802016-07-13 06:44:41 -0700914// Plot the total bandwidth used by all RTP streams.
915void EventLogAnalyzer::CreateTotalBitrateGraph(
916 PacketDirection desired_direction,
philipel23c7f252017-07-14 06:30:03 -0700917 Plot* plot,
918 bool show_detector_state) {
terelius54ce6802016-07-13 06:44:41 -0700919 struct TimestampSize {
920 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
921 uint64_t timestamp;
922 size_t size;
923 };
924 std::vector<TimestampSize> packets;
925
926 PacketDirection direction;
927 size_t total_length;
928
929 // Extract timestamps and sizes for the relevant packets.
930 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
931 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
932 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
Elad Alon1d87b0e2017-10-03 15:01:03 +0200933 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, &total_length,
934 nullptr);
terelius54ce6802016-07-13 06:44:41 -0700935 if (direction == desired_direction) {
936 uint64_t timestamp = parsed_log_.GetTimestamp(i);
937 packets.push_back(TimestampSize(timestamp, total_length));
938 }
939 }
940 }
941
942 size_t window_index_begin = 0;
943 size_t window_index_end = 0;
944 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700945
946 // Calculate a moving average of the bitrate and store in a TimeSeries.
philipel35ba9bd2017-04-19 05:58:51 -0700947 TimeSeries bitrate_series("Bitrate", LINE_GRAPH);
terelius54ce6802016-07-13 06:44:41 -0700948 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
949 while (window_index_end < packets.size() &&
950 packets[window_index_end].timestamp < time) {
951 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700952 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700953 }
954 while (window_index_begin < packets.size() &&
955 packets[window_index_begin].timestamp < time - window_duration_) {
956 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
957 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700958 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700959 }
960 float window_duration_in_seconds =
961 static_cast<float>(window_duration_) / 1000000;
962 float x = static_cast<float>(time - begin_time_) / 1000000;
963 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
philipel35ba9bd2017-04-19 05:58:51 -0700964 bitrate_series.points.emplace_back(x, y);
terelius54ce6802016-07-13 06:44:41 -0700965 }
philipel35ba9bd2017-04-19 05:58:51 -0700966 plot->AppendTimeSeries(std::move(bitrate_series));
terelius54ce6802016-07-13 06:44:41 -0700967
terelius8058e582016-07-25 01:32:41 -0700968 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
969 if (desired_direction == kOutgoingPacket) {
philipel35ba9bd2017-04-19 05:58:51 -0700970 TimeSeries loss_series("Loss-based estimate", LINE_STEP_GRAPH);
philipel10fc0e62017-04-11 01:50:23 -0700971 for (auto& loss_update : bwe_loss_updates_) {
terelius8058e582016-07-25 01:32:41 -0700972 float x =
philipel10fc0e62017-04-11 01:50:23 -0700973 static_cast<float>(loss_update.timestamp - begin_time_) / 1000000;
974 float y = static_cast<float>(loss_update.new_bitrate) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -0700975 loss_series.points.emplace_back(x, y);
philipel10fc0e62017-04-11 01:50:23 -0700976 }
977
philipel35ba9bd2017-04-19 05:58:51 -0700978 TimeSeries delay_series("Delay-based estimate", LINE_STEP_GRAPH);
philipel23c7f252017-07-14 06:30:03 -0700979 IntervalSeries overusing_series("Overusing", "#ff8e82",
980 IntervalSeries::kHorizontal);
981 IntervalSeries underusing_series("Underusing", "#5092fc",
982 IntervalSeries::kHorizontal);
983 IntervalSeries normal_series("Normal", "#c4ffc4",
984 IntervalSeries::kHorizontal);
985 IntervalSeries* last_series = &normal_series;
986 double last_detector_switch = 0.0;
987
988 BandwidthUsage last_detector_state = BandwidthUsage::kBwNormal;
989
philipel10fc0e62017-04-11 01:50:23 -0700990 for (auto& delay_update : bwe_delay_updates_) {
991 float x =
992 static_cast<float>(delay_update.timestamp - begin_time_) / 1000000;
993 float y = static_cast<float>(delay_update.bitrate_bps) / 1000;
philipel23c7f252017-07-14 06:30:03 -0700994
995 if (last_detector_state != delay_update.detector_state) {
996 last_series->intervals.emplace_back(last_detector_switch, x);
997 last_detector_state = delay_update.detector_state;
998 last_detector_switch = x;
999
1000 switch (delay_update.detector_state) {
1001 case BandwidthUsage::kBwNormal:
1002 last_series = &normal_series;
1003 break;
1004 case BandwidthUsage::kBwUnderusing:
1005 last_series = &underusing_series;
1006 break;
1007 case BandwidthUsage::kBwOverusing:
1008 last_series = &overusing_series;
1009 break;
Elad Alon1d87b0e2017-10-03 15:01:03 +02001010 case BandwidthUsage::kLast:
1011 RTC_NOTREACHED();
philipel23c7f252017-07-14 06:30:03 -07001012 }
1013 }
1014
philipel35ba9bd2017-04-19 05:58:51 -07001015 delay_series.points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -07001016 }
philipele127e7a2017-03-29 16:28:53 +02001017
philipel23c7f252017-07-14 06:30:03 -07001018 RTC_CHECK(last_series);
1019 last_series->intervals.emplace_back(last_detector_switch, end_time_);
1020
philipel35ba9bd2017-04-19 05:58:51 -07001021 TimeSeries created_series("Probe cluster created.", DOT_GRAPH);
philipele127e7a2017-03-29 16:28:53 +02001022 for (auto& cluster : bwe_probe_cluster_created_events_) {
1023 float x = static_cast<float>(cluster.timestamp - begin_time_) / 1000000;
1024 float y = static_cast<float>(cluster.bitrate_bps) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001025 created_series.points.emplace_back(x, y);
philipele127e7a2017-03-29 16:28:53 +02001026 }
1027
philipel35ba9bd2017-04-19 05:58:51 -07001028 TimeSeries result_series("Probing results.", DOT_GRAPH);
philipele127e7a2017-03-29 16:28:53 +02001029 for (auto& result : bwe_probe_result_events_) {
1030 if (result.bitrate_bps) {
1031 float x = static_cast<float>(result.timestamp - begin_time_) / 1000000;
1032 float y = static_cast<float>(*result.bitrate_bps) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001033 result_series.points.emplace_back(x, y);
philipele127e7a2017-03-29 16:28:53 +02001034 }
1035 }
philipel23c7f252017-07-14 06:30:03 -07001036
1037 if (show_detector_state) {
1038 plot->AppendIntervalSeries(std::move(overusing_series));
1039 plot->AppendIntervalSeries(std::move(underusing_series));
1040 plot->AppendIntervalSeries(std::move(normal_series));
1041 }
1042
philipel35ba9bd2017-04-19 05:58:51 -07001043 plot->AppendTimeSeries(std::move(loss_series));
1044 plot->AppendTimeSeries(std::move(delay_series));
1045 plot->AppendTimeSeries(std::move(created_series));
1046 plot->AppendTimeSeries(std::move(result_series));
terelius8058e582016-07-25 01:32:41 -07001047 }
philipele127e7a2017-03-29 16:28:53 +02001048
terelius2c8e8a32017-06-02 01:29:48 -07001049 // Overlay the incoming REMB over the outgoing bitrate
1050 // and outgoing REMB over incoming bitrate.
1051 PacketDirection remb_direction =
1052 desired_direction == kOutgoingPacket ? kIncomingPacket : kOutgoingPacket;
1053 TimeSeries remb_series("Remb", LINE_STEP_GRAPH);
1054 std::multimap<uint64_t, const LoggedRtcpPacket*> remb_packets;
1055 for (const auto& kv : rtcp_packets_) {
1056 if (kv.first.GetDirection() == remb_direction) {
1057 for (const LoggedRtcpPacket& rtcp_packet : kv.second) {
1058 if (rtcp_packet.type == kRtcpRemb) {
1059 remb_packets.insert(
1060 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1061 }
1062 }
1063 }
1064 }
1065
1066 for (const auto& kv : remb_packets) {
1067 const LoggedRtcpPacket* const rtcp = kv.second;
1068 const rtcp::Remb* const remb = static_cast<rtcp::Remb*>(rtcp->packet.get());
1069 float x = static_cast<float>(rtcp->timestamp - begin_time_) / 1000000;
1070 float y = static_cast<float>(remb->bitrate_bps()) / 1000;
1071 remb_series.points.emplace_back(x, y);
1072 }
1073 plot->AppendTimeSeriesIfNotEmpty(std::move(remb_series));
1074
tereliusdc35dcd2016-08-01 12:03:27 -07001075 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1076 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -07001077 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001078 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001079 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001080 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001081 }
1082}
1083
1084// For each SSRC, plot the bandwidth used by that stream.
1085void EventLogAnalyzer::CreateStreamBitrateGraph(
1086 PacketDirection desired_direction,
1087 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -07001088 for (auto& kv : rtp_packets_) {
1089 StreamId stream_id = kv.first;
1090 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
1091 // Filter on direction and SSRC.
1092 if (stream_id.GetDirection() != desired_direction ||
1093 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
1094 continue;
terelius54ce6802016-07-13 06:44:41 -07001095 }
1096
terelius23c595a2017-03-15 01:59:12 -07001097 TimeSeries time_series(GetStreamName(stream_id), LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001098 MovingAverage<LoggedRtpPacket, double>(
1099 [](const LoggedRtpPacket& packet) {
1100 return rtc::Optional<double>(packet.total_length * 8.0 / 1000.0);
1101 },
1102 packet_stream, begin_time_, end_time_, window_duration_, step_,
1103 &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001104 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -07001105 }
1106
tereliusdc35dcd2016-08-01 12:03:27 -07001107 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1108 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -07001109 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001110 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001111 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001112 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001113 }
1114}
1115
Bjorn Terelius28db2662017-10-04 14:22:43 +02001116void EventLogAnalyzer::CreateSendSideBweSimulationGraph(Plot* plot) {
stefanff421622017-04-20 03:24:01 -07001117 std::multimap<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1118 std::multimap<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
Stefan Holmer13181032016-07-29 14:48:54 +02001119
1120 for (const auto& kv : rtp_packets_) {
1121 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1122 for (const LoggedRtpPacket& rtp_packet : kv.second)
1123 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1124 }
1125 }
1126
1127 for (const auto& kv : rtcp_packets_) {
1128 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1129 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1130 incoming_rtcp.insert(
1131 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1132 }
1133 }
1134
1135 SimulatedClock clock(0);
1136 BitrateObserver observer;
1137 RtcEventLogNullImpl null_event_log;
nisse0245da02016-11-30 03:35:20 -08001138 PacketRouter packet_router;
Stefan Holmer5c8942a2017-08-22 16:16:44 +02001139 PacedSender pacer(&clock, &packet_router, &null_event_log);
1140 SendSideCongestionController cc(&clock, &observer, &null_event_log, &pacer);
Stefan Holmer13181032016-07-29 14:48:54 +02001141 // TODO(holmer): Log the call config and use that here instead.
1142 static const uint32_t kDefaultStartBitrateBps = 300000;
1143 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1144
terelius23c595a2017-03-15 01:59:12 -07001145 TimeSeries time_series("Delay-based estimate", LINE_DOT_GRAPH);
1146 TimeSeries acked_time_series("Acked bitrate", LINE_DOT_GRAPH);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001147 TimeSeries acked_estimate_time_series("Acked bitrate estimate",
1148 LINE_DOT_GRAPH);
Stefan Holmer13181032016-07-29 14:48:54 +02001149
1150 auto rtp_iterator = outgoing_rtp.begin();
1151 auto rtcp_iterator = incoming_rtcp.begin();
1152
1153 auto NextRtpTime = [&]() {
1154 if (rtp_iterator != outgoing_rtp.end())
1155 return static_cast<int64_t>(rtp_iterator->first);
1156 return std::numeric_limits<int64_t>::max();
1157 };
1158
1159 auto NextRtcpTime = [&]() {
1160 if (rtcp_iterator != incoming_rtcp.end())
1161 return static_cast<int64_t>(rtcp_iterator->first);
1162 return std::numeric_limits<int64_t>::max();
1163 };
1164
1165 auto NextProcessTime = [&]() {
1166 if (rtcp_iterator != incoming_rtcp.end() ||
1167 rtp_iterator != outgoing_rtp.end()) {
1168 return clock.TimeInMicroseconds() +
1169 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
1170 }
1171 return std::numeric_limits<int64_t>::max();
1172 };
1173
Stefan Holmer492ee282016-10-27 17:19:20 +02001174 RateStatistics acked_bitrate(250, 8000);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001175#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1176 // The event_log_visualizer should normally not be compiled with
1177 // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE since the normal plots won't work.
1178 // However, compiling with BWE_TEST_LOGGING, runnning with --plot_sendside_bwe
1179 // and piping the output to plot_dynamics.py can be used as a hack to get the
1180 // internal state of various BWE components. In this case, it is important
1181 // we don't instantiate the AcknowledgedBitrateEstimator both here and in
1182 // SendSideCongestionController since that would lead to duplicate outputs.
1183 AcknowledgedBitrateEstimator acknowledged_bitrate_estimator(
1184 rtc::MakeUnique<BitrateEstimator>());
1185#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Stefan Holmer13181032016-07-29 14:48:54 +02001186 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
Stefan Holmer492ee282016-10-27 17:19:20 +02001187 int64_t last_update_us = 0;
Stefan Holmer13181032016-07-29 14:48:54 +02001188 while (time_us != std::numeric_limits<int64_t>::max()) {
1189 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1190 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001191 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001192 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1193 if (rtcp.type == kRtcpTransportFeedback) {
elad.alon5bbf43f2017-03-09 06:40:08 -08001194 cc.OnTransportFeedback(
1195 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
1196 std::vector<PacketFeedback> feedback = cc.GetTransportFeedbackVector();
elad.alonec304f92017-03-08 05:03:53 -08001197 SortPacketFeedbackVector(&feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +02001198 rtc::Optional<uint32_t> bitrate_bps;
1199 if (!feedback.empty()) {
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001200#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1201 acknowledged_bitrate_estimator.IncomingPacketFeedbackVector(feedback);
1202#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
elad.alonf9490002017-03-06 05:32:21 -08001203 for (const PacketFeedback& packet : feedback)
Stefan Holmer60e43462016-09-07 09:58:20 +02001204 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1205 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
1206 }
Stefan Holmer60e43462016-09-07 09:58:20 +02001207 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1208 1000000;
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001209 float y = bitrate_bps.value_or(0) / 1000;
Stefan Holmer60e43462016-09-07 09:58:20 +02001210 acked_time_series.points.emplace_back(x, y);
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001211#if !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
1212 y = acknowledged_bitrate_estimator.bitrate_bps().value_or(0) / 1000;
1213 acked_estimate_time_series.points.emplace_back(x, y);
1214#endif // !(BWE_TEST_LOGGING_COMPILE_TIME_ENABLE)
Stefan Holmer13181032016-07-29 14:48:54 +02001215 }
1216 ++rtcp_iterator;
1217 }
1218 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001219 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001220 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1221 if (rtp.header.extension.hasTransportSequenceNumber) {
1222 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
elad.alond12a8e12017-03-23 11:04:48 -07001223 cc.AddPacket(rtp.header.ssrc,
1224 rtp.header.extension.transportSequenceNumber,
elad.alon5bbf43f2017-03-09 06:40:08 -08001225 rtp.total_length, PacedPacketInfo());
Stefan Holmer13181032016-07-29 14:48:54 +02001226 rtc::SentPacket sent_packet(
1227 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1228 cc.OnSentPacket(sent_packet);
1229 }
1230 ++rtp_iterator;
1231 }
stefanc3de0332016-08-02 07:22:17 -07001232 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1233 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001234 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001235 }
Stefan Holmer492ee282016-10-27 17:19:20 +02001236 if (observer.GetAndResetBitrateUpdated() ||
1237 time_us - last_update_us >= 1e6) {
Stefan Holmer13181032016-07-29 14:48:54 +02001238 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +02001239 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1240 1000000;
1241 time_series.points.emplace_back(x, y);
Stefan Holmer492ee282016-10-27 17:19:20 +02001242 last_update_us = time_us;
Stefan Holmer13181032016-07-29 14:48:54 +02001243 }
1244 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1245 }
1246 // Add the data set to the plot.
philipel35ba9bd2017-04-19 05:58:51 -07001247 plot->AppendTimeSeries(std::move(time_series));
1248 plot->AppendTimeSeries(std::move(acked_time_series));
Bjorn Terelius6984ad22017-10-24 12:19:45 +02001249 plot->AppendTimeSeriesIfNotEmpty(std::move(acked_estimate_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001250
tereliusdc35dcd2016-08-01 12:03:27 -07001251 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1252 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001253 plot->SetTitle("Simulated send-side BWE behavior");
1254}
1255
1256void EventLogAnalyzer::CreateReceiveSideBweSimulationGraph(Plot* plot) {
1257 class RembInterceptingPacketRouter : public PacketRouter {
1258 public:
1259 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
1260 uint32_t bitrate_bps) override {
1261 last_bitrate_bps_ = bitrate_bps;
1262 bitrate_updated_ = true;
1263 PacketRouter::OnReceiveBitrateChanged(ssrcs, bitrate_bps);
1264 }
1265 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
1266 bool GetAndResetBitrateUpdated() {
1267 bool bitrate_updated = bitrate_updated_;
1268 bitrate_updated_ = false;
1269 return bitrate_updated;
1270 }
1271
1272 private:
1273 uint32_t last_bitrate_bps_;
1274 bool bitrate_updated_;
1275 };
1276
1277 std::multimap<uint64_t, const LoggedRtpPacket*> incoming_rtp;
1278
1279 for (const auto& kv : rtp_packets_) {
1280 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket &&
1281 IsVideoSsrc(kv.first)) {
1282 for (const LoggedRtpPacket& rtp_packet : kv.second)
1283 incoming_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1284 }
1285 }
1286
1287 SimulatedClock clock(0);
1288 RembInterceptingPacketRouter packet_router;
1289 // TODO(terelius): The PacketRrouter is the used as the RemoteBitrateObserver.
1290 // Is this intentional?
1291 ReceiveSideCongestionController rscc(&clock, &packet_router);
1292 // TODO(holmer): Log the call config and use that here instead.
1293 // static const uint32_t kDefaultStartBitrateBps = 300000;
1294 // rscc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1295
1296 TimeSeries time_series("Receive side estimate", LINE_DOT_GRAPH);
1297 TimeSeries acked_time_series("Received bitrate", LINE_GRAPH);
1298
1299 RateStatistics acked_bitrate(250, 8000);
1300 int64_t last_update_us = 0;
1301 for (const auto& kv : incoming_rtp) {
1302 const LoggedRtpPacket& packet = *kv.second;
1303 int64_t arrival_time_ms = packet.timestamp / 1000;
1304 size_t payload = packet.total_length; /*Should subtract header?*/
1305 clock.AdvanceTimeMicroseconds(packet.timestamp -
1306 clock.TimeInMicroseconds());
1307 rscc.OnReceivedPacket(arrival_time_ms, payload, packet.header);
1308 acked_bitrate.Update(payload, arrival_time_ms);
1309 rtc::Optional<uint32_t> bitrate_bps = acked_bitrate.Rate(arrival_time_ms);
1310 if (bitrate_bps) {
1311 uint32_t y = *bitrate_bps / 1000;
1312 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1313 1000000;
1314 acked_time_series.points.emplace_back(x, y);
1315 }
1316 if (packet_router.GetAndResetBitrateUpdated() ||
1317 clock.TimeInMicroseconds() - last_update_us >= 1e6) {
1318 uint32_t y = packet_router.last_bitrate_bps() / 1000;
1319 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1320 1000000;
1321 time_series.points.emplace_back(x, y);
1322 last_update_us = clock.TimeInMicroseconds();
1323 }
1324 }
1325 // Add the data set to the plot.
1326 plot->AppendTimeSeries(std::move(time_series));
1327 plot->AppendTimeSeries(std::move(acked_time_series));
1328
1329 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1330 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1331 plot->SetTitle("Simulated receive-side BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001332}
1333
tereliuse34c19c2016-08-15 08:47:14 -07001334void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanff421622017-04-20 03:24:01 -07001335 std::multimap<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1336 std::multimap<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
stefanc3de0332016-08-02 07:22:17 -07001337
1338 for (const auto& kv : rtp_packets_) {
1339 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1340 for (const LoggedRtpPacket& rtp_packet : kv.second)
1341 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1342 }
1343 }
1344
1345 for (const auto& kv : rtcp_packets_) {
1346 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1347 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1348 incoming_rtcp.insert(
1349 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1350 }
1351 }
1352
1353 SimulatedClock clock(0);
elad.alon5bbf43f2017-03-09 06:40:08 -08001354 TransportFeedbackAdapter feedback_adapter(&clock);
stefanc3de0332016-08-02 07:22:17 -07001355
stefana0a8ed72017-09-06 02:06:32 -07001356 TimeSeries late_feedback_series("Late feedback results.", DOT_GRAPH);
terelius23c595a2017-03-15 01:59:12 -07001357 TimeSeries time_series("Network Delay Change", LINE_DOT_GRAPH);
stefanc3de0332016-08-02 07:22:17 -07001358 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1359
1360 auto rtp_iterator = outgoing_rtp.begin();
1361 auto rtcp_iterator = incoming_rtcp.begin();
1362
1363 auto NextRtpTime = [&]() {
1364 if (rtp_iterator != outgoing_rtp.end())
1365 return static_cast<int64_t>(rtp_iterator->first);
1366 return std::numeric_limits<int64_t>::max();
1367 };
1368
1369 auto NextRtcpTime = [&]() {
1370 if (rtcp_iterator != incoming_rtcp.end())
1371 return static_cast<int64_t>(rtcp_iterator->first);
1372 return std::numeric_limits<int64_t>::max();
1373 };
1374
1375 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
stefana0a8ed72017-09-06 02:06:32 -07001376 int64_t prev_y = 0;
stefanc3de0332016-08-02 07:22:17 -07001377 while (time_us != std::numeric_limits<int64_t>::max()) {
1378 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1379 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1380 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1381 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1382 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001383 feedback_adapter.OnTransportFeedback(
1384 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
elad.alonf9490002017-03-06 05:32:21 -08001385 std::vector<PacketFeedback> feedback =
1386 feedback_adapter.GetTransportFeedbackVector();
elad.alonec304f92017-03-08 05:03:53 -08001387 SortPacketFeedbackVector(&feedback);
elad.alonf9490002017-03-06 05:32:21 -08001388 for (const PacketFeedback& packet : feedback) {
stefanc3de0332016-08-02 07:22:17 -07001389 float x =
1390 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1391 1000000;
stefana0a8ed72017-09-06 02:06:32 -07001392 if (packet.send_time_ms == -1) {
1393 late_feedback_series.points.emplace_back(x, prev_y);
1394 continue;
1395 }
1396 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1397 prev_y = y;
stefanc3de0332016-08-02 07:22:17 -07001398 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1399 time_series.points.emplace_back(x, y);
1400 }
1401 }
1402 ++rtcp_iterator;
1403 }
1404 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1405 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1406 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1407 if (rtp.header.extension.hasTransportSequenceNumber) {
1408 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
elad.alond12a8e12017-03-23 11:04:48 -07001409 feedback_adapter.AddPacket(rtp.header.ssrc,
1410 rtp.header.extension.transportSequenceNumber,
philipel8aadd502017-02-23 02:56:13 -08001411 rtp.total_length, PacedPacketInfo());
stefanc3de0332016-08-02 07:22:17 -07001412 feedback_adapter.OnSentPacket(
1413 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1414 }
1415 ++rtp_iterator;
1416 }
1417 time_us = std::min(NextRtpTime(), NextRtcpTime());
1418 }
1419 // We assume that the base network delay (w/o queues) is the min delay
1420 // observed during the call.
1421 for (TimeSeriesPoint& point : time_series.points)
1422 point.y -= estimated_base_delay_ms;
stefana0a8ed72017-09-06 02:06:32 -07001423 for (TimeSeriesPoint& point : late_feedback_series.points)
1424 point.y -= estimated_base_delay_ms;
stefanc3de0332016-08-02 07:22:17 -07001425 // Add the data set to the plot.
stefana0a8ed72017-09-06 02:06:32 -07001426 plot->AppendTimeSeriesIfNotEmpty(std::move(time_series));
1427 plot->AppendTimeSeriesIfNotEmpty(std::move(late_feedback_series));
stefanc3de0332016-08-02 07:22:17 -07001428
1429 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1430 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1431 plot->SetTitle("Network Delay Change.");
1432}
stefan08383272016-12-20 08:51:52 -08001433
1434std::vector<std::pair<int64_t, int64_t>> EventLogAnalyzer::GetFrameTimestamps()
1435 const {
1436 std::vector<std::pair<int64_t, int64_t>> timestamps;
1437 size_t largest_stream_size = 0;
1438 const std::vector<LoggedRtpPacket>* largest_video_stream = nullptr;
1439 // Find the incoming video stream with the most number of packets that is
1440 // not rtx.
1441 for (const auto& kv : rtp_packets_) {
1442 if (kv.first.GetDirection() == kIncomingPacket &&
1443 video_ssrcs_.find(kv.first) != video_ssrcs_.end() &&
1444 rtx_ssrcs_.find(kv.first) == rtx_ssrcs_.end() &&
1445 kv.second.size() > largest_stream_size) {
1446 largest_stream_size = kv.second.size();
1447 largest_video_stream = &kv.second;
1448 }
1449 }
1450 if (largest_video_stream == nullptr) {
1451 for (auto& packet : *largest_video_stream) {
1452 if (packet.header.markerBit) {
1453 int64_t capture_ms = packet.header.timestamp / 90.0;
1454 int64_t arrival_ms = packet.timestamp / 1000.0;
1455 timestamps.push_back(std::make_pair(capture_ms, arrival_ms));
1456 }
1457 }
1458 }
1459 return timestamps;
1460}
stefane372d3c2017-02-02 08:04:18 -08001461
1462void EventLogAnalyzer::CreateTimestampGraph(Plot* plot) {
1463 for (const auto& kv : rtp_packets_) {
1464 const std::vector<LoggedRtpPacket>& rtp_packets = kv.second;
1465 StreamId stream_id = kv.first;
1466
1467 {
terelius23c595a2017-03-15 01:59:12 -07001468 TimeSeries timestamp_data(GetStreamName(stream_id) + " capture-time",
1469 LINE_DOT_GRAPH);
stefane372d3c2017-02-02 08:04:18 -08001470 for (LoggedRtpPacket packet : rtp_packets) {
1471 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
1472 float y = packet.header.timestamp;
1473 timestamp_data.points.emplace_back(x, y);
1474 }
philipel35ba9bd2017-04-19 05:58:51 -07001475 plot->AppendTimeSeries(std::move(timestamp_data));
stefane372d3c2017-02-02 08:04:18 -08001476 }
1477
1478 {
1479 auto kv = rtcp_packets_.find(stream_id);
1480 if (kv != rtcp_packets_.end()) {
1481 const auto& packets = kv->second;
terelius23c595a2017-03-15 01:59:12 -07001482 TimeSeries timestamp_data(
1483 GetStreamName(stream_id) + " rtcp capture-time", LINE_DOT_GRAPH);
stefane372d3c2017-02-02 08:04:18 -08001484 for (const LoggedRtcpPacket& rtcp : packets) {
1485 if (rtcp.type != kRtcpSr)
1486 continue;
1487 rtcp::SenderReport* sr;
1488 sr = static_cast<rtcp::SenderReport*>(rtcp.packet.get());
1489 float x = static_cast<float>(rtcp.timestamp - begin_time_) / 1000000;
1490 float y = sr->rtp_timestamp();
1491 timestamp_data.points.emplace_back(x, y);
1492 }
philipel35ba9bd2017-04-19 05:58:51 -07001493 plot->AppendTimeSeries(std::move(timestamp_data));
stefane372d3c2017-02-02 08:04:18 -08001494 }
1495 }
1496 }
1497
1498 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1499 plot->SetSuggestedYAxis(0, 1, "Timestamp (90khz)", kBottomMargin, kTopMargin);
1500 plot->SetTitle("Timestamps");
1501}
michaelt6e5b2192017-02-22 07:33:27 -08001502
1503void EventLogAnalyzer::CreateAudioEncoderTargetBitrateGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001504 TimeSeries time_series("Audio encoder target bitrate", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001505 ProcessPoints<AudioNetworkAdaptationEvent>(
1506 [](const AudioNetworkAdaptationEvent& ana_event) -> rtc::Optional<float> {
michaelt6e5b2192017-02-22 07:33:27 -08001507 if (ana_event.config.bitrate_bps)
1508 return rtc::Optional<float>(
1509 static_cast<float>(*ana_event.config.bitrate_bps));
1510 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001511 },
philipel35ba9bd2017-04-19 05:58:51 -07001512 audio_network_adaptation_events_, begin_time_, &time_series);
1513 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001514 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1515 plot->SetSuggestedYAxis(0, 1, "Bitrate (bps)", kBottomMargin, kTopMargin);
1516 plot->SetTitle("Reported audio encoder target bitrate");
1517}
1518
1519void EventLogAnalyzer::CreateAudioEncoderFrameLengthGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001520 TimeSeries time_series("Audio encoder frame length", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001521 ProcessPoints<AudioNetworkAdaptationEvent>(
1522 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001523 if (ana_event.config.frame_length_ms)
1524 return rtc::Optional<float>(
1525 static_cast<float>(*ana_event.config.frame_length_ms));
1526 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001527 },
philipel35ba9bd2017-04-19 05:58:51 -07001528 audio_network_adaptation_events_, begin_time_, &time_series);
1529 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001530 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1531 plot->SetSuggestedYAxis(0, 1, "Frame length (ms)", kBottomMargin, kTopMargin);
1532 plot->SetTitle("Reported audio encoder frame length");
1533}
1534
terelius2ee076d2017-08-15 02:04:02 -07001535void EventLogAnalyzer::CreateAudioEncoderPacketLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001536 TimeSeries time_series("Audio encoder uplink packet loss fraction",
1537 LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001538 ProcessPoints<AudioNetworkAdaptationEvent>(
1539 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001540 if (ana_event.config.uplink_packet_loss_fraction)
1541 return rtc::Optional<float>(static_cast<float>(
1542 *ana_event.config.uplink_packet_loss_fraction));
1543 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001544 },
philipel35ba9bd2017-04-19 05:58:51 -07001545 audio_network_adaptation_events_, begin_time_, &time_series);
1546 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001547 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1548 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
1549 kTopMargin);
1550 plot->SetTitle("Reported audio encoder lost packets");
1551}
1552
1553void EventLogAnalyzer::CreateAudioEncoderEnableFecGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001554 TimeSeries time_series("Audio encoder FEC", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001555 ProcessPoints<AudioNetworkAdaptationEvent>(
1556 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001557 if (ana_event.config.enable_fec)
1558 return rtc::Optional<float>(
1559 static_cast<float>(*ana_event.config.enable_fec));
1560 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001561 },
philipel35ba9bd2017-04-19 05:58:51 -07001562 audio_network_adaptation_events_, begin_time_, &time_series);
1563 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001564 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1565 plot->SetSuggestedYAxis(0, 1, "FEC (false/true)", kBottomMargin, kTopMargin);
1566 plot->SetTitle("Reported audio encoder FEC");
1567}
1568
1569void EventLogAnalyzer::CreateAudioEncoderEnableDtxGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001570 TimeSeries time_series("Audio encoder DTX", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001571 ProcessPoints<AudioNetworkAdaptationEvent>(
1572 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001573 if (ana_event.config.enable_dtx)
1574 return rtc::Optional<float>(
1575 static_cast<float>(*ana_event.config.enable_dtx));
1576 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001577 },
philipel35ba9bd2017-04-19 05:58:51 -07001578 audio_network_adaptation_events_, begin_time_, &time_series);
1579 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001580 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1581 plot->SetSuggestedYAxis(0, 1, "DTX (false/true)", kBottomMargin, kTopMargin);
1582 plot->SetTitle("Reported audio encoder DTX");
1583}
1584
1585void EventLogAnalyzer::CreateAudioEncoderNumChannelsGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001586 TimeSeries time_series("Audio encoder number of channels", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001587 ProcessPoints<AudioNetworkAdaptationEvent>(
1588 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001589 if (ana_event.config.num_channels)
1590 return rtc::Optional<float>(
1591 static_cast<float>(*ana_event.config.num_channels));
1592 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001593 },
philipel35ba9bd2017-04-19 05:58:51 -07001594 audio_network_adaptation_events_, begin_time_, &time_series);
1595 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001596 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1597 plot->SetSuggestedYAxis(0, 1, "Number of channels (1 (mono)/2 (stereo))",
1598 kBottomMargin, kTopMargin);
1599 plot->SetTitle("Reported audio encoder number of channels");
1600}
henrik.lundin3c938fc2017-06-14 06:09:58 -07001601
1602class NetEqStreamInput : public test::NetEqInput {
1603 public:
1604 // Does not take any ownership, and all pointers must refer to valid objects
1605 // that outlive the one constructed.
1606 NetEqStreamInput(const std::vector<LoggedRtpPacket>* packet_stream,
1607 const std::vector<uint64_t>* output_events_us,
1608 rtc::Optional<uint64_t> end_time_us)
1609 : packet_stream_(*packet_stream),
1610 packet_stream_it_(packet_stream_.begin()),
1611 output_events_us_it_(output_events_us->begin()),
1612 output_events_us_end_(output_events_us->end()),
1613 end_time_us_(end_time_us) {
1614 RTC_DCHECK(packet_stream);
1615 RTC_DCHECK(output_events_us);
1616 }
1617
1618 rtc::Optional<int64_t> NextPacketTime() const override {
1619 if (packet_stream_it_ == packet_stream_.end()) {
1620 return rtc::Optional<int64_t>();
1621 }
1622 if (end_time_us_ && packet_stream_it_->timestamp > *end_time_us_) {
1623 return rtc::Optional<int64_t>();
1624 }
1625 // Convert from us to ms.
1626 return rtc::Optional<int64_t>(packet_stream_it_->timestamp / 1000);
1627 }
1628
1629 rtc::Optional<int64_t> NextOutputEventTime() const override {
1630 if (output_events_us_it_ == output_events_us_end_) {
1631 return rtc::Optional<int64_t>();
1632 }
1633 if (end_time_us_ && *output_events_us_it_ > *end_time_us_) {
1634 return rtc::Optional<int64_t>();
1635 }
1636 // Convert from us to ms.
1637 return rtc::Optional<int64_t>(
1638 rtc::checked_cast<int64_t>(*output_events_us_it_ / 1000));
1639 }
1640
1641 std::unique_ptr<PacketData> PopPacket() override {
1642 if (packet_stream_it_ == packet_stream_.end()) {
1643 return std::unique_ptr<PacketData>();
1644 }
1645 std::unique_ptr<PacketData> packet_data(new PacketData());
1646 packet_data->header = packet_stream_it_->header;
1647 // Convert from us to ms.
1648 packet_data->time_ms = packet_stream_it_->timestamp / 1000.0;
1649
1650 // This is a header-only "dummy" packet. Set the payload to all zeros, with
1651 // length according to the virtual length.
1652 packet_data->payload.SetSize(packet_stream_it_->total_length);
1653 std::fill_n(packet_data->payload.data(), packet_data->payload.size(), 0);
1654
1655 ++packet_stream_it_;
1656 return packet_data;
1657 }
1658
1659 void AdvanceOutputEvent() override {
1660 if (output_events_us_it_ != output_events_us_end_) {
1661 ++output_events_us_it_;
1662 }
1663 }
1664
1665 bool ended() const override { return !NextEventTime(); }
1666
1667 rtc::Optional<RTPHeader> NextHeader() const override {
1668 if (packet_stream_it_ == packet_stream_.end()) {
1669 return rtc::Optional<RTPHeader>();
1670 }
1671 return rtc::Optional<RTPHeader>(packet_stream_it_->header);
1672 }
1673
1674 private:
1675 const std::vector<LoggedRtpPacket>& packet_stream_;
1676 std::vector<LoggedRtpPacket>::const_iterator packet_stream_it_;
1677 std::vector<uint64_t>::const_iterator output_events_us_it_;
1678 const std::vector<uint64_t>::const_iterator output_events_us_end_;
1679 const rtc::Optional<uint64_t> end_time_us_;
1680};
1681
1682namespace {
1683// Creates a NetEq test object and all necessary input and output helpers. Runs
1684// the test and returns the NetEqDelayAnalyzer object that was used to
1685// instrument the test.
1686std::unique_ptr<test::NetEqDelayAnalyzer> CreateNetEqTestAndRun(
1687 const std::vector<LoggedRtpPacket>* packet_stream,
1688 const std::vector<uint64_t>* output_events_us,
1689 rtc::Optional<uint64_t> end_time_us,
1690 const std::string& replacement_file_name,
1691 int file_sample_rate_hz) {
1692 std::unique_ptr<test::NetEqInput> input(
1693 new NetEqStreamInput(packet_stream, output_events_us, end_time_us));
1694
1695 constexpr int kReplacementPt = 127;
1696 std::set<uint8_t> cn_types;
1697 std::set<uint8_t> forbidden_types;
1698 input.reset(new test::NetEqReplacementInput(std::move(input), kReplacementPt,
1699 cn_types, forbidden_types));
1700
1701 NetEq::Config config;
1702 config.max_packets_in_buffer = 200;
1703 config.enable_fast_accelerate = true;
1704
1705 std::unique_ptr<test::VoidAudioSink> output(new test::VoidAudioSink());
1706
1707 test::NetEqTest::DecoderMap codecs;
1708
1709 // Create a "replacement decoder" that produces the decoded audio by reading
1710 // from a file rather than from the encoded payloads.
1711 std::unique_ptr<test::ResampleInputAudioFile> replacement_file(
1712 new test::ResampleInputAudioFile(replacement_file_name,
1713 file_sample_rate_hz));
1714 replacement_file->set_output_rate_hz(48000);
1715 std::unique_ptr<AudioDecoder> replacement_decoder(
1716 new test::FakeDecodeFromFile(std::move(replacement_file), 48000, false));
1717 test::NetEqTest::ExtDecoderMap ext_codecs;
1718 ext_codecs[kReplacementPt] = {replacement_decoder.get(),
1719 NetEqDecoder::kDecoderArbitrary,
1720 "replacement codec"};
1721
1722 std::unique_ptr<test::NetEqDelayAnalyzer> delay_cb(
1723 new test::NetEqDelayAnalyzer);
1724 test::DefaultNetEqTestErrorCallback error_cb;
1725 test::NetEqTest::Callbacks callbacks;
1726 callbacks.error_callback = &error_cb;
1727 callbacks.post_insert_packet = delay_cb.get();
1728 callbacks.get_audio_callback = delay_cb.get();
1729
1730 test::NetEqTest test(config, codecs, ext_codecs, std::move(input),
1731 std::move(output), callbacks);
1732 test.Run();
1733 return delay_cb;
1734}
1735} // namespace
1736
1737// Plots the jitter buffer delay profile. This will plot only for the first
1738// incoming audio SSRC. If the stream contains more than one incoming audio
1739// SSRC, all but the first will be ignored.
1740void EventLogAnalyzer::CreateAudioJitterBufferGraph(
1741 const std::string& replacement_file_name,
1742 int file_sample_rate_hz,
1743 Plot* plot) {
1744 const auto& incoming_audio_kv = std::find_if(
1745 rtp_packets_.begin(), rtp_packets_.end(),
1746 [this](std::pair<StreamId, std::vector<LoggedRtpPacket>> kv) {
1747 return kv.first.GetDirection() == kIncomingPacket &&
1748 this->IsAudioSsrc(kv.first);
1749 });
1750 if (incoming_audio_kv == rtp_packets_.end()) {
1751 // No incoming audio stream found.
1752 return;
1753 }
1754
1755 const uint32_t ssrc = incoming_audio_kv->first.GetSsrc();
1756
1757 std::map<uint32_t, std::vector<uint64_t>>::const_iterator output_events_it =
1758 audio_playout_events_.find(ssrc);
1759 if (output_events_it == audio_playout_events_.end()) {
1760 // Could not find output events with SSRC matching the input audio stream.
1761 // Using the first available stream of output events.
1762 output_events_it = audio_playout_events_.cbegin();
1763 }
1764
1765 rtc::Optional<uint64_t> end_time_us =
1766 log_segments_.empty()
1767 ? rtc::Optional<uint64_t>()
1768 : rtc::Optional<uint64_t>(log_segments_.front().second);
1769
1770 auto delay_cb = CreateNetEqTestAndRun(
1771 &incoming_audio_kv->second, &output_events_it->second, end_time_us,
1772 replacement_file_name, file_sample_rate_hz);
1773
1774 std::vector<float> send_times_s;
1775 std::vector<float> arrival_delay_ms;
1776 std::vector<float> corrected_arrival_delay_ms;
1777 std::vector<rtc::Optional<float>> playout_delay_ms;
1778 std::vector<rtc::Optional<float>> target_delay_ms;
1779 delay_cb->CreateGraphs(&send_times_s, &arrival_delay_ms,
1780 &corrected_arrival_delay_ms, &playout_delay_ms,
1781 &target_delay_ms);
1782 RTC_DCHECK_EQ(send_times_s.size(), arrival_delay_ms.size());
1783 RTC_DCHECK_EQ(send_times_s.size(), corrected_arrival_delay_ms.size());
1784 RTC_DCHECK_EQ(send_times_s.size(), playout_delay_ms.size());
1785 RTC_DCHECK_EQ(send_times_s.size(), target_delay_ms.size());
1786
1787 std::map<StreamId, TimeSeries> time_series_packet_arrival;
1788 std::map<StreamId, TimeSeries> time_series_relative_packet_arrival;
1789 std::map<StreamId, TimeSeries> time_series_play_time;
1790 std::map<StreamId, TimeSeries> time_series_target_time;
1791 float min_y_axis = 0.f;
1792 float max_y_axis = 0.f;
1793 const StreamId stream_id = incoming_audio_kv->first;
1794 for (size_t i = 0; i < send_times_s.size(); ++i) {
1795 time_series_packet_arrival[stream_id].points.emplace_back(
1796 TimeSeriesPoint(send_times_s[i], arrival_delay_ms[i]));
1797 time_series_relative_packet_arrival[stream_id].points.emplace_back(
1798 TimeSeriesPoint(send_times_s[i], corrected_arrival_delay_ms[i]));
1799 min_y_axis = std::min(min_y_axis, corrected_arrival_delay_ms[i]);
1800 max_y_axis = std::max(max_y_axis, corrected_arrival_delay_ms[i]);
1801 if (playout_delay_ms[i]) {
1802 time_series_play_time[stream_id].points.emplace_back(
1803 TimeSeriesPoint(send_times_s[i], *playout_delay_ms[i]));
1804 min_y_axis = std::min(min_y_axis, *playout_delay_ms[i]);
1805 max_y_axis = std::max(max_y_axis, *playout_delay_ms[i]);
1806 }
1807 if (target_delay_ms[i]) {
1808 time_series_target_time[stream_id].points.emplace_back(
1809 TimeSeriesPoint(send_times_s[i], *target_delay_ms[i]));
1810 min_y_axis = std::min(min_y_axis, *target_delay_ms[i]);
1811 max_y_axis = std::max(max_y_axis, *target_delay_ms[i]);
1812 }
1813 }
1814
1815 // This code is adapted for a single stream. The creation of the streams above
1816 // guarantee that no more than one steam is included. If multiple streams are
1817 // to be plotted, they should likely be given distinct labels below.
1818 RTC_DCHECK_EQ(time_series_relative_packet_arrival.size(), 1);
1819 for (auto& series : time_series_relative_packet_arrival) {
1820 series.second.label = "Relative packet arrival delay";
1821 series.second.style = LINE_GRAPH;
1822 plot->AppendTimeSeries(std::move(series.second));
1823 }
1824 RTC_DCHECK_EQ(time_series_play_time.size(), 1);
1825 for (auto& series : time_series_play_time) {
1826 series.second.label = "Playout delay";
1827 series.second.style = LINE_GRAPH;
1828 plot->AppendTimeSeries(std::move(series.second));
1829 }
1830 RTC_DCHECK_EQ(time_series_target_time.size(), 1);
1831 for (auto& series : time_series_target_time) {
1832 series.second.label = "Target delay";
1833 series.second.style = LINE_DOT_GRAPH;
1834 plot->AppendTimeSeries(std::move(series.second));
1835 }
1836
1837 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1838 plot->SetYAxis(min_y_axis, max_y_axis, "Relative delay (ms)", kBottomMargin,
1839 kTopMargin);
1840 plot->SetTitle("NetEq timing");
1841}
terelius54ce6802016-07-13 06:44:41 -07001842} // namespace plotting
1843} // namespace webrtc