blob: 97cdd8ac5bd666d1abafa9bd2dd6a175a704245c [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
Henrik Kjellander2557b862015-11-18 22:00:21 +010011#include "webrtc/modules/video_coding/inter_frame_delay.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13namespace webrtc {
14
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +000015VCMInterFrameDelay::VCMInterFrameDelay(int64_t currentWallClock)
niklase@google.com470e71d2011-07-07 08:21:25 +000016{
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +000017 Reset(currentWallClock);
niklase@google.com470e71d2011-07-07 08:21:25 +000018}
19
20// Resets the delay estimate
21void
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +000022VCMInterFrameDelay::Reset(int64_t currentWallClock)
niklase@google.com470e71d2011-07-07 08:21:25 +000023{
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +000024 _zeroWallClock = currentWallClock;
niklase@google.com470e71d2011-07-07 08:21:25 +000025 _wrapArounds = 0;
26 _prevWallClock = 0;
27 _prevTimestamp = 0;
28 _dTS = 0;
29}
30
31// Calculates the delay of a frame with the given timestamp.
32// This method is called when the frame is complete.
33bool
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000034VCMInterFrameDelay::CalculateDelay(uint32_t timestamp,
35 int64_t *delay,
henrik.lundin@webrtc.org7d8c72e2011-12-21 15:24:01 +000036 int64_t currentWallClock)
niklase@google.com470e71d2011-07-07 08:21:25 +000037{
niklase@google.com470e71d2011-07-07 08:21:25 +000038 if (_prevWallClock == 0)
39 {
40 // First set of data, initialization, wait for next frame
41 _prevWallClock = currentWallClock;
42 _prevTimestamp = timestamp;
43 *delay = 0;
44 return true;
45 }
46
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000047 int32_t prevWrapArounds = _wrapArounds;
niklase@google.com470e71d2011-07-07 08:21:25 +000048 CheckForWrapArounds(timestamp);
49
50 // This will be -1 for backward wrap arounds and +1 for forward wrap arounds
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000051 int32_t wrapAroundsSincePrev = _wrapArounds - prevWrapArounds;
niklase@google.com470e71d2011-07-07 08:21:25 +000052
53 // Account for reordering in jitter variance estimate in the future?
54 // Note that this also captures incomplete frames which are grabbed
55 // for decoding after a later frame has been complete, i.e. real
56 // packet losses.
57 if ((wrapAroundsSincePrev == 0 && timestamp < _prevTimestamp) || wrapAroundsSincePrev < 0)
58 {
59 *delay = 0;
60 return false;
61 }
62
63 // Compute the compensated timestamp difference and convert it to ms and
64 // round it to closest integer.
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000065 _dTS = static_cast<int64_t>((timestamp + wrapAroundsSincePrev *
66 (static_cast<int64_t>(1)<<32) - _prevTimestamp) / 90.0 + 0.5);
niklase@google.com470e71d2011-07-07 08:21:25 +000067
68 // frameDelay is the difference of dT and dTS -- i.e. the difference of
69 // the wall clock time difference and the timestamp difference between
70 // two following frames.
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000071 *delay = static_cast<int64_t>(currentWallClock - _prevWallClock - _dTS);
niklase@google.com470e71d2011-07-07 08:21:25 +000072
73 _prevTimestamp = timestamp;
74 _prevWallClock = currentWallClock;
75
76 return true;
77}
78
79// Returns the current difference between incoming timestamps
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000080uint32_t VCMInterFrameDelay::CurrentTimeStampDiffMs() const
niklase@google.com470e71d2011-07-07 08:21:25 +000081{
82 if (_dTS < 0)
83 {
84 return 0;
85 }
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000086 return static_cast<uint32_t>(_dTS);
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
89// Investigates if the timestamp clock has overflowed since the last timestamp and
90// keeps track of the number of wrap arounds since reset.
91void
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000092VCMInterFrameDelay::CheckForWrapArounds(uint32_t timestamp)
niklase@google.com470e71d2011-07-07 08:21:25 +000093{
94 if (timestamp < _prevTimestamp)
95 {
96 // This difference will probably be less than -2^31 if we have had a wrap around
97 // (e.g. timestamp = 1, _previousTimestamp = 2^32 - 1). Since it is cast to a Word32,
98 // it should be positive.
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000099 if (static_cast<int32_t>(timestamp - _prevTimestamp) > 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000100 {
101 // Forward wrap around
102 _wrapArounds++;
103 }
104 }
105 // This difference will probably be less than -2^31 if we have had a backward wrap around.
106 // Since it is cast to a Word32, it should be positive.
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000107 else if (static_cast<int32_t>(_prevTimestamp - timestamp) > 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000108 {
109 // Backward wrap around
110 _wrapArounds--;
111 }
112}
113
114}