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