1 /*
2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #ifndef RTC_TOOLS_RTC_EVENT_LOG_VISUALIZER_ANALYZER_COMMON_H_
12 #define RTC_TOOLS_RTC_EVENT_LOG_VISUALIZER_ANALYZER_COMMON_H_
13
14 #include <cstdint>
15 #include <string>
16
17 #include "absl/types/optional.h"
18 #include "api/function_view.h"
19 #include "logging/rtc_event_log/rtc_event_log_parser.h"
20 #include "rtc_tools/rtc_event_log_visualizer/plot_base.h"
21
22 namespace webrtc {
23
24 constexpr int kNumMicrosecsPerSec = 1000000;
25 constexpr int kNumMillisecsPerSec = 1000;
26 constexpr float kLeftMargin = 0.01f;
27 constexpr float kRightMargin = 0.02f;
28 constexpr float kBottomMargin = 0.02f;
29 constexpr float kTopMargin = 0.05f;
30
31 class AnalyzerConfig {
32 public:
GetCallTimeSec(Timestamp timestamp)33 float GetCallTimeSec(Timestamp timestamp) const {
34 Timestamp offset = normalize_time_ ? begin_time_ : Timestamp::Zero();
35 return static_cast<float>((timestamp - offset).us()) / 1000000;
36 }
37
GetCallTimeSecFromMs(int64_t timestamp_ms)38 float GetCallTimeSecFromMs(int64_t timestamp_ms) const {
39 return GetCallTimeSec(Timestamp::Millis(timestamp_ms));
40 }
41
CallBeginTimeSec()42 float CallBeginTimeSec() const { return GetCallTimeSec(begin_time_); }
43
CallEndTimeSec()44 float CallEndTimeSec() const { return GetCallTimeSec(end_time_); }
45
CallTimeToUtcOffsetMs()46 int64_t CallTimeToUtcOffsetMs() {
47 if (normalize_time_) {
48 Timestamp utc_begin_time_ = begin_time_ + rtc_to_utc_offset_;
49 return utc_begin_time_.ms();
50 }
51 return rtc_to_utc_offset_.ms();
52 }
53
54 // Window and step size used for calculating moving averages, e.g. bitrate.
55 // The generated data points will be `step_.ms()` milliseconds apart.
56 // Only events occurring at most `window_duration_.ms()` milliseconds before
57 // the current data point will be part of the average.
58 TimeDelta window_duration_ = TimeDelta::Millis(250);
59 TimeDelta step_ = TimeDelta::Millis(10);
60
61 // First and last events of the log.
62 Timestamp begin_time_ = Timestamp::MinusInfinity();
63 Timestamp end_time_ = Timestamp::MinusInfinity();
64 TimeDelta rtc_to_utc_offset_ = TimeDelta::Zero();
65 bool normalize_time_;
66 };
67
68 struct LayerDescription {
LayerDescriptionLayerDescription69 LayerDescription(uint32_t ssrc, uint8_t spatial_layer, uint8_t temporal_layer)
70 : ssrc(ssrc),
71 spatial_layer(spatial_layer),
72 temporal_layer(temporal_layer) {}
73 bool operator<(const LayerDescription& other) const {
74 if (ssrc != other.ssrc)
75 return ssrc < other.ssrc;
76 if (spatial_layer != other.spatial_layer)
77 return spatial_layer < other.spatial_layer;
78 return temporal_layer < other.temporal_layer;
79 }
80 uint32_t ssrc;
81 uint8_t spatial_layer;
82 uint8_t temporal_layer;
83 };
84
85 bool IsRtxSsrc(const ParsedRtcEventLog& parsed_log,
86 PacketDirection direction,
87 uint32_t ssrc);
88 bool IsVideoSsrc(const ParsedRtcEventLog& parsed_log,
89 PacketDirection direction,
90 uint32_t ssrc);
91 bool IsAudioSsrc(const ParsedRtcEventLog& parsed_log,
92 PacketDirection direction,
93 uint32_t ssrc);
94
95 std::string GetStreamName(const ParsedRtcEventLog& parsed_log,
96 PacketDirection direction,
97 uint32_t ssrc);
98 std::string GetLayerName(LayerDescription layer);
99
100 // For each element in data_view, use `f()` to extract a y-coordinate and
101 // store the result in a TimeSeries.
102 template <typename DataType, typename IterableType>
ProcessPoints(rtc::FunctionView<float (const DataType &)> fx,rtc::FunctionView<absl::optional<float> (const DataType &)> fy,const IterableType & data_view,TimeSeries * result)103 void ProcessPoints(rtc::FunctionView<float(const DataType&)> fx,
104 rtc::FunctionView<absl::optional<float>(const DataType&)> fy,
105 const IterableType& data_view,
106 TimeSeries* result) {
107 for (size_t i = 0; i < data_view.size(); i++) {
108 const DataType& elem = data_view[i];
109 float x = fx(elem);
110 absl::optional<float> y = fy(elem);
111 if (y)
112 result->points.emplace_back(x, *y);
113 }
114 }
115
116 // For each pair of adjacent elements in `data`, use `f()` to extract a
117 // y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
118 // will be the time of the second element in the pair.
119 template <typename DataType, typename ResultType, typename IterableType>
ProcessPairs(rtc::FunctionView<float (const DataType &)> fx,rtc::FunctionView<absl::optional<ResultType> (const DataType &,const DataType &)> fy,const IterableType & data,TimeSeries * result)120 void ProcessPairs(
121 rtc::FunctionView<float(const DataType&)> fx,
122 rtc::FunctionView<absl::optional<ResultType>(const DataType&,
123 const DataType&)> fy,
124 const IterableType& data,
125 TimeSeries* result) {
126 for (size_t i = 1; i < data.size(); i++) {
127 float x = fx(data[i]);
128 absl::optional<ResultType> y = fy(data[i - 1], data[i]);
129 if (y)
130 result->points.emplace_back(x, static_cast<float>(*y));
131 }
132 }
133
134 // For each pair of adjacent elements in `data`, use `f()` to extract a
135 // y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
136 // will be the time of the second element in the pair.
137 template <typename DataType, typename ResultType, typename IterableType>
AccumulatePairs(rtc::FunctionView<float (const DataType &)> fx,rtc::FunctionView<absl::optional<ResultType> (const DataType &,const DataType &)> fy,const IterableType & data,TimeSeries * result)138 void AccumulatePairs(
139 rtc::FunctionView<float(const DataType&)> fx,
140 rtc::FunctionView<absl::optional<ResultType>(const DataType&,
141 const DataType&)> fy,
142 const IterableType& data,
143 TimeSeries* result) {
144 ResultType sum = 0;
145 for (size_t i = 1; i < data.size(); i++) {
146 float x = fx(data[i]);
147 absl::optional<ResultType> y = fy(data[i - 1], data[i]);
148 if (y) {
149 sum += *y;
150 result->points.emplace_back(x, static_cast<float>(sum));
151 }
152 }
153 }
154
155 // Calculates a moving average of `data` and stores the result in a TimeSeries.
156 // A data point is generated every `step` microseconds from `begin_time`
157 // to `end_time`. The value of each data point is the average of the data
158 // during the preceding `window_duration_us` microseconds.
159 template <typename DataType, typename ResultType, typename IterableType>
MovingAverage(rtc::FunctionView<absl::optional<ResultType> (const DataType &)> fy,const IterableType & data_view,AnalyzerConfig config,TimeSeries * result)160 void MovingAverage(
161 rtc::FunctionView<absl::optional<ResultType>(const DataType&)> fy,
162 const IterableType& data_view,
163 AnalyzerConfig config,
164 TimeSeries* result) {
165 size_t window_index_begin = 0;
166 size_t window_index_end = 0;
167 ResultType sum_in_window = 0;
168
169 for (Timestamp t = config.begin_time_; t < config.end_time_ + config.step_;
170 t += config.step_) {
171 while (window_index_end < data_view.size() &&
172 data_view[window_index_end].log_time() < t) {
173 absl::optional<ResultType> value = fy(data_view[window_index_end]);
174 if (value)
175 sum_in_window += *value;
176 ++window_index_end;
177 }
178 while (window_index_begin < data_view.size() &&
179 data_view[window_index_begin].log_time() <
180 t - config.window_duration_) {
181 absl::optional<ResultType> value = fy(data_view[window_index_begin]);
182 if (value)
183 sum_in_window -= *value;
184 ++window_index_begin;
185 }
186 float window_duration_s =
187 static_cast<float>(config.window_duration_.us()) / kNumMicrosecsPerSec;
188 float x = config.GetCallTimeSec(t);
189 float y = sum_in_window / window_duration_s;
190 result->points.emplace_back(x, y);
191 }
192 }
193
194 } // namespace webrtc
195
196 #endif // RTC_TOOLS_RTC_EVENT_LOG_VISUALIZER_ANALYZER_COMMON_H_
197