blob: a45ec770f0c078a75d047300b5e197890c649cd5 [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);
philipelccd74892016-09-05 02:46:25 -0700558 time_series.style = LINE_GRAPH;
559
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;
563 time_series.points.emplace_back(x, i);
564 time_series.points.emplace_back(x, i + 1);
565 }
566
567 plot->series_list_.push_back(std::move(time_series));
568 }
569}
570
571void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
572 PacketDirection desired_direction,
573 Plot* plot) {
574 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
575 "RTP");
576 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
577 "RTCP");
578
579 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
580 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
581 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
582 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
583 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
584 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
585 }
586}
587
terelius54ce6802016-07-13 06:44:41 -0700588// For each SSRC, plot the time between the consecutive playouts.
589void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
590 std::map<uint32_t, TimeSeries> time_series;
591 std::map<uint32_t, uint64_t> last_playout;
592
593 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700594
595 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
596 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
597 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
598 parsed_log_.GetAudioPlayout(i, &ssrc);
599 uint64_t timestamp = parsed_log_.GetTimestamp(i);
600 if (MatchingSsrc(ssrc, desired_ssrc_)) {
601 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
602 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
603 if (time_series[ssrc].points.size() == 0) {
604 // There were no previusly logged playout for this SSRC.
605 // Generate a point, but place it on the x-axis.
606 y = 0;
607 }
terelius54ce6802016-07-13 06:44:41 -0700608 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
609 last_playout[ssrc] = timestamp;
610 }
611 }
612 }
613
614 // Set labels and put in graph.
615 for (auto& kv : time_series) {
616 kv.second.label = SsrcToString(kv.first);
617 kv.second.style = BAR_GRAPH;
tereliusdc35dcd2016-08-01 12:03:27 -0700618 plot->series_list_.push_back(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700619 }
620
tereliusdc35dcd2016-08-01 12:03:27 -0700621 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
622 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
623 kTopMargin);
624 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700625}
626
ivocaac9d6f2016-09-22 07:01:47 -0700627// For audio SSRCs, plot the audio level.
628void EventLogAnalyzer::CreateAudioLevelGraph(Plot* plot) {
629 std::map<StreamId, TimeSeries> time_series;
630
631 for (auto& kv : rtp_packets_) {
632 StreamId stream_id = kv.first;
633 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
634 // TODO(ivoc): When audio send/receive configs are stored in the event
635 // log, a check should be added here to only process audio
636 // streams. Tracking bug: webrtc:6399
637 for (auto& packet : packet_stream) {
638 if (packet.header.extension.hasAudioLevel) {
639 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
640 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
641 // Here we convert it to dBov.
642 float y = static_cast<float>(-packet.header.extension.audioLevel);
643 time_series[stream_id].points.emplace_back(TimeSeriesPoint(x, y));
644 }
645 }
646 }
647
648 for (auto& series : time_series) {
649 series.second.label = GetStreamName(series.first);
650 series.second.style = LINE_GRAPH;
651 plot->series_list_.push_back(std::move(series.second));
652 }
653
654 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
ivocbf676632016-11-24 08:30:34 -0800655 plot->SetYAxis(-127, 0, "Audio level (dBov)", kBottomMargin,
ivocaac9d6f2016-09-22 07:01:47 -0700656 kTopMargin);
657 plot->SetTitle("Audio level");
658}
659
terelius54ce6802016-07-13 06:44:41 -0700660// For each SSRC, plot the time between the consecutive playouts.
661void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700662 for (auto& kv : rtp_packets_) {
663 StreamId stream_id = kv.first;
664 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
665 // Filter on direction and SSRC.
666 if (stream_id.GetDirection() != kIncomingPacket ||
667 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
668 continue;
terelius54ce6802016-07-13 06:44:41 -0700669 }
terelius54ce6802016-07-13 06:44:41 -0700670
terelius6addf492016-08-23 17:34:07 -0700671 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200672 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700673 time_series.style = BAR_GRAPH;
674 Pairwise<SequenceNumberDiff>(packet_stream, begin_time_, &time_series);
675 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700676 }
677
tereliusdc35dcd2016-08-01 12:03:27 -0700678 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
679 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
680 kTopMargin);
681 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700682}
683
Stefan Holmer99f8e082016-09-09 13:37:50 +0200684void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
685 for (auto& kv : rtp_packets_) {
686 StreamId stream_id = kv.first;
687 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
688 // Filter on direction and SSRC.
689 if (stream_id.GetDirection() != kIncomingPacket ||
terelius4c9b4af2017-01-30 08:44:51 -0800690 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
691 packet_stream.size() == 0) {
Stefan Holmer99f8e082016-09-09 13:37:50 +0200692 continue;
693 }
694
695 TimeSeries time_series;
696 time_series.label = GetStreamName(stream_id);
697 time_series.style = LINE_DOT_GRAPH;
698 const uint64_t kWindowUs = 1000000;
terelius4c9b4af2017-01-30 08:44:51 -0800699 const uint64_t kStep = 1000000;
700 SequenceNumberUnwrapper unwrapper_;
701 SequenceNumberUnwrapper prior_unwrapper_;
702 size_t window_index_begin = 0;
703 size_t window_index_end = 0;
704 int64_t highest_seq_number =
705 unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
706 int64_t highest_prior_seq_number =
707 prior_unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
708
709 for (uint64_t t = begin_time_; t < end_time_ + kStep; t += kStep) {
710 while (window_index_end < packet_stream.size() &&
711 packet_stream[window_index_end].timestamp < t) {
712 int64_t sequence_number = unwrapper_.Unwrap(
713 packet_stream[window_index_end].header.sequenceNumber);
714 highest_seq_number = std::max(highest_seq_number, sequence_number);
715 ++window_index_end;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200716 }
terelius4c9b4af2017-01-30 08:44:51 -0800717 while (window_index_begin < packet_stream.size() &&
718 packet_stream[window_index_begin].timestamp < t - kWindowUs) {
719 int64_t sequence_number = prior_unwrapper_.Unwrap(
720 packet_stream[window_index_begin].header.sequenceNumber);
721 highest_prior_seq_number =
722 std::max(highest_prior_seq_number, sequence_number);
723 ++window_index_begin;
724 }
725 float x = static_cast<float>(t - begin_time_) / 1000000;
726 int64_t expected_packets = highest_seq_number - highest_prior_seq_number;
727 if (expected_packets > 0) {
728 int64_t received_packets = window_index_end - window_index_begin;
729 int64_t lost_packets = expected_packets - received_packets;
730 float y = static_cast<float>(lost_packets) / expected_packets * 100;
731 time_series.points.emplace_back(x, y);
732 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200733 }
734 plot->series_list_.push_back(std::move(time_series));
735 }
736
737 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
738 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
739 kTopMargin);
740 plot->SetTitle("Estimated incoming loss rate");
741}
742
terelius54ce6802016-07-13 06:44:41 -0700743void EventLogAnalyzer::CreateDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700744 for (auto& kv : rtp_packets_) {
745 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700746 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700747 // Filter on direction and SSRC.
748 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200749 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
750 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
751 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700752 continue;
753 }
terelius54ce6802016-07-13 06:44:41 -0700754
tereliusccbbf8d2016-08-10 07:34:28 -0700755 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200756 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700757 capture_time_data.style = BAR_GRAPH;
758 Pairwise<NetworkDelayDiff::CaptureTime>(packet_stream, begin_time_,
759 &capture_time_data);
760 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700761
tereliusccbbf8d2016-08-10 07:34:28 -0700762 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200763 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700764 send_time_data.style = BAR_GRAPH;
765 Pairwise<NetworkDelayDiff::AbsSendTime>(packet_stream, begin_time_,
766 &send_time_data);
767 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700768 }
769
tereliusdc35dcd2016-08-01 12:03:27 -0700770 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
771 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
772 kTopMargin);
773 plot->SetTitle("Network latency change between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700774}
775
776void EventLogAnalyzer::CreateAccumulatedDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700777 for (auto& kv : rtp_packets_) {
778 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700779 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700780 // Filter on direction and SSRC.
781 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200782 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
783 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
784 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700785 continue;
786 }
terelius54ce6802016-07-13 06:44:41 -0700787
tereliusccbbf8d2016-08-10 07:34:28 -0700788 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200789 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700790 capture_time_data.style = LINE_GRAPH;
791 Pairwise<Accumulated<NetworkDelayDiff::CaptureTime>>(
792 packet_stream, begin_time_, &capture_time_data);
793 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700794
tereliusccbbf8d2016-08-10 07:34:28 -0700795 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200796 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700797 send_time_data.style = LINE_GRAPH;
798 Pairwise<Accumulated<NetworkDelayDiff::AbsSendTime>>(
799 packet_stream, begin_time_, &send_time_data);
800 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700801 }
802
tereliusdc35dcd2016-08-01 12:03:27 -0700803 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
804 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
805 kTopMargin);
806 plot->SetTitle("Accumulated network latency change");
terelius54ce6802016-07-13 06:44:41 -0700807}
808
tereliusf736d232016-08-04 10:00:11 -0700809// Plot the fraction of packets lost (as perceived by the loss-based BWE).
810void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
811 plot->series_list_.push_back(TimeSeries());
812 for (auto& bwe_update : bwe_loss_updates_) {
813 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
814 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
815 plot->series_list_.back().points.emplace_back(x, y);
816 }
817 plot->series_list_.back().label = "Fraction lost";
818 plot->series_list_.back().style = LINE_DOT_GRAPH;
819
820 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
821 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
822 kTopMargin);
823 plot->SetTitle("Reported packet loss");
824}
825
terelius54ce6802016-07-13 06:44:41 -0700826// Plot the total bandwidth used by all RTP streams.
827void EventLogAnalyzer::CreateTotalBitrateGraph(
828 PacketDirection desired_direction,
829 Plot* plot) {
830 struct TimestampSize {
831 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
832 uint64_t timestamp;
833 size_t size;
834 };
835 std::vector<TimestampSize> packets;
836
837 PacketDirection direction;
838 size_t total_length;
839
840 // Extract timestamps and sizes for the relevant packets.
841 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
842 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
843 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
844 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, nullptr,
845 &total_length);
846 if (direction == desired_direction) {
847 uint64_t timestamp = parsed_log_.GetTimestamp(i);
848 packets.push_back(TimestampSize(timestamp, total_length));
849 }
850 }
851 }
852
853 size_t window_index_begin = 0;
854 size_t window_index_end = 0;
855 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700856
857 // Calculate a moving average of the bitrate and store in a TimeSeries.
tereliusdc35dcd2016-08-01 12:03:27 -0700858 plot->series_list_.push_back(TimeSeries());
terelius54ce6802016-07-13 06:44:41 -0700859 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
860 while (window_index_end < packets.size() &&
861 packets[window_index_end].timestamp < time) {
862 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700863 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700864 }
865 while (window_index_begin < packets.size() &&
866 packets[window_index_begin].timestamp < time - window_duration_) {
867 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
868 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700869 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700870 }
871 float window_duration_in_seconds =
872 static_cast<float>(window_duration_) / 1000000;
873 float x = static_cast<float>(time - begin_time_) / 1000000;
874 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700875 plot->series_list_.back().points.push_back(TimeSeriesPoint(x, y));
terelius54ce6802016-07-13 06:44:41 -0700876 }
877
878 // Set labels.
879 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700880 plot->series_list_.back().label = "Incoming bitrate";
terelius54ce6802016-07-13 06:44:41 -0700881 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700882 plot->series_list_.back().label = "Outgoing bitrate";
terelius54ce6802016-07-13 06:44:41 -0700883 }
tereliusdc35dcd2016-08-01 12:03:27 -0700884 plot->series_list_.back().style = LINE_GRAPH;
terelius54ce6802016-07-13 06:44:41 -0700885
terelius8058e582016-07-25 01:32:41 -0700886 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
887 if (desired_direction == kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700888 plot->series_list_.push_back(TimeSeries());
terelius8058e582016-07-25 01:32:41 -0700889 for (auto& bwe_update : bwe_loss_updates_) {
890 float x =
891 static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
892 float y = static_cast<float>(bwe_update.new_bitrate) / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700893 plot->series_list_.back().points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -0700894 }
tereliusdc35dcd2016-08-01 12:03:27 -0700895 plot->series_list_.back().label = "Loss-based estimate";
896 plot->series_list_.back().style = LINE_GRAPH;
terelius8058e582016-07-25 01:32:41 -0700897 }
tereliusdc35dcd2016-08-01 12:03:27 -0700898 plot->series_list_.back().style = LINE_GRAPH;
899 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
900 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700901 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700902 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700903 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700904 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700905 }
906}
907
908// For each SSRC, plot the bandwidth used by that stream.
909void EventLogAnalyzer::CreateStreamBitrateGraph(
910 PacketDirection desired_direction,
911 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700912 for (auto& kv : rtp_packets_) {
913 StreamId stream_id = kv.first;
914 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
915 // Filter on direction and SSRC.
916 if (stream_id.GetDirection() != desired_direction ||
917 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
918 continue;
terelius54ce6802016-07-13 06:44:41 -0700919 }
920
terelius6addf492016-08-23 17:34:07 -0700921 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200922 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700923 time_series.style = LINE_GRAPH;
924 double bytes_to_kilobits = 8.0 / 1000;
925 MovingAverage<PacketSizeBytes>(packet_stream, begin_time_, end_time_,
926 window_duration_, step_, bytes_to_kilobits,
927 &time_series);
928 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700929 }
930
tereliusdc35dcd2016-08-01 12:03:27 -0700931 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
932 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700933 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700934 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700935 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700936 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700937 }
938}
939
tereliuse34c19c2016-08-15 08:47:14 -0700940void EventLogAnalyzer::CreateBweSimulationGraph(Plot* plot) {
Stefan Holmer13181032016-07-29 14:48:54 +0200941 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
942 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
943
944 for (const auto& kv : rtp_packets_) {
945 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
946 for (const LoggedRtpPacket& rtp_packet : kv.second)
947 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
948 }
949 }
950
951 for (const auto& kv : rtcp_packets_) {
952 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
953 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
954 incoming_rtcp.insert(
955 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
956 }
957 }
958
959 SimulatedClock clock(0);
960 BitrateObserver observer;
961 RtcEventLogNullImpl null_event_log;
nisse0245da02016-11-30 03:35:20 -0800962 PacketRouter packet_router;
963 CongestionController cc(&clock, &observer, &observer, &null_event_log,
964 &packet_router);
Stefan Holmer13181032016-07-29 14:48:54 +0200965 // TODO(holmer): Log the call config and use that here instead.
966 static const uint32_t kDefaultStartBitrateBps = 300000;
967 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
968
969 TimeSeries time_series;
tereliuse34c19c2016-08-15 08:47:14 -0700970 time_series.label = "Delay-based estimate";
Stefan Holmer13181032016-07-29 14:48:54 +0200971 time_series.style = LINE_DOT_GRAPH;
Stefan Holmer60e43462016-09-07 09:58:20 +0200972 TimeSeries acked_time_series;
973 acked_time_series.label = "Acked bitrate";
974 acked_time_series.style = LINE_DOT_GRAPH;
Stefan Holmer13181032016-07-29 14:48:54 +0200975
976 auto rtp_iterator = outgoing_rtp.begin();
977 auto rtcp_iterator = incoming_rtcp.begin();
978
979 auto NextRtpTime = [&]() {
980 if (rtp_iterator != outgoing_rtp.end())
981 return static_cast<int64_t>(rtp_iterator->first);
982 return std::numeric_limits<int64_t>::max();
983 };
984
985 auto NextRtcpTime = [&]() {
986 if (rtcp_iterator != incoming_rtcp.end())
987 return static_cast<int64_t>(rtcp_iterator->first);
988 return std::numeric_limits<int64_t>::max();
989 };
990
991 auto NextProcessTime = [&]() {
992 if (rtcp_iterator != incoming_rtcp.end() ||
993 rtp_iterator != outgoing_rtp.end()) {
994 return clock.TimeInMicroseconds() +
995 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
996 }
997 return std::numeric_limits<int64_t>::max();
998 };
999
Stefan Holmer492ee282016-10-27 17:19:20 +02001000 RateStatistics acked_bitrate(250, 8000);
Stefan Holmer60e43462016-09-07 09:58:20 +02001001
Stefan Holmer13181032016-07-29 14:48:54 +02001002 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
Stefan Holmer492ee282016-10-27 17:19:20 +02001003 int64_t last_update_us = 0;
Stefan Holmer13181032016-07-29 14:48:54 +02001004 while (time_us != std::numeric_limits<int64_t>::max()) {
1005 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1006 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001007 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001008 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1009 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001010 TransportFeedbackObserver* observer = cc.GetTransportFeedbackObserver();
1011 observer->OnTransportFeedback(*static_cast<rtcp::TransportFeedback*>(
1012 rtcp.packet.get()));
1013 std::vector<PacketInfo> feedback =
1014 observer->GetTransportFeedbackVector();
1015 rtc::Optional<uint32_t> bitrate_bps;
1016 if (!feedback.empty()) {
1017 for (const PacketInfo& packet : feedback)
1018 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1019 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
1020 }
1021 uint32_t y = 0;
1022 if (bitrate_bps)
1023 y = *bitrate_bps / 1000;
1024 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1025 1000000;
1026 acked_time_series.points.emplace_back(x, y);
Stefan Holmer13181032016-07-29 14:48:54 +02001027 }
1028 ++rtcp_iterator;
1029 }
1030 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001031 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001032 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1033 if (rtp.header.extension.hasTransportSequenceNumber) {
1034 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1035 cc.GetTransportFeedbackObserver()->AddPacket(
stefana93d5ac2016-08-17 02:14:32 -07001036 rtp.header.extension.transportSequenceNumber, rtp.total_length,
1037 PacketInfo::kNotAProbe);
Stefan Holmer13181032016-07-29 14:48:54 +02001038 rtc::SentPacket sent_packet(
1039 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1040 cc.OnSentPacket(sent_packet);
1041 }
1042 ++rtp_iterator;
1043 }
stefanc3de0332016-08-02 07:22:17 -07001044 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1045 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001046 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001047 }
Stefan Holmer492ee282016-10-27 17:19:20 +02001048 if (observer.GetAndResetBitrateUpdated() ||
1049 time_us - last_update_us >= 1e6) {
Stefan Holmer13181032016-07-29 14:48:54 +02001050 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +02001051 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1052 1000000;
1053 time_series.points.emplace_back(x, y);
Stefan Holmer492ee282016-10-27 17:19:20 +02001054 last_update_us = time_us;
Stefan Holmer13181032016-07-29 14:48:54 +02001055 }
1056 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1057 }
1058 // Add the data set to the plot.
tereliusdc35dcd2016-08-01 12:03:27 -07001059 plot->series_list_.push_back(std::move(time_series));
Stefan Holmer60e43462016-09-07 09:58:20 +02001060 plot->series_list_.push_back(std::move(acked_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001061
tereliusdc35dcd2016-08-01 12:03:27 -07001062 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1063 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1064 plot->SetTitle("Simulated BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001065}
1066
Stefan Holmer280de9e2016-09-30 10:06:51 +02001067// TODO(holmer): Remove once TransportFeedbackAdapter no longer needs a
1068// BitrateController.
1069class NullBitrateController : public BitrateController {
1070 public:
1071 ~NullBitrateController() override {}
1072 RtcpBandwidthObserver* CreateRtcpBandwidthObserver() override {
1073 return nullptr;
1074 }
1075 void SetStartBitrate(int start_bitrate_bps) override {}
1076 void SetMinMaxBitrate(int min_bitrate_bps, int max_bitrate_bps) override {}
1077 void SetBitrates(int start_bitrate_bps,
1078 int min_bitrate_bps,
1079 int max_bitrate_bps) override {}
1080 void ResetBitrates(int bitrate_bps,
1081 int min_bitrate_bps,
1082 int max_bitrate_bps) override {}
1083 void OnDelayBasedBweResult(const DelayBasedBwe::Result& result) override {}
1084 bool AvailableBandwidth(uint32_t* bandwidth) const override { return false; }
1085 void SetReservedBitrate(uint32_t reserved_bitrate_bps) override {}
1086 bool GetNetworkParameters(uint32_t* bitrate,
1087 uint8_t* fraction_loss,
1088 int64_t* rtt) override {
1089 return false;
1090 }
1091 int64_t TimeUntilNextProcess() override { return 0; }
1092 void Process() override {}
1093};
1094
tereliuse34c19c2016-08-15 08:47:14 -07001095void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanc3de0332016-08-02 07:22:17 -07001096 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1097 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
1098
1099 for (const auto& kv : rtp_packets_) {
1100 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1101 for (const LoggedRtpPacket& rtp_packet : kv.second)
1102 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1103 }
1104 }
1105
1106 for (const auto& kv : rtcp_packets_) {
1107 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1108 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1109 incoming_rtcp.insert(
1110 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1111 }
1112 }
1113
1114 SimulatedClock clock(0);
Stefan Holmer280de9e2016-09-30 10:06:51 +02001115 NullBitrateController null_controller;
1116 TransportFeedbackAdapter feedback_adapter(&clock, &null_controller);
stefan41aab322016-10-10 08:16:30 -07001117 feedback_adapter.InitBwe();
stefanc3de0332016-08-02 07:22:17 -07001118
1119 TimeSeries time_series;
1120 time_series.label = "Network Delay Change";
1121 time_series.style = LINE_DOT_GRAPH;
1122 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1123
1124 auto rtp_iterator = outgoing_rtp.begin();
1125 auto rtcp_iterator = incoming_rtcp.begin();
1126
1127 auto NextRtpTime = [&]() {
1128 if (rtp_iterator != outgoing_rtp.end())
1129 return static_cast<int64_t>(rtp_iterator->first);
1130 return std::numeric_limits<int64_t>::max();
1131 };
1132
1133 auto NextRtcpTime = [&]() {
1134 if (rtcp_iterator != incoming_rtcp.end())
1135 return static_cast<int64_t>(rtcp_iterator->first);
1136 return std::numeric_limits<int64_t>::max();
1137 };
1138
1139 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
1140 while (time_us != std::numeric_limits<int64_t>::max()) {
1141 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1142 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1143 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1144 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1145 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001146 feedback_adapter.OnTransportFeedback(
1147 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
stefanc3de0332016-08-02 07:22:17 -07001148 std::vector<PacketInfo> feedback =
Stefan Holmer60e43462016-09-07 09:58:20 +02001149 feedback_adapter.GetTransportFeedbackVector();
stefanc3de0332016-08-02 07:22:17 -07001150 for (const PacketInfo& packet : feedback) {
1151 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1152 float x =
1153 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1154 1000000;
1155 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1156 time_series.points.emplace_back(x, y);
1157 }
1158 }
1159 ++rtcp_iterator;
1160 }
1161 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1162 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1163 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1164 if (rtp.header.extension.hasTransportSequenceNumber) {
1165 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1166 feedback_adapter.AddPacket(rtp.header.extension.transportSequenceNumber,
stefan985d2802016-11-15 06:54:09 -08001167 rtp.total_length, PacketInfo::kNotAProbe);
stefanc3de0332016-08-02 07:22:17 -07001168 feedback_adapter.OnSentPacket(
1169 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1170 }
1171 ++rtp_iterator;
1172 }
1173 time_us = std::min(NextRtpTime(), NextRtcpTime());
1174 }
1175 // We assume that the base network delay (w/o queues) is the min delay
1176 // observed during the call.
1177 for (TimeSeriesPoint& point : time_series.points)
1178 point.y -= estimated_base_delay_ms;
1179 // Add the data set to the plot.
1180 plot->series_list_.push_back(std::move(time_series));
1181
1182 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1183 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1184 plot->SetTitle("Network Delay Change.");
1185}
stefan08383272016-12-20 08:51:52 -08001186
1187std::vector<std::pair<int64_t, int64_t>> EventLogAnalyzer::GetFrameTimestamps()
1188 const {
1189 std::vector<std::pair<int64_t, int64_t>> timestamps;
1190 size_t largest_stream_size = 0;
1191 const std::vector<LoggedRtpPacket>* largest_video_stream = nullptr;
1192 // Find the incoming video stream with the most number of packets that is
1193 // not rtx.
1194 for (const auto& kv : rtp_packets_) {
1195 if (kv.first.GetDirection() == kIncomingPacket &&
1196 video_ssrcs_.find(kv.first) != video_ssrcs_.end() &&
1197 rtx_ssrcs_.find(kv.first) == rtx_ssrcs_.end() &&
1198 kv.second.size() > largest_stream_size) {
1199 largest_stream_size = kv.second.size();
1200 largest_video_stream = &kv.second;
1201 }
1202 }
1203 if (largest_video_stream == nullptr) {
1204 for (auto& packet : *largest_video_stream) {
1205 if (packet.header.markerBit) {
1206 int64_t capture_ms = packet.header.timestamp / 90.0;
1207 int64_t arrival_ms = packet.timestamp / 1000.0;
1208 timestamps.push_back(std::make_pair(capture_ms, arrival_ms));
1209 }
1210 }
1211 }
1212 return timestamps;
1213}
terelius54ce6802016-07-13 06:44:41 -07001214} // namespace plotting
1215} // namespace webrtc