blob: 2620a3af6be9d855791f3229e323a711e2615a21 [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
kjellanderd2b63cf2017-06-30 03:04:59 -070011#include "webrtc/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
ossuf515ab82016-12-07 04:52:58 -080020#include "webrtc/call/audio_receive_stream.h"
21#include "webrtc/call/audio_send_stream.h"
22#include "webrtc/call/call.h"
terelius54ce6802016-07-13 06:44:41 -070023#include "webrtc/common_types.h"
henrik.lundin3c938fc2017-06-14 06:09:58 -070024#include "webrtc/modules/audio_coding/neteq/tools/audio_sink.h"
25#include "webrtc/modules/audio_coding/neteq/tools/fake_decode_from_file.h"
26#include "webrtc/modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
27#include "webrtc/modules/audio_coding/neteq/tools/neteq_replacement_input.h"
28#include "webrtc/modules/audio_coding/neteq/tools/neteq_test.h"
29#include "webrtc/modules/audio_coding/neteq/tools/resample_input_audio_file.h"
Stefan Holmer13181032016-07-29 14:48:54 +020030#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
terelius4c9b4af2017-01-30 08:44:51 -080031#include "webrtc/modules/include/module_common_types.h"
terelius54ce6802016-07-13 06:44:41 -070032#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
33#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
danilchapbf369fe2016-10-07 07:39:54 -070034#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
stefane372d3c2017-02-02 08:04:18 -080035#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
terelius2c8e8a32017-06-02 01:29:48 -070036#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/remb.h"
stefane372d3c2017-02-02 08:04:18 -080037#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
Stefan Holmer13181032016-07-29 14:48:54 +020038#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
ossuf515ab82016-12-07 04:52:58 -080039#include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h"
40#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020041#include "webrtc/rtc_base/checks.h"
42#include "webrtc/rtc_base/format_macros.h"
43#include "webrtc/rtc_base/logging.h"
44#include "webrtc/rtc_base/ptr_util.h"
45#include "webrtc/rtc_base/rate_statistics.h"
terelius54ce6802016-07-13 06:44:41 -070046#include "webrtc/video_receive_stream.h"
47#include "webrtc/video_send_stream.h"
48
tereliusdc35dcd2016-08-01 12:03:27 -070049namespace webrtc {
50namespace plotting {
51
terelius54ce6802016-07-13 06:44:41 -070052namespace {
53
elad.alonec304f92017-03-08 05:03:53 -080054void SortPacketFeedbackVector(std::vector<PacketFeedback>* vec) {
55 auto pred = [](const PacketFeedback& packet_feedback) {
56 return packet_feedback.arrival_time_ms == PacketFeedback::kNotReceived;
57 };
58 vec->erase(std::remove_if(vec->begin(), vec->end(), pred), vec->end());
59 std::sort(vec->begin(), vec->end(), PacketFeedbackComparator());
60}
61
terelius54ce6802016-07-13 06:44:41 -070062std::string SsrcToString(uint32_t ssrc) {
63 std::stringstream ss;
64 ss << "SSRC " << ssrc;
65 return ss.str();
66}
67
68// Checks whether an SSRC is contained in the list of desired SSRCs.
69// Note that an empty SSRC list matches every SSRC.
70bool MatchingSsrc(uint32_t ssrc, const std::vector<uint32_t>& desired_ssrc) {
71 if (desired_ssrc.size() == 0)
72 return true;
73 return std::find(desired_ssrc.begin(), desired_ssrc.end(), ssrc) !=
74 desired_ssrc.end();
75}
76
77double AbsSendTimeToMicroseconds(int64_t abs_send_time) {
78 // The timestamp is a fixed point representation with 6 bits for seconds
79 // and 18 bits for fractions of a second. Thus, we divide by 2^18 to get the
80 // time in seconds and then multiply by 1000000 to convert to microseconds.
81 static constexpr double kTimestampToMicroSec =
tereliusccbbf8d2016-08-10 07:34:28 -070082 1000000.0 / static_cast<double>(1ul << 18);
terelius54ce6802016-07-13 06:44:41 -070083 return abs_send_time * kTimestampToMicroSec;
84}
85
86// Computes the difference |later| - |earlier| where |later| and |earlier|
87// are counters that wrap at |modulus|. The difference is chosen to have the
88// least absolute value. For example if |modulus| is 8, then the difference will
89// be chosen in the range [-3, 4]. If |modulus| is 9, then the difference will
90// be in [-4, 4].
91int64_t WrappingDifference(uint32_t later, uint32_t earlier, int64_t modulus) {
92 RTC_DCHECK_LE(1, modulus);
93 RTC_DCHECK_LT(later, modulus);
94 RTC_DCHECK_LT(earlier, modulus);
95 int64_t difference =
96 static_cast<int64_t>(later) - static_cast<int64_t>(earlier);
97 int64_t max_difference = modulus / 2;
98 int64_t min_difference = max_difference - modulus + 1;
99 if (difference > max_difference) {
100 difference -= modulus;
101 }
102 if (difference < min_difference) {
103 difference += modulus;
104 }
terelius6addf492016-08-23 17:34:07 -0700105 if (difference > max_difference / 2 || difference < min_difference / 2) {
106 LOG(LS_WARNING) << "Difference between" << later << " and " << earlier
107 << " expected to be in the range (" << min_difference / 2
108 << "," << max_difference / 2 << ") but is " << difference
109 << ". Correct unwrapping is uncertain.";
110 }
terelius54ce6802016-07-13 06:44:41 -0700111 return difference;
112}
113
ivocaac9d6f2016-09-22 07:01:47 -0700114// Return default values for header extensions, to use on streams without stored
115// mapping data. Currently this only applies to audio streams, since the mapping
116// is not stored in the event log.
117// TODO(ivoc): Remove this once this mapping is stored in the event log for
118// audio streams. Tracking bug: webrtc:6399
119webrtc::RtpHeaderExtensionMap GetDefaultHeaderExtensionMap() {
120 webrtc::RtpHeaderExtensionMap default_map;
danilchap4aecc582016-11-15 09:21:00 -0800121 default_map.Register<AudioLevel>(webrtc::RtpExtension::kAudioLevelDefaultId);
terelius007d5622017-08-08 05:40:26 -0700122 default_map.Register<TransmissionOffset>(
123 webrtc::RtpExtension::kTimestampOffsetDefaultId);
danilchap4aecc582016-11-15 09:21:00 -0800124 default_map.Register<AbsoluteSendTime>(
ivocaac9d6f2016-09-22 07:01:47 -0700125 webrtc::RtpExtension::kAbsSendTimeDefaultId);
terelius007d5622017-08-08 05:40:26 -0700126 default_map.Register<VideoOrientation>(
127 webrtc::RtpExtension::kVideoRotationDefaultId);
128 default_map.Register<VideoContentTypeExtension>(
129 webrtc::RtpExtension::kVideoContentTypeDefaultId);
130 default_map.Register<VideoTimingExtension>(
131 webrtc::RtpExtension::kVideoTimingDefaultId);
132 default_map.Register<TransportSequenceNumber>(
133 webrtc::RtpExtension::kTransportSequenceNumberDefaultId);
134 default_map.Register<PlayoutDelayLimits>(
135 webrtc::RtpExtension::kPlayoutDelayDefaultId);
ivocaac9d6f2016-09-22 07:01:47 -0700136 return default_map;
137}
138
tereliusdc35dcd2016-08-01 12:03:27 -0700139constexpr float kLeftMargin = 0.01f;
140constexpr float kRightMargin = 0.02f;
141constexpr float kBottomMargin = 0.02f;
142constexpr float kTopMargin = 0.05f;
terelius54ce6802016-07-13 06:44:41 -0700143
terelius53dc23c2017-03-13 05:24:05 -0700144rtc::Optional<double> NetworkDelayDiff_AbsSendTime(
145 const LoggedRtpPacket& old_packet,
146 const LoggedRtpPacket& new_packet) {
147 if (old_packet.header.extension.hasAbsoluteSendTime &&
148 new_packet.header.extension.hasAbsoluteSendTime) {
149 int64_t send_time_diff = WrappingDifference(
150 new_packet.header.extension.absoluteSendTime,
151 old_packet.header.extension.absoluteSendTime, 1ul << 24);
152 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
153 double delay_change_us =
154 recv_time_diff - AbsSendTimeToMicroseconds(send_time_diff);
155 return rtc::Optional<double>(delay_change_us / 1000);
156 } else {
157 return rtc::Optional<double>();
terelius6addf492016-08-23 17:34:07 -0700158 }
159}
160
terelius53dc23c2017-03-13 05:24:05 -0700161rtc::Optional<double> NetworkDelayDiff_CaptureTime(
162 const LoggedRtpPacket& old_packet,
163 const LoggedRtpPacket& new_packet) {
164 int64_t send_time_diff = WrappingDifference(
165 new_packet.header.timestamp, old_packet.header.timestamp, 1ull << 32);
166 int64_t recv_time_diff = new_packet.timestamp - old_packet.timestamp;
167
168 const double kVideoSampleRate = 90000;
169 // TODO(terelius): We treat all streams as video for now, even though
170 // audio might be sampled at e.g. 16kHz, because it is really difficult to
171 // figure out the true sampling rate of a stream. The effect is that the
172 // delay will be scaled incorrectly for non-video streams.
173
174 double delay_change =
175 static_cast<double>(recv_time_diff) / 1000 -
176 static_cast<double>(send_time_diff) / kVideoSampleRate * 1000;
177 if (delay_change < -10000 || 10000 < delay_change) {
178 LOG(LS_WARNING) << "Very large delay change. Timestamps correct?";
179 LOG(LS_WARNING) << "Old capture time " << old_packet.header.timestamp
180 << ", received time " << old_packet.timestamp;
181 LOG(LS_WARNING) << "New capture time " << new_packet.header.timestamp
182 << ", received time " << new_packet.timestamp;
183 LOG(LS_WARNING) << "Receive time difference " << recv_time_diff << " = "
184 << static_cast<double>(recv_time_diff) / 1000000 << "s";
185 LOG(LS_WARNING) << "Send time difference " << send_time_diff << " = "
186 << static_cast<double>(send_time_diff) / kVideoSampleRate
187 << "s";
188 }
189 return rtc::Optional<double>(delay_change);
190}
191
192// For each element in data, use |get_y()| to extract a y-coordinate and
193// store the result in a TimeSeries.
194template <typename DataType>
195void ProcessPoints(
196 rtc::FunctionView<rtc::Optional<float>(const DataType&)> get_y,
197 const std::vector<DataType>& data,
198 uint64_t begin_time,
199 TimeSeries* result) {
200 for (size_t i = 0; i < data.size(); i++) {
201 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
202 rtc::Optional<float> y = get_y(data[i]);
203 if (y)
204 result->points.emplace_back(x, *y);
205 }
206}
207
208// For each pair of adjacent elements in |data|, use |get_y| to extract a
terelius6addf492016-08-23 17:34:07 -0700209// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
210// will be the time of the second element in the pair.
terelius53dc23c2017-03-13 05:24:05 -0700211template <typename DataType, typename ResultType>
212void ProcessPairs(
213 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
214 const DataType&)> get_y,
215 const std::vector<DataType>& data,
216 uint64_t begin_time,
217 TimeSeries* result) {
tereliusccbbf8d2016-08-10 07:34:28 -0700218 for (size_t i = 1; i < data.size(); i++) {
219 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
terelius53dc23c2017-03-13 05:24:05 -0700220 rtc::Optional<ResultType> y = get_y(data[i - 1], data[i]);
221 if (y)
222 result->points.emplace_back(x, static_cast<float>(*y));
223 }
224}
225
226// For each element in data, use |extract()| to extract a y-coordinate and
227// store the result in a TimeSeries.
228template <typename DataType, typename ResultType>
229void AccumulatePoints(
230 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> extract,
231 const std::vector<DataType>& data,
232 uint64_t begin_time,
233 TimeSeries* result) {
234 ResultType sum = 0;
235 for (size_t i = 0; i < data.size(); i++) {
236 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
237 rtc::Optional<ResultType> y = extract(data[i]);
238 if (y) {
239 sum += *y;
240 result->points.emplace_back(x, static_cast<float>(sum));
241 }
242 }
243}
244
245// For each pair of adjacent elements in |data|, use |extract()| to extract a
246// y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
247// will be the time of the second element in the pair.
248template <typename DataType, typename ResultType>
249void AccumulatePairs(
250 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&,
251 const DataType&)> extract,
252 const std::vector<DataType>& data,
253 uint64_t begin_time,
254 TimeSeries* result) {
255 ResultType sum = 0;
256 for (size_t i = 1; i < data.size(); i++) {
257 float x = static_cast<float>(data[i].timestamp - begin_time) / 1000000;
258 rtc::Optional<ResultType> y = extract(data[i - 1], data[i]);
259 if (y)
260 sum += *y;
261 result->points.emplace_back(x, static_cast<float>(sum));
tereliusccbbf8d2016-08-10 07:34:28 -0700262 }
263}
264
terelius6addf492016-08-23 17:34:07 -0700265// Calculates a moving average of |data| and stores the result in a TimeSeries.
266// A data point is generated every |step| microseconds from |begin_time|
267// to |end_time|. The value of each data point is the average of the data
268// during the preceeding |window_duration_us| microseconds.
terelius53dc23c2017-03-13 05:24:05 -0700269template <typename DataType, typename ResultType>
270void MovingAverage(
271 rtc::FunctionView<rtc::Optional<ResultType>(const DataType&)> extract,
272 const std::vector<DataType>& data,
273 uint64_t begin_time,
274 uint64_t end_time,
275 uint64_t window_duration_us,
276 uint64_t step,
277 webrtc::plotting::TimeSeries* result) {
terelius6addf492016-08-23 17:34:07 -0700278 size_t window_index_begin = 0;
279 size_t window_index_end = 0;
terelius53dc23c2017-03-13 05:24:05 -0700280 ResultType sum_in_window = 0;
terelius6addf492016-08-23 17:34:07 -0700281
282 for (uint64_t t = begin_time; t < end_time + step; t += step) {
283 while (window_index_end < data.size() &&
284 data[window_index_end].timestamp < t) {
terelius53dc23c2017-03-13 05:24:05 -0700285 rtc::Optional<ResultType> value = extract(data[window_index_end]);
286 if (value)
287 sum_in_window += *value;
terelius6addf492016-08-23 17:34:07 -0700288 ++window_index_end;
289 }
290 while (window_index_begin < data.size() &&
291 data[window_index_begin].timestamp < t - window_duration_us) {
terelius53dc23c2017-03-13 05:24:05 -0700292 rtc::Optional<ResultType> value = extract(data[window_index_begin]);
293 if (value)
294 sum_in_window -= *value;
terelius6addf492016-08-23 17:34:07 -0700295 ++window_index_begin;
296 }
297 float window_duration_s = static_cast<float>(window_duration_us) / 1000000;
298 float x = static_cast<float>(t - begin_time) / 1000000;
terelius53dc23c2017-03-13 05:24:05 -0700299 float y = sum_in_window / window_duration_s;
terelius6addf492016-08-23 17:34:07 -0700300 result->points.emplace_back(x, y);
301 }
302}
303
terelius54ce6802016-07-13 06:44:41 -0700304} // namespace
305
terelius54ce6802016-07-13 06:44:41 -0700306EventLogAnalyzer::EventLogAnalyzer(const ParsedRtcEventLog& log)
307 : parsed_log_(log), window_duration_(250000), step_(10000) {
308 uint64_t first_timestamp = std::numeric_limits<uint64_t>::max();
309 uint64_t last_timestamp = std::numeric_limits<uint64_t>::min();
terelius88e64e52016-07-19 01:51:06 -0700310
terelius88e64e52016-07-19 01:51:06 -0700311 PacketDirection direction;
terelius88e64e52016-07-19 01:51:06 -0700312 uint8_t header[IP_PACKET_SIZE];
313 size_t header_length;
314 size_t total_length;
315
perkjbbbad6d2017-05-19 06:30:28 -0700316 uint8_t last_incoming_rtcp_packet[IP_PACKET_SIZE];
317 uint8_t last_incoming_rtcp_packet_length = 0;
318
ivocaac9d6f2016-09-22 07:01:47 -0700319 // Make a default extension map for streams without configuration information.
320 // TODO(ivoc): Once configuration of audio streams is stored in the event log,
321 // this can be removed. Tracking bug: webrtc:6399
322 RtpHeaderExtensionMap default_extension_map = GetDefaultHeaderExtensionMap();
323
henrik.lundin3c938fc2017-06-14 06:09:58 -0700324 rtc::Optional<uint64_t> last_log_start;
325
terelius54ce6802016-07-13 06:44:41 -0700326 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
327 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
terelius88e64e52016-07-19 01:51:06 -0700328 if (event_type != ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT &&
329 event_type != ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT &&
330 event_type != ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT &&
terelius88c1d2b2016-08-01 05:20:33 -0700331 event_type != ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT &&
332 event_type != ParsedRtcEventLog::LOG_START &&
333 event_type != ParsedRtcEventLog::LOG_END) {
terelius88e64e52016-07-19 01:51:06 -0700334 uint64_t timestamp = parsed_log_.GetTimestamp(i);
335 first_timestamp = std::min(first_timestamp, timestamp);
336 last_timestamp = std::max(last_timestamp, timestamp);
337 }
338
339 switch (parsed_log_.GetEventType(i)) {
340 case ParsedRtcEventLog::VIDEO_RECEIVER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700341 rtclog::StreamConfig config = parsed_log_.GetVideoReceiveConfig(i);
perkj09e71da2017-05-22 03:26:49 -0700342 StreamId stream(config.remote_ssrc, kIncomingPacket);
terelius0740a202016-08-08 10:21:04 -0700343 video_ssrcs_.insert(stream);
perkj09e71da2017-05-22 03:26:49 -0700344 StreamId rtx_stream(config.rtx_ssrc, kIncomingPacket);
brandtr14742122017-01-27 04:53:07 -0800345 video_ssrcs_.insert(rtx_stream);
346 rtx_ssrcs_.insert(rtx_stream);
terelius88e64e52016-07-19 01:51:06 -0700347 break;
348 }
349 case ParsedRtcEventLog::VIDEO_SENDER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700350 std::vector<rtclog::StreamConfig> configs =
351 parsed_log_.GetVideoSendConfig(i);
terelius405f90c2017-06-01 03:50:31 -0700352 for (const auto& config : configs) {
353 StreamId stream(config.local_ssrc, kOutgoingPacket);
terelius8fbc7652017-05-31 02:03:16 -0700354 video_ssrcs_.insert(stream);
terelius405f90c2017-06-01 03:50:31 -0700355 StreamId rtx_stream(config.rtx_ssrc, kOutgoingPacket);
terelius8fbc7652017-05-31 02:03:16 -0700356 video_ssrcs_.insert(rtx_stream);
357 rtx_ssrcs_.insert(rtx_stream);
358 }
terelius88e64e52016-07-19 01:51:06 -0700359 break;
360 }
361 case ParsedRtcEventLog::AUDIO_RECEIVER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700362 rtclog::StreamConfig config = parsed_log_.GetAudioReceiveConfig(i);
perkjac8f52d2017-05-22 09:36:28 -0700363 StreamId stream(config.remote_ssrc, kIncomingPacket);
ivoce0928d82016-10-10 05:12:51 -0700364 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700365 break;
366 }
367 case ParsedRtcEventLog::AUDIO_SENDER_CONFIG_EVENT: {
terelius8fbc7652017-05-31 02:03:16 -0700368 rtclog::StreamConfig config = parsed_log_.GetAudioSendConfig(i);
perkjf4726992017-05-22 10:12:26 -0700369 StreamId stream(config.local_ssrc, kOutgoingPacket);
ivoce0928d82016-10-10 05:12:51 -0700370 audio_ssrcs_.insert(stream);
terelius88e64e52016-07-19 01:51:06 -0700371 break;
372 }
373 case ParsedRtcEventLog::RTP_EVENT: {
ilnika8e781a2017-06-12 01:02:46 -0700374 RtpHeaderExtensionMap* extension_map = parsed_log_.GetRtpHeader(
375 i, &direction, header, &header_length, &total_length);
terelius88e64e52016-07-19 01:51:06 -0700376 RtpUtility::RtpHeaderParser rtp_parser(header, header_length);
377 RTPHeader parsed_header;
ilnika8e781a2017-06-12 01:02:46 -0700378 if (extension_map != nullptr) {
terelius88e64e52016-07-19 01:51:06 -0700379 rtp_parser.Parse(&parsed_header, extension_map);
ivocaac9d6f2016-09-22 07:01:47 -0700380 } else {
381 // Use the default extension map.
382 // TODO(ivoc): Once configuration of audio streams is stored in the
383 // event log, this can be removed.
384 // Tracking bug: webrtc:6399
385 rtp_parser.Parse(&parsed_header, &default_extension_map);
terelius88e64e52016-07-19 01:51:06 -0700386 }
387 uint64_t timestamp = parsed_log_.GetTimestamp(i);
ilnika8e781a2017-06-12 01:02:46 -0700388 StreamId stream(parsed_header.ssrc, direction);
terelius88e64e52016-07-19 01:51:06 -0700389 rtp_packets_[stream].push_back(
Stefan Holmer13181032016-07-29 14:48:54 +0200390 LoggedRtpPacket(timestamp, parsed_header, total_length));
terelius88e64e52016-07-19 01:51:06 -0700391 break;
392 }
393 case ParsedRtcEventLog::RTCP_EVENT: {
Stefan Holmer13181032016-07-29 14:48:54 +0200394 uint8_t packet[IP_PACKET_SIZE];
perkj77cd58e2017-05-30 03:52:10 -0700395 parsed_log_.GetRtcpPacket(i, &direction, packet, &total_length);
perkjbbbad6d2017-05-19 06:30:28 -0700396 // Currently incoming RTCP packets are logged twice, both for audio and
397 // video. Only act on one of them. Compare against the previous parsed
398 // incoming RTCP packet.
399 if (direction == webrtc::kIncomingPacket) {
400 RTC_CHECK_LE(total_length, IP_PACKET_SIZE);
401 if (total_length == last_incoming_rtcp_packet_length &&
402 memcmp(last_incoming_rtcp_packet, packet, total_length) == 0) {
403 continue;
404 } else {
405 memcpy(last_incoming_rtcp_packet, packet, total_length);
406 last_incoming_rtcp_packet_length = total_length;
407 }
408 }
409 rtcp::CommonHeader header;
410 const uint8_t* packet_end = packet + total_length;
411 for (const uint8_t* block = packet; block < packet_end;
412 block = header.NextPacket()) {
413 RTC_CHECK(header.Parse(block, packet_end - block));
414 if (header.type() == rtcp::TransportFeedback::kPacketType &&
415 header.fmt() == rtcp::TransportFeedback::kFeedbackMessageType) {
416 std::unique_ptr<rtcp::TransportFeedback> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700417 rtc::MakeUnique<rtcp::TransportFeedback>());
perkjbbbad6d2017-05-19 06:30:28 -0700418 if (rtcp_packet->Parse(header)) {
419 uint32_t ssrc = rtcp_packet->sender_ssrc();
420 StreamId stream(ssrc, direction);
421 uint64_t timestamp = parsed_log_.GetTimestamp(i);
422 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
423 timestamp, kRtcpTransportFeedback, std::move(rtcp_packet)));
424 }
425 } else if (header.type() == rtcp::SenderReport::kPacketType) {
426 std::unique_ptr<rtcp::SenderReport> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700427 rtc::MakeUnique<rtcp::SenderReport>());
perkjbbbad6d2017-05-19 06:30:28 -0700428 if (rtcp_packet->Parse(header)) {
429 uint32_t ssrc = rtcp_packet->sender_ssrc();
430 StreamId stream(ssrc, direction);
431 uint64_t timestamp = parsed_log_.GetTimestamp(i);
432 rtcp_packets_[stream].push_back(
433 LoggedRtcpPacket(timestamp, kRtcpSr, std::move(rtcp_packet)));
434 }
435 } else if (header.type() == rtcp::ReceiverReport::kPacketType) {
436 std::unique_ptr<rtcp::ReceiverReport> rtcp_packet(
terelius2c8e8a32017-06-02 01:29:48 -0700437 rtc::MakeUnique<rtcp::ReceiverReport>());
perkjbbbad6d2017-05-19 06:30:28 -0700438 if (rtcp_packet->Parse(header)) {
439 uint32_t ssrc = rtcp_packet->sender_ssrc();
440 StreamId stream(ssrc, direction);
441 uint64_t timestamp = parsed_log_.GetTimestamp(i);
442 rtcp_packets_[stream].push_back(
443 LoggedRtcpPacket(timestamp, kRtcpRr, std::move(rtcp_packet)));
Stefan Holmer13181032016-07-29 14:48:54 +0200444 }
terelius2c8e8a32017-06-02 01:29:48 -0700445 } else if (header.type() == rtcp::Remb::kPacketType &&
446 header.fmt() == rtcp::Remb::kFeedbackMessageType) {
447 std::unique_ptr<rtcp::Remb> rtcp_packet(
448 rtc::MakeUnique<rtcp::Remb>());
449 if (rtcp_packet->Parse(header)) {
450 uint32_t ssrc = rtcp_packet->sender_ssrc();
451 StreamId stream(ssrc, direction);
452 uint64_t timestamp = parsed_log_.GetTimestamp(i);
453 rtcp_packets_[stream].push_back(LoggedRtcpPacket(
454 timestamp, kRtcpRemb, std::move(rtcp_packet)));
455 }
Stefan Holmer13181032016-07-29 14:48:54 +0200456 }
Stefan Holmer13181032016-07-29 14:48:54 +0200457 }
terelius88e64e52016-07-19 01:51:06 -0700458 break;
459 }
460 case ParsedRtcEventLog::LOG_START: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700461 if (last_log_start) {
462 // A LOG_END event was missing. Use last_timestamp.
463 RTC_DCHECK_GE(last_timestamp, *last_log_start);
464 log_segments_.push_back(
465 std::make_pair(*last_log_start, last_timestamp));
466 }
467 last_log_start = rtc::Optional<uint64_t>(parsed_log_.GetTimestamp(i));
terelius88e64e52016-07-19 01:51:06 -0700468 break;
469 }
470 case ParsedRtcEventLog::LOG_END: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700471 RTC_DCHECK(last_log_start);
472 log_segments_.push_back(
473 std::make_pair(*last_log_start, parsed_log_.GetTimestamp(i)));
474 last_log_start.reset();
terelius88e64e52016-07-19 01:51:06 -0700475 break;
476 }
terelius424e6cf2017-02-20 05:14:41 -0800477 case ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT: {
henrik.lundin3c938fc2017-06-14 06:09:58 -0700478 uint32_t this_ssrc;
479 parsed_log_.GetAudioPlayout(i, &this_ssrc);
480 audio_playout_events_[this_ssrc].push_back(parsed_log_.GetTimestamp(i));
terelius424e6cf2017-02-20 05:14:41 -0800481 break;
482 }
483 case ParsedRtcEventLog::LOSS_BASED_BWE_UPDATE: {
484 LossBasedBweUpdate bwe_update;
terelius8058e582016-07-25 01:32:41 -0700485 bwe_update.timestamp = parsed_log_.GetTimestamp(i);
terelius424e6cf2017-02-20 05:14:41 -0800486 parsed_log_.GetLossBasedBweUpdate(i, &bwe_update.new_bitrate,
487 &bwe_update.fraction_loss,
488 &bwe_update.expected_packets);
terelius8058e582016-07-25 01:32:41 -0700489 bwe_loss_updates_.push_back(bwe_update);
terelius88e64e52016-07-19 01:51:06 -0700490 break;
491 }
terelius424e6cf2017-02-20 05:14:41 -0800492 case ParsedRtcEventLog::DELAY_BASED_BWE_UPDATE: {
philipel10fc0e62017-04-11 01:50:23 -0700493 bwe_delay_updates_.push_back(parsed_log_.GetDelayBasedBweUpdate(i));
terelius424e6cf2017-02-20 05:14:41 -0800494 break;
495 }
minyue4b7c9522017-01-24 04:54:59 -0800496 case ParsedRtcEventLog::AUDIO_NETWORK_ADAPTATION_EVENT: {
michaelt6e5b2192017-02-22 07:33:27 -0800497 AudioNetworkAdaptationEvent ana_event;
498 ana_event.timestamp = parsed_log_.GetTimestamp(i);
499 parsed_log_.GetAudioNetworkAdaptation(i, &ana_event.config);
500 audio_network_adaptation_events_.push_back(ana_event);
minyue4b7c9522017-01-24 04:54:59 -0800501 break;
502 }
philipel32d00102017-02-27 02:18:46 -0800503 case ParsedRtcEventLog::BWE_PROBE_CLUSTER_CREATED_EVENT: {
philipele127e7a2017-03-29 16:28:53 +0200504 bwe_probe_cluster_created_events_.push_back(
505 parsed_log_.GetBweProbeClusterCreated(i));
philipel32d00102017-02-27 02:18:46 -0800506 break;
507 }
508 case ParsedRtcEventLog::BWE_PROBE_RESULT_EVENT: {
philipele127e7a2017-03-29 16:28:53 +0200509 bwe_probe_result_events_.push_back(parsed_log_.GetBweProbeResult(i));
philipel32d00102017-02-27 02:18:46 -0800510 break;
511 }
terelius88e64e52016-07-19 01:51:06 -0700512 case ParsedRtcEventLog::UNKNOWN_EVENT: {
513 break;
514 }
515 }
terelius54ce6802016-07-13 06:44:41 -0700516 }
terelius88e64e52016-07-19 01:51:06 -0700517
terelius54ce6802016-07-13 06:44:41 -0700518 if (last_timestamp < first_timestamp) {
519 // No useful events in the log.
520 first_timestamp = last_timestamp = 0;
521 }
522 begin_time_ = first_timestamp;
523 end_time_ = last_timestamp;
tereliusdc35dcd2016-08-01 12:03:27 -0700524 call_duration_s_ = static_cast<float>(end_time_ - begin_time_) / 1000000;
henrik.lundin3c938fc2017-06-14 06:09:58 -0700525 if (last_log_start) {
526 // The log was missing the last LOG_END event. Fake it.
527 log_segments_.push_back(std::make_pair(*last_log_start, end_time_));
528 }
terelius54ce6802016-07-13 06:44:41 -0700529}
530
Stefan Holmer13181032016-07-29 14:48:54 +0200531class BitrateObserver : public CongestionController::Observer,
532 public RemoteBitrateObserver {
533 public:
534 BitrateObserver() : last_bitrate_bps_(0), bitrate_updated_(false) {}
535
minyue78b4d562016-11-30 04:47:39 -0800536 // TODO(minyue): remove this when old OnNetworkChanged is deprecated. See
537 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6796
538 using CongestionController::Observer::OnNetworkChanged;
539
Stefan Holmer13181032016-07-29 14:48:54 +0200540 void OnNetworkChanged(uint32_t bitrate_bps,
541 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800542 int64_t rtt_ms,
543 int64_t probing_interval_ms) override {
Stefan Holmer13181032016-07-29 14:48:54 +0200544 last_bitrate_bps_ = bitrate_bps;
545 bitrate_updated_ = true;
546 }
547
548 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
549 uint32_t bitrate) override {}
550
551 uint32_t last_bitrate_bps() const { return last_bitrate_bps_; }
552 bool GetAndResetBitrateUpdated() {
553 bool bitrate_updated = bitrate_updated_;
554 bitrate_updated_ = false;
555 return bitrate_updated;
556 }
557
558 private:
559 uint32_t last_bitrate_bps_;
560 bool bitrate_updated_;
561};
562
Stefan Holmer99f8e082016-09-09 13:37:50 +0200563bool EventLogAnalyzer::IsRtxSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700564 return rtx_ssrcs_.count(stream_id) == 1;
565}
566
Stefan Holmer99f8e082016-09-09 13:37:50 +0200567bool EventLogAnalyzer::IsVideoSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700568 return video_ssrcs_.count(stream_id) == 1;
569}
570
Stefan Holmer99f8e082016-09-09 13:37:50 +0200571bool EventLogAnalyzer::IsAudioSsrc(StreamId stream_id) const {
terelius0740a202016-08-08 10:21:04 -0700572 return audio_ssrcs_.count(stream_id) == 1;
573}
574
Stefan Holmer99f8e082016-09-09 13:37:50 +0200575std::string EventLogAnalyzer::GetStreamName(StreamId stream_id) const {
576 std::stringstream name;
577 if (IsAudioSsrc(stream_id)) {
578 name << "Audio ";
579 } else if (IsVideoSsrc(stream_id)) {
580 name << "Video ";
581 } else {
582 name << "Unknown ";
583 }
584 if (IsRtxSsrc(stream_id))
585 name << "RTX ";
ivocaac9d6f2016-09-22 07:01:47 -0700586 if (stream_id.GetDirection() == kIncomingPacket) {
587 name << "(In) ";
588 } else {
589 name << "(Out) ";
590 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200591 name << SsrcToString(stream_id.GetSsrc());
592 return name.str();
593}
594
terelius54ce6802016-07-13 06:44:41 -0700595void EventLogAnalyzer::CreatePacketGraph(PacketDirection desired_direction,
596 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700597 for (auto& kv : rtp_packets_) {
598 StreamId stream_id = kv.first;
599 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
600 // Filter on direction and SSRC.
601 if (stream_id.GetDirection() != desired_direction ||
602 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
603 continue;
terelius54ce6802016-07-13 06:44:41 -0700604 }
terelius54ce6802016-07-13 06:44:41 -0700605
terelius23c595a2017-03-15 01:59:12 -0700606 TimeSeries time_series(GetStreamName(stream_id), BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700607 ProcessPoints<LoggedRtpPacket>(
608 [](const LoggedRtpPacket& packet) -> rtc::Optional<float> {
609 return rtc::Optional<float>(packet.total_length);
610 },
611 packet_stream, begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700612 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700613 }
614
tereliusdc35dcd2016-08-01 12:03:27 -0700615 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
616 plot->SetSuggestedYAxis(0, 1, "Packet size (bytes)", kBottomMargin,
617 kTopMargin);
terelius54ce6802016-07-13 06:44:41 -0700618 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700619 plot->SetTitle("Incoming RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700620 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -0700621 plot->SetTitle("Outgoing RTP packets");
terelius54ce6802016-07-13 06:44:41 -0700622 }
623}
624
philipelccd74892016-09-05 02:46:25 -0700625template <typename T>
626void EventLogAnalyzer::CreateAccumulatedPacketsTimeSeries(
627 PacketDirection desired_direction,
628 Plot* plot,
629 const std::map<StreamId, std::vector<T>>& packets,
630 const std::string& label_prefix) {
631 for (auto& kv : packets) {
632 StreamId stream_id = kv.first;
633 const std::vector<T>& packet_stream = kv.second;
634 // Filter on direction and SSRC.
635 if (stream_id.GetDirection() != desired_direction ||
636 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
637 continue;
638 }
639
terelius23c595a2017-03-15 01:59:12 -0700640 std::string label = label_prefix + " " + GetStreamName(stream_id);
641 TimeSeries time_series(label, LINE_STEP_GRAPH);
philipelccd74892016-09-05 02:46:25 -0700642 for (size_t i = 0; i < packet_stream.size(); i++) {
643 float x = static_cast<float>(packet_stream[i].timestamp - begin_time_) /
644 1000000;
philipelccd74892016-09-05 02:46:25 -0700645 time_series.points.emplace_back(x, i + 1);
646 }
647
philipel35ba9bd2017-04-19 05:58:51 -0700648 plot->AppendTimeSeries(std::move(time_series));
philipelccd74892016-09-05 02:46:25 -0700649 }
650}
651
652void EventLogAnalyzer::CreateAccumulatedPacketsGraph(
653 PacketDirection desired_direction,
654 Plot* plot) {
655 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtp_packets_,
656 "RTP");
657 CreateAccumulatedPacketsTimeSeries(desired_direction, plot, rtcp_packets_,
658 "RTCP");
659
660 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
661 plot->SetSuggestedYAxis(0, 1, "Received Packets", kBottomMargin, kTopMargin);
662 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
663 plot->SetTitle("Accumulated Incoming RTP/RTCP packets");
664 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
665 plot->SetTitle("Accumulated Outgoing RTP/RTCP packets");
666 }
667}
668
terelius54ce6802016-07-13 06:44:41 -0700669// For each SSRC, plot the time between the consecutive playouts.
670void EventLogAnalyzer::CreatePlayoutGraph(Plot* plot) {
671 std::map<uint32_t, TimeSeries> time_series;
672 std::map<uint32_t, uint64_t> last_playout;
673
674 uint32_t ssrc;
terelius54ce6802016-07-13 06:44:41 -0700675
676 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
677 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
678 if (event_type == ParsedRtcEventLog::AUDIO_PLAYOUT_EVENT) {
679 parsed_log_.GetAudioPlayout(i, &ssrc);
680 uint64_t timestamp = parsed_log_.GetTimestamp(i);
681 if (MatchingSsrc(ssrc, desired_ssrc_)) {
682 float x = static_cast<float>(timestamp - begin_time_) / 1000000;
683 float y = static_cast<float>(timestamp - last_playout[ssrc]) / 1000;
684 if (time_series[ssrc].points.size() == 0) {
685 // There were no previusly logged playout for this SSRC.
686 // Generate a point, but place it on the x-axis.
687 y = 0;
688 }
terelius54ce6802016-07-13 06:44:41 -0700689 time_series[ssrc].points.push_back(TimeSeriesPoint(x, y));
690 last_playout[ssrc] = timestamp;
691 }
692 }
693 }
694
695 // Set labels and put in graph.
696 for (auto& kv : time_series) {
697 kv.second.label = SsrcToString(kv.first);
698 kv.second.style = BAR_GRAPH;
philipel35ba9bd2017-04-19 05:58:51 -0700699 plot->AppendTimeSeries(std::move(kv.second));
terelius54ce6802016-07-13 06:44:41 -0700700 }
701
tereliusdc35dcd2016-08-01 12:03:27 -0700702 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
703 plot->SetSuggestedYAxis(0, 1, "Time since last playout (ms)", kBottomMargin,
704 kTopMargin);
705 plot->SetTitle("Audio playout");
terelius54ce6802016-07-13 06:44:41 -0700706}
707
ivocaac9d6f2016-09-22 07:01:47 -0700708// For audio SSRCs, plot the audio level.
709void EventLogAnalyzer::CreateAudioLevelGraph(Plot* plot) {
710 std::map<StreamId, TimeSeries> time_series;
711
712 for (auto& kv : rtp_packets_) {
713 StreamId stream_id = kv.first;
714 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
715 // TODO(ivoc): When audio send/receive configs are stored in the event
716 // log, a check should be added here to only process audio
717 // streams. Tracking bug: webrtc:6399
718 for (auto& packet : packet_stream) {
719 if (packet.header.extension.hasAudioLevel) {
720 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
721 // The audio level is stored in -dBov (so e.g. -10 dBov is stored as 10)
722 // Here we convert it to dBov.
723 float y = static_cast<float>(-packet.header.extension.audioLevel);
724 time_series[stream_id].points.emplace_back(TimeSeriesPoint(x, y));
725 }
726 }
727 }
728
729 for (auto& series : time_series) {
730 series.second.label = GetStreamName(series.first);
731 series.second.style = LINE_GRAPH;
philipel35ba9bd2017-04-19 05:58:51 -0700732 plot->AppendTimeSeries(std::move(series.second));
ivocaac9d6f2016-09-22 07:01:47 -0700733 }
734
735 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
ivocbf676632016-11-24 08:30:34 -0800736 plot->SetYAxis(-127, 0, "Audio level (dBov)", kBottomMargin,
ivocaac9d6f2016-09-22 07:01:47 -0700737 kTopMargin);
738 plot->SetTitle("Audio level");
739}
740
terelius54ce6802016-07-13 06:44:41 -0700741// For each SSRC, plot the time between the consecutive playouts.
742void EventLogAnalyzer::CreateSequenceNumberGraph(Plot* plot) {
terelius6addf492016-08-23 17:34:07 -0700743 for (auto& kv : rtp_packets_) {
744 StreamId stream_id = kv.first;
745 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
746 // Filter on direction and SSRC.
747 if (stream_id.GetDirection() != kIncomingPacket ||
748 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
749 continue;
terelius54ce6802016-07-13 06:44:41 -0700750 }
terelius54ce6802016-07-13 06:44:41 -0700751
terelius23c595a2017-03-15 01:59:12 -0700752 TimeSeries time_series(GetStreamName(stream_id), BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700753 ProcessPairs<LoggedRtpPacket, float>(
754 [](const LoggedRtpPacket& old_packet,
755 const LoggedRtpPacket& new_packet) {
756 int64_t diff =
757 WrappingDifference(new_packet.header.sequenceNumber,
758 old_packet.header.sequenceNumber, 1ul << 16);
759 return rtc::Optional<float>(diff);
760 },
761 packet_stream, begin_time_, &time_series);
philipel35ba9bd2017-04-19 05:58:51 -0700762 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -0700763 }
764
tereliusdc35dcd2016-08-01 12:03:27 -0700765 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
766 plot->SetSuggestedYAxis(0, 1, "Difference since last packet", kBottomMargin,
767 kTopMargin);
768 plot->SetTitle("Sequence number");
terelius54ce6802016-07-13 06:44:41 -0700769}
770
Stefan Holmer99f8e082016-09-09 13:37:50 +0200771void EventLogAnalyzer::CreateIncomingPacketLossGraph(Plot* plot) {
772 for (auto& kv : rtp_packets_) {
773 StreamId stream_id = kv.first;
774 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
775 // Filter on direction and SSRC.
776 if (stream_id.GetDirection() != kIncomingPacket ||
terelius4c9b4af2017-01-30 08:44:51 -0800777 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
778 packet_stream.size() == 0) {
Stefan Holmer99f8e082016-09-09 13:37:50 +0200779 continue;
780 }
781
terelius23c595a2017-03-15 01:59:12 -0700782 TimeSeries time_series(GetStreamName(stream_id), LINE_DOT_GRAPH);
Stefan Holmer99f8e082016-09-09 13:37:50 +0200783 const uint64_t kWindowUs = 1000000;
terelius4c9b4af2017-01-30 08:44:51 -0800784 const uint64_t kStep = 1000000;
785 SequenceNumberUnwrapper unwrapper_;
786 SequenceNumberUnwrapper prior_unwrapper_;
787 size_t window_index_begin = 0;
788 size_t window_index_end = 0;
789 int64_t highest_seq_number =
790 unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
791 int64_t highest_prior_seq_number =
792 prior_unwrapper_.Unwrap(packet_stream[0].header.sequenceNumber) - 1;
793
794 for (uint64_t t = begin_time_; t < end_time_ + kStep; t += kStep) {
795 while (window_index_end < packet_stream.size() &&
796 packet_stream[window_index_end].timestamp < t) {
797 int64_t sequence_number = unwrapper_.Unwrap(
798 packet_stream[window_index_end].header.sequenceNumber);
799 highest_seq_number = std::max(highest_seq_number, sequence_number);
800 ++window_index_end;
Stefan Holmer99f8e082016-09-09 13:37:50 +0200801 }
terelius4c9b4af2017-01-30 08:44:51 -0800802 while (window_index_begin < packet_stream.size() &&
803 packet_stream[window_index_begin].timestamp < t - kWindowUs) {
804 int64_t sequence_number = prior_unwrapper_.Unwrap(
805 packet_stream[window_index_begin].header.sequenceNumber);
806 highest_prior_seq_number =
807 std::max(highest_prior_seq_number, sequence_number);
808 ++window_index_begin;
809 }
810 float x = static_cast<float>(t - begin_time_) / 1000000;
811 int64_t expected_packets = highest_seq_number - highest_prior_seq_number;
812 if (expected_packets > 0) {
813 int64_t received_packets = window_index_end - window_index_begin;
814 int64_t lost_packets = expected_packets - received_packets;
815 float y = static_cast<float>(lost_packets) / expected_packets * 100;
816 time_series.points.emplace_back(x, y);
817 }
Stefan Holmer99f8e082016-09-09 13:37:50 +0200818 }
philipel35ba9bd2017-04-19 05:58:51 -0700819 plot->AppendTimeSeries(std::move(time_series));
Stefan Holmer99f8e082016-09-09 13:37:50 +0200820 }
821
822 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
823 plot->SetSuggestedYAxis(0, 1, "Estimated loss rate (%)", kBottomMargin,
824 kTopMargin);
825 plot->SetTitle("Estimated incoming loss rate");
826}
827
terelius2ee076d2017-08-15 02:04:02 -0700828void EventLogAnalyzer::CreateIncomingDelayDeltaGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700829 for (auto& kv : rtp_packets_) {
830 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700831 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700832 // Filter on direction and SSRC.
833 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200834 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
835 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
836 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700837 continue;
838 }
terelius54ce6802016-07-13 06:44:41 -0700839
terelius23c595a2017-03-15 01:59:12 -0700840 TimeSeries capture_time_data(GetStreamName(stream_id) + " capture-time",
841 BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700842 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
843 packet_stream, begin_time_,
844 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700845 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700846
terelius23c595a2017-03-15 01:59:12 -0700847 TimeSeries send_time_data(GetStreamName(stream_id) + " abs-send-time",
848 BAR_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700849 ProcessPairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
850 packet_stream, begin_time_,
851 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700852 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700853 }
854
tereliusdc35dcd2016-08-01 12:03:27 -0700855 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
856 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
857 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700858 plot->SetTitle("Network latency difference between consecutive packets");
terelius54ce6802016-07-13 06:44:41 -0700859}
860
terelius2ee076d2017-08-15 02:04:02 -0700861void EventLogAnalyzer::CreateIncomingDelayGraph(Plot* plot) {
terelius88e64e52016-07-19 01:51:06 -0700862 for (auto& kv : rtp_packets_) {
863 StreamId stream_id = kv.first;
tereliusccbbf8d2016-08-10 07:34:28 -0700864 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
terelius88e64e52016-07-19 01:51:06 -0700865 // Filter on direction and SSRC.
866 if (stream_id.GetDirection() != kIncomingPacket ||
Stefan Holmer99f8e082016-09-09 13:37:50 +0200867 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_) ||
868 IsAudioSsrc(stream_id) || !IsVideoSsrc(stream_id) ||
869 IsRtxSsrc(stream_id)) {
terelius88e64e52016-07-19 01:51:06 -0700870 continue;
871 }
terelius54ce6802016-07-13 06:44:41 -0700872
terelius23c595a2017-03-15 01:59:12 -0700873 TimeSeries capture_time_data(GetStreamName(stream_id) + " capture-time",
874 LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700875 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_CaptureTime,
876 packet_stream, begin_time_,
877 &capture_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700878 plot->AppendTimeSeries(std::move(capture_time_data));
terelius88e64e52016-07-19 01:51:06 -0700879
terelius23c595a2017-03-15 01:59:12 -0700880 TimeSeries send_time_data(GetStreamName(stream_id) + " abs-send-time",
881 LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -0700882 AccumulatePairs<LoggedRtpPacket, double>(NetworkDelayDiff_AbsSendTime,
883 packet_stream, begin_time_,
884 &send_time_data);
philipel35ba9bd2017-04-19 05:58:51 -0700885 plot->AppendTimeSeries(std::move(send_time_data));
terelius54ce6802016-07-13 06:44:41 -0700886 }
887
tereliusdc35dcd2016-08-01 12:03:27 -0700888 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
889 plot->SetSuggestedYAxis(0, 1, "Latency change (ms)", kBottomMargin,
890 kTopMargin);
terelius2ee076d2017-08-15 02:04:02 -0700891 plot->SetTitle("Network latency (relative to first packet)");
terelius54ce6802016-07-13 06:44:41 -0700892}
893
tereliusf736d232016-08-04 10:00:11 -0700894// Plot the fraction of packets lost (as perceived by the loss-based BWE).
895void EventLogAnalyzer::CreateFractionLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -0700896 TimeSeries time_series("Fraction lost", LINE_DOT_GRAPH);
tereliusf736d232016-08-04 10:00:11 -0700897 for (auto& bwe_update : bwe_loss_updates_) {
898 float x = static_cast<float>(bwe_update.timestamp - begin_time_) / 1000000;
899 float y = static_cast<float>(bwe_update.fraction_loss) / 255 * 100;
philipel35ba9bd2017-04-19 05:58:51 -0700900 time_series.points.emplace_back(x, y);
tereliusf736d232016-08-04 10:00:11 -0700901 }
tereliusf736d232016-08-04 10:00:11 -0700902
903 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
904 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
905 kTopMargin);
906 plot->SetTitle("Reported packet loss");
philipel35ba9bd2017-04-19 05:58:51 -0700907 plot->AppendTimeSeries(std::move(time_series));
tereliusf736d232016-08-04 10:00:11 -0700908}
909
terelius54ce6802016-07-13 06:44:41 -0700910// Plot the total bandwidth used by all RTP streams.
911void EventLogAnalyzer::CreateTotalBitrateGraph(
912 PacketDirection desired_direction,
philipel23c7f252017-07-14 06:30:03 -0700913 Plot* plot,
914 bool show_detector_state) {
terelius54ce6802016-07-13 06:44:41 -0700915 struct TimestampSize {
916 TimestampSize(uint64_t t, size_t s) : timestamp(t), size(s) {}
917 uint64_t timestamp;
918 size_t size;
919 };
920 std::vector<TimestampSize> packets;
921
922 PacketDirection direction;
923 size_t total_length;
924
925 // Extract timestamps and sizes for the relevant packets.
926 for (size_t i = 0; i < parsed_log_.GetNumberOfEvents(); i++) {
927 ParsedRtcEventLog::EventType event_type = parsed_log_.GetEventType(i);
928 if (event_type == ParsedRtcEventLog::RTP_EVENT) {
perkj77cd58e2017-05-30 03:52:10 -0700929 parsed_log_.GetRtpHeader(i, &direction, nullptr, nullptr, &total_length);
terelius54ce6802016-07-13 06:44:41 -0700930 if (direction == desired_direction) {
931 uint64_t timestamp = parsed_log_.GetTimestamp(i);
932 packets.push_back(TimestampSize(timestamp, total_length));
933 }
934 }
935 }
936
937 size_t window_index_begin = 0;
938 size_t window_index_end = 0;
939 size_t bytes_in_window = 0;
terelius54ce6802016-07-13 06:44:41 -0700940
941 // Calculate a moving average of the bitrate and store in a TimeSeries.
philipel35ba9bd2017-04-19 05:58:51 -0700942 TimeSeries bitrate_series("Bitrate", LINE_GRAPH);
terelius54ce6802016-07-13 06:44:41 -0700943 for (uint64_t time = begin_time_; time < end_time_ + step_; time += step_) {
944 while (window_index_end < packets.size() &&
945 packets[window_index_end].timestamp < time) {
946 bytes_in_window += packets[window_index_end].size;
terelius6addf492016-08-23 17:34:07 -0700947 ++window_index_end;
terelius54ce6802016-07-13 06:44:41 -0700948 }
949 while (window_index_begin < packets.size() &&
950 packets[window_index_begin].timestamp < time - window_duration_) {
951 RTC_DCHECK_LE(packets[window_index_begin].size, bytes_in_window);
952 bytes_in_window -= packets[window_index_begin].size;
terelius6addf492016-08-23 17:34:07 -0700953 ++window_index_begin;
terelius54ce6802016-07-13 06:44:41 -0700954 }
955 float window_duration_in_seconds =
956 static_cast<float>(window_duration_) / 1000000;
957 float x = static_cast<float>(time - begin_time_) / 1000000;
958 float y = bytes_in_window * 8 / window_duration_in_seconds / 1000;
philipel35ba9bd2017-04-19 05:58:51 -0700959 bitrate_series.points.emplace_back(x, y);
terelius54ce6802016-07-13 06:44:41 -0700960 }
philipel35ba9bd2017-04-19 05:58:51 -0700961 plot->AppendTimeSeries(std::move(bitrate_series));
terelius54ce6802016-07-13 06:44:41 -0700962
terelius8058e582016-07-25 01:32:41 -0700963 // Overlay the send-side bandwidth estimate over the outgoing bitrate.
964 if (desired_direction == kOutgoingPacket) {
philipel35ba9bd2017-04-19 05:58:51 -0700965 TimeSeries loss_series("Loss-based estimate", LINE_STEP_GRAPH);
philipel10fc0e62017-04-11 01:50:23 -0700966 for (auto& loss_update : bwe_loss_updates_) {
terelius8058e582016-07-25 01:32:41 -0700967 float x =
philipel10fc0e62017-04-11 01:50:23 -0700968 static_cast<float>(loss_update.timestamp - begin_time_) / 1000000;
969 float y = static_cast<float>(loss_update.new_bitrate) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -0700970 loss_series.points.emplace_back(x, y);
philipel10fc0e62017-04-11 01:50:23 -0700971 }
972
philipel35ba9bd2017-04-19 05:58:51 -0700973 TimeSeries delay_series("Delay-based estimate", LINE_STEP_GRAPH);
philipel23c7f252017-07-14 06:30:03 -0700974 IntervalSeries overusing_series("Overusing", "#ff8e82",
975 IntervalSeries::kHorizontal);
976 IntervalSeries underusing_series("Underusing", "#5092fc",
977 IntervalSeries::kHorizontal);
978 IntervalSeries normal_series("Normal", "#c4ffc4",
979 IntervalSeries::kHorizontal);
980 IntervalSeries* last_series = &normal_series;
981 double last_detector_switch = 0.0;
982
983 BandwidthUsage last_detector_state = BandwidthUsage::kBwNormal;
984
philipel10fc0e62017-04-11 01:50:23 -0700985 for (auto& delay_update : bwe_delay_updates_) {
986 float x =
987 static_cast<float>(delay_update.timestamp - begin_time_) / 1000000;
988 float y = static_cast<float>(delay_update.bitrate_bps) / 1000;
philipel23c7f252017-07-14 06:30:03 -0700989
990 if (last_detector_state != delay_update.detector_state) {
991 last_series->intervals.emplace_back(last_detector_switch, x);
992 last_detector_state = delay_update.detector_state;
993 last_detector_switch = x;
994
995 switch (delay_update.detector_state) {
996 case BandwidthUsage::kBwNormal:
997 last_series = &normal_series;
998 break;
999 case BandwidthUsage::kBwUnderusing:
1000 last_series = &underusing_series;
1001 break;
1002 case BandwidthUsage::kBwOverusing:
1003 last_series = &overusing_series;
1004 break;
1005 }
1006 }
1007
philipel35ba9bd2017-04-19 05:58:51 -07001008 delay_series.points.emplace_back(x, y);
terelius8058e582016-07-25 01:32:41 -07001009 }
philipele127e7a2017-03-29 16:28:53 +02001010
philipel23c7f252017-07-14 06:30:03 -07001011 RTC_CHECK(last_series);
1012 last_series->intervals.emplace_back(last_detector_switch, end_time_);
1013
philipel35ba9bd2017-04-19 05:58:51 -07001014 TimeSeries created_series("Probe cluster created.", DOT_GRAPH);
philipele127e7a2017-03-29 16:28:53 +02001015 for (auto& cluster : bwe_probe_cluster_created_events_) {
1016 float x = static_cast<float>(cluster.timestamp - begin_time_) / 1000000;
1017 float y = static_cast<float>(cluster.bitrate_bps) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001018 created_series.points.emplace_back(x, y);
philipele127e7a2017-03-29 16:28:53 +02001019 }
1020
philipel35ba9bd2017-04-19 05:58:51 -07001021 TimeSeries result_series("Probing results.", DOT_GRAPH);
philipele127e7a2017-03-29 16:28:53 +02001022 for (auto& result : bwe_probe_result_events_) {
1023 if (result.bitrate_bps) {
1024 float x = static_cast<float>(result.timestamp - begin_time_) / 1000000;
1025 float y = static_cast<float>(*result.bitrate_bps) / 1000;
philipel35ba9bd2017-04-19 05:58:51 -07001026 result_series.points.emplace_back(x, y);
philipele127e7a2017-03-29 16:28:53 +02001027 }
1028 }
philipel23c7f252017-07-14 06:30:03 -07001029
1030 if (show_detector_state) {
1031 plot->AppendIntervalSeries(std::move(overusing_series));
1032 plot->AppendIntervalSeries(std::move(underusing_series));
1033 plot->AppendIntervalSeries(std::move(normal_series));
1034 }
1035
1036 plot->AppendTimeSeries(std::move(bitrate_series));
philipel35ba9bd2017-04-19 05:58:51 -07001037 plot->AppendTimeSeries(std::move(loss_series));
1038 plot->AppendTimeSeries(std::move(delay_series));
1039 plot->AppendTimeSeries(std::move(created_series));
1040 plot->AppendTimeSeries(std::move(result_series));
terelius8058e582016-07-25 01:32:41 -07001041 }
philipele127e7a2017-03-29 16:28:53 +02001042
terelius2c8e8a32017-06-02 01:29:48 -07001043 // Overlay the incoming REMB over the outgoing bitrate
1044 // and outgoing REMB over incoming bitrate.
1045 PacketDirection remb_direction =
1046 desired_direction == kOutgoingPacket ? kIncomingPacket : kOutgoingPacket;
1047 TimeSeries remb_series("Remb", LINE_STEP_GRAPH);
1048 std::multimap<uint64_t, const LoggedRtcpPacket*> remb_packets;
1049 for (const auto& kv : rtcp_packets_) {
1050 if (kv.first.GetDirection() == remb_direction) {
1051 for (const LoggedRtcpPacket& rtcp_packet : kv.second) {
1052 if (rtcp_packet.type == kRtcpRemb) {
1053 remb_packets.insert(
1054 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1055 }
1056 }
1057 }
1058 }
1059
1060 for (const auto& kv : remb_packets) {
1061 const LoggedRtcpPacket* const rtcp = kv.second;
1062 const rtcp::Remb* const remb = static_cast<rtcp::Remb*>(rtcp->packet.get());
1063 float x = static_cast<float>(rtcp->timestamp - begin_time_) / 1000000;
1064 float y = static_cast<float>(remb->bitrate_bps()) / 1000;
1065 remb_series.points.emplace_back(x, y);
1066 }
1067 plot->AppendTimeSeriesIfNotEmpty(std::move(remb_series));
1068
tereliusdc35dcd2016-08-01 12:03:27 -07001069 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1070 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -07001071 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001072 plot->SetTitle("Incoming RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001073 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001074 plot->SetTitle("Outgoing RTP bitrate");
terelius54ce6802016-07-13 06:44:41 -07001075 }
1076}
1077
1078// For each SSRC, plot the bandwidth used by that stream.
1079void EventLogAnalyzer::CreateStreamBitrateGraph(
1080 PacketDirection desired_direction,
1081 Plot* plot) {
terelius6addf492016-08-23 17:34:07 -07001082 for (auto& kv : rtp_packets_) {
1083 StreamId stream_id = kv.first;
1084 const std::vector<LoggedRtpPacket>& packet_stream = kv.second;
1085 // Filter on direction and SSRC.
1086 if (stream_id.GetDirection() != desired_direction ||
1087 !MatchingSsrc(stream_id.GetSsrc(), desired_ssrc_)) {
1088 continue;
terelius54ce6802016-07-13 06:44:41 -07001089 }
1090
terelius23c595a2017-03-15 01:59:12 -07001091 TimeSeries time_series(GetStreamName(stream_id), LINE_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001092 MovingAverage<LoggedRtpPacket, double>(
1093 [](const LoggedRtpPacket& packet) {
1094 return rtc::Optional<double>(packet.total_length * 8.0 / 1000.0);
1095 },
1096 packet_stream, begin_time_, end_time_, window_duration_, step_,
1097 &time_series);
philipel35ba9bd2017-04-19 05:58:51 -07001098 plot->AppendTimeSeries(std::move(time_series));
terelius54ce6802016-07-13 06:44:41 -07001099 }
1100
tereliusdc35dcd2016-08-01 12:03:27 -07001101 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1102 plot->SetSuggestedYAxis(0, 1, "Bitrate (kbps)", kBottomMargin, kTopMargin);
terelius54ce6802016-07-13 06:44:41 -07001103 if (desired_direction == webrtc::PacketDirection::kIncomingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001104 plot->SetTitle("Incoming bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001105 } else if (desired_direction == webrtc::PacketDirection::kOutgoingPacket) {
tereliusdc35dcd2016-08-01 12:03:27 -07001106 plot->SetTitle("Outgoing bitrate per stream");
terelius54ce6802016-07-13 06:44:41 -07001107 }
1108}
1109
tereliuse34c19c2016-08-15 08:47:14 -07001110void EventLogAnalyzer::CreateBweSimulationGraph(Plot* plot) {
stefanff421622017-04-20 03:24:01 -07001111 std::multimap<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1112 std::multimap<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
Stefan Holmer13181032016-07-29 14:48:54 +02001113
1114 for (const auto& kv : rtp_packets_) {
1115 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1116 for (const LoggedRtpPacket& rtp_packet : kv.second)
1117 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1118 }
1119 }
1120
1121 for (const auto& kv : rtcp_packets_) {
1122 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1123 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1124 incoming_rtcp.insert(
1125 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1126 }
1127 }
1128
1129 SimulatedClock clock(0);
1130 BitrateObserver observer;
1131 RtcEventLogNullImpl null_event_log;
nisse0245da02016-11-30 03:35:20 -08001132 PacketRouter packet_router;
1133 CongestionController cc(&clock, &observer, &observer, &null_event_log,
1134 &packet_router);
Stefan Holmer13181032016-07-29 14:48:54 +02001135 // TODO(holmer): Log the call config and use that here instead.
1136 static const uint32_t kDefaultStartBitrateBps = 300000;
1137 cc.SetBweBitrates(0, kDefaultStartBitrateBps, -1);
1138
terelius23c595a2017-03-15 01:59:12 -07001139 TimeSeries time_series("Delay-based estimate", LINE_DOT_GRAPH);
1140 TimeSeries acked_time_series("Acked bitrate", LINE_DOT_GRAPH);
Stefan Holmer13181032016-07-29 14:48:54 +02001141
1142 auto rtp_iterator = outgoing_rtp.begin();
1143 auto rtcp_iterator = incoming_rtcp.begin();
1144
1145 auto NextRtpTime = [&]() {
1146 if (rtp_iterator != outgoing_rtp.end())
1147 return static_cast<int64_t>(rtp_iterator->first);
1148 return std::numeric_limits<int64_t>::max();
1149 };
1150
1151 auto NextRtcpTime = [&]() {
1152 if (rtcp_iterator != incoming_rtcp.end())
1153 return static_cast<int64_t>(rtcp_iterator->first);
1154 return std::numeric_limits<int64_t>::max();
1155 };
1156
1157 auto NextProcessTime = [&]() {
1158 if (rtcp_iterator != incoming_rtcp.end() ||
1159 rtp_iterator != outgoing_rtp.end()) {
1160 return clock.TimeInMicroseconds() +
1161 std::max<int64_t>(cc.TimeUntilNextProcess() * 1000, 0);
1162 }
1163 return std::numeric_limits<int64_t>::max();
1164 };
1165
Stefan Holmer492ee282016-10-27 17:19:20 +02001166 RateStatistics acked_bitrate(250, 8000);
Stefan Holmer60e43462016-09-07 09:58:20 +02001167
Stefan Holmer13181032016-07-29 14:48:54 +02001168 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
Stefan Holmer492ee282016-10-27 17:19:20 +02001169 int64_t last_update_us = 0;
Stefan Holmer13181032016-07-29 14:48:54 +02001170 while (time_us != std::numeric_limits<int64_t>::max()) {
1171 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1172 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001173 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001174 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1175 if (rtcp.type == kRtcpTransportFeedback) {
elad.alon5bbf43f2017-03-09 06:40:08 -08001176 cc.OnTransportFeedback(
1177 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
1178 std::vector<PacketFeedback> feedback = cc.GetTransportFeedbackVector();
elad.alonec304f92017-03-08 05:03:53 -08001179 SortPacketFeedbackVector(&feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +02001180 rtc::Optional<uint32_t> bitrate_bps;
1181 if (!feedback.empty()) {
elad.alonf9490002017-03-06 05:32:21 -08001182 for (const PacketFeedback& packet : feedback)
Stefan Holmer60e43462016-09-07 09:58:20 +02001183 acked_bitrate.Update(packet.payload_size, packet.arrival_time_ms);
1184 bitrate_bps = acked_bitrate.Rate(feedback.back().arrival_time_ms);
1185 }
1186 uint32_t y = 0;
1187 if (bitrate_bps)
1188 y = *bitrate_bps / 1000;
1189 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1190 1000000;
1191 acked_time_series.points.emplace_back(x, y);
Stefan Holmer13181032016-07-29 14:48:54 +02001192 }
1193 ++rtcp_iterator;
1194 }
1195 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
stefanc3de0332016-08-02 07:22:17 -07001196 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001197 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1198 if (rtp.header.extension.hasTransportSequenceNumber) {
1199 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
elad.alond12a8e12017-03-23 11:04:48 -07001200 cc.AddPacket(rtp.header.ssrc,
1201 rtp.header.extension.transportSequenceNumber,
elad.alon5bbf43f2017-03-09 06:40:08 -08001202 rtp.total_length, PacedPacketInfo());
Stefan Holmer13181032016-07-29 14:48:54 +02001203 rtc::SentPacket sent_packet(
1204 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1205 cc.OnSentPacket(sent_packet);
1206 }
1207 ++rtp_iterator;
1208 }
stefanc3de0332016-08-02 07:22:17 -07001209 if (clock.TimeInMicroseconds() >= NextProcessTime()) {
1210 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextProcessTime());
Stefan Holmer13181032016-07-29 14:48:54 +02001211 cc.Process();
stefanc3de0332016-08-02 07:22:17 -07001212 }
Stefan Holmer492ee282016-10-27 17:19:20 +02001213 if (observer.GetAndResetBitrateUpdated() ||
1214 time_us - last_update_us >= 1e6) {
Stefan Holmer13181032016-07-29 14:48:54 +02001215 uint32_t y = observer.last_bitrate_bps() / 1000;
Stefan Holmer13181032016-07-29 14:48:54 +02001216 float x = static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1217 1000000;
1218 time_series.points.emplace_back(x, y);
Stefan Holmer492ee282016-10-27 17:19:20 +02001219 last_update_us = time_us;
Stefan Holmer13181032016-07-29 14:48:54 +02001220 }
1221 time_us = std::min({NextRtpTime(), NextRtcpTime(), NextProcessTime()});
1222 }
1223 // Add the data set to the plot.
philipel35ba9bd2017-04-19 05:58:51 -07001224 plot->AppendTimeSeries(std::move(time_series));
1225 plot->AppendTimeSeries(std::move(acked_time_series));
Stefan Holmer13181032016-07-29 14:48:54 +02001226
tereliusdc35dcd2016-08-01 12:03:27 -07001227 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1228 plot->SetSuggestedYAxis(0, 10, "Bitrate (kbps)", kBottomMargin, kTopMargin);
1229 plot->SetTitle("Simulated BWE behavior");
Stefan Holmer13181032016-07-29 14:48:54 +02001230}
1231
tereliuse34c19c2016-08-15 08:47:14 -07001232void EventLogAnalyzer::CreateNetworkDelayFeedbackGraph(Plot* plot) {
stefanff421622017-04-20 03:24:01 -07001233 std::multimap<uint64_t, const LoggedRtpPacket*> outgoing_rtp;
1234 std::multimap<uint64_t, const LoggedRtcpPacket*> incoming_rtcp;
stefanc3de0332016-08-02 07:22:17 -07001235
1236 for (const auto& kv : rtp_packets_) {
1237 if (kv.first.GetDirection() == PacketDirection::kOutgoingPacket) {
1238 for (const LoggedRtpPacket& rtp_packet : kv.second)
1239 outgoing_rtp.insert(std::make_pair(rtp_packet.timestamp, &rtp_packet));
1240 }
1241 }
1242
1243 for (const auto& kv : rtcp_packets_) {
1244 if (kv.first.GetDirection() == PacketDirection::kIncomingPacket) {
1245 for (const LoggedRtcpPacket& rtcp_packet : kv.second)
1246 incoming_rtcp.insert(
1247 std::make_pair(rtcp_packet.timestamp, &rtcp_packet));
1248 }
1249 }
1250
1251 SimulatedClock clock(0);
elad.alon5bbf43f2017-03-09 06:40:08 -08001252 TransportFeedbackAdapter feedback_adapter(&clock);
stefanc3de0332016-08-02 07:22:17 -07001253
terelius23c595a2017-03-15 01:59:12 -07001254 TimeSeries time_series("Network Delay Change", LINE_DOT_GRAPH);
stefanc3de0332016-08-02 07:22:17 -07001255 int64_t estimated_base_delay_ms = std::numeric_limits<int64_t>::max();
1256
1257 auto rtp_iterator = outgoing_rtp.begin();
1258 auto rtcp_iterator = incoming_rtcp.begin();
1259
1260 auto NextRtpTime = [&]() {
1261 if (rtp_iterator != outgoing_rtp.end())
1262 return static_cast<int64_t>(rtp_iterator->first);
1263 return std::numeric_limits<int64_t>::max();
1264 };
1265
1266 auto NextRtcpTime = [&]() {
1267 if (rtcp_iterator != incoming_rtcp.end())
1268 return static_cast<int64_t>(rtcp_iterator->first);
1269 return std::numeric_limits<int64_t>::max();
1270 };
1271
1272 int64_t time_us = std::min(NextRtpTime(), NextRtcpTime());
1273 while (time_us != std::numeric_limits<int64_t>::max()) {
1274 clock.AdvanceTimeMicroseconds(time_us - clock.TimeInMicroseconds());
1275 if (clock.TimeInMicroseconds() >= NextRtcpTime()) {
1276 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtcpTime());
1277 const LoggedRtcpPacket& rtcp = *rtcp_iterator->second;
1278 if (rtcp.type == kRtcpTransportFeedback) {
Stefan Holmer60e43462016-09-07 09:58:20 +02001279 feedback_adapter.OnTransportFeedback(
1280 *static_cast<rtcp::TransportFeedback*>(rtcp.packet.get()));
elad.alonf9490002017-03-06 05:32:21 -08001281 std::vector<PacketFeedback> feedback =
1282 feedback_adapter.GetTransportFeedbackVector();
elad.alonec304f92017-03-08 05:03:53 -08001283 SortPacketFeedbackVector(&feedback);
elad.alonf9490002017-03-06 05:32:21 -08001284 for (const PacketFeedback& packet : feedback) {
stefanc3de0332016-08-02 07:22:17 -07001285 int64_t y = packet.arrival_time_ms - packet.send_time_ms;
1286 float x =
1287 static_cast<float>(clock.TimeInMicroseconds() - begin_time_) /
1288 1000000;
1289 estimated_base_delay_ms = std::min(y, estimated_base_delay_ms);
1290 time_series.points.emplace_back(x, y);
1291 }
1292 }
1293 ++rtcp_iterator;
1294 }
1295 if (clock.TimeInMicroseconds() >= NextRtpTime()) {
1296 RTC_DCHECK_EQ(clock.TimeInMicroseconds(), NextRtpTime());
1297 const LoggedRtpPacket& rtp = *rtp_iterator->second;
1298 if (rtp.header.extension.hasTransportSequenceNumber) {
1299 RTC_DCHECK(rtp.header.extension.hasTransportSequenceNumber);
elad.alond12a8e12017-03-23 11:04:48 -07001300 feedback_adapter.AddPacket(rtp.header.ssrc,
1301 rtp.header.extension.transportSequenceNumber,
philipel8aadd502017-02-23 02:56:13 -08001302 rtp.total_length, PacedPacketInfo());
stefanc3de0332016-08-02 07:22:17 -07001303 feedback_adapter.OnSentPacket(
1304 rtp.header.extension.transportSequenceNumber, rtp.timestamp / 1000);
1305 }
1306 ++rtp_iterator;
1307 }
1308 time_us = std::min(NextRtpTime(), NextRtcpTime());
1309 }
1310 // We assume that the base network delay (w/o queues) is the min delay
1311 // observed during the call.
1312 for (TimeSeriesPoint& point : time_series.points)
1313 point.y -= estimated_base_delay_ms;
1314 // Add the data set to the plot.
philipel35ba9bd2017-04-19 05:58:51 -07001315 plot->AppendTimeSeries(std::move(time_series));
stefanc3de0332016-08-02 07:22:17 -07001316
1317 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1318 plot->SetSuggestedYAxis(0, 10, "Delay (ms)", kBottomMargin, kTopMargin);
1319 plot->SetTitle("Network Delay Change.");
1320}
stefan08383272016-12-20 08:51:52 -08001321
1322std::vector<std::pair<int64_t, int64_t>> EventLogAnalyzer::GetFrameTimestamps()
1323 const {
1324 std::vector<std::pair<int64_t, int64_t>> timestamps;
1325 size_t largest_stream_size = 0;
1326 const std::vector<LoggedRtpPacket>* largest_video_stream = nullptr;
1327 // Find the incoming video stream with the most number of packets that is
1328 // not rtx.
1329 for (const auto& kv : rtp_packets_) {
1330 if (kv.first.GetDirection() == kIncomingPacket &&
1331 video_ssrcs_.find(kv.first) != video_ssrcs_.end() &&
1332 rtx_ssrcs_.find(kv.first) == rtx_ssrcs_.end() &&
1333 kv.second.size() > largest_stream_size) {
1334 largest_stream_size = kv.second.size();
1335 largest_video_stream = &kv.second;
1336 }
1337 }
1338 if (largest_video_stream == nullptr) {
1339 for (auto& packet : *largest_video_stream) {
1340 if (packet.header.markerBit) {
1341 int64_t capture_ms = packet.header.timestamp / 90.0;
1342 int64_t arrival_ms = packet.timestamp / 1000.0;
1343 timestamps.push_back(std::make_pair(capture_ms, arrival_ms));
1344 }
1345 }
1346 }
1347 return timestamps;
1348}
stefane372d3c2017-02-02 08:04:18 -08001349
1350void EventLogAnalyzer::CreateTimestampGraph(Plot* plot) {
1351 for (const auto& kv : rtp_packets_) {
1352 const std::vector<LoggedRtpPacket>& rtp_packets = kv.second;
1353 StreamId stream_id = kv.first;
1354
1355 {
terelius23c595a2017-03-15 01:59:12 -07001356 TimeSeries timestamp_data(GetStreamName(stream_id) + " capture-time",
1357 LINE_DOT_GRAPH);
stefane372d3c2017-02-02 08:04:18 -08001358 for (LoggedRtpPacket packet : rtp_packets) {
1359 float x = static_cast<float>(packet.timestamp - begin_time_) / 1000000;
1360 float y = packet.header.timestamp;
1361 timestamp_data.points.emplace_back(x, y);
1362 }
philipel35ba9bd2017-04-19 05:58:51 -07001363 plot->AppendTimeSeries(std::move(timestamp_data));
stefane372d3c2017-02-02 08:04:18 -08001364 }
1365
1366 {
1367 auto kv = rtcp_packets_.find(stream_id);
1368 if (kv != rtcp_packets_.end()) {
1369 const auto& packets = kv->second;
terelius23c595a2017-03-15 01:59:12 -07001370 TimeSeries timestamp_data(
1371 GetStreamName(stream_id) + " rtcp capture-time", LINE_DOT_GRAPH);
stefane372d3c2017-02-02 08:04:18 -08001372 for (const LoggedRtcpPacket& rtcp : packets) {
1373 if (rtcp.type != kRtcpSr)
1374 continue;
1375 rtcp::SenderReport* sr;
1376 sr = static_cast<rtcp::SenderReport*>(rtcp.packet.get());
1377 float x = static_cast<float>(rtcp.timestamp - begin_time_) / 1000000;
1378 float y = sr->rtp_timestamp();
1379 timestamp_data.points.emplace_back(x, y);
1380 }
philipel35ba9bd2017-04-19 05:58:51 -07001381 plot->AppendTimeSeries(std::move(timestamp_data));
stefane372d3c2017-02-02 08:04:18 -08001382 }
1383 }
1384 }
1385
1386 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1387 plot->SetSuggestedYAxis(0, 1, "Timestamp (90khz)", kBottomMargin, kTopMargin);
1388 plot->SetTitle("Timestamps");
1389}
michaelt6e5b2192017-02-22 07:33:27 -08001390
1391void EventLogAnalyzer::CreateAudioEncoderTargetBitrateGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001392 TimeSeries time_series("Audio encoder target bitrate", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001393 ProcessPoints<AudioNetworkAdaptationEvent>(
1394 [](const AudioNetworkAdaptationEvent& ana_event) -> rtc::Optional<float> {
michaelt6e5b2192017-02-22 07:33:27 -08001395 if (ana_event.config.bitrate_bps)
1396 return rtc::Optional<float>(
1397 static_cast<float>(*ana_event.config.bitrate_bps));
1398 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001399 },
philipel35ba9bd2017-04-19 05:58:51 -07001400 audio_network_adaptation_events_, begin_time_, &time_series);
1401 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001402 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1403 plot->SetSuggestedYAxis(0, 1, "Bitrate (bps)", kBottomMargin, kTopMargin);
1404 plot->SetTitle("Reported audio encoder target bitrate");
1405}
1406
1407void EventLogAnalyzer::CreateAudioEncoderFrameLengthGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001408 TimeSeries time_series("Audio encoder frame length", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001409 ProcessPoints<AudioNetworkAdaptationEvent>(
1410 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001411 if (ana_event.config.frame_length_ms)
1412 return rtc::Optional<float>(
1413 static_cast<float>(*ana_event.config.frame_length_ms));
1414 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001415 },
philipel35ba9bd2017-04-19 05:58:51 -07001416 audio_network_adaptation_events_, begin_time_, &time_series);
1417 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001418 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1419 plot->SetSuggestedYAxis(0, 1, "Frame length (ms)", kBottomMargin, kTopMargin);
1420 plot->SetTitle("Reported audio encoder frame length");
1421}
1422
terelius2ee076d2017-08-15 02:04:02 -07001423void EventLogAnalyzer::CreateAudioEncoderPacketLossGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001424 TimeSeries time_series("Audio encoder uplink packet loss fraction",
1425 LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001426 ProcessPoints<AudioNetworkAdaptationEvent>(
1427 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001428 if (ana_event.config.uplink_packet_loss_fraction)
1429 return rtc::Optional<float>(static_cast<float>(
1430 *ana_event.config.uplink_packet_loss_fraction));
1431 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001432 },
philipel35ba9bd2017-04-19 05:58:51 -07001433 audio_network_adaptation_events_, begin_time_, &time_series);
1434 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001435 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1436 plot->SetSuggestedYAxis(0, 10, "Percent lost packets", kBottomMargin,
1437 kTopMargin);
1438 plot->SetTitle("Reported audio encoder lost packets");
1439}
1440
1441void EventLogAnalyzer::CreateAudioEncoderEnableFecGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001442 TimeSeries time_series("Audio encoder FEC", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001443 ProcessPoints<AudioNetworkAdaptationEvent>(
1444 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001445 if (ana_event.config.enable_fec)
1446 return rtc::Optional<float>(
1447 static_cast<float>(*ana_event.config.enable_fec));
1448 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001449 },
philipel35ba9bd2017-04-19 05:58:51 -07001450 audio_network_adaptation_events_, begin_time_, &time_series);
1451 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001452 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1453 plot->SetSuggestedYAxis(0, 1, "FEC (false/true)", kBottomMargin, kTopMargin);
1454 plot->SetTitle("Reported audio encoder FEC");
1455}
1456
1457void EventLogAnalyzer::CreateAudioEncoderEnableDtxGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001458 TimeSeries time_series("Audio encoder DTX", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001459 ProcessPoints<AudioNetworkAdaptationEvent>(
1460 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001461 if (ana_event.config.enable_dtx)
1462 return rtc::Optional<float>(
1463 static_cast<float>(*ana_event.config.enable_dtx));
1464 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001465 },
philipel35ba9bd2017-04-19 05:58:51 -07001466 audio_network_adaptation_events_, begin_time_, &time_series);
1467 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001468 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1469 plot->SetSuggestedYAxis(0, 1, "DTX (false/true)", kBottomMargin, kTopMargin);
1470 plot->SetTitle("Reported audio encoder DTX");
1471}
1472
1473void EventLogAnalyzer::CreateAudioEncoderNumChannelsGraph(Plot* plot) {
philipel35ba9bd2017-04-19 05:58:51 -07001474 TimeSeries time_series("Audio encoder number of channels", LINE_DOT_GRAPH);
terelius53dc23c2017-03-13 05:24:05 -07001475 ProcessPoints<AudioNetworkAdaptationEvent>(
1476 [](const AudioNetworkAdaptationEvent& ana_event) {
michaelt6e5b2192017-02-22 07:33:27 -08001477 if (ana_event.config.num_channels)
1478 return rtc::Optional<float>(
1479 static_cast<float>(*ana_event.config.num_channels));
1480 return rtc::Optional<float>();
terelius53dc23c2017-03-13 05:24:05 -07001481 },
philipel35ba9bd2017-04-19 05:58:51 -07001482 audio_network_adaptation_events_, begin_time_, &time_series);
1483 plot->AppendTimeSeries(std::move(time_series));
michaelt6e5b2192017-02-22 07:33:27 -08001484 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1485 plot->SetSuggestedYAxis(0, 1, "Number of channels (1 (mono)/2 (stereo))",
1486 kBottomMargin, kTopMargin);
1487 plot->SetTitle("Reported audio encoder number of channels");
1488}
henrik.lundin3c938fc2017-06-14 06:09:58 -07001489
1490class NetEqStreamInput : public test::NetEqInput {
1491 public:
1492 // Does not take any ownership, and all pointers must refer to valid objects
1493 // that outlive the one constructed.
1494 NetEqStreamInput(const std::vector<LoggedRtpPacket>* packet_stream,
1495 const std::vector<uint64_t>* output_events_us,
1496 rtc::Optional<uint64_t> end_time_us)
1497 : packet_stream_(*packet_stream),
1498 packet_stream_it_(packet_stream_.begin()),
1499 output_events_us_it_(output_events_us->begin()),
1500 output_events_us_end_(output_events_us->end()),
1501 end_time_us_(end_time_us) {
1502 RTC_DCHECK(packet_stream);
1503 RTC_DCHECK(output_events_us);
1504 }
1505
1506 rtc::Optional<int64_t> NextPacketTime() const override {
1507 if (packet_stream_it_ == packet_stream_.end()) {
1508 return rtc::Optional<int64_t>();
1509 }
1510 if (end_time_us_ && packet_stream_it_->timestamp > *end_time_us_) {
1511 return rtc::Optional<int64_t>();
1512 }
1513 // Convert from us to ms.
1514 return rtc::Optional<int64_t>(packet_stream_it_->timestamp / 1000);
1515 }
1516
1517 rtc::Optional<int64_t> NextOutputEventTime() const override {
1518 if (output_events_us_it_ == output_events_us_end_) {
1519 return rtc::Optional<int64_t>();
1520 }
1521 if (end_time_us_ && *output_events_us_it_ > *end_time_us_) {
1522 return rtc::Optional<int64_t>();
1523 }
1524 // Convert from us to ms.
1525 return rtc::Optional<int64_t>(
1526 rtc::checked_cast<int64_t>(*output_events_us_it_ / 1000));
1527 }
1528
1529 std::unique_ptr<PacketData> PopPacket() override {
1530 if (packet_stream_it_ == packet_stream_.end()) {
1531 return std::unique_ptr<PacketData>();
1532 }
1533 std::unique_ptr<PacketData> packet_data(new PacketData());
1534 packet_data->header = packet_stream_it_->header;
1535 // Convert from us to ms.
1536 packet_data->time_ms = packet_stream_it_->timestamp / 1000.0;
1537
1538 // This is a header-only "dummy" packet. Set the payload to all zeros, with
1539 // length according to the virtual length.
1540 packet_data->payload.SetSize(packet_stream_it_->total_length);
1541 std::fill_n(packet_data->payload.data(), packet_data->payload.size(), 0);
1542
1543 ++packet_stream_it_;
1544 return packet_data;
1545 }
1546
1547 void AdvanceOutputEvent() override {
1548 if (output_events_us_it_ != output_events_us_end_) {
1549 ++output_events_us_it_;
1550 }
1551 }
1552
1553 bool ended() const override { return !NextEventTime(); }
1554
1555 rtc::Optional<RTPHeader> NextHeader() const override {
1556 if (packet_stream_it_ == packet_stream_.end()) {
1557 return rtc::Optional<RTPHeader>();
1558 }
1559 return rtc::Optional<RTPHeader>(packet_stream_it_->header);
1560 }
1561
1562 private:
1563 const std::vector<LoggedRtpPacket>& packet_stream_;
1564 std::vector<LoggedRtpPacket>::const_iterator packet_stream_it_;
1565 std::vector<uint64_t>::const_iterator output_events_us_it_;
1566 const std::vector<uint64_t>::const_iterator output_events_us_end_;
1567 const rtc::Optional<uint64_t> end_time_us_;
1568};
1569
1570namespace {
1571// Creates a NetEq test object and all necessary input and output helpers. Runs
1572// the test and returns the NetEqDelayAnalyzer object that was used to
1573// instrument the test.
1574std::unique_ptr<test::NetEqDelayAnalyzer> CreateNetEqTestAndRun(
1575 const std::vector<LoggedRtpPacket>* packet_stream,
1576 const std::vector<uint64_t>* output_events_us,
1577 rtc::Optional<uint64_t> end_time_us,
1578 const std::string& replacement_file_name,
1579 int file_sample_rate_hz) {
1580 std::unique_ptr<test::NetEqInput> input(
1581 new NetEqStreamInput(packet_stream, output_events_us, end_time_us));
1582
1583 constexpr int kReplacementPt = 127;
1584 std::set<uint8_t> cn_types;
1585 std::set<uint8_t> forbidden_types;
1586 input.reset(new test::NetEqReplacementInput(std::move(input), kReplacementPt,
1587 cn_types, forbidden_types));
1588
1589 NetEq::Config config;
1590 config.max_packets_in_buffer = 200;
1591 config.enable_fast_accelerate = true;
1592
1593 std::unique_ptr<test::VoidAudioSink> output(new test::VoidAudioSink());
1594
1595 test::NetEqTest::DecoderMap codecs;
1596
1597 // Create a "replacement decoder" that produces the decoded audio by reading
1598 // from a file rather than from the encoded payloads.
1599 std::unique_ptr<test::ResampleInputAudioFile> replacement_file(
1600 new test::ResampleInputAudioFile(replacement_file_name,
1601 file_sample_rate_hz));
1602 replacement_file->set_output_rate_hz(48000);
1603 std::unique_ptr<AudioDecoder> replacement_decoder(
1604 new test::FakeDecodeFromFile(std::move(replacement_file), 48000, false));
1605 test::NetEqTest::ExtDecoderMap ext_codecs;
1606 ext_codecs[kReplacementPt] = {replacement_decoder.get(),
1607 NetEqDecoder::kDecoderArbitrary,
1608 "replacement codec"};
1609
1610 std::unique_ptr<test::NetEqDelayAnalyzer> delay_cb(
1611 new test::NetEqDelayAnalyzer);
1612 test::DefaultNetEqTestErrorCallback error_cb;
1613 test::NetEqTest::Callbacks callbacks;
1614 callbacks.error_callback = &error_cb;
1615 callbacks.post_insert_packet = delay_cb.get();
1616 callbacks.get_audio_callback = delay_cb.get();
1617
1618 test::NetEqTest test(config, codecs, ext_codecs, std::move(input),
1619 std::move(output), callbacks);
1620 test.Run();
1621 return delay_cb;
1622}
1623} // namespace
1624
1625// Plots the jitter buffer delay profile. This will plot only for the first
1626// incoming audio SSRC. If the stream contains more than one incoming audio
1627// SSRC, all but the first will be ignored.
1628void EventLogAnalyzer::CreateAudioJitterBufferGraph(
1629 const std::string& replacement_file_name,
1630 int file_sample_rate_hz,
1631 Plot* plot) {
1632 const auto& incoming_audio_kv = std::find_if(
1633 rtp_packets_.begin(), rtp_packets_.end(),
1634 [this](std::pair<StreamId, std::vector<LoggedRtpPacket>> kv) {
1635 return kv.first.GetDirection() == kIncomingPacket &&
1636 this->IsAudioSsrc(kv.first);
1637 });
1638 if (incoming_audio_kv == rtp_packets_.end()) {
1639 // No incoming audio stream found.
1640 return;
1641 }
1642
1643 const uint32_t ssrc = incoming_audio_kv->first.GetSsrc();
1644
1645 std::map<uint32_t, std::vector<uint64_t>>::const_iterator output_events_it =
1646 audio_playout_events_.find(ssrc);
1647 if (output_events_it == audio_playout_events_.end()) {
1648 // Could not find output events with SSRC matching the input audio stream.
1649 // Using the first available stream of output events.
1650 output_events_it = audio_playout_events_.cbegin();
1651 }
1652
1653 rtc::Optional<uint64_t> end_time_us =
1654 log_segments_.empty()
1655 ? rtc::Optional<uint64_t>()
1656 : rtc::Optional<uint64_t>(log_segments_.front().second);
1657
1658 auto delay_cb = CreateNetEqTestAndRun(
1659 &incoming_audio_kv->second, &output_events_it->second, end_time_us,
1660 replacement_file_name, file_sample_rate_hz);
1661
1662 std::vector<float> send_times_s;
1663 std::vector<float> arrival_delay_ms;
1664 std::vector<float> corrected_arrival_delay_ms;
1665 std::vector<rtc::Optional<float>> playout_delay_ms;
1666 std::vector<rtc::Optional<float>> target_delay_ms;
1667 delay_cb->CreateGraphs(&send_times_s, &arrival_delay_ms,
1668 &corrected_arrival_delay_ms, &playout_delay_ms,
1669 &target_delay_ms);
1670 RTC_DCHECK_EQ(send_times_s.size(), arrival_delay_ms.size());
1671 RTC_DCHECK_EQ(send_times_s.size(), corrected_arrival_delay_ms.size());
1672 RTC_DCHECK_EQ(send_times_s.size(), playout_delay_ms.size());
1673 RTC_DCHECK_EQ(send_times_s.size(), target_delay_ms.size());
1674
1675 std::map<StreamId, TimeSeries> time_series_packet_arrival;
1676 std::map<StreamId, TimeSeries> time_series_relative_packet_arrival;
1677 std::map<StreamId, TimeSeries> time_series_play_time;
1678 std::map<StreamId, TimeSeries> time_series_target_time;
1679 float min_y_axis = 0.f;
1680 float max_y_axis = 0.f;
1681 const StreamId stream_id = incoming_audio_kv->first;
1682 for (size_t i = 0; i < send_times_s.size(); ++i) {
1683 time_series_packet_arrival[stream_id].points.emplace_back(
1684 TimeSeriesPoint(send_times_s[i], arrival_delay_ms[i]));
1685 time_series_relative_packet_arrival[stream_id].points.emplace_back(
1686 TimeSeriesPoint(send_times_s[i], corrected_arrival_delay_ms[i]));
1687 min_y_axis = std::min(min_y_axis, corrected_arrival_delay_ms[i]);
1688 max_y_axis = std::max(max_y_axis, corrected_arrival_delay_ms[i]);
1689 if (playout_delay_ms[i]) {
1690 time_series_play_time[stream_id].points.emplace_back(
1691 TimeSeriesPoint(send_times_s[i], *playout_delay_ms[i]));
1692 min_y_axis = std::min(min_y_axis, *playout_delay_ms[i]);
1693 max_y_axis = std::max(max_y_axis, *playout_delay_ms[i]);
1694 }
1695 if (target_delay_ms[i]) {
1696 time_series_target_time[stream_id].points.emplace_back(
1697 TimeSeriesPoint(send_times_s[i], *target_delay_ms[i]));
1698 min_y_axis = std::min(min_y_axis, *target_delay_ms[i]);
1699 max_y_axis = std::max(max_y_axis, *target_delay_ms[i]);
1700 }
1701 }
1702
1703 // This code is adapted for a single stream. The creation of the streams above
1704 // guarantee that no more than one steam is included. If multiple streams are
1705 // to be plotted, they should likely be given distinct labels below.
1706 RTC_DCHECK_EQ(time_series_relative_packet_arrival.size(), 1);
1707 for (auto& series : time_series_relative_packet_arrival) {
1708 series.second.label = "Relative packet arrival delay";
1709 series.second.style = LINE_GRAPH;
1710 plot->AppendTimeSeries(std::move(series.second));
1711 }
1712 RTC_DCHECK_EQ(time_series_play_time.size(), 1);
1713 for (auto& series : time_series_play_time) {
1714 series.second.label = "Playout delay";
1715 series.second.style = LINE_GRAPH;
1716 plot->AppendTimeSeries(std::move(series.second));
1717 }
1718 RTC_DCHECK_EQ(time_series_target_time.size(), 1);
1719 for (auto& series : time_series_target_time) {
1720 series.second.label = "Target delay";
1721 series.second.style = LINE_DOT_GRAPH;
1722 plot->AppendTimeSeries(std::move(series.second));
1723 }
1724
1725 plot->SetXAxis(0, call_duration_s_, "Time (s)", kLeftMargin, kRightMargin);
1726 plot->SetYAxis(min_y_axis, max_y_axis, "Relative delay (ms)", kBottomMargin,
1727 kTopMargin);
1728 plot->SetTitle("NetEq timing");
1729}
terelius54ce6802016-07-13 06:44:41 -07001730} // namespace plotting
1731} // namespace webrtc