call_stats2.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2020 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 VIDEO_CALL_STATS2_H_
  11. #define VIDEO_CALL_STATS2_H_
  12. #include <list>
  13. #include <memory>
  14. #include "api/units/timestamp.h"
  15. #include "modules/include/module_common_types.h"
  16. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/critical_section.h"
  19. #include "rtc_base/synchronization/sequence_checker.h"
  20. #include "rtc_base/task_queue.h"
  21. #include "rtc_base/task_utils/pending_task_safety_flag.h"
  22. #include "rtc_base/task_utils/repeating_task.h"
  23. #include "system_wrappers/include/clock.h"
  24. namespace webrtc {
  25. namespace internal {
  26. class CallStats {
  27. public:
  28. // Time interval for updating the observers.
  29. static constexpr TimeDelta kUpdateInterval = TimeDelta::Millis(1000);
  30. CallStats(Clock* clock, TaskQueueBase* task_queue);
  31. ~CallStats();
  32. // Expose an RtcpRttStats implementation without inheriting from RtcpRttStats.
  33. // That allows us to separate the threading model of how RtcpRttStats is
  34. // used (mostly on a process thread) and how CallStats is used (mostly on
  35. // the TQ/worker thread). Since for both cases, there is a LastProcessedRtt()
  36. // method, this separation allows us to not need a lock for either.
  37. RtcpRttStats* AsRtcpRttStats() { return &rtcp_rtt_stats_impl_; }
  38. // Registers/deregisters a new observer to receive statistics updates.
  39. // Must be called from the construction thread.
  40. void RegisterStatsObserver(CallStatsObserver* observer);
  41. void DeregisterStatsObserver(CallStatsObserver* observer);
  42. // Expose |LastProcessedRtt()| from RtcpRttStats to the public interface, as
  43. // it is the part of the API that is needed by direct users of CallStats.
  44. // TODO(tommi): Threading or lifetime guarantees are not explicit in how
  45. // CallStats is used as RtcpRttStats or how pointers are cached in a
  46. // few different places (distributed via Call). It would be good to clarify
  47. // from what thread/TQ calls to OnRttUpdate and LastProcessedRtt need to be
  48. // allowed.
  49. int64_t LastProcessedRtt() const;
  50. // Exposed for tests to test histogram support.
  51. void UpdateHistogramsForTest() { UpdateHistograms(); }
  52. // Helper struct keeping track of the time a rtt value is reported.
  53. struct RttTime {
  54. RttTime(int64_t new_rtt, int64_t rtt_time) : rtt(new_rtt), time(rtt_time) {}
  55. const int64_t rtt;
  56. const int64_t time;
  57. };
  58. private:
  59. // Part of the RtcpRttStats implementation. Called by RtcpRttStatsImpl.
  60. void OnRttUpdate(int64_t rtt);
  61. int64_t LastProcessedRttFromProcessThread() const;
  62. void UpdateAndReport();
  63. // This method must only be called when the process thread is not
  64. // running, and from the construction thread.
  65. void UpdateHistograms();
  66. class RtcpRttStatsImpl : public RtcpRttStats {
  67. public:
  68. explicit RtcpRttStatsImpl(CallStats* owner) : owner_(owner) {
  69. process_thread_checker_.Detach();
  70. }
  71. ~RtcpRttStatsImpl() override = default;
  72. private:
  73. void OnRttUpdate(int64_t rtt) override {
  74. RTC_DCHECK_RUN_ON(&process_thread_checker_);
  75. owner_->OnRttUpdate(rtt);
  76. }
  77. int64_t LastProcessedRtt() const override {
  78. RTC_DCHECK_RUN_ON(&process_thread_checker_);
  79. return owner_->LastProcessedRttFromProcessThread();
  80. }
  81. CallStats* const owner_;
  82. SequenceChecker process_thread_checker_;
  83. } rtcp_rtt_stats_impl_{this};
  84. Clock* const clock_;
  85. // Used to regularly call UpdateAndReport().
  86. RepeatingTaskHandle repeating_task_
  87. RTC_GUARDED_BY(construction_thread_checker_);
  88. // The last RTT in the statistics update (zero if there is no valid estimate).
  89. int64_t max_rtt_ms_ RTC_GUARDED_BY(construction_thread_checker_);
  90. // Accessed from two separate threads.
  91. // |avg_rtt_ms_| may be read on the construction thread without a lock.
  92. // |avg_rtt_ms_lock_| must be held elsewhere for reading.
  93. // |avg_rtt_ms_lock_| must be held on the construction thread for writing.
  94. int64_t avg_rtt_ms_;
  95. // Protects |avg_rtt_ms_|.
  96. rtc::CriticalSection avg_rtt_ms_lock_;
  97. // |sum_avg_rtt_ms_|, |num_avg_rtt_| and |time_of_first_rtt_ms_| are only used
  98. // on the ProcessThread when running. When the Process Thread is not running,
  99. // (and only then) they can be used in UpdateHistograms(), usually called from
  100. // the dtor.
  101. int64_t sum_avg_rtt_ms_ RTC_GUARDED_BY(construction_thread_checker_);
  102. int64_t num_avg_rtt_ RTC_GUARDED_BY(construction_thread_checker_);
  103. int64_t time_of_first_rtt_ms_ RTC_GUARDED_BY(construction_thread_checker_);
  104. // All Rtt reports within valid time interval, oldest first.
  105. std::list<RttTime> reports_ RTC_GUARDED_BY(construction_thread_checker_);
  106. // Observers getting stats reports.
  107. // When attached to ProcessThread, this is read-only. In order to allow
  108. // modification, we detach from the process thread while the observer
  109. // list is updated, to avoid races. This allows us to not require a lock
  110. // for the observers_ list, which makes the most common case lock free.
  111. std::list<CallStatsObserver*> observers_;
  112. SequenceChecker construction_thread_checker_;
  113. SequenceChecker process_thread_checker_;
  114. TaskQueueBase* const task_queue_;
  115. // Used to signal destruction to potentially pending tasks.
  116. ScopedTaskSafety task_safety_;
  117. RTC_DISALLOW_COPY_AND_ASSIGN(CallStats);
  118. };
  119. } // namespace internal
  120. } // namespace webrtc
  121. #endif // VIDEO_CALL_STATS2_H_