blob: 4ec1a2972a5db01c38ebeacc64733866890b4219 [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
11#include "webrtc/tools/event_log_visualizer/analyzer.h"
12
13#include <algorithm>
14#include <limits>
15#include <map>
16#include <sstream>
17#include <string>
18#include <utility>
19
kjellandera69d9732016-08-31 07:33:05 -070020#include "webrtc/api/call/audio_receive_stream.h"
21#include "webrtc/api/call/audio_send_stream.h"
terelius54ce6802016-07-13 06:44:41 -070022#include "webrtc/base/checks.h"
stefan6a850c32016-07-29 10:28:08 -070023#include "webrtc/base/logging.h"
Stefan Holmer60e43462016-09-07 09:58:20 +020024#include "webrtc/base/rate_statistics.h"
terelius54ce6802016-07-13 06:44:41 -070025#include "webrtc/call.h"
26#include "webrtc/common_types.h"
Stefan Holmer280de9e2016-09-30 10:06:51 +020027#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
Stefan Holmer13181032016-07-29 14:48:54 +020028#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
terelius54ce6802016-07-13 06:44:41 -070029#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
30#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
31#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
danilchapbf369fe2016-10-07 07:39:54 -070032#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
Stefan Holmer13181032016-07-29 14:48:54 +020033#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
terelius54ce6802016-07-13 06:44:41 -070034#include "webrtc/video_receive_stream.h"
35#include "webrtc/video_send_stream.h"
36
tereliusdc35dcd2016-08-01 12:03:27 -070037namespace webrtc {
38namespace plotting {
39
terelius54ce6802016-07-13 06:44:41 -070040namespace {
41
42std::string SsrcToString(uint32_t ssrc) {
43 std::stringstream ss;
44 ss << "SSRC " << ssrc;
45 return ss.str();
46}
47
48// Checks whether an SSRC is contained in the list of desired SSRCs.
49// Note that an empty SSRC list matches every SSRC.
50bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
51 if (desired_ssrc.size() == 0)
52 return true;
53 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
54 desired_ssrc.end();
55}
56
57double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
58 // The timestamp is a fixed point representation with 6 bits for seconds
59 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
60 // time in seconds and then multiply by 1000000 to convert to microseconds.
61 static constexpr double kTimestampToMicroSec =
tereliusccbbf8d2016-08-10 07:34:28 -070062 1000000.0 / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -070063 return abs_send_time * kTimestampToMicroSec;
64}
65
66// Computes the difference |later| - |earlier| where |later| and |earlier|
67// are counters that wrap at |modulus|. The difference is chosen to have the
68// least absolute value. For example if |modulus| is 8, then the difference will
69// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
70// be in [-4, 4].
71int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
72 RTC_DCHECK_LE(1, modulus);
73 RTC_DCHECK_LT(later, modulus);
74 RTC_DCHECK_LT(earlier, modulus);
75 int64_t difference =
76 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
77 int64_t max_difference = modulus / 2;
78 int64_t min_difference = max_difference - modulus + 1;
79 if (difference > max_difference) {
80 difference -= modulus;
81 }
82 if (difference < min_difference) {
83 difference += modulus;
84 }
terelius6addf492016-08-23 17:34:07 -070085 if (difference > max_difference / 2 || difference < min_difference / 2) {
86 LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
87 << " expected to be in the range (" << min_difference / 2
88 << "," << max_difference / 2 << ") but is " << difference
89 << ". Correct unwrapping is uncertain.";
90 }
terelius54ce6802016-07-13 06:44:41 -070091 return difference;
92}
93
stefan6a850c32016-07-29 10:28:08 -070094void RegisterHeaderExtensions(
95 const std::vector<webrtc::RtpExtension>& extensions,
96 webrtc::RtpHeaderExtensionMap* extension_map) {
97 extension_map->Erase();
98 for (const webrtc::RtpExtension& extension : extensions) {
99 extension_map->Register(webrtc::StringToRtpExtensionType(extension.uri),
100 extension.id);
101 }
102}
103
ivocaac9d6f2016-09-22 07:01:47 -0700104// Return default values for header extensions, to use on streams without stored
105// mapping data. Currently this only applies to audio streams, since the mapping
106// is not stored in the event log.
107// TODO(ivoc): Remove this once this mapping is stored in the event log for
108// audio streams. Tracking bug: webrtc:6399
109webrtc::RtpHeaderExtensionMap GetDefaultHeaderExtensionMap() {
110 webrtc::RtpHeaderExtensionMap default_map;
111 default_map.Register(
112 webrtc::StringToRtpExtensionType(webrtc::RtpExtension::kAudioLevelUri),
113 webrtc::RtpExtension::kAudioLevelDefaultId);
114 default_map.Register(
115 webrtc::StringToRtpExtensionType(webrtc::RtpExtension::kAbsSendTimeUri),
116 webrtc::RtpExtension::kAbsSendTimeDefaultId);
117 return default_map;
118}
119
tereliusdc35dcd2016-08-01 12:03:27 -0700120constexpr float kLeftMargin = 0.01f;
121constexpr float kRightMargin = 0.02f;
122constexpr float kBottomMargin = 0.02f;
123constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700124
terelius6addf492016-08-23 17:34:07 -0700125class PacketSizeBytes {
126 public:
127 using DataType = LoggedRtpPacket;
128 using ResultType = size_t;
129 size_t operator()(const LoggedRtpPacket& packet) {
130 return packet.total_length;
131 }
132};
133
134class SequenceNumberDiff {
135 public:
136 using DataType = LoggedRtpPacket;
137 using ResultType = int64_t;
138 int64_t operator()(const LoggedRtpPacket& old_packet,
139 const LoggedRtpPacket& new_packet) {
140 return WrappingDifference(new_packet.header.sequenceNumber,
141 old_packet.header.sequenceNumber, 1ul << 16);
142 }
143};
144
tereliusccbbf8d2016-08-10 07:34:28 -0700145class NetworkDelayDiff {
146 public:
147 class AbsSendTime {
148 public:
149 using DataType = LoggedRtpPacket;
150 using ResultType = double;
151 double operator()(const LoggedRtpPacket& old_packet,
152 const LoggedRtpPacket& new_packet) {
153 if (old_packet.header.extension.hasAbsoluteSendTime &&
154 new_packet.header.extension.hasAbsoluteSendTime) {
155 int64_t send_time_diff = WrappingDifference(
156 new_packet.header.extension.absoluteSendTime,
157 old_packet.header.extension.absoluteSendTime, 1ul << 24);
158 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
159 return static_cast<double>(recv_time_diff -
160 AbsSendTimeToMicroseconds(send_time_diff)) /
161 1000;
162 } else {
163 return 0;
164 }
165 }
166 };
167
168 class CaptureTime {
169 public:
170 using DataType = LoggedRtpPacket;
171 using ResultType = double;
172 double operator()(const LoggedRtpPacket& old_packet,
173 const LoggedRtpPacket& new_packet) {
174 int64_t send_time_diff = WrappingDifference(
175 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
176 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
177
178 const double kVideoSampleRate = 90000;
179 // TODO(terelius): We treat all streams as video for now, even though
180 // audio might be sampled at e.g. 16kHz, because it is really difficult to
181 // figure out the true sampling rate of a stream. The effect is that the
182 // delay will be scaled incorrectly for non-video streams.
183
184 double delay_change =
185 static_cast<double>(recv_time_diff) / 1000 -
186 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
terelius6addf492016-08-23 17:34:07 -0700187 if (delay_change < -10000 || 10000 < delay_change) {
188 LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
189 LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
190 << ", received time " << old_packet.timestamp;
191 LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
192 << ", received time " << new_packet.timestamp;
193 LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
194 << static_cast<double>(recv_time_diff) / 1000000 << "s";
195 LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
196 << static_cast<double>(send_time_diff) /
197 kVideoSampleRate
198 << "s";
199 }
tereliusccbbf8d2016-08-10 07:34:28 -0700200 return delay_change;
201 }
202 };
203};
204
205template <typename Extractor>
206class Accumulated {
207 public:
208 using DataType = typename Extractor::DataType;
209 using ResultType = typename Extractor::ResultType;
210 ResultType operator()(const DataType& old_packet,
211 const DataType& new_packet) {
212 sum += extract(old_packet, new_packet);
213 return sum;
214 }
215
216 private:
217 Extractor extract;
218 ResultType sum = 0;
219};
220
terelius6addf492016-08-23 17:34:07 -0700221// For each element in data, use |Extractor| to extract a y-coordinate and
222// store the result in a TimeSeries.
223template <typename Extractor>
224void Pointwise(const std::vector<typename Extractor::DataType>& data,
225 uint64_t begin_time,
226 TimeSeries* result) {
227 Extractor extract;
228 for (size_t i = 0; i < data.size(); i++) {
229 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
230 float y = extract(data[i]);
231 result->points.emplace_back(x, y);
232 }
233}
234
235// For each pair of adjacent elements in |data|, use |Extractor| to extract a
236// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
237// will be the time of the second element in the pair.
tereliusccbbf8d2016-08-10 07:34:28 -0700238template <typename Extractor>
239void Pairwise(const std::vector<typename Extractor::DataType>& data,
240 uint64_t begin_time,
241 TimeSeries* result) {
242 Extractor extract;
243 for (size_t i = 1; i < data.size(); i++) {
244 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
245 float y = extract(data[i - 1], data[i]);
246 result->points.emplace_back(x, y);
247 }
248}
249
terelius6addf492016-08-23 17:34:07 -0700250// Calculates a moving average of |data| and stores the result in a TimeSeries.
251// A data point is generated every |step| microseconds from |begin_time|
252// to |end_time|. The value of each data point is the average of the data
253// during the preceeding |window_duration_us| microseconds.
254template <typename Extractor>
255void MovingAverage(const std::vector<typename Extractor::DataType>& data,
256 uint64_t begin_time,
257 uint64_t end_time,
258 uint64_t window_duration_us,
259 uint64_t step,
260 float y_scaling,
261 webrtc::plotting::TimeSeries* result) {
262 size_t window_index_begin = 0;
263 size_t window_index_end = 0;
264 typename Extractor::ResultType sum_in_window = 0;
265 Extractor extract;
266
267 for (uint64_t t = begin_time; t < end_time + step; t += step) {
268 while (window_index_end < data.size() &&
269 data[window_index_end].timestamp < t) {
270 sum_in_window += extract(data[window_index_end]);
271 ++window_index_end;
272 }
273 while (window_index_begin < data.size() &&
274 data[window_index_begin].timestamp < t - window_duration_us) {
275 sum_in_window -= extract(data[window_index_begin]);
276 ++window_index_begin;
277 }
278 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
279 float x = static_cast<float>(t - begin_time) / 1000000;
280 float y = sum_in_window / window_duration_s * y_scaling;
281 result->points.emplace_back(x, y);
282 }
283}
284
terelius54ce6802016-07-13 06:44:41 -0700285} // namespace
286
terelius54ce6802016-07-13 06:44:41 -0700287EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
288 : parsed_log_(log), window_duration_(250000), step_(10000) {
289 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
290 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700291
Stefan Holmer13181032016-07-29 14:48:54 +0200292 // Maps a stream identifier consisting of ssrc and direction
terelius88e64e52016-07-19 01:51:06 -0700293 // to the header extensions used by that stream,
294 std::map<StreamId, RtpHeaderExtensionMap> extension_maps;
295
296 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700297 uint8_t header[IP_PACKET_SIZE];
298 size_t header_length;
299 size_t total_length;
300
ivocaac9d6f2016-09-22 07:01:47 -0700301 // Make a default extension map for streams without configuration information.
302 // TODO(ivoc): Once configuration of audio streams is stored in the event log,
303 // this can be removed. Tracking bug: webrtc:6399
304 RtpHeaderExtensionMap default_extension_map = GetDefaultHeaderExtensionMap();
305
terelius54ce6802016-07-13 06:44:41 -0700306 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
307 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700308 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
309 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
310 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700311 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
312 event_type != ParsedRtcEventLog::LOG_START &&
313 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700314 uint64_t timestamp = parsed_log_.GetTimestamp(i);
315 first_timestamp = std::min(first_timestamp, timestamp);
316 last_timestamp = std::max(last_timestamp, timestamp);
317 }
318
319 switch (parsed_log_.GetEventType(i)) {
320 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
321 VideoReceiveStream::Config config(nullptr);
322 parsed_log_.GetVideoReceiveConfig(i, &config);
Stefan Holmer13181032016-07-29 14:48:54 +0200323 StreamId stream(config.rtp.remote_ssrc, kIncomingPacket);
stefan6a850c32016-07-29 10:28:08 -0700324 RegisterHeaderExtensions(config.rtp.extensions,
325 &extension_maps[stream]);
terelius0740a202016-08-08 10:21:04 -0700326 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700327 for (auto kv : config.rtp.rtx) {
328 StreamId rtx_stream(kv.second.ssrc, kIncomingPacket);
329 RegisterHeaderExtensions(config.rtp.extensions,
330 &extension_maps[rtx_stream]);
terelius0740a202016-08-08 10:21:04 -0700331 video_ssrcs_.insert(rtx_stream);
332 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700333 }
334 break;
335 }
336 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
337 VideoSendStream::Config config(nullptr);
338 parsed_log_.GetVideoSendConfig(i, &config);
339 for (auto ssrc : config.rtp.ssrcs) {
Stefan Holmer13181032016-07-29 14:48:54 +0200340 StreamId stream(ssrc, kOutgoingPacket);
stefan6a850c32016-07-29 10:28:08 -0700341 RegisterHeaderExtensions(config.rtp.extensions,
342 &extension_maps[stream]);
terelius0740a202016-08-08 10:21:04 -0700343 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700344 }
345 for (auto ssrc : config.rtp.rtx.ssrcs) {
terelius0740a202016-08-08 10:21:04 -0700346 StreamId rtx_stream(ssrc, kOutgoingPacket);
stefan6a850c32016-07-29 10:28:08 -0700347 RegisterHeaderExtensions(config.rtp.extensions,
terelius0740a202016-08-08 10:21:04 -0700348 &extension_maps[rtx_stream]);
349 video_ssrcs_.insert(rtx_stream);
350 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700351 }
352 break;
353 }
354 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
355 AudioReceiveStream::Config config;
356 // TODO(terelius): Parse the audio configs once we have them.
357 break;
358 }
359 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
360 AudioSendStream::Config config(nullptr);
361 // TODO(terelius): Parse the audio configs once we have them.
362 break;
363 }
364 case ParsedRtcEventLog::RTP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200365 MediaType media_type;
terelius88e64e52016-07-19 01:51:06 -0700366 parsed_log_.GetRtpHeader(i, &direction, &media_type, header,
367 &header_length, &total_length);
368 // Parse header to get SSRC.
369 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
370 RTPHeader parsed_header;
371 rtp_parser.Parse(&parsed_header);
Stefan Holmer13181032016-07-29 14:48:54 +0200372 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700373 // Look up the extension_map and parse it again to get the extensions.
374 if (extension_maps.count(stream) == 1) {
375 RtpHeaderExtensionMap* extension_map = &extension_maps[stream];
376 rtp_parser.Parse(&parsed_header, extension_map);
ivocaac9d6f2016-09-22 07:01:47 -0700377 } else {
378 // Use the default extension map.
379 // TODO(ivoc): Once configuration of audio streams is stored in the
380 // event log, this can be removed.
381 // Tracking bug: webrtc:6399
382 rtp_parser.Parse(&parsed_header, &default_extension_map);
terelius88e64e52016-07-19 01:51:06 -0700383 }
384 uint64_t timestamp = parsed_log_.GetTimestamp(i);
385 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200386 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700387 break;
388 }
389 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200390 uint8_t packet[IP_PACKET_SIZE];
391 MediaType media_type;
392 parsed_log_.GetRtcpPacket(i, &direction, &media_type, packet,
393 &total_length);
394
danilchapbf369fe2016-10-07 07:39:54 -0700395 // Currently feedback is logged twice, both for audio and video.
396 // Only act on one of them.
397 if (media_type == MediaType::VIDEO) {
398 rtcp::CommonHeader header;
399 const uint8_t* packet_end = packet + total_length;
400 for (const uint8_t* block = packet; block < packet_end;
401 block = header.NextPacket()) {
402 RTC_CHECK(header.Parse(block, packet_end - block));
403 if (header.type() == rtcp::TransportFeedback::kPacketType &&
404 header.fmt() == rtcp::TransportFeedback::kFeedbackMessageType) {
405 std::unique_ptr<rtcp::TransportFeedback> rtcp_packet(
406 new rtcp::TransportFeedback());
407 if (rtcp_packet->Parse(header)) {
408 uint32_t ssrc = rtcp_packet->sender_ssrc();
Stefan Holmer13181032016-07-29 14:48:54 +0200409 StreamId stream(ssrc, direction);
410 uint64_t timestamp = parsed_log_.GetTimestamp(i);
411 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
412 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
413 }
Stefan Holmer13181032016-07-29 14:48:54 +0200414 }
Stefan Holmer13181032016-07-29 14:48:54 +0200415 }
Stefan Holmer13181032016-07-29 14:48:54 +0200416 }
terelius88e64e52016-07-19 01:51:06 -0700417 break;
418 }
419 case ParsedRtcEventLog::LOG_START: {
420 break;
421 }
422 case ParsedRtcEventLog::LOG_END: {
423 break;
424 }
425 case ParsedRtcEventLog::BWE_PACKET_LOSS_EVENT: {
terelius8058e582016-07-25 01:32:41 -0700426 BwePacketLossEvent bwe_update;
427 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
428 parsed_log_.GetBwePacketLossEvent(i, &bwe_update.new_bitrate,
429 &bwe_update.fraction_loss,
430 &bwe_update.expected_packets);
431 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700432 break;
433 }
434 case ParsedRtcEventLog::BWE_PACKET_DELAY_EVENT: {
435 break;
436 }
437 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
438 break;
439 }
440 case ParsedRtcEventLog::UNKNOWN_EVENT: {
441 break;
442 }
443 }
terelius54ce6802016-07-13 06:44:41 -0700444 }
terelius88e64e52016-07-19 01:51:06 -0700445
terelius54ce6802016-07-13 06:44:41 -0700446 if (last_timestamp < first_timestamp) {
447 // No useful events in the log.
448 first_timestamp = last_timestamp = 0;
449 }
450 begin_time_ = first_timestamp;
451 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700452 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
terelius54ce6802016-07-13 06:44:41 -0700453}
454
Stefan Holmer13181032016-07-29 14:48:54 +0200455class BitrateObserver : public CongestionController::Observer,
456 public RemoteBitrateObserver {
457 public:
458 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
459
460 void OnNetworkChanged(uint32_t bitrate_bps,
461 uint8_t fraction_loss,
462 int64_t rtt_ms) override {
463 last_bitrate_bps_ = bitrate_bps;
464 bitrate_updated_ = true;
465 }
466
467 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
468 uint32_t bitrate) override {}
469
470 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
471 bool GetAndResetBitrateUpdated() {
472 bool bitrate_updated = bitrate_updated_;
473 bitrate_updated_ = false;
474 return bitrate_updated;
475 }
476
477 private:
478 uint32_t last_bitrate_bps_;
479 bool bitrate_updated_;
480};
481
Stefan Holmer99f8e082016-09-09 13:37:50 +0200482bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700483 return rtx_ssrcs_.count(stream_id) == 1;
484}
485
Stefan Holmer99f8e082016-09-09 13:37:50 +0200486bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700487 return video_ssrcs_.count(stream_id) == 1;
488}
489
Stefan Holmer99f8e082016-09-09 13:37:50 +0200490bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700491 return audio_ssrcs_.count(stream_id) == 1;
492}
493
Stefan Holmer99f8e082016-09-09 13:37:50 +0200494std::string EventLogAnalyzer::GetStreamName(StreamId stream_id) const {
495 std::stringstream name;
496 if (IsAudioSsrc(stream_id)) {
497 name << "Audio ";
498 } else if (IsVideoSsrc(stream_id)) {
499 name << "Video ";
500 } else {
501 name << "Unknown ";
502 }
503 if (IsRtxSsrc(stream_id))
504 name << "RTX ";
ivocaac9d6f2016-09-22 07:01:47 -0700505 if (stream_id.GetDirection() == kIncomingPacket) {
506 name << "(In) ";
507 } else {
508 name << "(Out) ";
509 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200510 name << SsrcToString(stream_id.GetSsrc());
511 return name.str();
512}
513
terelius54ce6802016-07-13 06:44:41 -0700514void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
515 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700516 for (auto& kv : rtp_packets_) {
517 StreamId stream_id = kv.first;
518 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
519 // Filter on direction and SSRC.
520 if (stream_id.GetDirection() != desired_direction ||
521 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
522 continue;
terelius54ce6802016-07-13 06:44:41 -0700523 }
terelius54ce6802016-07-13 06:44:41 -0700524
terelius6addf492016-08-23 17:34:07 -0700525 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200526 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700527 time_series.style = BAR_GRAPH;
528 Pointwise<PacketSizeBytes>(packet_stream, begin_time_, &time_series);
529 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700530 }
531
tereliusdc35dcd2016-08-01 12:03:27 -0700532 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
533 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
534 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700535 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700536 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700537 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700538 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700539 }
540}
541
philipelccd74892016-09-05 02:46:25 -0700542template <typename T>
543void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
544 PacketDirection desired_direction,
545 Plot* plot,
546 const std::map<StreamId, std::vector<T>>& packets,
547 const std::string& label_prefix) {
548 for (auto& kv : packets) {
549 StreamId stream_id = kv.first;
550 const std::vector<T>& packet_stream = kv.second;
551 // Filter on direction and SSRC.
552 if (stream_id.GetDirection() != desired_direction ||
553 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
554 continue;
555 }
556
557 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200558 time_series.label = label_prefix + " " + GetStreamName(stream_id);
philipelccd74892016-09-05 02:46:25 -0700559 time_series.style = LINE_GRAPH;
560
561 for (size_t i = 0; i < packet_stream.size(); i++) {
562 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
563 1000000;
564 time_series.points.emplace_back(x, i);
565 time_series.points.emplace_back(x, i + 1);
566 }
567
568 plot->series_list_.push_back(std::move(time_series));
569 }
570}
571
572void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
573 PacketDirection desired_direction,
574 Plot* plot) {
575 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
576 "RTP");
577 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
578 "RTCP");
579
580 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
581 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
582 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
583 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
584 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
585 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
586 }
587}
588
terelius54ce6802016-07-13 06:44:41 -0700589// For each SSRC, plot the time between the consecutive playouts.
590void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
591 std::map<uint32_t, TimeSeries> time_series;
592 std::map<uint32_t, uint64_t> last_playout;
593
594 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700595
596 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
597 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
598 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
599 parsed_log_.GetAudioPlayout(i, &ssrc);
600 uint64_t timestamp = parsed_log_.GetTimestamp(i);
601 if (MatchingSsrc(ssrc, desired_ssrc_)) {
602 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
603 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
604 if (time_series[ssrc].points.size() == 0) {
605 // There were no previusly logged playout for this SSRC.
606 // Generate a point, but place it on the x-axis.
607 y = 0;
608 }
terelius54ce6802016-07-13 06:44:41 -0700609 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
610 last_playout[ssrc] = timestamp;
611 }
612 }
613 }
614
615 // Set labels and put in graph.
616 for (auto& kv : time_series) {
617 kv.second.label = SsrcToString(kv.first);
618 kv.second.style = BAR_GRAPH;
tereliusdc35dcd2016-08-01 12:03:27 -0700619 plot->series_list_.push_back(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700620 }
621
tereliusdc35dcd2016-08-01 12:03:27 -0700622 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
623 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
624 kTopMargin);
625 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700626}
627
ivocaac9d6f2016-09-22 07:01:47 -0700628// For audio SSRCs, plot the audio level.
629void EventLogAnalyzer::CreateAudioLevelGraph(Plot* plot) {
630 std::map<StreamId, TimeSeries> time_series;
631
632 for (auto& kv : rtp_packets_) {
633 StreamId stream_id = kv.first;
634 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
635 // TODO(ivoc): When audio send/receive configs are stored in the event
636 // log, a check should be added here to only process audio
637 // streams. Tracking bug: webrtc:6399
638 for (auto& packet : packet_stream) {
639 if (packet.header.extension.hasAudioLevel) {
640 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
641 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
642 // Here we convert it to dBov.
643 float y = static_cast<float>(-packet.header.extension.audioLevel);
644 time_series[stream_id].points.emplace_back(TimeSeriesPoint(x, y));
645 }
646 }
647 }
648
649 for (auto& series : time_series) {
650 series.second.label = GetStreamName(series.first);
651 series.second.style = LINE_GRAPH;
652 plot->series_list_.push_back(std::move(series.second));
653 }
654
655 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
656 plot->SetYAxis(-127, 0, "Audio playout level (dBov)", kBottomMargin,
657 kTopMargin);
658 plot->SetTitle("Audio level");
659}
660
terelius54ce6802016-07-13 06:44:41 -0700661// For each SSRC, plot the time between the consecutive playouts.
662void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700663 for (auto& kv : rtp_packets_) {
664 StreamId stream_id = kv.first;
665 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
666 // Filter on direction and SSRC.
667 if (stream_id.GetDirection() != kIncomingPacket ||
668 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
669 continue;
terelius54ce6802016-07-13 06:44:41 -0700670 }
terelius54ce6802016-07-13 06:44:41 -0700671
terelius6addf492016-08-23 17:34:07 -0700672 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200673 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700674 time_series.style = BAR_GRAPH;
675 Pairwise<SequenceNumberDiff>(packet_stream, begin_time_, &time_series);
676 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700677 }
678
tereliusdc35dcd2016-08-01 12:03:27 -0700679 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
680 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
681 kTopMargin);
682 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700683}
684
Stefan Holmer99f8e082016-09-09 13:37:50 +0200685void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
686 for (auto& kv : rtp_packets_) {
687 StreamId stream_id = kv.first;
688 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
689 // Filter on direction and SSRC.
690 if (stream_id.GetDirection() != kIncomingPacket ||
691 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
692 continue;
693 }
694
695 TimeSeries time_series;
696 time_series.label = GetStreamName(stream_id);
697 time_series.style = LINE_DOT_GRAPH;
698 const uint64_t kWindowUs = 1000000;
699 const LoggedRtpPacket* first_in_window = &packet_stream.front();
700 const LoggedRtpPacket* last_in_window = &packet_stream.front();
701 int packets_in_window = 0;
702 for (const LoggedRtpPacket& packet : packet_stream) {
703 if (packet.timestamp > first_in_window->timestamp + kWindowUs) {
704 uint16_t expected_num_packets = last_in_window->header.sequenceNumber -
705 first_in_window->header.sequenceNumber + 1;
706 float fraction_lost = (expected_num_packets - packets_in_window) /
707 static_cast<float>(expected_num_packets);
708 float y = fraction_lost * 100;
709 float x =
710 static_cast<float>(last_in_window->timestamp - begin_time_) /
711 1000000;
712 time_series.points.emplace_back(x, y);
713 first_in_window = &packet;
714 last_in_window = &packet;
715 packets_in_window = 1;
716 continue;
717 }
718 ++packets_in_window;
719 last_in_window = &packet;
720 }
721 plot->series_list_.push_back(std::move(time_series));
722 }
723
724 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
725 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
726 kTopMargin);
727 plot->SetTitle("Estimated incoming loss rate");
728}
729
terelius54ce6802016-07-13 06:44:41 -0700730void EventLogAnalyzer::CreateDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700731 for (auto& kv : rtp_packets_) {
732 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700733 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700734 // Filter on direction and SSRC.
735 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200736 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
737 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
738 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700739 continue;
740 }
terelius54ce6802016-07-13 06:44:41 -0700741
tereliusccbbf8d2016-08-10 07:34:28 -0700742 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200743 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700744 capture_time_data.style = BAR_GRAPH;
745 Pairwise<NetworkDelayDiff::CaptureTime>(packet_stream, begin_time_,
746 &capture_time_data);
747 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700748
tereliusccbbf8d2016-08-10 07:34:28 -0700749 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200750 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700751 send_time_data.style = BAR_GRAPH;
752 Pairwise<NetworkDelayDiff::AbsSendTime>(packet_stream, begin_time_,
753 &send_time_data);
754 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700755 }
756
tereliusdc35dcd2016-08-01 12:03:27 -0700757 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
758 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
759 kTopMargin);
760 plot->SetTitle("Network latency change between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700761}
762
763void EventLogAnalyzer::CreateAccumulatedDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700764 for (auto& kv : rtp_packets_) {
765 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700766 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700767 // Filter on direction and SSRC.
768 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200769 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
770 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
771 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700772 continue;
773 }
terelius54ce6802016-07-13 06:44:41 -0700774
tereliusccbbf8d2016-08-10 07:34:28 -0700775 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200776 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700777 capture_time_data.style = LINE_GRAPH;
778 Pairwise<Accumulated<NetworkDelayDiff::CaptureTime>>(
779 packet_stream, begin_time_, &capture_time_data);
780 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700781
tereliusccbbf8d2016-08-10 07:34:28 -0700782 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200783 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700784 send_time_data.style = LINE_GRAPH;
785 Pairwise<Accumulated<NetworkDelayDiff::AbsSendTime>>(
786 packet_stream, begin_time_, &send_time_data);
787 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700788 }
789
tereliusdc35dcd2016-08-01 12:03:27 -0700790 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
791 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
792 kTopMargin);
793 plot->SetTitle("Accumulated network latency change");
terelius54ce6802016-07-13 06:44:41 -0700794}
795
tereliusf736d232016-08-04 10:00:11 -0700796// Plot the fraction of packets lost (as perceived by the loss-based BWE).
797void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
798 plot->series_list_.push_back(TimeSeries());
799 for (auto& bwe_update : bwe_loss_updates_) {
800 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
801 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
802 plot->series_list_.back().points.emplace_back(x, y);
803 }
804 plot->series_list_.back().label = "Fraction lost";
805 plot->series_list_.back().style = LINE_DOT_GRAPH;
806
807 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
808 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
809 kTopMargin);
810 plot->SetTitle("Reported packet loss");
811}
812
terelius54ce6802016-07-13 06:44:41 -0700813// Plot the total bandwidth used by all RTP streams.
814void EventLogAnalyzer::CreateTotalBitrateGraph(
815 PacketDirection desired_direction,
816 Plot* plot) {
817 struct TimestampSize {
818 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
819 uint64_t timestamp;
820 size_t size;
821 };
822 std::vector<TimestampSize> packets;
823
824 PacketDirection direction;
825 size_t total_length;
826
827 // Extract timestamps and sizes for the relevant packets.
828 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
829 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
830 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
831 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, nullptr,
832 &total_length);
833 if (direction == desired_direction) {
834 uint64_t timestamp = parsed_log_.GetTimestamp(i);
835 packets.push_back(TimestampSize(timestamp, total_length));
836 }
837 }
838 }
839
840 size_t window_index_begin = 0;
841 size_t window_index_end = 0;
842 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700843
844 // Calculate a moving average of the bitrate and store in a TimeSeries.
tereliusdc35dcd2016-08-01 12:03:27 -0700845 plot->series_list_.push_back(TimeSeries());
terelius54ce6802016-07-13 06:44:41 -0700846 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
847 while (window_index_end < packets.size() &&
848 packets[window_index_end].timestamp < time) {
849 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700850 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700851 }
852 while (window_index_begin < packets.size() &&
853 packets[window_index_begin].timestamp < time - window_duration_) {
854 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
855 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700856 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700857 }
858 float window_duration_in_seconds =
859 static_cast<float>(window_duration_) / 1000000;
860 float x = static_cast<float>(time - begin_time_) / 1000000;
861 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700862 plot->series_list_.back().points.push_back(TimeSeriesPoint(x, y));
terelius54ce6802016-07-13 06:44:41 -0700863 }
864
865 // Set labels.
866 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700867 plot->series_list_.back().label = "Incoming bitrate";
terelius54ce6802016-07-13 06:44:41 -0700868 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700869 plot->series_list_.back().label = "Outgoing bitrate";
terelius54ce6802016-07-13 06:44:41 -0700870 }
tereliusdc35dcd2016-08-01 12:03:27 -0700871 plot->series_list_.back().style = LINE_GRAPH;
terelius54ce6802016-07-13 06:44:41 -0700872
terelius8058e582016-07-25 01:32:41 -0700873 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
874 if (desired_direction == kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700875 plot->series_list_.push_back(TimeSeries());
terelius8058e582016-07-25 01:32:41 -0700876 for (auto& bwe_update : bwe_loss_updates_) {
877 float x =
878 static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
879 float y = static_cast<float>(bwe_update.new_bitrate) / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700880 plot->series_list_.back().points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -0700881 }
tereliusdc35dcd2016-08-01 12:03:27 -0700882 plot->series_list_.back().label = "Loss-based estimate";
883 plot->series_list_.back().style = LINE_GRAPH;
terelius8058e582016-07-25 01:32:41 -0700884 }
tereliusdc35dcd2016-08-01 12:03:27 -0700885 plot->series_list_.back().style = LINE_GRAPH;
886 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
887 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700888 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700889 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700890 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700891 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700892 }
893}
894
895// For each SSRC, plot the bandwidth used by that stream.
896void EventLogAnalyzer::CreateStreamBitrateGraph(
897 PacketDirection desired_direction,
898 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700899 for (auto& kv : rtp_packets_) {
900 StreamId stream_id = kv.first;
901 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
902 // Filter on direction and SSRC.
903 if (stream_id.GetDirection() != desired_direction ||
904 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
905 continue;
terelius54ce6802016-07-13 06:44:41 -0700906 }
907
terelius6addf492016-08-23 17:34:07 -0700908 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200909 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700910 time_series.style = LINE_GRAPH;
911 double bytes_to_kilobits = 8.0 / 1000;
912 MovingAverage<PacketSizeBytes>(packet_stream, begin_time_, end_time_,
913 window_duration_, step_, bytes_to_kilobits,
914 &time_series);
915 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700916 }
917
tereliusdc35dcd2016-08-01 12:03:27 -0700918 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
919 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700920 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700921 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700922 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700923 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700924 }
925}
926
tereliuse34c19c2016-08-15 08:47:14 -0700927void EventLogAnalyzer::CreateBweSimulationGraph(Plot* plot) {
Stefan Holmer13181032016-07-29 14:48:54 +0200928 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
929 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
930
931 for (const auto& kv : rtp_packets_) {
932 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
933 for (const LoggedRtpPacket& rtp_packet : kv.second)
934 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
935 }
936 }
937
938 for (const auto& kv : rtcp_packets_) {
939 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
940 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
941 incoming_rtcp.insert(
942 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
943 }
944 }
945
946 SimulatedClock clock(0);
947 BitrateObserver observer;
948 RtcEventLogNullImpl null_event_log;
949 CongestionController cc(&clock, &observer, &observer, &null_event_log);
950 // TODO(holmer): Log the call config and use that here instead.
951 static const uint32_t kDefaultStartBitrateBps = 300000;
952 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
953
954 TimeSeries time_series;
tereliuse34c19c2016-08-15 08:47:14 -0700955 time_series.label = "Delay-based estimate";
Stefan Holmer13181032016-07-29 14:48:54 +0200956 time_series.style = LINE_DOT_GRAPH;
Stefan Holmer60e43462016-09-07 09:58:20 +0200957 TimeSeries acked_time_series;
958 acked_time_series.label = "Acked bitrate";
959 acked_time_series.style = LINE_DOT_GRAPH;
Stefan Holmer13181032016-07-29 14:48:54 +0200960
961 auto rtp_iterator = outgoing_rtp.begin();
962 auto rtcp_iterator = incoming_rtcp.begin();
963
964 auto NextRtpTime = [&]() {
965 if (rtp_iterator != outgoing_rtp.end())
966 return static_cast<int64_t>(rtp_iterator->first);
967 return std::numeric_limits<int64_t>::max();
968 };
969
970 auto NextRtcpTime = [&]() {
971 if (rtcp_iterator != incoming_rtcp.end())
972 return static_cast<int64_t>(rtcp_iterator->first);
973 return std::numeric_limits<int64_t>::max();
974 };
975
976 auto NextProcessTime = [&]() {
977 if (rtcp_iterator != incoming_rtcp.end() ||
978 rtp_iterator != outgoing_rtp.end()) {
979 return clock.TimeInMicroseconds() +
980 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
981 }
982 return std::numeric_limits<int64_t>::max();
983 };
984
Stefan Holmer60e43462016-09-07 09:58:20 +0200985 RateStatistics acked_bitrate(1000, 8000);
986
Stefan Holmer13181032016-07-29 14:48:54 +0200987 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
988 while (time_us != std::numeric_limits<int64_t>::max()) {
989 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
990 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -0700991 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200992 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
993 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +0200994 TransportFeedbackObserver* observer = cc.GetTransportFeedbackObserver();
995 observer->OnTransportFeedback(*static_cast<rtcp::TransportFeedback*>(
996 rtcp.packet.get()));
997 std::vector<PacketInfo> feedback =
998 observer->GetTransportFeedbackVector();
999 rtc::Optional<uint32_t> bitrate_bps;
1000 if (!feedback.empty()) {
1001 for (const PacketInfo& packet : feedback)
1002 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1003 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
1004 }
1005 uint32_t y = 0;
1006 if (bitrate_bps)
1007 y = *bitrate_bps / 1000;
1008 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1009 1000000;
1010 acked_time_series.points.emplace_back(x, y);
Stefan Holmer13181032016-07-29 14:48:54 +02001011 }
1012 ++rtcp_iterator;
1013 }
1014 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001015 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001016 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1017 if (rtp.header.extension.hasTransportSequenceNumber) {
1018 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1019 cc.GetTransportFeedbackObserver()->AddPacket(
stefana93d5ac2016-08-17 02:14:32 -07001020 rtp.header.extension.transportSequenceNumber, rtp.total_length,
1021 PacketInfo::kNotAProbe);
Stefan Holmer13181032016-07-29 14:48:54 +02001022 rtc::SentPacket sent_packet(
1023 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1024 cc.OnSentPacket(sent_packet);
1025 }
1026 ++rtp_iterator;
1027 }
stefanc3de0332016-08-02 07:22:17 -07001028 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1029 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001030 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001031 }
Stefan Holmer13181032016-07-29 14:48:54 +02001032 if (observer.GetAndResetBitrateUpdated()) {
1033 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +02001034 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1035 1000000;
1036 time_series.points.emplace_back(x, y);
1037 }
1038 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1039 }
1040 // Add the data set to the plot.
tereliusdc35dcd2016-08-01 12:03:27 -07001041 plot->series_list_.push_back(std::move(time_series));
Stefan Holmer60e43462016-09-07 09:58:20 +02001042 plot->series_list_.push_back(std::move(acked_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001043
tereliusdc35dcd2016-08-01 12:03:27 -07001044 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1045 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1046 plot->SetTitle("Simulated BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001047}
1048
Stefan Holmer280de9e2016-09-30 10:06:51 +02001049// TODO(holmer): Remove once TransportFeedbackAdapter no longer needs a
1050// BitrateController.
1051class NullBitrateController : public BitrateController {
1052 public:
1053 ~NullBitrateController() override {}
1054 RtcpBandwidthObserver* CreateRtcpBandwidthObserver() override {
1055 return nullptr;
1056 }
1057 void SetStartBitrate(int start_bitrate_bps) override {}
1058 void SetMinMaxBitrate(int min_bitrate_bps, int max_bitrate_bps) override {}
1059 void SetBitrates(int start_bitrate_bps,
1060 int min_bitrate_bps,
1061 int max_bitrate_bps) override {}
1062 void ResetBitrates(int bitrate_bps,
1063 int min_bitrate_bps,
1064 int max_bitrate_bps) override {}
1065 void OnDelayBasedBweResult(const DelayBasedBwe::Result& result) override {}
1066 bool AvailableBandwidth(uint32_t* bandwidth) const override { return false; }
1067 void SetReservedBitrate(uint32_t reserved_bitrate_bps) override {}
1068 bool GetNetworkParameters(uint32_t* bitrate,
1069 uint8_t* fraction_loss,
1070 int64_t* rtt) override {
1071 return false;
1072 }
1073 int64_t TimeUntilNextProcess() override { return 0; }
1074 void Process() override {}
1075};
1076
tereliuse34c19c2016-08-15 08:47:14 -07001077void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanc3de0332016-08-02 07:22:17 -07001078 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1079 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
1080
1081 for (const auto& kv : rtp_packets_) {
1082 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1083 for (const LoggedRtpPacket& rtp_packet : kv.second)
1084 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1085 }
1086 }
1087
1088 for (const auto& kv : rtcp_packets_) {
1089 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1090 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1091 incoming_rtcp.insert(
1092 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1093 }
1094 }
1095
1096 SimulatedClock clock(0);
Stefan Holmer280de9e2016-09-30 10:06:51 +02001097 NullBitrateController null_controller;
1098 TransportFeedbackAdapter feedback_adapter(&clock, &null_controller);
stefanc3de0332016-08-02 07:22:17 -07001099
1100 TimeSeries time_series;
1101 time_series.label = "Network Delay Change";
1102 time_series.style = LINE_DOT_GRAPH;
1103 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1104
1105 auto rtp_iterator = outgoing_rtp.begin();
1106 auto rtcp_iterator = incoming_rtcp.begin();
1107
1108 auto NextRtpTime = [&]() {
1109 if (rtp_iterator != outgoing_rtp.end())
1110 return static_cast<int64_t>(rtp_iterator->first);
1111 return std::numeric_limits<int64_t>::max();
1112 };
1113
1114 auto NextRtcpTime = [&]() {
1115 if (rtcp_iterator != incoming_rtcp.end())
1116 return static_cast<int64_t>(rtcp_iterator->first);
1117 return std::numeric_limits<int64_t>::max();
1118 };
1119
1120 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
1121 while (time_us != std::numeric_limits<int64_t>::max()) {
1122 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1123 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1124 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1125 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1126 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001127 feedback_adapter.OnTransportFeedback(
1128 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
stefanc3de0332016-08-02 07:22:17 -07001129 std::vector<PacketInfo> feedback =
Stefan Holmer60e43462016-09-07 09:58:20 +02001130 feedback_adapter.GetTransportFeedbackVector();
stefanc3de0332016-08-02 07:22:17 -07001131 for (const PacketInfo& packet : feedback) {
1132 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1133 float x =
1134 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1135 1000000;
1136 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1137 time_series.points.emplace_back(x, y);
1138 }
1139 }
1140 ++rtcp_iterator;
1141 }
1142 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1143 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1144 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1145 if (rtp.header.extension.hasTransportSequenceNumber) {
1146 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1147 feedback_adapter.AddPacket(rtp.header.extension.transportSequenceNumber,
1148 rtp.total_length, 0);
1149 feedback_adapter.OnSentPacket(
1150 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1151 }
1152 ++rtp_iterator;
1153 }
1154 time_us = std::min(NextRtpTime(), NextRtcpTime());
1155 }
1156 // We assume that the base network delay (w/o queues) is the min delay
1157 // observed during the call.
1158 for (TimeSeriesPoint& point : time_series.points)
1159 point.y -= estimated_base_delay_ms;
1160 // Add the data set to the plot.
1161 plot->series_list_.push_back(std::move(time_series));
1162
1163 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1164 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1165 plot->SetTitle("Network Delay Change.");
1166}
terelius54ce6802016-07-13 06:44:41 -07001167} // namespace plotting
1168} // namespace webrtc