blob: 2c340127de5c9e205015f457ae6755faadb78a19 [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
terelius54ce6802016-07-13 06:44:41 -070020#include "webrtc/base/checks.h"
stefan6a850c32016-07-29 10:28:08 -070021#include "webrtc/base/logging.h"
Stefan Holmer60e43462016-09-07 09:58:20 +020022#include "webrtc/base/rate_statistics.h"
ossuf515ab82016-12-07 04:52:58 -080023#include "webrtc/call/audio_receive_stream.h"
24#include "webrtc/call/audio_send_stream.h"
25#include "webrtc/call/call.h"
terelius54ce6802016-07-13 06:44:41 -070026#include "webrtc/common_types.h"
Stefan Holmer280de9e2016-09-30 10:06:51 +020027#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
Stefan Holmer13181032016-07-29 14:48:54 +020028#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
terelius4c9b4af2017-01-30 08:44:51 -080029#include "webrtc/modules/include/module_common_types.h"
terelius54ce6802016-07-13 06:44:41 -070030#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
31#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
danilchapbf369fe2016-10-07 07:39:54 -070032#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
Stefan Holmer13181032016-07-29 14:48:54 +020033#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
ossuf515ab82016-12-07 04:52:58 -080034#include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h"
35#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
terelius54ce6802016-07-13 06:44:41 -070036#include "webrtc/video_receive_stream.h"
37#include "webrtc/video_send_stream.h"
38
tereliusdc35dcd2016-08-01 12:03:27 -070039namespace webrtc {
40namespace plotting {
41
terelius54ce6802016-07-13 06:44:41 -070042namespace {
43
44std::string SsrcToString(uint32_t ssrc) {
45 std::stringstream ss;
46 ss << "SSRC " << ssrc;
47 return ss.str();
48}
49
50// Checks whether an SSRC is contained in the list of desired SSRCs.
51// Note that an empty SSRC list matches every SSRC.
52bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
53 if (desired_ssrc.size() == 0)
54 return true;
55 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
56 desired_ssrc.end();
57}
58
59double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
60 // The timestamp is a fixed point representation with 6 bits for seconds
61 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
62 // time in seconds and then multiply by 1000000 to convert to microseconds.
63 static constexpr double kTimestampToMicroSec =
tereliusccbbf8d2016-08-10 07:34:28 -070064 1000000.0 / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -070065 return abs_send_time * kTimestampToMicroSec;
66}
67
68// Computes the difference |later| - |earlier| where |later| and |earlier|
69// are counters that wrap at |modulus|. The difference is chosen to have the
70// least absolute value. For example if |modulus| is 8, then the difference will
71// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
72// be in [-4, 4].
73int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
74 RTC_DCHECK_LE(1, modulus);
75 RTC_DCHECK_LT(later, modulus);
76 RTC_DCHECK_LT(earlier, modulus);
77 int64_t difference =
78 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
79 int64_t max_difference = modulus / 2;
80 int64_t min_difference = max_difference - modulus + 1;
81 if (difference > max_difference) {
82 difference -= modulus;
83 }
84 if (difference < min_difference) {
85 difference += modulus;
86 }
terelius6addf492016-08-23 17:34:07 -070087 if (difference > max_difference / 2 || difference < min_difference / 2) {
88 LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
89 << " expected to be in the range (" << min_difference / 2
90 << "," << max_difference / 2 << ") but is " << difference
91 << ". Correct unwrapping is uncertain.";
92 }
terelius54ce6802016-07-13 06:44:41 -070093 return difference;
94}
95
ivocaac9d6f2016-09-22 07:01:47 -070096// Return default values for header extensions, to use on streams without stored
97// mapping data. Currently this only applies to audio streams, since the mapping
98// is not stored in the event log.
99// TODO(ivoc): Remove this once this mapping is stored in the event log for
100// audio streams. Tracking bug: webrtc:6399
101webrtc::RtpHeaderExtensionMap GetDefaultHeaderExtensionMap() {
102 webrtc::RtpHeaderExtensionMap default_map;
danilchap4aecc582016-11-15 09:21:00 -0800103 default_map.Register<AudioLevel>(webrtc::RtpExtension::kAudioLevelDefaultId);
104 default_map.Register<AbsoluteSendTime>(
ivocaac9d6f2016-09-22 07:01:47 -0700105 webrtc::RtpExtension::kAbsSendTimeDefaultId);
106 return default_map;
107}
108
tereliusdc35dcd2016-08-01 12:03:27 -0700109constexpr float kLeftMargin = 0.01f;
110constexpr float kRightMargin = 0.02f;
111constexpr float kBottomMargin = 0.02f;
112constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700113
terelius6addf492016-08-23 17:34:07 -0700114class PacketSizeBytes {
115 public:
116 using DataType = LoggedRtpPacket;
117 using ResultType = size_t;
118 size_t operator()(const LoggedRtpPacket& packet) {
119 return packet.total_length;
120 }
121};
122
123class SequenceNumberDiff {
124 public:
125 using DataType = LoggedRtpPacket;
126 using ResultType = int64_t;
127 int64_t operator()(const LoggedRtpPacket& old_packet,
128 const LoggedRtpPacket& new_packet) {
129 return WrappingDifference(new_packet.header.sequenceNumber,
130 old_packet.header.sequenceNumber, 1ul << 16);
131 }
132};
133
tereliusccbbf8d2016-08-10 07:34:28 -0700134class NetworkDelayDiff {
135 public:
136 class AbsSendTime {
137 public:
138 using DataType = LoggedRtpPacket;
139 using ResultType = double;
140 double operator()(const LoggedRtpPacket& old_packet,
141 const LoggedRtpPacket& new_packet) {
142 if (old_packet.header.extension.hasAbsoluteSendTime &&
143 new_packet.header.extension.hasAbsoluteSendTime) {
144 int64_t send_time_diff = WrappingDifference(
145 new_packet.header.extension.absoluteSendTime,
146 old_packet.header.extension.absoluteSendTime, 1ul << 24);
147 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
148 return static_cast<double>(recv_time_diff -
149 AbsSendTimeToMicroseconds(send_time_diff)) /
150 1000;
151 } else {
152 return 0;
153 }
154 }
155 };
156
157 class CaptureTime {
158 public:
159 using DataType = LoggedRtpPacket;
160 using ResultType = double;
161 double operator()(const LoggedRtpPacket& old_packet,
162 const LoggedRtpPacket& new_packet) {
163 int64_t send_time_diff = WrappingDifference(
164 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
165 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
166
167 const double kVideoSampleRate = 90000;
168 // TODO(terelius): We treat all streams as video for now, even though
169 // audio might be sampled at e.g. 16kHz, because it is really difficult to
170 // figure out the true sampling rate of a stream. The effect is that the
171 // delay will be scaled incorrectly for non-video streams.
172
173 double delay_change =
174 static_cast<double>(recv_time_diff) / 1000 -
175 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
terelius6addf492016-08-23 17:34:07 -0700176 if (delay_change < -10000 || 10000 < delay_change) {
177 LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
178 LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
179 << ", received time " << old_packet.timestamp;
180 LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
181 << ", received time " << new_packet.timestamp;
182 LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
183 << static_cast<double>(recv_time_diff) / 1000000 << "s";
184 LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
185 << static_cast<double>(send_time_diff) /
186 kVideoSampleRate
187 << "s";
188 }
tereliusccbbf8d2016-08-10 07:34:28 -0700189 return delay_change;
190 }
191 };
192};
193
194template <typename Extractor>
195class Accumulated {
196 public:
197 using DataType = typename Extractor::DataType;
198 using ResultType = typename Extractor::ResultType;
199 ResultType operator()(const DataType& old_packet,
200 const DataType& new_packet) {
201 sum += extract(old_packet, new_packet);
202 return sum;
203 }
204
205 private:
206 Extractor extract;
207 ResultType sum = 0;
208};
209
terelius6addf492016-08-23 17:34:07 -0700210// For each element in data, use |Extractor| to extract a y-coordinate and
211// store the result in a TimeSeries.
212template <typename Extractor>
213void Pointwise(const std::vector<typename Extractor::DataType>& data,
214 uint64_t begin_time,
215 TimeSeries* result) {
216 Extractor extract;
217 for (size_t i = 0; i < data.size(); i++) {
218 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
219 float y = extract(data[i]);
220 result->points.emplace_back(x, y);
221 }
222}
223
224// For each pair of adjacent elements in |data|, use |Extractor| to extract a
225// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
226// will be the time of the second element in the pair.
tereliusccbbf8d2016-08-10 07:34:28 -0700227template <typename Extractor>
228void Pairwise(const std::vector<typename Extractor::DataType>& data,
229 uint64_t begin_time,
230 TimeSeries* result) {
231 Extractor extract;
232 for (size_t i = 1; i < data.size(); i++) {
233 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
234 float y = extract(data[i - 1], data[i]);
235 result->points.emplace_back(x, y);
236 }
237}
238
terelius6addf492016-08-23 17:34:07 -0700239// Calculates a moving average of |data| and stores the result in a TimeSeries.
240// A data point is generated every |step| microseconds from |begin_time|
241// to |end_time|. The value of each data point is the average of the data
242// during the preceeding |window_duration_us| microseconds.
243template <typename Extractor>
244void MovingAverage(const std::vector<typename Extractor::DataType>& data,
245 uint64_t begin_time,
246 uint64_t end_time,
247 uint64_t window_duration_us,
248 uint64_t step,
249 float y_scaling,
250 webrtc::plotting::TimeSeries* result) {
251 size_t window_index_begin = 0;
252 size_t window_index_end = 0;
253 typename Extractor::ResultType sum_in_window = 0;
254 Extractor extract;
255
256 for (uint64_t t = begin_time; t < end_time + step; t += step) {
257 while (window_index_end < data.size() &&
258 data[window_index_end].timestamp < t) {
259 sum_in_window += extract(data[window_index_end]);
260 ++window_index_end;
261 }
262 while (window_index_begin < data.size() &&
263 data[window_index_begin].timestamp < t - window_duration_us) {
264 sum_in_window -= extract(data[window_index_begin]);
265 ++window_index_begin;
266 }
267 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
268 float x = static_cast<float>(t - begin_time) / 1000000;
269 float y = sum_in_window / window_duration_s * y_scaling;
270 result->points.emplace_back(x, y);
271 }
272}
273
terelius54ce6802016-07-13 06:44:41 -0700274} // namespace
275
terelius54ce6802016-07-13 06:44:41 -0700276EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
277 : parsed_log_(log), window_duration_(250000), step_(10000) {
278 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
279 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700280
Stefan Holmer13181032016-07-29 14:48:54 +0200281 // Maps a stream identifier consisting of ssrc and direction
terelius88e64e52016-07-19 01:51:06 -0700282 // to the header extensions used by that stream,
283 std::map<StreamId, RtpHeaderExtensionMap> extension_maps;
284
285 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700286 uint8_t header[IP_PACKET_SIZE];
287 size_t header_length;
288 size_t total_length;
289
ivocaac9d6f2016-09-22 07:01:47 -0700290 // Make a default extension map for streams without configuration information.
291 // TODO(ivoc): Once configuration of audio streams is stored in the event log,
292 // this can be removed. Tracking bug: webrtc:6399
293 RtpHeaderExtensionMap default_extension_map = GetDefaultHeaderExtensionMap();
294
terelius54ce6802016-07-13 06:44:41 -0700295 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
296 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700297 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
298 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
299 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700300 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
301 event_type != ParsedRtcEventLog::LOG_START &&
302 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700303 uint64_t timestamp = parsed_log_.GetTimestamp(i);
304 first_timestamp = std::min(first_timestamp, timestamp);
305 last_timestamp = std::max(last_timestamp, timestamp);
306 }
307
308 switch (parsed_log_.GetEventType(i)) {
309 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
310 VideoReceiveStream::Config config(nullptr);
311 parsed_log_.GetVideoReceiveConfig(i, &config);
Stefan Holmer13181032016-07-29 14:48:54 +0200312 StreamId stream(config.rtp.remote_ssrc, kIncomingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800313 extension_maps[stream] = RtpHeaderExtensionMap(config.rtp.extensions);
terelius0740a202016-08-08 10:21:04 -0700314 video_ssrcs_.insert(stream);
brandtr14742122017-01-27 04:53:07 -0800315 StreamId rtx_stream(config.rtp.rtx_ssrc, kIncomingPacket);
316 extension_maps[rtx_stream] =
317 RtpHeaderExtensionMap(config.rtp.extensions);
318 video_ssrcs_.insert(rtx_stream);
319 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700320 break;
321 }
322 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
323 VideoSendStream::Config config(nullptr);
324 parsed_log_.GetVideoSendConfig(i, &config);
325 for (auto ssrc : config.rtp.ssrcs) {
Stefan Holmer13181032016-07-29 14:48:54 +0200326 StreamId stream(ssrc, kOutgoingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800327 extension_maps[stream] = RtpHeaderExtensionMap(config.rtp.extensions);
terelius0740a202016-08-08 10:21:04 -0700328 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700329 }
330 for (auto ssrc : config.rtp.rtx.ssrcs) {
terelius0740a202016-08-08 10:21:04 -0700331 StreamId rtx_stream(ssrc, kOutgoingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800332 extension_maps[rtx_stream] =
333 RtpHeaderExtensionMap(config.rtp.extensions);
terelius0740a202016-08-08 10:21:04 -0700334 video_ssrcs_.insert(rtx_stream);
335 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700336 }
337 break;
338 }
339 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
340 AudioReceiveStream::Config config;
ivoce0928d82016-10-10 05:12:51 -0700341 parsed_log_.GetAudioReceiveConfig(i, &config);
342 StreamId stream(config.rtp.remote_ssrc, kIncomingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800343 extension_maps[stream] = RtpHeaderExtensionMap(config.rtp.extensions);
ivoce0928d82016-10-10 05:12:51 -0700344 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700345 break;
346 }
347 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
348 AudioSendStream::Config config(nullptr);
ivoce0928d82016-10-10 05:12:51 -0700349 parsed_log_.GetAudioSendConfig(i, &config);
350 StreamId stream(config.rtp.ssrc, kOutgoingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800351 extension_maps[stream] = RtpHeaderExtensionMap(config.rtp.extensions);
ivoce0928d82016-10-10 05:12:51 -0700352 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700353 break;
354 }
355 case ParsedRtcEventLog::RTP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200356 MediaType media_type;
terelius88e64e52016-07-19 01:51:06 -0700357 parsed_log_.GetRtpHeader(i, &direction, &media_type, header,
358 &header_length, &total_length);
359 // Parse header to get SSRC.
360 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
361 RTPHeader parsed_header;
362 rtp_parser.Parse(&parsed_header);
Stefan Holmer13181032016-07-29 14:48:54 +0200363 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700364 // Look up the extension_map and parse it again to get the extensions.
365 if (extension_maps.count(stream) == 1) {
366 RtpHeaderExtensionMap* extension_map = &extension_maps[stream];
367 rtp_parser.Parse(&parsed_header, extension_map);
ivocaac9d6f2016-09-22 07:01:47 -0700368 } else {
369 // Use the default extension map.
370 // TODO(ivoc): Once configuration of audio streams is stored in the
371 // event log, this can be removed.
372 // Tracking bug: webrtc:6399
373 rtp_parser.Parse(&parsed_header, &default_extension_map);
terelius88e64e52016-07-19 01:51:06 -0700374 }
375 uint64_t timestamp = parsed_log_.GetTimestamp(i);
376 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200377 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700378 break;
379 }
380 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200381 uint8_t packet[IP_PACKET_SIZE];
382 MediaType media_type;
383 parsed_log_.GetRtcpPacket(i, &direction, &media_type, packet,
384 &total_length);
385
danilchapbf369fe2016-10-07 07:39:54 -0700386 // Currently feedback is logged twice, both for audio and video.
387 // Only act on one of them.
388 if (media_type == MediaType::VIDEO) {
389 rtcp::CommonHeader header;
390 const uint8_t* packet_end = packet + total_length;
391 for (const uint8_t* block = packet; block < packet_end;
392 block = header.NextPacket()) {
393 RTC_CHECK(header.Parse(block, packet_end - block));
394 if (header.type() == rtcp::TransportFeedback::kPacketType &&
395 header.fmt() == rtcp::TransportFeedback::kFeedbackMessageType) {
396 std::unique_ptr<rtcp::TransportFeedback> rtcp_packet(
397 new rtcp::TransportFeedback());
398 if (rtcp_packet->Parse(header)) {
399 uint32_t ssrc = rtcp_packet->sender_ssrc();
Stefan Holmer13181032016-07-29 14:48:54 +0200400 StreamId stream(ssrc, direction);
401 uint64_t timestamp = parsed_log_.GetTimestamp(i);
402 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
403 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
404 }
Stefan Holmer13181032016-07-29 14:48:54 +0200405 }
Stefan Holmer13181032016-07-29 14:48:54 +0200406 }
Stefan Holmer13181032016-07-29 14:48:54 +0200407 }
terelius88e64e52016-07-19 01:51:06 -0700408 break;
409 }
410 case ParsedRtcEventLog::LOG_START: {
411 break;
412 }
413 case ParsedRtcEventLog::LOG_END: {
414 break;
415 }
416 case ParsedRtcEventLog::BWE_PACKET_LOSS_EVENT: {
terelius8058e582016-07-25 01:32:41 -0700417 BwePacketLossEvent bwe_update;
418 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
419 parsed_log_.GetBwePacketLossEvent(i, &bwe_update.new_bitrate,
420 &bwe_update.fraction_loss,
421 &bwe_update.expected_packets);
422 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700423 break;
424 }
minyue4b7c9522017-01-24 04:54:59 -0800425 case ParsedRtcEventLog::AUDIO_NETWORK_ADAPTATION_EVENT: {
426 break;
427 }
terelius88e64e52016-07-19 01:51:06 -0700428 case ParsedRtcEventLog::BWE_PACKET_DELAY_EVENT: {
429 break;
430 }
431 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
432 break;
433 }
434 case ParsedRtcEventLog::UNKNOWN_EVENT: {
435 break;
436 }
437 }
terelius54ce6802016-07-13 06:44:41 -0700438 }
terelius88e64e52016-07-19 01:51:06 -0700439
terelius54ce6802016-07-13 06:44:41 -0700440 if (last_timestamp < first_timestamp) {
441 // No useful events in the log.
442 first_timestamp = last_timestamp = 0;
443 }
444 begin_time_ = first_timestamp;
445 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700446 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
terelius54ce6802016-07-13 06:44:41 -0700447}
448
Stefan Holmer13181032016-07-29 14:48:54 +0200449class BitrateObserver : public CongestionController::Observer,
450 public RemoteBitrateObserver {
451 public:
452 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
453
minyue78b4d562016-11-30 04:47:39 -0800454 // TODO(minyue): remove this when old OnNetworkChanged is deprecated. See
455 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6796
456 using CongestionController::Observer::OnNetworkChanged;
457
Stefan Holmer13181032016-07-29 14:48:54 +0200458 void OnNetworkChanged(uint32_t bitrate_bps,
459 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800460 int64_t rtt_ms,
461 int64_t probing_interval_ms) override {
Stefan Holmer13181032016-07-29 14:48:54 +0200462 last_bitrate_bps_ = bitrate_bps;
463 bitrate_updated_ = true;
464 }
465
466 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
467 uint32_t bitrate) override {}
468
469 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
470 bool GetAndResetBitrateUpdated() {
471 bool bitrate_updated = bitrate_updated_;
472 bitrate_updated_ = false;
473 return bitrate_updated;
474 }
475
476 private:
477 uint32_t last_bitrate_bps_;
478 bool bitrate_updated_;
479};
480
Stefan Holmer99f8e082016-09-09 13:37:50 +0200481bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700482 return rtx_ssrcs_.count(stream_id) == 1;
483}
484
Stefan Holmer99f8e082016-09-09 13:37:50 +0200485bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700486 return video_ssrcs_.count(stream_id) == 1;
487}
488
Stefan Holmer99f8e082016-09-09 13:37:50 +0200489bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700490 return audio_ssrcs_.count(stream_id) == 1;
491}
492
Stefan Holmer99f8e082016-09-09 13:37:50 +0200493std::string EventLogAnalyzer::GetStreamName(StreamId stream_id) const {
494 std::stringstream name;
495 if (IsAudioSsrc(stream_id)) {
496 name << "Audio ";
497 } else if (IsVideoSsrc(stream_id)) {
498 name << "Video ";
499 } else {
500 name << "Unknown ";
501 }
502 if (IsRtxSsrc(stream_id))
503 name << "RTX ";
ivocaac9d6f2016-09-22 07:01:47 -0700504 if (stream_id.GetDirection() == kIncomingPacket) {
505 name << "(In) ";
506 } else {
507 name << "(Out) ";
508 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200509 name << SsrcToString(stream_id.GetSsrc());
510 return name.str();
511}
512
terelius54ce6802016-07-13 06:44:41 -0700513void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
514 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700515 for (auto& kv : rtp_packets_) {
516 StreamId stream_id = kv.first;
517 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
518 // Filter on direction and SSRC.
519 if (stream_id.GetDirection() != desired_direction ||
520 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
521 continue;
terelius54ce6802016-07-13 06:44:41 -0700522 }
terelius54ce6802016-07-13 06:44:41 -0700523
terelius6addf492016-08-23 17:34:07 -0700524 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200525 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700526 time_series.style = BAR_GRAPH;
527 Pointwise<PacketSizeBytes>(packet_stream, begin_time_, &time_series);
528 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700529 }
530
tereliusdc35dcd2016-08-01 12:03:27 -0700531 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
532 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
533 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700534 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700535 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700536 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700537 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700538 }
539}
540
philipelccd74892016-09-05 02:46:25 -0700541template <typename T>
542void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
543 PacketDirection desired_direction,
544 Plot* plot,
545 const std::map<StreamId, std::vector<T>>& packets,
546 const std::string& label_prefix) {
547 for (auto& kv : packets) {
548 StreamId stream_id = kv.first;
549 const std::vector<T>& packet_stream = kv.second;
550 // Filter on direction and SSRC.
551 if (stream_id.GetDirection() != desired_direction ||
552 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
553 continue;
554 }
555
556 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200557 time_series.label = label_prefix + " " + GetStreamName(stream_id);
terelius77f05802017-02-01 06:34:53 -0800558 time_series.style = LINE_STEP_GRAPH;
philipelccd74892016-09-05 02:46:25 -0700559
560 for (size_t i = 0; i < packet_stream.size(); i++) {
561 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
562 1000000;
philipelccd74892016-09-05 02:46:25 -0700563 time_series.points.emplace_back(x, i + 1);
564 }
565
566 plot->series_list_.push_back(std::move(time_series));
567 }
568}
569
570void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
571 PacketDirection desired_direction,
572 Plot* plot) {
573 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
574 "RTP");
575 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
576 "RTCP");
577
578 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
579 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
580 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
581 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
582 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
583 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
584 }
585}
586
terelius54ce6802016-07-13 06:44:41 -0700587// For each SSRC, plot the time between the consecutive playouts.
588void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
589 std::map<uint32_t, TimeSeries> time_series;
590 std::map<uint32_t, uint64_t> last_playout;
591
592 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700593
594 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
595 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
596 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
597 parsed_log_.GetAudioPlayout(i, &ssrc);
598 uint64_t timestamp = parsed_log_.GetTimestamp(i);
599 if (MatchingSsrc(ssrc, desired_ssrc_)) {
600 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
601 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
602 if (time_series[ssrc].points.size() == 0) {
603 // There were no previusly logged playout for this SSRC.
604 // Generate a point, but place it on the x-axis.
605 y = 0;
606 }
terelius54ce6802016-07-13 06:44:41 -0700607 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
608 last_playout[ssrc] = timestamp;
609 }
610 }
611 }
612
613 // Set labels and put in graph.
614 for (auto& kv : time_series) {
615 kv.second.label = SsrcToString(kv.first);
616 kv.second.style = BAR_GRAPH;
tereliusdc35dcd2016-08-01 12:03:27 -0700617 plot->series_list_.push_back(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700618 }
619
tereliusdc35dcd2016-08-01 12:03:27 -0700620 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
621 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
622 kTopMargin);
623 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700624}
625
ivocaac9d6f2016-09-22 07:01:47 -0700626// For audio SSRCs, plot the audio level.
627void EventLogAnalyzer::CreateAudioLevelGraph(Plot* plot) {
628 std::map<StreamId, TimeSeries> time_series;
629
630 for (auto& kv : rtp_packets_) {
631 StreamId stream_id = kv.first;
632 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
633 // TODO(ivoc): When audio send/receive configs are stored in the event
634 // log, a check should be added here to only process audio
635 // streams. Tracking bug: webrtc:6399
636 for (auto& packet : packet_stream) {
637 if (packet.header.extension.hasAudioLevel) {
638 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
639 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
640 // Here we convert it to dBov.
641 float y = static_cast<float>(-packet.header.extension.audioLevel);
642 time_series[stream_id].points.emplace_back(TimeSeriesPoint(x, y));
643 }
644 }
645 }
646
647 for (auto& series : time_series) {
648 series.second.label = GetStreamName(series.first);
649 series.second.style = LINE_GRAPH;
650 plot->series_list_.push_back(std::move(series.second));
651 }
652
653 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
ivocbf676632016-11-24 08:30:34 -0800654 plot->SetYAxis(-127, 0, "Audio level (dBov)", kBottomMargin,
ivocaac9d6f2016-09-22 07:01:47 -0700655 kTopMargin);
656 plot->SetTitle("Audio level");
657}
658
terelius54ce6802016-07-13 06:44:41 -0700659// For each SSRC, plot the time between the consecutive playouts.
660void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700661 for (auto& kv : rtp_packets_) {
662 StreamId stream_id = kv.first;
663 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
664 // Filter on direction and SSRC.
665 if (stream_id.GetDirection() != kIncomingPacket ||
666 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
667 continue;
terelius54ce6802016-07-13 06:44:41 -0700668 }
terelius54ce6802016-07-13 06:44:41 -0700669
terelius6addf492016-08-23 17:34:07 -0700670 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200671 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700672 time_series.style = BAR_GRAPH;
673 Pairwise<SequenceNumberDiff>(packet_stream, begin_time_, &time_series);
674 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700675 }
676
tereliusdc35dcd2016-08-01 12:03:27 -0700677 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
678 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
679 kTopMargin);
680 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700681}
682
Stefan Holmer99f8e082016-09-09 13:37:50 +0200683void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
684 for (auto& kv : rtp_packets_) {
685 StreamId stream_id = kv.first;
686 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
687 // Filter on direction and SSRC.
688 if (stream_id.GetDirection() != kIncomingPacket ||
terelius4c9b4af2017-01-30 08:44:51 -0800689 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
690 packet_stream.size() == 0) {
Stefan Holmer99f8e082016-09-09 13:37:50 +0200691 continue;
692 }
693
694 TimeSeries time_series;
695 time_series.label = GetStreamName(stream_id);
696 time_series.style = LINE_DOT_GRAPH;
697 const uint64_t kWindowUs = 1000000;
terelius4c9b4af2017-01-30 08:44:51 -0800698 const uint64_t kStep = 1000000;
699 SequenceNumberUnwrapper unwrapper_;
700 SequenceNumberUnwrapper prior_unwrapper_;
701 size_t window_index_begin = 0;
702 size_t window_index_end = 0;
703 int64_t highest_seq_number =
704 unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
705 int64_t highest_prior_seq_number =
706 prior_unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
707
708 for (uint64_t t = begin_time_; t < end_time_ + kStep; t += kStep) {
709 while (window_index_end < packet_stream.size() &&
710 packet_stream[window_index_end].timestamp < t) {
711 int64_t sequence_number = unwrapper_.Unwrap(
712 packet_stream[window_index_end].header.sequenceNumber);
713 highest_seq_number = std::max(highest_seq_number, sequence_number);
714 ++window_index_end;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200715 }
terelius4c9b4af2017-01-30 08:44:51 -0800716 while (window_index_begin < packet_stream.size() &&
717 packet_stream[window_index_begin].timestamp < t - kWindowUs) {
718 int64_t sequence_number = prior_unwrapper_.Unwrap(
719 packet_stream[window_index_begin].header.sequenceNumber);
720 highest_prior_seq_number =
721 std::max(highest_prior_seq_number, sequence_number);
722 ++window_index_begin;
723 }
724 float x = static_cast<float>(t - begin_time_) / 1000000;
725 int64_t expected_packets = highest_seq_number - highest_prior_seq_number;
726 if (expected_packets > 0) {
727 int64_t received_packets = window_index_end - window_index_begin;
728 int64_t lost_packets = expected_packets - received_packets;
729 float y = static_cast<float>(lost_packets) / expected_packets * 100;
730 time_series.points.emplace_back(x, y);
731 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200732 }
733 plot->series_list_.push_back(std::move(time_series));
734 }
735
736 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
737 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
738 kTopMargin);
739 plot->SetTitle("Estimated incoming loss rate");
740}
741
terelius54ce6802016-07-13 06:44:41 -0700742void EventLogAnalyzer::CreateDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700743 for (auto& kv : rtp_packets_) {
744 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700745 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700746 // Filter on direction and SSRC.
747 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200748 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
749 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
750 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700751 continue;
752 }
terelius54ce6802016-07-13 06:44:41 -0700753
tereliusccbbf8d2016-08-10 07:34:28 -0700754 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200755 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700756 capture_time_data.style = BAR_GRAPH;
757 Pairwise<NetworkDelayDiff::CaptureTime>(packet_stream, begin_time_,
758 &capture_time_data);
759 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700760
tereliusccbbf8d2016-08-10 07:34:28 -0700761 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200762 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700763 send_time_data.style = BAR_GRAPH;
764 Pairwise<NetworkDelayDiff::AbsSendTime>(packet_stream, begin_time_,
765 &send_time_data);
766 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700767 }
768
tereliusdc35dcd2016-08-01 12:03:27 -0700769 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
770 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
771 kTopMargin);
772 plot->SetTitle("Network latency change between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700773}
774
775void EventLogAnalyzer::CreateAccumulatedDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700776 for (auto& kv : rtp_packets_) {
777 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700778 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700779 // Filter on direction and SSRC.
780 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200781 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
782 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
783 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700784 continue;
785 }
terelius54ce6802016-07-13 06:44:41 -0700786
tereliusccbbf8d2016-08-10 07:34:28 -0700787 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200788 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700789 capture_time_data.style = LINE_GRAPH;
790 Pairwise<Accumulated<NetworkDelayDiff::CaptureTime>>(
791 packet_stream, begin_time_, &capture_time_data);
792 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700793
tereliusccbbf8d2016-08-10 07:34:28 -0700794 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200795 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700796 send_time_data.style = LINE_GRAPH;
797 Pairwise<Accumulated<NetworkDelayDiff::AbsSendTime>>(
798 packet_stream, begin_time_, &send_time_data);
799 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700800 }
801
tereliusdc35dcd2016-08-01 12:03:27 -0700802 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
803 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
804 kTopMargin);
805 plot->SetTitle("Accumulated network latency change");
terelius54ce6802016-07-13 06:44:41 -0700806}
807
tereliusf736d232016-08-04 10:00:11 -0700808// Plot the fraction of packets lost (as perceived by the loss-based BWE).
809void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
810 plot->series_list_.push_back(TimeSeries());
811 for (auto& bwe_update : bwe_loss_updates_) {
812 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
813 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
814 plot->series_list_.back().points.emplace_back(x, y);
815 }
816 plot->series_list_.back().label = "Fraction lost";
817 plot->series_list_.back().style = LINE_DOT_GRAPH;
818
819 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
820 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
821 kTopMargin);
822 plot->SetTitle("Reported packet loss");
823}
824
terelius54ce6802016-07-13 06:44:41 -0700825// Plot the total bandwidth used by all RTP streams.
826void EventLogAnalyzer::CreateTotalBitrateGraph(
827 PacketDirection desired_direction,
828 Plot* plot) {
829 struct TimestampSize {
830 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
831 uint64_t timestamp;
832 size_t size;
833 };
834 std::vector<TimestampSize> packets;
835
836 PacketDirection direction;
837 size_t total_length;
838
839 // Extract timestamps and sizes for the relevant packets.
840 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
841 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
842 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
843 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, nullptr,
844 &total_length);
845 if (direction == desired_direction) {
846 uint64_t timestamp = parsed_log_.GetTimestamp(i);
847 packets.push_back(TimestampSize(timestamp, total_length));
848 }
849 }
850 }
851
852 size_t window_index_begin = 0;
853 size_t window_index_end = 0;
854 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700855
856 // Calculate a moving average of the bitrate and store in a TimeSeries.
tereliusdc35dcd2016-08-01 12:03:27 -0700857 plot->series_list_.push_back(TimeSeries());
terelius54ce6802016-07-13 06:44:41 -0700858 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
859 while (window_index_end < packets.size() &&
860 packets[window_index_end].timestamp < time) {
861 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700862 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700863 }
864 while (window_index_begin < packets.size() &&
865 packets[window_index_begin].timestamp < time - window_duration_) {
866 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
867 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700868 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700869 }
870 float window_duration_in_seconds =
871 static_cast<float>(window_duration_) / 1000000;
872 float x = static_cast<float>(time - begin_time_) / 1000000;
873 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700874 plot->series_list_.back().points.push_back(TimeSeriesPoint(x, y));
terelius54ce6802016-07-13 06:44:41 -0700875 }
876
877 // Set labels.
878 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700879 plot->series_list_.back().label = "Incoming bitrate";
terelius54ce6802016-07-13 06:44:41 -0700880 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700881 plot->series_list_.back().label = "Outgoing bitrate";
terelius54ce6802016-07-13 06:44:41 -0700882 }
tereliusdc35dcd2016-08-01 12:03:27 -0700883 plot->series_list_.back().style = LINE_GRAPH;
terelius54ce6802016-07-13 06:44:41 -0700884
terelius8058e582016-07-25 01:32:41 -0700885 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
886 if (desired_direction == kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700887 plot->series_list_.push_back(TimeSeries());
terelius8058e582016-07-25 01:32:41 -0700888 for (auto& bwe_update : bwe_loss_updates_) {
889 float x =
890 static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
891 float y = static_cast<float>(bwe_update.new_bitrate) / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700892 plot->series_list_.back().points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -0700893 }
tereliusdc35dcd2016-08-01 12:03:27 -0700894 plot->series_list_.back().label = "Loss-based estimate";
terelius77f05802017-02-01 06:34:53 -0800895 plot->series_list_.back().style = LINE_STEP_GRAPH;
terelius8058e582016-07-25 01:32:41 -0700896 }
tereliusdc35dcd2016-08-01 12:03:27 -0700897 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
898 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700899 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700900 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700901 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700902 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700903 }
904}
905
906// For each SSRC, plot the bandwidth used by that stream.
907void EventLogAnalyzer::CreateStreamBitrateGraph(
908 PacketDirection desired_direction,
909 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700910 for (auto& kv : rtp_packets_) {
911 StreamId stream_id = kv.first;
912 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
913 // Filter on direction and SSRC.
914 if (stream_id.GetDirection() != desired_direction ||
915 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
916 continue;
terelius54ce6802016-07-13 06:44:41 -0700917 }
918
terelius6addf492016-08-23 17:34:07 -0700919 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200920 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700921 time_series.style = LINE_GRAPH;
922 double bytes_to_kilobits = 8.0 / 1000;
923 MovingAverage<PacketSizeBytes>(packet_stream, begin_time_, end_time_,
924 window_duration_, step_, bytes_to_kilobits,
925 &time_series);
926 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700927 }
928
tereliusdc35dcd2016-08-01 12:03:27 -0700929 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
930 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700931 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700932 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700933 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700934 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700935 }
936}
937
tereliuse34c19c2016-08-15 08:47:14 -0700938void EventLogAnalyzer::CreateBweSimulationGraph(Plot* plot) {
Stefan Holmer13181032016-07-29 14:48:54 +0200939 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
940 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
941
942 for (const auto& kv : rtp_packets_) {
943 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
944 for (const LoggedRtpPacket& rtp_packet : kv.second)
945 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
946 }
947 }
948
949 for (const auto& kv : rtcp_packets_) {
950 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
951 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
952 incoming_rtcp.insert(
953 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
954 }
955 }
956
957 SimulatedClock clock(0);
958 BitrateObserver observer;
959 RtcEventLogNullImpl null_event_log;
nisse0245da02016-11-30 03:35:20 -0800960 PacketRouter packet_router;
961 CongestionController cc(&clock, &observer, &observer, &null_event_log,
962 &packet_router);
Stefan Holmer13181032016-07-29 14:48:54 +0200963 // TODO(holmer): Log the call config and use that here instead.
964 static const uint32_t kDefaultStartBitrateBps = 300000;
965 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
966
967 TimeSeries time_series;
tereliuse34c19c2016-08-15 08:47:14 -0700968 time_series.label = "Delay-based estimate";
Stefan Holmer13181032016-07-29 14:48:54 +0200969 time_series.style = LINE_DOT_GRAPH;
Stefan Holmer60e43462016-09-07 09:58:20 +0200970 TimeSeries acked_time_series;
971 acked_time_series.label = "Acked bitrate";
972 acked_time_series.style = LINE_DOT_GRAPH;
Stefan Holmer13181032016-07-29 14:48:54 +0200973
974 auto rtp_iterator = outgoing_rtp.begin();
975 auto rtcp_iterator = incoming_rtcp.begin();
976
977 auto NextRtpTime = [&]() {
978 if (rtp_iterator != outgoing_rtp.end())
979 return static_cast<int64_t>(rtp_iterator->first);
980 return std::numeric_limits<int64_t>::max();
981 };
982
983 auto NextRtcpTime = [&]() {
984 if (rtcp_iterator != incoming_rtcp.end())
985 return static_cast<int64_t>(rtcp_iterator->first);
986 return std::numeric_limits<int64_t>::max();
987 };
988
989 auto NextProcessTime = [&]() {
990 if (rtcp_iterator != incoming_rtcp.end() ||
991 rtp_iterator != outgoing_rtp.end()) {
992 return clock.TimeInMicroseconds() +
993 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
994 }
995 return std::numeric_limits<int64_t>::max();
996 };
997
Stefan Holmer492ee282016-10-27 17:19:20 +0200998 RateStatistics acked_bitrate(250, 8000);
Stefan Holmer60e43462016-09-07 09:58:20 +0200999
Stefan Holmer13181032016-07-29 14:48:54 +02001000 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
Stefan Holmer492ee282016-10-27 17:19:20 +02001001 int64_t last_update_us = 0;
Stefan Holmer13181032016-07-29 14:48:54 +02001002 while (time_us != std::numeric_limits<int64_t>::max()) {
1003 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1004 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001005 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001006 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1007 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001008 TransportFeedbackObserver* observer = cc.GetTransportFeedbackObserver();
1009 observer->OnTransportFeedback(*static_cast<rtcp::TransportFeedback*>(
1010 rtcp.packet.get()));
1011 std::vector<PacketInfo> feedback =
1012 observer->GetTransportFeedbackVector();
1013 rtc::Optional<uint32_t> bitrate_bps;
1014 if (!feedback.empty()) {
1015 for (const PacketInfo& packet : feedback)
1016 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1017 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
1018 }
1019 uint32_t y = 0;
1020 if (bitrate_bps)
1021 y = *bitrate_bps / 1000;
1022 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1023 1000000;
1024 acked_time_series.points.emplace_back(x, y);
Stefan Holmer13181032016-07-29 14:48:54 +02001025 }
1026 ++rtcp_iterator;
1027 }
1028 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001029 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001030 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1031 if (rtp.header.extension.hasTransportSequenceNumber) {
1032 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1033 cc.GetTransportFeedbackObserver()->AddPacket(
stefana93d5ac2016-08-17 02:14:32 -07001034 rtp.header.extension.transportSequenceNumber, rtp.total_length,
1035 PacketInfo::kNotAProbe);
Stefan Holmer13181032016-07-29 14:48:54 +02001036 rtc::SentPacket sent_packet(
1037 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1038 cc.OnSentPacket(sent_packet);
1039 }
1040 ++rtp_iterator;
1041 }
stefanc3de0332016-08-02 07:22:17 -07001042 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1043 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001044 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001045 }
Stefan Holmer492ee282016-10-27 17:19:20 +02001046 if (observer.GetAndResetBitrateUpdated() ||
1047 time_us - last_update_us >= 1e6) {
Stefan Holmer13181032016-07-29 14:48:54 +02001048 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +02001049 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1050 1000000;
1051 time_series.points.emplace_back(x, y);
Stefan Holmer492ee282016-10-27 17:19:20 +02001052 last_update_us = time_us;
Stefan Holmer13181032016-07-29 14:48:54 +02001053 }
1054 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1055 }
1056 // Add the data set to the plot.
tereliusdc35dcd2016-08-01 12:03:27 -07001057 plot->series_list_.push_back(std::move(time_series));
Stefan Holmer60e43462016-09-07 09:58:20 +02001058 plot->series_list_.push_back(std::move(acked_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001059
tereliusdc35dcd2016-08-01 12:03:27 -07001060 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1061 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1062 plot->SetTitle("Simulated BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001063}
1064
Stefan Holmer280de9e2016-09-30 10:06:51 +02001065// TODO(holmer): Remove once TransportFeedbackAdapter no longer needs a
1066// BitrateController.
1067class NullBitrateController : public BitrateController {
1068 public:
1069 ~NullBitrateController() override {}
1070 RtcpBandwidthObserver* CreateRtcpBandwidthObserver() override {
1071 return nullptr;
1072 }
1073 void SetStartBitrate(int start_bitrate_bps) override {}
1074 void SetMinMaxBitrate(int min_bitrate_bps, int max_bitrate_bps) override {}
1075 void SetBitrates(int start_bitrate_bps,
1076 int min_bitrate_bps,
1077 int max_bitrate_bps) override {}
1078 void ResetBitrates(int bitrate_bps,
1079 int min_bitrate_bps,
1080 int max_bitrate_bps) override {}
1081 void OnDelayBasedBweResult(const DelayBasedBwe::Result& result) override {}
1082 bool AvailableBandwidth(uint32_t* bandwidth) const override { return false; }
1083 void SetReservedBitrate(uint32_t reserved_bitrate_bps) override {}
1084 bool GetNetworkParameters(uint32_t* bitrate,
1085 uint8_t* fraction_loss,
1086 int64_t* rtt) override {
1087 return false;
1088 }
1089 int64_t TimeUntilNextProcess() override { return 0; }
1090 void Process() override {}
1091};
1092
tereliuse34c19c2016-08-15 08:47:14 -07001093void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanc3de0332016-08-02 07:22:17 -07001094 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1095 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
1096
1097 for (const auto& kv : rtp_packets_) {
1098 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1099 for (const LoggedRtpPacket& rtp_packet : kv.second)
1100 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1101 }
1102 }
1103
1104 for (const auto& kv : rtcp_packets_) {
1105 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1106 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1107 incoming_rtcp.insert(
1108 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1109 }
1110 }
1111
1112 SimulatedClock clock(0);
Stefan Holmer280de9e2016-09-30 10:06:51 +02001113 NullBitrateController null_controller;
1114 TransportFeedbackAdapter feedback_adapter(&clock, &null_controller);
stefan41aab322016-10-10 08:16:30 -07001115 feedback_adapter.InitBwe();
stefanc3de0332016-08-02 07:22:17 -07001116
1117 TimeSeries time_series;
1118 time_series.label = "Network Delay Change";
1119 time_series.style = LINE_DOT_GRAPH;
1120 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1121
1122 auto rtp_iterator = outgoing_rtp.begin();
1123 auto rtcp_iterator = incoming_rtcp.begin();
1124
1125 auto NextRtpTime = [&]() {
1126 if (rtp_iterator != outgoing_rtp.end())
1127 return static_cast<int64_t>(rtp_iterator->first);
1128 return std::numeric_limits<int64_t>::max();
1129 };
1130
1131 auto NextRtcpTime = [&]() {
1132 if (rtcp_iterator != incoming_rtcp.end())
1133 return static_cast<int64_t>(rtcp_iterator->first);
1134 return std::numeric_limits<int64_t>::max();
1135 };
1136
1137 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
1138 while (time_us != std::numeric_limits<int64_t>::max()) {
1139 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1140 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1141 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1142 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1143 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001144 feedback_adapter.OnTransportFeedback(
1145 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
stefanc3de0332016-08-02 07:22:17 -07001146 std::vector<PacketInfo> feedback =
Stefan Holmer60e43462016-09-07 09:58:20 +02001147 feedback_adapter.GetTransportFeedbackVector();
stefanc3de0332016-08-02 07:22:17 -07001148 for (const PacketInfo& packet : feedback) {
1149 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1150 float x =
1151 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1152 1000000;
1153 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1154 time_series.points.emplace_back(x, y);
1155 }
1156 }
1157 ++rtcp_iterator;
1158 }
1159 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1160 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1161 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1162 if (rtp.header.extension.hasTransportSequenceNumber) {
1163 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1164 feedback_adapter.AddPacket(rtp.header.extension.transportSequenceNumber,
stefan985d2802016-11-15 06:54:09 -08001165 rtp.total_length, PacketInfo::kNotAProbe);
stefanc3de0332016-08-02 07:22:17 -07001166 feedback_adapter.OnSentPacket(
1167 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1168 }
1169 ++rtp_iterator;
1170 }
1171 time_us = std::min(NextRtpTime(), NextRtcpTime());
1172 }
1173 // We assume that the base network delay (w/o queues) is the min delay
1174 // observed during the call.
1175 for (TimeSeriesPoint& point : time_series.points)
1176 point.y -= estimated_base_delay_ms;
1177 // Add the data set to the plot.
1178 plot->series_list_.push_back(std::move(time_series));
1179
1180 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1181 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1182 plot->SetTitle("Network Delay Change.");
1183}
stefan08383272016-12-20 08:51:52 -08001184
1185std::vector<std::pair<int64_t, int64_t>> EventLogAnalyzer::GetFrameTimestamps()
1186 const {
1187 std::vector<std::pair<int64_t, int64_t>> timestamps;
1188 size_t largest_stream_size = 0;
1189 const std::vector<LoggedRtpPacket>* largest_video_stream = nullptr;
1190 // Find the incoming video stream with the most number of packets that is
1191 // not rtx.
1192 for (const auto& kv : rtp_packets_) {
1193 if (kv.first.GetDirection() == kIncomingPacket &&
1194 video_ssrcs_.find(kv.first) != video_ssrcs_.end() &&
1195 rtx_ssrcs_.find(kv.first) == rtx_ssrcs_.end() &&
1196 kv.second.size() > largest_stream_size) {
1197 largest_stream_size = kv.second.size();
1198 largest_video_stream = &kv.second;
1199 }
1200 }
1201 if (largest_video_stream == nullptr) {
1202 for (auto& packet : *largest_video_stream) {
1203 if (packet.header.markerBit) {
1204 int64_t capture_ms = packet.header.timestamp / 90.0;
1205 int64_t arrival_ms = packet.timestamp / 1000.0;
1206 timestamps.push_back(std::make_pair(capture_ms, arrival_ms));
1207 }
1208 }
1209 }
1210 return timestamps;
1211}
terelius54ce6802016-07-13 06:44:41 -07001212} // namespace plotting
1213} // namespace webrtc