hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 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 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 11 | #ifndef WEBRTC_API_RTCSTATSCOLLECTOR_H_ |
| 12 | #define WEBRTC_API_RTCSTATSCOLLECTOR_H_ |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 13 | |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 14 | #include <map> |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 15 | #include <memory> |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 16 | #include <set> |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 17 | #include <vector> |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 18 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 19 | #include "webrtc/api/datachannel.h" |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 20 | #include "webrtc/api/datachannelinterface.h" |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 21 | #include "webrtc/api/stats/rtcstats_objects.h" |
| 22 | #include "webrtc/api/stats/rtcstatsreport.h" |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 23 | #include "webrtc/base/asyncinvoker.h" |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame^] | 24 | #include "webrtc/base/optional.h" |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 25 | #include "webrtc/base/refcount.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 26 | #include "webrtc/base/scoped_ref_ptr.h" |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 27 | #include "webrtc/base/sigslot.h" |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 28 | #include "webrtc/base/sslidentity.h" |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 29 | #include "webrtc/base/timeutils.h" |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame^] | 30 | #include "webrtc/media/base/mediachannel.h" |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 31 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 32 | namespace cricket { |
| 33 | class Candidate; |
| 34 | } // namespace cricket |
| 35 | |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 36 | namespace rtc { |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 37 | class SSLCertificate; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 38 | } // namespace rtc |
| 39 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 40 | namespace webrtc { |
| 41 | |
| 42 | class PeerConnection; |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 43 | struct SessionStats; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 44 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 45 | class RTCStatsCollectorCallback : public virtual rtc::RefCountInterface { |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 46 | public: |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 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. |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 57 | class RTCStatsCollector : public virtual rtc::RefCountInterface, |
| 58 | public sigslot::has_slots<> { |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 59 | public: |
| 60 | static rtc::scoped_refptr<RTCStatsCollector> Create( |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 61 | PeerConnection* pc, |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 62 | int64_t cache_lifetime_us = 50 * rtc::kNumMicrosecsPerMillisec); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 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. |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 68 | void GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback> callback); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 69 | // Clears the cache's reference to the most recent stats report. Subsequently |
| 70 | // calling |GetStatsReport| guarantees fresh stats. |
| 71 | void ClearCachedStatsReport(); |
| 72 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 73 | protected: |
| 74 | RTCStatsCollector(PeerConnection* pc, int64_t cache_lifetime_us); |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 75 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 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: |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 87 | struct CertificateStatsPair { |
| 88 | std::unique_ptr<rtc::SSLCertificateStats> local; |
| 89 | std::unique_ptr<rtc::SSLCertificateStats> remote; |
| 90 | }; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame^] | 91 | struct MediaInfo { |
| 92 | rtc::Optional<cricket::VoiceMediaInfo> voice; |
| 93 | rtc::Optional<cricket::VideoMediaInfo> video; |
| 94 | }; |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 95 | |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 96 | void AddPartialResults_s(rtc::scoped_refptr<RTCStatsReport> partial_report); |
| 97 | void DeliverCachedReport(); |
| 98 | |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 99 | // Produces |RTCCertificateStats|. |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 100 | void ProduceCertificateStats_s( |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 101 | int64_t timestamp_us, |
| 102 | const std::map<std::string, CertificateStatsPair>& transport_cert_stats, |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 103 | RTCStatsReport* report) const; |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame^] | 104 | // Produces |RTCCodecStats|. |
| 105 | void ProduceCodecStats_s( |
| 106 | int64_t timestamp_us, const MediaInfo& media_info, |
| 107 | RTCStatsReport* report) const; |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 108 | // Produces |RTCDataChannelStats|. |
| 109 | void ProduceDataChannelStats_s( |
| 110 | int64_t timestamp_us, RTCStatsReport* report) const; |
hbos | c47a0c3 | 2016-10-11 14:54:49 -0700 | [diff] [blame] | 111 | // Produces |RTCIceCandidatePairStats| and |RTCIceCandidateStats|. |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 112 | void ProduceIceCandidateAndPairStats_s( |
| 113 | int64_t timestamp_us, const SessionStats& session_stats, |
| 114 | RTCStatsReport* report) const; |
hbos | 09bc128 | 2016-11-08 06:29:22 -0800 | [diff] [blame] | 115 | // Produces |RTCMediaStreamStats| and |RTCMediaStreamTrackStats|. |
| 116 | void ProduceMediaStreamAndTrackStats_s( |
| 117 | int64_t timestamp_us, RTCStatsReport* report) const; |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 118 | // Produces |RTCPeerConnectionStats|. |
hbos | 6ab97ce | 2016-10-03 14:16:56 -0700 | [diff] [blame] | 119 | void ProducePeerConnectionStats_s( |
| 120 | int64_t timestamp_us, RTCStatsReport* report) const; |
hbos | eeafe94 | 2016-11-01 03:00:17 -0700 | [diff] [blame] | 121 | // Produces |RTCInboundRTPStreamStats| and |RTCOutboundRTPStreamStats|. |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 122 | void ProduceRTPStreamStats_s( |
| 123 | int64_t timestamp_us, const SessionStats& session_stats, |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame^] | 124 | const MediaInfo& media_info, RTCStatsReport* report) const; |
hbos | 2fa7c67 | 2016-10-24 04:00:05 -0700 | [diff] [blame] | 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> |
hbos | 0adb828 | 2016-11-23 02:32:06 -0800 | [diff] [blame^] | 133 | PrepareTransportCertificateStats(const SessionStats& session_stats) const; |
| 134 | MediaInfo PrepareMediaInfo(const SessionStats& session_stats) const; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 135 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 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 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 142 | PeerConnection* const pc_; |
hbos | c82f2e1 | 2016-09-05 01:36:50 -0700 | [diff] [blame] | 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 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 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_; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 159 | rtc::scoped_refptr<const RTCStatsReport> cached_report_; |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 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_; |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
hbos | cc555c5 | 2016-10-18 12:48:31 -0700 | [diff] [blame] | 181 | const char* CandidateTypeToRTCIceCandidateTypeForTesting( |
| 182 | const std::string& type); |
| 183 | const char* DataStateToRTCDataChannelStateForTesting( |
| 184 | DataChannelInterface::DataState state); |
hbos | ab9f6e4 | 2016-10-07 02:18:47 -0700 | [diff] [blame] | 185 | |
hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 186 | } // namespace webrtc |
| 187 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 188 | #endif // WEBRTC_API_RTCSTATSCOLLECTOR_H_ |