You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
7.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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 "modules/video_coding/timing.h"
  11. #include <assert.h>
  12. #include <algorithm>
  13. #include "rtc_base/time/timestamp_extrapolator.h"
  14. #include "system_wrappers/include/clock.h"
  15. namespace webrtc {
  16. VCMTiming::VCMTiming(Clock* clock, VCMTiming* master_timing)
  17. : clock_(clock),
  18. master_(false),
  19. ts_extrapolator_(),
  20. codec_timer_(new VCMCodecTimer()),
  21. render_delay_ms_(kDefaultRenderDelayMs),
  22. min_playout_delay_ms_(0),
  23. max_playout_delay_ms_(10000),
  24. jitter_delay_ms_(0),
  25. current_delay_ms_(0),
  26. prev_frame_timestamp_(0),
  27. timing_frame_info_(),
  28. num_decoded_frames_(0) {
  29. if (master_timing == NULL) {
  30. master_ = true;
  31. ts_extrapolator_ = new TimestampExtrapolator(clock_->TimeInMilliseconds());
  32. } else {
  33. ts_extrapolator_ = master_timing->ts_extrapolator_;
  34. }
  35. }
  36. VCMTiming::~VCMTiming() {
  37. if (master_) {
  38. delete ts_extrapolator_;
  39. }
  40. }
  41. void VCMTiming::Reset() {
  42. rtc::CritScope cs(&crit_sect_);
  43. ts_extrapolator_->Reset(clock_->TimeInMilliseconds());
  44. codec_timer_.reset(new VCMCodecTimer());
  45. render_delay_ms_ = kDefaultRenderDelayMs;
  46. min_playout_delay_ms_ = 0;
  47. jitter_delay_ms_ = 0;
  48. current_delay_ms_ = 0;
  49. prev_frame_timestamp_ = 0;
  50. }
  51. void VCMTiming::set_render_delay(int render_delay_ms) {
  52. rtc::CritScope cs(&crit_sect_);
  53. render_delay_ms_ = render_delay_ms;
  54. }
  55. void VCMTiming::set_min_playout_delay(int min_playout_delay_ms) {
  56. rtc::CritScope cs(&crit_sect_);
  57. min_playout_delay_ms_ = min_playout_delay_ms;
  58. }
  59. int VCMTiming::min_playout_delay() {
  60. rtc::CritScope cs(&crit_sect_);
  61. return min_playout_delay_ms_;
  62. }
  63. void VCMTiming::set_max_playout_delay(int max_playout_delay_ms) {
  64. rtc::CritScope cs(&crit_sect_);
  65. max_playout_delay_ms_ = max_playout_delay_ms;
  66. }
  67. int VCMTiming::max_playout_delay() {
  68. rtc::CritScope cs(&crit_sect_);
  69. return max_playout_delay_ms_;
  70. }
  71. void VCMTiming::SetJitterDelay(int jitter_delay_ms) {
  72. rtc::CritScope cs(&crit_sect_);
  73. if (jitter_delay_ms != jitter_delay_ms_) {
  74. jitter_delay_ms_ = jitter_delay_ms;
  75. // When in initial state, set current delay to minimum delay.
  76. if (current_delay_ms_ == 0) {
  77. current_delay_ms_ = jitter_delay_ms_;
  78. }
  79. }
  80. }
  81. void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
  82. rtc::CritScope cs(&crit_sect_);
  83. int target_delay_ms = TargetDelayInternal();
  84. if (current_delay_ms_ == 0) {
  85. // Not initialized, set current delay to target.
  86. current_delay_ms_ = target_delay_ms;
  87. } else if (target_delay_ms != current_delay_ms_) {
  88. int64_t delay_diff_ms =
  89. static_cast<int64_t>(target_delay_ms) - current_delay_ms_;
  90. // Never change the delay with more than 100 ms every second. If we're
  91. // changing the delay in too large steps we will get noticeable freezes. By
  92. // limiting the change we can increase the delay in smaller steps, which
  93. // will be experienced as the video is played in slow motion. When lowering
  94. // the delay the video will be played at a faster pace.
  95. int64_t max_change_ms = 0;
  96. if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
  97. // wrap
  98. max_change_ms = kDelayMaxChangeMsPerS *
  99. (frame_timestamp + (static_cast<int64_t>(1) << 32) -
  100. prev_frame_timestamp_) /
  101. 90000;
  102. } else {
  103. max_change_ms = kDelayMaxChangeMsPerS *
  104. (frame_timestamp - prev_frame_timestamp_) / 90000;
  105. }
  106. if (max_change_ms <= 0) {
  107. // Any changes less than 1 ms are truncated and will be postponed.
  108. // Negative change will be due to reordering and should be ignored.
  109. return;
  110. }
  111. delay_diff_ms = std::max(delay_diff_ms, -max_change_ms);
  112. delay_diff_ms = std::min(delay_diff_ms, max_change_ms);
  113. current_delay_ms_ = current_delay_ms_ + delay_diff_ms;
  114. }
  115. prev_frame_timestamp_ = frame_timestamp;
  116. }
  117. void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms,
  118. int64_t actual_decode_time_ms) {
  119. rtc::CritScope cs(&crit_sect_);
  120. uint32_t target_delay_ms = TargetDelayInternal();
  121. int64_t delayed_ms =
  122. actual_decode_time_ms -
  123. (render_time_ms - RequiredDecodeTimeMs() - render_delay_ms_);
  124. if (delayed_ms < 0) {
  125. return;
  126. }
  127. if (current_delay_ms_ + delayed_ms <= target_delay_ms) {
  128. current_delay_ms_ += delayed_ms;
  129. } else {
  130. current_delay_ms_ = target_delay_ms;
  131. }
  132. }
  133. void VCMTiming::StopDecodeTimer(uint32_t /*time_stamp*/,
  134. int32_t decode_time_ms,
  135. int64_t now_ms,
  136. int64_t /*render_time_ms*/) {
  137. StopDecodeTimer(decode_time_ms, now_ms);
  138. }
  139. void VCMTiming::StopDecodeTimer(int32_t decode_time_ms, int64_t now_ms) {
  140. rtc::CritScope cs(&crit_sect_);
  141. codec_timer_->AddTiming(decode_time_ms, now_ms);
  142. assert(decode_time_ms >= 0);
  143. ++num_decoded_frames_;
  144. }
  145. void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) {
  146. rtc::CritScope cs(&crit_sect_);
  147. ts_extrapolator_->Update(now_ms, time_stamp);
  148. }
  149. int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp,
  150. int64_t now_ms) const {
  151. rtc::CritScope cs(&crit_sect_);
  152. return RenderTimeMsInternal(frame_timestamp, now_ms);
  153. }
  154. int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp,
  155. int64_t now_ms) const {
  156. if (min_playout_delay_ms_ == 0 && max_playout_delay_ms_ == 0) {
  157. // Render as soon as possible.
  158. return 0;
  159. }
  160. int64_t estimated_complete_time_ms =
  161. ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp);
  162. if (estimated_complete_time_ms == -1) {
  163. estimated_complete_time_ms = now_ms;
  164. }
  165. // Make sure the actual delay stays in the range of |min_playout_delay_ms_|
  166. // and |max_playout_delay_ms_|.
  167. int actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_);
  168. actual_delay = std::min(actual_delay, max_playout_delay_ms_);
  169. return estimated_complete_time_ms + actual_delay;
  170. }
  171. int VCMTiming::RequiredDecodeTimeMs() const {
  172. const int decode_time_ms = codec_timer_->RequiredDecodeTimeMs();
  173. assert(decode_time_ms >= 0);
  174. return decode_time_ms;
  175. }
  176. int64_t VCMTiming::MaxWaitingTime(int64_t render_time_ms,
  177. int64_t now_ms) const {
  178. rtc::CritScope cs(&crit_sect_);
  179. const int64_t max_wait_time_ms =
  180. render_time_ms - now_ms - RequiredDecodeTimeMs() - render_delay_ms_;
  181. return max_wait_time_ms;
  182. }
  183. int VCMTiming::TargetVideoDelay() const {
  184. rtc::CritScope cs(&crit_sect_);
  185. return TargetDelayInternal();
  186. }
  187. int VCMTiming::TargetDelayInternal() const {
  188. return std::max(min_playout_delay_ms_,
  189. jitter_delay_ms_ + RequiredDecodeTimeMs() + render_delay_ms_);
  190. }
  191. bool VCMTiming::GetTimings(int* max_decode_ms,
  192. int* current_delay_ms,
  193. int* target_delay_ms,
  194. int* jitter_buffer_ms,
  195. int* min_playout_delay_ms,
  196. int* render_delay_ms) const {
  197. rtc::CritScope cs(&crit_sect_);
  198. *max_decode_ms = RequiredDecodeTimeMs();
  199. *current_delay_ms = current_delay_ms_;
  200. *target_delay_ms = TargetDelayInternal();
  201. *jitter_buffer_ms = jitter_delay_ms_;
  202. *min_playout_delay_ms = min_playout_delay_ms_;
  203. *render_delay_ms = render_delay_ms_;
  204. return (num_decoded_frames_ > 0);
  205. }
  206. void VCMTiming::SetTimingFrameInfo(const TimingFrameInfo& info) {
  207. rtc::CritScope cs(&crit_sect_);
  208. timing_frame_info_.emplace(info);
  209. }
  210. absl::optional<TimingFrameInfo> VCMTiming::GetTimingFrameInfo() {
  211. rtc::CritScope cs(&crit_sect_);
  212. return timing_frame_info_;
  213. }
  214. } // namespace webrtc