blob: 60051a775e2051f4da5ec134f5dfc55df62b6c35 [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
20#include "webrtc/audio_receive_stream.h"
21#include "webrtc/audio_send_stream.h"
22#include "webrtc/base/checks.h"
stefan6a850c32016-07-29 10:28:08 -070023#include "webrtc/base/logging.h"
terelius54ce6802016-07-13 06:44:41 -070024#include "webrtc/call.h"
25#include "webrtc/common_types.h"
Stefan Holmer13181032016-07-29 14:48:54 +020026#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
terelius54ce6802016-07-13 06:44:41 -070027#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
28#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
29#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
Stefan Holmer13181032016-07-29 14:48:54 +020030#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
31#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
terelius54ce6802016-07-13 06:44:41 -070032#include "webrtc/video_receive_stream.h"
33#include "webrtc/video_send_stream.h"
34
tereliusdc35dcd2016-08-01 12:03:27 -070035namespace webrtc {
36namespace plotting {
37
terelius54ce6802016-07-13 06:44:41 -070038namespace {
39
40std::string SsrcToString(uint32_t ssrc) {
41 std::stringstream ss;
42 ss << "SSRC " << ssrc;
43 return ss.str();
44}
45
46// Checks whether an SSRC is contained in the list of desired SSRCs.
47// Note that an empty SSRC list matches every SSRC.
48bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
49 if (desired_ssrc.size() == 0)
50 return true;
51 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
52 desired_ssrc.end();
53}
54
55double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
56 // The timestamp is a fixed point representation with 6 bits for seconds
57 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
58 // time in seconds and then multiply by 1000000 to convert to microseconds.
59 static constexpr double kTimestampToMicroSec =
tereliusccbbf8d2016-08-10 07:34:28 -070060 1000000.0 / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -070061 return abs_send_time * kTimestampToMicroSec;
62}
63
64// Computes the difference |later| - |earlier| where |later| and |earlier|
65// are counters that wrap at |modulus|. The difference is chosen to have the
66// least absolute value. For example if |modulus| is 8, then the difference will
67// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
68// be in [-4, 4].
69int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
70 RTC_DCHECK_LE(1, modulus);
71 RTC_DCHECK_LT(later, modulus);
72 RTC_DCHECK_LT(earlier, modulus);
73 int64_t difference =
74 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
75 int64_t max_difference = modulus / 2;
76 int64_t min_difference = max_difference - modulus + 1;
77 if (difference > max_difference) {
78 difference -= modulus;
79 }
80 if (difference < min_difference) {
81 difference += modulus;
82 }
terelius6addf492016-08-23 17:34:07 -070083 if (difference > max_difference / 2 || difference < min_difference / 2) {
84 LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
85 << " expected to be in the range (" << min_difference / 2
86 << "," << max_difference / 2 << ") but is " << difference
87 << ". Correct unwrapping is uncertain.";
88 }
terelius54ce6802016-07-13 06:44:41 -070089 return difference;
90}
91
stefan6a850c32016-07-29 10:28:08 -070092void RegisterHeaderExtensions(
93 const std::vector<webrtc::RtpExtension>& extensions,
94 webrtc::RtpHeaderExtensionMap* extension_map) {
95 extension_map->Erase();
96 for (const webrtc::RtpExtension& extension : extensions) {
97 extension_map->Register(webrtc::StringToRtpExtensionType(extension.uri),
98 extension.id);
99 }
100}
101
tereliusdc35dcd2016-08-01 12:03:27 -0700102constexpr float kLeftMargin = 0.01f;
103constexpr float kRightMargin = 0.02f;
104constexpr float kBottomMargin = 0.02f;
105constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700106
terelius6addf492016-08-23 17:34:07 -0700107class PacketSizeBytes {
108 public:
109 using DataType = LoggedRtpPacket;
110 using ResultType = size_t;
111 size_t operator()(const LoggedRtpPacket& packet) {
112 return packet.total_length;
113 }
114};
115
116class SequenceNumberDiff {
117 public:
118 using DataType = LoggedRtpPacket;
119 using ResultType = int64_t;
120 int64_t operator()(const LoggedRtpPacket& old_packet,
121 const LoggedRtpPacket& new_packet) {
122 return WrappingDifference(new_packet.header.sequenceNumber,
123 old_packet.header.sequenceNumber, 1ul << 16);
124 }
125};
126
tereliusccbbf8d2016-08-10 07:34:28 -0700127class NetworkDelayDiff {
128 public:
129 class AbsSendTime {
130 public:
131 using DataType = LoggedRtpPacket;
132 using ResultType = double;
133 double operator()(const LoggedRtpPacket& old_packet,
134 const LoggedRtpPacket& new_packet) {
135 if (old_packet.header.extension.hasAbsoluteSendTime &&
136 new_packet.header.extension.hasAbsoluteSendTime) {
137 int64_t send_time_diff = WrappingDifference(
138 new_packet.header.extension.absoluteSendTime,
139 old_packet.header.extension.absoluteSendTime, 1ul << 24);
140 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
141 return static_cast<double>(recv_time_diff -
142 AbsSendTimeToMicroseconds(send_time_diff)) /
143 1000;
144 } else {
145 return 0;
146 }
147 }
148 };
149
150 class CaptureTime {
151 public:
152 using DataType = LoggedRtpPacket;
153 using ResultType = double;
154 double operator()(const LoggedRtpPacket& old_packet,
155 const LoggedRtpPacket& new_packet) {
156 int64_t send_time_diff = WrappingDifference(
157 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
158 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
159
160 const double kVideoSampleRate = 90000;
161 // TODO(terelius): We treat all streams as video for now, even though
162 // audio might be sampled at e.g. 16kHz, because it is really difficult to
163 // figure out the true sampling rate of a stream. The effect is that the
164 // delay will be scaled incorrectly for non-video streams.
165
166 double delay_change =
167 static_cast<double>(recv_time_diff) / 1000 -
168 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
terelius6addf492016-08-23 17:34:07 -0700169 if (delay_change < -10000 || 10000 < delay_change) {
170 LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
171 LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
172 << ", received time " << old_packet.timestamp;
173 LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
174 << ", received time " << new_packet.timestamp;
175 LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
176 << static_cast<double>(recv_time_diff) / 1000000 << "s";
177 LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
178 << static_cast<double>(send_time_diff) /
179 kVideoSampleRate
180 << "s";
181 }
tereliusccbbf8d2016-08-10 07:34:28 -0700182 return delay_change;
183 }
184 };
185};
186
187template <typename Extractor>
188class Accumulated {
189 public:
190 using DataType = typename Extractor::DataType;
191 using ResultType = typename Extractor::ResultType;
192 ResultType operator()(const DataType& old_packet,
193 const DataType& new_packet) {
194 sum += extract(old_packet, new_packet);
195 return sum;
196 }
197
198 private:
199 Extractor extract;
200 ResultType sum = 0;
201};
202
terelius6addf492016-08-23 17:34:07 -0700203// For each element in data, use |Extractor| to extract a y-coordinate and
204// store the result in a TimeSeries.
205template <typename Extractor>
206void Pointwise(const std::vector<typename Extractor::DataType>& data,
207 uint64_t begin_time,
208 TimeSeries* result) {
209 Extractor extract;
210 for (size_t i = 0; i < data.size(); i++) {
211 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
212 float y = extract(data[i]);
213 result->points.emplace_back(x, y);
214 }
215}
216
217// For each pair of adjacent elements in |data|, use |Extractor| to extract a
218// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
219// will be the time of the second element in the pair.
tereliusccbbf8d2016-08-10 07:34:28 -0700220template <typename Extractor>
221void Pairwise(const std::vector<typename Extractor::DataType>& data,
222 uint64_t begin_time,
223 TimeSeries* result) {
224 Extractor extract;
225 for (size_t i = 1; i < data.size(); i++) {
226 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
227 float y = extract(data[i - 1], data[i]);
228 result->points.emplace_back(x, y);
229 }
230}
231
terelius6addf492016-08-23 17:34:07 -0700232// Calculates a moving average of |data| and stores the result in a TimeSeries.
233// A data point is generated every |step| microseconds from |begin_time|
234// to |end_time|. The value of each data point is the average of the data
235// during the preceeding |window_duration_us| microseconds.
236template <typename Extractor>
237void MovingAverage(const std::vector<typename Extractor::DataType>& data,
238 uint64_t begin_time,
239 uint64_t end_time,
240 uint64_t window_duration_us,
241 uint64_t step,
242 float y_scaling,
243 webrtc::plotting::TimeSeries* result) {
244 size_t window_index_begin = 0;
245 size_t window_index_end = 0;
246 typename Extractor::ResultType sum_in_window = 0;
247 Extractor extract;
248
249 for (uint64_t t = begin_time; t < end_time + step; t += step) {
250 while (window_index_end < data.size() &&
251 data[window_index_end].timestamp < t) {
252 sum_in_window += extract(data[window_index_end]);
253 ++window_index_end;
254 }
255 while (window_index_begin < data.size() &&
256 data[window_index_begin].timestamp < t - window_duration_us) {
257 sum_in_window -= extract(data[window_index_begin]);
258 ++window_index_begin;
259 }
260 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
261 float x = static_cast<float>(t - begin_time) / 1000000;
262 float y = sum_in_window / window_duration_s * y_scaling;
263 result->points.emplace_back(x, y);
264 }
265}
266
terelius54ce6802016-07-13 06:44:41 -0700267} // namespace
268
terelius54ce6802016-07-13 06:44:41 -0700269EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
270 : parsed_log_(log), window_duration_(250000), step_(10000) {
271 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
272 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700273
Stefan Holmer13181032016-07-29 14:48:54 +0200274 // Maps a stream identifier consisting of ssrc and direction
terelius88e64e52016-07-19 01:51:06 -0700275 // to the header extensions used by that stream,
276 std::map<StreamId, RtpHeaderExtensionMap> extension_maps;
277
278 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700279 uint8_t header[IP_PACKET_SIZE];
280 size_t header_length;
281 size_t total_length;
282
terelius54ce6802016-07-13 06:44:41 -0700283 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
284 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700285 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
286 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
287 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700288 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
289 event_type != ParsedRtcEventLog::LOG_START &&
290 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700291 uint64_t timestamp = parsed_log_.GetTimestamp(i);
292 first_timestamp = std::min(first_timestamp, timestamp);
293 last_timestamp = std::max(last_timestamp, timestamp);
294 }
295
296 switch (parsed_log_.GetEventType(i)) {
297 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
298 VideoReceiveStream::Config config(nullptr);
299 parsed_log_.GetVideoReceiveConfig(i, &config);
Stefan Holmer13181032016-07-29 14:48:54 +0200300 StreamId stream(config.rtp.remote_ssrc, kIncomingPacket);
stefan6a850c32016-07-29 10:28:08 -0700301 RegisterHeaderExtensions(config.rtp.extensions,
302 &extension_maps[stream]);
terelius0740a202016-08-08 10:21:04 -0700303 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700304 for (auto kv : config.rtp.rtx) {
305 StreamId rtx_stream(kv.second.ssrc, kIncomingPacket);
306 RegisterHeaderExtensions(config.rtp.extensions,
307 &extension_maps[rtx_stream]);
terelius0740a202016-08-08 10:21:04 -0700308 video_ssrcs_.insert(rtx_stream);
309 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700310 }
311 break;
312 }
313 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
314 VideoSendStream::Config config(nullptr);
315 parsed_log_.GetVideoSendConfig(i, &config);
316 for (auto ssrc : config.rtp.ssrcs) {
Stefan Holmer13181032016-07-29 14:48:54 +0200317 StreamId stream(ssrc, kOutgoingPacket);
stefan6a850c32016-07-29 10:28:08 -0700318 RegisterHeaderExtensions(config.rtp.extensions,
319 &extension_maps[stream]);
terelius0740a202016-08-08 10:21:04 -0700320 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700321 }
322 for (auto ssrc : config.rtp.rtx.ssrcs) {
terelius0740a202016-08-08 10:21:04 -0700323 StreamId rtx_stream(ssrc, kOutgoingPacket);
stefan6a850c32016-07-29 10:28:08 -0700324 RegisterHeaderExtensions(config.rtp.extensions,
terelius0740a202016-08-08 10:21:04 -0700325 &extension_maps[rtx_stream]);
326 video_ssrcs_.insert(rtx_stream);
327 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700328 }
329 break;
330 }
331 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
332 AudioReceiveStream::Config config;
333 // TODO(terelius): Parse the audio configs once we have them.
334 break;
335 }
336 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
337 AudioSendStream::Config config(nullptr);
338 // TODO(terelius): Parse the audio configs once we have them.
339 break;
340 }
341 case ParsedRtcEventLog::RTP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200342 MediaType media_type;
terelius88e64e52016-07-19 01:51:06 -0700343 parsed_log_.GetRtpHeader(i, &direction, &media_type, header,
344 &header_length, &total_length);
345 // Parse header to get SSRC.
346 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
347 RTPHeader parsed_header;
348 rtp_parser.Parse(&parsed_header);
Stefan Holmer13181032016-07-29 14:48:54 +0200349 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700350 // Look up the extension_map and parse it again to get the extensions.
351 if (extension_maps.count(stream) == 1) {
352 RtpHeaderExtensionMap* extension_map = &extension_maps[stream];
353 rtp_parser.Parse(&parsed_header, extension_map);
354 }
355 uint64_t timestamp = parsed_log_.GetTimestamp(i);
356 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200357 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700358 break;
359 }
360 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200361 uint8_t packet[IP_PACKET_SIZE];
362 MediaType media_type;
363 parsed_log_.GetRtcpPacket(i, &direction, &media_type, packet,
364 &total_length);
365
366 RtpUtility::RtpHeaderParser rtp_parser(packet, total_length);
367 RTPHeader parsed_header;
368 RTC_CHECK(rtp_parser.ParseRtcp(&parsed_header));
369 uint32_t ssrc = parsed_header.ssrc;
370
371 RTCPUtility::RTCPParserV2 rtcp_parser(packet, total_length, true);
372 RTC_CHECK(rtcp_parser.IsValid());
373
374 RTCPUtility::RTCPPacketTypes packet_type = rtcp_parser.Begin();
375 while (packet_type != RTCPUtility::RTCPPacketTypes::kInvalid) {
376 switch (packet_type) {
377 case RTCPUtility::RTCPPacketTypes::kTransportFeedback: {
378 // Currently feedback is logged twice, both for audio and video.
379 // Only act on one of them.
380 if (media_type == MediaType::VIDEO) {
381 std::unique_ptr<rtcp::RtcpPacket> rtcp_packet(
382 rtcp_parser.ReleaseRtcpPacket());
383 StreamId stream(ssrc, direction);
384 uint64_t timestamp = parsed_log_.GetTimestamp(i);
385 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
386 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
387 }
388 break;
389 }
390 default:
391 break;
392 }
393 rtcp_parser.Iterate();
394 packet_type = rtcp_parser.PacketType();
395 }
terelius88e64e52016-07-19 01:51:06 -0700396 break;
397 }
398 case ParsedRtcEventLog::LOG_START: {
399 break;
400 }
401 case ParsedRtcEventLog::LOG_END: {
402 break;
403 }
404 case ParsedRtcEventLog::BWE_PACKET_LOSS_EVENT: {
terelius8058e582016-07-25 01:32:41 -0700405 BwePacketLossEvent bwe_update;
406 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
407 parsed_log_.GetBwePacketLossEvent(i, &bwe_update.new_bitrate,
408 &bwe_update.fraction_loss,
409 &bwe_update.expected_packets);
410 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700411 break;
412 }
413 case ParsedRtcEventLog::BWE_PACKET_DELAY_EVENT: {
414 break;
415 }
416 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
417 break;
418 }
419 case ParsedRtcEventLog::UNKNOWN_EVENT: {
420 break;
421 }
422 }
terelius54ce6802016-07-13 06:44:41 -0700423 }
terelius88e64e52016-07-19 01:51:06 -0700424
terelius54ce6802016-07-13 06:44:41 -0700425 if (last_timestamp < first_timestamp) {
426 // No useful events in the log.
427 first_timestamp = last_timestamp = 0;
428 }
429 begin_time_ = first_timestamp;
430 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700431 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
terelius54ce6802016-07-13 06:44:41 -0700432}
433
Stefan Holmer13181032016-07-29 14:48:54 +0200434class BitrateObserver : public CongestionController::Observer,
435 public RemoteBitrateObserver {
436 public:
437 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
438
439 void OnNetworkChanged(uint32_t bitrate_bps,
440 uint8_t fraction_loss,
441 int64_t rtt_ms) override {
442 last_bitrate_bps_ = bitrate_bps;
443 bitrate_updated_ = true;
444 }
445
446 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
447 uint32_t bitrate) override {}
448
449 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
450 bool GetAndResetBitrateUpdated() {
451 bool bitrate_updated = bitrate_updated_;
452 bitrate_updated_ = false;
453 return bitrate_updated;
454 }
455
456 private:
457 uint32_t last_bitrate_bps_;
458 bool bitrate_updated_;
459};
460
terelius0740a202016-08-08 10:21:04 -0700461bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) {
462 return rtx_ssrcs_.count(stream_id) == 1;
463}
464
465bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) {
466 return video_ssrcs_.count(stream_id) == 1;
467}
468
469bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) {
470 return audio_ssrcs_.count(stream_id) == 1;
471}
472
terelius54ce6802016-07-13 06:44:41 -0700473void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
474 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700475 for (auto& kv : rtp_packets_) {
476 StreamId stream_id = kv.first;
477 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
478 // Filter on direction and SSRC.
479 if (stream_id.GetDirection() != desired_direction ||
480 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
481 continue;
terelius54ce6802016-07-13 06:44:41 -0700482 }
terelius54ce6802016-07-13 06:44:41 -0700483
terelius6addf492016-08-23 17:34:07 -0700484 TimeSeries time_series;
485 time_series.label = SsrcToString(stream_id.GetSsrc());
486 time_series.style = BAR_GRAPH;
487 Pointwise<PacketSizeBytes>(packet_stream, begin_time_, &time_series);
488 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700489 }
490
tereliusdc35dcd2016-08-01 12:03:27 -0700491 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
492 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
493 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700494 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700495 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700496 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700497 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700498 }
499}
500
501// For each SSRC, plot the time between the consecutive playouts.
502void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
503 std::map<uint32_t, TimeSeries> time_series;
504 std::map<uint32_t, uint64_t> last_playout;
505
506 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700507
508 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
509 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
510 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
511 parsed_log_.GetAudioPlayout(i, &ssrc);
512 uint64_t timestamp = parsed_log_.GetTimestamp(i);
513 if (MatchingSsrc(ssrc, desired_ssrc_)) {
514 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
515 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
516 if (time_series[ssrc].points.size() == 0) {
517 // There were no previusly logged playout for this SSRC.
518 // Generate a point, but place it on the x-axis.
519 y = 0;
520 }
terelius54ce6802016-07-13 06:44:41 -0700521 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
522 last_playout[ssrc] = timestamp;
523 }
524 }
525 }
526
527 // Set labels and put in graph.
528 for (auto& kv : time_series) {
529 kv.second.label = SsrcToString(kv.first);
530 kv.second.style = BAR_GRAPH;
tereliusdc35dcd2016-08-01 12:03:27 -0700531 plot->series_list_.push_back(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700532 }
533
tereliusdc35dcd2016-08-01 12:03:27 -0700534 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
535 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
536 kTopMargin);
537 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700538}
539
540// For each SSRC, plot the time between the consecutive playouts.
541void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700542 for (auto& kv : rtp_packets_) {
543 StreamId stream_id = kv.first;
544 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
545 // Filter on direction and SSRC.
546 if (stream_id.GetDirection() != kIncomingPacket ||
547 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
548 continue;
terelius54ce6802016-07-13 06:44:41 -0700549 }
terelius54ce6802016-07-13 06:44:41 -0700550
terelius6addf492016-08-23 17:34:07 -0700551 TimeSeries time_series;
552 time_series.label = SsrcToString(stream_id.GetSsrc());
553 time_series.style = BAR_GRAPH;
554 Pairwise<SequenceNumberDiff>(packet_stream, begin_time_, &time_series);
555 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700556 }
557
tereliusdc35dcd2016-08-01 12:03:27 -0700558 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
559 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
560 kTopMargin);
561 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700562}
563
564void EventLogAnalyzer::CreateDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700565 for (auto& kv : rtp_packets_) {
566 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700567 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
568 uint32_t ssrc = stream_id.GetSsrc();
terelius88e64e52016-07-19 01:51:06 -0700569 // Filter on direction and SSRC.
570 if (stream_id.GetDirection() != kIncomingPacket ||
tereliusccbbf8d2016-08-10 07:34:28 -0700571 !MatchingSsrc(ssrc, desired_ssrc_) || IsAudioSsrc(stream_id) ||
572 !IsVideoSsrc(stream_id) || IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700573 continue;
574 }
terelius54ce6802016-07-13 06:44:41 -0700575
tereliusccbbf8d2016-08-10 07:34:28 -0700576 TimeSeries capture_time_data;
577 capture_time_data.label = SsrcToString(ssrc) + " capture-time";
578 capture_time_data.style = BAR_GRAPH;
579 Pairwise<NetworkDelayDiff::CaptureTime>(packet_stream, begin_time_,
580 &capture_time_data);
581 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700582
tereliusccbbf8d2016-08-10 07:34:28 -0700583 TimeSeries send_time_data;
584 send_time_data.label = SsrcToString(ssrc) + " abs-send-time";
585 send_time_data.style = BAR_GRAPH;
586 Pairwise<NetworkDelayDiff::AbsSendTime>(packet_stream, begin_time_,
587 &send_time_data);
588 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700589 }
590
tereliusdc35dcd2016-08-01 12:03:27 -0700591 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
592 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
593 kTopMargin);
594 plot->SetTitle("Network latency change between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700595}
596
597void EventLogAnalyzer::CreateAccumulatedDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700598 for (auto& kv : rtp_packets_) {
599 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700600 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
601 uint32_t ssrc = stream_id.GetSsrc();
terelius88e64e52016-07-19 01:51:06 -0700602 // Filter on direction and SSRC.
603 if (stream_id.GetDirection() != kIncomingPacket ||
tereliusccbbf8d2016-08-10 07:34:28 -0700604 !MatchingSsrc(ssrc, desired_ssrc_) || IsAudioSsrc(stream_id) ||
605 !IsVideoSsrc(stream_id) || IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700606 continue;
607 }
terelius54ce6802016-07-13 06:44:41 -0700608
tereliusccbbf8d2016-08-10 07:34:28 -0700609 TimeSeries capture_time_data;
610 capture_time_data.label = SsrcToString(ssrc) + " capture-time";
611 capture_time_data.style = LINE_GRAPH;
612 Pairwise<Accumulated<NetworkDelayDiff::CaptureTime>>(
613 packet_stream, begin_time_, &capture_time_data);
614 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700615
tereliusccbbf8d2016-08-10 07:34:28 -0700616 TimeSeries send_time_data;
617 send_time_data.label = SsrcToString(ssrc) + " abs-send-time";
618 send_time_data.style = LINE_GRAPH;
619 Pairwise<Accumulated<NetworkDelayDiff::AbsSendTime>>(
620 packet_stream, begin_time_, &send_time_data);
621 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700622 }
623
tereliusdc35dcd2016-08-01 12:03:27 -0700624 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
625 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
626 kTopMargin);
627 plot->SetTitle("Accumulated network latency change");
terelius54ce6802016-07-13 06:44:41 -0700628}
629
tereliusf736d232016-08-04 10:00:11 -0700630// Plot the fraction of packets lost (as perceived by the loss-based BWE).
631void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
632 plot->series_list_.push_back(TimeSeries());
633 for (auto& bwe_update : bwe_loss_updates_) {
634 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
635 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
636 plot->series_list_.back().points.emplace_back(x, y);
637 }
638 plot->series_list_.back().label = "Fraction lost";
639 plot->series_list_.back().style = LINE_DOT_GRAPH;
640
641 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
642 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
643 kTopMargin);
644 plot->SetTitle("Reported packet loss");
645}
646
terelius54ce6802016-07-13 06:44:41 -0700647// Plot the total bandwidth used by all RTP streams.
648void EventLogAnalyzer::CreateTotalBitrateGraph(
649 PacketDirection desired_direction,
650 Plot* plot) {
651 struct TimestampSize {
652 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
653 uint64_t timestamp;
654 size_t size;
655 };
656 std::vector<TimestampSize> packets;
657
658 PacketDirection direction;
659 size_t total_length;
660
661 // Extract timestamps and sizes for the relevant packets.
662 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
663 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
664 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
665 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, nullptr,
666 &total_length);
667 if (direction == desired_direction) {
668 uint64_t timestamp = parsed_log_.GetTimestamp(i);
669 packets.push_back(TimestampSize(timestamp, total_length));
670 }
671 }
672 }
673
674 size_t window_index_begin = 0;
675 size_t window_index_end = 0;
676 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700677
678 // Calculate a moving average of the bitrate and store in a TimeSeries.
tereliusdc35dcd2016-08-01 12:03:27 -0700679 plot->series_list_.push_back(TimeSeries());
terelius54ce6802016-07-13 06:44:41 -0700680 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
681 while (window_index_end < packets.size() &&
682 packets[window_index_end].timestamp < time) {
683 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700684 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700685 }
686 while (window_index_begin < packets.size() &&
687 packets[window_index_begin].timestamp < time - window_duration_) {
688 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
689 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700690 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700691 }
692 float window_duration_in_seconds =
693 static_cast<float>(window_duration_) / 1000000;
694 float x = static_cast<float>(time - begin_time_) / 1000000;
695 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700696 plot->series_list_.back().points.push_back(TimeSeriesPoint(x, y));
terelius54ce6802016-07-13 06:44:41 -0700697 }
698
699 // Set labels.
700 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700701 plot->series_list_.back().label = "Incoming bitrate";
terelius54ce6802016-07-13 06:44:41 -0700702 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700703 plot->series_list_.back().label = "Outgoing bitrate";
terelius54ce6802016-07-13 06:44:41 -0700704 }
tereliusdc35dcd2016-08-01 12:03:27 -0700705 plot->series_list_.back().style = LINE_GRAPH;
terelius54ce6802016-07-13 06:44:41 -0700706
terelius8058e582016-07-25 01:32:41 -0700707 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
708 if (desired_direction == kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700709 plot->series_list_.push_back(TimeSeries());
terelius8058e582016-07-25 01:32:41 -0700710 for (auto& bwe_update : bwe_loss_updates_) {
711 float x =
712 static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
713 float y = static_cast<float>(bwe_update.new_bitrate) / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700714 plot->series_list_.back().points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -0700715 }
tereliusdc35dcd2016-08-01 12:03:27 -0700716 plot->series_list_.back().label = "Loss-based estimate";
717 plot->series_list_.back().style = LINE_GRAPH;
terelius8058e582016-07-25 01:32:41 -0700718 }
tereliusdc35dcd2016-08-01 12:03:27 -0700719 plot->series_list_.back().style = LINE_GRAPH;
720 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
721 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700722 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700723 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700724 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700725 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700726 }
727}
728
729// For each SSRC, plot the bandwidth used by that stream.
730void EventLogAnalyzer::CreateStreamBitrateGraph(
731 PacketDirection desired_direction,
732 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700733 for (auto& kv : rtp_packets_) {
734 StreamId stream_id = kv.first;
735 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
736 // Filter on direction and SSRC.
737 if (stream_id.GetDirection() != desired_direction ||
738 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
739 continue;
terelius54ce6802016-07-13 06:44:41 -0700740 }
741
terelius6addf492016-08-23 17:34:07 -0700742 TimeSeries time_series;
743 time_series.label = SsrcToString(stream_id.GetSsrc());
744 time_series.style = LINE_GRAPH;
745 double bytes_to_kilobits = 8.0 / 1000;
746 MovingAverage<PacketSizeBytes>(packet_stream, begin_time_, end_time_,
747 window_duration_, step_, bytes_to_kilobits,
748 &time_series);
749 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700750 }
751
tereliusdc35dcd2016-08-01 12:03:27 -0700752 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
753 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700754 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700755 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700756 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700757 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700758 }
759}
760
tereliuse34c19c2016-08-15 08:47:14 -0700761void EventLogAnalyzer::CreateBweSimulationGraph(Plot* plot) {
Stefan Holmer13181032016-07-29 14:48:54 +0200762 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
763 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
764
765 for (const auto& kv : rtp_packets_) {
766 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
767 for (const LoggedRtpPacket& rtp_packet : kv.second)
768 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
769 }
770 }
771
772 for (const auto& kv : rtcp_packets_) {
773 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
774 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
775 incoming_rtcp.insert(
776 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
777 }
778 }
779
780 SimulatedClock clock(0);
781 BitrateObserver observer;
782 RtcEventLogNullImpl null_event_log;
783 CongestionController cc(&clock, &observer, &observer, &null_event_log);
784 // TODO(holmer): Log the call config and use that here instead.
785 static const uint32_t kDefaultStartBitrateBps = 300000;
786 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
787
788 TimeSeries time_series;
tereliuse34c19c2016-08-15 08:47:14 -0700789 time_series.label = "Delay-based estimate";
Stefan Holmer13181032016-07-29 14:48:54 +0200790 time_series.style = LINE_DOT_GRAPH;
Stefan Holmer13181032016-07-29 14:48:54 +0200791
792 auto rtp_iterator = outgoing_rtp.begin();
793 auto rtcp_iterator = incoming_rtcp.begin();
794
795 auto NextRtpTime = [&]() {
796 if (rtp_iterator != outgoing_rtp.end())
797 return static_cast<int64_t>(rtp_iterator->first);
798 return std::numeric_limits<int64_t>::max();
799 };
800
801 auto NextRtcpTime = [&]() {
802 if (rtcp_iterator != incoming_rtcp.end())
803 return static_cast<int64_t>(rtcp_iterator->first);
804 return std::numeric_limits<int64_t>::max();
805 };
806
807 auto NextProcessTime = [&]() {
808 if (rtcp_iterator != incoming_rtcp.end() ||
809 rtp_iterator != outgoing_rtp.end()) {
810 return clock.TimeInMicroseconds() +
811 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
812 }
813 return std::numeric_limits<int64_t>::max();
814 };
815
816 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
817 while (time_us != std::numeric_limits<int64_t>::max()) {
818 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
819 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -0700820 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200821 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
822 if (rtcp.type == kRtcpTransportFeedback) {
823 cc.GetTransportFeedbackObserver()->OnTransportFeedback(
824 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
825 }
826 ++rtcp_iterator;
827 }
828 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -0700829 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200830 const LoggedRtpPacket& rtp = *rtp_iterator->second;
831 if (rtp.header.extension.hasTransportSequenceNumber) {
832 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
833 cc.GetTransportFeedbackObserver()->AddPacket(
stefana93d5ac2016-08-17 02:14:32 -0700834 rtp.header.extension.transportSequenceNumber, rtp.total_length,
835 PacketInfo::kNotAProbe);
Stefan Holmer13181032016-07-29 14:48:54 +0200836 rtc::SentPacket sent_packet(
837 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
838 cc.OnSentPacket(sent_packet);
839 }
840 ++rtp_iterator;
841 }
stefanc3de0332016-08-02 07:22:17 -0700842 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
843 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200844 cc.Process();
stefanc3de0332016-08-02 07:22:17 -0700845 }
Stefan Holmer13181032016-07-29 14:48:54 +0200846 if (observer.GetAndResetBitrateUpdated()) {
847 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +0200848 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
849 1000000;
850 time_series.points.emplace_back(x, y);
851 }
852 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
853 }
854 // Add the data set to the plot.
tereliusdc35dcd2016-08-01 12:03:27 -0700855 plot->series_list_.push_back(std::move(time_series));
Stefan Holmer13181032016-07-29 14:48:54 +0200856
tereliusdc35dcd2016-08-01 12:03:27 -0700857 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
858 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
859 plot->SetTitle("Simulated BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +0200860}
861
tereliuse34c19c2016-08-15 08:47:14 -0700862void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanc3de0332016-08-02 07:22:17 -0700863 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
864 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
865
866 for (const auto& kv : rtp_packets_) {
867 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
868 for (const LoggedRtpPacket& rtp_packet : kv.second)
869 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
870 }
871 }
872
873 for (const auto& kv : rtcp_packets_) {
874 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
875 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
876 incoming_rtcp.insert(
877 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
878 }
879 }
880
881 SimulatedClock clock(0);
882 TransportFeedbackAdapter feedback_adapter(nullptr, &clock);
883
884 TimeSeries time_series;
885 time_series.label = "Network Delay Change";
886 time_series.style = LINE_DOT_GRAPH;
887 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
888
889 auto rtp_iterator = outgoing_rtp.begin();
890 auto rtcp_iterator = incoming_rtcp.begin();
891
892 auto NextRtpTime = [&]() {
893 if (rtp_iterator != outgoing_rtp.end())
894 return static_cast<int64_t>(rtp_iterator->first);
895 return std::numeric_limits<int64_t>::max();
896 };
897
898 auto NextRtcpTime = [&]() {
899 if (rtcp_iterator != incoming_rtcp.end())
900 return static_cast<int64_t>(rtcp_iterator->first);
901 return std::numeric_limits<int64_t>::max();
902 };
903
904 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
905 while (time_us != std::numeric_limits<int64_t>::max()) {
906 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
907 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
908 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
909 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
910 if (rtcp.type == kRtcpTransportFeedback) {
911 std::vector<PacketInfo> feedback =
912 feedback_adapter.GetPacketFeedbackVector(
913 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
914 for (const PacketInfo& packet : feedback) {
915 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
916 float x =
917 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
918 1000000;
919 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
920 time_series.points.emplace_back(x, y);
921 }
922 }
923 ++rtcp_iterator;
924 }
925 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
926 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
927 const LoggedRtpPacket& rtp = *rtp_iterator->second;
928 if (rtp.header.extension.hasTransportSequenceNumber) {
929 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
930 feedback_adapter.AddPacket(rtp.header.extension.transportSequenceNumber,
931 rtp.total_length, 0);
932 feedback_adapter.OnSentPacket(
933 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
934 }
935 ++rtp_iterator;
936 }
937 time_us = std::min(NextRtpTime(), NextRtcpTime());
938 }
939 // We assume that the base network delay (w/o queues) is the min delay
940 // observed during the call.
941 for (TimeSeriesPoint& point : time_series.points)
942 point.y -= estimated_base_delay_ms;
943 // Add the data set to the plot.
944 plot->series_list_.push_back(std::move(time_series));
945
946 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
947 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
948 plot->SetTitle("Network Delay Change.");
949}
terelius54ce6802016-07-13 06:44:41 -0700950} // namespace plotting
951} // namespace webrtc