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 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame^] | 11 | #include "webrtc/modules/video_coding/inter_frame_delay.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | namespace webrtc { |
| 14 | |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 15 | VCMInterFrameDelay::VCMInterFrameDelay(int64_t currentWallClock) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | { |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 17 | Reset(currentWallClock); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | // Resets the delay estimate |
| 21 | void |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 22 | VCMInterFrameDelay::Reset(int64_t currentWallClock) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | { |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 24 | _zeroWallClock = currentWallClock; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | _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. |
| 33 | bool |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 34 | VCMInterFrameDelay::CalculateDelay(uint32_t timestamp, |
| 35 | int64_t *delay, |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 36 | int64_t currentWallClock) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | 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.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 47 | int32_t prevWrapArounds = _wrapArounds; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | CheckForWrapArounds(timestamp); |
| 49 | |
| 50 | // This will be -1 for backward wrap arounds and +1 for forward wrap arounds |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 51 | int32_t wrapAroundsSincePrev = _wrapArounds - prevWrapArounds; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | |
| 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.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 65 | _dTS = static_cast<int64_t>((timestamp + wrapAroundsSincePrev * |
| 66 | (static_cast<int64_t>(1)<<32) - _prevTimestamp) / 90.0 + 0.5); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | |
| 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.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 71 | *delay = static_cast<int64_t>(currentWallClock - _prevWallClock - _dTS); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | |
| 73 | _prevTimestamp = timestamp; |
| 74 | _prevWallClock = currentWallClock; |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | // Returns the current difference between incoming timestamps |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 80 | uint32_t VCMInterFrameDelay::CurrentTimeStampDiffMs() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | { |
| 82 | if (_dTS < 0) |
| 83 | { |
| 84 | return 0; |
| 85 | } |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 86 | return static_cast<uint32_t>(_dTS); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | } |
| 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. |
| 91 | void |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 92 | VCMInterFrameDelay::CheckForWrapArounds(uint32_t timestamp) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | { |
| 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.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 99 | if (static_cast<int32_t>(timestamp - _prevTimestamp) > 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | { |
| 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.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 107 | else if (static_cast<int32_t>(_prevTimestamp - timestamp) > 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | { |
| 109 | // Backward wrap around |
| 110 | _wrapArounds--; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | } |