PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/CSipSimple/jni/webrtc/sources/modules/video_coding/main/source/inter_frame_delay.cc

https://bitbucket.org/bohlooli/csipsimple
C++ | 114 lines | 66 code | 15 blank | 33 comment | 11 complexity | 2ad0a7b0feb79f40243f03f58002525c MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, LGPL-3.0, GPL-3.0, GPL-2.0
  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. #include "inter_frame_delay.h"
  11. namespace webrtc {
  12. VCMInterFrameDelay::VCMInterFrameDelay(int64_t currentWallClock)
  13. {
  14. Reset(currentWallClock);
  15. }
  16. // Resets the delay estimate
  17. void
  18. VCMInterFrameDelay::Reset(int64_t currentWallClock)
  19. {
  20. _zeroWallClock = currentWallClock;
  21. _wrapArounds = 0;
  22. _prevWallClock = 0;
  23. _prevTimestamp = 0;
  24. _dTS = 0;
  25. }
  26. // Calculates the delay of a frame with the given timestamp.
  27. // This method is called when the frame is complete.
  28. bool
  29. VCMInterFrameDelay::CalculateDelay(WebRtc_UWord32 timestamp,
  30. WebRtc_Word64 *delay,
  31. int64_t currentWallClock)
  32. {
  33. if (_prevWallClock == 0)
  34. {
  35. // First set of data, initialization, wait for next frame
  36. _prevWallClock = currentWallClock;
  37. _prevTimestamp = timestamp;
  38. *delay = 0;
  39. return true;
  40. }
  41. WebRtc_Word32 prevWrapArounds = _wrapArounds;
  42. CheckForWrapArounds(timestamp);
  43. // This will be -1 for backward wrap arounds and +1 for forward wrap arounds
  44. WebRtc_Word32 wrapAroundsSincePrev = _wrapArounds - prevWrapArounds;
  45. // Account for reordering in jitter variance estimate in the future?
  46. // Note that this also captures incomplete frames which are grabbed
  47. // for decoding after a later frame has been complete, i.e. real
  48. // packet losses.
  49. if ((wrapAroundsSincePrev == 0 && timestamp < _prevTimestamp) || wrapAroundsSincePrev < 0)
  50. {
  51. *delay = 0;
  52. return false;
  53. }
  54. // Compute the compensated timestamp difference and convert it to ms and
  55. // round it to closest integer.
  56. _dTS = static_cast<WebRtc_Word64>((timestamp + wrapAroundsSincePrev *
  57. (static_cast<WebRtc_Word64>(1)<<32) - _prevTimestamp) / 90.0 + 0.5);
  58. // frameDelay is the difference of dT and dTS -- i.e. the difference of
  59. // the wall clock time difference and the timestamp difference between
  60. // two following frames.
  61. *delay = static_cast<WebRtc_Word64>(currentWallClock - _prevWallClock - _dTS);
  62. _prevTimestamp = timestamp;
  63. _prevWallClock = currentWallClock;
  64. return true;
  65. }
  66. // Returns the current difference between incoming timestamps
  67. WebRtc_UWord32 VCMInterFrameDelay::CurrentTimeStampDiffMs() const
  68. {
  69. if (_dTS < 0)
  70. {
  71. return 0;
  72. }
  73. return static_cast<WebRtc_UWord32>(_dTS);
  74. }
  75. // Investigates if the timestamp clock has overflowed since the last timestamp and
  76. // keeps track of the number of wrap arounds since reset.
  77. void
  78. VCMInterFrameDelay::CheckForWrapArounds(WebRtc_UWord32 timestamp)
  79. {
  80. if (timestamp < _prevTimestamp)
  81. {
  82. // This difference will probably be less than -2^31 if we have had a wrap around
  83. // (e.g. timestamp = 1, _previousTimestamp = 2^32 - 1). Since it is cast to a Word32,
  84. // it should be positive.
  85. if (static_cast<WebRtc_Word32>(timestamp - _prevTimestamp) > 0)
  86. {
  87. // Forward wrap around
  88. _wrapArounds++;
  89. }
  90. }
  91. // This difference will probably be less than -2^31 if we have had a backward wrap around.
  92. // Since it is cast to a Word32, it should be positive.
  93. else if (static_cast<WebRtc_Word32>(_prevTimestamp - timestamp) > 0)
  94. {
  95. // Backward wrap around
  96. _wrapArounds--;
  97. }
  98. }
  99. }