capture_scheduler.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/protocol/capture_scheduler.h"
  5. #include <algorithm>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/check_op.h"
  9. #include "base/system/sys_info.h"
  10. #include "base/time/default_tick_clock.h"
  11. #include "base/time/time.h"
  12. #include "remoting/proto/video.pb.h"
  13. namespace {
  14. // Number of samples to average the most recent capture and encode time
  15. // over.
  16. const int kStatisticsWindow = 3;
  17. // The hard limit is 30fps or 33ms per recording cycle.
  18. const int64_t kDefaultMinimumIntervalMs = 33;
  19. // Controls how much CPU time we can use for encode and capture.
  20. // Range of this value is between 0 to 1. 0 means using 0% of of all CPUs
  21. // available while 1 means using 100% of all CPUs available.
  22. const double kRecordingCpuConsumption = 0.5;
  23. // Maximum number of captured frames in the encoding queue. Currently capturer
  24. // implementations do not allow to keep more than 2 DesktopFrame objects.
  25. static const int kMaxFramesInEncodingQueue = 2;
  26. // Maximum number of unacknowledged frames. Ignored if the client doesn't
  27. // support ACKs. This value was chosen experimentally, using synthetic
  28. // performance tests (see ProtocolPerfTest), to maximize frame rate, while
  29. // keeping round-trip latency low.
  30. static const int kMaxUnacknowledgedFrames = 4;
  31. } // namespace
  32. namespace remoting {
  33. namespace protocol {
  34. // We assume that the number of available cores is constant.
  35. CaptureScheduler::CaptureScheduler(
  36. const base::RepeatingClosure& capture_closure)
  37. : capture_closure_(capture_closure),
  38. tick_clock_(base::DefaultTickClock::GetInstance()),
  39. capture_timer_(new base::OneShotTimer()),
  40. minimum_interval_(base::Milliseconds(kDefaultMinimumIntervalMs)),
  41. num_of_processors_(base::SysInfo::NumberOfProcessors()),
  42. capture_time_(kStatisticsWindow),
  43. encode_time_(kStatisticsWindow),
  44. num_encoding_frames_(0),
  45. num_unacknowledged_frames_(0),
  46. capture_pending_(false),
  47. is_paused_(false),
  48. next_frame_id_(0) {
  49. DCHECK(num_of_processors_);
  50. }
  51. CaptureScheduler::~CaptureScheduler() {
  52. DCHECK(thread_checker_.CalledOnValidThread());
  53. }
  54. void CaptureScheduler::Start() {
  55. DCHECK(thread_checker_.CalledOnValidThread());
  56. ScheduleNextCapture();
  57. }
  58. void CaptureScheduler::Pause(bool pause) {
  59. DCHECK(thread_checker_.CalledOnValidThread());
  60. if (is_paused_ != pause) {
  61. is_paused_ = pause;
  62. if (is_paused_) {
  63. capture_timer_->Stop();
  64. } else {
  65. ScheduleNextCapture();
  66. }
  67. }
  68. }
  69. void CaptureScheduler::OnCaptureCompleted() {
  70. DCHECK(thread_checker_.CalledOnValidThread());
  71. capture_pending_ = false;
  72. capture_time_.Record(
  73. (tick_clock_->NowTicks() - last_capture_started_time_).InMilliseconds());
  74. ++num_encoding_frames_;
  75. ScheduleNextCapture();
  76. }
  77. void CaptureScheduler::OnFrameEncoded(VideoPacket* packet) {
  78. DCHECK(thread_checker_.CalledOnValidThread());
  79. // Set packet_id for the outgoing packet.
  80. packet->set_frame_id(next_frame_id_);
  81. ++next_frame_id_;
  82. // Update internal stats.
  83. encode_time_.Record(packet->encode_time_ms());
  84. --num_encoding_frames_;
  85. ++num_unacknowledged_frames_;
  86. ScheduleNextCapture();
  87. }
  88. void CaptureScheduler::OnFrameSent() {
  89. DCHECK(thread_checker_.CalledOnValidThread());
  90. ScheduleNextCapture();
  91. }
  92. void CaptureScheduler::ProcessVideoAck(std::unique_ptr<VideoAck> video_ack) {
  93. DCHECK(thread_checker_.CalledOnValidThread());
  94. --num_unacknowledged_frames_;
  95. DCHECK_GE(num_unacknowledged_frames_, 0);
  96. ScheduleNextCapture();
  97. }
  98. void CaptureScheduler::SetTickClockForTest(const base::TickClock* tick_clock) {
  99. tick_clock_ = tick_clock;
  100. }
  101. void CaptureScheduler::SetTimerForTest(
  102. std::unique_ptr<base::OneShotTimer> timer) {
  103. capture_timer_ = std::move(timer);
  104. }
  105. void CaptureScheduler::SetNumOfProcessorsForTest(int num_of_processors) {
  106. num_of_processors_ = num_of_processors;
  107. }
  108. void CaptureScheduler::ScheduleNextCapture() {
  109. DCHECK(thread_checker_.CalledOnValidThread());
  110. if (is_paused_ || capture_pending_ ||
  111. num_encoding_frames_ >= kMaxFramesInEncodingQueue) {
  112. return;
  113. }
  114. if (num_encoding_frames_ + num_unacknowledged_frames_ >=
  115. kMaxUnacknowledgedFrames) {
  116. return;
  117. }
  118. // Delay by an amount chosen such that if capture and encode times
  119. // continue to follow the averages, then we'll consume the target
  120. // fraction of CPU across all cores.
  121. base::TimeDelta delay = std::max(
  122. minimum_interval_,
  123. base::Milliseconds((capture_time_.Average() + encode_time_.Average()) /
  124. (kRecordingCpuConsumption * num_of_processors_)));
  125. // Account for the time that has passed since the last capture.
  126. delay = std::max(base::TimeDelta(), delay - (tick_clock_->NowTicks() -
  127. last_capture_started_time_));
  128. capture_timer_->Start(FROM_HERE, delay,
  129. base::BindOnce(&CaptureScheduler::CaptureNextFrame,
  130. base::Unretained(this)));
  131. }
  132. void CaptureScheduler::CaptureNextFrame() {
  133. DCHECK(thread_checker_.CalledOnValidThread());
  134. DCHECK(!is_paused_);
  135. DCHECK(!capture_pending_);
  136. capture_pending_ = true;
  137. last_capture_started_time_ = tick_clock_->NowTicks();
  138. capture_closure_.Run();
  139. }
  140. } // namespace protocol
  141. } // namespace remoting