2011-07-07 08:21:25 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/video_coding/inter_frame_delay.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2022-03-07 14:50:51 +01:00
|
|
|
#include "absl/types/optional.h"
|
|
|
|
|
#include "api/units/frequency.h"
|
|
|
|
|
#include "api/units/time_delta.h"
|
|
|
|
|
#include "modules/include/module_common_types_public.h"
|
|
|
|
|
|
2011-07-07 08:21:25 +00:00
|
|
|
namespace webrtc {
|
|
|
|
|
|
2022-03-07 14:50:51 +01:00
|
|
|
namespace {
|
|
|
|
|
constexpr Frequency k90kHz = Frequency::KiloHertz(90);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VCMInterFrameDelay::VCMInterFrameDelay() {
|
|
|
|
|
Reset();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-01 14:31:46 +02:00
|
|
|
// Resets the delay estimate.
|
2022-03-07 14:50:51 +01:00
|
|
|
void VCMInterFrameDelay::Reset() {
|
|
|
|
|
prev_wall_clock_ = absl::nullopt;
|
|
|
|
|
prev_rtp_timestamp_unwrapped_ = 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculates the delay of a frame with the given timestamp.
|
|
|
|
|
// This method is called when the frame is complete.
|
2022-03-07 14:50:51 +01:00
|
|
|
absl::optional<TimeDelta> VCMInterFrameDelay::CalculateDelay(
|
|
|
|
|
uint32_t rtp_timestamp,
|
|
|
|
|
Timestamp now) {
|
|
|
|
|
int64_t rtp_timestamp_unwrapped = unwrapper_.Unwrap(rtp_timestamp);
|
|
|
|
|
if (!prev_wall_clock_) {
|
2019-04-01 14:31:46 +02:00
|
|
|
// First set of data, initialization, wait for next frame.
|
2022-03-07 14:50:51 +01:00
|
|
|
prev_wall_clock_ = now;
|
|
|
|
|
prev_rtp_timestamp_unwrapped_ = rtp_timestamp_unwrapped;
|
|
|
|
|
return TimeDelta::Zero();
|
2015-12-21 04:12:39 -08:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-12-21 04:12:39 -08:00
|
|
|
// Account for reordering in jitter variance estimate in the future?
|
2019-04-01 14:31:46 +02:00
|
|
|
// Note that this also captures incomplete frames which are grabbed for
|
|
|
|
|
// decoding after a later frame has been complete, i.e. real packet losses.
|
2022-03-07 14:50:51 +01:00
|
|
|
uint32_t cropped_last = static_cast<uint32_t>(prev_rtp_timestamp_unwrapped_);
|
|
|
|
|
if (rtp_timestamp_unwrapped < prev_rtp_timestamp_unwrapped_ ||
|
|
|
|
|
!IsNewerTimestamp(rtp_timestamp, cropped_last)) {
|
|
|
|
|
return absl::nullopt;
|
2015-12-21 04:12:39 -08:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2022-03-07 14:50:51 +01:00
|
|
|
// Compute the compensated timestamp difference.
|
|
|
|
|
int64_t d_rtp_ticks = rtp_timestamp_unwrapped - prev_rtp_timestamp_unwrapped_;
|
|
|
|
|
TimeDelta dts = d_rtp_ticks / k90kHz;
|
|
|
|
|
TimeDelta dt = now - *prev_wall_clock_;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2019-04-01 14:31:46 +02:00
|
|
|
// frameDelay is the difference of dT and dTS -- i.e. the difference of the
|
|
|
|
|
// wall clock time difference and the timestamp difference between two
|
|
|
|
|
// following frames.
|
2022-03-07 14:50:51 +01:00
|
|
|
TimeDelta delay = dt - dts;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2022-03-07 14:50:51 +01:00
|
|
|
prev_rtp_timestamp_unwrapped_ = rtp_timestamp_unwrapped;
|
|
|
|
|
prev_wall_clock_ = now;
|
|
|
|
|
return delay;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-21 04:12:39 -08:00
|
|
|
} // namespace webrtc
|