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