blob: 7db82ae47806a4c262a9b7353d3869f160b30d2f [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"
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
philipelccd74892016-09-05 02:46:25 -0700501template <typename T>
502void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
503 PacketDirection desired_direction,
504 Plot* plot,
505 const std::map<StreamId, std::vector<T>>& packets,
506 const std::string& label_prefix) {
507 for (auto& kv : packets) {
508 StreamId stream_id = kv.first;
509 const std::vector<T>& packet_stream = kv.second;
510 // Filter on direction and SSRC.
511 if (stream_id.GetDirection() != desired_direction ||
512 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
513 continue;
514 }
515
516 TimeSeries time_series;
517 time_series.label = label_prefix + " " + SsrcToString(stream_id.GetSsrc());
518 time_series.style = LINE_GRAPH;
519
520 for (size_t i = 0; i < packet_stream.size(); i++) {
521 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
522 1000000;
523 time_series.points.emplace_back(x, i);
524 time_series.points.emplace_back(x, i + 1);
525 }
526
527 plot->series_list_.push_back(std::move(time_series));
528 }
529}
530
531void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
532 PacketDirection desired_direction,
533 Plot* plot) {
534 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
535 "RTP");
536 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
537 "RTCP");
538
539 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
540 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
541 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
542 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
543 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
544 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
545 }
546}
547
terelius54ce6802016-07-13 06:44:41 -0700548// For each SSRC, plot the time between the consecutive playouts.
549void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
550 std::map<uint32_t, TimeSeries> time_series;
551 std::map<uint32_t, uint64_t> last_playout;
552
553 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700554
555 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
556 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
557 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
558 parsed_log_.GetAudioPlayout(i, &ssrc);
559 uint64_t timestamp = parsed_log_.GetTimestamp(i);
560 if (MatchingSsrc(ssrc, desired_ssrc_)) {
561 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
562 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
563 if (time_series[ssrc].points.size() == 0) {
564 // There were no previusly logged playout for this SSRC.
565 // Generate a point, but place it on the x-axis.
566 y = 0;
567 }
terelius54ce6802016-07-13 06:44:41 -0700568 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
569 last_playout[ssrc] = timestamp;
570 }
571 }
572 }
573
574 // Set labels and put in graph.
575 for (auto& kv : time_series) {
576 kv.second.label = SsrcToString(kv.first);
577 kv.second.style = BAR_GRAPH;
tereliusdc35dcd2016-08-01 12:03:27 -0700578 plot->series_list_.push_back(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700579 }
580
tereliusdc35dcd2016-08-01 12:03:27 -0700581 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
582 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
583 kTopMargin);
584 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700585}
586
587// For each SSRC, plot the time between the consecutive playouts.
588void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700589 for (auto& kv : rtp_packets_) {
590 StreamId stream_id = kv.first;
591 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
592 // Filter on direction and SSRC.
593 if (stream_id.GetDirection() != kIncomingPacket ||
594 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
595 continue;
terelius54ce6802016-07-13 06:44:41 -0700596 }
terelius54ce6802016-07-13 06:44:41 -0700597
terelius6addf492016-08-23 17:34:07 -0700598 TimeSeries time_series;
599 time_series.label = SsrcToString(stream_id.GetSsrc());
600 time_series.style = BAR_GRAPH;
601 Pairwise<SequenceNumberDiff>(packet_stream, begin_time_, &time_series);
602 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700603 }
604
tereliusdc35dcd2016-08-01 12:03:27 -0700605 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
606 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
607 kTopMargin);
608 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700609}
610
611void EventLogAnalyzer::CreateDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700612 for (auto& kv : rtp_packets_) {
613 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700614 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
615 uint32_t ssrc = stream_id.GetSsrc();
terelius88e64e52016-07-19 01:51:06 -0700616 // Filter on direction and SSRC.
617 if (stream_id.GetDirection() != kIncomingPacket ||
tereliusccbbf8d2016-08-10 07:34:28 -0700618 !MatchingSsrc(ssrc, desired_ssrc_) || IsAudioSsrc(stream_id) ||
619 !IsVideoSsrc(stream_id) || IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700620 continue;
621 }
terelius54ce6802016-07-13 06:44:41 -0700622
tereliusccbbf8d2016-08-10 07:34:28 -0700623 TimeSeries capture_time_data;
624 capture_time_data.label = SsrcToString(ssrc) + " capture-time";
625 capture_time_data.style = BAR_GRAPH;
626 Pairwise<NetworkDelayDiff::CaptureTime>(packet_stream, begin_time_,
627 &capture_time_data);
628 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700629
tereliusccbbf8d2016-08-10 07:34:28 -0700630 TimeSeries send_time_data;
631 send_time_data.label = SsrcToString(ssrc) + " abs-send-time";
632 send_time_data.style = BAR_GRAPH;
633 Pairwise<NetworkDelayDiff::AbsSendTime>(packet_stream, begin_time_,
634 &send_time_data);
635 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700636 }
637
tereliusdc35dcd2016-08-01 12:03:27 -0700638 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
639 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
640 kTopMargin);
641 plot->SetTitle("Network latency change between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700642}
643
644void EventLogAnalyzer::CreateAccumulatedDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700645 for (auto& kv : rtp_packets_) {
646 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700647 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
648 uint32_t ssrc = stream_id.GetSsrc();
terelius88e64e52016-07-19 01:51:06 -0700649 // Filter on direction and SSRC.
650 if (stream_id.GetDirection() != kIncomingPacket ||
tereliusccbbf8d2016-08-10 07:34:28 -0700651 !MatchingSsrc(ssrc, desired_ssrc_) || IsAudioSsrc(stream_id) ||
652 !IsVideoSsrc(stream_id) || IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700653 continue;
654 }
terelius54ce6802016-07-13 06:44:41 -0700655
tereliusccbbf8d2016-08-10 07:34:28 -0700656 TimeSeries capture_time_data;
657 capture_time_data.label = SsrcToString(ssrc) + " capture-time";
658 capture_time_data.style = LINE_GRAPH;
659 Pairwise<Accumulated<NetworkDelayDiff::CaptureTime>>(
660 packet_stream, begin_time_, &capture_time_data);
661 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700662
tereliusccbbf8d2016-08-10 07:34:28 -0700663 TimeSeries send_time_data;
664 send_time_data.label = SsrcToString(ssrc) + " abs-send-time";
665 send_time_data.style = LINE_GRAPH;
666 Pairwise<Accumulated<NetworkDelayDiff::AbsSendTime>>(
667 packet_stream, begin_time_, &send_time_data);
668 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700669 }
670
tereliusdc35dcd2016-08-01 12:03:27 -0700671 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
672 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
673 kTopMargin);
674 plot->SetTitle("Accumulated network latency change");
terelius54ce6802016-07-13 06:44:41 -0700675}
676
tereliusf736d232016-08-04 10:00:11 -0700677// Plot the fraction of packets lost (as perceived by the loss-based BWE).
678void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
679 plot->series_list_.push_back(TimeSeries());
680 for (auto& bwe_update : bwe_loss_updates_) {
681 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
682 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
683 plot->series_list_.back().points.emplace_back(x, y);
684 }
685 plot->series_list_.back().label = "Fraction lost";
686 plot->series_list_.back().style = LINE_DOT_GRAPH;
687
688 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
689 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
690 kTopMargin);
691 plot->SetTitle("Reported packet loss");
692}
693
terelius54ce6802016-07-13 06:44:41 -0700694// Plot the total bandwidth used by all RTP streams.
695void EventLogAnalyzer::CreateTotalBitrateGraph(
696 PacketDirection desired_direction,
697 Plot* plot) {
698 struct TimestampSize {
699 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
700 uint64_t timestamp;
701 size_t size;
702 };
703 std::vector<TimestampSize> packets;
704
705 PacketDirection direction;
706 size_t total_length;
707
708 // Extract timestamps and sizes for the relevant packets.
709 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
710 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
711 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
712 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, nullptr,
713 &total_length);
714 if (direction == desired_direction) {
715 uint64_t timestamp = parsed_log_.GetTimestamp(i);
716 packets.push_back(TimestampSize(timestamp, total_length));
717 }
718 }
719 }
720
721 size_t window_index_begin = 0;
722 size_t window_index_end = 0;
723 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700724
725 // Calculate a moving average of the bitrate and store in a TimeSeries.
tereliusdc35dcd2016-08-01 12:03:27 -0700726 plot->series_list_.push_back(TimeSeries());
terelius54ce6802016-07-13 06:44:41 -0700727 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
728 while (window_index_end < packets.size() &&
729 packets[window_index_end].timestamp < time) {
730 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700731 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700732 }
733 while (window_index_begin < packets.size() &&
734 packets[window_index_begin].timestamp < time - window_duration_) {
735 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
736 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700737 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700738 }
739 float window_duration_in_seconds =
740 static_cast<float>(window_duration_) / 1000000;
741 float x = static_cast<float>(time - begin_time_) / 1000000;
742 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700743 plot->series_list_.back().points.push_back(TimeSeriesPoint(x, y));
terelius54ce6802016-07-13 06:44:41 -0700744 }
745
746 // Set labels.
747 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700748 plot->series_list_.back().label = "Incoming bitrate";
terelius54ce6802016-07-13 06:44:41 -0700749 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700750 plot->series_list_.back().label = "Outgoing bitrate";
terelius54ce6802016-07-13 06:44:41 -0700751 }
tereliusdc35dcd2016-08-01 12:03:27 -0700752 plot->series_list_.back().style = LINE_GRAPH;
terelius54ce6802016-07-13 06:44:41 -0700753
terelius8058e582016-07-25 01:32:41 -0700754 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
755 if (desired_direction == kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700756 plot->series_list_.push_back(TimeSeries());
terelius8058e582016-07-25 01:32:41 -0700757 for (auto& bwe_update : bwe_loss_updates_) {
758 float x =
759 static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
760 float y = static_cast<float>(bwe_update.new_bitrate) / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700761 plot->series_list_.back().points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -0700762 }
tereliusdc35dcd2016-08-01 12:03:27 -0700763 plot->series_list_.back().label = "Loss-based estimate";
764 plot->series_list_.back().style = LINE_GRAPH;
terelius8058e582016-07-25 01:32:41 -0700765 }
tereliusdc35dcd2016-08-01 12:03:27 -0700766 plot->series_list_.back().style = LINE_GRAPH;
767 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
768 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700769 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700770 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700771 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700772 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700773 }
774}
775
776// For each SSRC, plot the bandwidth used by that stream.
777void EventLogAnalyzer::CreateStreamBitrateGraph(
778 PacketDirection desired_direction,
779 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700780 for (auto& kv : rtp_packets_) {
781 StreamId stream_id = kv.first;
782 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
783 // Filter on direction and SSRC.
784 if (stream_id.GetDirection() != desired_direction ||
785 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
786 continue;
terelius54ce6802016-07-13 06:44:41 -0700787 }
788
terelius6addf492016-08-23 17:34:07 -0700789 TimeSeries time_series;
790 time_series.label = SsrcToString(stream_id.GetSsrc());
791 time_series.style = LINE_GRAPH;
792 double bytes_to_kilobits = 8.0 / 1000;
793 MovingAverage<PacketSizeBytes>(packet_stream, begin_time_, end_time_,
794 window_duration_, step_, bytes_to_kilobits,
795 &time_series);
796 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700797 }
798
tereliusdc35dcd2016-08-01 12:03:27 -0700799 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
800 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700801 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700802 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700803 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700804 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700805 }
806}
807
tereliuse34c19c2016-08-15 08:47:14 -0700808void EventLogAnalyzer::CreateBweSimulationGraph(Plot* plot) {
Stefan Holmer13181032016-07-29 14:48:54 +0200809 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
810 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
811
812 for (const auto& kv : rtp_packets_) {
813 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
814 for (const LoggedRtpPacket& rtp_packet : kv.second)
815 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
816 }
817 }
818
819 for (const auto& kv : rtcp_packets_) {
820 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
821 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
822 incoming_rtcp.insert(
823 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
824 }
825 }
826
827 SimulatedClock clock(0);
828 BitrateObserver observer;
829 RtcEventLogNullImpl null_event_log;
830 CongestionController cc(&clock, &observer, &observer, &null_event_log);
831 // TODO(holmer): Log the call config and use that here instead.
832 static const uint32_t kDefaultStartBitrateBps = 300000;
833 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
834
835 TimeSeries time_series;
tereliuse34c19c2016-08-15 08:47:14 -0700836 time_series.label = "Delay-based estimate";
Stefan Holmer13181032016-07-29 14:48:54 +0200837 time_series.style = LINE_DOT_GRAPH;
Stefan Holmer13181032016-07-29 14:48:54 +0200838
839 auto rtp_iterator = outgoing_rtp.begin();
840 auto rtcp_iterator = incoming_rtcp.begin();
841
842 auto NextRtpTime = [&]() {
843 if (rtp_iterator != outgoing_rtp.end())
844 return static_cast<int64_t>(rtp_iterator->first);
845 return std::numeric_limits<int64_t>::max();
846 };
847
848 auto NextRtcpTime = [&]() {
849 if (rtcp_iterator != incoming_rtcp.end())
850 return static_cast<int64_t>(rtcp_iterator->first);
851 return std::numeric_limits<int64_t>::max();
852 };
853
854 auto NextProcessTime = [&]() {
855 if (rtcp_iterator != incoming_rtcp.end() ||
856 rtp_iterator != outgoing_rtp.end()) {
857 return clock.TimeInMicroseconds() +
858 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
859 }
860 return std::numeric_limits<int64_t>::max();
861 };
862
863 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
864 while (time_us != std::numeric_limits<int64_t>::max()) {
865 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
866 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -0700867 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200868 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
869 if (rtcp.type == kRtcpTransportFeedback) {
870 cc.GetTransportFeedbackObserver()->OnTransportFeedback(
871 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
872 }
873 ++rtcp_iterator;
874 }
875 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -0700876 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200877 const LoggedRtpPacket& rtp = *rtp_iterator->second;
878 if (rtp.header.extension.hasTransportSequenceNumber) {
879 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
880 cc.GetTransportFeedbackObserver()->AddPacket(
stefana93d5ac2016-08-17 02:14:32 -0700881 rtp.header.extension.transportSequenceNumber, rtp.total_length,
882 PacketInfo::kNotAProbe);
Stefan Holmer13181032016-07-29 14:48:54 +0200883 rtc::SentPacket sent_packet(
884 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
885 cc.OnSentPacket(sent_packet);
886 }
887 ++rtp_iterator;
888 }
stefanc3de0332016-08-02 07:22:17 -0700889 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
890 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200891 cc.Process();
stefanc3de0332016-08-02 07:22:17 -0700892 }
Stefan Holmer13181032016-07-29 14:48:54 +0200893 if (observer.GetAndResetBitrateUpdated()) {
894 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +0200895 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
896 1000000;
897 time_series.points.emplace_back(x, y);
898 }
899 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
900 }
901 // Add the data set to the plot.
tereliusdc35dcd2016-08-01 12:03:27 -0700902 plot->series_list_.push_back(std::move(time_series));
Stefan Holmer13181032016-07-29 14:48:54 +0200903
tereliusdc35dcd2016-08-01 12:03:27 -0700904 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
905 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
906 plot->SetTitle("Simulated BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +0200907}
908
tereliuse34c19c2016-08-15 08:47:14 -0700909void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanc3de0332016-08-02 07:22:17 -0700910 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
911 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
912
913 for (const auto& kv : rtp_packets_) {
914 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
915 for (const LoggedRtpPacket& rtp_packet : kv.second)
916 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
917 }
918 }
919
920 for (const auto& kv : rtcp_packets_) {
921 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
922 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
923 incoming_rtcp.insert(
924 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
925 }
926 }
927
928 SimulatedClock clock(0);
929 TransportFeedbackAdapter feedback_adapter(nullptr, &clock);
930
931 TimeSeries time_series;
932 time_series.label = "Network Delay Change";
933 time_series.style = LINE_DOT_GRAPH;
934 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
935
936 auto rtp_iterator = outgoing_rtp.begin();
937 auto rtcp_iterator = incoming_rtcp.begin();
938
939 auto NextRtpTime = [&]() {
940 if (rtp_iterator != outgoing_rtp.end())
941 return static_cast<int64_t>(rtp_iterator->first);
942 return std::numeric_limits<int64_t>::max();
943 };
944
945 auto NextRtcpTime = [&]() {
946 if (rtcp_iterator != incoming_rtcp.end())
947 return static_cast<int64_t>(rtcp_iterator->first);
948 return std::numeric_limits<int64_t>::max();
949 };
950
951 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
952 while (time_us != std::numeric_limits<int64_t>::max()) {
953 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
954 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
955 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
956 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
957 if (rtcp.type == kRtcpTransportFeedback) {
958 std::vector<PacketInfo> feedback =
959 feedback_adapter.GetPacketFeedbackVector(
960 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
961 for (const PacketInfo& packet : feedback) {
962 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
963 float x =
964 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
965 1000000;
966 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
967 time_series.points.emplace_back(x, y);
968 }
969 }
970 ++rtcp_iterator;
971 }
972 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
973 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
974 const LoggedRtpPacket& rtp = *rtp_iterator->second;
975 if (rtp.header.extension.hasTransportSequenceNumber) {
976 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
977 feedback_adapter.AddPacket(rtp.header.extension.transportSequenceNumber,
978 rtp.total_length, 0);
979 feedback_adapter.OnSentPacket(
980 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
981 }
982 ++rtp_iterator;
983 }
984 time_us = std::min(NextRtpTime(), NextRtcpTime());
985 }
986 // We assume that the base network delay (w/o queues) is the min delay
987 // observed during the call.
988 for (TimeSeriesPoint& point : time_series.points)
989 point.y -= estimated_base_delay_ms;
990 // Add the data set to the plot.
991 plot->series_list_.push_back(std::move(time_series));
992
993 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
994 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
995 plot->SetTitle("Network Delay Change.");
996}
terelius54ce6802016-07-13 06:44:41 -0700997} // namespace plotting
998} // namespace webrtc