call_stats2.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/synchronization/sequence_checker.h"
  19. #include "rtc_base/system/no_unique_address.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. void UpdateAndReport();
  62. // This method must only be called when the process thread is not
  63. // running, and from the construction thread.
  64. void UpdateHistograms();
  65. class RtcpRttStatsImpl : public RtcpRttStats {
  66. public:
  67. explicit RtcpRttStatsImpl(CallStats* owner) : owner_(owner) {}
  68. ~RtcpRttStatsImpl() override = default;
  69. private:
  70. void OnRttUpdate(int64_t rtt) override {
  71. // For video send streams (video/video_send_stream.cc), the RtpRtcp module
  72. // is currently created on a transport worker TaskQueue and not the worker
  73. // thread - which is what happens in other cases. We should probably fix
  74. // that so that the call consistently comes in on the right thread.
  75. owner_->OnRttUpdate(rtt);
  76. }
  77. int64_t LastProcessedRtt() const override {
  78. // This call path shouldn't be used anymore. This impl is only for
  79. // propagating the rtt from the RtpRtcp module, which does not call
  80. // LastProcessedRtt(). Down the line we should consider removing
  81. // LastProcessedRtt() and use the interface for event notifications only.
  82. RTC_NOTREACHED() << "Legacy call path";
  83. return 0;
  84. }
  85. CallStats* const owner_;
  86. } rtcp_rtt_stats_impl_{this};
  87. Clock* const clock_;
  88. // Used to regularly call UpdateAndReport().
  89. RepeatingTaskHandle repeating_task_
  90. RTC_GUARDED_BY(construction_thread_checker_);
  91. // The last RTT in the statistics update (zero if there is no valid estimate).
  92. int64_t max_rtt_ms_ RTC_GUARDED_BY(construction_thread_checker_);
  93. // Last reported average RTT value.
  94. int64_t avg_rtt_ms_ RTC_GUARDED_BY(construction_thread_checker_);
  95. // |sum_avg_rtt_ms_|, |num_avg_rtt_| and |time_of_first_rtt_ms_| are only used
  96. // on the ProcessThread when running. When the Process Thread is not running,
  97. // (and only then) they can be used in UpdateHistograms(), usually called from
  98. // the dtor.
  99. int64_t sum_avg_rtt_ms_ RTC_GUARDED_BY(construction_thread_checker_);
  100. int64_t num_avg_rtt_ RTC_GUARDED_BY(construction_thread_checker_);
  101. int64_t time_of_first_rtt_ms_ RTC_GUARDED_BY(construction_thread_checker_);
  102. // All Rtt reports within valid time interval, oldest first.
  103. std::list<RttTime> reports_ RTC_GUARDED_BY(construction_thread_checker_);
  104. // Observers getting stats reports.
  105. // When attached to ProcessThread, this is read-only. In order to allow
  106. // modification, we detach from the process thread while the observer
  107. // list is updated, to avoid races. This allows us to not require a lock
  108. // for the observers_ list, which makes the most common case lock free.
  109. std::list<CallStatsObserver*> observers_;
  110. RTC_NO_UNIQUE_ADDRESS SequenceChecker construction_thread_checker_;
  111. RTC_NO_UNIQUE_ADDRESS SequenceChecker process_thread_checker_;
  112. TaskQueueBase* const task_queue_;
  113. // Used to signal destruction to potentially pending tasks.
  114. ScopedTaskSafety task_safety_;
  115. RTC_DISALLOW_COPY_AND_ASSIGN(CallStats);
  116. };
  117. } // namespace internal
  118. } // namespace webrtc
  119. #endif // VIDEO_CALL_STATS2_H_