timing.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #ifndef MODULES_VIDEO_CODING_TIMING_H_
  11. #define MODULES_VIDEO_CODING_TIMING_H_
  12. #include <memory>
  13. #include "absl/types/optional.h"
  14. #include "api/video/video_timing.h"
  15. #include "modules/video_coding/codec_timer.h"
  16. #include "rtc_base/synchronization/mutex.h"
  17. #include "rtc_base/thread_annotations.h"
  18. namespace webrtc {
  19. class Clock;
  20. class TimestampExtrapolator;
  21. class VCMTiming {
  22. public:
  23. // The primary timing component should be passed
  24. // if this is the dual timing component.
  25. explicit VCMTiming(Clock* clock, VCMTiming* master_timing = NULL);
  26. virtual ~VCMTiming();
  27. // Resets the timing to the initial state.
  28. void Reset();
  29. // Set the amount of time needed to render an image. Defaults to 10 ms.
  30. void set_render_delay(int render_delay_ms);
  31. // Set the minimum time the video must be delayed on the receiver to
  32. // get the desired jitter buffer level.
  33. void SetJitterDelay(int required_delay_ms);
  34. // Set/get the minimum playout delay from capture to render in ms.
  35. void set_min_playout_delay(int min_playout_delay_ms);
  36. int min_playout_delay();
  37. // Set/get the maximum playout delay from capture to render in ms.
  38. void set_max_playout_delay(int max_playout_delay_ms);
  39. int max_playout_delay();
  40. // Increases or decreases the current delay to get closer to the target delay.
  41. // Calculates how long it has been since the previous call to this function,
  42. // and increases/decreases the delay in proportion to the time difference.
  43. void UpdateCurrentDelay(uint32_t frame_timestamp);
  44. // Increases or decreases the current delay to get closer to the target delay.
  45. // Given the actual decode time in ms and the render time in ms for a frame,
  46. // this function calculates how late the frame is and increases the delay
  47. // accordingly.
  48. void UpdateCurrentDelay(int64_t render_time_ms,
  49. int64_t actual_decode_time_ms);
  50. // Stops the decoder timer, should be called when the decoder returns a frame
  51. // or when the decoded frame callback is called.
  52. void StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms);
  53. // TODO(kron): Remove once downstream projects has been changed to use the
  54. // above function.
  55. void StopDecodeTimer(uint32_t time_stamp,
  56. int32_t decode_time_ms,
  57. int64_t now_ms,
  58. int64_t render_time_ms);
  59. // Used to report that a frame is passed to decoding. Updates the timestamp
  60. // filter which is used to map between timestamps and receiver system time.
  61. void IncomingTimestamp(uint32_t time_stamp, int64_t last_packet_time_ms);
  62. // Returns the receiver system time when the frame with timestamp
  63. // |frame_timestamp| should be rendered, assuming that the system time
  64. // currently is |now_ms|.
  65. virtual int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const;
  66. // Returns the maximum time in ms that we can wait for a frame to become
  67. // complete before we must pass it to the decoder.
  68. virtual int64_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const;
  69. // Returns the current target delay which is required delay + decode time +
  70. // render delay.
  71. int TargetVideoDelay() const;
  72. // Return current timing information. Returns true if the first frame has been
  73. // decoded, false otherwise.
  74. virtual bool GetTimings(int* max_decode_ms,
  75. int* current_delay_ms,
  76. int* target_delay_ms,
  77. int* jitter_buffer_ms,
  78. int* min_playout_delay_ms,
  79. int* render_delay_ms) const;
  80. void SetTimingFrameInfo(const TimingFrameInfo& info);
  81. absl::optional<TimingFrameInfo> GetTimingFrameInfo();
  82. enum { kDefaultRenderDelayMs = 10 };
  83. enum { kDelayMaxChangeMsPerS = 100 };
  84. protected:
  85. int RequiredDecodeTimeMs() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  86. int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const
  87. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  88. int TargetDelayInternal() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  89. private:
  90. mutable Mutex mutex_;
  91. Clock* const clock_;
  92. bool master_ RTC_GUARDED_BY(mutex_);
  93. TimestampExtrapolator* ts_extrapolator_ RTC_GUARDED_BY(mutex_);
  94. std::unique_ptr<VCMCodecTimer> codec_timer_ RTC_GUARDED_BY(mutex_);
  95. int render_delay_ms_ RTC_GUARDED_BY(mutex_);
  96. // Best-effort playout delay range for frames from capture to render.
  97. // The receiver tries to keep the delay between |min_playout_delay_ms_|
  98. // and |max_playout_delay_ms_| taking the network jitter into account.
  99. // A special case is where min_playout_delay_ms_ = max_playout_delay_ms_ = 0,
  100. // in which case the receiver tries to play the frames as they arrive.
  101. int min_playout_delay_ms_ RTC_GUARDED_BY(mutex_);
  102. int max_playout_delay_ms_ RTC_GUARDED_BY(mutex_);
  103. int jitter_delay_ms_ RTC_GUARDED_BY(mutex_);
  104. int current_delay_ms_ RTC_GUARDED_BY(mutex_);
  105. uint32_t prev_frame_timestamp_ RTC_GUARDED_BY(mutex_);
  106. absl::optional<TimingFrameInfo> timing_frame_info_ RTC_GUARDED_BY(mutex_);
  107. size_t num_decoded_frames_ RTC_GUARDED_BY(mutex_);
  108. };
  109. } // namespace webrtc
  110. #endif // MODULES_VIDEO_CODING_TIMING_H_