Rasmus Brandt | cde5354 | 2023-06-08 08:41:25 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 TEST_JITTER_DELAY_VARIATION_CALCULATOR_H_ |
| 12 | #define TEST_JITTER_DELAY_VARIATION_CALCULATOR_H_ |
| 13 | |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | #include <map> |
| 17 | #include <string> |
| 18 | |
| 19 | #include "absl/types/optional.h" |
| 20 | #include "api/numerics/samples_stats_counter.h" |
| 21 | #include "api/test/metrics/metrics_logger.h" |
| 22 | #include "api/units/data_size.h" |
| 23 | #include "api/units/timestamp.h" |
| 24 | #include "api/video/video_frame_type.h" |
| 25 | #include "rtc_base/numerics/sequence_number_unwrapper.h" |
| 26 | |
| 27 | namespace webrtc { |
| 28 | namespace test { |
| 29 | |
| 30 | // Helper class for calculating different delay variation statistics for "RTP |
| 31 | // frame arrival events". One use case is gathering statistics from |
| 32 | // RtcEventLogs. Another use case is online logging of data in test calls. |
| 33 | class DelayVariationCalculator { |
| 34 | public: |
| 35 | struct TimeSeries { |
| 36 | // Time series of RTP timestamps `t(n)` for each frame `n`. |
| 37 | SamplesStatsCounter rtp_timestamps; |
| 38 | // Time series of local arrival timestamps `r(n)` for each frame. |
| 39 | SamplesStatsCounter arrival_times_ms; |
| 40 | // Time series of sizes `s(n)` for each frame. |
| 41 | SamplesStatsCounter sizes_bytes; |
| 42 | // Time series of `d_t(n) = t(n) - t(n-1)` for each frame. |
| 43 | SamplesStatsCounter inter_departure_times_ms; |
| 44 | // Time series of `d_r(n) = r(n) - r(n-1)` for each frame. |
| 45 | SamplesStatsCounter inter_arrival_times_ms; |
| 46 | // Time series of `d_r(n) - d_t(n) = (r(n) - r(n-1)) - (t(n) - t(n-1))` |
| 47 | // for each frame. |
| 48 | SamplesStatsCounter inter_delay_variations_ms; |
| 49 | // Time series of `s(n) - s(n-1)`, for each frame. |
| 50 | SamplesStatsCounter inter_size_variations_bytes; |
| 51 | }; |
| 52 | |
| 53 | DelayVariationCalculator() = default; |
| 54 | ~DelayVariationCalculator() = default; |
| 55 | |
| 56 | void Insert(uint32_t rtp_timestamp, |
| 57 | Timestamp arrival_time, |
| 58 | DataSize size, |
| 59 | absl::optional<int> spatial_layer = absl::nullopt, |
| 60 | absl::optional<int> temporal_layer = absl::nullopt, |
| 61 | absl::optional<VideoFrameType> frame_type = absl::nullopt); |
| 62 | |
| 63 | const TimeSeries& time_series() const { return time_series_; } |
| 64 | |
| 65 | private: |
| 66 | struct Frame { |
| 67 | uint32_t rtp_timestamp; |
| 68 | int64_t unwrapped_rtp_timestamp; |
| 69 | Timestamp arrival_time; |
| 70 | DataSize size; |
| 71 | absl::optional<int> spatial_layer; |
| 72 | absl::optional<int> temporal_layer; |
| 73 | absl::optional<VideoFrameType> frame_type; |
| 74 | }; |
| 75 | using MetadataT = std::map<std::string, std::string>; |
| 76 | |
| 77 | void InsertFirstFrame(const Frame& frame, |
| 78 | Timestamp sample_time, |
| 79 | MetadataT sample_metadata); |
| 80 | void InsertFrame(const Frame& frame, |
| 81 | Timestamp sample_time, |
| 82 | MetadataT sample_metadata); |
| 83 | |
| 84 | MetadataT BuildMetadata(const Frame& frame); |
| 85 | |
| 86 | RtpTimestampUnwrapper unwrapper_; |
| 87 | absl::optional<Frame> prev_frame_ = absl::nullopt; |
| 88 | TimeSeries time_series_; |
| 89 | }; |
| 90 | |
| 91 | } // namespace test |
| 92 | } // namespace webrtc |
| 93 | |
| 94 | #endif // TEST_JITTER_DELAY_VARIATION_CALCULATOR_H_ |