• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 float kLeftMargin = 0.01f;
26 constexpr float kRightMargin = 0.02f;
27 constexpr float kBottomMargin = 0.02f;
28 constexpr float kTopMargin = 0.05f;
29 
30 class AnalyzerConfig {
31  public:
GetCallTimeSec(int64_t timestamp_us)32   float GetCallTimeSec(int64_t timestamp_us) const {
33     int64_t offset = normalize_time_ ? begin_time_ : 0;
34     return static_cast<float>(timestamp_us - offset) / 1000000;
35   }
36 
CallBeginTimeSec()37   float CallBeginTimeSec() const { return GetCallTimeSec(begin_time_); }
38 
CallEndTimeSec()39   float CallEndTimeSec() const { return GetCallTimeSec(end_time_); }
40 
41   // Window and step size used for calculating moving averages, e.g. bitrate.
42   // The generated data points will be |step_| microseconds apart.
43   // Only events occurring at most |window_duration_| microseconds before the
44   // current data point will be part of the average.
45   int64_t window_duration_;
46   int64_t step_;
47 
48   // First and last events of the log.
49   int64_t begin_time_;
50   int64_t end_time_;
51   bool normalize_time_;
52 };
53 
54 struct LayerDescription {
LayerDescriptionLayerDescription55   LayerDescription(uint32_t ssrc, uint8_t spatial_layer, uint8_t temporal_layer)
56       : ssrc(ssrc),
57         spatial_layer(spatial_layer),
58         temporal_layer(temporal_layer) {}
59   bool operator<(const LayerDescription& other) const {
60     if (ssrc != other.ssrc)
61       return ssrc < other.ssrc;
62     if (spatial_layer != other.spatial_layer)
63       return spatial_layer < other.spatial_layer;
64     return temporal_layer < other.temporal_layer;
65   }
66   uint32_t ssrc;
67   uint8_t spatial_layer;
68   uint8_t temporal_layer;
69 };
70 
71 bool IsRtxSsrc(const ParsedRtcEventLog& parsed_log,
72                PacketDirection direction,
73                uint32_t ssrc);
74 bool IsVideoSsrc(const ParsedRtcEventLog& parsed_log,
75                  PacketDirection direction,
76                  uint32_t ssrc);
77 bool IsAudioSsrc(const ParsedRtcEventLog& parsed_log,
78                  PacketDirection direction,
79                  uint32_t ssrc);
80 
81 std::string GetStreamName(const ParsedRtcEventLog& parsed_log,
82                           PacketDirection direction,
83                           uint32_t ssrc);
84 std::string GetLayerName(LayerDescription layer);
85 
86 // For each element in data_view, use |f()| to extract a y-coordinate and
87 // store the result in a TimeSeries.
88 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)89 void ProcessPoints(rtc::FunctionView<float(const DataType&)> fx,
90                    rtc::FunctionView<absl::optional<float>(const DataType&)> fy,
91                    const IterableType& data_view,
92                    TimeSeries* result) {
93   for (size_t i = 0; i < data_view.size(); i++) {
94     const DataType& elem = data_view[i];
95     float x = fx(elem);
96     absl::optional<float> y = fy(elem);
97     if (y)
98       result->points.emplace_back(x, *y);
99   }
100 }
101 
102 // For each pair of adjacent elements in |data|, use |f()| to extract a
103 // y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
104 // will be the time of the second element in the pair.
105 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)106 void ProcessPairs(
107     rtc::FunctionView<float(const DataType&)> fx,
108     rtc::FunctionView<absl::optional<ResultType>(const DataType&,
109                                                  const DataType&)> fy,
110     const IterableType& data,
111     TimeSeries* result) {
112   for (size_t i = 1; i < data.size(); i++) {
113     float x = fx(data[i]);
114     absl::optional<ResultType> y = fy(data[i - 1], data[i]);
115     if (y)
116       result->points.emplace_back(x, static_cast<float>(*y));
117   }
118 }
119 
120 // For each pair of adjacent elements in |data|, use |f()| to extract a
121 // y-coordinate and store the result in a TimeSeries. Note that the x-coordinate
122 // will be the time of the second element in the pair.
123 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)124 void AccumulatePairs(
125     rtc::FunctionView<float(const DataType&)> fx,
126     rtc::FunctionView<absl::optional<ResultType>(const DataType&,
127                                                  const DataType&)> fy,
128     const IterableType& data,
129     TimeSeries* result) {
130   ResultType sum = 0;
131   for (size_t i = 1; i < data.size(); i++) {
132     float x = fx(data[i]);
133     absl::optional<ResultType> y = fy(data[i - 1], data[i]);
134     if (y) {
135       sum += *y;
136       result->points.emplace_back(x, static_cast<float>(sum));
137     }
138   }
139 }
140 
141 // Calculates a moving average of |data| and stores the result in a TimeSeries.
142 // A data point is generated every |step| microseconds from |begin_time|
143 // to |end_time|. The value of each data point is the average of the data
144 // during the preceding |window_duration_us| microseconds.
145 template <typename DataType, typename ResultType, typename IterableType>
MovingAverage(rtc::FunctionView<absl::optional<ResultType> (const DataType &)> fy,const IterableType & data_view,AnalyzerConfig config,TimeSeries * result)146 void MovingAverage(
147     rtc::FunctionView<absl::optional<ResultType>(const DataType&)> fy,
148     const IterableType& data_view,
149     AnalyzerConfig config,
150     TimeSeries* result) {
151   size_t window_index_begin = 0;
152   size_t window_index_end = 0;
153   ResultType sum_in_window = 0;
154 
155   for (int64_t t = config.begin_time_; t < config.end_time_ + config.step_;
156        t += config.step_) {
157     while (window_index_end < data_view.size() &&
158            data_view[window_index_end].log_time_us() < t) {
159       absl::optional<ResultType> value = fy(data_view[window_index_end]);
160       if (value)
161         sum_in_window += *value;
162       ++window_index_end;
163     }
164     while (window_index_begin < data_view.size() &&
165            data_view[window_index_begin].log_time_us() <
166                t - config.window_duration_) {
167       absl::optional<ResultType> value = fy(data_view[window_index_begin]);
168       if (value)
169         sum_in_window -= *value;
170       ++window_index_begin;
171     }
172     float window_duration_s =
173         static_cast<float>(config.window_duration_) / kNumMicrosecsPerSec;
174     float x = config.GetCallTimeSec(t);
175     float y = sum_in_window / window_duration_s;
176     result->points.emplace_back(x, y);
177   }
178 }
179 
180 }  // namespace webrtc
181 
182 #endif  // RTC_TOOLS_RTC_EVENT_LOG_VISUALIZER_ANALYZER_COMMON_H_
183