blob: d0c21aa771c932a715907ce857a2d0f101db6a28 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/inter_frame_delay.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13namespace webrtc {
14
philipel9d3ab612015-12-21 04:12:39 -080015VCMInterFrameDelay::VCMInterFrameDelay(int64_t currentWallClock) {
16 Reset(currentWallClock);
niklase@google.com470e71d2011-07-07 08:21:25 +000017}
18
Åsa Persson288cbe82019-04-01 14:31:46 +020019// Resets the delay estimate.
philipel9d3ab612015-12-21 04:12:39 -080020void VCMInterFrameDelay::Reset(int64_t currentWallClock) {
21 _zeroWallClock = currentWallClock;
22 _wrapArounds = 0;
23 _prevWallClock = 0;
24 _prevTimestamp = 0;
25 _dTS = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000026}
27
28// Calculates the delay of a frame with the given timestamp.
29// This method is called when the frame is complete.
philipel9d3ab612015-12-21 04:12:39 -080030bool VCMInterFrameDelay::CalculateDelay(uint32_t timestamp,
31 int64_t* delay,
32 int64_t currentWallClock) {
33 if (_prevWallClock == 0) {
Åsa Persson288cbe82019-04-01 14:31:46 +020034 // First set of data, initialization, wait for next frame.
niklase@google.com470e71d2011-07-07 08:21:25 +000035 _prevWallClock = currentWallClock;
philipel9d3ab612015-12-21 04:12:39 -080036 _prevTimestamp = timestamp;
37 *delay = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000038 return true;
philipel9d3ab612015-12-21 04:12:39 -080039 }
40
41 int32_t prevWrapArounds = _wrapArounds;
42 CheckForWrapArounds(timestamp);
43
Åsa Persson288cbe82019-04-01 14:31:46 +020044 // This will be -1 for backward wrap arounds and +1 for forward wrap arounds.
philipel9d3ab612015-12-21 04:12:39 -080045 int32_t wrapAroundsSincePrev = _wrapArounds - prevWrapArounds;
46
47 // 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.
philipel9d3ab612015-12-21 04:12:39 -080050 if ((wrapAroundsSincePrev == 0 && timestamp < _prevTimestamp) ||
51 wrapAroundsSincePrev < 0) {
52 *delay = 0;
53 return false;
54 }
55
Åsa Persson288cbe82019-04-01 14:31:46 +020056 // Compute the compensated timestamp difference and convert it to ms and round
57 // it to closest integer.
philipel9d3ab612015-12-21 04:12:39 -080058 _dTS = static_cast<int64_t>(
59 (timestamp + wrapAroundsSincePrev * (static_cast<int64_t>(1) << 32) -
60 _prevTimestamp) /
61 90.0 +
62 0.5);
63
Åsa Persson288cbe82019-04-01 14:31:46 +020064 // frameDelay is the difference of dT and dTS -- i.e. the difference of the
65 // wall clock time difference and the timestamp difference between two
66 // following frames.
philipel9d3ab612015-12-21 04:12:39 -080067 *delay = static_cast<int64_t>(currentWallClock - _prevWallClock - _dTS);
68
69 _prevTimestamp = timestamp;
70 _prevWallClock = currentWallClock;
71
72 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +000073}
74
philipel9d3ab612015-12-21 04:12:39 -080075// Investigates if the timestamp clock has overflowed since the last timestamp
Åsa Persson288cbe82019-04-01 14:31:46 +020076// and keeps track of the number of wrap arounds since reset.
philipel9d3ab612015-12-21 04:12:39 -080077void VCMInterFrameDelay::CheckForWrapArounds(uint32_t timestamp) {
78 if (timestamp < _prevTimestamp) {
79 // This difference will probably be less than -2^31 if we have had a wrap
Åsa Persson288cbe82019-04-01 14:31:46 +020080 // around (e.g. timestamp = 1, _prevTimestamp = 2^32 - 1). Since it is cast
81 // to a int32_t, it should be positive.
philipel9d3ab612015-12-21 04:12:39 -080082 if (static_cast<int32_t>(timestamp - _prevTimestamp) > 0) {
Åsa Persson288cbe82019-04-01 14:31:46 +020083 // Forward wrap around.
philipel9d3ab612015-12-21 04:12:39 -080084 _wrapArounds++;
niklase@google.com470e71d2011-07-07 08:21:25 +000085 }
philipel9d3ab612015-12-21 04:12:39 -080086 // This difference will probably be less than -2^31 if we have had a
Åsa Persson288cbe82019-04-01 14:31:46 +020087 // backward wrap around. Since it is cast to a int32_t, it should be
88 // positive.
philipel9d3ab612015-12-21 04:12:39 -080089 } else if (static_cast<int32_t>(_prevTimestamp - timestamp) > 0) {
Åsa Persson288cbe82019-04-01 14:31:46 +020090 // Backward wrap around.
philipel9d3ab612015-12-21 04:12:39 -080091 _wrapArounds--;
92 }
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
philipel9d3ab612015-12-21 04:12:39 -080094} // namespace webrtc