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