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