blob: 2362409d71e3a1b63fc3ddb9ce8736353ec7dde2 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 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
Rasmus Brandt10944e62022-05-25 10:12:42 +020011#ifndef MODULES_VIDEO_CODING_TIMING_JITTER_ESTIMATOR_H_
12#define MODULES_VIDEO_CODING_TIMING_JITTER_ESTIMATOR_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Evan Shrubsole13e42a82022-03-07 13:21:51 +010014#include "absl/types/optional.h"
Jonas Orelande62c2f22022-03-29 11:04:48 +020015#include "api/field_trials_view.h"
Evan Shrubsole13e42a82022-03-07 13:21:51 +010016#include "api/units/data_size.h"
17#include "api/units/frequency.h"
18#include "api/units/time_delta.h"
19#include "api/units/timestamp.h"
Rasmus Brandt39ae6962022-08-09 14:40:05 +020020#include "modules/video_coding/timing/frame_delay_delta_kalman_filter.h"
Rasmus Brandt23772262022-05-23 09:53:15 +020021#include "modules/video_coding/timing/rtt_filter.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/rolling_accumulator.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
philipel9d3ab612015-12-21 04:12:39 -080024namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000025
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000026class Clock;
27
Rasmus Brandt10944e62022-05-25 10:12:42 +020028class JitterEstimator {
philipel9d3ab612015-12-21 04:12:39 -080029 public:
Rasmus Brandt10944e62022-05-25 10:12:42 +020030 explicit JitterEstimator(Clock* clock, const FieldTrialsView& field_trials);
Rasmus Brandt24a8e0d2022-08-12 16:39:25 +020031 ~JitterEstimator();
Rasmus Brandt10944e62022-05-25 10:12:42 +020032 JitterEstimator(const JitterEstimator&) = delete;
33 JitterEstimator& operator=(const JitterEstimator&) = delete;
niklase@google.com470e71d2011-07-07 08:21:25 +000034
Åsa Persson3fcc5be2019-04-04 09:40:27 +020035 // Resets the estimate to the initial state.
philipel9d3ab612015-12-21 04:12:39 -080036 void Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000037
philipel9d3ab612015-12-21 04:12:39 -080038 // Updates the jitter estimate with the new data.
39 //
40 // Input:
Evan Shrubsole13e42a82022-03-07 13:21:51 +010041 // - frame_delay : Delay-delta calculated by UTILDelayEstimate.
42 // - frame_size : Frame size of the current frame.
philipele1c707c2022-07-05 14:03:25 +020043 void UpdateEstimate(TimeDelta frame_delay, DataSize frame_size);
niklase@google.com470e71d2011-07-07 08:21:25 +000044
Evan Shrubsole13e42a82022-03-07 13:21:51 +010045 // Returns the current jitter estimate and adds an RTT dependent term in cases
46 // of retransmission.
philipel9d3ab612015-12-21 04:12:39 -080047 // Input:
Evan Shrubsole13e42a82022-03-07 13:21:51 +010048 // - rtt_multiplier : RTT param multiplier (when applicable).
49 // - rtt_mult_add_cap : Multiplier cap from the RTTMultExperiment.
philipel9d3ab612015-12-21 04:12:39 -080050 //
Evan Shrubsole13e42a82022-03-07 13:21:51 +010051 // Return value : Jitter estimate.
Rasmus Brandt24a8e0d2022-08-12 16:39:25 +020052 TimeDelta GetJitterEstimate(double rtt_multiplier,
53 absl::optional<TimeDelta> rtt_mult_add_cap);
niklase@google.com470e71d2011-07-07 08:21:25 +000054
philipel9d3ab612015-12-21 04:12:39 -080055 // Updates the nack counter.
56 void FrameNacked();
niklase@google.com470e71d2011-07-07 08:21:25 +000057
philipel9d3ab612015-12-21 04:12:39 -080058 // Updates the RTT filter.
59 //
60 // Input:
Evan Shrubsole13e42a82022-03-07 13:21:51 +010061 // - rtt : Round trip time.
62 void UpdateRtt(TimeDelta rtt);
niklase@google.com470e71d2011-07-07 08:21:25 +000063
Åsa Persson3fcc5be2019-04-04 09:40:27 +020064 // A constant describing the delay from the jitter buffer to the delay on the
65 // receiving side which is not accounted for by the jitter buffer nor the
66 // decoding delay estimate.
Evan Shrubsole13e42a82022-03-07 13:21:51 +010067 static constexpr TimeDelta OPERATING_SYSTEM_JITTER = TimeDelta::Millis(10);
niklase@google.com470e71d2011-07-07 08:21:25 +000068
philipel9d3ab612015-12-21 04:12:39 -080069 private:
Rasmus Brandt39ae6962022-08-09 14:40:05 +020070 double var_noise_; // Variance of the time-deviation from the line
niklase@google.com470e71d2011-07-07 08:21:25 +000071
Åsa Persson3fcc5be2019-04-04 09:40:27 +020072 // Updates the random jitter estimate, i.e. the variance of the time
73 // deviations from the line given by the Kalman filter.
philipel9d3ab612015-12-21 04:12:39 -080074 //
75 // Input:
Åsa Persson3fcc5be2019-04-04 09:40:27 +020076 // - d_dT : The deviation from the kalman estimate.
philipele1c707c2022-07-05 14:03:25 +020077 void EstimateRandomJitter(double d_dT);
niklase@google.com470e71d2011-07-07 08:21:25 +000078
philipel9d3ab612015-12-21 04:12:39 -080079 double NoiseThreshold() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000080
philipel9d3ab612015-12-21 04:12:39 -080081 // Calculates the current jitter estimate.
82 //
Evan Shrubsole13e42a82022-03-07 13:21:51 +010083 // Return value : The current jitter estimate.
84 TimeDelta CalculateEstimate();
niklase@google.com470e71d2011-07-07 08:21:25 +000085
Åsa Persson3fcc5be2019-04-04 09:40:27 +020086 // Post process the calculated estimate.
philipel9d3ab612015-12-21 04:12:39 -080087 void PostProcessEstimate();
niklase@google.com470e71d2011-07-07 08:21:25 +000088
Evan Shrubsole13e42a82022-03-07 13:21:51 +010089 Frequency GetFrameRate() const;
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000090
Rasmus Brandt39ae6962022-08-09 14:40:05 +020091 // Filters the {frame_delay_delta, frame_size_delta} measurements through
92 // a linear Kalman filter.
93 FrameDelayDeltaKalmanFilter kalman_filter_;
niklase@google.com470e71d2011-07-07 08:21:25 +000094
philipel55a9a3d2022-08-19 13:23:26 +020095 // TODO(bugs.webrtc.org/14381): Update `avg_frame_size_bytes_` to DataSize
96 // when api/units have sufficient precision.
97 double avg_frame_size_bytes_; // Average frame size
Evan Shrubsole13e42a82022-03-07 13:21:51 +010098 double var_frame_size_; // Frame size variance. Unit is bytes^2.
99 // Largest frame size received (descending with a factor kPsi)
philipel55a9a3d2022-08-19 13:23:26 +0200100 // TODO(bugs.webrtc.org/14381): Update `max_frame_size_bytes_` to DataSize
101 // when api/units have sufficient precision.
102 double max_frame_size_bytes_;
103 // TODO(bugs.webrtc.org/14381): Update `frame_size_sum_bytes_` to DataSize
104 // when api/units have sufficient precision.
105 double frame_size_sum_bytes_;
Evan Shrubsole13e42a82022-03-07 13:21:51 +0100106 uint32_t frame_size_count_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
Evan Shrubsole13e42a82022-03-07 13:21:51 +0100108 absl::optional<Timestamp> last_update_time_;
109 // The previously returned jitter estimate
110 absl::optional<TimeDelta> prev_estimate_;
111 // Frame size of the previous frame
112 absl::optional<DataSize> prev_frame_size_;
113 // Average of the random jitter
114 double avg_noise_;
115 uint32_t alpha_count_;
116 // The filtered sum of jitter estimates
117 TimeDelta filter_jitter_estimate_ = TimeDelta::Zero();
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
Evan Shrubsole13e42a82022-03-07 13:21:51 +0100119 uint32_t startup_count_;
120 // Time when the latest nack was seen
121 Timestamp latest_nack_ = Timestamp::Zero();
122 // Keeps track of the number of nacks received, but never goes above
123 // kNackLimit.
124 uint32_t nack_count_;
Rasmus Brandt23772262022-05-23 09:53:15 +0200125 RttFilter rtt_filter_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
Evan Shrubsole13e42a82022-03-07 13:21:51 +0100127 // Tracks frame rates in microseconds.
philipel9d3ab612015-12-21 04:12:39 -0800128 rtc::RollingAccumulator<uint64_t> fps_counter_;
Sebastian Janssonaa01f272019-01-30 11:28:59 +0100129 Clock* clock_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130};
131
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000132} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
Rasmus Brandt10944e62022-05-25 10:12:42 +0200134#endif // MODULES_VIDEO_CODING_TIMING_JITTER_ESTIMATOR_H_