blob: bed9f875ee24e6d104d334c88275493345d1984e [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 Brandt23772262022-05-23 09:53:15 +020011#include "modules/video_coding/timing/inter_frame_delay.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Evan Shrubsolee9126c12022-03-07 14:50:51 +010013#include "absl/types/optional.h"
14#include "api/units/frequency.h"
15#include "api/units/time_delta.h"
16#include "modules/include/module_common_types_public.h"
17
niklase@google.com470e71d2011-07-07 08:21:25 +000018namespace webrtc {
19
Evan Shrubsolee9126c12022-03-07 14:50:51 +010020namespace {
21constexpr Frequency k90kHz = Frequency::KiloHertz(90);
22}
23
Rasmus Brandt23772262022-05-23 09:53:15 +020024InterFrameDelay::InterFrameDelay() {
Evan Shrubsolee9126c12022-03-07 14:50:51 +010025 Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000026}
27
Åsa Persson288cbe82019-04-01 14:31:46 +020028// Resets the delay estimate.
Rasmus Brandt23772262022-05-23 09:53:15 +020029void InterFrameDelay::Reset() {
Evan Shrubsolee9126c12022-03-07 14:50:51 +010030 prev_wall_clock_ = absl::nullopt;
31 prev_rtp_timestamp_unwrapped_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
34// Calculates the delay of a frame with the given timestamp.
35// This method is called when the frame is complete.
Rasmus Brandt23772262022-05-23 09:53:15 +020036absl::optional<TimeDelta> InterFrameDelay::CalculateDelay(
Evan Shrubsolee9126c12022-03-07 14:50:51 +010037 uint32_t rtp_timestamp,
38 Timestamp now) {
39 int64_t rtp_timestamp_unwrapped = unwrapper_.Unwrap(rtp_timestamp);
40 if (!prev_wall_clock_) {
Åsa Persson288cbe82019-04-01 14:31:46 +020041 // First set of data, initialization, wait for next frame.
Evan Shrubsolee9126c12022-03-07 14:50:51 +010042 prev_wall_clock_ = now;
43 prev_rtp_timestamp_unwrapped_ = rtp_timestamp_unwrapped;
44 return TimeDelta::Zero();
philipel9d3ab612015-12-21 04:12:39 -080045 }
46
philipel9d3ab612015-12-21 04:12:39 -080047 // Account for reordering in jitter variance estimate in the future?
Åsa Persson288cbe82019-04-01 14:31:46 +020048 // Note that this also captures incomplete frames which are grabbed for
49 // decoding after a later frame has been complete, i.e. real packet losses.
Evan Shrubsolee9126c12022-03-07 14:50:51 +010050 uint32_t cropped_last = static_cast<uint32_t>(prev_rtp_timestamp_unwrapped_);
51 if (rtp_timestamp_unwrapped < prev_rtp_timestamp_unwrapped_ ||
52 !IsNewerTimestamp(rtp_timestamp, cropped_last)) {
53 return absl::nullopt;
philipel9d3ab612015-12-21 04:12:39 -080054 }
55
Evan Shrubsolee9126c12022-03-07 14:50:51 +010056 // Compute the compensated timestamp difference.
57 int64_t d_rtp_ticks = rtp_timestamp_unwrapped - prev_rtp_timestamp_unwrapped_;
58 TimeDelta dts = d_rtp_ticks / k90kHz;
59 TimeDelta dt = now - *prev_wall_clock_;
philipel9d3ab612015-12-21 04:12:39 -080060
Åsa Persson288cbe82019-04-01 14:31:46 +020061 // frameDelay is the difference of dT and dTS -- i.e. the difference of the
62 // wall clock time difference and the timestamp difference between two
63 // following frames.
Evan Shrubsolee9126c12022-03-07 14:50:51 +010064 TimeDelta delay = dt - dts;
philipel9d3ab612015-12-21 04:12:39 -080065
Evan Shrubsolee9126c12022-03-07 14:50:51 +010066 prev_rtp_timestamp_unwrapped_ = rtp_timestamp_unwrapped;
67 prev_wall_clock_ = now;
68 return delay;
niklase@google.com470e71d2011-07-07 08:21:25 +000069}
70
philipel9d3ab612015-12-21 04:12:39 -080071} // namespace webrtc