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.

228 lines
7.8 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 "video/call_stats.h"
  11. #include <algorithm>
  12. #include <memory>
  13. #include "absl/algorithm/container.h"
  14. #include "modules/utility/include/process_thread.h"
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/location.h"
  17. #include "rtc_base/task_utils/to_queued_task.h"
  18. #include "system_wrappers/include/metrics.h"
  19. namespace webrtc {
  20. namespace {
  21. void RemoveOldReports(int64_t now, std::list<CallStats::RttTime>* reports) {
  22. static constexpr const int64_t kRttTimeoutMs = 1500;
  23. reports->remove_if(
  24. [&now](CallStats::RttTime& r) { return now - r.time > kRttTimeoutMs; });
  25. }
  26. int64_t GetMaxRttMs(const std::list<CallStats::RttTime>& reports) {
  27. int64_t max_rtt_ms = -1;
  28. for (const CallStats::RttTime& rtt_time : reports)
  29. max_rtt_ms = std::max(rtt_time.rtt, max_rtt_ms);
  30. return max_rtt_ms;
  31. }
  32. int64_t GetAvgRttMs(const std::list<CallStats::RttTime>& reports) {
  33. RTC_DCHECK(!reports.empty());
  34. int64_t sum = 0;
  35. for (std::list<CallStats::RttTime>::const_iterator it = reports.begin();
  36. it != reports.end(); ++it) {
  37. sum += it->rtt;
  38. }
  39. return sum / reports.size();
  40. }
  41. int64_t GetNewAvgRttMs(const std::list<CallStats::RttTime>& reports,
  42. int64_t prev_avg_rtt) {
  43. if (reports.empty())
  44. return -1; // Reset (invalid average).
  45. int64_t cur_rtt_ms = GetAvgRttMs(reports);
  46. if (prev_avg_rtt == -1)
  47. return cur_rtt_ms; // New initial average value.
  48. // Weight factor to apply to the average rtt.
  49. // We weigh the old average at 70% against the new average (30%).
  50. constexpr const float kWeightFactor = 0.3f;
  51. return prev_avg_rtt * (1.0f - kWeightFactor) + cur_rtt_ms * kWeightFactor;
  52. }
  53. // This class is used to de-register a Module from a ProcessThread to satisfy
  54. // threading requirements of the Module (CallStats).
  55. // The guarantee offered by TemporaryDeregistration is that while its in scope,
  56. // no calls to |TimeUntilNextProcess| or |Process()| will occur and therefore
  57. // synchronization with those methods, is not necessary.
  58. class TemporaryDeregistration {
  59. public:
  60. TemporaryDeregistration(Module* module,
  61. ProcessThread* process_thread,
  62. bool thread_running)
  63. : module_(module),
  64. process_thread_(process_thread),
  65. deregistered_(thread_running) {
  66. if (thread_running)
  67. process_thread_->DeRegisterModule(module_);
  68. }
  69. ~TemporaryDeregistration() {
  70. if (deregistered_)
  71. process_thread_->RegisterModule(module_, RTC_FROM_HERE);
  72. }
  73. private:
  74. Module* const module_;
  75. ProcessThread* const process_thread_;
  76. const bool deregistered_;
  77. };
  78. } // namespace
  79. CallStats::CallStats(Clock* clock, ProcessThread* process_thread)
  80. : clock_(clock),
  81. last_process_time_(clock_->TimeInMilliseconds()),
  82. max_rtt_ms_(-1),
  83. avg_rtt_ms_(-1),
  84. sum_avg_rtt_ms_(0),
  85. num_avg_rtt_(0),
  86. time_of_first_rtt_ms_(-1),
  87. process_thread_(process_thread),
  88. process_thread_running_(false) {
  89. RTC_DCHECK(process_thread_);
  90. process_thread_checker_.Detach();
  91. }
  92. CallStats::~CallStats() {
  93. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  94. RTC_DCHECK(!process_thread_running_);
  95. RTC_DCHECK(observers_.empty());
  96. UpdateHistograms();
  97. }
  98. int64_t CallStats::TimeUntilNextProcess() {
  99. RTC_DCHECK_RUN_ON(&process_thread_checker_);
  100. return last_process_time_ + kUpdateIntervalMs - clock_->TimeInMilliseconds();
  101. }
  102. void CallStats::Process() {
  103. RTC_DCHECK_RUN_ON(&process_thread_checker_);
  104. int64_t now = clock_->TimeInMilliseconds();
  105. last_process_time_ = now;
  106. // |avg_rtt_ms_| is allowed to be read on the process thread since that's the
  107. // only thread that modifies the value.
  108. int64_t avg_rtt_ms = avg_rtt_ms_;
  109. RemoveOldReports(now, &reports_);
  110. max_rtt_ms_ = GetMaxRttMs(reports_);
  111. avg_rtt_ms = GetNewAvgRttMs(reports_, avg_rtt_ms);
  112. {
  113. rtc::CritScope lock(&avg_rtt_ms_lock_);
  114. avg_rtt_ms_ = avg_rtt_ms;
  115. }
  116. // If there is a valid rtt, update all observers with the max rtt.
  117. if (max_rtt_ms_ >= 0) {
  118. RTC_DCHECK_GE(avg_rtt_ms, 0);
  119. for (CallStatsObserver* observer : observers_)
  120. observer->OnRttUpdate(avg_rtt_ms, max_rtt_ms_);
  121. // Sum for Histogram of average RTT reported over the entire call.
  122. sum_avg_rtt_ms_ += avg_rtt_ms;
  123. ++num_avg_rtt_;
  124. }
  125. }
  126. void CallStats::ProcessThreadAttached(ProcessThread* process_thread) {
  127. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  128. RTC_DCHECK(!process_thread || process_thread_ == process_thread);
  129. process_thread_running_ = process_thread != nullptr;
  130. // Whether we just got attached or detached, we clear the
  131. // |process_thread_checker_| so that it can be used to protect variables
  132. // in either the process thread when it starts again, or UpdateHistograms()
  133. // (mutually exclusive).
  134. process_thread_checker_.Detach();
  135. }
  136. void CallStats::RegisterStatsObserver(CallStatsObserver* observer) {
  137. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  138. TemporaryDeregistration deregister(this, process_thread_,
  139. process_thread_running_);
  140. if (!absl::c_linear_search(observers_, observer))
  141. observers_.push_back(observer);
  142. }
  143. void CallStats::DeregisterStatsObserver(CallStatsObserver* observer) {
  144. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  145. TemporaryDeregistration deregister(this, process_thread_,
  146. process_thread_running_);
  147. observers_.remove(observer);
  148. }
  149. int64_t CallStats::LastProcessedRtt() const {
  150. // TODO(tommi): This currently gets called from the construction thread of
  151. // Call as well as from the process thread. Look into restricting this to
  152. // allow only reading this from the process thread (or TQ once we get there)
  153. // so that the lock isn't necessary.
  154. rtc::CritScope cs(&avg_rtt_ms_lock_);
  155. return avg_rtt_ms_;
  156. }
  157. void CallStats::OnRttUpdate(int64_t rtt) {
  158. RTC_DCHECK_RUN_ON(&process_thread_checker_);
  159. int64_t now_ms = clock_->TimeInMilliseconds();
  160. reports_.push_back(RttTime(rtt, now_ms));
  161. if (time_of_first_rtt_ms_ == -1)
  162. time_of_first_rtt_ms_ = now_ms;
  163. // Make sure Process() will be called and deliver the updates asynchronously.
  164. last_process_time_ -= kUpdateIntervalMs;
  165. process_thread_->WakeUp(this);
  166. }
  167. void CallStats::UpdateHistograms() {
  168. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  169. RTC_DCHECK(!process_thread_running_);
  170. // The extra scope is because we have two 'dcheck run on' thread checkers.
  171. // This is a special case since it's safe to access variables on the current
  172. // thread that normally are only touched on the process thread.
  173. // Since we're not attached to the process thread and/or the process thread
  174. // isn't running, it's OK to touch these variables here.
  175. {
  176. // This method is called on the ctor thread (usually from the dtor, unless
  177. // a test calls it). It's a requirement that the function be called when
  178. // the process thread is not running (a condition that's met at destruction
  179. // time), and thanks to that, we don't need a lock to synchronize against
  180. // it.
  181. RTC_DCHECK_RUN_ON(&process_thread_checker_);
  182. if (time_of_first_rtt_ms_ == -1 || num_avg_rtt_ < 1)
  183. return;
  184. int64_t elapsed_sec =
  185. (clock_->TimeInMilliseconds() - time_of_first_rtt_ms_) / 1000;
  186. if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
  187. int64_t avg_rtt_ms = (sum_avg_rtt_ms_ + num_avg_rtt_ / 2) / num_avg_rtt_;
  188. RTC_HISTOGRAM_COUNTS_10000(
  189. "WebRTC.Video.AverageRoundTripTimeInMilliseconds", avg_rtt_ms);
  190. }
  191. }
  192. }
  193. } // namespace webrtc