blob: 5a8733c074fe0d89bf5da906d3533b67ce78a766 [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
tereliusdc35dcd2016-08-01 12:03:27 -0700103constexpr float kLeftMargin = 0.01f;
104constexpr float kRightMargin = 0.02f;
105constexpr float kBottomMargin = 0.02f;
106constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700107
terelius6addf492016-08-23 17:34:07 -0700108class PacketSizeBytes {
109 public:
110 using DataType = LoggedRtpPacket;
111 using ResultType = size_t;
112 size_t operator()(const LoggedRtpPacket& packet) {
113 return packet.total_length;
114 }
115};
116
117class SequenceNumberDiff {
118 public:
119 using DataType = LoggedRtpPacket;
120 using ResultType = int64_t;
121 int64_t operator()(const LoggedRtpPacket& old_packet,
122 const LoggedRtpPacket& new_packet) {
123 return WrappingDifference(new_packet.header.sequenceNumber,
124 old_packet.header.sequenceNumber, 1ul << 16);
125 }
126};
127
tereliusccbbf8d2016-08-10 07:34:28 -0700128class NetworkDelayDiff {
129 public:
130 class AbsSendTime {
131 public:
132 using DataType = LoggedRtpPacket;
133 using ResultType = double;
134 double operator()(const LoggedRtpPacket& old_packet,
135 const LoggedRtpPacket& new_packet) {
136 if (old_packet.header.extension.hasAbsoluteSendTime &&
137 new_packet.header.extension.hasAbsoluteSendTime) {
138 int64_t send_time_diff = WrappingDifference(
139 new_packet.header.extension.absoluteSendTime,
140 old_packet.header.extension.absoluteSendTime, 1ul << 24);
141 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
142 return static_cast<double>(recv_time_diff -
143 AbsSendTimeToMicroseconds(send_time_diff)) /
144 1000;
145 } else {
146 return 0;
147 }
148 }
149 };
150
151 class CaptureTime {
152 public:
153 using DataType = LoggedRtpPacket;
154 using ResultType = double;
155 double operator()(const LoggedRtpPacket& old_packet,
156 const LoggedRtpPacket& new_packet) {
157 int64_t send_time_diff = WrappingDifference(
158 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
159 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
160
161 const double kVideoSampleRate = 90000;
162 // TODO(terelius): We treat all streams as video for now, even though
163 // audio might be sampled at e.g. 16kHz, because it is really difficult to
164 // figure out the true sampling rate of a stream. The effect is that the
165 // delay will be scaled incorrectly for non-video streams.
166
167 double delay_change =
168 static_cast<double>(recv_time_diff) / 1000 -
169 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
terelius6addf492016-08-23 17:34:07 -0700170 if (delay_change < -10000 || 10000 < delay_change) {
171 LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
172 LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
173 << ", received time " << old_packet.timestamp;
174 LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
175 << ", received time " << new_packet.timestamp;
176 LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
177 << static_cast<double>(recv_time_diff) / 1000000 << "s";
178 LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
179 << static_cast<double>(send_time_diff) /
180 kVideoSampleRate
181 << "s";
182 }
tereliusccbbf8d2016-08-10 07:34:28 -0700183 return delay_change;
184 }
185 };
186};
187
188template <typename Extractor>
189class Accumulated {
190 public:
191 using DataType = typename Extractor::DataType;
192 using ResultType = typename Extractor::ResultType;
193 ResultType operator()(const DataType& old_packet,
194 const DataType& new_packet) {
195 sum += extract(old_packet, new_packet);
196 return sum;
197 }
198
199 private:
200 Extractor extract;
201 ResultType sum = 0;
202};
203
terelius6addf492016-08-23 17:34:07 -0700204// For each element in data, use |Extractor| to extract a y-coordinate and
205// store the result in a TimeSeries.
206template <typename Extractor>
207void Pointwise(const std::vector<typename Extractor::DataType>& data,
208 uint64_t begin_time,
209 TimeSeries* result) {
210 Extractor extract;
211 for (size_t i = 0; i < data.size(); i++) {
212 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
213 float y = extract(data[i]);
214 result->points.emplace_back(x, y);
215 }
216}
217
218// For each pair of adjacent elements in |data|, use |Extractor| to extract a
219// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
220// will be the time of the second element in the pair.
tereliusccbbf8d2016-08-10 07:34:28 -0700221template <typename Extractor>
222void Pairwise(const std::vector<typename Extractor::DataType>& data,
223 uint64_t begin_time,
224 TimeSeries* result) {
225 Extractor extract;
226 for (size_t i = 1; i < data.size(); i++) {
227 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
228 float y = extract(data[i - 1], data[i]);
229 result->points.emplace_back(x, y);
230 }
231}
232
terelius6addf492016-08-23 17:34:07 -0700233// Calculates a moving average of |data| and stores the result in a TimeSeries.
234// A data point is generated every |step| microseconds from |begin_time|
235// to |end_time|. The value of each data point is the average of the data
236// during the preceeding |window_duration_us| microseconds.
237template <typename Extractor>
238void MovingAverage(const std::vector<typename Extractor::DataType>& data,
239 uint64_t begin_time,
240 uint64_t end_time,
241 uint64_t window_duration_us,
242 uint64_t step,
243 float y_scaling,
244 webrtc::plotting::TimeSeries* result) {
245 size_t window_index_begin = 0;
246 size_t window_index_end = 0;
247 typename Extractor::ResultType sum_in_window = 0;
248 Extractor extract;
249
250 for (uint64_t t = begin_time; t < end_time + step; t += step) {
251 while (window_index_end < data.size() &&
252 data[window_index_end].timestamp < t) {
253 sum_in_window += extract(data[window_index_end]);
254 ++window_index_end;
255 }
256 while (window_index_begin < data.size() &&
257 data[window_index_begin].timestamp < t - window_duration_us) {
258 sum_in_window -= extract(data[window_index_begin]);
259 ++window_index_begin;
260 }
261 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
262 float x = static_cast<float>(t - begin_time) / 1000000;
263 float y = sum_in_window / window_duration_s * y_scaling;
264 result->points.emplace_back(x, y);
265 }
266}
267
terelius54ce6802016-07-13 06:44:41 -0700268} // namespace
269
terelius54ce6802016-07-13 06:44:41 -0700270EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
271 : parsed_log_(log), window_duration_(250000), step_(10000) {
272 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
273 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700274
Stefan Holmer13181032016-07-29 14:48:54 +0200275 // Maps a stream identifier consisting of ssrc and direction
terelius88e64e52016-07-19 01:51:06 -0700276 // to the header extensions used by that stream,
277 std::map<StreamId, RtpHeaderExtensionMap> extension_maps;
278
279 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700280 uint8_t header[IP_PACKET_SIZE];
281 size_t header_length;
282 size_t total_length;
283
terelius54ce6802016-07-13 06:44:41 -0700284 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
285 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700286 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
287 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
288 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700289 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
290 event_type != ParsedRtcEventLog::LOG_START &&
291 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700292 uint64_t timestamp = parsed_log_.GetTimestamp(i);
293 first_timestamp = std::min(first_timestamp, timestamp);
294 last_timestamp = std::max(last_timestamp, timestamp);
295 }
296
297 switch (parsed_log_.GetEventType(i)) {
298 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
299 VideoReceiveStream::Config config(nullptr);
300 parsed_log_.GetVideoReceiveConfig(i, &config);
Stefan Holmer13181032016-07-29 14:48:54 +0200301 StreamId stream(config.rtp.remote_ssrc, kIncomingPacket);
stefan6a850c32016-07-29 10:28:08 -0700302 RegisterHeaderExtensions(config.rtp.extensions,
303 &extension_maps[stream]);
terelius0740a202016-08-08 10:21:04 -0700304 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700305 for (auto kv : config.rtp.rtx) {
306 StreamId rtx_stream(kv.second.ssrc, kIncomingPacket);
307 RegisterHeaderExtensions(config.rtp.extensions,
308 &extension_maps[rtx_stream]);
terelius0740a202016-08-08 10:21:04 -0700309 video_ssrcs_.insert(rtx_stream);
310 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700311 }
312 break;
313 }
314 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
315 VideoSendStream::Config config(nullptr);
316 parsed_log_.GetVideoSendConfig(i, &config);
317 for (auto ssrc : config.rtp.ssrcs) {
Stefan Holmer13181032016-07-29 14:48:54 +0200318 StreamId stream(ssrc, kOutgoingPacket);
stefan6a850c32016-07-29 10:28:08 -0700319 RegisterHeaderExtensions(config.rtp.extensions,
320 &extension_maps[stream]);
terelius0740a202016-08-08 10:21:04 -0700321 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700322 }
323 for (auto ssrc : config.rtp.rtx.ssrcs) {
terelius0740a202016-08-08 10:21:04 -0700324 StreamId rtx_stream(ssrc, kOutgoingPacket);
stefan6a850c32016-07-29 10:28:08 -0700325 RegisterHeaderExtensions(config.rtp.extensions,
terelius0740a202016-08-08 10:21:04 -0700326 &extension_maps[rtx_stream]);
327 video_ssrcs_.insert(rtx_stream);
328 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700329 }
330 break;
331 }
332 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
333 AudioReceiveStream::Config config;
334 // TODO(terelius): Parse the audio configs once we have them.
335 break;
336 }
337 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
338 AudioSendStream::Config config(nullptr);
339 // TODO(terelius): Parse the audio configs once we have them.
340 break;
341 }
342 case ParsedRtcEventLog::RTP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200343 MediaType media_type;
terelius88e64e52016-07-19 01:51:06 -0700344 parsed_log_.GetRtpHeader(i, &direction, &media_type, header,
345 &header_length, &total_length);
346 // Parse header to get SSRC.
347 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
348 RTPHeader parsed_header;
349 rtp_parser.Parse(&parsed_header);
Stefan Holmer13181032016-07-29 14:48:54 +0200350 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700351 // Look up the extension_map and parse it again to get the extensions.
352 if (extension_maps.count(stream) == 1) {
353 RtpHeaderExtensionMap* extension_map = &extension_maps[stream];
354 rtp_parser.Parse(&parsed_header, extension_map);
355 }
356 uint64_t timestamp = parsed_log_.GetTimestamp(i);
357 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200358 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700359 break;
360 }
361 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200362 uint8_t packet[IP_PACKET_SIZE];
363 MediaType media_type;
364 parsed_log_.GetRtcpPacket(i, &direction, &media_type, packet,
365 &total_length);
366
367 RtpUtility::RtpHeaderParser rtp_parser(packet, total_length);
368 RTPHeader parsed_header;
369 RTC_CHECK(rtp_parser.ParseRtcp(&parsed_header));
370 uint32_t ssrc = parsed_header.ssrc;
371
372 RTCPUtility::RTCPParserV2 rtcp_parser(packet, total_length, true);
373 RTC_CHECK(rtcp_parser.IsValid());
374
375 RTCPUtility::RTCPPacketTypes packet_type = rtcp_parser.Begin();
376 while (packet_type != RTCPUtility::RTCPPacketTypes::kInvalid) {
377 switch (packet_type) {
378 case RTCPUtility::RTCPPacketTypes::kTransportFeedback: {
379 // Currently feedback is logged twice, both for audio and video.
380 // Only act on one of them.
381 if (media_type == MediaType::VIDEO) {
382 std::unique_ptr<rtcp::RtcpPacket> rtcp_packet(
383 rtcp_parser.ReleaseRtcpPacket());
384 StreamId stream(ssrc, direction);
385 uint64_t timestamp = parsed_log_.GetTimestamp(i);
386 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
387 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
388 }
389 break;
390 }
391 default:
392 break;
393 }
394 rtcp_parser.Iterate();
395 packet_type = rtcp_parser.PacketType();
396 }
terelius88e64e52016-07-19 01:51:06 -0700397 break;
398 }
399 case ParsedRtcEventLog::LOG_START: {
400 break;
401 }
402 case ParsedRtcEventLog::LOG_END: {
403 break;
404 }
405 case ParsedRtcEventLog::BWE_PACKET_LOSS_EVENT: {
terelius8058e582016-07-25 01:32:41 -0700406 BwePacketLossEvent bwe_update;
407 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
408 parsed_log_.GetBwePacketLossEvent(i, &bwe_update.new_bitrate,
409 &bwe_update.fraction_loss,
410 &bwe_update.expected_packets);
411 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700412 break;
413 }
414 case ParsedRtcEventLog::BWE_PACKET_DELAY_EVENT: {
415 break;
416 }
417 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
418 break;
419 }
420 case ParsedRtcEventLog::UNKNOWN_EVENT: {
421 break;
422 }
423 }
terelius54ce6802016-07-13 06:44:41 -0700424 }
terelius88e64e52016-07-19 01:51:06 -0700425
terelius54ce6802016-07-13 06:44:41 -0700426 if (last_timestamp < first_timestamp) {
427 // No useful events in the log.
428 first_timestamp = last_timestamp = 0;
429 }
430 begin_time_ = first_timestamp;
431 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700432 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
terelius54ce6802016-07-13 06:44:41 -0700433}
434
Stefan Holmer13181032016-07-29 14:48:54 +0200435class BitrateObserver : public CongestionController::Observer,
436 public RemoteBitrateObserver {
437 public:
438 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
439
440 void OnNetworkChanged(uint32_t bitrate_bps,
441 uint8_t fraction_loss,
442 int64_t rtt_ms) override {
443 last_bitrate_bps_ = bitrate_bps;
444 bitrate_updated_ = true;
445 }
446
447 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
448 uint32_t bitrate) override {}
449
450 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
451 bool GetAndResetBitrateUpdated() {
452 bool bitrate_updated = bitrate_updated_;
453 bitrate_updated_ = false;
454 return bitrate_updated;
455 }
456
457 private:
458 uint32_t last_bitrate_bps_;
459 bool bitrate_updated_;
460};
461
Stefan Holmer99f8e082016-09-09 13:37:50 +0200462bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700463 return rtx_ssrcs_.count(stream_id) == 1;
464}
465
Stefan Holmer99f8e082016-09-09 13:37:50 +0200466bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700467 return video_ssrcs_.count(stream_id) == 1;
468}
469
Stefan Holmer99f8e082016-09-09 13:37:50 +0200470bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700471 return audio_ssrcs_.count(stream_id) == 1;
472}
473
Stefan Holmer99f8e082016-09-09 13:37:50 +0200474std::string EventLogAnalyzer::GetStreamName(StreamId stream_id) const {
475 std::stringstream name;
476 if (IsAudioSsrc(stream_id)) {
477 name << "Audio ";
478 } else if (IsVideoSsrc(stream_id)) {
479 name << "Video ";
480 } else {
481 name << "Unknown ";
482 }
483 if (IsRtxSsrc(stream_id))
484 name << "RTX ";
485 name << SsrcToString(stream_id.GetSsrc());
486 return name.str();
487}
488
terelius54ce6802016-07-13 06:44:41 -0700489void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
490 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700491 for (auto& kv : rtp_packets_) {
492 StreamId stream_id = kv.first;
493 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
494 // Filter on direction and SSRC.
495 if (stream_id.GetDirection() != desired_direction ||
496 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
497 continue;
terelius54ce6802016-07-13 06:44:41 -0700498 }
terelius54ce6802016-07-13 06:44:41 -0700499
terelius6addf492016-08-23 17:34:07 -0700500 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200501 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700502 time_series.style = BAR_GRAPH;
503 Pointwise<PacketSizeBytes>(packet_stream, begin_time_, &time_series);
504 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700505 }
506
tereliusdc35dcd2016-08-01 12:03:27 -0700507 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
508 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
509 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700510 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700511 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700512 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700513 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700514 }
515}
516
philipelccd74892016-09-05 02:46:25 -0700517template <typename T>
518void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
519 PacketDirection desired_direction,
520 Plot* plot,
521 const std::map<StreamId, std::vector<T>>& packets,
522 const std::string& label_prefix) {
523 for (auto& kv : packets) {
524 StreamId stream_id = kv.first;
525 const std::vector<T>& 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;
530 }
531
532 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200533 time_series.label = label_prefix + " " + GetStreamName(stream_id);
philipelccd74892016-09-05 02:46:25 -0700534 time_series.style = LINE_GRAPH;
535
536 for (size_t i = 0; i < packet_stream.size(); i++) {
537 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
538 1000000;
539 time_series.points.emplace_back(x, i);
540 time_series.points.emplace_back(x, i + 1);
541 }
542
543 plot->series_list_.push_back(std::move(time_series));
544 }
545}
546
547void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
548 PacketDirection desired_direction,
549 Plot* plot) {
550 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
551 "RTP");
552 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
553 "RTCP");
554
555 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
556 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
557 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
558 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
559 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
560 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
561 }
562}
563
terelius54ce6802016-07-13 06:44:41 -0700564// For each SSRC, plot the time between the consecutive playouts.
565void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
566 std::map<uint32_t, TimeSeries> time_series;
567 std::map<uint32_t, uint64_t> last_playout;
568
569 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700570
571 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
572 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
573 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
574 parsed_log_.GetAudioPlayout(i, &ssrc);
575 uint64_t timestamp = parsed_log_.GetTimestamp(i);
576 if (MatchingSsrc(ssrc, desired_ssrc_)) {
577 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
578 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
579 if (time_series[ssrc].points.size() == 0) {
580 // There were no previusly logged playout for this SSRC.
581 // Generate a point, but place it on the x-axis.
582 y = 0;
583 }
terelius54ce6802016-07-13 06:44:41 -0700584 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
585 last_playout[ssrc] = timestamp;
586 }
587 }
588 }
589
590 // Set labels and put in graph.
591 for (auto& kv : time_series) {
592 kv.second.label = SsrcToString(kv.first);
593 kv.second.style = BAR_GRAPH;
tereliusdc35dcd2016-08-01 12:03:27 -0700594 plot->series_list_.push_back(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700595 }
596
tereliusdc35dcd2016-08-01 12:03:27 -0700597 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
598 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
599 kTopMargin);
600 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700601}
602
603// For each SSRC, plot the time between the consecutive playouts.
604void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700605 for (auto& kv : rtp_packets_) {
606 StreamId stream_id = kv.first;
607 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
608 // Filter on direction and SSRC.
609 if (stream_id.GetDirection() != kIncomingPacket ||
610 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
611 continue;
terelius54ce6802016-07-13 06:44:41 -0700612 }
terelius54ce6802016-07-13 06:44:41 -0700613
terelius6addf492016-08-23 17:34:07 -0700614 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200615 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700616 time_series.style = BAR_GRAPH;
617 Pairwise<SequenceNumberDiff>(packet_stream, begin_time_, &time_series);
618 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700619 }
620
tereliusdc35dcd2016-08-01 12:03:27 -0700621 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
622 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
623 kTopMargin);
624 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700625}
626
Stefan Holmer99f8e082016-09-09 13:37:50 +0200627void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
628 for (auto& kv : rtp_packets_) {
629 StreamId stream_id = kv.first;
630 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
631 // Filter on direction and SSRC.
632 if (stream_id.GetDirection() != kIncomingPacket ||
633 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
634 continue;
635 }
636
637 TimeSeries time_series;
638 time_series.label = GetStreamName(stream_id);
639 time_series.style = LINE_DOT_GRAPH;
640 const uint64_t kWindowUs = 1000000;
641 const LoggedRtpPacket* first_in_window = &packet_stream.front();
642 const LoggedRtpPacket* last_in_window = &packet_stream.front();
643 int packets_in_window = 0;
644 for (const LoggedRtpPacket& packet : packet_stream) {
645 if (packet.timestamp > first_in_window->timestamp + kWindowUs) {
646 uint16_t expected_num_packets = last_in_window->header.sequenceNumber -
647 first_in_window->header.sequenceNumber + 1;
648 float fraction_lost = (expected_num_packets - packets_in_window) /
649 static_cast<float>(expected_num_packets);
650 float y = fraction_lost * 100;
651 float x =
652 static_cast<float>(last_in_window->timestamp - begin_time_) /
653 1000000;
654 time_series.points.emplace_back(x, y);
655 first_in_window = &packet;
656 last_in_window = &packet;
657 packets_in_window = 1;
658 continue;
659 }
660 ++packets_in_window;
661 last_in_window = &packet;
662 }
663 plot->series_list_.push_back(std::move(time_series));
664 }
665
666 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
667 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
668 kTopMargin);
669 plot->SetTitle("Estimated incoming loss rate");
670}
671
terelius54ce6802016-07-13 06:44:41 -0700672void EventLogAnalyzer::CreateDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700673 for (auto& kv : rtp_packets_) {
674 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700675 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700676 // Filter on direction and SSRC.
677 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200678 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
679 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
680 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700681 continue;
682 }
terelius54ce6802016-07-13 06:44:41 -0700683
tereliusccbbf8d2016-08-10 07:34:28 -0700684 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200685 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700686 capture_time_data.style = BAR_GRAPH;
687 Pairwise<NetworkDelayDiff::CaptureTime>(packet_stream, begin_time_,
688 &capture_time_data);
689 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700690
tereliusccbbf8d2016-08-10 07:34:28 -0700691 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200692 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700693 send_time_data.style = BAR_GRAPH;
694 Pairwise<NetworkDelayDiff::AbsSendTime>(packet_stream, begin_time_,
695 &send_time_data);
696 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700697 }
698
tereliusdc35dcd2016-08-01 12:03:27 -0700699 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
700 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
701 kTopMargin);
702 plot->SetTitle("Network latency change between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700703}
704
705void EventLogAnalyzer::CreateAccumulatedDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700706 for (auto& kv : rtp_packets_) {
707 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700708 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700709 // Filter on direction and SSRC.
710 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200711 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
712 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
713 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700714 continue;
715 }
terelius54ce6802016-07-13 06:44:41 -0700716
tereliusccbbf8d2016-08-10 07:34:28 -0700717 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200718 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700719 capture_time_data.style = LINE_GRAPH;
720 Pairwise<Accumulated<NetworkDelayDiff::CaptureTime>>(
721 packet_stream, begin_time_, &capture_time_data);
722 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700723
tereliusccbbf8d2016-08-10 07:34:28 -0700724 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200725 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700726 send_time_data.style = LINE_GRAPH;
727 Pairwise<Accumulated<NetworkDelayDiff::AbsSendTime>>(
728 packet_stream, begin_time_, &send_time_data);
729 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700730 }
731
tereliusdc35dcd2016-08-01 12:03:27 -0700732 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
733 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
734 kTopMargin);
735 plot->SetTitle("Accumulated network latency change");
terelius54ce6802016-07-13 06:44:41 -0700736}
737
tereliusf736d232016-08-04 10:00:11 -0700738// Plot the fraction of packets lost (as perceived by the loss-based BWE).
739void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
740 plot->series_list_.push_back(TimeSeries());
741 for (auto& bwe_update : bwe_loss_updates_) {
742 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
743 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
744 plot->series_list_.back().points.emplace_back(x, y);
745 }
746 plot->series_list_.back().label = "Fraction lost";
747 plot->series_list_.back().style = LINE_DOT_GRAPH;
748
749 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
750 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
751 kTopMargin);
752 plot->SetTitle("Reported packet loss");
753}
754
terelius54ce6802016-07-13 06:44:41 -0700755// Plot the total bandwidth used by all RTP streams.
756void EventLogAnalyzer::CreateTotalBitrateGraph(
757 PacketDirection desired_direction,
758 Plot* plot) {
759 struct TimestampSize {
760 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
761 uint64_t timestamp;
762 size_t size;
763 };
764 std::vector<TimestampSize> packets;
765
766 PacketDirection direction;
767 size_t total_length;
768
769 // Extract timestamps and sizes for the relevant packets.
770 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
771 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
772 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
773 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, nullptr,
774 &total_length);
775 if (direction == desired_direction) {
776 uint64_t timestamp = parsed_log_.GetTimestamp(i);
777 packets.push_back(TimestampSize(timestamp, total_length));
778 }
779 }
780 }
781
782 size_t window_index_begin = 0;
783 size_t window_index_end = 0;
784 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700785
786 // Calculate a moving average of the bitrate and store in a TimeSeries.
tereliusdc35dcd2016-08-01 12:03:27 -0700787 plot->series_list_.push_back(TimeSeries());
terelius54ce6802016-07-13 06:44:41 -0700788 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
789 while (window_index_end < packets.size() &&
790 packets[window_index_end].timestamp < time) {
791 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700792 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700793 }
794 while (window_index_begin < packets.size() &&
795 packets[window_index_begin].timestamp < time - window_duration_) {
796 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
797 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700798 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700799 }
800 float window_duration_in_seconds =
801 static_cast<float>(window_duration_) / 1000000;
802 float x = static_cast<float>(time - begin_time_) / 1000000;
803 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700804 plot->series_list_.back().points.push_back(TimeSeriesPoint(x, y));
terelius54ce6802016-07-13 06:44:41 -0700805 }
806
807 // Set labels.
808 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700809 plot->series_list_.back().label = "Incoming bitrate";
terelius54ce6802016-07-13 06:44:41 -0700810 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700811 plot->series_list_.back().label = "Outgoing bitrate";
terelius54ce6802016-07-13 06:44:41 -0700812 }
tereliusdc35dcd2016-08-01 12:03:27 -0700813 plot->series_list_.back().style = LINE_GRAPH;
terelius54ce6802016-07-13 06:44:41 -0700814
terelius8058e582016-07-25 01:32:41 -0700815 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
816 if (desired_direction == kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700817 plot->series_list_.push_back(TimeSeries());
terelius8058e582016-07-25 01:32:41 -0700818 for (auto& bwe_update : bwe_loss_updates_) {
819 float x =
820 static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
821 float y = static_cast<float>(bwe_update.new_bitrate) / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700822 plot->series_list_.back().points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -0700823 }
tereliusdc35dcd2016-08-01 12:03:27 -0700824 plot->series_list_.back().label = "Loss-based estimate";
825 plot->series_list_.back().style = LINE_GRAPH;
terelius8058e582016-07-25 01:32:41 -0700826 }
tereliusdc35dcd2016-08-01 12:03:27 -0700827 plot->series_list_.back().style = LINE_GRAPH;
828 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
829 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700830 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700831 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700832 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700833 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700834 }
835}
836
837// For each SSRC, plot the bandwidth used by that stream.
838void EventLogAnalyzer::CreateStreamBitrateGraph(
839 PacketDirection desired_direction,
840 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700841 for (auto& kv : rtp_packets_) {
842 StreamId stream_id = kv.first;
843 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
844 // Filter on direction and SSRC.
845 if (stream_id.GetDirection() != desired_direction ||
846 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
847 continue;
terelius54ce6802016-07-13 06:44:41 -0700848 }
849
terelius6addf492016-08-23 17:34:07 -0700850 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200851 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700852 time_series.style = LINE_GRAPH;
853 double bytes_to_kilobits = 8.0 / 1000;
854 MovingAverage<PacketSizeBytes>(packet_stream, begin_time_, end_time_,
855 window_duration_, step_, bytes_to_kilobits,
856 &time_series);
857 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700858 }
859
tereliusdc35dcd2016-08-01 12:03:27 -0700860 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
861 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700862 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700863 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700864 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700865 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700866 }
867}
868
tereliuse34c19c2016-08-15 08:47:14 -0700869void EventLogAnalyzer::CreateBweSimulationGraph(Plot* plot) {
Stefan Holmer13181032016-07-29 14:48:54 +0200870 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
871 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
872
873 for (const auto& kv : rtp_packets_) {
874 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
875 for (const LoggedRtpPacket& rtp_packet : kv.second)
876 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
877 }
878 }
879
880 for (const auto& kv : rtcp_packets_) {
881 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
882 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
883 incoming_rtcp.insert(
884 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
885 }
886 }
887
888 SimulatedClock clock(0);
889 BitrateObserver observer;
890 RtcEventLogNullImpl null_event_log;
891 CongestionController cc(&clock, &observer, &observer, &null_event_log);
892 // TODO(holmer): Log the call config and use that here instead.
893 static const uint32_t kDefaultStartBitrateBps = 300000;
894 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
895
896 TimeSeries time_series;
tereliuse34c19c2016-08-15 08:47:14 -0700897 time_series.label = "Delay-based estimate";
Stefan Holmer13181032016-07-29 14:48:54 +0200898 time_series.style = LINE_DOT_GRAPH;
Stefan Holmer60e43462016-09-07 09:58:20 +0200899 TimeSeries acked_time_series;
900 acked_time_series.label = "Acked bitrate";
901 acked_time_series.style = LINE_DOT_GRAPH;
Stefan Holmer13181032016-07-29 14:48:54 +0200902
903 auto rtp_iterator = outgoing_rtp.begin();
904 auto rtcp_iterator = incoming_rtcp.begin();
905
906 auto NextRtpTime = [&]() {
907 if (rtp_iterator != outgoing_rtp.end())
908 return static_cast<int64_t>(rtp_iterator->first);
909 return std::numeric_limits<int64_t>::max();
910 };
911
912 auto NextRtcpTime = [&]() {
913 if (rtcp_iterator != incoming_rtcp.end())
914 return static_cast<int64_t>(rtcp_iterator->first);
915 return std::numeric_limits<int64_t>::max();
916 };
917
918 auto NextProcessTime = [&]() {
919 if (rtcp_iterator != incoming_rtcp.end() ||
920 rtp_iterator != outgoing_rtp.end()) {
921 return clock.TimeInMicroseconds() +
922 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
923 }
924 return std::numeric_limits<int64_t>::max();
925 };
926
Stefan Holmer60e43462016-09-07 09:58:20 +0200927 RateStatistics acked_bitrate(1000, 8000);
928
Stefan Holmer13181032016-07-29 14:48:54 +0200929 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
930 while (time_us != std::numeric_limits<int64_t>::max()) {
931 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
932 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -0700933 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200934 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
935 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +0200936 TransportFeedbackObserver* observer = cc.GetTransportFeedbackObserver();
937 observer->OnTransportFeedback(*static_cast<rtcp::TransportFeedback*>(
938 rtcp.packet.get()));
939 std::vector<PacketInfo> feedback =
940 observer->GetTransportFeedbackVector();
941 rtc::Optional<uint32_t> bitrate_bps;
942 if (!feedback.empty()) {
943 for (const PacketInfo& packet : feedback)
944 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
945 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
946 }
947 uint32_t y = 0;
948 if (bitrate_bps)
949 y = *bitrate_bps / 1000;
950 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
951 1000000;
952 acked_time_series.points.emplace_back(x, y);
Stefan Holmer13181032016-07-29 14:48:54 +0200953 }
954 ++rtcp_iterator;
955 }
956 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -0700957 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200958 const LoggedRtpPacket& rtp = *rtp_iterator->second;
959 if (rtp.header.extension.hasTransportSequenceNumber) {
960 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
961 cc.GetTransportFeedbackObserver()->AddPacket(
stefana93d5ac2016-08-17 02:14:32 -0700962 rtp.header.extension.transportSequenceNumber, rtp.total_length,
963 PacketInfo::kNotAProbe);
Stefan Holmer13181032016-07-29 14:48:54 +0200964 rtc::SentPacket sent_packet(
965 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
966 cc.OnSentPacket(sent_packet);
967 }
968 ++rtp_iterator;
969 }
stefanc3de0332016-08-02 07:22:17 -0700970 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
971 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200972 cc.Process();
stefanc3de0332016-08-02 07:22:17 -0700973 }
Stefan Holmer13181032016-07-29 14:48:54 +0200974 if (observer.GetAndResetBitrateUpdated()) {
975 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +0200976 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
977 1000000;
978 time_series.points.emplace_back(x, y);
979 }
980 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
981 }
982 // Add the data set to the plot.
tereliusdc35dcd2016-08-01 12:03:27 -0700983 plot->series_list_.push_back(std::move(time_series));
Stefan Holmer60e43462016-09-07 09:58:20 +0200984 plot->series_list_.push_back(std::move(acked_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +0200985
tereliusdc35dcd2016-08-01 12:03:27 -0700986 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
987 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
988 plot->SetTitle("Simulated BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +0200989}
990
tereliuse34c19c2016-08-15 08:47:14 -0700991void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanc3de0332016-08-02 07:22:17 -0700992 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
993 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
994
995 for (const auto& kv : rtp_packets_) {
996 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
997 for (const LoggedRtpPacket& rtp_packet : kv.second)
998 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
999 }
1000 }
1001
1002 for (const auto& kv : rtcp_packets_) {
1003 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1004 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1005 incoming_rtcp.insert(
1006 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1007 }
1008 }
1009
1010 SimulatedClock clock(0);
1011 TransportFeedbackAdapter feedback_adapter(nullptr, &clock);
1012
1013 TimeSeries time_series;
1014 time_series.label = "Network Delay Change";
1015 time_series.style = LINE_DOT_GRAPH;
1016 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1017
1018 auto rtp_iterator = outgoing_rtp.begin();
1019 auto rtcp_iterator = incoming_rtcp.begin();
1020
1021 auto NextRtpTime = [&]() {
1022 if (rtp_iterator != outgoing_rtp.end())
1023 return static_cast<int64_t>(rtp_iterator->first);
1024 return std::numeric_limits<int64_t>::max();
1025 };
1026
1027 auto NextRtcpTime = [&]() {
1028 if (rtcp_iterator != incoming_rtcp.end())
1029 return static_cast<int64_t>(rtcp_iterator->first);
1030 return std::numeric_limits<int64_t>::max();
1031 };
1032
1033 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
1034 while (time_us != std::numeric_limits<int64_t>::max()) {
1035 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1036 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1037 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1038 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1039 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001040 feedback_adapter.OnTransportFeedback(
1041 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
stefanc3de0332016-08-02 07:22:17 -07001042 std::vector<PacketInfo> feedback =
Stefan Holmer60e43462016-09-07 09:58:20 +02001043 feedback_adapter.GetTransportFeedbackVector();
stefanc3de0332016-08-02 07:22:17 -07001044 for (const PacketInfo& packet : feedback) {
1045 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1046 float x =
1047 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1048 1000000;
1049 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1050 time_series.points.emplace_back(x, y);
1051 }
1052 }
1053 ++rtcp_iterator;
1054 }
1055 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1056 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1057 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1058 if (rtp.header.extension.hasTransportSequenceNumber) {
1059 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1060 feedback_adapter.AddPacket(rtp.header.extension.transportSequenceNumber,
1061 rtp.total_length, 0);
1062 feedback_adapter.OnSentPacket(
1063 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1064 }
1065 ++rtp_iterator;
1066 }
1067 time_us = std::min(NextRtpTime(), NextRtcpTime());
1068 }
1069 // We assume that the base network delay (w/o queues) is the min delay
1070 // observed during the call.
1071 for (TimeSeriesPoint& point : time_series.points)
1072 point.y -= estimated_base_delay_ms;
1073 // Add the data set to the plot.
1074 plot->series_list_.push_back(std::move(time_series));
1075
1076 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1077 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1078 plot->SetTitle("Network Delay Change.");
1079}
terelius54ce6802016-07-13 06:44:41 -07001080} // namespace plotting
1081} // namespace webrtc