blob: 260975b4b530f96bfff06f3f0b3eeff66e7e1526 [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 Holmer13181032016-07-29 14:48:54 +020027#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
terelius54ce6802016-07-13 06:44:41 -070028#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
29#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
30#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
Stefan Holmer13181032016-07-29 14:48:54 +020031#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
32#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
terelius54ce6802016-07-13 06:44:41 -070033#include "webrtc/video_receive_stream.h"
34#include "webrtc/video_send_stream.h"
35
tereliusdc35dcd2016-08-01 12:03:27 -070036namespace webrtc {
37namespace plotting {
38
terelius54ce6802016-07-13 06:44:41 -070039namespace {
40
41std::string SsrcToString(uint32_t ssrc) {
42 std::stringstream ss;
43 ss << "SSRC " << ssrc;
44 return ss.str();
45}
46
47// Checks whether an SSRC is contained in the list of desired SSRCs.
48// Note that an empty SSRC list matches every SSRC.
49bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
50 if (desired_ssrc.size() == 0)
51 return true;
52 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
53 desired_ssrc.end();
54}
55
56double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
57 // The timestamp is a fixed point representation with 6 bits for seconds
58 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
59 // time in seconds and then multiply by 1000000 to convert to microseconds.
60 static constexpr double kTimestampToMicroSec =
tereliusccbbf8d2016-08-10 07:34:28 -070061 1000000.0 / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -070062 return abs_send_time * kTimestampToMicroSec;
63}
64
65// Computes the difference |later| - |earlier| where |later| and |earlier|
66// are counters that wrap at |modulus|. The difference is chosen to have the
67// least absolute value. For example if |modulus| is 8, then the difference will
68// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
69// be in [-4, 4].
70int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
71 RTC_DCHECK_LE(1, modulus);
72 RTC_DCHECK_LT(later, modulus);
73 RTC_DCHECK_LT(earlier, modulus);
74 int64_t difference =
75 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
76 int64_t max_difference = modulus / 2;
77 int64_t min_difference = max_difference - modulus + 1;
78 if (difference > max_difference) {
79 difference -= modulus;
80 }
81 if (difference < min_difference) {
82 difference += modulus;
83 }
terelius6addf492016-08-23 17:34:07 -070084 if (difference > max_difference / 2 || difference < min_difference / 2) {
85 LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
86 << " expected to be in the range (" << min_difference / 2
87 << "," << max_difference / 2 << ") but is " << difference
88 << ". Correct unwrapping is uncertain.";
89 }
terelius54ce6802016-07-13 06:44:41 -070090 return difference;
91}
92
stefan6a850c32016-07-29 10:28:08 -070093void RegisterHeaderExtensions(
94 const std::vector<webrtc::RtpExtension>& extensions,
95 webrtc::RtpHeaderExtensionMap* extension_map) {
96 extension_map->Erase();
97 for (const webrtc::RtpExtension& extension : extensions) {
98 extension_map->Register(webrtc::StringToRtpExtensionType(extension.uri),
99 extension.id);
100 }
101}
102
ivocaac9d6f2016-09-22 07:01:47 -0700103// Return default values for header extensions, to use on streams without stored
104// mapping data. Currently this only applies to audio streams, since the mapping
105// is not stored in the event log.
106// TODO(ivoc): Remove this once this mapping is stored in the event log for
107// audio streams. Tracking bug: webrtc:6399
108webrtc::RtpHeaderExtensionMap GetDefaultHeaderExtensionMap() {
109 webrtc::RtpHeaderExtensionMap default_map;
110 default_map.Register(
111 webrtc::StringToRtpExtensionType(webrtc::RtpExtension::kAudioLevelUri),
112 webrtc::RtpExtension::kAudioLevelDefaultId);
113 default_map.Register(
114 webrtc::StringToRtpExtensionType(webrtc::RtpExtension::kAbsSendTimeUri),
115 webrtc::RtpExtension::kAbsSendTimeDefaultId);
116 return default_map;
117}
118
tereliusdc35dcd2016-08-01 12:03:27 -0700119constexpr float kLeftMargin = 0.01f;
120constexpr float kRightMargin = 0.02f;
121constexpr float kBottomMargin = 0.02f;
122constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700123
terelius6addf492016-08-23 17:34:07 -0700124class PacketSizeBytes {
125 public:
126 using DataType = LoggedRtpPacket;
127 using ResultType = size_t;
128 size_t operator()(const LoggedRtpPacket& packet) {
129 return packet.total_length;
130 }
131};
132
133class SequenceNumberDiff {
134 public:
135 using DataType = LoggedRtpPacket;
136 using ResultType = int64_t;
137 int64_t operator()(const LoggedRtpPacket& old_packet,
138 const LoggedRtpPacket& new_packet) {
139 return WrappingDifference(new_packet.header.sequenceNumber,
140 old_packet.header.sequenceNumber, 1ul << 16);
141 }
142};
143
tereliusccbbf8d2016-08-10 07:34:28 -0700144class NetworkDelayDiff {
145 public:
146 class AbsSendTime {
147 public:
148 using DataType = LoggedRtpPacket;
149 using ResultType = double;
150 double operator()(const LoggedRtpPacket& old_packet,
151 const LoggedRtpPacket& new_packet) {
152 if (old_packet.header.extension.hasAbsoluteSendTime &&
153 new_packet.header.extension.hasAbsoluteSendTime) {
154 int64_t send_time_diff = WrappingDifference(
155 new_packet.header.extension.absoluteSendTime,
156 old_packet.header.extension.absoluteSendTime, 1ul << 24);
157 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
158 return static_cast<double>(recv_time_diff -
159 AbsSendTimeToMicroseconds(send_time_diff)) /
160 1000;
161 } else {
162 return 0;
163 }
164 }
165 };
166
167 class CaptureTime {
168 public:
169 using DataType = LoggedRtpPacket;
170 using ResultType = double;
171 double operator()(const LoggedRtpPacket& old_packet,
172 const LoggedRtpPacket& new_packet) {
173 int64_t send_time_diff = WrappingDifference(
174 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
175 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
176
177 const double kVideoSampleRate = 90000;
178 // TODO(terelius): We treat all streams as video for now, even though
179 // audio might be sampled at e.g. 16kHz, because it is really difficult to
180 // figure out the true sampling rate of a stream. The effect is that the
181 // delay will be scaled incorrectly for non-video streams.
182
183 double delay_change =
184 static_cast<double>(recv_time_diff) / 1000 -
185 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
terelius6addf492016-08-23 17:34:07 -0700186 if (delay_change < -10000 || 10000 < delay_change) {
187 LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
188 LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
189 << ", received time " << old_packet.timestamp;
190 LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
191 << ", received time " << new_packet.timestamp;
192 LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
193 << static_cast<double>(recv_time_diff) / 1000000 << "s";
194 LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
195 << static_cast<double>(send_time_diff) /
196 kVideoSampleRate
197 << "s";
198 }
tereliusccbbf8d2016-08-10 07:34:28 -0700199 return delay_change;
200 }
201 };
202};
203
204template <typename Extractor>
205class Accumulated {
206 public:
207 using DataType = typename Extractor::DataType;
208 using ResultType = typename Extractor::ResultType;
209 ResultType operator()(const DataType& old_packet,
210 const DataType& new_packet) {
211 sum += extract(old_packet, new_packet);
212 return sum;
213 }
214
215 private:
216 Extractor extract;
217 ResultType sum = 0;
218};
219
terelius6addf492016-08-23 17:34:07 -0700220// For each element in data, use |Extractor| to extract a y-coordinate and
221// store the result in a TimeSeries.
222template <typename Extractor>
223void Pointwise(const std::vector<typename Extractor::DataType>& data,
224 uint64_t begin_time,
225 TimeSeries* result) {
226 Extractor extract;
227 for (size_t i = 0; i < data.size(); i++) {
228 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
229 float y = extract(data[i]);
230 result->points.emplace_back(x, y);
231 }
232}
233
234// For each pair of adjacent elements in |data|, use |Extractor| to extract a
235// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
236// will be the time of the second element in the pair.
tereliusccbbf8d2016-08-10 07:34:28 -0700237template <typename Extractor>
238void Pairwise(const std::vector<typename Extractor::DataType>& data,
239 uint64_t begin_time,
240 TimeSeries* result) {
241 Extractor extract;
242 for (size_t i = 1; i < data.size(); i++) {
243 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
244 float y = extract(data[i - 1], data[i]);
245 result->points.emplace_back(x, y);
246 }
247}
248
terelius6addf492016-08-23 17:34:07 -0700249// Calculates a moving average of |data| and stores the result in a TimeSeries.
250// A data point is generated every |step| microseconds from |begin_time|
251// to |end_time|. The value of each data point is the average of the data
252// during the preceeding |window_duration_us| microseconds.
253template <typename Extractor>
254void MovingAverage(const std::vector<typename Extractor::DataType>& data,
255 uint64_t begin_time,
256 uint64_t end_time,
257 uint64_t window_duration_us,
258 uint64_t step,
259 float y_scaling,
260 webrtc::plotting::TimeSeries* result) {
261 size_t window_index_begin = 0;
262 size_t window_index_end = 0;
263 typename Extractor::ResultType sum_in_window = 0;
264 Extractor extract;
265
266 for (uint64_t t = begin_time; t < end_time + step; t += step) {
267 while (window_index_end < data.size() &&
268 data[window_index_end].timestamp < t) {
269 sum_in_window += extract(data[window_index_end]);
270 ++window_index_end;
271 }
272 while (window_index_begin < data.size() &&
273 data[window_index_begin].timestamp < t - window_duration_us) {
274 sum_in_window -= extract(data[window_index_begin]);
275 ++window_index_begin;
276 }
277 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
278 float x = static_cast<float>(t - begin_time) / 1000000;
279 float y = sum_in_window / window_duration_s * y_scaling;
280 result->points.emplace_back(x, y);
281 }
282}
283
terelius54ce6802016-07-13 06:44:41 -0700284} // namespace
285
terelius54ce6802016-07-13 06:44:41 -0700286EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
287 : parsed_log_(log), window_duration_(250000), step_(10000) {
288 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
289 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700290
Stefan Holmer13181032016-07-29 14:48:54 +0200291 // Maps a stream identifier consisting of ssrc and direction
terelius88e64e52016-07-19 01:51:06 -0700292 // to the header extensions used by that stream,
293 std::map<StreamId, RtpHeaderExtensionMap> extension_maps;
294
295 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700296 uint8_t header[IP_PACKET_SIZE];
297 size_t header_length;
298 size_t total_length;
299
ivocaac9d6f2016-09-22 07:01:47 -0700300 // Make a default extension map for streams without configuration information.
301 // TODO(ivoc): Once configuration of audio streams is stored in the event log,
302 // this can be removed. Tracking bug: webrtc:6399
303 RtpHeaderExtensionMap default_extension_map = GetDefaultHeaderExtensionMap();
304
terelius54ce6802016-07-13 06:44:41 -0700305 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
306 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700307 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
308 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
309 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700310 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
311 event_type != ParsedRtcEventLog::LOG_START &&
312 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700313 uint64_t timestamp = parsed_log_.GetTimestamp(i);
314 first_timestamp = std::min(first_timestamp, timestamp);
315 last_timestamp = std::max(last_timestamp, timestamp);
316 }
317
318 switch (parsed_log_.GetEventType(i)) {
319 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
320 VideoReceiveStream::Config config(nullptr);
321 parsed_log_.GetVideoReceiveConfig(i, &config);
Stefan Holmer13181032016-07-29 14:48:54 +0200322 StreamId stream(config.rtp.remote_ssrc, kIncomingPacket);
stefan6a850c32016-07-29 10:28:08 -0700323 RegisterHeaderExtensions(config.rtp.extensions,
324 &extension_maps[stream]);
terelius0740a202016-08-08 10:21:04 -0700325 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700326 for (auto kv : config.rtp.rtx) {
327 StreamId rtx_stream(kv.second.ssrc, kIncomingPacket);
328 RegisterHeaderExtensions(config.rtp.extensions,
329 &extension_maps[rtx_stream]);
terelius0740a202016-08-08 10:21:04 -0700330 video_ssrcs_.insert(rtx_stream);
331 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700332 }
333 break;
334 }
335 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
336 VideoSendStream::Config config(nullptr);
337 parsed_log_.GetVideoSendConfig(i, &config);
338 for (auto ssrc : config.rtp.ssrcs) {
Stefan Holmer13181032016-07-29 14:48:54 +0200339 StreamId stream(ssrc, kOutgoingPacket);
stefan6a850c32016-07-29 10:28:08 -0700340 RegisterHeaderExtensions(config.rtp.extensions,
341 &extension_maps[stream]);
terelius0740a202016-08-08 10:21:04 -0700342 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700343 }
344 for (auto ssrc : config.rtp.rtx.ssrcs) {
terelius0740a202016-08-08 10:21:04 -0700345 StreamId rtx_stream(ssrc, kOutgoingPacket);
stefan6a850c32016-07-29 10:28:08 -0700346 RegisterHeaderExtensions(config.rtp.extensions,
terelius0740a202016-08-08 10:21:04 -0700347 &extension_maps[rtx_stream]);
348 video_ssrcs_.insert(rtx_stream);
349 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700350 }
351 break;
352 }
353 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
354 AudioReceiveStream::Config config;
355 // TODO(terelius): Parse the audio configs once we have them.
356 break;
357 }
358 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
359 AudioSendStream::Config config(nullptr);
360 // TODO(terelius): Parse the audio configs once we have them.
361 break;
362 }
363 case ParsedRtcEventLog::RTP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200364 MediaType media_type;
terelius88e64e52016-07-19 01:51:06 -0700365 parsed_log_.GetRtpHeader(i, &direction, &media_type, header,
366 &header_length, &total_length);
367 // Parse header to get SSRC.
368 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
369 RTPHeader parsed_header;
370 rtp_parser.Parse(&parsed_header);
Stefan Holmer13181032016-07-29 14:48:54 +0200371 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700372 // Look up the extension_map and parse it again to get the extensions.
373 if (extension_maps.count(stream) == 1) {
374 RtpHeaderExtensionMap* extension_map = &extension_maps[stream];
375 rtp_parser.Parse(&parsed_header, extension_map);
ivocaac9d6f2016-09-22 07:01:47 -0700376 } else {
377 // Use the default extension map.
378 // TODO(ivoc): Once configuration of audio streams is stored in the
379 // event log, this can be removed.
380 // Tracking bug: webrtc:6399
381 rtp_parser.Parse(&parsed_header, &default_extension_map);
terelius88e64e52016-07-19 01:51:06 -0700382 }
383 uint64_t timestamp = parsed_log_.GetTimestamp(i);
384 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200385 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700386 break;
387 }
388 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200389 uint8_t packet[IP_PACKET_SIZE];
390 MediaType media_type;
391 parsed_log_.GetRtcpPacket(i, &direction, &media_type, packet,
392 &total_length);
393
394 RtpUtility::RtpHeaderParser rtp_parser(packet, total_length);
395 RTPHeader parsed_header;
396 RTC_CHECK(rtp_parser.ParseRtcp(&parsed_header));
397 uint32_t ssrc = parsed_header.ssrc;
398
399 RTCPUtility::RTCPParserV2 rtcp_parser(packet, total_length, true);
400 RTC_CHECK(rtcp_parser.IsValid());
401
402 RTCPUtility::RTCPPacketTypes packet_type = rtcp_parser.Begin();
403 while (packet_type != RTCPUtility::RTCPPacketTypes::kInvalid) {
404 switch (packet_type) {
405 case RTCPUtility::RTCPPacketTypes::kTransportFeedback: {
406 // Currently feedback is logged twice, both for audio and video.
407 // Only act on one of them.
408 if (media_type == MediaType::VIDEO) {
409 std::unique_ptr<rtcp::RtcpPacket> rtcp_packet(
410 rtcp_parser.ReleaseRtcpPacket());
411 StreamId stream(ssrc, direction);
412 uint64_t timestamp = parsed_log_.GetTimestamp(i);
413 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
414 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
415 }
416 break;
417 }
418 default:
419 break;
420 }
421 rtcp_parser.Iterate();
422 packet_type = rtcp_parser.PacketType();
423 }
terelius88e64e52016-07-19 01:51:06 -0700424 break;
425 }
426 case ParsedRtcEventLog::LOG_START: {
427 break;
428 }
429 case ParsedRtcEventLog::LOG_END: {
430 break;
431 }
432 case ParsedRtcEventLog::BWE_PACKET_LOSS_EVENT: {
terelius8058e582016-07-25 01:32:41 -0700433 BwePacketLossEvent bwe_update;
434 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
435 parsed_log_.GetBwePacketLossEvent(i, &bwe_update.new_bitrate,
436 &bwe_update.fraction_loss,
437 &bwe_update.expected_packets);
438 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700439 break;
440 }
441 case ParsedRtcEventLog::BWE_PACKET_DELAY_EVENT: {
442 break;
443 }
444 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
445 break;
446 }
447 case ParsedRtcEventLog::UNKNOWN_EVENT: {
448 break;
449 }
450 }
terelius54ce6802016-07-13 06:44:41 -0700451 }
terelius88e64e52016-07-19 01:51:06 -0700452
terelius54ce6802016-07-13 06:44:41 -0700453 if (last_timestamp < first_timestamp) {
454 // No useful events in the log.
455 first_timestamp = last_timestamp = 0;
456 }
457 begin_time_ = first_timestamp;
458 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700459 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
terelius54ce6802016-07-13 06:44:41 -0700460}
461
Stefan Holmer13181032016-07-29 14:48:54 +0200462class BitrateObserver : public CongestionController::Observer,
463 public RemoteBitrateObserver {
464 public:
465 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
466
467 void OnNetworkChanged(uint32_t bitrate_bps,
468 uint8_t fraction_loss,
469 int64_t rtt_ms) override {
470 last_bitrate_bps_ = bitrate_bps;
471 bitrate_updated_ = true;
472 }
473
474 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
475 uint32_t bitrate) override {}
476
477 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
478 bool GetAndResetBitrateUpdated() {
479 bool bitrate_updated = bitrate_updated_;
480 bitrate_updated_ = false;
481 return bitrate_updated;
482 }
483
484 private:
485 uint32_t last_bitrate_bps_;
486 bool bitrate_updated_;
487};
488
Stefan Holmer99f8e082016-09-09 13:37:50 +0200489bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700490 return rtx_ssrcs_.count(stream_id) == 1;
491}
492
Stefan Holmer99f8e082016-09-09 13:37:50 +0200493bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700494 return video_ssrcs_.count(stream_id) == 1;
495}
496
Stefan Holmer99f8e082016-09-09 13:37:50 +0200497bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700498 return audio_ssrcs_.count(stream_id) == 1;
499}
500
Stefan Holmer99f8e082016-09-09 13:37:50 +0200501std::string EventLogAnalyzer::GetStreamName(StreamId stream_id) const {
502 std::stringstream name;
503 if (IsAudioSsrc(stream_id)) {
504 name << "Audio ";
505 } else if (IsVideoSsrc(stream_id)) {
506 name << "Video ";
507 } else {
508 name << "Unknown ";
509 }
510 if (IsRtxSsrc(stream_id))
511 name << "RTX ";
ivocaac9d6f2016-09-22 07:01:47 -0700512 if (stream_id.GetDirection() == kIncomingPacket) {
513 name << "(In) ";
514 } else {
515 name << "(Out) ";
516 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200517 name << SsrcToString(stream_id.GetSsrc());
518 return name.str();
519}
520
terelius54ce6802016-07-13 06:44:41 -0700521void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
522 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700523 for (auto& kv : rtp_packets_) {
524 StreamId stream_id = kv.first;
525 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
526 // Filter on direction and SSRC.
527 if (stream_id.GetDirection() != desired_direction ||
528 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
529 continue;
terelius54ce6802016-07-13 06:44:41 -0700530 }
terelius54ce6802016-07-13 06:44:41 -0700531
terelius6addf492016-08-23 17:34:07 -0700532 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200533 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700534 time_series.style = BAR_GRAPH;
535 Pointwise<PacketSizeBytes>(packet_stream, begin_time_, &time_series);
536 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700537 }
538
tereliusdc35dcd2016-08-01 12:03:27 -0700539 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
540 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
541 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700542 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700543 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700544 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700545 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700546 }
547}
548
philipelccd74892016-09-05 02:46:25 -0700549template <typename T>
550void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
551 PacketDirection desired_direction,
552 Plot* plot,
553 const std::map<StreamId, std::vector<T>>& packets,
554 const std::string& label_prefix) {
555 for (auto& kv : packets) {
556 StreamId stream_id = kv.first;
557 const std::vector<T>& packet_stream = kv.second;
558 // Filter on direction and SSRC.
559 if (stream_id.GetDirection() != desired_direction ||
560 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
561 continue;
562 }
563
564 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200565 time_series.label = label_prefix + " " + GetStreamName(stream_id);
philipelccd74892016-09-05 02:46:25 -0700566 time_series.style = LINE_GRAPH;
567
568 for (size_t i = 0; i < packet_stream.size(); i++) {
569 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
570 1000000;
571 time_series.points.emplace_back(x, i);
572 time_series.points.emplace_back(x, i + 1);
573 }
574
575 plot->series_list_.push_back(std::move(time_series));
576 }
577}
578
579void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
580 PacketDirection desired_direction,
581 Plot* plot) {
582 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
583 "RTP");
584 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
585 "RTCP");
586
587 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
588 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
589 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
590 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
591 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
592 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
593 }
594}
595
terelius54ce6802016-07-13 06:44:41 -0700596// For each SSRC, plot the time between the consecutive playouts.
597void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
598 std::map<uint32_t, TimeSeries> time_series;
599 std::map<uint32_t, uint64_t> last_playout;
600
601 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700602
603 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
604 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
605 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
606 parsed_log_.GetAudioPlayout(i, &ssrc);
607 uint64_t timestamp = parsed_log_.GetTimestamp(i);
608 if (MatchingSsrc(ssrc, desired_ssrc_)) {
609 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
610 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
611 if (time_series[ssrc].points.size() == 0) {
612 // There were no previusly logged playout for this SSRC.
613 // Generate a point, but place it on the x-axis.
614 y = 0;
615 }
terelius54ce6802016-07-13 06:44:41 -0700616 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
617 last_playout[ssrc] = timestamp;
618 }
619 }
620 }
621
622 // Set labels and put in graph.
623 for (auto& kv : time_series) {
624 kv.second.label = SsrcToString(kv.first);
625 kv.second.style = BAR_GRAPH;
tereliusdc35dcd2016-08-01 12:03:27 -0700626 plot->series_list_.push_back(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700627 }
628
tereliusdc35dcd2016-08-01 12:03:27 -0700629 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
630 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
631 kTopMargin);
632 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700633}
634
ivocaac9d6f2016-09-22 07:01:47 -0700635// For audio SSRCs, plot the audio level.
636void EventLogAnalyzer::CreateAudioLevelGraph(Plot* plot) {
637 std::map<StreamId, TimeSeries> time_series;
638
639 for (auto& kv : rtp_packets_) {
640 StreamId stream_id = kv.first;
641 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
642 // TODO(ivoc): When audio send/receive configs are stored in the event
643 // log, a check should be added here to only process audio
644 // streams. Tracking bug: webrtc:6399
645 for (auto& packet : packet_stream) {
646 if (packet.header.extension.hasAudioLevel) {
647 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
648 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
649 // Here we convert it to dBov.
650 float y = static_cast<float>(-packet.header.extension.audioLevel);
651 time_series[stream_id].points.emplace_back(TimeSeriesPoint(x, y));
652 }
653 }
654 }
655
656 for (auto& series : time_series) {
657 series.second.label = GetStreamName(series.first);
658 series.second.style = LINE_GRAPH;
659 plot->series_list_.push_back(std::move(series.second));
660 }
661
662 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
663 plot->SetYAxis(-127, 0, "Audio playout level (dBov)", kBottomMargin,
664 kTopMargin);
665 plot->SetTitle("Audio level");
666}
667
terelius54ce6802016-07-13 06:44:41 -0700668// For each SSRC, plot the time between the consecutive playouts.
669void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700670 for (auto& kv : rtp_packets_) {
671 StreamId stream_id = kv.first;
672 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
673 // Filter on direction and SSRC.
674 if (stream_id.GetDirection() != kIncomingPacket ||
675 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
676 continue;
terelius54ce6802016-07-13 06:44:41 -0700677 }
terelius54ce6802016-07-13 06:44:41 -0700678
terelius6addf492016-08-23 17:34:07 -0700679 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200680 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700681 time_series.style = BAR_GRAPH;
682 Pairwise<SequenceNumberDiff>(packet_stream, begin_time_, &time_series);
683 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700684 }
685
tereliusdc35dcd2016-08-01 12:03:27 -0700686 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
687 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
688 kTopMargin);
689 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700690}
691
Stefan Holmer99f8e082016-09-09 13:37:50 +0200692void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
693 for (auto& kv : rtp_packets_) {
694 StreamId stream_id = kv.first;
695 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
696 // Filter on direction and SSRC.
697 if (stream_id.GetDirection() != kIncomingPacket ||
698 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
699 continue;
700 }
701
702 TimeSeries time_series;
703 time_series.label = GetStreamName(stream_id);
704 time_series.style = LINE_DOT_GRAPH;
705 const uint64_t kWindowUs = 1000000;
706 const LoggedRtpPacket* first_in_window = &packet_stream.front();
707 const LoggedRtpPacket* last_in_window = &packet_stream.front();
708 int packets_in_window = 0;
709 for (const LoggedRtpPacket& packet : packet_stream) {
710 if (packet.timestamp > first_in_window->timestamp + kWindowUs) {
711 uint16_t expected_num_packets = last_in_window->header.sequenceNumber -
712 first_in_window->header.sequenceNumber + 1;
713 float fraction_lost = (expected_num_packets - packets_in_window) /
714 static_cast<float>(expected_num_packets);
715 float y = fraction_lost * 100;
716 float x =
717 static_cast<float>(last_in_window->timestamp - begin_time_) /
718 1000000;
719 time_series.points.emplace_back(x, y);
720 first_in_window = &packet;
721 last_in_window = &packet;
722 packets_in_window = 1;
723 continue;
724 }
725 ++packets_in_window;
726 last_in_window = &packet;
727 }
728 plot->series_list_.push_back(std::move(time_series));
729 }
730
731 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
732 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
733 kTopMargin);
734 plot->SetTitle("Estimated incoming loss rate");
735}
736
terelius54ce6802016-07-13 06:44:41 -0700737void EventLogAnalyzer::CreateDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700738 for (auto& kv : rtp_packets_) {
739 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700740 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700741 // Filter on direction and SSRC.
742 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200743 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
744 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
745 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700746 continue;
747 }
terelius54ce6802016-07-13 06:44:41 -0700748
tereliusccbbf8d2016-08-10 07:34:28 -0700749 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200750 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700751 capture_time_data.style = BAR_GRAPH;
752 Pairwise<NetworkDelayDiff::CaptureTime>(packet_stream, begin_time_,
753 &capture_time_data);
754 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700755
tereliusccbbf8d2016-08-10 07:34:28 -0700756 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200757 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700758 send_time_data.style = BAR_GRAPH;
759 Pairwise<NetworkDelayDiff::AbsSendTime>(packet_stream, begin_time_,
760 &send_time_data);
761 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700762 }
763
tereliusdc35dcd2016-08-01 12:03:27 -0700764 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
765 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
766 kTopMargin);
767 plot->SetTitle("Network latency change between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700768}
769
770void EventLogAnalyzer::CreateAccumulatedDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700771 for (auto& kv : rtp_packets_) {
772 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700773 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700774 // Filter on direction and SSRC.
775 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200776 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
777 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
778 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700779 continue;
780 }
terelius54ce6802016-07-13 06:44:41 -0700781
tereliusccbbf8d2016-08-10 07:34:28 -0700782 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200783 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700784 capture_time_data.style = LINE_GRAPH;
785 Pairwise<Accumulated<NetworkDelayDiff::CaptureTime>>(
786 packet_stream, begin_time_, &capture_time_data);
787 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700788
tereliusccbbf8d2016-08-10 07:34:28 -0700789 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200790 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700791 send_time_data.style = LINE_GRAPH;
792 Pairwise<Accumulated<NetworkDelayDiff::AbsSendTime>>(
793 packet_stream, begin_time_, &send_time_data);
794 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700795 }
796
tereliusdc35dcd2016-08-01 12:03:27 -0700797 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
798 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
799 kTopMargin);
800 plot->SetTitle("Accumulated network latency change");
terelius54ce6802016-07-13 06:44:41 -0700801}
802
tereliusf736d232016-08-04 10:00:11 -0700803// Plot the fraction of packets lost (as perceived by the loss-based BWE).
804void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
805 plot->series_list_.push_back(TimeSeries());
806 for (auto& bwe_update : bwe_loss_updates_) {
807 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
808 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
809 plot->series_list_.back().points.emplace_back(x, y);
810 }
811 plot->series_list_.back().label = "Fraction lost";
812 plot->series_list_.back().style = LINE_DOT_GRAPH;
813
814 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
815 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
816 kTopMargin);
817 plot->SetTitle("Reported packet loss");
818}
819
terelius54ce6802016-07-13 06:44:41 -0700820// Plot the total bandwidth used by all RTP streams.
821void EventLogAnalyzer::CreateTotalBitrateGraph(
822 PacketDirection desired_direction,
823 Plot* plot) {
824 struct TimestampSize {
825 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
826 uint64_t timestamp;
827 size_t size;
828 };
829 std::vector<TimestampSize> packets;
830
831 PacketDirection direction;
832 size_t total_length;
833
834 // Extract timestamps and sizes for the relevant packets.
835 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
836 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
837 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
838 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, nullptr,
839 &total_length);
840 if (direction == desired_direction) {
841 uint64_t timestamp = parsed_log_.GetTimestamp(i);
842 packets.push_back(TimestampSize(timestamp, total_length));
843 }
844 }
845 }
846
847 size_t window_index_begin = 0;
848 size_t window_index_end = 0;
849 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700850
851 // Calculate a moving average of the bitrate and store in a TimeSeries.
tereliusdc35dcd2016-08-01 12:03:27 -0700852 plot->series_list_.push_back(TimeSeries());
terelius54ce6802016-07-13 06:44:41 -0700853 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
854 while (window_index_end < packets.size() &&
855 packets[window_index_end].timestamp < time) {
856 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700857 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700858 }
859 while (window_index_begin < packets.size() &&
860 packets[window_index_begin].timestamp < time - window_duration_) {
861 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
862 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700863 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700864 }
865 float window_duration_in_seconds =
866 static_cast<float>(window_duration_) / 1000000;
867 float x = static_cast<float>(time - begin_time_) / 1000000;
868 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700869 plot->series_list_.back().points.push_back(TimeSeriesPoint(x, y));
terelius54ce6802016-07-13 06:44:41 -0700870 }
871
872 // Set labels.
873 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700874 plot->series_list_.back().label = "Incoming bitrate";
terelius54ce6802016-07-13 06:44:41 -0700875 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700876 plot->series_list_.back().label = "Outgoing bitrate";
terelius54ce6802016-07-13 06:44:41 -0700877 }
tereliusdc35dcd2016-08-01 12:03:27 -0700878 plot->series_list_.back().style = LINE_GRAPH;
terelius54ce6802016-07-13 06:44:41 -0700879
terelius8058e582016-07-25 01:32:41 -0700880 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
881 if (desired_direction == kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700882 plot->series_list_.push_back(TimeSeries());
terelius8058e582016-07-25 01:32:41 -0700883 for (auto& bwe_update : bwe_loss_updates_) {
884 float x =
885 static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
886 float y = static_cast<float>(bwe_update.new_bitrate) / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700887 plot->series_list_.back().points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -0700888 }
tereliusdc35dcd2016-08-01 12:03:27 -0700889 plot->series_list_.back().label = "Loss-based estimate";
890 plot->series_list_.back().style = LINE_GRAPH;
terelius8058e582016-07-25 01:32:41 -0700891 }
tereliusdc35dcd2016-08-01 12:03:27 -0700892 plot->series_list_.back().style = LINE_GRAPH;
893 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
894 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700895 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700896 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700897 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700898 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700899 }
900}
901
902// For each SSRC, plot the bandwidth used by that stream.
903void EventLogAnalyzer::CreateStreamBitrateGraph(
904 PacketDirection desired_direction,
905 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700906 for (auto& kv : rtp_packets_) {
907 StreamId stream_id = kv.first;
908 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
909 // Filter on direction and SSRC.
910 if (stream_id.GetDirection() != desired_direction ||
911 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
912 continue;
terelius54ce6802016-07-13 06:44:41 -0700913 }
914
terelius6addf492016-08-23 17:34:07 -0700915 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200916 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700917 time_series.style = LINE_GRAPH;
918 double bytes_to_kilobits = 8.0 / 1000;
919 MovingAverage<PacketSizeBytes>(packet_stream, begin_time_, end_time_,
920 window_duration_, step_, bytes_to_kilobits,
921 &time_series);
922 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700923 }
924
tereliusdc35dcd2016-08-01 12:03:27 -0700925 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
926 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700927 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700928 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700929 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700930 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700931 }
932}
933
tereliuse34c19c2016-08-15 08:47:14 -0700934void EventLogAnalyzer::CreateBweSimulationGraph(Plot* plot) {
Stefan Holmer13181032016-07-29 14:48:54 +0200935 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
936 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
937
938 for (const auto& kv : rtp_packets_) {
939 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
940 for (const LoggedRtpPacket& rtp_packet : kv.second)
941 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
942 }
943 }
944
945 for (const auto& kv : rtcp_packets_) {
946 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
947 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
948 incoming_rtcp.insert(
949 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
950 }
951 }
952
953 SimulatedClock clock(0);
954 BitrateObserver observer;
955 RtcEventLogNullImpl null_event_log;
956 CongestionController cc(&clock, &observer, &observer, &null_event_log);
957 // TODO(holmer): Log the call config and use that here instead.
958 static const uint32_t kDefaultStartBitrateBps = 300000;
959 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
960
961 TimeSeries time_series;
tereliuse34c19c2016-08-15 08:47:14 -0700962 time_series.label = "Delay-based estimate";
Stefan Holmer13181032016-07-29 14:48:54 +0200963 time_series.style = LINE_DOT_GRAPH;
Stefan Holmer60e43462016-09-07 09:58:20 +0200964 TimeSeries acked_time_series;
965 acked_time_series.label = "Acked bitrate";
966 acked_time_series.style = LINE_DOT_GRAPH;
Stefan Holmer13181032016-07-29 14:48:54 +0200967
968 auto rtp_iterator = outgoing_rtp.begin();
969 auto rtcp_iterator = incoming_rtcp.begin();
970
971 auto NextRtpTime = [&]() {
972 if (rtp_iterator != outgoing_rtp.end())
973 return static_cast<int64_t>(rtp_iterator->first);
974 return std::numeric_limits<int64_t>::max();
975 };
976
977 auto NextRtcpTime = [&]() {
978 if (rtcp_iterator != incoming_rtcp.end())
979 return static_cast<int64_t>(rtcp_iterator->first);
980 return std::numeric_limits<int64_t>::max();
981 };
982
983 auto NextProcessTime = [&]() {
984 if (rtcp_iterator != incoming_rtcp.end() ||
985 rtp_iterator != outgoing_rtp.end()) {
986 return clock.TimeInMicroseconds() +
987 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
988 }
989 return std::numeric_limits<int64_t>::max();
990 };
991
Stefan Holmer60e43462016-09-07 09:58:20 +0200992 RateStatistics acked_bitrate(1000, 8000);
993
Stefan Holmer13181032016-07-29 14:48:54 +0200994 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
995 while (time_us != std::numeric_limits<int64_t>::max()) {
996 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
997 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -0700998 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200999 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1000 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001001 TransportFeedbackObserver* observer = cc.GetTransportFeedbackObserver();
1002 observer->OnTransportFeedback(*static_cast<rtcp::TransportFeedback*>(
1003 rtcp.packet.get()));
1004 std::vector<PacketInfo> feedback =
1005 observer->GetTransportFeedbackVector();
1006 rtc::Optional<uint32_t> bitrate_bps;
1007 if (!feedback.empty()) {
1008 for (const PacketInfo& packet : feedback)
1009 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1010 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
1011 }
1012 uint32_t y = 0;
1013 if (bitrate_bps)
1014 y = *bitrate_bps / 1000;
1015 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1016 1000000;
1017 acked_time_series.points.emplace_back(x, y);
Stefan Holmer13181032016-07-29 14:48:54 +02001018 }
1019 ++rtcp_iterator;
1020 }
1021 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001022 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001023 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1024 if (rtp.header.extension.hasTransportSequenceNumber) {
1025 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1026 cc.GetTransportFeedbackObserver()->AddPacket(
stefana93d5ac2016-08-17 02:14:32 -07001027 rtp.header.extension.transportSequenceNumber, rtp.total_length,
1028 PacketInfo::kNotAProbe);
Stefan Holmer13181032016-07-29 14:48:54 +02001029 rtc::SentPacket sent_packet(
1030 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1031 cc.OnSentPacket(sent_packet);
1032 }
1033 ++rtp_iterator;
1034 }
stefanc3de0332016-08-02 07:22:17 -07001035 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1036 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001037 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001038 }
Stefan Holmer13181032016-07-29 14:48:54 +02001039 if (observer.GetAndResetBitrateUpdated()) {
1040 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +02001041 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1042 1000000;
1043 time_series.points.emplace_back(x, y);
1044 }
1045 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1046 }
1047 // Add the data set to the plot.
tereliusdc35dcd2016-08-01 12:03:27 -07001048 plot->series_list_.push_back(std::move(time_series));
Stefan Holmer60e43462016-09-07 09:58:20 +02001049 plot->series_list_.push_back(std::move(acked_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001050
tereliusdc35dcd2016-08-01 12:03:27 -07001051 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1052 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1053 plot->SetTitle("Simulated BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001054}
1055
tereliuse34c19c2016-08-15 08:47:14 -07001056void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanc3de0332016-08-02 07:22:17 -07001057 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1058 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
1059
1060 for (const auto& kv : rtp_packets_) {
1061 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1062 for (const LoggedRtpPacket& rtp_packet : kv.second)
1063 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1064 }
1065 }
1066
1067 for (const auto& kv : rtcp_packets_) {
1068 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1069 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1070 incoming_rtcp.insert(
1071 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1072 }
1073 }
1074
1075 SimulatedClock clock(0);
stefan5ec85fb2016-09-29 04:19:38 -07001076 TransportFeedbackAdapter feedback_adapter(&clock);
stefanc3de0332016-08-02 07:22:17 -07001077
1078 TimeSeries time_series;
1079 time_series.label = "Network Delay Change";
1080 time_series.style = LINE_DOT_GRAPH;
1081 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1082
1083 auto rtp_iterator = outgoing_rtp.begin();
1084 auto rtcp_iterator = incoming_rtcp.begin();
1085
1086 auto NextRtpTime = [&]() {
1087 if (rtp_iterator != outgoing_rtp.end())
1088 return static_cast<int64_t>(rtp_iterator->first);
1089 return std::numeric_limits<int64_t>::max();
1090 };
1091
1092 auto NextRtcpTime = [&]() {
1093 if (rtcp_iterator != incoming_rtcp.end())
1094 return static_cast<int64_t>(rtcp_iterator->first);
1095 return std::numeric_limits<int64_t>::max();
1096 };
1097
1098 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
1099 while (time_us != std::numeric_limits<int64_t>::max()) {
1100 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1101 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1102 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1103 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1104 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001105 feedback_adapter.OnTransportFeedback(
1106 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
stefanc3de0332016-08-02 07:22:17 -07001107 std::vector<PacketInfo> feedback =
Stefan Holmer60e43462016-09-07 09:58:20 +02001108 feedback_adapter.GetTransportFeedbackVector();
stefanc3de0332016-08-02 07:22:17 -07001109 for (const PacketInfo& packet : feedback) {
1110 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1111 float x =
1112 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1113 1000000;
1114 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1115 time_series.points.emplace_back(x, y);
1116 }
1117 }
1118 ++rtcp_iterator;
1119 }
1120 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1121 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1122 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1123 if (rtp.header.extension.hasTransportSequenceNumber) {
1124 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1125 feedback_adapter.AddPacket(rtp.header.extension.transportSequenceNumber,
1126 rtp.total_length, 0);
1127 feedback_adapter.OnSentPacket(
1128 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1129 }
1130 ++rtp_iterator;
1131 }
1132 time_us = std::min(NextRtpTime(), NextRtcpTime());
1133 }
1134 // We assume that the base network delay (w/o queues) is the min delay
1135 // observed during the call.
1136 for (TimeSeriesPoint& point : time_series.points)
1137 point.y -= estimated_base_delay_ms;
1138 // Add the data set to the plot.
1139 plot->series_list_.push_back(std::move(time_series));
1140
1141 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1142 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1143 plot->SetTitle("Network Delay Change.");
1144}
terelius54ce6802016-07-13 06:44:41 -07001145} // namespace plotting
1146} // namespace webrtc