call_stats.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef VIDEO_CALL_STATS_H_
  11. #define VIDEO_CALL_STATS_H_
  12. #include <list>
  13. #include <memory>
  14. #include "modules/include/module.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/mutex.h"
  19. #include "rtc_base/thread_checker.h"
  20. #include "system_wrappers/include/clock.h"
  21. namespace webrtc {
  22. // CallStats keeps track of statistics for a call.
  23. // TODO(webrtc:11489): Make call_stats_ not depend on ProcessThread and
  24. // make callbacks on the worker thread (TQ).
  25. class CallStats : public Module, public RtcpRttStats {
  26. public:
  27. // Time interval for updating the observers.
  28. static constexpr int64_t kUpdateIntervalMs = 1000;
  29. CallStats(Clock* clock, ProcessThread* process_thread);
  30. ~CallStats() override;
  31. // Registers/deregisters a new observer to receive statistics updates.
  32. // Must be called from the construction thread.
  33. void RegisterStatsObserver(CallStatsObserver* observer);
  34. void DeregisterStatsObserver(CallStatsObserver* observer);
  35. // Expose |LastProcessedRtt()| from RtcpRttStats to the public interface, as
  36. // it is the part of the API that is needed by direct users of CallStats.
  37. // TODO(tommi): Threading or lifetime guarantees are not explicit in how
  38. // CallStats is used as RtcpRttStats or how pointers are cached in a
  39. // few different places (distributed via Call). It would be good to clarify
  40. // from what thread/TQ calls to OnRttUpdate and LastProcessedRtt need to be
  41. // allowed.
  42. int64_t LastProcessedRtt() const override;
  43. // Exposed for tests to test histogram support.
  44. void UpdateHistogramsForTest() { UpdateHistograms(); }
  45. // Helper struct keeping track of the time a rtt value is reported.
  46. struct RttTime {
  47. RttTime(int64_t new_rtt, int64_t rtt_time) : rtt(new_rtt), time(rtt_time) {}
  48. const int64_t rtt;
  49. const int64_t time;
  50. };
  51. private:
  52. // RtcpRttStats implementation.
  53. void OnRttUpdate(int64_t rtt) override;
  54. // Implements Module, to use the process thread.
  55. int64_t TimeUntilNextProcess() override;
  56. void Process() override;
  57. // TODO(tommi): Use this to know when we're attached to the process thread?
  58. // Alternatively, inject that pointer via the ctor since the call_stats
  59. // test code, isn't using a processthread atm.
  60. void ProcessThreadAttached(ProcessThread* process_thread) override;
  61. // This method must only be called when the process thread is not
  62. // running, and from the construction thread.
  63. void UpdateHistograms();
  64. Clock* const clock_;
  65. // The last time 'Process' resulted in statistic update.
  66. int64_t last_process_time_ RTC_GUARDED_BY(process_thread_checker_);
  67. // The last RTT in the statistics update (zero if there is no valid estimate).
  68. int64_t max_rtt_ms_ RTC_GUARDED_BY(process_thread_checker_);
  69. // Accessed from random threads (seemingly). Consider atomic.
  70. // |avg_rtt_ms_| is allowed to be read on the process thread without a lock.
  71. // |avg_rtt_ms_lock_| must be held elsewhere for reading.
  72. // |avg_rtt_ms_lock_| must be held on the process thread for writing.
  73. int64_t avg_rtt_ms_;
  74. // Protects |avg_rtt_ms_|.
  75. mutable Mutex avg_rtt_ms_lock_;
  76. // |sum_avg_rtt_ms_|, |num_avg_rtt_| and |time_of_first_rtt_ms_| are only used
  77. // on the ProcessThread when running. When the Process Thread is not running,
  78. // (and only then) they can be used in UpdateHistograms(), usually called from
  79. // the dtor.
  80. int64_t sum_avg_rtt_ms_ RTC_GUARDED_BY(process_thread_checker_);
  81. int64_t num_avg_rtt_ RTC_GUARDED_BY(process_thread_checker_);
  82. int64_t time_of_first_rtt_ms_ RTC_GUARDED_BY(process_thread_checker_);
  83. // All Rtt reports within valid time interval, oldest first.
  84. std::list<RttTime> reports_ RTC_GUARDED_BY(process_thread_checker_);
  85. // Observers getting stats reports.
  86. // When attached to ProcessThread, this is read-only. In order to allow
  87. // modification, we detach from the process thread while the observer
  88. // list is updated, to avoid races. This allows us to not require a lock
  89. // for the observers_ list, which makes the most common case lock free.
  90. std::list<CallStatsObserver*> observers_;
  91. rtc::ThreadChecker construction_thread_checker_;
  92. rtc::ThreadChecker process_thread_checker_;
  93. ProcessThread* const process_thread_;
  94. bool process_thread_running_ RTC_GUARDED_BY(construction_thread_checker_);
  95. RTC_DISALLOW_COPY_AND_ASSIGN(CallStats);
  96. };
  97. } // namespace webrtc
  98. #endif // VIDEO_CALL_STATS_H_