blob: f98c5443d79b380593098a860d69615856e02f45 [file] [log] [blame]
terelius54ce6802016-07-13 06:44:41 -07001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/tools/event_log_visualizer/analyzer.h"
12
13#include <algorithm>
14#include <limits>
15#include <map>
16#include <sstream>
17#include <string>
18#include <utility>
19
kjellandera69d9732016-08-31 07:33:05 -070020#include "webrtc/api/call/audio_receive_stream.h"
21#include "webrtc/api/call/audio_send_stream.h"
terelius54ce6802016-07-13 06:44:41 -070022#include "webrtc/base/checks.h"
stefan6a850c32016-07-29 10:28:08 -070023#include "webrtc/base/logging.h"
Stefan Holmer60e43462016-09-07 09:58:20 +020024#include "webrtc/base/rate_statistics.h"
terelius54ce6802016-07-13 06:44:41 -070025#include "webrtc/call.h"
26#include "webrtc/common_types.h"
Stefan 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"
terelius54ce6802016-07-13 06:44:41 -070029#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
30#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
danilchap4aecc582016-11-15 09:21:00 -080031#include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h"
terelius54ce6802016-07-13 06:44:41 -070032#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
danilchapbf369fe2016-10-07 07:39:54 -070033#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
Stefan Holmer13181032016-07-29 14:48:54 +020034#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
terelius54ce6802016-07-13 06:44:41 -070035#include "webrtc/video_receive_stream.h"
36#include "webrtc/video_send_stream.h"
37
tereliusdc35dcd2016-08-01 12:03:27 -070038namespace webrtc {
39namespace plotting {
40
terelius54ce6802016-07-13 06:44:41 -070041namespace {
42
43std::string SsrcToString(uint32_t ssrc) {
44 std::stringstream ss;
45 ss << "SSRC " << ssrc;
46 return ss.str();
47}
48
49// Checks whether an SSRC is contained in the list of desired SSRCs.
50// Note that an empty SSRC list matches every SSRC.
51bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
52 if (desired_ssrc.size() == 0)
53 return true;
54 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
55 desired_ssrc.end();
56}
57
58double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
59 // The timestamp is a fixed point representation with 6 bits for seconds
60 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
61 // time in seconds and then multiply by 1000000 to convert to microseconds.
62 static constexpr double kTimestampToMicroSec =
tereliusccbbf8d2016-08-10 07:34:28 -070063 1000000.0 / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -070064 return abs_send_time * kTimestampToMicroSec;
65}
66
67// Computes the difference |later| - |earlier| where |later| and |earlier|
68// are counters that wrap at |modulus|. The difference is chosen to have the
69// least absolute value. For example if |modulus| is 8, then the difference will
70// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
71// be in [-4, 4].
72int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
73 RTC_DCHECK_LE(1, modulus);
74 RTC_DCHECK_LT(later, modulus);
75 RTC_DCHECK_LT(earlier, modulus);
76 int64_t difference =
77 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
78 int64_t max_difference = modulus / 2;
79 int64_t min_difference = max_difference - modulus + 1;
80 if (difference > max_difference) {
81 difference -= modulus;
82 }
83 if (difference < min_difference) {
84 difference += modulus;
85 }
terelius6addf492016-08-23 17:34:07 -070086 if (difference > max_difference / 2 || difference < min_difference / 2) {
87 LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
88 << " expected to be in the range (" << min_difference / 2
89 << "," << max_difference / 2 << ") but is " << difference
90 << ". Correct unwrapping is uncertain.";
91 }
terelius54ce6802016-07-13 06:44:41 -070092 return difference;
93}
94
ivocaac9d6f2016-09-22 07:01:47 -070095// Return default values for header extensions, to use on streams without stored
96// mapping data. Currently this only applies to audio streams, since the mapping
97// is not stored in the event log.
98// TODO(ivoc): Remove this once this mapping is stored in the event log for
99// audio streams. Tracking bug: webrtc:6399
100webrtc::RtpHeaderExtensionMap GetDefaultHeaderExtensionMap() {
101 webrtc::RtpHeaderExtensionMap default_map;
danilchap4aecc582016-11-15 09:21:00 -0800102 default_map.Register<AudioLevel>(webrtc::RtpExtension::kAudioLevelDefaultId);
103 default_map.Register<AbsoluteSendTime>(
ivocaac9d6f2016-09-22 07:01:47 -0700104 webrtc::RtpExtension::kAbsSendTimeDefaultId);
105 return default_map;
106}
107
tereliusdc35dcd2016-08-01 12:03:27 -0700108constexpr float kLeftMargin = 0.01f;
109constexpr float kRightMargin = 0.02f;
110constexpr float kBottomMargin = 0.02f;
111constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700112
terelius6addf492016-08-23 17:34:07 -0700113class PacketSizeBytes {
114 public:
115 using DataType = LoggedRtpPacket;
116 using ResultType = size_t;
117 size_t operator()(const LoggedRtpPacket& packet) {
118 return packet.total_length;
119 }
120};
121
122class SequenceNumberDiff {
123 public:
124 using DataType = LoggedRtpPacket;
125 using ResultType = int64_t;
126 int64_t operator()(const LoggedRtpPacket& old_packet,
127 const LoggedRtpPacket& new_packet) {
128 return WrappingDifference(new_packet.header.sequenceNumber,
129 old_packet.header.sequenceNumber, 1ul << 16);
130 }
131};
132
tereliusccbbf8d2016-08-10 07:34:28 -0700133class NetworkDelayDiff {
134 public:
135 class AbsSendTime {
136 public:
137 using DataType = LoggedRtpPacket;
138 using ResultType = double;
139 double operator()(const LoggedRtpPacket& old_packet,
140 const LoggedRtpPacket& new_packet) {
141 if (old_packet.header.extension.hasAbsoluteSendTime &&
142 new_packet.header.extension.hasAbsoluteSendTime) {
143 int64_t send_time_diff = WrappingDifference(
144 new_packet.header.extension.absoluteSendTime,
145 old_packet.header.extension.absoluteSendTime, 1ul << 24);
146 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
147 return static_cast<double>(recv_time_diff -
148 AbsSendTimeToMicroseconds(send_time_diff)) /
149 1000;
150 } else {
151 return 0;
152 }
153 }
154 };
155
156 class CaptureTime {
157 public:
158 using DataType = LoggedRtpPacket;
159 using ResultType = double;
160 double operator()(const LoggedRtpPacket& old_packet,
161 const LoggedRtpPacket& new_packet) {
162 int64_t send_time_diff = WrappingDifference(
163 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
164 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
165
166 const double kVideoSampleRate = 90000;
167 // TODO(terelius): We treat all streams as video for now, even though
168 // audio might be sampled at e.g. 16kHz, because it is really difficult to
169 // figure out the true sampling rate of a stream. The effect is that the
170 // delay will be scaled incorrectly for non-video streams.
171
172 double delay_change =
173 static_cast<double>(recv_time_diff) / 1000 -
174 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
terelius6addf492016-08-23 17:34:07 -0700175 if (delay_change < -10000 || 10000 < delay_change) {
176 LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
177 LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
178 << ", received time " << old_packet.timestamp;
179 LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
180 << ", received time " << new_packet.timestamp;
181 LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
182 << static_cast<double>(recv_time_diff) / 1000000 << "s";
183 LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
184 << static_cast<double>(send_time_diff) /
185 kVideoSampleRate
186 << "s";
187 }
tereliusccbbf8d2016-08-10 07:34:28 -0700188 return delay_change;
189 }
190 };
191};
192
193template <typename Extractor>
194class Accumulated {
195 public:
196 using DataType = typename Extractor::DataType;
197 using ResultType = typename Extractor::ResultType;
198 ResultType operator()(const DataType& old_packet,
199 const DataType& new_packet) {
200 sum += extract(old_packet, new_packet);
201 return sum;
202 }
203
204 private:
205 Extractor extract;
206 ResultType sum = 0;
207};
208
terelius6addf492016-08-23 17:34:07 -0700209// For each element in data, use |Extractor| to extract a y-coordinate and
210// store the result in a TimeSeries.
211template <typename Extractor>
212void Pointwise(const std::vector<typename Extractor::DataType>& data,
213 uint64_t begin_time,
214 TimeSeries* result) {
215 Extractor extract;
216 for (size_t i = 0; i < data.size(); i++) {
217 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
218 float y = extract(data[i]);
219 result->points.emplace_back(x, y);
220 }
221}
222
223// For each pair of adjacent elements in |data|, use |Extractor| to extract a
224// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
225// will be the time of the second element in the pair.
tereliusccbbf8d2016-08-10 07:34:28 -0700226template <typename Extractor>
227void Pairwise(const std::vector<typename Extractor::DataType>& data,
228 uint64_t begin_time,
229 TimeSeries* result) {
230 Extractor extract;
231 for (size_t i = 1; i < data.size(); i++) {
232 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
233 float y = extract(data[i - 1], data[i]);
234 result->points.emplace_back(x, y);
235 }
236}
237
terelius6addf492016-08-23 17:34:07 -0700238// Calculates a moving average of |data| and stores the result in a TimeSeries.
239// A data point is generated every |step| microseconds from |begin_time|
240// to |end_time|. The value of each data point is the average of the data
241// during the preceeding |window_duration_us| microseconds.
242template <typename Extractor>
243void MovingAverage(const std::vector<typename Extractor::DataType>& data,
244 uint64_t begin_time,
245 uint64_t end_time,
246 uint64_t window_duration_us,
247 uint64_t step,
248 float y_scaling,
249 webrtc::plotting::TimeSeries* result) {
250 size_t window_index_begin = 0;
251 size_t window_index_end = 0;
252 typename Extractor::ResultType sum_in_window = 0;
253 Extractor extract;
254
255 for (uint64_t t = begin_time; t < end_time + step; t += step) {
256 while (window_index_end < data.size() &&
257 data[window_index_end].timestamp < t) {
258 sum_in_window += extract(data[window_index_end]);
259 ++window_index_end;
260 }
261 while (window_index_begin < data.size() &&
262 data[window_index_begin].timestamp < t - window_duration_us) {
263 sum_in_window -= extract(data[window_index_begin]);
264 ++window_index_begin;
265 }
266 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
267 float x = static_cast<float>(t - begin_time) / 1000000;
268 float y = sum_in_window / window_duration_s * y_scaling;
269 result->points.emplace_back(x, y);
270 }
271}
272
terelius54ce6802016-07-13 06:44:41 -0700273} // namespace
274
terelius54ce6802016-07-13 06:44:41 -0700275EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
276 : parsed_log_(log), window_duration_(250000), step_(10000) {
277 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
278 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700279
Stefan Holmer13181032016-07-29 14:48:54 +0200280 // Maps a stream identifier consisting of ssrc and direction
terelius88e64e52016-07-19 01:51:06 -0700281 // to the header extensions used by that stream,
282 std::map<StreamId, RtpHeaderExtensionMap> extension_maps;
283
284 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700285 uint8_t header[IP_PACKET_SIZE];
286 size_t header_length;
287 size_t total_length;
288
ivocaac9d6f2016-09-22 07:01:47 -0700289 // Make a default extension map for streams without configuration information.
290 // TODO(ivoc): Once configuration of audio streams is stored in the event log,
291 // this can be removed. Tracking bug: webrtc:6399
292 RtpHeaderExtensionMap default_extension_map = GetDefaultHeaderExtensionMap();
293
terelius54ce6802016-07-13 06:44:41 -0700294 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
295 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700296 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
297 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
298 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700299 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
300 event_type != ParsedRtcEventLog::LOG_START &&
301 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700302 uint64_t timestamp = parsed_log_.GetTimestamp(i);
303 first_timestamp = std::min(first_timestamp, timestamp);
304 last_timestamp = std::max(last_timestamp, timestamp);
305 }
306
307 switch (parsed_log_.GetEventType(i)) {
308 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
309 VideoReceiveStream::Config config(nullptr);
310 parsed_log_.GetVideoReceiveConfig(i, &config);
Stefan Holmer13181032016-07-29 14:48:54 +0200311 StreamId stream(config.rtp.remote_ssrc, kIncomingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800312 extension_maps[stream] = RtpHeaderExtensionMap(config.rtp.extensions);
terelius0740a202016-08-08 10:21:04 -0700313 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700314 for (auto kv : config.rtp.rtx) {
315 StreamId rtx_stream(kv.second.ssrc, kIncomingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800316 extension_maps[rtx_stream] =
317 RtpHeaderExtensionMap(config.rtp.extensions);
terelius0740a202016-08-08 10:21:04 -0700318 video_ssrcs_.insert(rtx_stream);
319 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700320 }
321 break;
322 }
323 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
324 VideoSendStream::Config config(nullptr);
325 parsed_log_.GetVideoSendConfig(i, &config);
326 for (auto ssrc : config.rtp.ssrcs) {
Stefan Holmer13181032016-07-29 14:48:54 +0200327 StreamId stream(ssrc, kOutgoingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800328 extension_maps[stream] = RtpHeaderExtensionMap(config.rtp.extensions);
terelius0740a202016-08-08 10:21:04 -0700329 video_ssrcs_.insert(stream);
stefan6a850c32016-07-29 10:28:08 -0700330 }
331 for (auto ssrc : config.rtp.rtx.ssrcs) {
terelius0740a202016-08-08 10:21:04 -0700332 StreamId rtx_stream(ssrc, kOutgoingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800333 extension_maps[rtx_stream] =
334 RtpHeaderExtensionMap(config.rtp.extensions);
terelius0740a202016-08-08 10:21:04 -0700335 video_ssrcs_.insert(rtx_stream);
336 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700337 }
338 break;
339 }
340 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
341 AudioReceiveStream::Config config;
ivoce0928d82016-10-10 05:12:51 -0700342 parsed_log_.GetAudioReceiveConfig(i, &config);
343 StreamId stream(config.rtp.remote_ssrc, kIncomingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800344 extension_maps[stream] = RtpHeaderExtensionMap(config.rtp.extensions);
ivoce0928d82016-10-10 05:12:51 -0700345 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700346 break;
347 }
348 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
349 AudioSendStream::Config config(nullptr);
ivoce0928d82016-10-10 05:12:51 -0700350 parsed_log_.GetAudioSendConfig(i, &config);
351 StreamId stream(config.rtp.ssrc, kOutgoingPacket);
danilchap4aecc582016-11-15 09:21:00 -0800352 extension_maps[stream] = RtpHeaderExtensionMap(config.rtp.extensions);
ivoce0928d82016-10-10 05:12:51 -0700353 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700354 break;
355 }
356 case ParsedRtcEventLog::RTP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200357 MediaType media_type;
terelius88e64e52016-07-19 01:51:06 -0700358 parsed_log_.GetRtpHeader(i, &direction, &media_type, header,
359 &header_length, &total_length);
360 // Parse header to get SSRC.
361 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
362 RTPHeader parsed_header;
363 rtp_parser.Parse(&parsed_header);
Stefan Holmer13181032016-07-29 14:48:54 +0200364 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700365 // Look up the extension_map and parse it again to get the extensions.
366 if (extension_maps.count(stream) == 1) {
367 RtpHeaderExtensionMap* extension_map = &extension_maps[stream];
368 rtp_parser.Parse(&parsed_header, extension_map);
ivocaac9d6f2016-09-22 07:01:47 -0700369 } else {
370 // Use the default extension map.
371 // TODO(ivoc): Once configuration of audio streams is stored in the
372 // event log, this can be removed.
373 // Tracking bug: webrtc:6399
374 rtp_parser.Parse(&parsed_header, &default_extension_map);
terelius88e64e52016-07-19 01:51:06 -0700375 }
376 uint64_t timestamp = parsed_log_.GetTimestamp(i);
377 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200378 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700379 break;
380 }
381 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200382 uint8_t packet[IP_PACKET_SIZE];
383 MediaType media_type;
384 parsed_log_.GetRtcpPacket(i, &direction, &media_type, packet,
385 &total_length);
386
danilchapbf369fe2016-10-07 07:39:54 -0700387 // Currently feedback is logged twice, both for audio and video.
388 // Only act on one of them.
389 if (media_type == MediaType::VIDEO) {
390 rtcp::CommonHeader header;
391 const uint8_t* packet_end = packet + total_length;
392 for (const uint8_t* block = packet; block < packet_end;
393 block = header.NextPacket()) {
394 RTC_CHECK(header.Parse(block, packet_end - block));
395 if (header.type() == rtcp::TransportFeedback::kPacketType &&
396 header.fmt() == rtcp::TransportFeedback::kFeedbackMessageType) {
397 std::unique_ptr<rtcp::TransportFeedback> rtcp_packet(
398 new rtcp::TransportFeedback());
399 if (rtcp_packet->Parse(header)) {
400 uint32_t ssrc = rtcp_packet->sender_ssrc();
Stefan Holmer13181032016-07-29 14:48:54 +0200401 StreamId stream(ssrc, direction);
402 uint64_t timestamp = parsed_log_.GetTimestamp(i);
403 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
404 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
405 }
Stefan Holmer13181032016-07-29 14:48:54 +0200406 }
Stefan Holmer13181032016-07-29 14:48:54 +0200407 }
Stefan Holmer13181032016-07-29 14:48:54 +0200408 }
terelius88e64e52016-07-19 01:51:06 -0700409 break;
410 }
411 case ParsedRtcEventLog::LOG_START: {
412 break;
413 }
414 case ParsedRtcEventLog::LOG_END: {
415 break;
416 }
417 case ParsedRtcEventLog::BWE_PACKET_LOSS_EVENT: {
terelius8058e582016-07-25 01:32:41 -0700418 BwePacketLossEvent bwe_update;
419 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
420 parsed_log_.GetBwePacketLossEvent(i, &bwe_update.new_bitrate,
421 &bwe_update.fraction_loss,
422 &bwe_update.expected_packets);
423 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700424 break;
425 }
426 case ParsedRtcEventLog::BWE_PACKET_DELAY_EVENT: {
427 break;
428 }
429 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
430 break;
431 }
432 case ParsedRtcEventLog::UNKNOWN_EVENT: {
433 break;
434 }
435 }
terelius54ce6802016-07-13 06:44:41 -0700436 }
terelius88e64e52016-07-19 01:51:06 -0700437
terelius54ce6802016-07-13 06:44:41 -0700438 if (last_timestamp < first_timestamp) {
439 // No useful events in the log.
440 first_timestamp = last_timestamp = 0;
441 }
442 begin_time_ = first_timestamp;
443 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700444 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
terelius54ce6802016-07-13 06:44:41 -0700445}
446
Stefan Holmer13181032016-07-29 14:48:54 +0200447class BitrateObserver : public CongestionController::Observer,
448 public RemoteBitrateObserver {
449 public:
450 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
451
452 void OnNetworkChanged(uint32_t bitrate_bps,
453 uint8_t fraction_loss,
ossu6287e822016-11-28 08:05:16 -0800454 int64_t rtt_ms) override {
Stefan Holmer13181032016-07-29 14:48:54 +0200455 last_bitrate_bps_ = bitrate_bps;
456 bitrate_updated_ = true;
457 }
458
459 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
460 uint32_t bitrate) override {}
461
462 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
463 bool GetAndResetBitrateUpdated() {
464 bool bitrate_updated = bitrate_updated_;
465 bitrate_updated_ = false;
466 return bitrate_updated;
467 }
468
469 private:
470 uint32_t last_bitrate_bps_;
471 bool bitrate_updated_;
472};
473
Stefan Holmer99f8e082016-09-09 13:37:50 +0200474bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700475 return rtx_ssrcs_.count(stream_id) == 1;
476}
477
Stefan Holmer99f8e082016-09-09 13:37:50 +0200478bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700479 return video_ssrcs_.count(stream_id) == 1;
480}
481
Stefan Holmer99f8e082016-09-09 13:37:50 +0200482bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700483 return audio_ssrcs_.count(stream_id) == 1;
484}
485
Stefan Holmer99f8e082016-09-09 13:37:50 +0200486std::string EventLogAnalyzer::GetStreamName(StreamId stream_id) const {
487 std::stringstream name;
488 if (IsAudioSsrc(stream_id)) {
489 name << "Audio ";
490 } else if (IsVideoSsrc(stream_id)) {
491 name << "Video ";
492 } else {
493 name << "Unknown ";
494 }
495 if (IsRtxSsrc(stream_id))
496 name << "RTX ";
ivocaac9d6f2016-09-22 07:01:47 -0700497 if (stream_id.GetDirection() == kIncomingPacket) {
498 name << "(In) ";
499 } else {
500 name << "(Out) ";
501 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200502 name << SsrcToString(stream_id.GetSsrc());
503 return name.str();
504}
505
terelius54ce6802016-07-13 06:44:41 -0700506void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
507 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700508 for (auto& kv : rtp_packets_) {
509 StreamId stream_id = kv.first;
510 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
511 // Filter on direction and SSRC.
512 if (stream_id.GetDirection() != desired_direction ||
513 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
514 continue;
terelius54ce6802016-07-13 06:44:41 -0700515 }
terelius54ce6802016-07-13 06:44:41 -0700516
terelius6addf492016-08-23 17:34:07 -0700517 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200518 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700519 time_series.style = BAR_GRAPH;
520 Pointwise<PacketSizeBytes>(packet_stream, begin_time_, &time_series);
521 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700522 }
523
tereliusdc35dcd2016-08-01 12:03:27 -0700524 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
525 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
526 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700527 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700528 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700529 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700530 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700531 }
532}
533
philipelccd74892016-09-05 02:46:25 -0700534template <typename T>
535void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
536 PacketDirection desired_direction,
537 Plot* plot,
538 const std::map<StreamId, std::vector<T>>& packets,
539 const std::string& label_prefix) {
540 for (auto& kv : packets) {
541 StreamId stream_id = kv.first;
542 const std::vector<T>& packet_stream = kv.second;
543 // Filter on direction and SSRC.
544 if (stream_id.GetDirection() != desired_direction ||
545 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
546 continue;
547 }
548
549 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200550 time_series.label = label_prefix + " " + GetStreamName(stream_id);
philipelccd74892016-09-05 02:46:25 -0700551 time_series.style = LINE_GRAPH;
552
553 for (size_t i = 0; i < packet_stream.size(); i++) {
554 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
555 1000000;
556 time_series.points.emplace_back(x, i);
557 time_series.points.emplace_back(x, i + 1);
558 }
559
560 plot->series_list_.push_back(std::move(time_series));
561 }
562}
563
564void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
565 PacketDirection desired_direction,
566 Plot* plot) {
567 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
568 "RTP");
569 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
570 "RTCP");
571
572 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
573 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
574 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
575 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
576 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
577 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
578 }
579}
580
terelius54ce6802016-07-13 06:44:41 -0700581// For each SSRC, plot the time between the consecutive playouts.
582void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
583 std::map<uint32_t, TimeSeries> time_series;
584 std::map<uint32_t, uint64_t> last_playout;
585
586 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700587
588 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
589 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
590 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
591 parsed_log_.GetAudioPlayout(i, &ssrc);
592 uint64_t timestamp = parsed_log_.GetTimestamp(i);
593 if (MatchingSsrc(ssrc, desired_ssrc_)) {
594 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
595 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
596 if (time_series[ssrc].points.size() == 0) {
597 // There were no previusly logged playout for this SSRC.
598 // Generate a point, but place it on the x-axis.
599 y = 0;
600 }
terelius54ce6802016-07-13 06:44:41 -0700601 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
602 last_playout[ssrc] = timestamp;
603 }
604 }
605 }
606
607 // Set labels and put in graph.
608 for (auto& kv : time_series) {
609 kv.second.label = SsrcToString(kv.first);
610 kv.second.style = BAR_GRAPH;
tereliusdc35dcd2016-08-01 12:03:27 -0700611 plot->series_list_.push_back(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700612 }
613
tereliusdc35dcd2016-08-01 12:03:27 -0700614 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
615 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
616 kTopMargin);
617 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700618}
619
ivocaac9d6f2016-09-22 07:01:47 -0700620// For audio SSRCs, plot the audio level.
621void EventLogAnalyzer::CreateAudioLevelGraph(Plot* plot) {
622 std::map<StreamId, TimeSeries> time_series;
623
624 for (auto& kv : rtp_packets_) {
625 StreamId stream_id = kv.first;
626 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
627 // TODO(ivoc): When audio send/receive configs are stored in the event
628 // log, a check should be added here to only process audio
629 // streams. Tracking bug: webrtc:6399
630 for (auto& packet : packet_stream) {
631 if (packet.header.extension.hasAudioLevel) {
632 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
633 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
634 // Here we convert it to dBov.
635 float y = static_cast<float>(-packet.header.extension.audioLevel);
636 time_series[stream_id].points.emplace_back(TimeSeriesPoint(x, y));
637 }
638 }
639 }
640
641 for (auto& series : time_series) {
642 series.second.label = GetStreamName(series.first);
643 series.second.style = LINE_GRAPH;
644 plot->series_list_.push_back(std::move(series.second));
645 }
646
647 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
ivocbf676632016-11-24 08:30:34 -0800648 plot->SetYAxis(-127, 0, "Audio level (dBov)", kBottomMargin,
ivocaac9d6f2016-09-22 07:01:47 -0700649 kTopMargin);
650 plot->SetTitle("Audio level");
651}
652
terelius54ce6802016-07-13 06:44:41 -0700653// For each SSRC, plot the time between the consecutive playouts.
654void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700655 for (auto& kv : rtp_packets_) {
656 StreamId stream_id = kv.first;
657 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
658 // Filter on direction and SSRC.
659 if (stream_id.GetDirection() != kIncomingPacket ||
660 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
661 continue;
terelius54ce6802016-07-13 06:44:41 -0700662 }
terelius54ce6802016-07-13 06:44:41 -0700663
terelius6addf492016-08-23 17:34:07 -0700664 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200665 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700666 time_series.style = BAR_GRAPH;
667 Pairwise<SequenceNumberDiff>(packet_stream, begin_time_, &time_series);
668 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700669 }
670
tereliusdc35dcd2016-08-01 12:03:27 -0700671 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
672 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
673 kTopMargin);
674 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700675}
676
Stefan Holmer99f8e082016-09-09 13:37:50 +0200677void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
678 for (auto& kv : rtp_packets_) {
679 StreamId stream_id = kv.first;
680 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
681 // Filter on direction and SSRC.
682 if (stream_id.GetDirection() != kIncomingPacket ||
683 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
684 continue;
685 }
686
687 TimeSeries time_series;
688 time_series.label = GetStreamName(stream_id);
689 time_series.style = LINE_DOT_GRAPH;
690 const uint64_t kWindowUs = 1000000;
691 const LoggedRtpPacket* first_in_window = &packet_stream.front();
692 const LoggedRtpPacket* last_in_window = &packet_stream.front();
693 int packets_in_window = 0;
694 for (const LoggedRtpPacket& packet : packet_stream) {
695 if (packet.timestamp > first_in_window->timestamp + kWindowUs) {
696 uint16_t expected_num_packets = last_in_window->header.sequenceNumber -
697 first_in_window->header.sequenceNumber + 1;
698 float fraction_lost = (expected_num_packets - packets_in_window) /
699 static_cast<float>(expected_num_packets);
700 float y = fraction_lost * 100;
701 float x =
702 static_cast<float>(last_in_window->timestamp - begin_time_) /
703 1000000;
704 time_series.points.emplace_back(x, y);
705 first_in_window = &packet;
706 last_in_window = &packet;
707 packets_in_window = 1;
708 continue;
709 }
710 ++packets_in_window;
711 last_in_window = &packet;
712 }
713 plot->series_list_.push_back(std::move(time_series));
714 }
715
716 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
717 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
718 kTopMargin);
719 plot->SetTitle("Estimated incoming loss rate");
720}
721
terelius54ce6802016-07-13 06:44:41 -0700722void EventLogAnalyzer::CreateDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700723 for (auto& kv : rtp_packets_) {
724 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700725 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700726 // Filter on direction and SSRC.
727 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200728 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
729 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
730 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700731 continue;
732 }
terelius54ce6802016-07-13 06:44:41 -0700733
tereliusccbbf8d2016-08-10 07:34:28 -0700734 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200735 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700736 capture_time_data.style = BAR_GRAPH;
737 Pairwise<NetworkDelayDiff::CaptureTime>(packet_stream, begin_time_,
738 &capture_time_data);
739 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700740
tereliusccbbf8d2016-08-10 07:34:28 -0700741 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200742 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700743 send_time_data.style = BAR_GRAPH;
744 Pairwise<NetworkDelayDiff::AbsSendTime>(packet_stream, begin_time_,
745 &send_time_data);
746 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700747 }
748
tereliusdc35dcd2016-08-01 12:03:27 -0700749 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
750 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
751 kTopMargin);
752 plot->SetTitle("Network latency change between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700753}
754
755void EventLogAnalyzer::CreateAccumulatedDelayChangeGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700756 for (auto& kv : rtp_packets_) {
757 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700758 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700759 // Filter on direction and SSRC.
760 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200761 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
762 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
763 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700764 continue;
765 }
terelius54ce6802016-07-13 06:44:41 -0700766
tereliusccbbf8d2016-08-10 07:34:28 -0700767 TimeSeries capture_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200768 capture_time_data.label = GetStreamName(stream_id) + " capture-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700769 capture_time_data.style = LINE_GRAPH;
770 Pairwise<Accumulated<NetworkDelayDiff::CaptureTime>>(
771 packet_stream, begin_time_, &capture_time_data);
772 plot->series_list_.push_back(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700773
tereliusccbbf8d2016-08-10 07:34:28 -0700774 TimeSeries send_time_data;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200775 send_time_data.label = GetStreamName(stream_id) + " abs-send-time";
tereliusccbbf8d2016-08-10 07:34:28 -0700776 send_time_data.style = LINE_GRAPH;
777 Pairwise<Accumulated<NetworkDelayDiff::AbsSendTime>>(
778 packet_stream, begin_time_, &send_time_data);
779 plot->series_list_.push_back(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700780 }
781
tereliusdc35dcd2016-08-01 12:03:27 -0700782 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
783 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
784 kTopMargin);
785 plot->SetTitle("Accumulated network latency change");
terelius54ce6802016-07-13 06:44:41 -0700786}
787
tereliusf736d232016-08-04 10:00:11 -0700788// Plot the fraction of packets lost (as perceived by the loss-based BWE).
789void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
790 plot->series_list_.push_back(TimeSeries());
791 for (auto& bwe_update : bwe_loss_updates_) {
792 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
793 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
794 plot->series_list_.back().points.emplace_back(x, y);
795 }
796 plot->series_list_.back().label = "Fraction lost";
797 plot->series_list_.back().style = LINE_DOT_GRAPH;
798
799 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
800 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
801 kTopMargin);
802 plot->SetTitle("Reported packet loss");
803}
804
terelius54ce6802016-07-13 06:44:41 -0700805// Plot the total bandwidth used by all RTP streams.
806void EventLogAnalyzer::CreateTotalBitrateGraph(
807 PacketDirection desired_direction,
808 Plot* plot) {
809 struct TimestampSize {
810 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
811 uint64_t timestamp;
812 size_t size;
813 };
814 std::vector<TimestampSize> packets;
815
816 PacketDirection direction;
817 size_t total_length;
818
819 // Extract timestamps and sizes for the relevant packets.
820 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
821 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
822 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
823 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, nullptr,
824 &total_length);
825 if (direction == desired_direction) {
826 uint64_t timestamp = parsed_log_.GetTimestamp(i);
827 packets.push_back(TimestampSize(timestamp, total_length));
828 }
829 }
830 }
831
832 size_t window_index_begin = 0;
833 size_t window_index_end = 0;
834 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700835
836 // Calculate a moving average of the bitrate and store in a TimeSeries.
tereliusdc35dcd2016-08-01 12:03:27 -0700837 plot->series_list_.push_back(TimeSeries());
terelius54ce6802016-07-13 06:44:41 -0700838 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
839 while (window_index_end < packets.size() &&
840 packets[window_index_end].timestamp < time) {
841 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700842 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700843 }
844 while (window_index_begin < packets.size() &&
845 packets[window_index_begin].timestamp < time - window_duration_) {
846 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
847 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700848 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700849 }
850 float window_duration_in_seconds =
851 static_cast<float>(window_duration_) / 1000000;
852 float x = static_cast<float>(time - begin_time_) / 1000000;
853 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700854 plot->series_list_.back().points.push_back(TimeSeriesPoint(x, y));
terelius54ce6802016-07-13 06:44:41 -0700855 }
856
857 // Set labels.
858 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700859 plot->series_list_.back().label = "Incoming bitrate";
terelius54ce6802016-07-13 06:44:41 -0700860 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700861 plot->series_list_.back().label = "Outgoing bitrate";
terelius54ce6802016-07-13 06:44:41 -0700862 }
tereliusdc35dcd2016-08-01 12:03:27 -0700863 plot->series_list_.back().style = LINE_GRAPH;
terelius54ce6802016-07-13 06:44:41 -0700864
terelius8058e582016-07-25 01:32:41 -0700865 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
866 if (desired_direction == kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700867 plot->series_list_.push_back(TimeSeries());
terelius8058e582016-07-25 01:32:41 -0700868 for (auto& bwe_update : bwe_loss_updates_) {
869 float x =
870 static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
871 float y = static_cast<float>(bwe_update.new_bitrate) / 1000;
tereliusdc35dcd2016-08-01 12:03:27 -0700872 plot->series_list_.back().points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -0700873 }
tereliusdc35dcd2016-08-01 12:03:27 -0700874 plot->series_list_.back().label = "Loss-based estimate";
875 plot->series_list_.back().style = LINE_GRAPH;
terelius8058e582016-07-25 01:32:41 -0700876 }
tereliusdc35dcd2016-08-01 12:03:27 -0700877 plot->series_list_.back().style = LINE_GRAPH;
878 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
879 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700880 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700881 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700882 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700883 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -0700884 }
885}
886
887// For each SSRC, plot the bandwidth used by that stream.
888void EventLogAnalyzer::CreateStreamBitrateGraph(
889 PacketDirection desired_direction,
890 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700891 for (auto& kv : rtp_packets_) {
892 StreamId stream_id = kv.first;
893 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
894 // Filter on direction and SSRC.
895 if (stream_id.GetDirection() != desired_direction ||
896 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
897 continue;
terelius54ce6802016-07-13 06:44:41 -0700898 }
899
terelius6addf492016-08-23 17:34:07 -0700900 TimeSeries time_series;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200901 time_series.label = GetStreamName(stream_id);
terelius6addf492016-08-23 17:34:07 -0700902 time_series.style = LINE_GRAPH;
903 double bytes_to_kilobits = 8.0 / 1000;
904 MovingAverage<PacketSizeBytes>(packet_stream, begin_time_, end_time_,
905 window_duration_, step_, bytes_to_kilobits,
906 &time_series);
907 plot->series_list_.push_back(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700908 }
909
tereliusdc35dcd2016-08-01 12:03:27 -0700910 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
911 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700912 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700913 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700914 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700915 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -0700916 }
917}
918
tereliuse34c19c2016-08-15 08:47:14 -0700919void EventLogAnalyzer::CreateBweSimulationGraph(Plot* plot) {
Stefan Holmer13181032016-07-29 14:48:54 +0200920 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
921 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
922
923 for (const auto& kv : rtp_packets_) {
924 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
925 for (const LoggedRtpPacket& rtp_packet : kv.second)
926 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
927 }
928 }
929
930 for (const auto& kv : rtcp_packets_) {
931 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
932 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
933 incoming_rtcp.insert(
934 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
935 }
936 }
937
938 SimulatedClock clock(0);
939 BitrateObserver observer;
940 RtcEventLogNullImpl null_event_log;
nisse0245da02016-11-30 03:35:20 -0800941 PacketRouter packet_router;
942 CongestionController cc(&clock, &observer, &observer, &null_event_log,
943 &packet_router);
Stefan Holmer13181032016-07-29 14:48:54 +0200944 // TODO(holmer): Log the call config and use that here instead.
945 static const uint32_t kDefaultStartBitrateBps = 300000;
946 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
947
948 TimeSeries time_series;
tereliuse34c19c2016-08-15 08:47:14 -0700949 time_series.label = "Delay-based estimate";
Stefan Holmer13181032016-07-29 14:48:54 +0200950 time_series.style = LINE_DOT_GRAPH;
Stefan Holmer60e43462016-09-07 09:58:20 +0200951 TimeSeries acked_time_series;
952 acked_time_series.label = "Acked bitrate";
953 acked_time_series.style = LINE_DOT_GRAPH;
Stefan Holmer13181032016-07-29 14:48:54 +0200954
955 auto rtp_iterator = outgoing_rtp.begin();
956 auto rtcp_iterator = incoming_rtcp.begin();
957
958 auto NextRtpTime = [&]() {
959 if (rtp_iterator != outgoing_rtp.end())
960 return static_cast<int64_t>(rtp_iterator->first);
961 return std::numeric_limits<int64_t>::max();
962 };
963
964 auto NextRtcpTime = [&]() {
965 if (rtcp_iterator != incoming_rtcp.end())
966 return static_cast<int64_t>(rtcp_iterator->first);
967 return std::numeric_limits<int64_t>::max();
968 };
969
970 auto NextProcessTime = [&]() {
971 if (rtcp_iterator != incoming_rtcp.end() ||
972 rtp_iterator != outgoing_rtp.end()) {
973 return clock.TimeInMicroseconds() +
974 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
975 }
976 return std::numeric_limits<int64_t>::max();
977 };
978
Stefan Holmer492ee282016-10-27 17:19:20 +0200979 RateStatistics acked_bitrate(250, 8000);
Stefan Holmer60e43462016-09-07 09:58:20 +0200980
Stefan Holmer13181032016-07-29 14:48:54 +0200981 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
Stefan Holmer492ee282016-10-27 17:19:20 +0200982 int64_t last_update_us = 0;
Stefan Holmer13181032016-07-29 14:48:54 +0200983 while (time_us != std::numeric_limits<int64_t>::max()) {
984 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
985 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -0700986 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +0200987 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
988 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +0200989 TransportFeedbackObserver* observer = cc.GetTransportFeedbackObserver();
990 observer->OnTransportFeedback(*static_cast<rtcp::TransportFeedback*>(
991 rtcp.packet.get()));
992 std::vector<PacketInfo> feedback =
993 observer->GetTransportFeedbackVector();
994 rtc::Optional<uint32_t> bitrate_bps;
995 if (!feedback.empty()) {
996 for (const PacketInfo& packet : feedback)
997 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
998 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
999 }
1000 uint32_t y = 0;
1001 if (bitrate_bps)
1002 y = *bitrate_bps / 1000;
1003 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1004 1000000;
1005 acked_time_series.points.emplace_back(x, y);
Stefan Holmer13181032016-07-29 14:48:54 +02001006 }
1007 ++rtcp_iterator;
1008 }
1009 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001010 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001011 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1012 if (rtp.header.extension.hasTransportSequenceNumber) {
1013 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1014 cc.GetTransportFeedbackObserver()->AddPacket(
stefana93d5ac2016-08-17 02:14:32 -07001015 rtp.header.extension.transportSequenceNumber, rtp.total_length,
1016 PacketInfo::kNotAProbe);
Stefan Holmer13181032016-07-29 14:48:54 +02001017 rtc::SentPacket sent_packet(
1018 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1019 cc.OnSentPacket(sent_packet);
1020 }
1021 ++rtp_iterator;
1022 }
stefanc3de0332016-08-02 07:22:17 -07001023 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1024 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001025 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001026 }
Stefan Holmer492ee282016-10-27 17:19:20 +02001027 if (observer.GetAndResetBitrateUpdated() ||
1028 time_us - last_update_us >= 1e6) {
Stefan Holmer13181032016-07-29 14:48:54 +02001029 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +02001030 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1031 1000000;
1032 time_series.points.emplace_back(x, y);
Stefan Holmer492ee282016-10-27 17:19:20 +02001033 last_update_us = time_us;
Stefan Holmer13181032016-07-29 14:48:54 +02001034 }
1035 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1036 }
1037 // Add the data set to the plot.
tereliusdc35dcd2016-08-01 12:03:27 -07001038 plot->series_list_.push_back(std::move(time_series));
Stefan Holmer60e43462016-09-07 09:58:20 +02001039 plot->series_list_.push_back(std::move(acked_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001040
tereliusdc35dcd2016-08-01 12:03:27 -07001041 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1042 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1043 plot->SetTitle("Simulated BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001044}
1045
Stefan Holmer280de9e2016-09-30 10:06:51 +02001046// TODO(holmer): Remove once TransportFeedbackAdapter no longer needs a
1047// BitrateController.
1048class NullBitrateController : public BitrateController {
1049 public:
1050 ~NullBitrateController() override {}
1051 RtcpBandwidthObserver* CreateRtcpBandwidthObserver() override {
1052 return nullptr;
1053 }
1054 void SetStartBitrate(int start_bitrate_bps) override {}
1055 void SetMinMaxBitrate(int min_bitrate_bps, int max_bitrate_bps) override {}
1056 void SetBitrates(int start_bitrate_bps,
1057 int min_bitrate_bps,
1058 int max_bitrate_bps) override {}
1059 void ResetBitrates(int bitrate_bps,
1060 int min_bitrate_bps,
1061 int max_bitrate_bps) override {}
1062 void OnDelayBasedBweResult(const DelayBasedBwe::Result& result) override {}
1063 bool AvailableBandwidth(uint32_t* bandwidth) const override { return false; }
1064 void SetReservedBitrate(uint32_t reserved_bitrate_bps) override {}
1065 bool GetNetworkParameters(uint32_t* bitrate,
1066 uint8_t* fraction_loss,
1067 int64_t* rtt) override {
1068 return false;
1069 }
1070 int64_t TimeUntilNextProcess() override { return 0; }
1071 void Process() override {}
1072};
1073
tereliuse34c19c2016-08-15 08:47:14 -07001074void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanc3de0332016-08-02 07:22:17 -07001075 std::map<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1076 std::map<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
1077
1078 for (const auto& kv : rtp_packets_) {
1079 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1080 for (const LoggedRtpPacket& rtp_packet : kv.second)
1081 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1082 }
1083 }
1084
1085 for (const auto& kv : rtcp_packets_) {
1086 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1087 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1088 incoming_rtcp.insert(
1089 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1090 }
1091 }
1092
1093 SimulatedClock clock(0);
Stefan Holmer280de9e2016-09-30 10:06:51 +02001094 NullBitrateController null_controller;
1095 TransportFeedbackAdapter feedback_adapter(&clock, &null_controller);
stefan41aab322016-10-10 08:16:30 -07001096 feedback_adapter.InitBwe();
stefanc3de0332016-08-02 07:22:17 -07001097
1098 TimeSeries time_series;
1099 time_series.label = "Network Delay Change";
1100 time_series.style = LINE_DOT_GRAPH;
1101 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1102
1103 auto rtp_iterator = outgoing_rtp.begin();
1104 auto rtcp_iterator = incoming_rtcp.begin();
1105
1106 auto NextRtpTime = [&]() {
1107 if (rtp_iterator != outgoing_rtp.end())
1108 return static_cast<int64_t>(rtp_iterator->first);
1109 return std::numeric_limits<int64_t>::max();
1110 };
1111
1112 auto NextRtcpTime = [&]() {
1113 if (rtcp_iterator != incoming_rtcp.end())
1114 return static_cast<int64_t>(rtcp_iterator->first);
1115 return std::numeric_limits<int64_t>::max();
1116 };
1117
1118 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
1119 while (time_us != std::numeric_limits<int64_t>::max()) {
1120 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1121 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1122 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1123 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1124 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001125 feedback_adapter.OnTransportFeedback(
1126 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
stefanc3de0332016-08-02 07:22:17 -07001127 std::vector<PacketInfo> feedback =
Stefan Holmer60e43462016-09-07 09:58:20 +02001128 feedback_adapter.GetTransportFeedbackVector();
stefanc3de0332016-08-02 07:22:17 -07001129 for (const PacketInfo& packet : feedback) {
1130 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1131 float x =
1132 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1133 1000000;
1134 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1135 time_series.points.emplace_back(x, y);
1136 }
1137 }
1138 ++rtcp_iterator;
1139 }
1140 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1141 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1142 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1143 if (rtp.header.extension.hasTransportSequenceNumber) {
1144 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
1145 feedback_adapter.AddPacket(rtp.header.extension.transportSequenceNumber,
stefan985d2802016-11-15 06:54:09 -08001146 rtp.total_length, PacketInfo::kNotAProbe);
stefanc3de0332016-08-02 07:22:17 -07001147 feedback_adapter.OnSentPacket(
1148 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1149 }
1150 ++rtp_iterator;
1151 }
1152 time_us = std::min(NextRtpTime(), NextRtcpTime());
1153 }
1154 // We assume that the base network delay (w/o queues) is the min delay
1155 // observed during the call.
1156 for (TimeSeriesPoint& point : time_series.points)
1157 point.y -= estimated_base_delay_ms;
1158 // Add the data set to the plot.
1159 plot->series_list_.push_back(std::move(time_series));
1160
1161 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1162 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1163 plot->SetTitle("Network Delay Change.");
1164}
terelius54ce6802016-07-13 06:44:41 -07001165} // namespace plotting
1166} // namespace webrtc