niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 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 Brandt | 2377226 | 2022-05-23 09:53:15 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/timing/inter_frame_delay.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Evan Shrubsole | e9126c1 | 2022-03-07 14:50:51 +0100 | [diff] [blame] | 13 | #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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
Evan Shrubsole | e9126c1 | 2022-03-07 14:50:51 +0100 | [diff] [blame] | 20 | namespace { |
| 21 | constexpr Frequency k90kHz = Frequency::KiloHertz(90); |
| 22 | } |
| 23 | |
Rasmus Brandt | 2377226 | 2022-05-23 09:53:15 +0200 | [diff] [blame] | 24 | InterFrameDelay::InterFrameDelay() { |
Evan Shrubsole | e9126c1 | 2022-03-07 14:50:51 +0100 | [diff] [blame] | 25 | Reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Åsa Persson | 288cbe8 | 2019-04-01 14:31:46 +0200 | [diff] [blame] | 28 | // Resets the delay estimate. |
Rasmus Brandt | 2377226 | 2022-05-23 09:53:15 +0200 | [diff] [blame] | 29 | void InterFrameDelay::Reset() { |
Evan Shrubsole | e9126c1 | 2022-03-07 14:50:51 +0100 | [diff] [blame] | 30 | prev_wall_clock_ = absl::nullopt; |
| 31 | prev_rtp_timestamp_unwrapped_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // Calculates the delay of a frame with the given timestamp. |
| 35 | // This method is called when the frame is complete. |
Rasmus Brandt | 2377226 | 2022-05-23 09:53:15 +0200 | [diff] [blame] | 36 | absl::optional<TimeDelta> InterFrameDelay::CalculateDelay( |
Evan Shrubsole | e9126c1 | 2022-03-07 14:50:51 +0100 | [diff] [blame] | 37 | uint32_t rtp_timestamp, |
| 38 | Timestamp now) { |
| 39 | int64_t rtp_timestamp_unwrapped = unwrapper_.Unwrap(rtp_timestamp); |
| 40 | if (!prev_wall_clock_) { |
Åsa Persson | 288cbe8 | 2019-04-01 14:31:46 +0200 | [diff] [blame] | 41 | // First set of data, initialization, wait for next frame. |
Evan Shrubsole | e9126c1 | 2022-03-07 14:50:51 +0100 | [diff] [blame] | 42 | prev_wall_clock_ = now; |
| 43 | prev_rtp_timestamp_unwrapped_ = rtp_timestamp_unwrapped; |
| 44 | return TimeDelta::Zero(); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 45 | } |
| 46 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 47 | // Account for reordering in jitter variance estimate in the future? |
Åsa Persson | 288cbe8 | 2019-04-01 14:31:46 +0200 | [diff] [blame] | 48 | // 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 Shrubsole | e9126c1 | 2022-03-07 14:50:51 +0100 | [diff] [blame] | 50 | 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; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Evan Shrubsole | e9126c1 | 2022-03-07 14:50:51 +0100 | [diff] [blame] | 56 | // 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_; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 60 | |
Åsa Persson | 288cbe8 | 2019-04-01 14:31:46 +0200 | [diff] [blame] | 61 | // 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 Shrubsole | e9126c1 | 2022-03-07 14:50:51 +0100 | [diff] [blame] | 64 | TimeDelta delay = dt - dts; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 65 | |
Evan Shrubsole | e9126c1 | 2022-03-07 14:50:51 +0100 | [diff] [blame] | 66 | prev_rtp_timestamp_unwrapped_ = rtp_timestamp_unwrapped; |
| 67 | prev_wall_clock_ = now; |
| 68 | return delay; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 71 | } // namespace webrtc |