mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef VIDEO_CALL_STATS_H_ |
| 12 | #define VIDEO_CALL_STATS_H_ |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
kwiberg | 27f982b | 2016-03-01 11:52:33 -0800 | [diff] [blame] | 15 | #include <memory> |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/include/module.h" |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 18 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/constructor_magic.h" |
| 20 | #include "rtc_base/critical_section.h" |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 21 | #include "rtc_base/thread_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "system_wrappers/include/clock.h" |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
fischman@webrtc.org | aea96d3 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 26 | class CallStatsObserver; |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 27 | |
| 28 | // CallStats keeps track of statistics for a call. |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 29 | class CallStats : public Module, public RtcpRttStats { |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 30 | public: |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 31 | // Time interval for updating the observers. |
| 32 | static constexpr int64_t kUpdateIntervalMs = 1000; |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 33 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 34 | CallStats(Clock* clock, ProcessThread* process_thread); |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 35 | ~CallStats() override; |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 36 | |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 37 | // Registers/deregisters a new observer to receive statistics updates. |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 38 | // Must be called from the construction thread. |
fischman@webrtc.org | aea96d3 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 39 | void RegisterStatsObserver(CallStatsObserver* observer); |
| 40 | void DeregisterStatsObserver(CallStatsObserver* observer); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 41 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 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 override; |
| 50 | |
| 51 | // Exposed for tests to test histogram support. |
| 52 | void UpdateHistogramsForTest() { UpdateHistograms(); } |
| 53 | |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 54 | // Helper struct keeping track of the time a rtt value is reported. |
| 55 | struct RttTime { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 56 | RttTime(int64_t new_rtt, int64_t rtt_time) : rtt(new_rtt), time(rtt_time) {} |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 57 | const int64_t rtt; |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 58 | const int64_t time; |
| 59 | }; |
| 60 | |
asapersson@webrtc.org | 8084f95 | 2014-12-10 11:04:13 +0000 | [diff] [blame] | 61 | private: |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 62 | // RtcpRttStats implementation. |
| 63 | void OnRttUpdate(int64_t rtt) override; |
| 64 | |
| 65 | // Implements Module, to use the process thread. |
| 66 | int64_t TimeUntilNextProcess() override; |
| 67 | void Process() override; |
| 68 | |
| 69 | // TODO(tommi): Use this to know when we're attached to the process thread? |
| 70 | // Alternatively, inject that pointer via the ctor since the call_stats |
| 71 | // test code, isn't using a processthread atm. |
| 72 | void ProcessThreadAttached(ProcessThread* process_thread) override; |
| 73 | |
| 74 | // This method must only be called when the process thread is not |
| 75 | // running, and from the construction thread. |
sprang | e2d83d6 | 2016-02-19 09:03:26 -0800 | [diff] [blame] | 76 | void UpdateHistograms(); |
| 77 | |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 78 | Clock* const clock_; |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 79 | |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 80 | // The last time 'Process' resulted in statistic update. |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 81 | int64_t last_process_time_ RTC_GUARDED_BY(process_thread_checker_); |
asapersson@webrtc.org | 1ae1d0c | 2013-11-20 12:46:11 +0000 | [diff] [blame] | 82 | // The last RTT in the statistics update (zero if there is no valid estimate). |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 83 | int64_t max_rtt_ms_ RTC_GUARDED_BY(process_thread_checker_); |
| 84 | |
| 85 | // Accessed from random threads (seemingly). Consider atomic. |
| 86 | // |avg_rtt_ms_| is allowed to be read on the process thread without a lock. |
| 87 | // |avg_rtt_ms_lock_| must be held elsewhere for reading. |
| 88 | // |avg_rtt_ms_lock_| must be held on the process thread for writing. |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 89 | int64_t avg_rtt_ms_; |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 90 | |
| 91 | // Protects |avg_rtt_ms_|. |
| 92 | rtc::CriticalSection avg_rtt_ms_lock_; |
| 93 | |
| 94 | // |sum_avg_rtt_ms_|, |num_avg_rtt_| and |time_of_first_rtt_ms_| are only used |
| 95 | // on the ProcessThread when running. When the Process Thread is not running, |
| 96 | // (and only then) they can be used in UpdateHistograms(), usually called from |
| 97 | // the dtor. |
| 98 | int64_t sum_avg_rtt_ms_ RTC_GUARDED_BY(process_thread_checker_); |
| 99 | int64_t num_avg_rtt_ RTC_GUARDED_BY(process_thread_checker_); |
| 100 | int64_t time_of_first_rtt_ms_ RTC_GUARDED_BY(process_thread_checker_); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 101 | |
| 102 | // All Rtt reports within valid time interval, oldest first. |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 103 | std::list<RttTime> reports_ RTC_GUARDED_BY(process_thread_checker_); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 104 | |
| 105 | // Observers getting stats reports. |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 106 | // When attached to ProcessThread, this is read-only. In order to allow |
| 107 | // modification, we detach from the process thread while the observer |
| 108 | // list is updated, to avoid races. This allows us to not require a lock |
| 109 | // for the observers_ list, which makes the most common case lock free. |
fischman@webrtc.org | aea96d3 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 110 | std::list<CallStatsObserver*> observers_; |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 111 | |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 112 | rtc::ThreadChecker construction_thread_checker_; |
| 113 | rtc::ThreadChecker process_thread_checker_; |
| 114 | ProcessThread* const process_thread_; |
| 115 | bool process_thread_running_ RTC_GUARDED_BY(construction_thread_checker_); |
| 116 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 117 | RTC_DISALLOW_COPY_AND_ASSIGN(CallStats); |
mflodman@webrtc.org | b2f474e | 2012-11-16 13:57:26 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // namespace webrtc |
| 121 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 122 | #endif // VIDEO_CALL_STATS_H_ |