blob: 6c42a72cf3329a5600c0cb7d0d2fd9d3a03535f2 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_tools/event_log_visualizer/analyzer.h"
terelius54ce6802016-07-13 06:44:41 -070012
13#include <algorithm>
14#include <limits>
15#include <map>
16#include <sstream>
17#include <string>
18#include <utility>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "call/audio_receive_stream.h"
21#include "call/audio_send_stream.h"
22#include "call/call.h"
23#include "call/video_receive_stream.h"
24#include "call/video_send_stream.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020025#include "common_types.h" // NOLINT(build/include)
Elad Alon99a81b62017-09-21 10:25:29 +020026#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_coding/neteq/tools/audio_sink.h"
28#include "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
29#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
30#include "modules/audio_coding/neteq/tools/neteq_replacement_input.h"
31#include "modules/audio_coding/neteq/tools/neteq_test.h"
32#include "modules/audio_coding/neteq/tools/resample_input_audio_file.h"
Bjorn Terelius28db2662017-10-04 14:22:43 +020033#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "modules/congestion_controller/include/send_side_congestion_controller.h"
35#include "modules/include/module_common_types.h"
36#include "modules/rtp_rtcp/include/rtp_rtcp.h"
37#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
38#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
39#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
40#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
41#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
42#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
43#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
44#include "modules/rtp_rtcp/source/rtp_utility.h"
45#include "rtc_base/checks.h"
46#include "rtc_base/format_macros.h"
47#include "rtc_base/logging.h"
48#include "rtc_base/ptr_util.h"
49#include "rtc_base/rate_statistics.h"
terelius54ce6802016-07-13 06:44:41 -070050
tereliusdc35dcd2016-08-01 12:03:27 -070051namespace webrtc {
52namespace plotting {
53
terelius54ce6802016-07-13 06:44:41 -070054namespace {
55
elad.alonec304f92017-03-08 05:03:53 -080056void SortPacketFeedbackVector(std::vector<PacketFeedback>* vec) {
57 auto pred = [](const PacketFeedback& packet_feedback) {
58 return packet_feedback.arrival_time_ms == PacketFeedback::kNotReceived;
59 };
60 vec->erase(std::remove_if(vec->begin(), vec->end(), pred), vec->end());
61 std::sort(vec->begin(), vec->end(), PacketFeedbackComparator());
62}
63
terelius54ce6802016-07-13 06:44:41 -070064std::string SsrcToString(uint32_t ssrc) {
65 std::stringstream ss;
66 ss << "SSRC " << ssrc;
67 return ss.str();
68}
69
70// Checks whether an SSRC is contained in the list of desired SSRCs.
71// Note that an empty SSRC list matches every SSRC.
72bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
73 if (desired_ssrc.size() == 0)
74 return true;
75 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
76 desired_ssrc.end();
77}
78
79double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
80 // The timestamp is a fixed point representation with 6 bits for seconds
81 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
82 // time in seconds and then multiply by 1000000 to convert to microseconds.
83 static constexpr double kTimestampToMicroSec =
tereliusccbbf8d2016-08-10 07:34:28 -070084 1000000.0 / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -070085 return abs_send_time * kTimestampToMicroSec;
86}
87
88// Computes the difference |later| - |earlier| where |later| and |earlier|
89// are counters that wrap at |modulus|. The difference is chosen to have the
90// least absolute value. For example if |modulus| is 8, then the difference will
91// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
92// be in [-4, 4].
93int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
94 RTC_DCHECK_LE(1, modulus);
95 RTC_DCHECK_LT(later, modulus);
96 RTC_DCHECK_LT(earlier, modulus);
97 int64_t difference =
98 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
99 int64_t max_difference = modulus / 2;
100 int64_t min_difference = max_difference - modulus + 1;
101 if (difference > max_difference) {
102 difference -= modulus;
103 }
104 if (difference < min_difference) {
105 difference += modulus;
106 }
terelius6addf492016-08-23 17:34:07 -0700107 if (difference > max_difference / 2 || difference < min_difference / 2) {
108 LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
109 << " expected to be in the range (" << min_difference / 2
110 << "," << max_difference / 2 << ") but is " << difference
111 << ". Correct unwrapping is uncertain.";
112 }
terelius54ce6802016-07-13 06:44:41 -0700113 return difference;
114}
115
ivocaac9d6f2016-09-22 07:01:47 -0700116// Return default values for header extensions, to use on streams without stored
117// mapping data. Currently this only applies to audio streams, since the mapping
118// is not stored in the event log.
119// TODO(ivoc): Remove this once this mapping is stored in the event log for
120// audio streams. Tracking bug: webrtc:6399
121webrtc::RtpHeaderExtensionMap GetDefaultHeaderExtensionMap() {
122 webrtc::RtpHeaderExtensionMap default_map;
danilchap4aecc582016-11-15 09:21:00 -0800123 default_map.Register<AudioLevel>(webrtc::RtpExtension::kAudioLevelDefaultId);
terelius007d5622017-08-08 05:40:26 -0700124 default_map.Register<TransmissionOffset>(
125 webrtc::RtpExtension::kTimestampOffsetDefaultId);
danilchap4aecc582016-11-15 09:21:00 -0800126 default_map.Register<AbsoluteSendTime>(
ivocaac9d6f2016-09-22 07:01:47 -0700127 webrtc::RtpExtension::kAbsSendTimeDefaultId);
terelius007d5622017-08-08 05:40:26 -0700128 default_map.Register<VideoOrientation>(
129 webrtc::RtpExtension::kVideoRotationDefaultId);
130 default_map.Register<VideoContentTypeExtension>(
131 webrtc::RtpExtension::kVideoContentTypeDefaultId);
132 default_map.Register<VideoTimingExtension>(
133 webrtc::RtpExtension::kVideoTimingDefaultId);
134 default_map.Register<TransportSequenceNumber>(
135 webrtc::RtpExtension::kTransportSequenceNumberDefaultId);
136 default_map.Register<PlayoutDelayLimits>(
137 webrtc::RtpExtension::kPlayoutDelayDefaultId);
ivocaac9d6f2016-09-22 07:01:47 -0700138 return default_map;
139}
140
tereliusdc35dcd2016-08-01 12:03:27 -0700141constexpr float kLeftMargin = 0.01f;
142constexpr float kRightMargin = 0.02f;
143constexpr float kBottomMargin = 0.02f;
144constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700145
terelius53dc23c2017-03-13 05:24:05 -0700146rtc::Optional<double> NetworkDelayDiff_AbsSendTime(
147 const LoggedRtpPacket& old_packet,
148 const LoggedRtpPacket& new_packet) {
149 if (old_packet.header.extension.hasAbsoluteSendTime &&
150 new_packet.header.extension.hasAbsoluteSendTime) {
151 int64_t send_time_diff = WrappingDifference(
152 new_packet.header.extension.absoluteSendTime,
153 old_packet.header.extension.absoluteSendTime, 1ul << 24);
154 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
155 double delay_change_us =
156 recv_time_diff - AbsSendTimeToMicroseconds(send_time_diff);
157 return rtc::Optional<double>(delay_change_us / 1000);
158 } else {
159 return rtc::Optional<double>();
terelius6addf492016-08-23 17:34:07 -0700160 }
161}
162
terelius53dc23c2017-03-13 05:24:05 -0700163rtc::Optional<double> NetworkDelayDiff_CaptureTime(
164 const LoggedRtpPacket& old_packet,
165 const LoggedRtpPacket& new_packet) {
166 int64_t send_time_diff = WrappingDifference(
167 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
168 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
169
170 const double kVideoSampleRate = 90000;
171 // TODO(terelius): We treat all streams as video for now, even though
172 // audio might be sampled at e.g. 16kHz, because it is really difficult to
173 // figure out the true sampling rate of a stream. The effect is that the
174 // delay will be scaled incorrectly for non-video streams.
175
176 double delay_change =
177 static_cast<double>(recv_time_diff) / 1000 -
178 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
179 if (delay_change < -10000 || 10000 < delay_change) {
180 LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
181 LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
182 << ", received time " << old_packet.timestamp;
183 LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
184 << ", received time " << new_packet.timestamp;
185 LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
186 << static_cast<double>(recv_time_diff) / 1000000 << "s";
187 LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
188 << static_cast<double>(send_time_diff) / kVideoSampleRate
189 << "s";
190 }
191 return rtc::Optional<double>(delay_change);
192}
193
194// For each element in data, use |get_y()| to extract a y-coordinate and
195// store the result in a TimeSeries.
196template <typename DataType>
197void ProcessPoints(
198 rtc::FunctionView<rtc::Optional<float>(const DataType&)> get_y,
199 const std::vector<DataType>& data,
200 uint64_t begin_time,
201 TimeSeries* result) {
202 for (size_t i = 0; i < data.size(); i++) {
203 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
204 rtc::Optional<float> y = get_y(data[i]);
205 if (y)
206 result->points.emplace_back(x, *y);
207 }
208}
209
210// For each pair of adjacent elements in |data|, use |get_y| to extract a
terelius6addf492016-08-23 17:34:07 -0700211// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
212// will be the time of the second element in the pair.
terelius53dc23c2017-03-13 05:24:05 -0700213template <typename DataType, typename ResultType>
214void ProcessPairs(
215 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
216 const DataType&)> get_y,
217 const std::vector<DataType>& data,
218 uint64_t begin_time,
219 TimeSeries* result) {
tereliusccbbf8d2016-08-10 07:34:28 -0700220 for (size_t i = 1; i < data.size(); i++) {
221 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
terelius53dc23c2017-03-13 05:24:05 -0700222 rtc::Optional<ResultType> y = get_y(data[i - 1], data[i]);
223 if (y)
224 result->points.emplace_back(x, static_cast<float>(*y));
225 }
226}
227
228// For each element in data, use |extract()| to extract a y-coordinate and
229// store the result in a TimeSeries.
230template <typename DataType, typename ResultType>
231void AccumulatePoints(
232 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> extract,
233 const std::vector<DataType>& data,
234 uint64_t begin_time,
235 TimeSeries* result) {
236 ResultType sum = 0;
237 for (size_t i = 0; i < data.size(); i++) {
238 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
239 rtc::Optional<ResultType> y = extract(data[i]);
240 if (y) {
241 sum += *y;
242 result->points.emplace_back(x, static_cast<float>(sum));
243 }
244 }
245}
246
247// For each pair of adjacent elements in |data|, use |extract()| to extract a
248// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
249// will be the time of the second element in the pair.
250template <typename DataType, typename ResultType>
251void AccumulatePairs(
252 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
253 const DataType&)> extract,
254 const std::vector<DataType>& data,
255 uint64_t begin_time,
256 TimeSeries* result) {
257 ResultType sum = 0;
258 for (size_t i = 1; i < data.size(); i++) {
259 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
260 rtc::Optional<ResultType> y = extract(data[i - 1], data[i]);
261 if (y)
262 sum += *y;
263 result->points.emplace_back(x, static_cast<float>(sum));
tereliusccbbf8d2016-08-10 07:34:28 -0700264 }
265}
266
terelius6addf492016-08-23 17:34:07 -0700267// Calculates a moving average of |data| and stores the result in a TimeSeries.
268// A data point is generated every |step| microseconds from |begin_time|
269// to |end_time|. The value of each data point is the average of the data
270// during the preceeding |window_duration_us| microseconds.
terelius53dc23c2017-03-13 05:24:05 -0700271template <typename DataType, typename ResultType>
272void MovingAverage(
273 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> extract,
274 const std::vector<DataType>& data,
275 uint64_t begin_time,
276 uint64_t end_time,
277 uint64_t window_duration_us,
278 uint64_t step,
279 webrtc::plotting::TimeSeries* result) {
terelius6addf492016-08-23 17:34:07 -0700280 size_t window_index_begin = 0;
281 size_t window_index_end = 0;
terelius53dc23c2017-03-13 05:24:05 -0700282 ResultType sum_in_window = 0;
terelius6addf492016-08-23 17:34:07 -0700283
284 for (uint64_t t = begin_time; t < end_time + step; t += step) {
285 while (window_index_end < data.size() &&
286 data[window_index_end].timestamp < t) {
terelius53dc23c2017-03-13 05:24:05 -0700287 rtc::Optional<ResultType> value = extract(data[window_index_end]);
288 if (value)
289 sum_in_window += *value;
terelius6addf492016-08-23 17:34:07 -0700290 ++window_index_end;
291 }
292 while (window_index_begin < data.size() &&
293 data[window_index_begin].timestamp < t - window_duration_us) {
terelius53dc23c2017-03-13 05:24:05 -0700294 rtc::Optional<ResultType> value = extract(data[window_index_begin]);
295 if (value)
296 sum_in_window -= *value;
terelius6addf492016-08-23 17:34:07 -0700297 ++window_index_begin;
298 }
299 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
300 float x = static_cast<float>(t - begin_time) / 1000000;
terelius53dc23c2017-03-13 05:24:05 -0700301 float y = sum_in_window / window_duration_s;
terelius6addf492016-08-23 17:34:07 -0700302 result->points.emplace_back(x, y);
303 }
304}
305
terelius54ce6802016-07-13 06:44:41 -0700306} // namespace
307
terelius54ce6802016-07-13 06:44:41 -0700308EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
309 : parsed_log_(log), window_duration_(250000), step_(10000) {
310 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
311 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700312
terelius88e64e52016-07-19 01:51:06 -0700313 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700314 uint8_t header[IP_PACKET_SIZE];
315 size_t header_length;
316 size_t total_length;
317
perkjbbbad6d2017-05-19 06:30:28 -0700318 uint8_t last_incoming_rtcp_packet[IP_PACKET_SIZE];
319 uint8_t last_incoming_rtcp_packet_length = 0;
320
ivocaac9d6f2016-09-22 07:01:47 -0700321 // Make a default extension map for streams without configuration information.
322 // TODO(ivoc): Once configuration of audio streams is stored in the event log,
323 // this can be removed. Tracking bug: webrtc:6399
324 RtpHeaderExtensionMap default_extension_map = GetDefaultHeaderExtensionMap();
325
henrik.lundin3c938fc2017-06-14 06:09:58 -0700326 rtc::Optional<uint64_t> last_log_start;
327
terelius54ce6802016-07-13 06:44:41 -0700328 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
329 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700330 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
331 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
332 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700333 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
334 event_type != ParsedRtcEventLog::LOG_START &&
335 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700336 uint64_t timestamp = parsed_log_.GetTimestamp(i);
337 first_timestamp = std::min(first_timestamp, timestamp);
338 last_timestamp = std::max(last_timestamp, timestamp);
339 }
340
341 switch (parsed_log_.GetEventType(i)) {
342 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700343 rtclog::StreamConfig config = parsed_log_.GetVideoReceiveConfig(i);
perkj09e71da2017-05-22 03:26:49 -0700344 StreamId stream(config.remote_ssrc, kIncomingPacket);
terelius0740a202016-08-08 10:21:04 -0700345 video_ssrcs_.insert(stream);
perkj09e71da2017-05-22 03:26:49 -0700346 StreamId rtx_stream(config.rtx_ssrc, kIncomingPacket);
brandtr14742122017-01-27 04:53:07 -0800347 video_ssrcs_.insert(rtx_stream);
348 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700349 break;
350 }
351 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700352 std::vector<rtclog::StreamConfig> configs =
353 parsed_log_.GetVideoSendConfig(i);
terelius405f90c2017-06-01 03:50:31 -0700354 for (const auto& config : configs) {
355 StreamId stream(config.local_ssrc, kOutgoingPacket);
terelius8fbc7652017-05-31 02:03:16 -0700356 video_ssrcs_.insert(stream);
terelius405f90c2017-06-01 03:50:31 -0700357 StreamId rtx_stream(config.rtx_ssrc, kOutgoingPacket);
terelius8fbc7652017-05-31 02:03:16 -0700358 video_ssrcs_.insert(rtx_stream);
359 rtx_ssrcs_.insert(rtx_stream);
360 }
terelius88e64e52016-07-19 01:51:06 -0700361 break;
362 }
363 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700364 rtclog::StreamConfig config = parsed_log_.GetAudioReceiveConfig(i);
perkjac8f52d2017-05-22 09:36:28 -0700365 StreamId stream(config.remote_ssrc, kIncomingPacket);
ivoce0928d82016-10-10 05:12:51 -0700366 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700367 break;
368 }
369 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700370 rtclog::StreamConfig config = parsed_log_.GetAudioSendConfig(i);
perkjf4726992017-05-22 10:12:26 -0700371 StreamId stream(config.local_ssrc, kOutgoingPacket);
ivoce0928d82016-10-10 05:12:51 -0700372 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700373 break;
374 }
375 case ParsedRtcEventLog::RTP_EVENT: {
ilnika8e781a2017-06-12 01:02:46 -0700376 RtpHeaderExtensionMap* extension_map = parsed_log_.GetRtpHeader(
Elad Alon1d87b0e2017-10-03 15:01:03 +0200377 i, &direction, header, &header_length, &total_length, nullptr);
terelius88e64e52016-07-19 01:51:06 -0700378 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
379 RTPHeader parsed_header;
ilnika8e781a2017-06-12 01:02:46 -0700380 if (extension_map != nullptr) {
terelius88e64e52016-07-19 01:51:06 -0700381 rtp_parser.Parse(&parsed_header, extension_map);
ivocaac9d6f2016-09-22 07:01:47 -0700382 } else {
383 // Use the default extension map.
384 // TODO(ivoc): Once configuration of audio streams is stored in the
385 // event log, this can be removed.
386 // Tracking bug: webrtc:6399
387 rtp_parser.Parse(&parsed_header, &default_extension_map);
terelius88e64e52016-07-19 01:51:06 -0700388 }
389 uint64_t timestamp = parsed_log_.GetTimestamp(i);
ilnika8e781a2017-06-12 01:02:46 -0700390 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700391 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200392 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700393 break;
394 }
395 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200396 uint8_t packet[IP_PACKET_SIZE];
perkj77cd58e2017-05-30 03:52:10 -0700397 parsed_log_.GetRtcpPacket(i, &direction, packet, &total_length);
perkjbbbad6d2017-05-19 06:30:28 -0700398 // Currently incoming RTCP packets are logged twice, both for audio and
399 // video. Only act on one of them. Compare against the previous parsed
400 // incoming RTCP packet.
401 if (direction == webrtc::kIncomingPacket) {
402 RTC_CHECK_LE(total_length, IP_PACKET_SIZE);
403 if (total_length == last_incoming_rtcp_packet_length &&
404 memcmp(last_incoming_rtcp_packet, packet, total_length) == 0) {
405 continue;
406 } else {
407 memcpy(last_incoming_rtcp_packet, packet, total_length);
408 last_incoming_rtcp_packet_length = total_length;
409 }
410 }
411 rtcp::CommonHeader header;
412 const uint8_t* packet_end = packet + total_length;
413 for (const uint8_t* block = packet; block < packet_end;
414 block = header.NextPacket()) {
415 RTC_CHECK(header.Parse(block, packet_end - block));
416 if (header.type() == rtcp::TransportFeedback::kPacketType &&
417 header.fmt() == rtcp::TransportFeedback::kFeedbackMessageType) {
418 std::unique_ptr<rtcp::TransportFeedback> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700419 rtc::MakeUnique<rtcp::TransportFeedback>());
perkjbbbad6d2017-05-19 06:30:28 -0700420 if (rtcp_packet->Parse(header)) {
421 uint32_t ssrc = rtcp_packet->sender_ssrc();
422 StreamId stream(ssrc, direction);
423 uint64_t timestamp = parsed_log_.GetTimestamp(i);
424 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
425 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
426 }
427 } else if (header.type() == rtcp::SenderReport::kPacketType) {
428 std::unique_ptr<rtcp::SenderReport> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700429 rtc::MakeUnique<rtcp::SenderReport>());
perkjbbbad6d2017-05-19 06:30:28 -0700430 if (rtcp_packet->Parse(header)) {
431 uint32_t ssrc = rtcp_packet->sender_ssrc();
432 StreamId stream(ssrc, direction);
433 uint64_t timestamp = parsed_log_.GetTimestamp(i);
434 rtcp_packets_[stream].push_back(
435 LoggedRtcpPacket(timestamp, kRtcpSr, std::move(rtcp_packet)));
436 }
437 } else if (header.type() == rtcp::ReceiverReport::kPacketType) {
438 std::unique_ptr<rtcp::ReceiverReport> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700439 rtc::MakeUnique<rtcp::ReceiverReport>());
perkjbbbad6d2017-05-19 06:30:28 -0700440 if (rtcp_packet->Parse(header)) {
441 uint32_t ssrc = rtcp_packet->sender_ssrc();
442 StreamId stream(ssrc, direction);
443 uint64_t timestamp = parsed_log_.GetTimestamp(i);
444 rtcp_packets_[stream].push_back(
445 LoggedRtcpPacket(timestamp, kRtcpRr, std::move(rtcp_packet)));
Stefan Holmer13181032016-07-29 14:48:54 +0200446 }
terelius2c8e8a32017-06-02 01:29:48 -0700447 } else if (header.type() == rtcp::Remb::kPacketType &&
448 header.fmt() == rtcp::Remb::kFeedbackMessageType) {
449 std::unique_ptr<rtcp::Remb> rtcp_packet(
450 rtc::MakeUnique<rtcp::Remb>());
451 if (rtcp_packet->Parse(header)) {
452 uint32_t ssrc = rtcp_packet->sender_ssrc();
453 StreamId stream(ssrc, direction);
454 uint64_t timestamp = parsed_log_.GetTimestamp(i);
455 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
456 timestamp, kRtcpRemb, std::move(rtcp_packet)));
457 }
Stefan Holmer13181032016-07-29 14:48:54 +0200458 }
Stefan Holmer13181032016-07-29 14:48:54 +0200459 }
terelius88e64e52016-07-19 01:51:06 -0700460 break;
461 }
462 case ParsedRtcEventLog::LOG_START: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700463 if (last_log_start) {
464 // A LOG_END event was missing. Use last_timestamp.
465 RTC_DCHECK_GE(last_timestamp, *last_log_start);
466 log_segments_.push_back(
467 std::make_pair(*last_log_start, last_timestamp));
468 }
469 last_log_start = rtc::Optional<uint64_t>(parsed_log_.GetTimestamp(i));
terelius88e64e52016-07-19 01:51:06 -0700470 break;
471 }
472 case ParsedRtcEventLog::LOG_END: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700473 RTC_DCHECK(last_log_start);
474 log_segments_.push_back(
475 std::make_pair(*last_log_start, parsed_log_.GetTimestamp(i)));
476 last_log_start.reset();
terelius88e64e52016-07-19 01:51:06 -0700477 break;
478 }
terelius424e6cf2017-02-20 05:14:41 -0800479 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700480 uint32_t this_ssrc;
481 parsed_log_.GetAudioPlayout(i, &this_ssrc);
482 audio_playout_events_[this_ssrc].push_back(parsed_log_.GetTimestamp(i));
terelius424e6cf2017-02-20 05:14:41 -0800483 break;
484 }
485 case ParsedRtcEventLog::LOSS_BASED_BWE_UPDATE: {
486 LossBasedBweUpdate bwe_update;
terelius8058e582016-07-25 01:32:41 -0700487 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
terelius424e6cf2017-02-20 05:14:41 -0800488 parsed_log_.GetLossBasedBweUpdate(i, &bwe_update.new_bitrate,
489 &bwe_update.fraction_loss,
490 &bwe_update.expected_packets);
terelius8058e582016-07-25 01:32:41 -0700491 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700492 break;
493 }
terelius424e6cf2017-02-20 05:14:41 -0800494 case ParsedRtcEventLog::DELAY_BASED_BWE_UPDATE: {
philipel10fc0e62017-04-11 01:50:23 -0700495 bwe_delay_updates_.push_back(parsed_log_.GetDelayBasedBweUpdate(i));
terelius424e6cf2017-02-20 05:14:41 -0800496 break;
497 }
minyue4b7c9522017-01-24 04:54:59 -0800498 case ParsedRtcEventLog::AUDIO_NETWORK_ADAPTATION_EVENT: {
michaelt6e5b2192017-02-22 07:33:27 -0800499 AudioNetworkAdaptationEvent ana_event;
500 ana_event.timestamp = parsed_log_.GetTimestamp(i);
501 parsed_log_.GetAudioNetworkAdaptation(i, &ana_event.config);
502 audio_network_adaptation_events_.push_back(ana_event);
minyue4b7c9522017-01-24 04:54:59 -0800503 break;
504 }
philipel32d00102017-02-27 02:18:46 -0800505 case ParsedRtcEventLog::BWE_PROBE_CLUSTER_CREATED_EVENT: {
philipele127e7a2017-03-29 16:28:53 +0200506 bwe_probe_cluster_created_events_.push_back(
507 parsed_log_.GetBweProbeClusterCreated(i));
philipel32d00102017-02-27 02:18:46 -0800508 break;
509 }
510 case ParsedRtcEventLog::BWE_PROBE_RESULT_EVENT: {
philipele127e7a2017-03-29 16:28:53 +0200511 bwe_probe_result_events_.push_back(parsed_log_.GetBweProbeResult(i));
philipel32d00102017-02-27 02:18:46 -0800512 break;
513 }
terelius88e64e52016-07-19 01:51:06 -0700514 case ParsedRtcEventLog::UNKNOWN_EVENT: {
515 break;
516 }
517 }
terelius54ce6802016-07-13 06:44:41 -0700518 }
terelius88e64e52016-07-19 01:51:06 -0700519
terelius54ce6802016-07-13 06:44:41 -0700520 if (last_timestamp < first_timestamp) {
521 // No useful events in the log.
522 first_timestamp = last_timestamp = 0;
523 }
524 begin_time_ = first_timestamp;
525 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700526 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
henrik.lundin3c938fc2017-06-14 06:09:58 -0700527 if (last_log_start) {
528 // The log was missing the last LOG_END event. Fake it.
529 log_segments_.push_back(std::make_pair(*last_log_start, end_time_));
530 }
terelius54ce6802016-07-13 06:44:41 -0700531}
532
Niels Möller245f17e2017-08-21 10:45:07 +0200533class BitrateObserver : public SendSideCongestionController::Observer,
Stefan Holmer13181032016-07-29 14:48:54 +0200534 public RemoteBitrateObserver {
535 public:
536 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
537
538 void OnNetworkChanged(uint32_t bitrate_bps,
539 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800540 int64_t rtt_ms,
541 int64_t probing_interval_ms) override {
Stefan Holmer13181032016-07-29 14:48:54 +0200542 last_bitrate_bps_ = bitrate_bps;
543 bitrate_updated_ = true;
544 }
545
546 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
547 uint32_t bitrate) override {}
548
549 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
550 bool GetAndResetBitrateUpdated() {
551 bool bitrate_updated = bitrate_updated_;
552 bitrate_updated_ = false;
553 return bitrate_updated;
554 }
555
556 private:
557 uint32_t last_bitrate_bps_;
558 bool bitrate_updated_;
559};
560
Stefan Holmer99f8e082016-09-09 13:37:50 +0200561bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700562 return rtx_ssrcs_.count(stream_id) == 1;
563}
564
Stefan Holmer99f8e082016-09-09 13:37:50 +0200565bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700566 return video_ssrcs_.count(stream_id) == 1;
567}
568
Stefan Holmer99f8e082016-09-09 13:37:50 +0200569bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700570 return audio_ssrcs_.count(stream_id) == 1;
571}
572
Stefan Holmer99f8e082016-09-09 13:37:50 +0200573std::string EventLogAnalyzer::GetStreamName(StreamId stream_id) const {
574 std::stringstream name;
575 if (IsAudioSsrc(stream_id)) {
576 name << "Audio ";
577 } else if (IsVideoSsrc(stream_id)) {
578 name << "Video ";
579 } else {
580 name << "Unknown ";
581 }
582 if (IsRtxSsrc(stream_id))
583 name << "RTX ";
ivocaac9d6f2016-09-22 07:01:47 -0700584 if (stream_id.GetDirection() == kIncomingPacket) {
585 name << "(In) ";
586 } else {
587 name << "(Out) ";
588 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200589 name << SsrcToString(stream_id.GetSsrc());
590 return name.str();
591}
592
terelius54ce6802016-07-13 06:44:41 -0700593void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
594 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700595 for (auto& kv : rtp_packets_) {
596 StreamId stream_id = kv.first;
597 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
598 // Filter on direction and SSRC.
599 if (stream_id.GetDirection() != desired_direction ||
600 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
601 continue;
terelius54ce6802016-07-13 06:44:41 -0700602 }
terelius54ce6802016-07-13 06:44:41 -0700603
terelius23c595a2017-03-15 01:59:12 -0700604 TimeSeries time_series(GetStreamName(stream_id), BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700605 ProcessPoints<LoggedRtpPacket>(
606 [](const LoggedRtpPacket& packet) -> rtc::Optional<float> {
607 return rtc::Optional<float>(packet.total_length);
608 },
609 packet_stream, begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700610 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700611 }
612
tereliusdc35dcd2016-08-01 12:03:27 -0700613 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
614 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
615 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700616 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700617 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700618 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700619 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700620 }
621}
622
philipelccd74892016-09-05 02:46:25 -0700623template <typename T>
624void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
625 PacketDirection desired_direction,
626 Plot* plot,
627 const std::map<StreamId, std::vector<T>>& packets,
628 const std::string& label_prefix) {
629 for (auto& kv : packets) {
630 StreamId stream_id = kv.first;
631 const std::vector<T>& packet_stream = kv.second;
632 // Filter on direction and SSRC.
633 if (stream_id.GetDirection() != desired_direction ||
634 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
635 continue;
636 }
637
terelius23c595a2017-03-15 01:59:12 -0700638 std::string label = label_prefix + " " + GetStreamName(stream_id);
639 TimeSeries time_series(label, LINE_STEP_GRAPH);
philipelccd74892016-09-05 02:46:25 -0700640 for (size_t i = 0; i < packet_stream.size(); i++) {
641 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
642 1000000;
philipelccd74892016-09-05 02:46:25 -0700643 time_series.points.emplace_back(x, i + 1);
644 }
645
philipel35ba9bd2017-04-19 05:58:51 -0700646 plot->AppendTimeSeries(std::move(time_series));
philipelccd74892016-09-05 02:46:25 -0700647 }
648}
649
650void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
651 PacketDirection desired_direction,
652 Plot* plot) {
653 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
654 "RTP");
655 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
656 "RTCP");
657
658 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
659 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
660 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
661 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
662 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
663 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
664 }
665}
666
terelius54ce6802016-07-13 06:44:41 -0700667// For each SSRC, plot the time between the consecutive playouts.
668void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
669 std::map<uint32_t, TimeSeries> time_series;
670 std::map<uint32_t, uint64_t> last_playout;
671
672 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700673
674 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
675 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
676 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
677 parsed_log_.GetAudioPlayout(i, &ssrc);
678 uint64_t timestamp = parsed_log_.GetTimestamp(i);
679 if (MatchingSsrc(ssrc, desired_ssrc_)) {
680 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
681 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
682 if (time_series[ssrc].points.size() == 0) {
683 // There were no previusly logged playout for this SSRC.
684 // Generate a point, but place it on the x-axis.
685 y = 0;
686 }
terelius54ce6802016-07-13 06:44:41 -0700687 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
688 last_playout[ssrc] = timestamp;
689 }
690 }
691 }
692
693 // Set labels and put in graph.
694 for (auto& kv : time_series) {
695 kv.second.label = SsrcToString(kv.first);
696 kv.second.style = BAR_GRAPH;
philipel35ba9bd2017-04-19 05:58:51 -0700697 plot->AppendTimeSeries(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700698 }
699
tereliusdc35dcd2016-08-01 12:03:27 -0700700 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
701 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
702 kTopMargin);
703 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700704}
705
ivocaac9d6f2016-09-22 07:01:47 -0700706// For audio SSRCs, plot the audio level.
707void EventLogAnalyzer::CreateAudioLevelGraph(Plot* plot) {
708 std::map<StreamId, TimeSeries> time_series;
709
710 for (auto& kv : rtp_packets_) {
711 StreamId stream_id = kv.first;
712 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
713 // TODO(ivoc): When audio send/receive configs are stored in the event
714 // log, a check should be added here to only process audio
715 // streams. Tracking bug: webrtc:6399
716 for (auto& packet : packet_stream) {
717 if (packet.header.extension.hasAudioLevel) {
718 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
719 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
720 // Here we convert it to dBov.
721 float y = static_cast<float>(-packet.header.extension.audioLevel);
722 time_series[stream_id].points.emplace_back(TimeSeriesPoint(x, y));
723 }
724 }
725 }
726
727 for (auto& series : time_series) {
728 series.second.label = GetStreamName(series.first);
729 series.second.style = LINE_GRAPH;
philipel35ba9bd2017-04-19 05:58:51 -0700730 plot->AppendTimeSeries(std::move(series.second));
ivocaac9d6f2016-09-22 07:01:47 -0700731 }
732
733 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
ivocbf676632016-11-24 08:30:34 -0800734 plot->SetYAxis(-127, 0, "Audio level (dBov)", kBottomMargin,
ivocaac9d6f2016-09-22 07:01:47 -0700735 kTopMargin);
736 plot->SetTitle("Audio level");
737}
738
terelius54ce6802016-07-13 06:44:41 -0700739// For each SSRC, plot the time between the consecutive playouts.
740void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700741 for (auto& kv : rtp_packets_) {
742 StreamId stream_id = kv.first;
743 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
744 // Filter on direction and SSRC.
745 if (stream_id.GetDirection() != kIncomingPacket ||
746 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
747 continue;
terelius54ce6802016-07-13 06:44:41 -0700748 }
terelius54ce6802016-07-13 06:44:41 -0700749
terelius23c595a2017-03-15 01:59:12 -0700750 TimeSeries time_series(GetStreamName(stream_id), BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700751 ProcessPairs<LoggedRtpPacket, float>(
752 [](const LoggedRtpPacket& old_packet,
753 const LoggedRtpPacket& new_packet) {
754 int64_t diff =
755 WrappingDifference(new_packet.header.sequenceNumber,
756 old_packet.header.sequenceNumber, 1ul << 16);
757 return rtc::Optional<float>(diff);
758 },
759 packet_stream, begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700760 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700761 }
762
tereliusdc35dcd2016-08-01 12:03:27 -0700763 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
764 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
765 kTopMargin);
766 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700767}
768
Stefan Holmer99f8e082016-09-09 13:37:50 +0200769void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
770 for (auto& kv : rtp_packets_) {
771 StreamId stream_id = kv.first;
772 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
773 // Filter on direction and SSRC.
774 if (stream_id.GetDirection() != kIncomingPacket ||
terelius4c9b4af2017-01-30 08:44:51 -0800775 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
776 packet_stream.size() == 0) {
Stefan Holmer99f8e082016-09-09 13:37:50 +0200777 continue;
778 }
779
terelius23c595a2017-03-15 01:59:12 -0700780 TimeSeries time_series(GetStreamName(stream_id), LINE_DOT_GRAPH);
Stefan Holmer99f8e082016-09-09 13:37:50 +0200781 const uint64_t kWindowUs = 1000000;
terelius4c9b4af2017-01-30 08:44:51 -0800782 const uint64_t kStep = 1000000;
783 SequenceNumberUnwrapper unwrapper_;
784 SequenceNumberUnwrapper prior_unwrapper_;
785 size_t window_index_begin = 0;
786 size_t window_index_end = 0;
787 int64_t highest_seq_number =
788 unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
789 int64_t highest_prior_seq_number =
790 prior_unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
791
792 for (uint64_t t = begin_time_; t < end_time_ + kStep; t += kStep) {
793 while (window_index_end < packet_stream.size() &&
794 packet_stream[window_index_end].timestamp < t) {
795 int64_t sequence_number = unwrapper_.Unwrap(
796 packet_stream[window_index_end].header.sequenceNumber);
797 highest_seq_number = std::max(highest_seq_number, sequence_number);
798 ++window_index_end;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200799 }
terelius4c9b4af2017-01-30 08:44:51 -0800800 while (window_index_begin < packet_stream.size() &&
801 packet_stream[window_index_begin].timestamp < t - kWindowUs) {
802 int64_t sequence_number = prior_unwrapper_.Unwrap(
803 packet_stream[window_index_begin].header.sequenceNumber);
804 highest_prior_seq_number =
805 std::max(highest_prior_seq_number, sequence_number);
806 ++window_index_begin;
807 }
808 float x = static_cast<float>(t - begin_time_) / 1000000;
809 int64_t expected_packets = highest_seq_number - highest_prior_seq_number;
810 if (expected_packets > 0) {
811 int64_t received_packets = window_index_end - window_index_begin;
812 int64_t lost_packets = expected_packets - received_packets;
813 float y = static_cast<float>(lost_packets) / expected_packets * 100;
814 time_series.points.emplace_back(x, y);
815 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200816 }
philipel35ba9bd2017-04-19 05:58:51 -0700817 plot->AppendTimeSeries(std::move(time_series));
Stefan Holmer99f8e082016-09-09 13:37:50 +0200818 }
819
820 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
821 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
822 kTopMargin);
823 plot->SetTitle("Estimated incoming loss rate");
824}
825
terelius2ee076d2017-08-15 02:04:02 -0700826void EventLogAnalyzer::CreateIncomingDelayDeltaGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700827 for (auto& kv : rtp_packets_) {
828 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700829 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700830 // Filter on direction and SSRC.
831 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200832 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
833 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
834 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700835 continue;
836 }
terelius54ce6802016-07-13 06:44:41 -0700837
terelius23c595a2017-03-15 01:59:12 -0700838 TimeSeries capture_time_data(GetStreamName(stream_id) + " capture-time",
839 BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700840 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
841 packet_stream, begin_time_,
842 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700843 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700844
terelius23c595a2017-03-15 01:59:12 -0700845 TimeSeries send_time_data(GetStreamName(stream_id) + " abs-send-time",
846 BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700847 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
848 packet_stream, begin_time_,
849 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700850 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700851 }
852
tereliusdc35dcd2016-08-01 12:03:27 -0700853 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
854 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
855 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700856 plot->SetTitle("Network latency difference between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700857}
858
terelius2ee076d2017-08-15 02:04:02 -0700859void EventLogAnalyzer::CreateIncomingDelayGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700860 for (auto& kv : rtp_packets_) {
861 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700862 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700863 // Filter on direction and SSRC.
864 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200865 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
866 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
867 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700868 continue;
869 }
terelius54ce6802016-07-13 06:44:41 -0700870
terelius23c595a2017-03-15 01:59:12 -0700871 TimeSeries capture_time_data(GetStreamName(stream_id) + " capture-time",
872 LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700873 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
874 packet_stream, begin_time_,
875 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700876 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700877
terelius23c595a2017-03-15 01:59:12 -0700878 TimeSeries send_time_data(GetStreamName(stream_id) + " abs-send-time",
879 LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700880 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
881 packet_stream, begin_time_,
882 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700883 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700884 }
885
tereliusdc35dcd2016-08-01 12:03:27 -0700886 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
887 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
888 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700889 plot->SetTitle("Network latency (relative to first packet)");
terelius54ce6802016-07-13 06:44:41 -0700890}
891
tereliusf736d232016-08-04 10:00:11 -0700892// Plot the fraction of packets lost (as perceived by the loss-based BWE).
893void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -0700894 TimeSeries time_series("Fraction lost", LINE_DOT_GRAPH);
tereliusf736d232016-08-04 10:00:11 -0700895 for (auto& bwe_update : bwe_loss_updates_) {
896 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
897 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
philipel35ba9bd2017-04-19 05:58:51 -0700898 time_series.points.emplace_back(x, y);
tereliusf736d232016-08-04 10:00:11 -0700899 }
tereliusf736d232016-08-04 10:00:11 -0700900
901 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
902 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
903 kTopMargin);
904 plot->SetTitle("Reported packet loss");
philipel35ba9bd2017-04-19 05:58:51 -0700905 plot->AppendTimeSeries(std::move(time_series));
tereliusf736d232016-08-04 10:00:11 -0700906}
907
terelius54ce6802016-07-13 06:44:41 -0700908// Plot the total bandwidth used by all RTP streams.
909void EventLogAnalyzer::CreateTotalBitrateGraph(
910 PacketDirection desired_direction,
philipel23c7f252017-07-14 06:30:03 -0700911 Plot* plot,
912 bool show_detector_state) {
terelius54ce6802016-07-13 06:44:41 -0700913 struct TimestampSize {
914 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
915 uint64_t timestamp;
916 size_t size;
917 };
918 std::vector<TimestampSize> packets;
919
920 PacketDirection direction;
921 size_t total_length;
922
923 // Extract timestamps and sizes for the relevant packets.
924 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
925 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
926 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
Elad Alon1d87b0e2017-10-03 15:01:03 +0200927 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, &total_length,
928 nullptr);
terelius54ce6802016-07-13 06:44:41 -0700929 if (direction == desired_direction) {
930 uint64_t timestamp = parsed_log_.GetTimestamp(i);
931 packets.push_back(TimestampSize(timestamp, total_length));
932 }
933 }
934 }
935
936 size_t window_index_begin = 0;
937 size_t window_index_end = 0;
938 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700939
940 // Calculate a moving average of the bitrate and store in a TimeSeries.
philipel35ba9bd2017-04-19 05:58:51 -0700941 TimeSeries bitrate_series("Bitrate", LINE_GRAPH);
terelius54ce6802016-07-13 06:44:41 -0700942 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
943 while (window_index_end < packets.size() &&
944 packets[window_index_end].timestamp < time) {
945 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700946 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700947 }
948 while (window_index_begin < packets.size() &&
949 packets[window_index_begin].timestamp < time - window_duration_) {
950 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
951 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700952 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700953 }
954 float window_duration_in_seconds =
955 static_cast<float>(window_duration_) / 1000000;
956 float x = static_cast<float>(time - begin_time_) / 1000000;
957 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
philipel35ba9bd2017-04-19 05:58:51 -0700958 bitrate_series.points.emplace_back(x, y);
terelius54ce6802016-07-13 06:44:41 -0700959 }
philipel35ba9bd2017-04-19 05:58:51 -0700960 plot->AppendTimeSeries(std::move(bitrate_series));
terelius54ce6802016-07-13 06:44:41 -0700961
terelius8058e582016-07-25 01:32:41 -0700962 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
963 if (desired_direction == kOutgoingPacket) {
philipel35ba9bd2017-04-19 05:58:51 -0700964 TimeSeries loss_series("Loss-based estimate", LINE_STEP_GRAPH);
philipel10fc0e62017-04-11 01:50:23 -0700965 for (auto& loss_update : bwe_loss_updates_) {
terelius8058e582016-07-25 01:32:41 -0700966 float x =
philipel10fc0e62017-04-11 01:50:23 -0700967 static_cast<float>(loss_update.timestamp - begin_time_) / 1000000;
968 float y = static_cast<float>(loss_update.new_bitrate) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -0700969 loss_series.points.emplace_back(x, y);
philipel10fc0e62017-04-11 01:50:23 -0700970 }
971
philipel35ba9bd2017-04-19 05:58:51 -0700972 TimeSeries delay_series("Delay-based estimate", LINE_STEP_GRAPH);
philipel23c7f252017-07-14 06:30:03 -0700973 IntervalSeries overusing_series("Overusing", "#ff8e82",
974 IntervalSeries::kHorizontal);
975 IntervalSeries underusing_series("Underusing", "#5092fc",
976 IntervalSeries::kHorizontal);
977 IntervalSeries normal_series("Normal", "#c4ffc4",
978 IntervalSeries::kHorizontal);
979 IntervalSeries* last_series = &normal_series;
980 double last_detector_switch = 0.0;
981
982 BandwidthUsage last_detector_state = BandwidthUsage::kBwNormal;
983
philipel10fc0e62017-04-11 01:50:23 -0700984 for (auto& delay_update : bwe_delay_updates_) {
985 float x =
986 static_cast<float>(delay_update.timestamp - begin_time_) / 1000000;
987 float y = static_cast<float>(delay_update.bitrate_bps) / 1000;
philipel23c7f252017-07-14 06:30:03 -0700988
989 if (last_detector_state != delay_update.detector_state) {
990 last_series->intervals.emplace_back(last_detector_switch, x);
991 last_detector_state = delay_update.detector_state;
992 last_detector_switch = x;
993
994 switch (delay_update.detector_state) {
995 case BandwidthUsage::kBwNormal:
996 last_series = &normal_series;
997 break;
998 case BandwidthUsage::kBwUnderusing:
999 last_series = &underusing_series;
1000 break;
1001 case BandwidthUsage::kBwOverusing:
1002 last_series = &overusing_series;
1003 break;
Elad Alon1d87b0e2017-10-03 15:01:03 +02001004 case BandwidthUsage::kLast:
1005 RTC_NOTREACHED();
philipel23c7f252017-07-14 06:30:03 -07001006 }
1007 }
1008
philipel35ba9bd2017-04-19 05:58:51 -07001009 delay_series.points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -07001010 }
philipele127e7a2017-03-29 16:28:53 +02001011
philipel23c7f252017-07-14 06:30:03 -07001012 RTC_CHECK(last_series);
1013 last_series->intervals.emplace_back(last_detector_switch, end_time_);
1014
philipel35ba9bd2017-04-19 05:58:51 -07001015 TimeSeries created_series("Probe cluster created.", DOT_GRAPH);
philipele127e7a2017-03-29 16:28:53 +02001016 for (auto& cluster : bwe_probe_cluster_created_events_) {
1017 float x = static_cast<float>(cluster.timestamp - begin_time_) / 1000000;
1018 float y = static_cast<float>(cluster.bitrate_bps) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001019 created_series.points.emplace_back(x, y);
philipele127e7a2017-03-29 16:28:53 +02001020 }
1021
philipel35ba9bd2017-04-19 05:58:51 -07001022 TimeSeries result_series("Probing results.", DOT_GRAPH);
philipele127e7a2017-03-29 16:28:53 +02001023 for (auto& result : bwe_probe_result_events_) {
1024 if (result.bitrate_bps) {
1025 float x = static_cast<float>(result.timestamp - begin_time_) / 1000000;
1026 float y = static_cast<float>(*result.bitrate_bps) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001027 result_series.points.emplace_back(x, y);
philipele127e7a2017-03-29 16:28:53 +02001028 }
1029 }
philipel23c7f252017-07-14 06:30:03 -07001030
1031 if (show_detector_state) {
1032 plot->AppendIntervalSeries(std::move(overusing_series));
1033 plot->AppendIntervalSeries(std::move(underusing_series));
1034 plot->AppendIntervalSeries(std::move(normal_series));
1035 }
1036
1037 plot->AppendTimeSeries(std::move(bitrate_series));
philipel35ba9bd2017-04-19 05:58:51 -07001038 plot->AppendTimeSeries(std::move(loss_series));
1039 plot->AppendTimeSeries(std::move(delay_series));
1040 plot->AppendTimeSeries(std::move(created_series));
1041 plot->AppendTimeSeries(std::move(result_series));
terelius8058e582016-07-25 01:32:41 -07001042 }
philipele127e7a2017-03-29 16:28:53 +02001043
terelius2c8e8a32017-06-02 01:29:48 -07001044 // Overlay the incoming REMB over the outgoing bitrate
1045 // and outgoing REMB over incoming bitrate.
1046 PacketDirection remb_direction =
1047 desired_direction == kOutgoingPacket ? kIncomingPacket : kOutgoingPacket;
1048 TimeSeries remb_series("Remb", LINE_STEP_GRAPH);
1049 std::multimap<uint64_t, const LoggedRtcpPacket*> remb_packets;
1050 for (const auto& kv : rtcp_packets_) {
1051 if (kv.first.GetDirection() == remb_direction) {
1052 for (const LoggedRtcpPacket& rtcp_packet : kv.second) {
1053 if (rtcp_packet.type == kRtcpRemb) {
1054 remb_packets.insert(
1055 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1056 }
1057 }
1058 }
1059 }
1060
1061 for (const auto& kv : remb_packets) {
1062 const LoggedRtcpPacket* const rtcp = kv.second;
1063 const rtcp::Remb* const remb = static_cast<rtcp::Remb*>(rtcp->packet.get());
1064 float x = static_cast<float>(rtcp->timestamp - begin_time_) / 1000000;
1065 float y = static_cast<float>(remb->bitrate_bps()) / 1000;
1066 remb_series.points.emplace_back(x, y);
1067 }
1068 plot->AppendTimeSeriesIfNotEmpty(std::move(remb_series));
1069
tereliusdc35dcd2016-08-01 12:03:27 -07001070 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1071 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -07001072 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001073 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001074 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001075 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001076 }
1077}
1078
1079// For each SSRC, plot the bandwidth used by that stream.
1080void EventLogAnalyzer::CreateStreamBitrateGraph(
1081 PacketDirection desired_direction,
1082 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -07001083 for (auto& kv : rtp_packets_) {
1084 StreamId stream_id = kv.first;
1085 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
1086 // Filter on direction and SSRC.
1087 if (stream_id.GetDirection() != desired_direction ||
1088 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
1089 continue;
terelius54ce6802016-07-13 06:44:41 -07001090 }
1091
terelius23c595a2017-03-15 01:59:12 -07001092 TimeSeries time_series(GetStreamName(stream_id), LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001093 MovingAverage<LoggedRtpPacket, double>(
1094 [](const LoggedRtpPacket& packet) {
1095 return rtc::Optional<double>(packet.total_length * 8.0 / 1000.0);
1096 },
1097 packet_stream, begin_time_, end_time_, window_duration_, step_,
1098 &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001099 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -07001100 }
1101
tereliusdc35dcd2016-08-01 12:03:27 -07001102 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1103 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -07001104 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001105 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001106 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001107 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001108 }
1109}
1110
Bjorn Terelius28db2662017-10-04 14:22:43 +02001111void EventLogAnalyzer::CreateSendSideBweSimulationGraph(Plot* plot) {
stefanff421622017-04-20 03:24:01 -07001112 std::multimap<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1113 std::multimap<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
Stefan Holmer13181032016-07-29 14:48:54 +02001114
1115 for (const auto& kv : rtp_packets_) {
1116 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1117 for (const LoggedRtpPacket& rtp_packet : kv.second)
1118 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1119 }
1120 }
1121
1122 for (const auto& kv : rtcp_packets_) {
1123 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1124 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1125 incoming_rtcp.insert(
1126 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1127 }
1128 }
1129
1130 SimulatedClock clock(0);
1131 BitrateObserver observer;
1132 RtcEventLogNullImpl null_event_log;
nisse0245da02016-11-30 03:35:20 -08001133 PacketRouter packet_router;
Stefan Holmer5c8942a2017-08-22 16:16:44 +02001134 PacedSender pacer(&clock, &packet_router, &null_event_log);
1135 SendSideCongestionController cc(&clock, &observer, &null_event_log, &pacer);
Stefan Holmer13181032016-07-29 14:48:54 +02001136 // TODO(holmer): Log the call config and use that here instead.
1137 static const uint32_t kDefaultStartBitrateBps = 300000;
1138 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1139
terelius23c595a2017-03-15 01:59:12 -07001140 TimeSeries time_series("Delay-based estimate", LINE_DOT_GRAPH);
1141 TimeSeries acked_time_series("Acked bitrate", LINE_DOT_GRAPH);
Stefan Holmer13181032016-07-29 14:48:54 +02001142
1143 auto rtp_iterator = outgoing_rtp.begin();
1144 auto rtcp_iterator = incoming_rtcp.begin();
1145
1146 auto NextRtpTime = [&]() {
1147 if (rtp_iterator != outgoing_rtp.end())
1148 return static_cast<int64_t>(rtp_iterator->first);
1149 return std::numeric_limits<int64_t>::max();
1150 };
1151
1152 auto NextRtcpTime = [&]() {
1153 if (rtcp_iterator != incoming_rtcp.end())
1154 return static_cast<int64_t>(rtcp_iterator->first);
1155 return std::numeric_limits<int64_t>::max();
1156 };
1157
1158 auto NextProcessTime = [&]() {
1159 if (rtcp_iterator != incoming_rtcp.end() ||
1160 rtp_iterator != outgoing_rtp.end()) {
1161 return clock.TimeInMicroseconds() +
1162 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
1163 }
1164 return std::numeric_limits<int64_t>::max();
1165 };
1166
Stefan Holmer492ee282016-10-27 17:19:20 +02001167 RateStatistics acked_bitrate(250, 8000);
Stefan Holmer60e43462016-09-07 09:58:20 +02001168
Stefan Holmer13181032016-07-29 14:48:54 +02001169 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
Stefan Holmer492ee282016-10-27 17:19:20 +02001170 int64_t last_update_us = 0;
Stefan Holmer13181032016-07-29 14:48:54 +02001171 while (time_us != std::numeric_limits<int64_t>::max()) {
1172 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1173 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001174 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001175 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1176 if (rtcp.type == kRtcpTransportFeedback) {
elad.alon5bbf43f2017-03-09 06:40:08 -08001177 cc.OnTransportFeedback(
1178 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
1179 std::vector<PacketFeedback> feedback = cc.GetTransportFeedbackVector();
elad.alonec304f92017-03-08 05:03:53 -08001180 SortPacketFeedbackVector(&feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +02001181 rtc::Optional<uint32_t> bitrate_bps;
1182 if (!feedback.empty()) {
elad.alonf9490002017-03-06 05:32:21 -08001183 for (const PacketFeedback& packet : feedback)
Stefan Holmer60e43462016-09-07 09:58:20 +02001184 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1185 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
1186 }
1187 uint32_t y = 0;
1188 if (bitrate_bps)
1189 y = *bitrate_bps / 1000;
1190 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1191 1000000;
1192 acked_time_series.points.emplace_back(x, y);
Stefan Holmer13181032016-07-29 14:48:54 +02001193 }
1194 ++rtcp_iterator;
1195 }
1196 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001197 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001198 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1199 if (rtp.header.extension.hasTransportSequenceNumber) {
1200 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
elad.alond12a8e12017-03-23 11:04:48 -07001201 cc.AddPacket(rtp.header.ssrc,
1202 rtp.header.extension.transportSequenceNumber,
elad.alon5bbf43f2017-03-09 06:40:08 -08001203 rtp.total_length, PacedPacketInfo());
Stefan Holmer13181032016-07-29 14:48:54 +02001204 rtc::SentPacket sent_packet(
1205 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1206 cc.OnSentPacket(sent_packet);
1207 }
1208 ++rtp_iterator;
1209 }
stefanc3de0332016-08-02 07:22:17 -07001210 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1211 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001212 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001213 }
Stefan Holmer492ee282016-10-27 17:19:20 +02001214 if (observer.GetAndResetBitrateUpdated() ||
1215 time_us - last_update_us >= 1e6) {
Stefan Holmer13181032016-07-29 14:48:54 +02001216 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +02001217 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1218 1000000;
1219 time_series.points.emplace_back(x, y);
Stefan Holmer492ee282016-10-27 17:19:20 +02001220 last_update_us = time_us;
Stefan Holmer13181032016-07-29 14:48:54 +02001221 }
1222 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1223 }
1224 // Add the data set to the plot.
philipel35ba9bd2017-04-19 05:58:51 -07001225 plot->AppendTimeSeries(std::move(time_series));
1226 plot->AppendTimeSeries(std::move(acked_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001227
tereliusdc35dcd2016-08-01 12:03:27 -07001228 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1229 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
Bjorn Terelius28db2662017-10-04 14:22:43 +02001230 plot->SetTitle("Simulated send-side BWE behavior");
1231}
1232
1233void EventLogAnalyzer::CreateReceiveSideBweSimulationGraph(Plot* plot) {
1234 class RembInterceptingPacketRouter : public PacketRouter {
1235 public:
1236 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
1237 uint32_t bitrate_bps) override {
1238 last_bitrate_bps_ = bitrate_bps;
1239 bitrate_updated_ = true;
1240 PacketRouter::OnReceiveBitrateChanged(ssrcs, bitrate_bps);
1241 }
1242 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
1243 bool GetAndResetBitrateUpdated() {
1244 bool bitrate_updated = bitrate_updated_;
1245 bitrate_updated_ = false;
1246 return bitrate_updated;
1247 }
1248
1249 private:
1250 uint32_t last_bitrate_bps_;
1251 bool bitrate_updated_;
1252 };
1253
1254 std::multimap<uint64_t, const LoggedRtpPacket*> incoming_rtp;
1255
1256 for (const auto& kv : rtp_packets_) {
1257 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket &&
1258 IsVideoSsrc(kv.first)) {
1259 for (const LoggedRtpPacket& rtp_packet : kv.second)
1260 incoming_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1261 }
1262 }
1263
1264 SimulatedClock clock(0);
1265 RembInterceptingPacketRouter packet_router;
1266 // TODO(terelius): The PacketRrouter is the used as the RemoteBitrateObserver.
1267 // Is this intentional?
1268 ReceiveSideCongestionController rscc(&clock, &packet_router);
1269 // TODO(holmer): Log the call config and use that here instead.
1270 // static const uint32_t kDefaultStartBitrateBps = 300000;
1271 // rscc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1272
1273 TimeSeries time_series("Receive side estimate", LINE_DOT_GRAPH);
1274 TimeSeries acked_time_series("Received bitrate", LINE_GRAPH);
1275
1276 RateStatistics acked_bitrate(250, 8000);
1277 int64_t last_update_us = 0;
1278 for (const auto& kv : incoming_rtp) {
1279 const LoggedRtpPacket& packet = *kv.second;
1280 int64_t arrival_time_ms = packet.timestamp / 1000;
1281 size_t payload = packet.total_length; /*Should subtract header?*/
1282 clock.AdvanceTimeMicroseconds(packet.timestamp -
1283 clock.TimeInMicroseconds());
1284 rscc.OnReceivedPacket(arrival_time_ms, payload, packet.header);
1285 acked_bitrate.Update(payload, arrival_time_ms);
1286 rtc::Optional<uint32_t> bitrate_bps = acked_bitrate.Rate(arrival_time_ms);
1287 if (bitrate_bps) {
1288 uint32_t y = *bitrate_bps / 1000;
1289 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1290 1000000;
1291 acked_time_series.points.emplace_back(x, y);
1292 }
1293 if (packet_router.GetAndResetBitrateUpdated() ||
1294 clock.TimeInMicroseconds() - last_update_us >= 1e6) {
1295 uint32_t y = packet_router.last_bitrate_bps() / 1000;
1296 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1297 1000000;
1298 time_series.points.emplace_back(x, y);
1299 last_update_us = clock.TimeInMicroseconds();
1300 }
1301 }
1302 // Add the data set to the plot.
1303 plot->AppendTimeSeries(std::move(time_series));
1304 plot->AppendTimeSeries(std::move(acked_time_series));
1305
1306 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1307 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1308 plot->SetTitle("Simulated receive-side BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001309}
1310
tereliuse34c19c2016-08-15 08:47:14 -07001311void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanff421622017-04-20 03:24:01 -07001312 std::multimap<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1313 std::multimap<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
stefanc3de0332016-08-02 07:22:17 -07001314
1315 for (const auto& kv : rtp_packets_) {
1316 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1317 for (const LoggedRtpPacket& rtp_packet : kv.second)
1318 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1319 }
1320 }
1321
1322 for (const auto& kv : rtcp_packets_) {
1323 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1324 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1325 incoming_rtcp.insert(
1326 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1327 }
1328 }
1329
1330 SimulatedClock clock(0);
elad.alon5bbf43f2017-03-09 06:40:08 -08001331 TransportFeedbackAdapter feedback_adapter(&clock);
stefanc3de0332016-08-02 07:22:17 -07001332
stefana0a8ed72017-09-06 02:06:32 -07001333 TimeSeries late_feedback_series("Late feedback results.", DOT_GRAPH);
terelius23c595a2017-03-15 01:59:12 -07001334 TimeSeries time_series("Network Delay Change", LINE_DOT_GRAPH);
stefanc3de0332016-08-02 07:22:17 -07001335 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1336
1337 auto rtp_iterator = outgoing_rtp.begin();
1338 auto rtcp_iterator = incoming_rtcp.begin();
1339
1340 auto NextRtpTime = [&]() {
1341 if (rtp_iterator != outgoing_rtp.end())
1342 return static_cast<int64_t>(rtp_iterator->first);
1343 return std::numeric_limits<int64_t>::max();
1344 };
1345
1346 auto NextRtcpTime = [&]() {
1347 if (rtcp_iterator != incoming_rtcp.end())
1348 return static_cast<int64_t>(rtcp_iterator->first);
1349 return std::numeric_limits<int64_t>::max();
1350 };
1351
1352 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
stefana0a8ed72017-09-06 02:06:32 -07001353 int64_t prev_y = 0;
stefanc3de0332016-08-02 07:22:17 -07001354 while (time_us != std::numeric_limits<int64_t>::max()) {
1355 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1356 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1357 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1358 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1359 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001360 feedback_adapter.OnTransportFeedback(
1361 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
elad.alonf9490002017-03-06 05:32:21 -08001362 std::vector<PacketFeedback> feedback =
1363 feedback_adapter.GetTransportFeedbackVector();
elad.alonec304f92017-03-08 05:03:53 -08001364 SortPacketFeedbackVector(&feedback);
elad.alonf9490002017-03-06 05:32:21 -08001365 for (const PacketFeedback& packet : feedback) {
stefanc3de0332016-08-02 07:22:17 -07001366 float x =
1367 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1368 1000000;
stefana0a8ed72017-09-06 02:06:32 -07001369 if (packet.send_time_ms == -1) {
1370 late_feedback_series.points.emplace_back(x, prev_y);
1371 continue;
1372 }
1373 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1374 prev_y = y;
stefanc3de0332016-08-02 07:22:17 -07001375 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1376 time_series.points.emplace_back(x, y);
1377 }
1378 }
1379 ++rtcp_iterator;
1380 }
1381 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1382 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1383 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1384 if (rtp.header.extension.hasTransportSequenceNumber) {
1385 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
elad.alond12a8e12017-03-23 11:04:48 -07001386 feedback_adapter.AddPacket(rtp.header.ssrc,
1387 rtp.header.extension.transportSequenceNumber,
philipel8aadd502017-02-23 02:56:13 -08001388 rtp.total_length, PacedPacketInfo());
stefanc3de0332016-08-02 07:22:17 -07001389 feedback_adapter.OnSentPacket(
1390 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1391 }
1392 ++rtp_iterator;
1393 }
1394 time_us = std::min(NextRtpTime(), NextRtcpTime());
1395 }
1396 // We assume that the base network delay (w/o queues) is the min delay
1397 // observed during the call.
1398 for (TimeSeriesPoint& point : time_series.points)
1399 point.y -= estimated_base_delay_ms;
stefana0a8ed72017-09-06 02:06:32 -07001400 for (TimeSeriesPoint& point : late_feedback_series.points)
1401 point.y -= estimated_base_delay_ms;
stefanc3de0332016-08-02 07:22:17 -07001402 // Add the data set to the plot.
stefana0a8ed72017-09-06 02:06:32 -07001403 plot->AppendTimeSeriesIfNotEmpty(std::move(time_series));
1404 plot->AppendTimeSeriesIfNotEmpty(std::move(late_feedback_series));
stefanc3de0332016-08-02 07:22:17 -07001405
1406 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1407 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1408 plot->SetTitle("Network Delay Change.");
1409}
stefan08383272016-12-20 08:51:52 -08001410
1411std::vector<std::pair<int64_t, int64_t>> EventLogAnalyzer::GetFrameTimestamps()
1412 const {
1413 std::vector<std::pair<int64_t, int64_t>> timestamps;
1414 size_t largest_stream_size = 0;
1415 const std::vector<LoggedRtpPacket>* largest_video_stream = nullptr;
1416 // Find the incoming video stream with the most number of packets that is
1417 // not rtx.
1418 for (const auto& kv : rtp_packets_) {
1419 if (kv.first.GetDirection() == kIncomingPacket &&
1420 video_ssrcs_.find(kv.first) != video_ssrcs_.end() &&
1421 rtx_ssrcs_.find(kv.first) == rtx_ssrcs_.end() &&
1422 kv.second.size() > largest_stream_size) {
1423 largest_stream_size = kv.second.size();
1424 largest_video_stream = &kv.second;
1425 }
1426 }
1427 if (largest_video_stream == nullptr) {
1428 for (auto& packet : *largest_video_stream) {
1429 if (packet.header.markerBit) {
1430 int64_t capture_ms = packet.header.timestamp / 90.0;
1431 int64_t arrival_ms = packet.timestamp / 1000.0;
1432 timestamps.push_back(std::make_pair(capture_ms, arrival_ms));
1433 }
1434 }
1435 }
1436 return timestamps;
1437}
stefane372d3c2017-02-02 08:04:18 -08001438
1439void EventLogAnalyzer::CreateTimestampGraph(Plot* plot) {
1440 for (const auto& kv : rtp_packets_) {
1441 const std::vector<LoggedRtpPacket>& rtp_packets = kv.second;
1442 StreamId stream_id = kv.first;
1443
1444 {
terelius23c595a2017-03-15 01:59:12 -07001445 TimeSeries timestamp_data(GetStreamName(stream_id) + " capture-time",
1446 LINE_DOT_GRAPH);
stefane372d3c2017-02-02 08:04:18 -08001447 for (LoggedRtpPacket packet : rtp_packets) {
1448 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
1449 float y = packet.header.timestamp;
1450 timestamp_data.points.emplace_back(x, y);
1451 }
philipel35ba9bd2017-04-19 05:58:51 -07001452 plot->AppendTimeSeries(std::move(timestamp_data));
stefane372d3c2017-02-02 08:04:18 -08001453 }
1454
1455 {
1456 auto kv = rtcp_packets_.find(stream_id);
1457 if (kv != rtcp_packets_.end()) {
1458 const auto& packets = kv->second;
terelius23c595a2017-03-15 01:59:12 -07001459 TimeSeries timestamp_data(
1460 GetStreamName(stream_id) + " rtcp capture-time", LINE_DOT_GRAPH);
stefane372d3c2017-02-02 08:04:18 -08001461 for (const LoggedRtcpPacket& rtcp : packets) {
1462 if (rtcp.type != kRtcpSr)
1463 continue;
1464 rtcp::SenderReport* sr;
1465 sr = static_cast<rtcp::SenderReport*>(rtcp.packet.get());
1466 float x = static_cast<float>(rtcp.timestamp - begin_time_) / 1000000;
1467 float y = sr->rtp_timestamp();
1468 timestamp_data.points.emplace_back(x, y);
1469 }
philipel35ba9bd2017-04-19 05:58:51 -07001470 plot->AppendTimeSeries(std::move(timestamp_data));
stefane372d3c2017-02-02 08:04:18 -08001471 }
1472 }
1473 }
1474
1475 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1476 plot->SetSuggestedYAxis(0, 1, "Timestamp (90khz)", kBottomMargin, kTopMargin);
1477 plot->SetTitle("Timestamps");
1478}
michaelt6e5b2192017-02-22 07:33:27 -08001479
1480void EventLogAnalyzer::CreateAudioEncoderTargetBitrateGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001481 TimeSeries time_series("Audio encoder target bitrate", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001482 ProcessPoints<AudioNetworkAdaptationEvent>(
1483 [](const AudioNetworkAdaptationEvent& ana_event) -> rtc::Optional<float> {
michaelt6e5b2192017-02-22 07:33:27 -08001484 if (ana_event.config.bitrate_bps)
1485 return rtc::Optional<float>(
1486 static_cast<float>(*ana_event.config.bitrate_bps));
1487 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001488 },
philipel35ba9bd2017-04-19 05:58:51 -07001489 audio_network_adaptation_events_, begin_time_, &time_series);
1490 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001491 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1492 plot->SetSuggestedYAxis(0, 1, "Bitrate (bps)", kBottomMargin, kTopMargin);
1493 plot->SetTitle("Reported audio encoder target bitrate");
1494}
1495
1496void EventLogAnalyzer::CreateAudioEncoderFrameLengthGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001497 TimeSeries time_series("Audio encoder frame length", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001498 ProcessPoints<AudioNetworkAdaptationEvent>(
1499 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001500 if (ana_event.config.frame_length_ms)
1501 return rtc::Optional<float>(
1502 static_cast<float>(*ana_event.config.frame_length_ms));
1503 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001504 },
philipel35ba9bd2017-04-19 05:58:51 -07001505 audio_network_adaptation_events_, begin_time_, &time_series);
1506 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001507 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1508 plot->SetSuggestedYAxis(0, 1, "Frame length (ms)", kBottomMargin, kTopMargin);
1509 plot->SetTitle("Reported audio encoder frame length");
1510}
1511
terelius2ee076d2017-08-15 02:04:02 -07001512void EventLogAnalyzer::CreateAudioEncoderPacketLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001513 TimeSeries time_series("Audio encoder uplink packet loss fraction",
1514 LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001515 ProcessPoints<AudioNetworkAdaptationEvent>(
1516 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001517 if (ana_event.config.uplink_packet_loss_fraction)
1518 return rtc::Optional<float>(static_cast<float>(
1519 *ana_event.config.uplink_packet_loss_fraction));
1520 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001521 },
philipel35ba9bd2017-04-19 05:58:51 -07001522 audio_network_adaptation_events_, begin_time_, &time_series);
1523 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001524 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1525 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
1526 kTopMargin);
1527 plot->SetTitle("Reported audio encoder lost packets");
1528}
1529
1530void EventLogAnalyzer::CreateAudioEncoderEnableFecGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001531 TimeSeries time_series("Audio encoder FEC", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001532 ProcessPoints<AudioNetworkAdaptationEvent>(
1533 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001534 if (ana_event.config.enable_fec)
1535 return rtc::Optional<float>(
1536 static_cast<float>(*ana_event.config.enable_fec));
1537 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001538 },
philipel35ba9bd2017-04-19 05:58:51 -07001539 audio_network_adaptation_events_, begin_time_, &time_series);
1540 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001541 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1542 plot->SetSuggestedYAxis(0, 1, "FEC (false/true)", kBottomMargin, kTopMargin);
1543 plot->SetTitle("Reported audio encoder FEC");
1544}
1545
1546void EventLogAnalyzer::CreateAudioEncoderEnableDtxGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001547 TimeSeries time_series("Audio encoder DTX", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001548 ProcessPoints<AudioNetworkAdaptationEvent>(
1549 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001550 if (ana_event.config.enable_dtx)
1551 return rtc::Optional<float>(
1552 static_cast<float>(*ana_event.config.enable_dtx));
1553 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001554 },
philipel35ba9bd2017-04-19 05:58:51 -07001555 audio_network_adaptation_events_, begin_time_, &time_series);
1556 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001557 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1558 plot->SetSuggestedYAxis(0, 1, "DTX (false/true)", kBottomMargin, kTopMargin);
1559 plot->SetTitle("Reported audio encoder DTX");
1560}
1561
1562void EventLogAnalyzer::CreateAudioEncoderNumChannelsGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001563 TimeSeries time_series("Audio encoder number of channels", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001564 ProcessPoints<AudioNetworkAdaptationEvent>(
1565 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001566 if (ana_event.config.num_channels)
1567 return rtc::Optional<float>(
1568 static_cast<float>(*ana_event.config.num_channels));
1569 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001570 },
philipel35ba9bd2017-04-19 05:58:51 -07001571 audio_network_adaptation_events_, begin_time_, &time_series);
1572 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001573 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1574 plot->SetSuggestedYAxis(0, 1, "Number of channels (1 (mono)/2 (stereo))",
1575 kBottomMargin, kTopMargin);
1576 plot->SetTitle("Reported audio encoder number of channels");
1577}
henrik.lundin3c938fc2017-06-14 06:09:58 -07001578
1579class NetEqStreamInput : public test::NetEqInput {
1580 public:
1581 // Does not take any ownership, and all pointers must refer to valid objects
1582 // that outlive the one constructed.
1583 NetEqStreamInput(const std::vector<LoggedRtpPacket>* packet_stream,
1584 const std::vector<uint64_t>* output_events_us,
1585 rtc::Optional<uint64_t> end_time_us)
1586 : packet_stream_(*packet_stream),
1587 packet_stream_it_(packet_stream_.begin()),
1588 output_events_us_it_(output_events_us->begin()),
1589 output_events_us_end_(output_events_us->end()),
1590 end_time_us_(end_time_us) {
1591 RTC_DCHECK(packet_stream);
1592 RTC_DCHECK(output_events_us);
1593 }
1594
1595 rtc::Optional<int64_t> NextPacketTime() const override {
1596 if (packet_stream_it_ == packet_stream_.end()) {
1597 return rtc::Optional<int64_t>();
1598 }
1599 if (end_time_us_ && packet_stream_it_->timestamp > *end_time_us_) {
1600 return rtc::Optional<int64_t>();
1601 }
1602 // Convert from us to ms.
1603 return rtc::Optional<int64_t>(packet_stream_it_->timestamp / 1000);
1604 }
1605
1606 rtc::Optional<int64_t> NextOutputEventTime() const override {
1607 if (output_events_us_it_ == output_events_us_end_) {
1608 return rtc::Optional<int64_t>();
1609 }
1610 if (end_time_us_ && *output_events_us_it_ > *end_time_us_) {
1611 return rtc::Optional<int64_t>();
1612 }
1613 // Convert from us to ms.
1614 return rtc::Optional<int64_t>(
1615 rtc::checked_cast<int64_t>(*output_events_us_it_ / 1000));
1616 }
1617
1618 std::unique_ptr<PacketData> PopPacket() override {
1619 if (packet_stream_it_ == packet_stream_.end()) {
1620 return std::unique_ptr<PacketData>();
1621 }
1622 std::unique_ptr<PacketData> packet_data(new PacketData());
1623 packet_data->header = packet_stream_it_->header;
1624 // Convert from us to ms.
1625 packet_data->time_ms = packet_stream_it_->timestamp / 1000.0;
1626
1627 // This is a header-only "dummy" packet. Set the payload to all zeros, with
1628 // length according to the virtual length.
1629 packet_data->payload.SetSize(packet_stream_it_->total_length);
1630 std::fill_n(packet_data->payload.data(), packet_data->payload.size(), 0);
1631
1632 ++packet_stream_it_;
1633 return packet_data;
1634 }
1635
1636 void AdvanceOutputEvent() override {
1637 if (output_events_us_it_ != output_events_us_end_) {
1638 ++output_events_us_it_;
1639 }
1640 }
1641
1642 bool ended() const override { return !NextEventTime(); }
1643
1644 rtc::Optional<RTPHeader> NextHeader() const override {
1645 if (packet_stream_it_ == packet_stream_.end()) {
1646 return rtc::Optional<RTPHeader>();
1647 }
1648 return rtc::Optional<RTPHeader>(packet_stream_it_->header);
1649 }
1650
1651 private:
1652 const std::vector<LoggedRtpPacket>& packet_stream_;
1653 std::vector<LoggedRtpPacket>::const_iterator packet_stream_it_;
1654 std::vector<uint64_t>::const_iterator output_events_us_it_;
1655 const std::vector<uint64_t>::const_iterator output_events_us_end_;
1656 const rtc::Optional<uint64_t> end_time_us_;
1657};
1658
1659namespace {
1660// Creates a NetEq test object and all necessary input and output helpers. Runs
1661// the test and returns the NetEqDelayAnalyzer object that was used to
1662// instrument the test.
1663std::unique_ptr<test::NetEqDelayAnalyzer> CreateNetEqTestAndRun(
1664 const std::vector<LoggedRtpPacket>* packet_stream,
1665 const std::vector<uint64_t>* output_events_us,
1666 rtc::Optional<uint64_t> end_time_us,
1667 const std::string& replacement_file_name,
1668 int file_sample_rate_hz) {
1669 std::unique_ptr<test::NetEqInput> input(
1670 new NetEqStreamInput(packet_stream, output_events_us, end_time_us));
1671
1672 constexpr int kReplacementPt = 127;
1673 std::set<uint8_t> cn_types;
1674 std::set<uint8_t> forbidden_types;
1675 input.reset(new test::NetEqReplacementInput(std::move(input), kReplacementPt,
1676 cn_types, forbidden_types));
1677
1678 NetEq::Config config;
1679 config.max_packets_in_buffer = 200;
1680 config.enable_fast_accelerate = true;
1681
1682 std::unique_ptr<test::VoidAudioSink> output(new test::VoidAudioSink());
1683
1684 test::NetEqTest::DecoderMap codecs;
1685
1686 // Create a "replacement decoder" that produces the decoded audio by reading
1687 // from a file rather than from the encoded payloads.
1688 std::unique_ptr<test::ResampleInputAudioFile> replacement_file(
1689 new test::ResampleInputAudioFile(replacement_file_name,
1690 file_sample_rate_hz));
1691 replacement_file->set_output_rate_hz(48000);
1692 std::unique_ptr<AudioDecoder> replacement_decoder(
1693 new test::FakeDecodeFromFile(std::move(replacement_file), 48000, false));
1694 test::NetEqTest::ExtDecoderMap ext_codecs;
1695 ext_codecs[kReplacementPt] = {replacement_decoder.get(),
1696 NetEqDecoder::kDecoderArbitrary,
1697 "replacement codec"};
1698
1699 std::unique_ptr<test::NetEqDelayAnalyzer> delay_cb(
1700 new test::NetEqDelayAnalyzer);
1701 test::DefaultNetEqTestErrorCallback error_cb;
1702 test::NetEqTest::Callbacks callbacks;
1703 callbacks.error_callback = &error_cb;
1704 callbacks.post_insert_packet = delay_cb.get();
1705 callbacks.get_audio_callback = delay_cb.get();
1706
1707 test::NetEqTest test(config, codecs, ext_codecs, std::move(input),
1708 std::move(output), callbacks);
1709 test.Run();
1710 return delay_cb;
1711}
1712} // namespace
1713
1714// Plots the jitter buffer delay profile. This will plot only for the first
1715// incoming audio SSRC. If the stream contains more than one incoming audio
1716// SSRC, all but the first will be ignored.
1717void EventLogAnalyzer::CreateAudioJitterBufferGraph(
1718 const std::string& replacement_file_name,
1719 int file_sample_rate_hz,
1720 Plot* plot) {
1721 const auto& incoming_audio_kv = std::find_if(
1722 rtp_packets_.begin(), rtp_packets_.end(),
1723 [this](std::pair<StreamId, std::vector<LoggedRtpPacket>> kv) {
1724 return kv.first.GetDirection() == kIncomingPacket &&
1725 this->IsAudioSsrc(kv.first);
1726 });
1727 if (incoming_audio_kv == rtp_packets_.end()) {
1728 // No incoming audio stream found.
1729 return;
1730 }
1731
1732 const uint32_t ssrc = incoming_audio_kv->first.GetSsrc();
1733
1734 std::map<uint32_t, std::vector<uint64_t>>::const_iterator output_events_it =
1735 audio_playout_events_.find(ssrc);
1736 if (output_events_it == audio_playout_events_.end()) {
1737 // Could not find output events with SSRC matching the input audio stream.
1738 // Using the first available stream of output events.
1739 output_events_it = audio_playout_events_.cbegin();
1740 }
1741
1742 rtc::Optional<uint64_t> end_time_us =
1743 log_segments_.empty()
1744 ? rtc::Optional<uint64_t>()
1745 : rtc::Optional<uint64_t>(log_segments_.front().second);
1746
1747 auto delay_cb = CreateNetEqTestAndRun(
1748 &incoming_audio_kv->second, &output_events_it->second, end_time_us,
1749 replacement_file_name, file_sample_rate_hz);
1750
1751 std::vector<float> send_times_s;
1752 std::vector<float> arrival_delay_ms;
1753 std::vector<float> corrected_arrival_delay_ms;
1754 std::vector<rtc::Optional<float>> playout_delay_ms;
1755 std::vector<rtc::Optional<float>> target_delay_ms;
1756 delay_cb->CreateGraphs(&send_times_s, &arrival_delay_ms,
1757 &corrected_arrival_delay_ms, &playout_delay_ms,
1758 &target_delay_ms);
1759 RTC_DCHECK_EQ(send_times_s.size(), arrival_delay_ms.size());
1760 RTC_DCHECK_EQ(send_times_s.size(), corrected_arrival_delay_ms.size());
1761 RTC_DCHECK_EQ(send_times_s.size(), playout_delay_ms.size());
1762 RTC_DCHECK_EQ(send_times_s.size(), target_delay_ms.size());
1763
1764 std::map<StreamId, TimeSeries> time_series_packet_arrival;
1765 std::map<StreamId, TimeSeries> time_series_relative_packet_arrival;
1766 std::map<StreamId, TimeSeries> time_series_play_time;
1767 std::map<StreamId, TimeSeries> time_series_target_time;
1768 float min_y_axis = 0.f;
1769 float max_y_axis = 0.f;
1770 const StreamId stream_id = incoming_audio_kv->first;
1771 for (size_t i = 0; i < send_times_s.size(); ++i) {
1772 time_series_packet_arrival[stream_id].points.emplace_back(
1773 TimeSeriesPoint(send_times_s[i], arrival_delay_ms[i]));
1774 time_series_relative_packet_arrival[stream_id].points.emplace_back(
1775 TimeSeriesPoint(send_times_s[i], corrected_arrival_delay_ms[i]));
1776 min_y_axis = std::min(min_y_axis, corrected_arrival_delay_ms[i]);
1777 max_y_axis = std::max(max_y_axis, corrected_arrival_delay_ms[i]);
1778 if (playout_delay_ms[i]) {
1779 time_series_play_time[stream_id].points.emplace_back(
1780 TimeSeriesPoint(send_times_s[i], *playout_delay_ms[i]));
1781 min_y_axis = std::min(min_y_axis, *playout_delay_ms[i]);
1782 max_y_axis = std::max(max_y_axis, *playout_delay_ms[i]);
1783 }
1784 if (target_delay_ms[i]) {
1785 time_series_target_time[stream_id].points.emplace_back(
1786 TimeSeriesPoint(send_times_s[i], *target_delay_ms[i]));
1787 min_y_axis = std::min(min_y_axis, *target_delay_ms[i]);
1788 max_y_axis = std::max(max_y_axis, *target_delay_ms[i]);
1789 }
1790 }
1791
1792 // This code is adapted for a single stream. The creation of the streams above
1793 // guarantee that no more than one steam is included. If multiple streams are
1794 // to be plotted, they should likely be given distinct labels below.
1795 RTC_DCHECK_EQ(time_series_relative_packet_arrival.size(), 1);
1796 for (auto& series : time_series_relative_packet_arrival) {
1797 series.second.label = "Relative packet arrival delay";
1798 series.second.style = LINE_GRAPH;
1799 plot->AppendTimeSeries(std::move(series.second));
1800 }
1801 RTC_DCHECK_EQ(time_series_play_time.size(), 1);
1802 for (auto& series : time_series_play_time) {
1803 series.second.label = "Playout delay";
1804 series.second.style = LINE_GRAPH;
1805 plot->AppendTimeSeries(std::move(series.second));
1806 }
1807 RTC_DCHECK_EQ(time_series_target_time.size(), 1);
1808 for (auto& series : time_series_target_time) {
1809 series.second.label = "Target delay";
1810 series.second.style = LINE_DOT_GRAPH;
1811 plot->AppendTimeSeries(std::move(series.second));
1812 }
1813
1814 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1815 plot->SetYAxis(min_y_axis, max_y_axis, "Relative delay (ms)", kBottomMargin,
1816 kTopMargin);
1817 plot->SetTitle("NetEq timing");
1818}
terelius54ce6802016-07-13 06:44:41 -07001819} // namespace plotting
1820} // namespace webrtc