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 
11 #include "modules/video_coding/inter_frame_delay.h"
12 
13 namespace webrtc {
14 
VCMInterFrameDelay(int64_t currentWallClock)15 VCMInterFrameDelay::VCMInterFrameDelay(int64_t currentWallClock) {
16   Reset(currentWallClock);
17 }
18 
19 // Resets the delay estimate.
Reset(int64_t currentWallClock)20 void VCMInterFrameDelay::Reset(int64_t currentWallClock) {
21   _zeroWallClock = currentWallClock;
22   _wrapArounds = 0;
23   _prevWallClock = 0;
24   _prevTimestamp = 0;
25   _dTS = 0;
26 }
27 
28 // Calculates the delay of a frame with the given timestamp.
29 // This method is called when the frame is complete.
CalculateDelay(uint32_t timestamp,int64_t * delay,int64_t currentWallClock)30 bool VCMInterFrameDelay::CalculateDelay(uint32_t timestamp,
31                                         int64_t* delay,
32                                         int64_t currentWallClock) {
33   if (_prevWallClock == 0) {
34     // First set of data, initialization, wait for next frame.
35     _prevWallClock = currentWallClock;
36     _prevTimestamp = timestamp;
37     *delay = 0;
38     return true;
39   }
40 
41   int32_t prevWrapArounds = _wrapArounds;
42   CheckForWrapArounds(timestamp);
43 
44   // This will be -1 for backward wrap arounds and +1 for forward wrap arounds.
45   int32_t wrapAroundsSincePrev = _wrapArounds - prevWrapArounds;
46 
47   // Account for reordering in jitter variance estimate in the future?
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.
50   if ((wrapAroundsSincePrev == 0 && timestamp < _prevTimestamp) ||
51       wrapAroundsSincePrev < 0) {
52     *delay = 0;
53     return false;
54   }
55 
56   // Compute the compensated timestamp difference and convert it to ms and round
57   // it to closest integer.
58   _dTS = static_cast<int64_t>(
59       (timestamp + wrapAroundsSincePrev * (static_cast<int64_t>(1) << 32) -
60        _prevTimestamp) /
61           90.0 +
62       0.5);
63 
64   // 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.
67   *delay = static_cast<int64_t>(currentWallClock - _prevWallClock - _dTS);
68 
69   _prevTimestamp = timestamp;
70   _prevWallClock = currentWallClock;
71 
72   return true;
73 }
74 
75 // Investigates if the timestamp clock has overflowed since the last timestamp
76 // and keeps track of the number of wrap arounds since reset.
CheckForWrapArounds(uint32_t timestamp)77 void 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
80     // around (e.g. timestamp = 1, _prevTimestamp = 2^32 - 1). Since it is cast
81     // to a int32_t, it should be positive.
82     if (static_cast<int32_t>(timestamp - _prevTimestamp) > 0) {
83       // Forward wrap around.
84       _wrapArounds++;
85     }
86     // This difference will probably be less than -2^31 if we have had a
87     // backward wrap around. Since it is cast to a int32_t, it should be
88     // positive.
89   } else if (static_cast<int32_t>(_prevTimestamp - timestamp) > 0) {
90     // Backward wrap around.
91     _wrapArounds--;
92   }
93 }
94 }  // namespace webrtc
95