OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_API_RTCSTATSCOLLECTOR_H_ | 11 #ifndef WEBRTC_API_RTCSTATSCOLLECTOR_H_ |
12 #define WEBRTC_API_RTCSTATSCOLLECTOR_H_ | 12 #define WEBRTC_API_RTCSTATSCOLLECTOR_H_ |
13 | 13 |
14 #include <map> | 14 #include <map> |
15 #include <memory> | 15 #include <memory> |
| 16 #include <set> |
16 #include <vector> | 17 #include <vector> |
17 | 18 |
| 19 #include "webrtc/api/datachannel.h" |
18 #include "webrtc/api/datachannelinterface.h" | 20 #include "webrtc/api/datachannelinterface.h" |
19 #include "webrtc/api/stats/rtcstats_objects.h" | 21 #include "webrtc/api/stats/rtcstats_objects.h" |
20 #include "webrtc/api/stats/rtcstatsreport.h" | 22 #include "webrtc/api/stats/rtcstatsreport.h" |
21 #include "webrtc/base/asyncinvoker.h" | 23 #include "webrtc/base/asyncinvoker.h" |
22 #include "webrtc/base/refcount.h" | 24 #include "webrtc/base/refcount.h" |
23 #include "webrtc/base/scoped_ref_ptr.h" | 25 #include "webrtc/base/scoped_ref_ptr.h" |
| 26 #include "webrtc/base/sigslot.h" |
24 #include "webrtc/base/sslidentity.h" | 27 #include "webrtc/base/sslidentity.h" |
25 #include "webrtc/base/timeutils.h" | 28 #include "webrtc/base/timeutils.h" |
26 | 29 |
27 namespace cricket { | 30 namespace cricket { |
28 class Candidate; | 31 class Candidate; |
29 } // namespace cricket | 32 } // namespace cricket |
30 | 33 |
31 namespace rtc { | 34 namespace rtc { |
32 class SSLCertificate; | 35 class SSLCertificate; |
33 } // namespace rtc | 36 } // namespace rtc |
34 | 37 |
35 namespace webrtc { | 38 namespace webrtc { |
36 | 39 |
37 class PeerConnection; | 40 class PeerConnection; |
38 struct SessionStats; | 41 struct SessionStats; |
39 | 42 |
40 class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface { | 43 class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface { |
41 public: | 44 public: |
42 virtual ~RTCStatsCollectorCallback() {} | 45 virtual ~RTCStatsCollectorCallback() {} |
43 | 46 |
44 virtual void OnStatsDelivered( | 47 virtual void OnStatsDelivered( |
45 const rtc::scoped_refptr<const RTCStatsReport>& report) = 0; | 48 const rtc::scoped_refptr<const RTCStatsReport>& report) = 0; |
46 }; | 49 }; |
47 | 50 |
48 // All public methods of the collector are to be called on the signaling thread. | 51 // All public methods of the collector are to be called on the signaling thread. |
49 // Stats are gathered on the signaling, worker and network threads | 52 // Stats are gathered on the signaling, worker and network threads |
50 // asynchronously. The callback is invoked on the signaling thread. Resulting | 53 // asynchronously. The callback is invoked on the signaling thread. Resulting |
51 // reports are cached for |cache_lifetime_| ms. | 54 // reports are cached for |cache_lifetime_| ms. |
52 class RTCStatsCollector : public virtual rtc::RefCountInterface { | 55 class RTCStatsCollector : public virtual rtc::RefCountInterface, |
| 56 public sigslot::has_slots<> { |
53 public: | 57 public: |
54 static rtc::scoped_refptr<RTCStatsCollector> Create( | 58 static rtc::scoped_refptr<RTCStatsCollector> Create( |
55 PeerConnection* pc, | 59 PeerConnection* pc, |
56 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec); | 60 int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec); |
57 | 61 |
58 // Gets a recent stats report. If there is a report cached that is still fresh | 62 // Gets a recent stats report. If there is a report cached that is still fresh |
59 // it is returned, otherwise new stats are gathered and returned. A report is | 63 // it is returned, otherwise new stats are gathered and returned. A report is |
60 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe | 64 // considered fresh for |cache_lifetime_| ms. const RTCStatsReports are safe |
61 // to use across multiple threads and may be destructed on any thread. | 65 // to use across multiple threads and may be destructed on any thread. |
62 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback); | 66 void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback); |
63 // Clears the cache's reference to the most recent stats report. Subsequently | 67 // Clears the cache's reference to the most recent stats report. Subsequently |
64 // calling |GetStatsReport| guarantees fresh stats. | 68 // calling |GetStatsReport| guarantees fresh stats. |
65 void ClearCachedStatsReport(); | 69 void ClearCachedStatsReport(); |
66 | 70 |
| 71 // Exposed for testing only. |
| 72 void OnDataChannelOpenedForTesting(DataChannel* channel); |
| 73 void OnDataChannelClosedForTesting(DataChannel* channel); |
| 74 |
67 protected: | 75 protected: |
68 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us); | 76 RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us); |
69 | 77 |
70 // Stats gathering on a particular thread. Calls |AddPartialResults| before | 78 // Stats gathering on a particular thread. Calls |AddPartialResults| before |
71 // returning. Virtual for the sake of testing. | 79 // returning. Virtual for the sake of testing. |
72 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us); | 80 virtual void ProducePartialResultsOnSignalingThread(int64_t timestamp_us); |
73 virtual void ProducePartialResultsOnWorkerThread(int64_t timestamp_us); | 81 virtual void ProducePartialResultsOnWorkerThread(int64_t timestamp_us); |
74 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us); | 82 virtual void ProducePartialResultsOnNetworkThread(int64_t timestamp_us); |
75 | 83 |
76 // Can be called on any thread. | 84 // Can be called on any thread. |
77 void AddPartialResults( | 85 void AddPartialResults( |
78 const rtc::scoped_refptr<RTCStatsReport>& partial_report); | 86 const rtc::scoped_refptr<RTCStatsReport>& partial_report); |
79 | 87 |
80 private: | 88 private: |
| 89 // To allow |pc_| to wire up RTCStatsCollector::OnBlahBlah signal slots. |
| 90 friend class PeerConnection; |
| 91 |
81 struct CertificateStatsPair { | 92 struct CertificateStatsPair { |
82 std::unique_ptr<rtc::SSLCertificateStats> local; | 93 std::unique_ptr<rtc::SSLCertificateStats> local; |
83 std::unique_ptr<rtc::SSLCertificateStats> remote; | 94 std::unique_ptr<rtc::SSLCertificateStats> remote; |
84 }; | 95 }; |
85 | 96 |
86 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report); | 97 void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report); |
87 void DeliverCachedReport(); | 98 void DeliverCachedReport(); |
88 | 99 |
89 // Produces |RTCCertificateStats|. | 100 // Produces |RTCCertificateStats|. |
90 void ProduceCertificateStats_s( | 101 void ProduceCertificateStats_s( |
(...skipping 17 matching lines...) Expand all Loading... |
108 // Produces |RTCTransportStats|. | 119 // Produces |RTCTransportStats|. |
109 void ProduceTransportStats_s( | 120 void ProduceTransportStats_s( |
110 int64_t timestamp_us, const SessionStats& session_stats, | 121 int64_t timestamp_us, const SessionStats& session_stats, |
111 const std::map<std::string, CertificateStatsPair>& transport_cert_stats, | 122 const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
112 RTCStatsReport* report) const; | 123 RTCStatsReport* report) const; |
113 | 124 |
114 // Helper function to stats-producing functions. | 125 // Helper function to stats-producing functions. |
115 std::map<std::string, CertificateStatsPair> | 126 std::map<std::string, CertificateStatsPair> |
116 PrepareTransportCertificateStats_s(const SessionStats& session_stats) const; | 127 PrepareTransportCertificateStats_s(const SessionStats& session_stats) const; |
117 | 128 |
| 129 // Slots for signals (sigslot). |
| 130 void OnDataChannelOpened(DataChannel* channel); |
| 131 void OnDataChannelClosed(DataChannel* channel); |
| 132 |
118 PeerConnection* const pc_; | 133 PeerConnection* const pc_; |
119 rtc::Thread* const signaling_thread_; | 134 rtc::Thread* const signaling_thread_; |
120 rtc::Thread* const worker_thread_; | 135 rtc::Thread* const worker_thread_; |
121 rtc::Thread* const network_thread_; | 136 rtc::Thread* const network_thread_; |
122 rtc::AsyncInvoker invoker_; | 137 rtc::AsyncInvoker invoker_; |
123 | 138 |
124 int num_pending_partial_reports_; | 139 int num_pending_partial_reports_; |
125 int64_t partial_report_timestamp_us_; | 140 int64_t partial_report_timestamp_us_; |
126 rtc::scoped_refptr<RTCStatsReport> partial_report_; | 141 rtc::scoped_refptr<RTCStatsReport> partial_report_; |
127 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_; | 142 std::vector<rtc::scoped_refptr<RTCStatsCollectorCallback>> callbacks_; |
128 | 143 |
129 // A timestamp, in microseconds, that is based on a timer that is | 144 // A timestamp, in microseconds, that is based on a timer that is |
130 // monotonically increasing. That is, even if the system clock is modified the | 145 // monotonically increasing. That is, even if the system clock is modified the |
131 // difference between the timer and this timestamp is how fresh the cached | 146 // difference between the timer and this timestamp is how fresh the cached |
132 // report is. | 147 // report is. |
133 int64_t cache_timestamp_us_; | 148 int64_t cache_timestamp_us_; |
134 int64_t cache_lifetime_us_; | 149 int64_t cache_lifetime_us_; |
135 rtc::scoped_refptr<const RTCStatsReport> cached_report_; | 150 rtc::scoped_refptr<const RTCStatsReport> cached_report_; |
| 151 |
| 152 // Data recorded and maintained by the stats collector during its lifetime. |
| 153 // Some stats are produced from this record instead of other components. |
| 154 struct InternalRecord { |
| 155 InternalRecord() : data_channels_opened(0), |
| 156 data_channels_closed(0) {} |
| 157 |
| 158 // The opened count goes up when a channel is fully opened and the closed |
| 159 // count goes up if a previously opened channel has fully closed. The opened |
| 160 // count does not go down when a channel closes, meaning (opened - closed) |
| 161 // is the number of channels currently opened. A channel that is closed |
| 162 // before reaching the open state does not affect these counters. |
| 163 uint32_t data_channels_opened; |
| 164 uint32_t data_channels_closed; |
| 165 // Identifies by address channels that have been opened, which remain in the |
| 166 // set until they have been fully closed. |
| 167 std::set<uintptr_t> opened_data_channels; |
| 168 }; |
| 169 InternalRecord internal_record_; |
136 }; | 170 }; |
137 | 171 |
138 const char* CandidateTypeToRTCIceCandidateTypeForTesting( | 172 const char* CandidateTypeToRTCIceCandidateTypeForTesting( |
139 const std::string& type); | 173 const std::string& type); |
140 const char* DataStateToRTCDataChannelStateForTesting( | 174 const char* DataStateToRTCDataChannelStateForTesting( |
141 DataChannelInterface::DataState state); | 175 DataChannelInterface::DataState state); |
142 | 176 |
143 } // namespace webrtc | 177 } // namespace webrtc |
144 | 178 |
145 #endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_ | 179 #endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_ |
OLD | NEW |