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.

116 lines
4.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2012 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 "common_video/video_render_frames.h"
  11. #include <type_traits>
  12. #include <utility>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/logging.h"
  15. #include "rtc_base/time_utils.h"
  16. #include "system_wrappers/include/metrics.h"
  17. namespace webrtc {
  18. namespace {
  19. // Don't render frames with timestamp older than 500ms from now.
  20. const int kOldRenderTimestampMS = 500;
  21. // Don't render frames with timestamp more than 10s into the future.
  22. const int kFutureRenderTimestampMS = 10000;
  23. const uint32_t kEventMaxWaitTimeMs = 200;
  24. const uint32_t kMinRenderDelayMs = 10;
  25. const uint32_t kMaxRenderDelayMs = 500;
  26. const size_t kMaxIncomingFramesBeforeLogged = 100;
  27. uint32_t EnsureValidRenderDelay(uint32_t render_delay) {
  28. return (render_delay < kMinRenderDelayMs || render_delay > kMaxRenderDelayMs)
  29. ? kMinRenderDelayMs
  30. : render_delay;
  31. }
  32. } // namespace
  33. VideoRenderFrames::VideoRenderFrames(uint32_t render_delay_ms)
  34. : render_delay_ms_(EnsureValidRenderDelay(render_delay_ms)) {}
  35. VideoRenderFrames::~VideoRenderFrames() {
  36. frames_dropped_ += incoming_frames_.size();
  37. RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.DroppedFrames.RenderQueue",
  38. frames_dropped_);
  39. RTC_LOG(LS_INFO) << "WebRTC.Video.DroppedFrames.RenderQueue "
  40. << frames_dropped_;
  41. }
  42. int32_t VideoRenderFrames::AddFrame(VideoFrame&& new_frame) {
  43. const int64_t time_now = rtc::TimeMillis();
  44. // Drop old frames only when there are other frames in the queue, otherwise, a
  45. // really slow system never renders any frames.
  46. if (!incoming_frames_.empty() &&
  47. new_frame.render_time_ms() + kOldRenderTimestampMS < time_now) {
  48. RTC_LOG(LS_WARNING) << "Too old frame, timestamp=" << new_frame.timestamp();
  49. ++frames_dropped_;
  50. return -1;
  51. }
  52. if (new_frame.render_time_ms() > time_now + kFutureRenderTimestampMS) {
  53. RTC_LOG(LS_WARNING) << "Frame too long into the future, timestamp="
  54. << new_frame.timestamp();
  55. ++frames_dropped_;
  56. return -1;
  57. }
  58. if (new_frame.render_time_ms() < last_render_time_ms_) {
  59. RTC_LOG(LS_WARNING) << "Frame scheduled out of order, render_time="
  60. << new_frame.render_time_ms()
  61. << ", latest=" << last_render_time_ms_;
  62. // For more details, see bug:
  63. // https://bugs.chromium.org/p/webrtc/issues/detail?id=7253
  64. ++frames_dropped_;
  65. return -1;
  66. }
  67. last_render_time_ms_ = new_frame.render_time_ms();
  68. incoming_frames_.emplace_back(std::move(new_frame));
  69. if (incoming_frames_.size() > kMaxIncomingFramesBeforeLogged) {
  70. RTC_LOG(LS_WARNING) << "Stored incoming frames: "
  71. << incoming_frames_.size();
  72. }
  73. return static_cast<int32_t>(incoming_frames_.size());
  74. }
  75. absl::optional<VideoFrame> VideoRenderFrames::FrameToRender() {
  76. absl::optional<VideoFrame> render_frame;
  77. // Get the newest frame that can be released for rendering.
  78. while (!incoming_frames_.empty() && TimeToNextFrameRelease() <= 0) {
  79. if (render_frame) {
  80. ++frames_dropped_;
  81. }
  82. render_frame = std::move(incoming_frames_.front());
  83. incoming_frames_.pop_front();
  84. }
  85. return render_frame;
  86. }
  87. uint32_t VideoRenderFrames::TimeToNextFrameRelease() {
  88. if (incoming_frames_.empty()) {
  89. return kEventMaxWaitTimeMs;
  90. }
  91. const int64_t time_to_release = incoming_frames_.front().render_time_ms() -
  92. render_delay_ms_ - rtc::TimeMillis();
  93. return time_to_release < 0 ? 0u : static_cast<uint32_t>(time_to_release);
  94. }
  95. bool VideoRenderFrames::HasPendingFrames() const {
  96. return !incoming_frames_.empty();
  97. }
  98. } // namespace webrtc